本篇內(nèi)容主要講解“vueJS如何實(shí)現(xiàn)圖片橫向滑動(dòng)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“vueJS如何實(shí)現(xiàn)圖片橫向滑動(dòng)”吧!
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供茄子河網(wǎng)站建設(shè)、茄子河做網(wǎng)站、茄子河網(wǎng)站設(shè)計(jì)、茄子河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、茄子河企業(yè)網(wǎng)站模板建站服務(wù),十載茄子河做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
vueJS實(shí)現(xiàn)圖片橫向滑動(dòng)的方法:1、使用npm安裝vue-awesome-swiper;2、在main.js中引用vue-awesome-swiper;3、使用swiper實(shí)現(xiàn)左右滑動(dòng)切換圖片即可。
本文操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。
vueJS怎么實(shí)現(xiàn)圖片橫向滑動(dòng)?
vue使用swiper實(shí)現(xiàn)左右滑動(dòng)切換圖片:
使用npm 安裝vue-awesome-swiper
npm install vue-awesome-swiper --save
在main.js中引用
import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.user(VueAwesomeSwiper) import 'swiper/dist/css/swiper.css'
在組件中使用
<template> <div> <label>{{ time }}</label> <div id="star-pic-vue"> <template v-if="data"> <img e v-for="(item, index) in images" :src="item.url" :key="index" id="contract_url" @click="enlargePic(index)" /> <template v-if="isDialogShow"> </template> <el-dialog :visible.sync="centerDialogVisible" width="100%" modal close-on-click-modal custom-class="dialog" > <swiper :options="swiperOption" ref="mySwiper" style="height: 100%;"> <swiper-slide v-for="(img, index) in images" :key="index"> <div> <img :src="img.url" alt="" /> </div> </swiper-slide> </swiper> </el-dialog> </template> </div> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; export default { name: "PictureComponent", props: ["data", "maxShow", "time"], data() { return { centerDialogVisible: false, showPic: "", isDialogShow: false, activeIndex: 1, startX: 0, swiperOption: { width: window.innerWidth, zoom: true, initialSlide: 0 } }; }, computed: { images() { if (this.data instanceof Array && this.data.length > 2) { var value = this.data; return value.splice(0, this.maxShow); } else { return this.data; } } }, components: { swiper, swiperSlide }, methods: { // 放大圖片 enlargePic(i) { this.activeIndex = i; this.isDialogShow = true; // 使用$refs,如果ref是定位在有v-if、v-for、v-show中的DOM節(jié)點(diǎn), // 返回來的只能是undefined,因?yàn)樵趍ounted階段他們根本不存在 this.$nextTick(() => { var swiper = this.$refs.mySwiper.swiper; swiper.activeIndex = i; }); this.centerDialogVisible = true; } } }; </script> <style> .timeline { display: block; margin: 10px 20px 5px; } #star-pic-vue .el-dialog__wrapper { position: fixed; top: 0; right: 0; bottom: 0; left: 0; overflow: auto; margin: 0; background: #171717; } #star-pic-vue { width: 100%; height: auto; display: flex; flex-wrap: wrap; justify-content: stretch; padding: 3px 13px; img { width: 82px; height: 80px; margin: 4px 0px 0px; padding-right: 2px; } .dialog { img { width: 100%; height: 100%; margin: 0; } } .el-carousel__item h4 { color: #475669; font-size: 18px; opacity: 0.75; line-height: 300px; margin: 0; height: 100%; width: 100%; } .el-dialog__header { display: none; } .el-dialog__body { padding: 0 !important; margin: 0 !important; height: 460px; background: #171717; } .el-carousel { height: 100%; } .el-carousel__container { height: 410px; } .el-carousel__indicators--outside { margin-top: 20px; } } </style>
效果
$refs定位不到的主要原因是因?yàn)関-if、v-for、v-show這些語句如果依賴父組件傳來的參數(shù)的話,該參數(shù)是在mounted()階段子還沒獲取得到。
如果想要真正地在DOM加載完成后拿到數(shù)據(jù),就需要調(diào)用VUE的全局api : this.$nextTick(() => {})
到此,相信大家對(duì)“vueJS如何實(shí)現(xiàn)圖片橫向滑動(dòng)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站標(biāo)題:vueJS如何實(shí)現(xiàn)圖片橫向滑動(dòng)
網(wǎng)站URL:http://bm7419.com/article28/jdejcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、App開發(fā)、小程序開發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站策劃、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)