最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件

 更新時(shí)間:2017年07月25日 14:45:27   作者:CRPER  
本篇文章主要介紹了Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件,可以實(shí)現(xiàn)回到頂部效果,具有一定的參考價(jià)值,有興趣的可以了解一下

最近在學(xué)習(xí)VUE。自己就在研究怎么用VUE實(shí)現(xiàn)一個(gè)組件的封裝,今日就算留個(gè)筆記

前言

返回頂部這個(gè)功能用jq實(shí)現(xiàn),好容易實(shí)現(xiàn),一個(gè)animate配合scrollTo就搞定了

今天我們來(lái)試試vue封裝一個(gè)原生js實(shí)現(xiàn)的返回頂部;
寫起來(lái)夠嗆,借助github,看了別人的gist,稍微封裝了下;

當(dāng)然不是用scrollTo直接調(diào)位那種,沒(méi)有過(guò)渡效果怎么說(shuō)得過(guò)去!!還是搗鼓出來(lái)了.

廢話不多說(shuō),看效果圖…

效果圖

實(shí)現(xiàn)思路

  1. 過(guò)渡用的是requestAnimationFrame,這貨只支持IE10+,所以必須做兼容
  2. 滾動(dòng)視圖是window.pageYOffset,這貨支持IE9+;
  3. 為了讓可控性更強(qiáng),圖標(biāo)采用iconfont,具體瞅代碼

你能學(xué)到什么?

  1. 學(xué)到一些頁(yè)面計(jì)算相關(guān)的東東
  2. 動(dòng)畫API的一些知識(shí)
  3. Vue封裝組件相關(guān)知識(shí)和生命周期和事件監(jiān)聽(tīng)銷毀相關(guān)知識(shí)的運(yùn)用

實(shí)現(xiàn)功能

  1. 視圖默認(rèn)在350處顯示返回頂部的按鈕和圖標(biāo)
  2. 提示文字和顏色,在圖標(biāo)上下左右的自定義,字段都限制了格式和默認(rèn)值
  3. 圖標(biāo)顏色和形狀,大小的自定義,字段都限制了格式和默認(rèn)值
  4. 過(guò)渡動(dòng)效的自定義,用法:scrollIt(0, 1500, 'easeInOutCubic', callback);
    1. 返回到視圖的point,也就是滾動(dòng)到哪里
    2. 過(guò)渡時(shí)間(ms級(jí)別)
    3. 一堆過(guò)渡效果,字符串格式,其實(shí)就是滾動(dòng)的計(jì)算函數(shù)..
    4. 當(dāng)然少不了默認(rèn)參數(shù)了,除了callback
  5. 兼容性是IE9+,特意開(kāi)了虛擬機(jī)去嘗試

代碼

scrollIt.js –過(guò)渡滾動(dòng)實(shí)現(xiàn)

export function scrollIt(
 destination = 0,
 duration = 200,
 easing = "linear",
 callback
) {
 // define timing functions -- 過(guò)渡動(dòng)效
 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()的兼容性封裝:先判斷是否原生支持各種帶前綴的
 //不行的話就采用延時(shí)的方案
 (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及一些瀏覽器對(duì)于documentElemnt的計(jì)算標(biāo)準(zhǔn)化,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; // 當(dāng)前滾動(dòng)距離
 let startTime = Date.now(); // 當(dāng)前時(shí)間

 function scroll() { // 滾動(dòng)的實(shí)現(xiàn)
  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; // 此次執(zhí)行回調(diào)函數(shù)
   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'; // 引入動(dòng)畫過(guò)渡的實(shí)現(xiàn)
 export default {
  name: 'back-to-top',
  props: {
   text: { // 文本提示
    type: String,
    default: '返回頂部'
   },
   textColor: { // 文本顏色
    type: String,
    default: '#f00'
   },
   iPos: { // 文本位置
    type: String,
    default: 'right'
   },
   iClass: { // 圖標(biāo)形狀
    type: String,
    default: 'fzicon fz-ad-fanhuidingbu1'
   },
   iColor: { // 圖標(biāo)顏色
    type: String,
    default: '#f00'
   },
   iFontsize: { // 圖標(biāo)大小
    type: String,
    default: '32px'
   },
   pageY: { // 默認(rèn)在哪個(gè)視圖顯示返回按鈕
    type: Number,
    default: 400
   },
   transitionName: { // 過(guò)渡動(dòng)畫名稱
    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 () {
    // 判斷滾動(dòng)區(qū)域大于多少的時(shí)候顯示返回頂部的按鈕
    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>

總結(jié)

從心血來(lái)潮到折騰出來(lái),為了兼顧兼容性和拓展性,好像幾個(gè)小時(shí)了.

不過(guò)實(shí)現(xiàn)了.你再搬到其他語(yǔ)言,類似ng4,也就是十來(lái)分鐘的事情,

思路會(huì)了,實(shí)現(xiàn)更多的是寫法而已,至于性能優(yōu)化,可以一邊寫一邊考慮,也可以實(shí)現(xiàn)后有空再優(yōu)化.

希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

城固县| 罗城| 闽侯县| 商洛市| 定南县| 饶阳县| 赤城县| 镇坪县| 同仁县| 通化县| 上犹县| 泉州市| 永定县| 类乌齐县| 青州市| 永城市| 竹溪县| 织金县| 安仁县| 友谊县| 庆元县| 宿松县| 巴彦淖尔市| 阳江市| 剑阁县| 阳山县| 龙海市| 曲周县| 会东县| 靖西县| 濉溪县| 诏安县| 恩施市| 健康| 保靖县| 石首市| 峨边| 正宁县| 乡城县| 定南县| 辽源市|