【发布时间】:2016-01-25 09:18:45
【问题描述】:
我正在使用 jQuery 函数作为 div 内的视差背景。
当页面加载时,我有一些 css webkit 动画来为背景设置动画。
但是,在页面加载完成后,我的在背景上动画视差效果的 jQuery 函数不起作用。
这是我的代码:
$(document).ready(function(){
$('#square').mousemove(function(e) {
var x = -(e.pageX + this.offsetLeft) / 4;
var y = -(e.pageY + this.offsetTop) / 4;
$(this).css('background-position', x + 'px ' + y + 'px');
});
});
#square { height: 700px;
width: 500px;
display: inline-block;
margin-left: 100px;
position: absolute;
right: 37%;
top: 15%;
background: transparent;
-webkit-animation-name: image-fadein;
-webkit-animation-delay: .8s;
-webkit-animation-duration: 1s;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-fill-mode: forwards;
@-webkit-keyframes image-fadein {
0% { background: transparent; }
25% { background: #f2efef; }
50% { background: #333; }
100% { background-image: url(https://destinyguides.files.wordpress.com/2014/08/destiny-wallpaper-3.jpg);
background-size: cover no-repeat;
background-position: 35% 30%; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div id="square">
<span class="l1"></span>
<span class="l2"></span>
<span class="l3"></span>
<span class="l4"></span>
</div>
我应该提到,当我从 div 元素中完全删除 webkit 动画并只保留高度、宽度、显示、边距、位置和背景时,jQuery 函数确实有效。
有谁知道为什么 webkit 动画似乎干扰了 jQuery 代码?
【问题讨论】:
标签: javascript jquery html css