vue實現(xiàn)簡單瀑布流布局
vue簡單實現(xiàn)瀑布流布局的一種方式(vue瀑布流組件),供大家參考,具體內(nèi)容如下
vue中的瀑布流布局組件
需求:圖片容器寬度固定,高度根據(jù)圖片自適應(yīng),圖片一行不能排列時候,換行依次從左往右排列。(瀑布流概念)
另外結(jié)合vue-lazy實現(xiàn)懶加載 ( npm i vue-lazyload --save-dev)
使用也很簡單,在需要懶加載的img標(biāo)簽上將:src換成v-lazy
父組件傳遞數(shù)據(jù):
waterfallData:[ { e_img: "test.jpg", // 圖片 e_intro: "描述信息", u_img: "test.jpeg", // 標(biāo)記圖 u_name: "開發(fā)者"}, { e_img: "test.jpg", e_intro: "描述信息", u_img: "test.jpeg", u_name: "開發(fā)者"} ]
定寬不定高瀑布流布局子組件
<template> <div> <div class="waterfall px-container clearfix" v-cloak> <ul class="px-waterfall left" v-for="(el,i) in newWaterfallData" :key="i"> <li v-for="(item,index) in el" :key="index"> <div style="position: relative;" > <router-link to="/goodsdetail/1"> <img v-lazy="item.e_img" class="bg-img" alt="" v-loading="true"> </router-link> <div class="px-hot-tag" v-if="item.u_img"> <img :src="item.u_img" alt="" > </div> <h3 class="font22 color-fff fh" v-if="item.e_intro"> {{item.e_intro}}</h3> </div> </li> </ul> </div> </div> </template> <script> export default { data() { return { newWaterfallData: '', waterfallDataNumber:'' } }, created() { let [ ...waterfallData ] = this.waterfallData let [ ...newWaterfallData ]= [[],[],[]] waterfallData.forEach((el,i) => { switch( i%3 ) { case 0 : newWaterfallData[0].push(el) break case 1: newWaterfallData[1].push(el) break case 2: newWaterfallData[2].push(el) break } }); this.newWaterfallData = newWaterfallData }, props: [ 'waterfallData' ] } </script> <style lang="scss"> .px-container { width: 100%; max-width: 1200px; margin: 0 auto; } .clearfix:before, .clearfix:after { content: ""; display: block; clear: both; } .clearfix { zoom: 1; } .left { float: left; } . font22{ font-size:22px; } .color-fff { color:#fff; } .fh { overflow:hidden; } [v-cloak]{ display: none!important; } .waterfall { margin-top: 20px; } .px-waterfall { width: calc(380px); &:nth-child(3n+2) { margin: 0 30px; } img.bg-img { border-radius: 8px; } h3 { text-overflow:ellipsis; white-space: nowrap; text-overflow: ellipsis; height:30px; font-family:PingFangSC-Semibold; font-weight:600; line-height:30px; letter-spacing:6px; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); z-index: 999; } li{ background: #fff; border-radius: 8px; margin-bottom: 20px; } } .px-hot-tag { position: absolute; top: 0; border-radius: 8px; width: 71px; height: 30px; img { border-radius: 8px 0 8px 0; } } .photo{ position: relative; height: 25px; .keywordbox { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); height: 25px; } } .keyword { height: 25px; line-height: 25px; padding: 0 22px; text-align: center; .color-666-line { border-bottom:2px solid #666666; } .red-line { border-bottom:2px solid #F65050; } } </style>
如有問題,歡迎指正,如您有好的方案,敬請與我分享!
更多文章可以點擊《Vue.js前端組件學(xué)習(xí)教程》學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3?響應(yīng)式系統(tǒng)實現(xiàn)?computed
這篇文章主要介紹了?Vue3?響應(yīng)式系統(tǒng)實現(xiàn)?computed,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06關(guān)于Vue新搭檔TypeScript快速入門實踐
這篇文章主要介紹了關(guān)于Vue新搭檔TypeScript快速入門實踐,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue中v-if和v-for一起使用的弊端及解決辦法(同時使用 v-if 和 v-for不
當(dāng) v-if 和 v-for 同時存在于一個元素上的時候,v-if 會首先被執(zhí)行,這篇文章主要介紹了vue中v-if和v-for一起使用的弊端及解決辦法,需要的朋友可以參考下2023-07-07