6

我在我的网站上使用 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,所以它不起作用:-(

4

1 回答 1

1

在移动元素之前,使用 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/

于 2015-05-28T09:20:52.170 回答