gpt4 book ai didi

javascript - 上下滚动一个div

转载 作者:行者123 更新时间:2023-11-30 10:18:13 24 4
gpt4 key购买 nike

我已经设置了一个 fiddle http://jsfiddle.net/austinind/aadcd/1/

我想要实现的是视差效果。

div 应该根据 scolling 上下滚动。

在我的例子中,div 只向上滚动:

 function parallax(){
var scrolled = $(window).scrollTop();
var elempos=$(".bg2").position().top;
$('.bg2').css('top', elempos-(scrolled * 0.2) + 'px');
}


$(window).scroll(function(e){
parallax();
});

最佳答案

看看下面的Js fiddle

用于滚动的 Javascript

(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

if ($.event.fixHooks) {
for ( var i=types.length; i; ) {
$.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
}
}

$.event.special.mousewheel = {
setup: function() {
if ( this.addEventListener ) {
for ( var i=types.length; i; ) {
this.addEventListener( types[--i], handler, false );
}
} else {
this.onmousewheel = handler;
}
},

teardown: function() {
if ( this.removeEventListener ) {
for ( var i=types.length; i; ) {
this.removeEventListener( types[--i], handler, false );
}
} else {
this.onmousewheel = null;
}
}
};

$.fn.extend({
mousewheel: function(fn) {
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
},

unmousewheel: function(fn) {
return this.unbind("mousewheel", fn);
}
});


function handler(event) {
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
event = $.event.fix(orgEvent);
event.type = "mousewheel";

// Old school scrollwheel delta
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }

// New school multidimensional scroll (touchpads) deltas
deltaY = delta;

// Gecko
if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
deltaY = 0;
deltaX = -1*delta;
}

// Webkit
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }

// Add event and delta to the front of the arguments
args.unshift(event, delta, deltaX, deltaY);

return ($.event.dispatch || $.event.handle).apply(this, args);
}

})(jQuery);





// OUR CODE





var winH = $(window).height();
$('.page').height(winH);

var c = 0;
var pagesN = $('.page').length;


$(document).bind('mousewheel', function(ev, delta) {

delta>0 ? --c : ++c ;
if(c===-1){
c=0;
}else if(c===pagesN){
c=pagesN-1;
}

var pagePos = $('.page').eq(c).position().top;
$('html, body').stop().animate({scrollTop: pagePos},1000);
return false;

});

关于javascript - 上下滚动一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22678737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com