【发布时间】:2015-05-28 09:08:11
【问题描述】:
我在我的网站上使用回弹。现在尝试将背景从左到右自动移动。但直到现在,它只是朝着一个方向发展。我正在寻找该动态图像的连续循环。
如何重置/移回图像?
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,所以它不起作用:-(
【问题讨论】:
-
设置一个可以帮助我们帮助你的小提琴。
标签: javascript jquery css jquery-backstretch