解決Vue的項(xiàng)目使用Element ui 走馬燈無(wú)法實(shí)現(xiàn)的問(wè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://m.b3g6.com/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è),必須是線(xiàn)上圖片。http類(lèi)型的
補(bǔ)充知識(shí):基于vue 使用element UI框架 實(shí)現(xiàn)走馬燈 圖片高度自適應(yīng)
走馬燈代碼結(jié)構(gòu)走一遍 (imgList數(shù)組在data中聲明,此為本地?cái)?shù)據(jù))
data() { return{ // 圖片需要引入, 否則無(wú)法顯示 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://m.b3g6.com/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)改變輪播圖的高度跟圖片高度相等即可。
首先獲取圖片的高度,通過(guò)ref來(lái)獲取DOM元素
監(jiān)聽(tīng)窗口發(fā)生改變時(shí),獲取img的高度,給輪播圖height屬性添加屬性值
that.imgHeight = ’620px’window.onresize = function temp() { // 通過(guò)點(diǎn)語(yǔ)法獲取img的height屬性值 that.imgHeight = `${that.$refs.imgHeight[’0’].height}px`}
以上這篇解決Vue的項(xiàng)目使用Element ui 走馬燈無(wú)法實(shí)現(xiàn)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP驗(yàn)證碼工具-Securimage2. Vue 實(shí)現(xiàn)對(duì)quill-editor組件中的工具欄添加title3. JavaScript實(shí)現(xiàn)簡(jiǎn)單的彈窗效果4. 我所理解的JavaScript中的this指向5. javascript實(shí)現(xiàn)貪吃蛇小練習(xí)6. PHP利用curl發(fā)送HTTP請(qǐng)求的實(shí)例代碼7. Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求8. PHP單件模式和命令鏈模式的基礎(chǔ)知識(shí)9. 一文帶你徹底理解Java序列化和反序列化10. js實(shí)現(xiàn)碰撞檢測(cè)

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