jQuery 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代码贴得有点难看,不知道怎么做才对

编辑:

非常感谢,但我想那不是我需要的。

我想我需要计算一下image-width和margin-left,以便从左移到右移和返回。

所以我试图计算我的图像的宽度,但是

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,它不起作用:-(

在移动元素之前,使用jQuery .data()来存储原始边距。这样,当你想要重置时,你可以将边距动画为该值。

这段代码并不完美。这只是让你知道你能做什么。

//I would avoid global variables, but I don't know enough of your code.
swoopingElement =  $('#backstretch img');
swoopingElement.data("startingmargin", $('#backstretch img').css('marginLeft'));
(function swoop(element) {
  element
    .animate({'margin-left':'-=5px'}, 100, function(){
      swoopingElement.data("timer", setTimeout(function(){
        swoop(element);
      }, 0));
    });
})($('#backstretch img'));
function resetElement(element){
  //First shut down any remaining animation
  element.stop();
  clearTimeout(element.data("timer"));  //even though it's zero...  it might be active.
  //Send element back
  element.animate({'margin-left':element.data("startingmargin")}, 100);
  //Or for instant return.
  // element.css({'margin-left':element.data("startingmargin")});
};
https://api.jquery.com/jquery.data/

最新更新