如何从左到右和从右到左为背景图像设置动画

时间:2017-03-07 13:01:08

标签: javascript jquery css

我希望使用jquery / javascript实现以下动画。我的图像宽度为2000像素,高度为200像素。我想从左到右动画图像,直到它到达一端然后从右到左。这应该继续提供全景视图。

我在互联网上尝试了[以下脚本] [1],但它只是在一边。我想要像平移效果这样的东西。 好心劝告。谢谢

指向代码的直接链接:http://www.jqueryscript.net/demo/jQuery-Plugin-To-Create-Auto-Scrolling-Background-AutoBackgroundScroll-js/js/autoBackgroundScroll.js

;(function(){
$.fn.autoBackgroundScroll = function(options) {

    var opts = $.extend({}, $.fn.autoBackgroundScroll.defaults, options);
    var $backslider = $(this);


    var duration = opts.duration;
    var speed = opts.speed;
    var imageWidth = opts.imageWidth;
    var imageHeight = opts.imageHeight;
    var direction1 = opts.direction1;
    var direction2 = opts.direction2;


    var posX = 0;
    var posY = 0;


    scroll(duration, speed, direction1, direction2);


    function scroll(duration, speed, direction1, direction2){
        setInterval(function(){
            if(direction1 == 'right'){
                moveRight();

                if(direction2 == 'top'){
                    moveTop();
                }

                if(direction2 == 'bottom'){
                    moveBottom();
                }

            } else if(direction1 == 'left'){
                moveLeft();

                if(direction2 == 'top'){
                    moveTop();
                }

                if(direction2 == 'bottom'){
                    moveBottom();
                }

            } else if(direction1 == 'bottom'){
                moveBottom();

                if(direction2 == 'right'){
                    moveRight();
                }

                if(direction2 == 'left'){
                    moveLeft();
                }

            } else if(direction1 == 'top'){
                moveTop();

                if(direction2 == 'right'){
                    moveRight();
                }

                if(direction2 == 'left'){
                    moveLeft();
                }

            }

            $backslider.css('background-position', posX + 'px ' + posY + 'px');


            function moveTop(){
                if(posY <= -imageHeight){
                    posY = 0;
                }
                posY -= speed;
            }


            function moveRight(){
                if(posX >= imageWidth){
                    posX = 0;
                }
                posX += speed;
            }


            function moveBottom(){
                if(posY >= imageHeight){
                    posY = 0;
                }
                posY += speed;
            }


            function moveLeft(){
                if(posX <= -imageWidth){
                    posX = 0;
                }
                posX -= speed;
            }

        }, duration);
    }

}

$.fn.autoBackgroundScroll.defaults = {
    direction1: 'right',
    direction2: '',
    duration: 1,
    speed: 0.5
};

})(jQuery);

代码礼貌:http://www.jqueryscript.net/animation/jQuery-Plugin-To-Create-Auto-Scrolling-Background-AutoBackgroundScroll-js.html

2 个答案:

答案 0 :(得分:0)

简单的jQuery动画为x和y设置正确的data-content值(我建议使用CSS过渡以获得更流畅的动画):

onLoadPopoverFunction
background-position
$(document).ready(function() {
  slide();

  setInterval(function() {
    slide();
  }, 10000);
});

function slide() {
  $('body').css({
    'background-position': -2000 + $(window).width() + 'px 0'
  });

  setTimeout(function() {
    $('body').css({
      'background-position': '0 0'
    });
  }, 5000);
}

答案 1 :(得分:0)

我建议使用CSS动画

.bg {
  background-image: url("https://dummyimage.com/1000x200/333/ccc.png");
  background-position-x: 50%;
  animation-delay: 0s;
  animation-duration: 10s;
  animation-name: panoramic;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
  animation-fill-mode: both;
  width: 444px;
  height: 200px;
  overflow: hidden;
  margin: 0 auto;
  border: 1px solid gold;
  will-change: background-position-x;
}

@keyframes panoramic {
  0% {
    background-position-x: 0%;
  }
  50% {
    background-position-x: 100%;
  }
  100% {
    background-position-x: 0%;
  }
}
<div class="bg"></div>

相关问题