视差容错

1.背景内容滑动速度与滑动条滑动速度一致【随着滑动条而滑动】

解决方法:

将background-attachment 属性设置为 "scroll"(默认)

实例:

css:

.top1{background:url(../images/xxx.png) no-repeat;}

2.背景内容与滑动条滑动速度(或方向)不一致

解决方法:

将background-attachment 属性设置为 "fixed" ,再在js中设置(计算)background-position;【若不设置background-position则背景图像将固定在设置的原始位置 ,滑动过程中不会发生改变】

实例:
css:
.top1{background:url(../images/xxx.png) no-repeat fixed;}

js:

$('.top1').parallax("50%", -0.1);//50%是x偏移量  -0.1是图像垂直滑动的速度,负号代表当向下滑动时方向是向下、数值的绝对值越大越快

代码参考:

page.js

$('.top1').parallax("50%", -0.1);

parallax.js

// JavaScript Document

(function( $ ){
var $window = $(window);
var windowHeight = $window.height();
$window.resize(function () {
windowHeight = $window.height();
});
$.fn.parallax = function(xpos, speedFactor, outerHeight) {
var $this = $(this);
var getHeight;
var firstTop;
var paddingTop = 0;
//get the starting position of each element to have parallax applied to it
$this.each(function(){
firstTop = $this.offset().top;
});
if (outerHeight) {
getHeight = function(jqo) {
return jqo.outerHeight(true);
};
} else {
getHeight = function(jqo) {
return jqo.height();
};
}
// setup defaults if arguments aren't specified
if (arguments.length < 1 || xpos === null) xpos = "50%";
if (arguments.length < 2 || speedFactor === null) speedFactor = 0.1;
if (arguments.length < 3 || outerHeight === null) outerHeight = true;
// function to be called whenever the window is scrolled or resized
function update(){
var pos = $window.scrollTop();
$this.each(function(){
var $element = $(this);
var top = $element.offset().top;
var height = getHeight($element);
// Check if totally above or totally below viewport
if (top + height < pos || top > pos + windowHeight) {
return;
}
$this.css('backgroundPosition', xpos + " " + Math.round((firstTop - pos) * speedFactor) + "px");
});
}
$window.bind('scroll', update).resize(update);
update();
};
})(jQuery); 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章