溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue怎么實現(xiàn)一個移動端輕量級輪播組件

發(fā)布時間:2021-06-02 17:00:24 來源:億速云 閱讀:171 作者:Leah 欄目:web開發(fā)

今天就跟大家聊聊有關(guān)使用vue怎么實現(xiàn)一個移動端輕量級輪播組件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

English Document

安裝

npm install c-swipe --save

使用

注冊組件

// main.js
// 引入 c-swipe 主文件
import 'c-swipe/dist/swipe.css';
import { Swipe, SwipeItem } from 'c-swipe';
// 全局注冊組件
Vue.component('swipe', Swipe);
Vue.component('swipe-item', SwipeItem);

在 .vue 單文件組件中使用:

<swipe
 v-model="index"
 
>
 <swipe-item >item1</swipe-item>
 <swipe-item >item2</swipe-item>
 <swipe-item >item3</swipe-item>
</swipe>
new Vue({
 data: function () {
  return {
   index: 0, // two way
  };
 },
});

或者,你想在 html 標簽中直接引用

<link href="../node-modules/c-swipe/dist/swipe.css" rel="external nofollow" rel="stylesheet"></head>
<script type="text/javascript" src="../node-modules/c-swipe/dist/swipe.js"></script>
var vueSwipe = swipe.Swipe;
var vueSwipeItem = swipe.SwipeItem;
new Vue({
 el: 'body',
 // 注冊組件
 components: {
  'swipe': vueSwipe,
  'swipe-item': vueSwipeItem
 },
 // ...
 // ...
});

配置

選項類型默認描述
v-modelNumber0綁定了當前顯示卡片的索引,該數(shù)據(jù)為雙向綁定,可通過更改 v-model 的值直接更改當前顯示卡片
paginationBooleantrue是否需要默認樣式的導航器
loopBooleantrue循環(huán)切換
autoplayTimeNumber0單位 ms,自動切換卡片的時間間隔,值為 0 時不自動切換
speedNumber300單位 ms, 切換卡片時的過渡效果的播放時長
minMoveDistanceString'20%'成功觸發(fā)切換卡片事件的最小滑動距離,可以傳入百分比,如 '20%',或者傳入具體像素距離,如 '80px'。

方法

swipe.reset()

c-swipe 內(nèi)部將重新計算 Swipe 的寬度,并根據(jù)新的寬度來計算滾屏的距離。這個可以解決容器重置大小后 c-swipe 滾屏距離不正確的問題。

例:

<swipe ref="swipe">
 <swipe-item>item1</swipe-item>
 <swipe-item>item2</swipe-item>
 <swipe-item>item3</swipe-item>
</swipe>
<script>
 export default {
  // ...
  // ...
  handleResize() {
   this.$refs.swipe.reset();
  }
  mounted() {
   // 避免上下文丟失
   this.handleResize = this.handleResize.bind(this);
   window.addEventListener('resize', this.handleResize);
  },
  destroyed() {
   window.removeEventListener('resize', this.handleResize);
  }
  // ...
  // ...
 }
</script>

看完上述內(nèi)容,你們對使用vue怎么實現(xiàn)一個移動端輕量級輪播組件有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI