简体   繁体   English

使用Jquery反复悬停后更改悬停图像

[英]Changing hover image after repeated hovers using Jquery

I am trying to change the src attribute in an img tag in html using jquery, I want scr attribute to change differently depending on the number of times the user has hovered over the img element. 我正在尝试使用jquery更改html中img标记中的src属性,我希望scr属性根据用户将鼠标悬停在img元素上的次数而有所不同。

My html is set up as follows: 我的html设置如下:

<img id="element" src="img1.png">

My js/jQuery is set up as follows: 我的js / jQuery设置如下:

var count = 0;

if(count == 0){
        $("#element").hover(function(){
            count++;
            $("#element").attr("src","img2.png");
        }, function(){
            $("#element").attr("src","img1.png");
        });
} else if(count>=1){
        $("#element").hover(function(){
            count++;
            $("#element").attr("src","img3.png");
        }, function(){
            $("#element").attr("src","img1.png");
        });
}

The hover function on its own works fine, and hovering in and out will switch between two images. 悬停功能本身可以正常工作,并且将鼠标移入和移出将在两个图像之间切换。 However, my goal is to make it switch to img2 in the first hover, and then img3 at the second hover, and thereafter. 但是,我的目标是使其在第一个悬停时切换到img2,然后在第二个悬停时切换到img3,然后再切换。 My hover function seems to work fine if I want it to hover between img1 and img2 or img 1 and img3, that is when I remove if statement and count variable. 如果我希望将其悬停在img1和img2或img 1和img3之间,也就是我删除if语句和count变量时,我的悬停函数似乎可以正常工作。

If someone could help identify my problem please. 如果有人可以帮助确定我的问题,请。

From what you've posted, if(count == 0) occurs in your setup code. 从您发布的内容来看,您的设置代码中出现了if(count == 0) None of your hover event handlers touch the value of count , so once the handlers are set (either to 1 and 2 or 1 and 3), they will never be changed. 您的悬停事件处理程序都不会触及count的值,因此一旦设置了处理程序(设置为1和2或1和3),它们就永远不会更改。 And since count is always zero when the handlers are added, you always get the first pair. 并且由于添加处理程序时count始终为零,因此您始终会获得第一对。

Instead, you should switch between the two images inside of your handler function, not in the code that assigns the handler. 相反,您应该在处理程序函数内部的两个图像之间切换,而不是在分配处理程序的代码中进行切换。

You need to do the counter check within the hover handler 您需要在悬停处理程序中进行计数器检查

 var counter = 0; $("#element").hover(function() { $(this).attr("src", ++counter > 1 ? '//placehold.it/64X64/00ff00' : '//placehold.it/64X64/0000ff'); }, function() { $(this).attr("src", "//placehold.it/64X64/ff0000"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img id="element" src="//placehold.it/64X64/ff0000"> 

In your code the value of counter is checked only once on page load where the value of counter is always 0 在您的代码中, counter的值仅在页面加载时检查一次,其中计数器的值始终为0

Try creating array of strings containing img src , utilizing Array.prototype.reverse() to toggle images 尝试使用Array.prototype.reverse()创建包含img src的字符串数组来切换图像

    var imgs = ["img2.png", "img3.png"];
    $("#element").hover(function() {
      $(this).attr("src", imgs[0]);
      imgs.reverse()
    }, function() {
      $(this).attr("src", "img1.png");
    });

  var imgs = ["http://lorempixel.com/100/100/sports", "http://lorempixel.com/100/100/cats"]; $("#element").hover(function() { $(this).attr("src", imgs[0]); imgs.reverse() }, function() { $(this).attr("src", "http://lorempixel.com/100/100/nature"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img id="element" width="100px" src="http://lorempixel.com/100/100/nature" /> 

What about some simplicity? 那么简单呢?

var counter = 1;
$("#element").hover(function () {
    counter = (counter===3) ? 1 : (counter+1);
    $(this).attr("src", 'img'+counter+'.png');
}

Not sure if this will work just something I looked up but you can try it. 不知道这是否可以正常工作,但是您可以尝试。 Just add more variables to it. 只需添加更多变量即可。

jQuery(document).ready(function($) {

//rollover swap images with rel

var img_src = "";
var new_src = "";

$(".rollover").hover(function(){
 //mouseover

img_src = $(this).attr('src'); //grab original image

new_src = $(this).attr('rel'); //grab rollover image

$(this).attr('src', new_src); //swap images

$(this).attr('rel', img_src); //swap images

},
function(){
//mouse out

$(this).attr('src', img_src); //swap images

$(this).attr('rel', new_src); //swap images
});

How about this? 这个怎么样?

$(document).ready(function(){
    var count = 1;

    $("#element").hover(function() {

        count++;
       $(this).attr("src","img"+count+".png");

       if(count == 3){
        //Reset the count
        count = 0;
       }

    }, function() {
      $(this).attr("src", "img1.png");

    });
});

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

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