gpt4 book ai didi

javascript - Smooth Scroll 插件在 Chrome 中不起作用

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

所以我已经完成了我的网站,并且刚刚发现我使用的平滑滚动 JS 在 Chrome 中无法工作。它在 Safari、Firefox 中完美运行,我尝试过其他的,但我似乎无法让它们识别我的 anchor ,或保持突出显示的导航。

这是我的导航:

<ul id="menu"><!-- BEGIN MENU -->
<li><img class="valigner"><a href="#home">home</a></li>
<li><img class="valigner"><a href="#about">about</a></li>
<li><img class="valigner"><a href="#headshots">headshots</a></li>
<li><img class="valigner"><a href="#fashion">fashion</a></li>
<li><img class="valigner"><a href="#editorial">editorial</a></li>
<li><a href="#home"><img src="img/logo.jpg" class="logo"/></a></li>
<li><img class="valigner"><a href="#live">live</a></li>
<li><img class="valigner"><a href="#events">events</a></li>
<li><img class="valigner"><a href="#outside">outside</a></li>
<li><img class="valigner"><a href="#product">product</a></li>
<li><img class="valigner"><a href="#contact">contact</a></li>
</ul><!-- END MENU -->

这是 JS:

;(function( $ ){

var $scrollTo = $.scrollTo = function( target, duration, settings ){
$(window).scrollTo( target, duration, settings );
};

$scrollTo.defaults = {
axis:'xy',
duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
};

// Returns the element that needs to be animated to scroll the window.
// Kept for backwards compatibility (specially for localScroll & serialScroll)
$scrollTo.window = function( scope ){
return $(window)._scrollable();
};

// Hack, hack, hack :)
// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
$.fn._scrollable = function(){
return this.map(function(){
var elem = this,
isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;

if( !isWin )
return elem;

var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;

return $.browser.safari || doc.compatMode == 'BackCompat' ?
doc.body :
doc.documentElement;
});
};

$.fn.scrollTo = function( target, duration, settings ){
if( typeof duration == 'object' ){
settings = duration;
duration = 0;
}
if( typeof settings == 'function' )
settings = { onAfter:settings };

if( target == 'max' )
target = 9e9;

settings = $.extend( {}, $scrollTo.defaults, settings );
// Speed is still recognized for backwards compatibility
duration = duration || settings.speed || settings.duration;
// Make sure the settings are given right
settings.queue = settings.queue && settings.axis.length > 1;

if( settings.queue )
// Let's keep the overall duration
duration /= 60000;
settings.offset = both( settings.offset );
settings.over = both( settings.over );

return this._scrollable().each(function(){
var elem = this,
$elem = $(elem),
targ = target, toff, attr = {},
win = $elem.is('html,body');

switch( typeof targ ){
// A number will pass the regex
case 'number':
case 'string':
if( /^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
targ = both( targ );
// We are done
break;
}
// Relative selector, no break!
targ = $(targ,this);
case 'object':
// DOMElement / jQuery
if( targ.is || targ.style )
// Get the real position of the target
toff = (targ = $(targ)).offset();
}
$.each( settings.axis.split(''), function( i, axis ){
var Pos = axis == 'x' ? 'Left' : 'Top',
pos = Pos.toLowerCase(),
key = 'scroll' + Pos,
old = elem[key],
max = $scrollTo.max(elem, axis);

if( toff ){// jQuery / DOMElement
attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );

// If it's a dom element, reduce the margin
if( settings.margin ){
attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
}

attr[key] += settings.offset[pos] || 0;

if( settings.over[pos] )
// Scroll to a fraction of its width/height
attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
}else{
var val = targ[pos];
// Handle percentage values
attr[key] = val.slice && val.slice(-1) == '%' ?
parseFloat(val) / 100 * max
: val;
}

// Number or 'number'
if( /^\d+$/.test(attr[key]) )
// Check the limits
attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );

// Queueing axes
if( !i && settings.queue ){
// Don't waste time animating, if there's no need.
if( old != attr[key] )
// Intermediate animation
animate( settings.onAfterFirst );
// Don't animate this axis again in the next iteration.
delete attr[key];
}
});

animate( settings.onAfter );

function animate( callback ){
$elem.animate( attr, duration, settings.easing, callback && function(){
callback.call(this, target, settings);
});
};

}).end();
};

// Max scrolling position, works on quirks mode
// It only fails (not too badly) on IE, quirks mode.
$scrollTo.max = function( elem, axis ){
var Dim = axis == 'x' ? 'Width' : 'Height',
scroll = 'scroll'+Dim;

if( !$(elem).is('html,body') )
return elem[scroll] - $(elem)[Dim.toLowerCase()]();

var size = 'client' + Dim,
html = elem.ownerDocument.documentElement,
body = elem.ownerDocument.body;

return Math.max( html[scroll], body[scroll] )
- Math.min( html[size] , body[size] );

};

function both( val ){
return typeof val == 'object' ? val : { top:val, left:val };
};

})( jQuery );

我相信它来自 Ariel Flesler,但他的演示页面和文档让我更加困惑。

最佳答案

这适用于所有浏览器,

$(document).ready(function () {
//run once
var el = $('#Container');
var originalelpos = el.offset().top; // take it where it originally is on the page

//run on scroll
$(window).scroll(function () {
var el = $('#Container'); // important! (local)
var elpos = el.offset().top; // take current situation
var windowpos = $(window).scrollTop();
var finaldestination = windowpos + originalelpos;
el.css('top', finaldestination);
//el.stop().animate({ 'top': finaldestination }, 1000);
});

});

希望对您有所帮助,您可以使用 animate 来制作动画或使用 .css 来更改样式中的顶部属性

关于javascript - Smooth Scroll 插件在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14096904/

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