vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片
本文實(shí)例為大家分享了vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖:(懸浮時(shí)暫停,移出后自動(dòng)輪播)
①創(chuàng)建圖片滑動(dòng)輪播組件ImageSlider.vue,可設(shè)置輪播間隔interval,當(dāng)頁(yè)面沒(méi)被激活(用戶沒(méi)在瀏覽當(dāng)前頁(yè)面)時(shí),自動(dòng)暫停,重新瀏覽當(dāng)前頁(yè)面(被激活)時(shí),自動(dòng)輪播
<template> ? <div class="m-slider" @mouseenter="onStop" @mouseleave="onStart"> ? ? <div class="m-panel" ref="slider" :style="`width: ${width}px;`"> ? ? ? <div ? ? ? ? v-for="(item, index) in imageData" ? ? ? ? :key="index" ? ? ? ? class="m-image"> ? ? ? ? <img :src="item.src" :alt="item.title"/> ? ? ? ? <p class="u-img-title" :title="item.title">{{ item.title }}</p> ? ? ? </div> ? ? ? <div class="m-image"> ? ? ? ? <img :src="imageData[0].src" :alt="imageData[0].title"/> ? ? ? ? <p class="u-img-title" :title="imageData[0].title">{{ imageData[0].title }}</p> ? ? ? </div> ? ? </div> ? </div> </template> <script> export default { ? name: 'ImageSlider', ? props: { ? ? imageData: { // 圖片數(shù)組 ? ? ? type: Array, ? ? ? default: () => { ? ? ? ? return [] ? ? ? } ? ? }, ? ? interval: { // 滑動(dòng)輪播間隔 ? ? ? type: Number, ? ? ? default: 3000 ? ? } ? }, ? data () { ? ? return { ? ? ? visibilityChangeEvent: '', ? ? ? timer: null, ? ? ? imgWidth: 400, // 圖片寬度,用于生成容器寬度 ? ? ? activeIndex: 0 // 當(dāng)前展示的圖片 ? ? } ? }, ? computed: { ? ? width () { ? ? ? return (this.imageData.length + 1) * this.imgWidth ? ? } ? }, ? created () { ? ? var hiddenProperty = 'hidden' in document ? 'hidden' ? ? ? : 'webkitHidden' in document ? 'webkitHidden' ? ? ? ? : 'mozHidden' in document ? 'mozHidden' ? ? ? ? ? : null ? ? this.visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange') ? ? var onVisibilityChange = () => { ? ? ? if (!document[hiddenProperty]) { ? ? ? ? this.onStart() ? ? ? ? console.log('頁(yè)面激活') ? ? ? } else { ? ? ? ? this.onStop() ? ? ? ? console.log('頁(yè)面非激活') ? ? ? } ? ? } ? ? document.addEventListener(this.visibilityChangeEvent, onVisibilityChange) ? ? setTimeout(() => { ? ? ? this.timer = setInterval(() => { // 自動(dòng)切換 ? ? ? ? this.onMove() ? ? ? }, this.interval) ? ? }, this.interval) ? }, ? methods: { ? ? // 滑動(dòng)動(dòng)畫(huà)效果 ? ? slideEffect (target) { ? ? ? const offsetLeft = this.$refs.slider.offsetLeft // 求出元素的當(dāng)前偏移位置 ? ? ? let step = (target - offsetLeft) / 10 // 由快到慢的過(guò)渡效果 ? ? ? step = step > 0 ? Math.ceil(step) : Math.floor(step) // 對(duì)每次移動(dòng)的距離取整,ceil:向上取整,floor:向下取整 ? ? ? setTimeout(() => { ? ? ? ? if (target !== offsetLeft) { ? ? ? ? ? this.$refs.slider.style.left = offsetLeft + step + 'px' // 移動(dòng) ? ? ? ? ? this.slideEffect(target) ? ? ? ? } ? ? ? }, 15) // 每隔15ms執(zhí)行一次 ? ? }, ? ? onSlider (moveX) { ? ? ? const offset = this.$refs.slider.offsetLeft ? ? ? const target = offset + moveX // 要移動(dòng)的目標(biāo)位置 ? ? ? this.slideEffect(target) ? ? }, ? ? onMove () { ? ? ? if (this.activeIndex === this.imageData.length - 1) { // 最后一張 ? ? ? ? this.activeIndex = 0 // 顯示第一張 ? ? ? ? this.$refs.slider.style.left = 0 ? ? ? ? this.onSlider(-this.imgWidth) ? ? ? } else { ? ? ? ? this.activeIndex++ // 顯示下一張 ? ? ? ? this.onSlider(-this.imgWidth) ? ? ? } ? ? }, ? ? onStop () { ? ? ? clearInterval(this.timer) ? ? ? this.timer = null ? ? }, ? ? onStart () { ? ? ? clearInterval(this.timer) ? ? ? this.timer = setInterval(() => { ? ? ? ? this.onMove() ? ? ? }, this.interval) ? ? }, ? ? beforeDestroy () { ? ? ? document.removeEventListener(this.visibilityChangeEvent) ? ? ? clearInterval(this.timer) ? ? ? this.timer = null ? ? } ? } } </script> <style lang="less" scoped> @themeColor: #D93844; .m-slider { ? margin: 100px auto; ? width: 400px; ? height: 300px; ? overflow: hidden; ? position: relative; ? .m-panel { ? ? position: absolute; ? ? top: 0; ? ? left: 0; ? ? // width: 1600px; // (圖片數(shù)組長(zhǎng)度+1) * 圖片寬度 ? ? height: 300px; ? ? .m-image { ? ? ? float: left; ? ? ? width: 400px; ? ? ? height: 300px; ? ? ? img { ? ? ? ? width: 400px; ? ? ? ? height: 270px; ? ? ? ? cursor: pointer; ? ? ? } ? ? ? .u-img-title { ? ? ? ? width: 400px; ? ? ? ? font-size: 16px; ? ? ? ? color: #333; ? ? ? ? line-height: 30px; ? ? ? ? overflow: hidden; ? ? ? ? text-align: left; ? ? ? ? cursor: pointer; ? ? ? ? text-overflow: ellipsis; ? ? ? ? display: -webkit-box; ? ? ? ? -webkit-box-orient: vertical; ? ? ? ? -webkit-line-clamp: 1; ? ? ? ? &:hover { ? ? ? ? ? font-size: 16px; ? ? ? ? ? color: @themeColor; ? ? ? ? } ? ? ? } ? ? } ? } } </style>
②在要使用滑動(dòng)輪播圖片的頁(yè)面引入使用組件,并傳入圖片數(shù)組
<ImageSlider :imageData="imageData" :interval="3000" /> import ImageSlider from '@/components/ImageSlider' components: { ? ? ImageSlider } data () { ? ? return { ? ? ? imageData: [ ? ? ? ? { ? ? ? ? ? title: 'image-1,image-1,image-1,image-1,image-1...', ? ? ? ? ? src: '圖片地址' ? ? ? ? }, ? ? ? ? { ? ? ? ? ? title: 'image-2,image-2,image-2,image-2,image-2...', ? ? ? ? ? src: '圖片地址' ? ? ? ? }, ? ? ? ? { ? ? ? ? ? title: 'image-3,image-3,image-3,image-3,image-3...', ? ? ? ? ? src: '圖片地址' ? ? ? ? } ? ? ? ] ? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)圖片輪播組件思路及實(shí)例解析
- vue不操作dom實(shí)現(xiàn)圖片輪播的示例代碼
- vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼
- 使用Vue制作圖片輪播組件思路詳解
- Vue封裝Swiper實(shí)現(xiàn)圖片輪播效果
- 利用Vue實(shí)現(xiàn)移動(dòng)端圖片輪播組件的方法實(shí)例
- 利用vueJs實(shí)現(xiàn)圖片輪播實(shí)例代碼
- VUE開(kāi)發(fā)一個(gè)圖片輪播的組件示例代碼
- 基于vue.js實(shí)現(xiàn)圖片輪播效果
- vue實(shí)現(xiàn)輪播圖片
相關(guān)文章
基于vue-cli vue-router搭建底部導(dǎo)航欄移動(dòng)前端項(xiàng)目
這篇文章主要介紹了基于vue-cli vue-router搭建底部導(dǎo)航欄移動(dòng)前端項(xiàng)目,項(xiàng)目中主要用了Flex布局,以及viewport相關(guān)知識(shí),已達(dá)到適應(yīng)各終端屏幕的目的。需要的朋友可以參考下2018-02-02vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解
這篇文章主要介紹了vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03基于vue2.0的活動(dòng)倒計(jì)時(shí)組件countdown(附源碼下載)
這是一款基于vue2.0的活動(dòng)倒計(jì)時(shí)組件,可以使用服務(wù)端時(shí)間作為當(dāng)前時(shí)間,在倒計(jì)時(shí)開(kāi)始和結(jié)束時(shí)可以自定義回調(diào)函數(shù)。這篇文章主要介紹了基于vue2.0的活動(dòng)倒計(jì)時(shí)組件countdown,需要的朋友可以參考下2018-10-10使用Karma做vue組件單元測(cè)試的實(shí)現(xiàn)
這篇文章主要介紹了使用Karma做vue組件單元測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01vue中前進(jìn)刷新、后退緩存用戶瀏覽數(shù)據(jù)和瀏覽位置的實(shí)例講解
今天小編就為大家分享一篇vue中前進(jìn)刷新、后退緩存用戶瀏覽數(shù)據(jù)和瀏覽位置的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式
這篇文章主要介紹了vue頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09VUE 常規(guī)截取和特殊字符之前之后截取(實(shí)例代碼)
這篇文章主要介紹了VUE 常規(guī)截取和特殊字符之前之后截取,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10