【问题标题】:Background Image is flickering on mouseover背景图像在鼠标悬停时闪烁
【发布时间】:2015-07-11 14:21:06
【问题描述】:

我试图在鼠标悬停时更改我的背景图像,图像正在更改,但过渡正在闪烁,并且淡入/淡出似乎不起作用……

代码:

css:

.background {
    position: fixed;
    top:100px;
    width: 100%;
    height: 100%;
    background: url(../img/angst.png) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

js:

$( ".project_link_01" )
  .mouseover(function() {
    $('.background').css('backgroundImage','url(../img/test.png)').fadeIn();
  })
  .mouseout(function() {
    $('.background').css('backgroundImage','url(../img/angst.png)');
});

【问题讨论】:

  • 当鼠标悬停时,当您拉起图像时,可能会因为加载图像的延迟而发生闪烁。尝试预加载它们。

标签: jquery css mouseover mouseenter


【解决方案1】:

尝试使用css:hover

.background {
  position: fixed;
  top: 100px;
  width: 100%;
  height: 100%;
  background: url(http://lorempixel.com/50/50/nature) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  transition: background 500ms;
}
.background:hover {
  background: url(http://lorempixel.com/50/50/technics) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
<img src="http://lorempixel.com/50/50/technics" style="display:none;" />
<div class="background"></div>

【讨论】:

    【解决方案2】:

    你需要像这样预加载图片:

    <img src="test.png" style="display:none">
    

    对于每一个。

    或者使用精灵:

    【讨论】:

    • 你可能会尝试限制或去抖动事件流。 underscore.js 等有一些方便的方法可以提供帮助。
    • 感谢您的回答。但它仍然在闪烁。它是一个全屏背景,每个链接悬停时,背景都应该改变,并带有不同的图像。
    • 预加载后真的闪烁吗?嗯,你能发个jsfiddle.net吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2014-10-21
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多