'修改返回顶部功能'

This commit is contained in:
dongsuo 2017-07-17 17:47:16 +08:00 committed by 花裤衩
parent bf08756644
commit 5069a4d1df
2 changed files with 64 additions and 30 deletions

View file

@ -1,9 +1,12 @@
<template> <template>
<transition name="fade"> <transition :name="transitionName">
<div class="back-to-top" @click="backToTop" v-show="visible"> <div class="back-to-top" @click="backToTop" v-show="visible" :style="customStyle">
<el-tooltip class="item" effect="dark" content="返回顶部" placement="top"> <svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height: 16px; width: 16px;">
<i class="el-icon-arrow-up"></i> <title>回到顶部</title>
</el-tooltip> <g>
<path d="M12.036 15.59c0 .55-.453.995-.997.995H5.032c-.55 0-.997-.445-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29c.39-.39 1.026-.385 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" fill-rule="evenodd"></path>
</g>
</svg>
</div> </div>
</transition> </transition>
</template> </template>
@ -18,18 +21,39 @@ export default {
backPosition: { backPosition: {
type: Number, type: Number,
default: 0 default: 0
},
customStyle: {
type: Object,
default: {
right: '50px',
bottom: '50px',
width: '40px',
height: '40px',
'border-radius': '4px',
'line-height': '40px',
background: '#e7eaf1'
}
},
transitionName: {
type: String,
default: 'fade'
} }
}, },
data() { data() {
return { return {
visible: false visible: false,
interval: null
} }
}, },
mounted() { mounted() {
window.addEventListener('scroll', this.handleScroll) window.addEventListener('scroll', this.handleScroll);
}, },
beforeDestroy() { beforeDestroy() {
window.removeEventListener('scroll', this.handleScroll) window.removeEventListener('scroll', this.handleScroll);
if (this.interval) {
clearInterval(this.interval);
}
}, },
methods: { methods: {
handleScroll() { handleScroll() {
@ -38,11 +62,11 @@ export default {
backToTop() { backToTop() {
const start = window.pageYOffset; const start = window.pageYOffset;
let i = 0; let i = 0;
const t = setInterval(() => { this.interval = setInterval(() => {
const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500)); const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500));
if (next <= this.backPosition) { if (next <= this.backPosition) {
window.scrollTo(0, this.backPosition); window.scrollTo(0, this.backPosition);
clearInterval(t) clearInterval(this.interval)
} else { } else {
window.scrollTo(0, next); window.scrollTo(0, next);
} }
@ -59,21 +83,18 @@ export default {
<style scoped> <style scoped>
.back-to-top { .back-to-top {
position: fixed; position: fixed;
right: 50px;
bottom: 50px;
display: inline-block; display: inline-block;
height: 40px;
width: 40px;
box-shadow: 1px 1px 1px #58B7FF;
text-align: center; text-align: center;
cursor: pointer; cursor: pointer;
background: #58B7FF; }
color: #fff;
.back-to-top:hover {
background: #d5dbe7;
} }
.fade-enter-active, .fade-enter-active,
.fade-leave-active { .fade-leave-active {
transition: opacity 1s transition: opacity .5s;
} }
.fade-enter, .fade-enter,
@ -81,11 +102,9 @@ export default {
opacity: 0 opacity: 0
} }
.back-to-top i { .back-to-top .Icon {
display: inline-block; fill: #9aaabf;
width: 100%; background: none;
height: 100%;
line-height: 40px;
} }
</style> </style>

View file

@ -1,6 +1,8 @@
<template> <template>
<div class="components-container"> <div class="components-container">
<code>页面滚动到指定位置会在右下角出现返回顶部按钮</code> <code>页面滚动到指定位置会在右下角出现返回顶部按钮</code>
<code>可自定义按钮的样式show/hide临界点返回的位置
如需文字提示可在外部使用Element的el-tooltip元素 </code>
<div>我是占位</div> <div>我是占位</div>
<div>我是占位</div> <div>我是占位</div>
<div>我是占位</div> <div>我是占位</div>
@ -125,19 +127,32 @@
<div>我是占位</div> <div>我是占位</div>
<div>我是占位</div> <div>我是占位</div>
<div>我是占位</div> <div>我是占位</div>
<back-to-top id="back-to-top-t" :visibilityHeight="300" :backPosition="50"></back-to-top> <!--可自定义按钮的样式show/hide临界点返回的位置 -->
<!--如需文字提示可在外部添加element的<el-tooltip></el-tooltip>元素 -->
<el-tooltip placement="top" content="文字提示">
<back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
</el-tooltip>
</div> </div>
</template> </template>
<script> <script>
import BackToTop from 'components/BackToTop'; import BackToTop from 'components/BackToTop';
export default { export default {
components: { BackToTop } components: { BackToTop },
data() {
return {
myBackToTopStyle: {
right: '50px',
bottom: '50px',
width: '40px',
height: '40px',
'border-radius': '4px',
'line-height': '40px', //
background: '#e7eaf1'//
}
}
}
} }
</script> </script>
<style scoped> <style scoped>
#back-to-top-t{
right: 100px;
bottom: 100px;
}
</style> </style>