欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue組件之Tooltip的示例代碼

 更新時間:2017年10月18日 11:05:15   作者:答案在風中飄著  
這篇文章主要介紹了Vue組件之Tooltip的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

本文主要Alert 組件的大致框架, 提供少量可配置選項。 旨在大致提供思路

tooltip

常用于展示鼠標 hover 時的提示信息。

模板結(jié)構

<template>
 <div style="position:relative;">
  <span ref="trigger">
   <slot>
   </slot>
  </span>
  <div class="tooltip"
   v-bind:class="{
    'top':   placement === 'top',
    'left':  placement === 'left',
    'right':  placement === 'right',
    'bottom': placement === 'bottom',
    'disable': type === 'disable',
    'delete': type === 'delete',
    'visible': show === true 
   }"
   ref="popover"
   role="tooltip">
   <div class="tooltip-arrow"></div>
   <div class="tooltip-inner">
    <slot name="content" v-html="content"></slot>
   </div>
  </div>
 </div>
</template>

大致結(jié)構DOM結(jié)構 一個div 包含 箭頭 及 氣泡內(nèi)容。

v-bind中可選tooltip位置,是否禁用,及顯示隱藏

slot 差值供自定義 默認接收content內(nèi)容

script

import EventListener from '../utils/EventListener.js';

export default {
 props: {
  // 需要監(jiān)聽的事件
  trigger: {
   type: String,
   default: 'click'
  },
  effect: {
   type: String,
   default: 'fadein'
  },
  title: {
   type: String
  },
  // toolTip消息提示
  content: {
   type: String
  },
  header: {
   type: Boolean,
   default: true
  },
  placement: {
   type: String
  }
 },
 data() {
  return {
   // 通過計算所得 氣泡位置 
   position: {
    top: 0,
    left: 0
   },
   show: true
  };
 },
 watch: {
  show: function(val) {
   if (val) {
    const popover = this.$refs.popover;
    const triger = this.$refs.trigger.children[0];
    // 通過placement計算出位子
    switch (this.placement) {
     case 'top' :
      this.position.left = triger.offsetLeft - popover.offsetWidth / 2 + triger.offsetWidth / 2;
      this.position.top = triger.offsetTop - popover.offsetHeight;
      break;
     case 'left':
      this.position.left = triger.offsetLeft - popover.offsetWidth;
      this.position.top = triger.offsetTop + triger.offsetHeight / 2 - popover.offsetHeight / 2;
      break;
     case 'right':
      this.position.left = triger.offsetLeft + triger.offsetWidth;
      this.position.top = triger.offsetTop + triger.offsetHeight / 2 - popover.offsetHeight / 2;
      break;
     case 'bottom':
      this.position.left = triger.offsetLeft - popover.offsetWidth / 2 + triger.offsetWidth / 2;
      this.position.top = triger.offsetTop + triger.offsetHeight;
      break;
     default:
      console.log('Wrong placement prop');
    }
    popover.style.top = this.position.top + 'px';
    popover.style.left = this.position.left + 'px';
   }
  }
 },
 methods: {
  toggle() {
   this.show = !this.show;
  }
 },
 mounted() {
  if (!this.$refs.popover) return console.error("Couldn't find popover ref in your component that uses popoverMixin.");
  // 獲取監(jiān)聽對象
  const triger = this.$refs.trigger.children[0];
  // 根據(jù)trigger監(jiān)聽特定事件
  if (this.trigger === 'hover') {
   this._mouseenterEvent = EventListener.listen(triger, 'mouseenter', () => {
    this.show = true;
   });
   this._mouseleaveEvent = EventListener.listen(triger, 'mouseleave', () => {
    this.show = false;
   });
  } else if (this.trigger === 'focus') {
   this._focusEvent = EventListener.listen(triger, 'focus', () => {
    this.show = true;
   });
   this._blurEvent = EventListener.listen(triger, 'blur', () => {
    this.show = false;
   });
  } else {
   this._clickEvent = EventListener.listen(triger, 'click', this.toggle);
  }
  this.show = !this.show;
 },
 // 在組件銷毀前移除監(jiān)聽,釋放內(nèi)存
 beforeDestroy() {
  if (this._blurEvent) {
   this._blurEvent.remove();
   this._focusEvent.remove();
  }
  if (this._mouseenterEvent) {
   this._mouseenterEvent.remove();
   this._mouseleaveEvent.remove();
  }
  if (this._clickEvent) this._clickEvent.remove();
 }
};

