解決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://www.piao2010.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://www.piao2010.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. XML實(shí)體注入深入理解2. XML入門(mén)的常見(jiàn)問(wèn)題(三)3. WMLScript腳本程序設(shè)計(jì)第1/9頁(yè)4. Xpath語(yǔ)法格式總結(jié)5. XML 非法字符(轉(zhuǎn)義字符)6. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. 不要在HTML中濫用div9. 利用CSS3新特性創(chuàng)建透明邊框三角10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
