【问题标题】:jQuery backstretch image auto-moving and backjQuery backstretch 图像自动移动和返回
【发布时间】: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


【解决方案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/

【讨论】:

  • 你好 Grallen 你能描述一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 2013-07-11
相关资源
最近更新 更多