我正在尝试使用jQuery做一个简单的背景动画。但动画并不顺畅。我怎样才能让它顺利?以下是代码,正在使用的图像大小为4288 X 3216 px
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>bg</title>
<style>
#mydiv
{
background:url(bg2.jpg) repeat-x;
height:900px;
width:100%;
}
</style>
</head>
<body>
<div id="mydiv">
</div>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/bgpos.js"></script>
<script type="text/javascript">
$(document).ready (function()
{
move();
function move()
{
$('#mydiv').animate({"backgroundPosition":"+=5"},100,"linear",move);
};
});
</script>
</body>
</html>
答案 0 :(得分:0)
以下是具有背景位置用法的平滑动画示例:
<强> jsFiddle example to play with. 强>
$(document).ready (function() {
$('div').bind({
mouseenter: function() {
$(this).stop().animate({'background-position-y':'-20px','height':'68px'},600);
},
mouseleave: function() {
$(this).stop().animate({'background-position-y':'-58px','height':'30px'},600);
}
});
});
这是animate()结构示例:
.animate( properties [, duration] [, easing] [, complete] )
//i didn't use easing here
$('#mydiv').stop().animate({'background-position-y':'-2px','height':'6px'},200,
function() {
$('#mydiv').stop().animate({'background-position-x':'-20px'},200);
//this will initualise when animate complete
}
);
,
用于多个动画声明。答案 1 :(得分:0)
你的图像可能在文件大小,将质量降低到合理的网页大小(100kb),它应该运行顺畅。
(我的小提琴,图片是100kb) http://fiddle.jshell.net/EuwGb/