与jquery的生气蓬勃的身体背景图象

时间:2014-03-12 02:05:55

标签: jquery background animated

我对此代码有疑问:

// Global Variables

var scrollSpeed = 50;
// set the default position
var current = 0;
// image height & rotation complete
var _ht, _finishNRot = false;
// set the direction
var direction = 'h';
// create hidden image element to grab height of it for calculation purpose
var url = $('body').css('background-image').replace(/^url\(["']?/,     '').replace(/["']?\)$/, '');
var bgImage = $('<img />');
bgImage.hide().bind('load', function () { _ht = $(this).height(); });
$('body').append(bgImage);
bgImage.attr('src', url);

function bgscroll() {
    // while rotation not completed 
    if (_finishNRot == false) { current -= 1; } else { current += 1; };
    // -ve Rotation completed
    if (-(current) == _ht) _finishNRot = true;
    // +ve Rotation completed
    if ((current) == 0) _finishNRot = false;
    // move the background with backgrond-position css properties
    $('body').css("backgroundPosition", (direction == 'h') ? current + "px 0" : "0 " + current + "px");
}

//Calls the scrolling function repeatedly
setInterval(bgscroll, scrollSpeed);

当我的屏幕分辨率如1024或1480时,背景图像没有到达其最终像素(水平)或超过其最终像素(水平),我该如何解决?以下是在线示例:http://www.ideas-web.net63.net/prueba/prueba.html

1 个答案:

答案 0 :(得分:0)

添加: //全局变量

var size = window.outerWidth; 

变化:

bgImage.hide().bind('load', function () { _ht = $(this).height(); });

bgImage.hide().bind('load', function () { _ht = $(this).width(); });

并改变:

if (-(current) == _ht) _finishNRot = true;

if ((size-current) > _ht) _finishNRot = true;