滚动丝般顺滑

This commit is contained in:
结发受长生 2019-05-26 20:00:40 +08:00
parent 46f5556257
commit c30aed06c7
6 changed files with 40 additions and 34 deletions

View File

@ -2,7 +2,7 @@
<div class="mod-side-operation"> <div class="mod-side-operation">
<% if (theme.top){ %> <% if (theme.top){ %>
<div class="jump-container" id="js-jump-container" style="display:none;"> <div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" > <a href="#top" >
<i class="icon icon-chevron-up"></i> <i class="icon icon-chevron-up"></i>
</a> </a>
</div> </div>

View File

@ -15,6 +15,7 @@
</div> </div>
</div> </div>
<div class="mid-col" :class="{show: isShow,hide: isShow!==undefined && !isShow}"> <div class="mid-col" :class="{show: isShow,hide: isShow!==undefined && !isShow}">
<a name="top"></a>
<%- partial('_partial/mobile-nav', null, {cache: !config.relative_link}) %> <%- partial('_partial/mobile-nav', null, {cache: !config.relative_link}) %>
<div id="wrapper" class="body-wrap" v-pre> <div id="wrapper" class="body-wrap" v-pre>
<div class="menu-l"> <div class="menu-l">

View File

@ -17,7 +17,7 @@
} }
.article { .article {
margin: 30px; margin: 0 30px 30px;
position: relative; position: relative;
border-width: 1px; border-width: 1px;
border-style: solid; border-style: solid;

View File

@ -20,7 +20,8 @@ html, body, #container {
#container{ #container{
position:relative; position:relative;
min-height:100%; min-height:100%;
background: linear-gradient(200deg,#5aa5c6,#a4854b); background: linear-gradient(200deg,#5aa5c6,#a4854b);
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling: touch;
overflow-scrolling: touch; overflow-scrolling: touch;
.anm-canvas { .anm-canvas {
@ -32,6 +33,7 @@ html, body, #container {
} }
} }
.body-wrap{ .body-wrap{
padding-top: 30px;
margin-bottom: 80px; margin-bottom: 80px;
} }
.mid-col { .mid-col {

View File

@ -1,4 +1,5 @@
.body-wrap { .body-wrap {
padding-top: 0;
margin-bottom: 0; margin-bottom: 0;
} }
.left-col { .left-col {

View File

@ -1,46 +1,48 @@
var backTop = function (domE, ctn, distance) { var backTop = function (domE, ctn, distance) {
if (!domE) return; if (!domE) return
var timer = null; var timer = null
var _onscroll = (ctn || window).onscroll, var _onscroll = ctn.onscroll
_onclick = domE.onclick; ctn.onscroll = throttle(function () { // 滚动到一定高度才显示"回到顶部"按钮
(ctn || window).onscroll = throttle(function () { typeof _onscroll === 'function' && _onscroll.apply(this, arguments)
typeof _onscroll === 'function' && _onscroll.apply(this, arguments); toggleDomE()
toggleDomE(); }, 100)
}, 100); if(!window.getComputedStyle || window.getComputedStyle(ctn).scrollBehavior === undefined) {
domE.onclick = function () { // 浏览器不支持scroll-behavior属性
typeof _onclick === 'function' && _onclick.apply(this, arguments); domE.querySelector('a[href="#top"]').addEventListener('click', function (event) {
timer = setInterval(function () { //设置一个计时器 event.preventDefault()
var ct = ctn.scrollTop || document.documentElement.scrollTop || document.body.scrollTop; //获取距离顶部的距离 timer = setInterval(function () { //设置一个计时器
var diff = Math.max(10, ct / 6); var ct = ctn.scrollTop || document.documentElement.scrollTop || document.body.scrollTop //获取距离顶部的距离
ct -= diff; var diff = Math.max(10, ct / 6)
if (ct > 0) {//如果与顶部的距离大于零 ct -= diff
ctn.scrollTop = ctn.scrollTop - diff; if (ct > 0) {//如果与顶部的距离大于零
window.scrollTo(0, ct);//向上移动10px ctn.scrollTop = ctn.scrollTop - diff
} else {//如果距离小于等于零 window.scrollTo(0, ct)//向上移动10px
ctn.scrollTop = 0; } else {//如果距离小于等于零
window.scrollTo(0, 0);//移动到顶部 ctn.scrollTop = 0
clearInterval(timer);//清除计时器 window.scrollTo(0, 0)//移动到顶部
} clearInterval(timer)//清除计时器
}, 10);//隔10ms执行一次前面的function展现一种平滑滑动效果 }
}; }, 13)//隔13ms执行一次前面的function展现一种平滑滑动效果
})
}
function toggleDomE() { function toggleDomE() {
domE.style.display = (ctn.scrollTop || document.documentElement.scrollTop || document.body.scrollTop) > (distance || 500) ? 'block' : 'none'; domE.style.display = (ctn.scrollTop || document.documentElement.scrollTop || document.body.scrollTop) > distance ? 'block' : 'none'
} }
function throttle(func, wait) { function throttle(func, wait) {
var timer = null; var timer = null
return function () { return function () {
var self = this, args = arguments; var self = this, args = arguments
if (timer) clearTimeout(timer); if (timer) clearTimeout(timer)
timer = setTimeout(function () { timer = setTimeout(function () {
return typeof func === 'function' && func.apply(self, args); return typeof func === 'function' && func.apply(self, args)
}, wait); }, wait)
} }
} }
}; };
function init() { function init() {
backTop(document.getElementById('js-jump-container'), document.getElementById('container')); backTop(document.getElementById('js-jump-container'), document.getElementById('container'), 300)
} }
export default { init } export default { init }