前言
返回頂部這個功能用jq實現,好容易實現,一個animate
配合scrollTo
就搞定了
今天我們來試試vue
封裝一個原生js實現的返回頂部;
寫起來夠嗆,藉助github
,看了別人的gist
,稍微封裝了下;
當然不是用scrollTo
直接調位那種,沒有過渡效果怎麼說得過去!!還是搗鼓出來了.
廢話不多說,看效果圖...
效果圖
實現思路
- 過渡用的是
requestAnimationFrame
,這貨只支援IE10+,所以必須做相容 - 滾動檢視是
window.pageYOffset
,這貨支援IE9+; - 為了讓可控性更強,圖示採用
iconfont
,具體瞅程式碼
你能學到什麼?
- 學到一些頁面計算相關的東東
- 動畫API的一些知識
- Vue封裝元件相關知識和生命週期和事件監聽銷燬相關知識的運用
實現功能
- 檢視預設在
350
處顯示返回頂部的按鈕和圖示 - 提示文字和顏色,在圖示上下左右的自定義,欄位都限制了格式和預設值
- 圖示顏色和形狀,大小的自定義,欄位都限制了格式和預設值
- 過渡動效的自定義,用法:
scrollIt(0, 1500, 'easeInOutCubic', callback);
- 返回到檢視的
point
,也就是滾動到哪裡 - 過渡時間(ms級別)
- 一堆過渡效果,字串格式,其實就是滾動的計算函式..
- 當然少不了預設引數了,除了
callback
- 返回到檢視的
- 相容性是
IE9+
,特意開了虛擬機器去嘗試
程式碼
scrollIt.js
--過渡滾動實現
export function scrollIt(
destination = 0,
duration = 200,
easing = "linear",
callback
) {
// define timing functions -- 過渡動效
let easings = {
// no easing, no acceleration
linear(t) {
return t;
},
// accelerating from zero velocity
easeInQuad(t) {
return t * t;
},
// decelerating to zero velocity
easeOutQuad(t) {
return t * (2 - t);
},
// acceleration until halfway, then deceleration
easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
// accelerating from zero velocity
easeInCubic(t) {
return t * t * t;
},
// decelerating to zero velocity
easeOutCubic(t) {
return --t * t * t + 1;
},
// acceleration until halfway, then deceleration
easeInOutCubic(t) {
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
},
// accelerating from zero velocity
easeInQuart(t) {
return t * t * t * t;
},
// decelerating to zero velocity
easeOutQuart(t) {
return 1 - --t * t * t * t;
},
// acceleration until halfway, then deceleration
easeInOutQuart(t) {
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t;
},
// accelerating from zero velocity
easeInQuint(t) {
return t * t * t * t * t;
},
// decelerating to zero velocity
easeOutQuint(t) {
return 1 + --t * t * t * t * t;
},
// acceleration until halfway, then deceleration
easeInOutQuint(t) {
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
}
};
// requestAnimationFrame()的相容性封裝:先判斷是否原生支援各種帶字首的
//不行的話就採用延時的方案
(function() {
var lastTime = 0;
var vendors = ["ms", "moz", "webkit", "o"];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame =
window[vendors[x] + "RequestAnimationFrame"];
window.cancelAnimationFrame =
window[vendors[x] + "CancelAnimationFrame"] ||
window[vendors[x] + "CancelRequestAnimationFrame"];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
})();
function checkElement() {
// chrome,safari及一些瀏覽器對於documentElemnt的計算標準化,reset的作用
document.documentElement.scrollTop += 1;
let elm =
document.documentElement.scrollTop !== 0
? document.documentElement
: document.body;
document.documentElement.scrollTop -= 1;
return elm;
}
let element = checkElement();
let start = element.scrollTop; // 當前滾動距離
let startTime = Date.now(); // 當前時間
function scroll() { // 滾動的實現
let now = Date.now();
let time = Math.min(1, (now - startTime) / duration);
let timeFunction = easings[easing](time);
element.scrollTop = timeFunction * (destination - start) + start;
if (element.scrollTop === destination) {
callback; // 此次執行回撥函式
return;
}
window.requestAnimationFrame(scroll);
}
scroll();
}複製程式碼
- backToTop.vue
<template>
<div class="back-to-top" @click="backToTop" v-show="showReturnToTop" @mouseenter="show" @mouseleave="hide">
<i :class="[bttOption.iClass]" :style="{color:bttOption.iColor,'font-size':bttOption.iFontsize}"></i>
<span class="tips" :class="[bttOption.iPos]" :style="{color:bttOption.textColor}" v-show="showTooltips">{{bttOption.text}}</span>
</div>
</template>
<script>
import { scrollIt } from './scrollIt'; // 引入動畫過渡的實現
export default {
name: 'back-to-top',
props: {
text: { // 文字提示
type: String,
default: '返回頂部'
},
textColor: { // 文字顏色
type: String,
default: '#f00'
},
iPos: { // 文字位置
type: String,
default: 'right'
},
iClass: { // 圖示形狀
type: String,
default: 'fzicon fz-ad-fanhuidingbu1'
},
iColor: { // 圖示顏色
type: String,
default: '#f00'
},
iFontsize: { // 圖示大小
type: String,
default: '32px'
},
pageY: { // 預設在哪個檢視顯示返回按鈕
type: Number,
default: 400
},
transitionName: { // 過渡動畫名稱
type: String,
default: 'linear'
}
},
data: function () {
return {
showTooltips: false,
showReturnToTop: false
}
},
computed: {
bttOption () {
return {
text: this.text,
textColor: this.textColor,
iPos: this.iPos,
iClass: this.iClass,
iColor: this.iColor,
iFontsize: this.iFontsize
}
}
},
methods: {
show () { // 顯示隱藏提示文字
return this.showTooltips = true;
},
hide () {
return this.showTooltips = false;
},
currentPageYOffset () {
// 判斷滾動區域大於多少的時候顯示返回頂部的按鈕
window.pageYOffset > this.pageY ? this.showReturnToTop = true : this.showReturnToTop = false;
},
backToTop () {
scrollIt(0, 1500, this.transitionName, this.currentPageYOffset);
}
},
created () {
window.addEventListener('scroll', this.currentPageYOffset);
},
beforeDestroy () {
window.removeEventListener('scroll', this.currentPageYOffset)
}
}
</script>
<style scoped lang="scss">
.back-to-top {
position: fixed;
bottom: 5%;
right: 100px;
z-index: 9999;
cursor: pointer;
width: auto;
i {
font-size: 32px;
display: inline-block;
position: relative;
text-align: center;
padding: 5px;
background-color: rgba(234, 231, 231, 0.52);
border-radius: 5px;
transition: all 0.3s linear;
&:hover {
border-radius: 50%;
background: #222;
color: #fff !important;
}
}
.tips {
display: inline-block;
position: absolute;
word-break: normal;
white-space: nowrap;
width: auto;
font-size: 12px;
color: #fff;
z-index: -1;
}
.left {
right: 0;
top: 50%;
margin-right: 50px;
transform: translateY(-50%);
}
.right {
left: 0;
top: 50%;
margin-left: 50px;
transform: translateY(-50%);
}
.bottom {
bottom: 0;
margin-top: 50px;
}
.top {
top: 0;
margin-bottom: 50px;
}
}
</style>複製程式碼
總結
從心血來潮到折騰出來,為了兼顧相容性和擴充性,好像幾個小時了.
不過實現了.你再搬到其他語言,類似ng4
,也就是十來分鐘的事情,
思路會了,實現更多的是寫法而已,至於效能優化,可以一邊寫一邊考慮,也可以實現後有空再優化.
有更好的實現方案和思路可以往下面留言,謝謝