嗨,我现在在我的网站上有这个代码...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var scrollSpeed = 50; // Speed in milliseconds
var step = 1; // How many pixels to move per step
var current = 0; // The current pixel row
var imageWidth = 4000; // Background image width
var headerWidth = screen.width; // How wide the header is.
//The pixel row where to start a new loop
var restartPosition = -(imageWidth - headerWidth);
function scrollBg(){
//Go to next pixel row.
current -= step;
//If at the end of the image, then go to the top.
if (current == restartPosition){
current = 0;
}
//Set the CSS of the header.
$('#body').css("background-position",current+"px 0");
}
//Calls the scrolling function repeatedly
var init = setInterval("scrollBg()", scrollSpeed);
</script>
我正在尝试添加第二个jQuery选择器:
$('#body').css("background-position",current+"px 0");
以便为两个元素更改css background-position属性。
IE 8无法使用渐变和背景,因此我使用嵌套div(class =“flash-bg”)和背景图片,并希望更新它的背景位置,以便它在IE中也有动画效果
如何在jquery代码中添加“flash-bg”?有没有办法将它添加到'#body'旁边?或者只是重复代码,除了'#flash-bg'?谢谢!
答案 0 :(得分:3)
如果我理解正确,如果浏览器是IE,你只想添加flash-bg
类。您可以使用IE的conditional comments:
<!--[if lte IE 8]>
<script type="text/javascript">
$(document).ready(function() {
$('#body').addClass("flash-bg");
});
</script>
<![endif]-->
答案 1 :(得分:3)
如果您想在background-position
和#body
上设置.flash-bg
,这将有效:
$('#body, .flash-bg').css("background-position",current+"px 0");
另外,在setInterval
中,您可以将对函数的引用而不是字符串传递给eval
:ed:
var init = setInterval(scrollBg, scrollSpeed);
答案 2 :(得分:1)
$('#body, div.flash-bg').css("background-position",current+"px 0");
答案 3 :(得分:0)
我认为您正在寻找addClass http://api.jquery.com/addClass/
$('#body').addClass('flash-bg');