简体   繁体   English

如何在jQuery中切换多个图像?

[英]How to toggle multiple images in jquery?

HTML 的HTML

<div class="image_rollover">
     <img id="image_one" src=image_one.png">
     <img id="image_two" src="image_two.png">
     <img id="image_three" src="image_three.png">
     <img id="image_four" src="image_four.png">
     <img id="image_five" src="image_five.png">
     <img id="image_six" src="image_six.png">
</div>

CSS 的CSS

.image_rollover{
    border:1px solid #000000;
    width:130px;
    height:80px;
    overflow:hidden;
}

Script 脚本

$(document).ready(function(){
    $("#image_two, #image_three, #image_four, #image_five, #image_six").hide();

    $(".image_rollover").hover(function(){
      $(this).find("#image_one, #image_two, #image_three, #image_four, #image_five, #image_six").toggle();
    });
});

When I hover mouse over the div, first image changes to second image and then nothing happens. 当我将鼠标悬停在div上时,第一个图像变为第二个图像,然后什么也没有发生。 When I hover mouse over the div, I want to change the image one by one from "image_one" to "image_six". 当我将鼠标悬停在div上时,我想将图像从“ image_one”一幅更改为“ image_six”。

Does any one know how to do this??? 有谁知道如何做到这一点???

Note that will be better if you hide your images by css and if you simplify the selector to get all images inside the div (or group those images with a class). 请注意,如果通过css隐藏图像,并且简化了选择器以将所有图像都包含在div中(或将这些图像与一个类分组),效果会更好。

Check out this: 看看这个:

JS JS

$(document).ready(function(){
  $(".image_rollover").hover(function(){
    $(this).find("img").toggle();
  });
});

CSS 的CSS

.image_rollover{
  border:1px solid #000000;
  width:130px;
  height:80px;
  overflow: hidden;
}

.image_rollover img{
   display: none;
}

HTML 的HTML

<div class="image_rollover">
  <img id="image_one" src=image_one.png">
  <img id="image_two" src="image_two.png">
  <img id="image_three" src="image_three.png">
  <img id="image_four" src="image_four.png">
  <img id="image_five" src="image_five.png">
  <img id="image_six" src="image_six.png">
</div>

Check out this codepen . 出此codepen

$(document).ready(function(){
    $('.image_rollover img').hide();
    var index = 0;
    var $imageRollover = $('.image_rollover');
    var maxIndex = $imageRollover.find('img').length;
    $imageRollover.on('mouseenter', function(){
        $('.image_rollover img').hide();        
        console.log(index);
        $imageRollover.find('img').eq(index).show();
        index++;
        if (index >= maxIndex) {
            index = 0;
        }
    });
});

You can also see http://jsfiddle.net/2q2ycbdz/2/ 您还可以看到http://jsfiddle.net/2q2ycbdz/2/

http://jsfiddle.net/p7dsm1h7/ http://jsfiddle.net/p7dsm1h7/

$(document).ready(function () {
    $("#image_two, #image_three, #image_four, #image_five, #image_six").hide();

    $(".image_rollover").hover(function () {
       animation()
    });
});

function animation() {
    var $curr=$(".image_rollover img:visible");
    var $next=$curr.next();    

    if($next.size()==0) $next=$(".image_rollover img:first");

    $next.show();
    $curr.hide();
}

or maybe something like this 也许像这样

UPDATE 更新

http://jsfiddle.net/9v8ykfjs/2/ http://jsfiddle.net/9v8ykfjs/2/

var hover=false;
var interval;

$(document).ready(function () {
    $("#image_two, #image_three, #image_four, #image_five, #image_six").hide();
    $(".image_rollover").hover(function () {
       hover=true;
       animation()
    },function(){
        hover=false;
        clearTimeout(interval);
        $(".image_rollover img:visible").hide();
        $(".image_rollover img:first").show();
    });
});

function animation() {

    if(hover==false) return;

    var $curr=$(".image_rollover img:visible");
    var $next=$curr.next();    

    if($next.size()==0) $next=$(".image_rollover img:first");

    $next.show();
    $curr.hide();

    interval=setTimeout(function(){ animation(); }, 1000);
}

If you are looking with some animation. 如果您正在寻找一些动画。 Try this. 尝试这个。

      $(document).ready(function () {

            var $siblings = $("#image_one").siblings();
            $siblings.hide();

            $("#image_one").on("mouseenter", function () {
                var timer = 100;
                var showTime = 0;
                $siblings.each(function () {
                    showTime += timer;
                    $(this).fadeIn(showTime).show()
                })
            });
        });

Or If you wants a chain kind of animation try this: 或者,如果您想要链式动画,请尝试以下操作:

So if you hover on first image, then second will appear, then if on second then third and so on.. is it what you are looking for?? 因此,如果您将鼠标悬停在第一张图像上,则将出现第二张图像,然后如果出现在第二张图像上,则出现第三张图像,依此类推。等等。这是您要查找的内容吗?

        $(document).ready(function () {

            var $siblings = $("#image_one").siblings();
            $siblings.hide();
            var timer = 500;
            var showTime = 0;
            function init_chain(ev){
                showTime += timer;
                var $next_element = $(ev.target).next()
                $next_element.fadeIn(showTime).show();
                $next_element.bind("mouseenter",init_chain);
            };

            $("#image_one").on("mouseenter", function (event) {
                init_chain(event);
            });
        });

Let us know. 让我们知道

Thanks! 谢谢!

JS JS

jQuery(document).ready(function(){
    var current_image = 1;
    jQuery('.image_rollover').hover(function(){
        jQuery('.image_rollover img').hide();
        jQuery('.image_rollover img:nth-child('+current_image+')').show();
        current_image = current_image + 1;
        if (current_image == 7)
            current_image = 1;
    },function(){});
});

jsFiddle demo jsFiddle演示

$('.image_rollover').hover(function(){
    $("img:first", this).appendTo(this);
});

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

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