我希望使用jquery/javascript實現以下動畫。我有一個寬度爲2000px,高度爲200px的圖片。我想從左到右對圖像進行動畫處理,直到它到達一端,然後從右到左。這應該繼續給全景。如何從左到右和從右到左動畫背景圖像
我在互聯網上試過[下面的腳本] [1],但它只能用於oneide。我想要一個平移效果。 請諮詢。謝謝
;(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);
向我們展示你到現在 – CaptainHere
你可以做到這一點與CSS過渡什麼樣的代碼 - 只需創建一個從變化的背景位置的轉變0至100% – eithed
@eithed'背景位置:100%0'將背景移出視圖 – Justinas