简体   繁体   English

为什么在使用JQuery时这些图像无法循环显示?

[英]Why are these images not cycling through while using JQuery?

I am trying to cycle through 3 images, stacked on top of one another. 我正在尝试浏览3张彼此重叠的图像。 The jQuery I am using does not have any effect on the images and I cannot see what the problem is. 我正在使用的jQuery对图像没有任何影响,并且我看不到问题出在哪里。 I'd really love some help! 我真的很想帮忙! Cheers!! 干杯!!

HTML: HTML:

        <div id="cycler">
        <img class="active" src="images/dubcity4.jpg" alt="my image"/>
        <img src="images/dubcity2.jpg" alt="my image"/>
        <img src="images/dubcity3.jpg" alt="my image"/>
        </div>

CSS: CSS:

      #cycler{position:relative;}
      #cycler img{position:absolute;z-index:1}
      #cycler img.active{z-index:3}

JavaScript: JavaScript:

function cycleImages(){
    var $active = $('#cycler.active');
    var $next = ($active.next().length > 0) ? $active.next() : $('#cycler img:first');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function(){
        $active.css('z-index',1).show().removeClass('active');
        $next.css('z-index',3).addClass('active');
    });
}
$(document).ready(function(){
    setInterval('cycleImages()',2000);
})

Change this: 更改此:

var $active = $('#cycler.active');

to that: 对此:

var $active = $('#cycler .active');

in your code you are trying to find element with id="cycler" AND class="active" which is wrong 在您的代码中,您尝试查找id =“ cycler” AND class =“ active”的元素,这是错误的

Also try to pass function to a setInterval instead of a string 还尝试将函数传递给setInterval而不是字符串

setInterval(cycleImages,2000);

See jSfiddle http://jsfiddle.net/ygV3z/ 参见jSfiddle http://jsfiddle.net/ygV3z/

For sure this is wrong: $('#cycler.active'); 可以肯定的是,这是错误的: $('#cycler.active');

You should have $('#cycler .active'); 您应该有$('#cycler .active'); since .active is descendant of #cycler . 因为.active为后代#cycler Your selector is looking for a element with id = cycler that also have active class assigned. 您的选择器正在寻找一个ID = cycler的元素,该元素也已分配了active类。

You have made a typo in your CSS selector. 您在CSS选择器中输入了错误。 Just fix it by changing this: 只需通过更改它来修复它:

var $active = $('#cycler.active');

into the following: 分为以下内容:

var $active = $('#cycler .active');

JQuery has an each method that could help you cycle through DOM elements: JQuery具有each方法,可以帮助您循环访问DOM元素:

$("#cycler img").each(function(key, obj) {
   // fetch the arguments passed
   var args = arguments;
   // get a jquery accessible form of each object
   var obj = $(obj);
   // do stuff
}); 

You can match on names or classes, or even the attributes if they are the same. 您可以匹配名称或类,甚至可以匹配属性(如果它们相同)。 I'm sure better skilled folks could tell you if 1 is faster than the other, but the notation for attributes would be $("#cycler [data-pick='true']") to match each image within #cycler that is defined as follows: <img src="..." data-pick="true" /> . 我敢肯定,技术娴熟的人会告诉您1是否比另一个更快,但是属性的符号应为$("#cycler [data-pick='true']")以匹配#cycler中的每个图像定义如下: <img src="..." data-pick="true" />

Hope this helps. 希望这可以帮助。

As everyone here has said, you need a space between your CSS selectors to denote that you want descendants. 就像这里的每个人都说的那样,您需要在CSS选择器之间留一个空格,以表示您想要后代。 Also, you need to use :first-child , not :first . 另外,您需要使用:first-child ,而不是:first

Also, you can not pass a string into setTimeout() . 同样,您不能将字符串传递给setTimeout() Furthermore, setTimeout() expects an actual callback function in its first argument, not what that function returns . 此外, setTimeout()期望在其第一个参数中使用实际的回调函数而不是该函数返回的值 What you're doing is very similar to this: 您正在执行的操作与此非常相似:

function looper() {
    $(this).hide();
}
$("img").each("looper()");

That doesn't make any sense, does it? 那没有任何意义,是吗?

Now, the fixed code: 现在,固定代码:

function cycleImages(){
    var $active = $('#cycler .active');
    var $next = ($active.next().length > 0) ? $active.next() : $('#cycler img:first-child');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function(){
        $active.css('z-index',1).show().removeClass('active');
        $next.css('z-index',3).addClass('active');
    });
}
$(document).ready(function(){
    setInterval(cycleImages,2000);
})

Here's the fiddle: http://jsfiddle.net/NobleMushtak/L4D7r/ 这是小提琴: http : //jsfiddle.net/NobleMushtak/L4D7r/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM