我在我的网站上使用 backstretch。现在尝试将背景从左到右自动移动。但直到现在,它只是朝着一个方向发展。我正在寻找那个移动图像的连续循环。
如何重置/移回图像?
backstretch.js:http ://dev.disaster-kfw.com/fileadmin/template/js/slidebackground.js
更多用于移动效果和初始化的 javascript
var images = ['fileadmin/template/gfx/background02-03.jpg'];
jQuery.backstretch( images[Math.floor(Math.random() * images.length)] );
jQuery(function($){
(function swoop(element) {
element
.animate({'margin-left':'-=5px'}, 100, function(){
setTimeout(function(){
swoop(element);
}, 0);
});
})($('#backstretch img'));
});
产生这个 HTML 输出
<div id="backstretch" style="left: 0px; top: 0px; position: fixed; overflow: hidden; z-index: -999999; margin: 0px; padding: 0px; height: 100%; width: 100%;"><img src="fileadmin/template/gfx/background02-03.jpg" style="position: absolute; margin: 0px 0px 0px -3404.98890491151px; padding: 0px; border: none; z-index: -999999; width: 3006px; height: 835px; left: -551.5px; top: 0px;"></div>
抱歉发布的 html 代码有点丑陋,不知道做对了......
编辑:
非常感谢,但我认为这不是我需要的。
我想我需要一个关于图像宽度和边距左的计算来从左移到右移和后退。
所以我试图计算我的图像的宽度,但是
iWidth = jQuery("#backstretch img").width();
始终为 NULL。
和
iWidth = $("#backstretch img").width();
破坏了整个 javascript。
我认为编写第二个名为 backswoop 的函数来计算左边距可能是一种解决方案。然后做一个关于margin-left和image-width的条件。
iWidth = jQuery('#backstretch img').width();
jQuery(function($){
(function swoop(element) {
element.animate({'margin-left':'-=5px'}, 50, function(){
setTimeout(function(){
if(element.css('margin-left')*-1 <= iWidth){
swoop(element);
}else{
backswoop(element);
}
}, 0);
});
})($('#backstretch img'));
(function backswoop(element) {
element.animate({'margin-left':'+=5px'}, 50, function(){
setTimeout(function(){
if(element.css('margin-left') >= 0){
swoop(element);
}else{
backswoop(element);
}
}, 0);
});
})($('#backstretch img'));
});
但是,因为我不擅长 javascript,所以它不起作用:-(