// EventListener.js
const EventListener = {
 /**
  * Listen to DOM events during the bubble phase.
  *
  * @param {DOMEventTarget} target DOM element to register listener on.
  * @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
  * @param {function} callback Callback function.
  * @return {object} Object with a `remove` method.
  */
 listen(target, eventType, callback) {
  if (target.addEventListener) {
   target.addEventListener(eventType, callback, false);
   return {
    remove() {
     target.removeEventListener(eventType, callback, false);
    }
   };
  } else if (target.attachEvent) {
   target.attachEvent('on' + eventType, callback);
   return {
    remove() {
     target.detachEvent('on' + eventType, callback);
    }
   };
  }
 }
};

export default EventListener;

封裝的事件監(jiān)聽

使用

使用content屬性來決定hover時的提示信息。由placement屬性決定展示效果:placement屬性值為:方向-對齊位置;四個方向:top、left、right、bottom。trigger屬性用于設置觸發(fā)tooltip的方式,默認為hover。

<d-tooltip content="我是tooltip">
 <d-button type="text">鼠標移動到我上面試試</d-button>
</d-tooltip>
<d-tooltip content="我是tooltip" trigger="click">
 <d-button type="text">點我試試</d-button>
</d-tooltip>

content內(nèi)容分發(fā)

設置一個名為content的slot。

<d-tooltip>
 <d-button type="text">鼠標移動到我上面試試</d-button>
 <p slot="content" class="tooltip-content">我是內(nèi)容分發(fā)的conent。</p>
</d-tooltip>

Attributes

參數(shù) 說明 類型 可選值 默認值
content 顯示的內(nèi)容,也可以通過 slot#content 傳入 DOM String
placement Tooltip 的出現(xiàn)位置 String top/right/bottom/left top
trigger tooltip觸發(fā)方式 String hover

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • axios簡單實現(xiàn)小程序延時loading指示

    axios簡單實現(xiàn)小程序延時loading指示

    這篇文章主要介紹了axios簡單實現(xiàn)小程序延時loading指示,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Vue?transition組件簡單實現(xiàn)數(shù)字滾動

    Vue?transition組件簡單實現(xiàn)數(shù)字滾動

    這篇文章主要為大家介紹了Vue?transition組件簡單實現(xiàn)數(shù)字滾動示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 解決vscode?Better?Comments插件在vue文件中不顯示相對應的顏色的問題

    解決vscode?Better?Comments插件在vue文件中不顯示相對應的顏色的問題

    最近使用了Better?Comments這款插件,發(fā)現(xiàn)在ts文件中可以顯示對應的顏色,但在vue文件中并不顯示對應顏色?,下面小編給大家分享解決方法,感興趣的朋友跟隨小編一起看看吧
    2022-09-09
  • Vue2.0實現(xiàn)自適應分辨率

    Vue2.0實現(xiàn)自適應分辨率

    這篇文章主要為大家詳細介紹了Vue2.0實現(xiàn)自適應分辨率,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Vue實現(xiàn)選擇城市功能

    Vue實現(xiàn)選擇城市功能

    這篇文章主要介紹了Vue實現(xiàn)選擇城市功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • 解讀vue分頁面打包方式

    解讀vue分頁面打包方式

    這篇文章主要介紹了解讀vue分頁面打包方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 淺談vuex 閑置狀態(tài)重置方案

    淺談vuex 閑置狀態(tài)重置方案

    本篇文章主要介紹了vuex 閑置狀態(tài)重置方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 在移動端使用vue-router和keep-alive的方法示例

    在移動端使用vue-router和keep-alive的方法示例

    這篇文章主要介紹了在移動端使用vue-router和keep-alive的方法示例,vue-router與keep-alive提供的路由體驗與移動端是有一定差別的,感興趣的小伙伴們可以參考一下
    2018-12-12
  • vue?首頁加載,速度優(yōu)化及解決首頁白屏的問題

    vue?首頁加載,速度優(yōu)化及解決首頁白屏的問題

    這篇文章主要介紹了vue?首頁加載,速度優(yōu)化及解決首頁白屏的問題,具有很好的參考價值,希望對大家有所幫助。
    2022-10-10
  • axios+Vue實現(xiàn)上傳文件顯示進度功能

    axios+Vue實現(xiàn)上傳文件顯示進度功能

    這篇文章主要介紹了axios+Vue上傳文件顯示進度效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04

最新評論