解決Vue的項(xiàng)目使用Element ui 走馬燈無法實(shí)現(xiàn)的問題
1.在vue項(xiàng)目中引入element ui
http://element.eleme.io/#/zh-CN/component/carousel
引入后,HTML部分
<el-carousel height='150px'><el-carousel-item v-for='item in imgList' :key='item' ><h3><img :src='http://www.leifengta.com.cn/bcjs/item' alt=''> </h3></el-carousel-item></el-carousel>
JS部分
<script>export default {data(){return {imgList:[require(’../../assets/img/images/a1.png’),require(’../../assets/img/images/a2.png’),require(’../../assets/img/images/a3.png’),require(’../../assets/img/images/a4.png’),require(’../../assets/img/images/a5.png’)]}},components: {}}</script>
用webpack搭建的項(xiàng)目不能直接使用絕對(duì)路徑,要用require,如果不使用這個(gè),必須是線上圖片。http類型的
補(bǔ)充知識(shí):基于vue 使用element UI框架 實(shí)現(xiàn)走馬燈 圖片高度自適應(yīng)
走馬燈代碼結(jié)構(gòu)走一遍 (imgList數(shù)組在data中聲明,此為本地?cái)?shù)據(jù))
data() { return{ // 圖片需要引入, 否則無法顯示 imgList: [ {id: 0, idView: require(’../assets/images/banner3.jpg’)}, {id: 1, name: ’詳情’, idView: require(’../assets/images/banner2.jpg’)}, {id: 2, name: ’推薦’, idView: require(’../assets/images/banner1.jpg’)} ] }}
<template> <el-carousel :interval='5000' arrow='always' :height='imgHeight'> <el-carousel-item v-for='item in imgList' :key='item.id'> <el-row> <el-col :span='24'><img ref='imgHeight' :src='http://www.leifengta.com.cn/bcjs/item.idView' /></el-col> </el-row> </el-carousel-item> </el-carousel></template>
element UI 官網(wǎng)地址戳這里
http://element-cn.eleme.io/#/zh-CN/component/carousel
Carousel 中有一個(gè)height參數(shù) 如果給固定值620px,那么它會(huì)出現(xiàn)如圖效果, 圖片的寬高隨可視窗口的改變等比放大或縮小,可視窗口縮小,圖片的寬度和高度縮小, 輪播圖的固定高度不變, so...如圖所示 如果圖片給height: 100%; 屬性,圖片會(huì)拉伸;好吧,那就換一個(gè)auto,則如圖所示
所以,要想圖片正常顯示,又不會(huì)出現(xiàn)空白條的辦法,就是動(dòng)態(tài)改變輪播圖的高度跟圖片高度相等即可。
首先獲取圖片的高度,通過ref來獲取DOM元素
監(jiān)聽窗口發(fā)生改變時(shí),獲取img的高度,給輪播圖height屬性添加屬性值
that.imgHeight = ’620px’window.onresize = function temp() { // 通過點(diǎn)語法獲取img的height屬性值 that.imgHeight = `${that.$refs.imgHeight[’0’].height}px`}
以上這篇解決Vue的項(xiàng)目使用Element ui 走馬燈無法實(shí)現(xiàn)的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 基于android studio的layout的xml文件的創(chuàng)建方式3. 編程語言PHP在Web開發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?4. 解決Android studio xml界面無法預(yù)覽問題5. 什么是python的自省6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)7. 圖文詳解vue中proto文件的函數(shù)調(diào)用8. Android如何加載Base64編碼格式圖片9. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案10. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備