Jquery 背景图像在链接鼠标悬停时更改,并在鼠标退出时返回



我按以下方式设置了背景图像:

#collection-5777203c893fc094ec1de004{
  background-image: url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df28c63c9e687f/57771ca7440243196bc6801b/1467423990309/Gator.jpg?format=1500w);
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
我有一个链接,我想在悬停

时触发背景图像更改,我希望它在不悬停在链接上时返回到原始图像。

我已经使用此jquery更改了背景图像:

<script>
  jQuery('#block-5777203c893fc094ec1de005').hover(function() 
{ 
    jQuery('#collection-5777203c893fc094ec1de004').css("background-image", "url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df28c63c9e687f/57771d441b631b48a1362df6/1467424076131/patagoniawater.jpg?format=750w)"); 
}); 
</script>

有人可以向我展示在不悬停在链接上时将图像恢复为原始背景图像的代码吗?

图像也以略微故障的方式变化。 我不确定我是否可以做些什么来使更改更顺畅。

提前谢谢。

jQuery悬停支持两个事件:处理程序"In"(鼠标悬停)和处理程序"Out"(mouseout),因此请利用它们。

而且,为了方便起见,您可以使用jQuery css作为"getter",以便在修改之前获取原始背景图像。

// Since we're using these multiple times, assign them to variables
var $link = jQuery('#block-5777203c893fc094ec1de005');
var $image = jQuery('#collection-5777203c893fc094ec1de004');
// Get the original background image
var originalBackground = $image.css('background-image');
// jQuery hover supports TWO functions: first is "mouseover", second is "mouseout"
$link.hover(
    // On mouseover, replace image
    function() { 
        $image.css("background-image", "url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df28c63c9e687f/57771d441b631b48a1362df6/1467424076131/patagoniawater.jpg?format=750w)"); 
    },
    // On mouseout, put original image back
    function() {
        $image.css('background-image', originalBackground);
    }
); 

HTML:

<a id="mylink" href="link" style="yourstyle" onmouseover="overStyle(this)" onmouseout="outStyle(this)">TEXT</a>

j查询:

function overStyle(object) 
{
  $('#mylink').css("background-image", "new image");
}
function outStyle(object) 
{
  $('#mylink').css("background-image", "old image");
}
您可以使用

代码段 1 中所示的简单 CSS :hover,也可以使用代码段 2 中所示的 mouseentermouseleave。实际上,CSS 在某些方面比 JS/jQ 做得更好,这是罕见的例子之一。

片段 1

.rollover {
  background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
  background-size: contain;
  width: 256px;
  height: 256px;
}
.rollover:hover {
  background: url(http://eightieskids.com/wp-content/uploads/2016/07/test-card-girl-feature.jpg) no-repeat;
  background-size: contain;
}
<div class='rollover'></div>

片段 2

$(function() {
  $(".rollover")
    .on("mouseenter", function() {
      var img1 = {
        backgroundImage: "url(http://eightieskids.com/wp-content/uploads/2016/07/test-card-girl-feature.jpg)"
      };
      $(this).css(img1);
    })
    .on("mouseleave", function() {
      var img2 = {
        backgroundImage: "url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png)"
      };
      $(this).css(img2);
    });
});
.rollover {
  background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
  background-size: contain;
  width: 256px;
  height: 256px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='rollover'></div>

最新更新