解決Vue的項目使用Element ui 走馬燈無法實現的問題
1.在vue項目中引入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" height="300px" > <h3><img :src="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搭建的項目不能直接使用絕對路徑,要用require,如果不使用這個,必須是線上圖片。http類型的
補充知識:基于vue 使用element UI框架 實現走馬燈 圖片高度自適應
走馬燈代碼結構走一遍 (imgList數組在data中聲明,此為本地數據)
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" class="d_jump" :height="imgHeight"> <el-carousel-item v-for="item in imgList" :key="item.id"> <el-row> <el-col :span="24"><img ref="imgHeight" :src="item.idView" class="banner_img"/></el-col> </el-row> </el-carousel-item> </el-carousel> </template>
element UI 官網地址戳這里
http://element-cn.eleme.io/#/zh-CN/component/carousel
Carousel 中有一個height參數 如果給固定值620px,那么它會出現如圖效果, 圖片的寬高隨可視窗口的改變等比放大或縮小,可視窗口縮小,圖片的寬度和高度縮小, 輪播圖的固定高度不變, so...如圖所示 如果圖片給height: 100%; 屬性,圖片會拉伸;好吧,那就換一個auto,則如圖所示
所以,要想圖片正常顯示,又不會出現空白條的辦法,就是動態(tài)改變輪播圖的高度跟圖片高度相等即可。
首先獲取圖片的高度,通過ref來獲取DOM元素
監(jiān)聽窗口發(fā)生改變時,獲取img的高度,給輪播圖height屬性添加屬性值
that.imgHeight = '620px'
window.onresize = function temp() {
// 通過點語法獲取img的height屬性值
that.imgHeight = `${that.$refs.imgHeight['0'].height}px`
}
以上這篇解決Vue的項目使用Element ui 走馬燈無法實現的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項目
后臺管理系統(tǒng)是我們日常開發(fā)學習經常遇到的一個項目,下面這篇文章主要給大家介紹了關于Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項目的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
淺談vue 組件中的setInterval方法和window的不同
這篇文章主要介紹了淺談vue 組件中的setInterval方法和window的不同,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue中watch和computed的區(qū)別與使用方法
這篇文章主要給大家介紹了關于vue中watch和computed的區(qū)別與使用方法的相關資料,文中通過實例代碼結束的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-08-08

