Html锚点定位偏差计算解决插件

/*=============== 以下为HTML中的锚点代码 =====================*/

<div id="fixedNavBar" class="clearfix collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="page-scroll home">
<a id="introduceBtn" href="#introduce" class="color-d3d3d3 hover">个人介绍</a>
</li>
<li class="page-scroll">
<a id="experienceBtn" href="#experience" class="color-d3d3d3">科研经历</a>
</li>
<li class="page-scroll">
<a id="studyBtn" href="#study" class="color-d3d3d3">研究成果</a>
</li>
<li class="page-scroll">
<a id="patentBtn" href="#patent" class="color-d3d3d3">技术专利</a>
</li>
<li class="page-scroll">
<a id="societyBtn" href="#society" class="color-d3d3d3">社会活动</a>
</li>
</ul>
</div>

/*========== =========以下为JS脚本=== =============*/

(function ($) {
$.fn.scrolld = function (options) {

var scrolldCustom = 0; // Set 'scrolldCustom' value here | Additional distance (+-) on scrolldTop & scrolldPre

var $win=$(window);var doc=document;var y=$win.scrollTop();var h=$win.height();var x=$win.width();var htmlBody=$("html, body");$win.scroll(function(a){y=$win.scrollTop();a.stopImmediatePropagation();return false});$win.resize(function(a){h=$win.height();x=$win.width();a.stopImmediatePropagation();return false});$.easing.scrolldEasing1=function(f,g,e,k,j){var i=(g/=j)*g;var a=i*g;return e+k*(-3.6*a*i+4.3*i*i+0.2*a+0.1*i)};$.easing.scrolldEasing2=function(f,g,e,k,j){var i=(g/=j)*g;var a=i*g;return e+k*(18.9925*a*i+-45.23*i*i+40.28*a+-19.89*i+6.8475*g)};$.easing.scrolldEasing3=function(e,f,a,i,g){if((f/=g)<(1.25/3)){return i*(9.5625*f*f)+a}else{if(f<(2.25/3)){return i*(8.5625*(f-=(1.5/2.75))*f+0.85)+a}else{if(f<(2.75/3)){return i*(7.5625*(f-=(2.25/2.75))*f+0.9375)+a}else{return i*(5.5625*(f-=(2.625/2.75))*f+1)+a}}}};var speed1=100;var speed2=200;var speed3=300;var speed4=400;var speed5=500;var speed6=600;var speed7=700;var speed8=800;var speed9=900;var speed10=1000;var speed11=1100;var speed12=1200;var speed13=1300;var speed14=1400;var speed15=1500;var speed16=1600;var speed17=1700;var speed18=1800;var speed19=1900;var speed20=2000;var speedX=3000;var idScroll=$(this).attr("id");var scrolldNavBar="fixedNavBar";var scrolldMobileNavBar="fixedNavBar";var scrolldFixed=$("#"+scrolldNavBar).innerHeight();var scrolldMobileFixed=$("#"+scrolldMobileNavBar).outerHeight();var idScrollElement=$("#"+idScroll);var idScrollString=idScroll.substr(0,idScroll.length-3);var idScrollDiv=document.getElementById(idScrollString).id;var idScrollDivElement=$("#"+idScrollDiv);var offsetDivElementTop=Math.round(idScrollDivElement.offset().top);var idScrollDivElementHeight=Math.round(idScrollDivElement.height());var scrolldTop=offsetDivElementTop+scrolldCustom;var scrolldTopFixed=Math.round(offsetDivElementTop-scrolldFixed)+scrolldCustom;var scrolldPre=offsetDivElementTop-Math.round(h/15)+scrolldCustom;var scrolldPreFixed=offsetDivElementTop-scrolldFixed-Math.round(h/20)+scrolldCustom;var scrolldCenter=offsetDivElementTop-Math.round(h/2-idScrollDivElementHeight/2);var scrolldMobileTopFixed=Math.round(offsetDivElementTop-scrolldMobileFixed)+scrolldCustom;var scrolldMobilePreFixed=offsetDivElementTop-scrolldMobileFixed-Math.round(h/20)+scrolldCustom;var scrolldDistance=scrolldTop;var scrolldDistanceMin=scrolldTop;var scrolldSpeed=500;var scrolldEasing="scrolldEasing1";var scrolldFixed=true;var scrolldMobile=true;var scrolldMobileWidth=979;var scrolldMobileDistance=scrolldTop;var scrolldMobileDistanceMin=scrolldTop;var scrolldMobileSpeed=500;var scrolldMobileEasing="scrolldEasing1";var scrolldMobileFixed=true;

var // Set Custom Scrolld.js Settings Here:
defaults = {
/// Set Web Settings
scrolldDistance: scrolldTop,
scrolldDistanceMin: scrolldTop,
scrolldSpeed: speed9,
scrolldEasing: 'scrolldEasing1',
scrolldFixed: true,
scrolldNavBar: fixedNavBar,
/// Set Mobile Settings
scrolldMobile: true,
scrolldMobileWidth: 979,
scrolldMobileDistance: scrolldTop,
scrolldMobileDistanceMin: scrolldTop,
scrolldMobileSpeed: speed9,
scrolldMobileEasing: 'scrolldEasing1',
scrolldMobileFixed: true,
scrolldMobileNavBar: fixedNavBar
},
settings = $.extend({}, defaults, options);
this.each(function () {
if($(this).attr("id")!=""){var $this=$(this);if(settings.scrolldDistance){scrolldDistance=settings.scrolldDistance}if(settings.scrolldDistanceMin){scrolldDistanceMin=settings.scrolldDistanceMin}if(settings.scrolldSpeed){scrolldSpeed=settings.scrolldSpeed}if(settings.scrolldEasing){scrolldEasing=settings.scrolldEasing}if(settings.scrolldFixed){scrolldFixed=settings.scrolldFixed}if(settings.scrolldNavBar){scrolldNavBar=settings.scrolldNavBar}if(settings.scrolldMobileNavBar){scrolldMobileNavBar=settings.scrolldMobileNavBar}if(settings.scrolldMobile){scrolldMobile=settings.scrolldMobile}if(settings.scrolldMobileWidth){scrolldMobileWidth=settings.scrolldMobileWidth}if(settings.scrolldMobileDistance){scrolldMobileDistance=settings.scrolldMobileDistance}if(settings.scrolldMobileDistanceMin){scrolldMobileDistanceMin=settings.scrolldMobileDistanceMin}if(settings.scrolldMobileSpeed){scrolldMobileSpeed=settings.scrolldMobileSpeed}if(settings.scrolldMobileEasing){scrolldMobileEasing=settings.scrolldMobileEasing}if(settings.scrolldMobileFixed){scrolldMobileFixed=settings.scrolldMobileFixed}if($this.hasClass("linear")){scrolldEasing="linear"}if($this.hasClass("linearMobile")){scrolldMobileEasing="linear"}if($this.hasClass("swing")){scrolldEasing="swing"}if($this.hasClass("swingMobile")){scrolldMobileEasing="swing"}if($this.hasClass("jswing")){scrolldEasing="jswing"}if($this.hasClass("jswingMobile")){scrolldMobileEasing="jswing"}if($this.hasClass("easeInQuad")){scrolldEasing="easeInQuad"}if($this.hasClass("easeInQuadMobile")){scrolldMobileEasing="easeInQuad"}if($this.hasClass("easeOutQuad")){scrolldEasing="easeOutQuad"}if($this.hasClass("easeOutQuadMobile")){scrolldMobileEasing="easeOutQuad"}if($this.hasClass("easeInOutQuad")){scrolldEasing="easeInOutQuad"}if($this.hasClass("easeInOutQuadMobile")){scrolldMobileEasing="easeInOutQuad"}if($this.hasClass("easeInCubic")){scrolldEasing="easeInCubic"}if($this.hasClass("easeInCubicMobile")){scrolldMobileEasing="easeInCubic"}if($this.hasClass("easeOutCubic")){scrolldEasing="easeOutCubic"}if($this.hasClass("easeOutCubicMobile")){scrolldMobileEasing="easeOutCubic"}if($this.hasClass("easeInOutCubic")){scrolldEasing="easeInOutCubic"}if($this.hasClass("easeInOutCubicMobile")){scrolldMobileEasing="easeInOutCubic"}if($this.hasClass("easeInQuart")){scrolldEasing="easeInQuart"}if($this.hasClass("easeInQuartMobile")){scrolldMobileEasing="easeInQuart"}if($this.hasClass("easeOutQuart")){scrolldEasing="easeOutQuart"}if($this.hasClass("easeOutQuartMobile")){scrolldMobileEasing="easeOutQuart"}if($this.hasClass("easeInOutQuart")){scrolldEasing="easeInOutQuart"}if($this.hasClass("easeInOutQuartMobile")){scrolldMobileEasing="easeInOutQuart"}if($this.hasClass("easeInQuint")){scrolldEasing="easeInQuint"}if($this.hasClass("easeInQuintMobile")){scrolldMobileEasing="easeInQuint"}if($this.hasClass("easeOutQuint")){scrolldEasing="easeOutQuint"}if($this.hasClass("easeOutQuintMobile")){scrolldMobileEasing="easeOutQuint"}if($this.hasClass("easeInOutQuint")){scrolldEasing="easeInOutQuint"}if($this.hasClass("easeInOutQuintMobile")){scrolldMobileEasing="easeInOutQuint"}if($this.hasClass("easeInSine")){scrolldEasing="easeInSine"}if($this.hasClass("easeInSineMobile")){scrolldMobileEasing="easeInSine"}if($this.hasClass("easeOutSine")){scrolldEasing="easeOutSine"}if($this.hasClass("easeOutSineMobile")){scrolldMobileEasing="easeOutSine"}if($this.hasClass("easeInOutSine")){scrolldEasing="easeInOutSine"}if($this.hasClass("easeInOutSineMobile")){scrolldMobileEasing="easeInOutSine"}if($this.hasClass("easeInExpo")){scrolldEasing="easeInExpo"}if($this.hasClass("easeInExpoMobile")){scrolldMobileEasing="easeInExpo"}if($this.hasClass("easeOutExpo")){scrolldEasing="easeOutExpo"}if($this.hasClass("easeOutExpoMobile")){scrolldMobileEasing="easeOutExpo"}if($this.hasClass("easeInOutExpo")){scrolldEasing="easeInOutExpo"}if($this.hasClass("easeInOutExpoMobile")){scrolldMobileEasing="easeInOutExpo"}if($this.hasClass("easeInCirc")){scrolldEasing="easeInCirc"}if($this.hasClass("easeInCircMobile")){scrolldMobileEasing="easeInCirc"}if($this.hasClass("easeOutCirc")){scrolldEasing="easeOutCirc"}if($this.hasClass("easeOutCircMobile")){scrolldMobileEasing="easeOutCirc"}if($this.hasClass("easeInOutCirc")){scrolldEasing="easeInOutCirc"}if($this.hasClass("easeInOutCircMobile")){scrolldMobileEasing="easeInOutCirc"}if($this.hasClass("easeInElastic")){scrolldEasing="easeInElastic"}if($this.hasClass("easeInElasticMobile")){scrolldMobileEasing="easeInElastic"}if($this.hasClass("easeOutElastic")){scrolldEasing="easeOutElastic"}if($this.hasClass("easeOutElasticMobile")){scrolldMobileEasing="easeOutElastic"}if($this.hasClass("easeInOutElastic")){scrolldEasing="easeInOutElastic"}if($this.hasClass("easeInOutElasticMobile")){scrolldMobileEasing="easeInOutElastic"}if($this.hasClass("easeInBack")){scrolldEasing="easeInBack"}if($this.hasClass("easeInBackMobile")){scrolldMobileEasing="easeInBack"}if($this.hasClass("easeOutBack")){scrolldEasing="easeOutBack"}if($this.hasClass("easeOutBackMobile")){scrolldMobileEasing="easeOutBack"}if($this.hasClass("easeInOutBack")){scrolldEasing="easeInOutBack"}if($this.hasClass("easeInOutBackMobile")){scrolldMobileEasing="easeInOutBack"}if($this.hasClass("easeInBounce")){scrolldEasing="easeInBounce"}if($this.hasClass("easeInBounceMobile")){scrolldMobileEasing="easeInBounce"}if($this.hasClass("easeOutBounce")){scrolldEasing="easeOutBounce"}if($this.hasClass("easeOutBounceMobile")){scrolldMobileEasing="easeOutBounce"}if($this.hasClass("easeInOutBounce")){scrolldEasing="easeInOutBounce"}if($this.hasClass("easeInOutBounceMobile")){scrolldMobileEasing="easeInOutBounce"}if($this.hasClass("scrolldEasing1")){scrolldEasing="scrolldEasing1"}if($this.hasClass("scrolldEasing1Mobile")){scrolldMobileEasing="scrolldEasing1"}if($this.hasClass("scrolldEasing2")){scrolldEasing="scrolldEasing2"}if($this.hasClass("scrolldEasing2Mobile")){scrolldMobileEasing="scrolldEasing2"}if($this.hasClass("scrolldEasing3")){scrolldEasing="scrolldEasing3"}if($this.hasClass("scrolldEasing3Mobile")){scrolldMobileEasing="scrolldEasing3"}if(x<scrolldMobileWidth){if(scrolldMobile===true&&scrolldMobileFixed===false){if(h<=idScrollDivElementHeight){htmlBody.stop(true).animate({scrollTop:scrolldMobileDistanceMin},scrolldMobileSpeed,scrolldMobileEasing)}else{htmlBody.stop(true).animate({scrollTop:scrolldMobileDistance},scrolldMobileSpeed,scrolldMobileEasing)}}else{if(scrolldMobile===true&&scrolldMobileFixed===true){if(h<=(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistanceMin===scrolldTop){htmlBody.stop(true).animate({scrollTop:scrolldTopFixed},scrolldMobileSpeed,scrolldMobileEasing)}else{if(h<=(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistanceMin===scrolldPre){htmlBody.stop(true).animate({scrollTop:scrolldPreFixed},scrolldMobileSpeed,scrolldMobileEasing)}else{if(h<=(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistanceMin===scrolldCenter){htmlBody.stop(true).animate({scrollTop:scrolldCenter},scrolldMobileSpeed,scrolldMobileEasing)}else{if(h>(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistance===scrolldTop){htmlBody.stop(true).animate({scrollTop:scrolldTopFixed},scrolldMobileSpeed,scrolldMobileEasing)}else{if(h>(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistance===scrolldPre){htmlBody.stop(true).animate({scrollTop:scrolldPreFixed},scrolldMobileSpeed,scrolldMobileEasing)}else{if(h>(idScrollDivElementHeight+(scrolldMobileFixed*2))&&scrolldMobileDistance===scrolldCenter){htmlBody.stop(true).animate({scrollTop:scrolldCenter},scrolldMobileSpeed,scrolldMobileEasing)}else{htmlBody.stop(true).animate({scrollTop:scrolldMobileDistance},scrolldMobileSpeed,scrolldMobileEasing)}}}}}}}}}if(x>=scrolldMobileWidth){if(scrolldFixed===false){if(h<=idScrollDivElementHeight){htmlBody.stop(true).animate({scrollTop:scrolldDistanceMin},scrolldSpeed,scrolldEasing)}else{htmlBody.stop(true).animate({scrollTop:scrolldDistance},scrolldMobileSpeed,scrolldMobileEasing)}}else{if(scrolldFixed===true){if(h<=(idScrollDivElementHeight+scrolldFixed)&&scrolldDistanceMin===scrolldTop){htmlBody.stop(true).animate({scrollTop:scrolldTopFixed},scrolldSpeed,scrolldEasing)}else{if(h<=(idScrollDivElementHeight+scrolldFixed)&&scrolldDistanceMin===scrolldPre){htmlBody.stop(true).animate({scrollTop:scrolldPreFixed},scrolldSpeed,scrolldEasing)}else{if(h<=(idScrollDivElementHeight+scrolldFixed*2)&&scrolldDistanceMin===scrolldCenter){htmlBody.stop(true).animate({scrollTop:scrolldCenter},scrolldSpeed,scrolldEasing)}else{if(h>(idScrollDivElementHeight+scrolldFixed)&&scrolldDistance===scrolldTop){htmlBody.stop(true).animate({scrollTop:scrolldTopFixed},scrolldSpeed,scrolldEasing)}else{if(h>(idScrollDivElementHeight+scrolldFixed)&&scrolldDistance===scrolldPre){htmlBody.stop(true).animate({scrollTop:scrolldPreFixed},scrolldSpeed,scrolldEasing)}else{if(h>(idScrollDivElementHeight+scrolldFixed*2)&&scrolldDistance===scrolldCenter){htmlBody.stop(true).animate({scrollTop:scrolldCenter},scrolldSpeed,scrolldEasing)}else{htmlBody.stop(true).animate({scrollTop:scrolldDistance},scrolldSpeed,scrolldEasing)}}}}}}}}}};
});
return this;
}
})(jQuery);

 

/*===================以下为调用方式==========================*/

$("[id*='Btn']").stop(true).on('click', function (e) { e.preventDefault(); $(this).scrolld(); });

posted @ 2016-07-26 16:13  xiaofoyuan  阅读(1412)  评论(0编辑  收藏  举报