【问题标题】:JQuery Image Slider Using "Cycle Plugin": Why Won't My Images "Cycle"?使用“循环插件”的 JQuery 图像滑块:为什么我的图像不能“循环”?
【发布时间】:2014-07-26 00:27:44
【问题描述】:

我已经构建了一个图像滑块,旨在通过淡入和淡出在四个图像之间进行转换。我使用了 J Query 插件“循环”并将效果设置为“淡出”。我还希望让用户能够通过将光标悬停在图像上来暂停幻灯片放映,并使用位于右上角的标准“下一个”、“上一个”按钮来控制流程。

代码如下:

全屏结果: http://jsfiddle.net/StacksOnStacksOverHoes/spL4W/3/embedded/result/

JS 小提琴编辑: http://jsfiddle.net/StacksOnStacksOverHoes/spL4W/3/

HTML:

    <!DOCTYPE html>
<title>slider</title>
<!-- Link to Google Jquery API -->
<!-- Link to Local Jquery-->
<script src="js/jquery-1.11.1.js"></script>
<script src="js/jquery.cycle.all"></script>
</head>
<body>
    <div id="hero">
        <div id="pager"></div>
        <div id="pause"></div>
        <div id="play"></div>
        <div id="next"></div>
        <div id="prev"></div>
        <div id="slider_01">
            <div class="items">
                <div class="image_01"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_02"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_03"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_04"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
        </div>
        <!--End of slider_01--> <a href="#">Previous</a>
 <a href="#">Next</a>

</body>
</html>

CSS:

#hero {
    width: 960px;
    height: 500px;
    display: block;
    position: relative;
    margin: auto;
}
#slider_01 {
    width: 960px;
    height: 500px;
    display: block;
    position: absolute;
    margin: auto;
    overflow: hidden;
}
.info {
    width: 90%;
    height: 80px;
    display: block;
    position: relative;
    bottom: 100px;
    border: 1px solid black;
    background: rgba(102, 204, 204, .2);
    z-index: 99;
}
.info h2 {
    font-family: sans-serif;
    font-size: 20px;
    font-weight: bold;
    color: #FFF;
    padding: 15px 0 0 20px;
    line-height: 1px;
}
.info p {
    font-family: sans-serif;
    font-size: 15px;
    font-weight: lighter;
    color: #FFF;
    padding: 0px 0px 0px 20px;
    line-height: 1px;
}
#play {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: block;
    position: absolute;
    top: 0px;
    right: 44px;
    background: rgba(102, 204, 0, .2);
    color: #FFF;
    cursor: pointer;
    z-index: 999;
}
#pause {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: block;
    position: absolute;
    top: 0px;
    right: 88px;
    background: rgba(102, 204, 0, .5);
    color: #FFF;
    cursor: pointer;
    z-index: 999;
}
#next {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: block;
    position: absolute;
    top: 0px;
    right: 0px;
    background: rgba(102, 204, 0, .5);
    color: #FFF;
    cursor: pointer;
    z-index: 999;
}
#prev {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: block;
    position: absolute;
    top: 0px;
    right: 133px;
    background: rgba(102, 204, 0, .5);
    color: #FFF;
    cursor: pointer;
    z-index: 999;
}
#pager {
    width: 100%;
    text-align: center;
    display: block;
    position: absolute;
    cursor: pointer;
    z-index: 999;
}
#pager a {
    width: 10px;
    height: 10px;
    display: inline-block;
    position: relative;
    border: 1px solid #FFF;
    border-radius: 20px;
    background-color: #FFF;
}
.image_01 {
    background-color: blue;
    width: 960px;
    height: 500px;
}
.image_02 {
    background-color: red;
    width: 960px;
    height: 500px;
}
.image_01 {
    background-color: green;
    width: 960px;
    height: 500px;
}
.image_01 {
    background-color: yellow;
    width: 960px;
    height: 500px;
}

J 查询:

$('#slider_01').cycle({
    fx: 'fade',
    next: '#next',
    prev: '#prev',
    pager: '#pager',
    speed: 900,
    timeout: 3000,
    pause: 1,
});

目前,尽管所有组件似乎都在那里,但我的代码实际上还没有做任何事情。我错过了什么?

【问题讨论】:

  • 你没有在Jsfiddle中导入Cycle插件,也没有激活jquery

标签: javascript jquery html css


【解决方案1】:

一些事情:

  • 您没有在 Jsfiddle 中导入 Cycle 插件(外部资源),也没有激活 jquery(框架和扩展)
  • jsfiddle添加html时,只添加相关的html,去掉head、body标签
  • 在您的 javascript 中,指定您希望插件在 Document Ready 上运行

HTML:

<div id="hero">
        <div id="pager"></div>
        <div id="pause"></div>
        <div id="play"></div>
        <div id="next"></div>
        <div id="prev"></div>
        <div id="slider_01">
            <div class="items">
                <div class="image_01"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_02"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_03"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
            <div class="items">
                <div class="image_04"></div>
                <div class="info">
                     <h2>Hello World</h2>

                    <p>Hello world. <a href="#">Learn More</a>

                    </p>
                </div>
                <!--End of Info-->
            </div>
            <!--End of Items-->
        </div>
        <!--End of slider_01--> <a href="#">Previous</a>
 <a href="#">Next</a>

Javascript/JQuery:

//Document ready
$(function(){
    $('#slider_01').cycle({
        fx: 'fade',
        next: '#next',
        prev: '#prev',
        pager: '#pager',
        speed: 900,
        timeout: 3000,
        pause: 1,
    });
});

Working example

【讨论】:

  • 当我在 JS fiddle 之外的任何地方运行它时,我的代码仍然无法正常工作。所以我一定误解了你所说的关于我原来的 JQuery 脚本的错误。你是说添加“$(document).ready(function(){”作为我的第一行 JS 吗?
  • 检查浏览器的 Web 开发工具控制台,看看是否有任何错误。有些东西可能没有加载。告诉我你看到了什么
  • 是的,为了让插件在页面加载时自行激活,您必须将 .cycle() 调用包装在 Document ready function '$(function(){ //添加 .cycle()代码});'
  • 我发现了这个错误信息:(Uncaught ReferenceError: $ is not defined)
  • 我发现了这个错误信息:(Uncaught ReferenceError: $ is not defined) 尽管我现在已经添加了“.ready”函数,但我还是发现了这个。
【解决方案2】:

工作小提琴:

http://jsfiddle.net/spL4W/8/

你没有使用 fiddle 导入,你自己编写导入,这不是 fiddle 的工作方式。

$('#slider_01').cycle({
    fx: 'fade',
    next: '#next',
    prev: '#prev',
    pager: '#pager',
    speed: 900,
    timeout: 3000,
    pause: 1,
});

工作得很好。
供参考 jsfiddle tutorial

【讨论】:

    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2021-03-22
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多