H5輪播圖Vue-cli3使用vue-awesome-swiper縮略圖靠左對(duì)齊

需求:H5活動(dòng)帶縮略圖的輪播担映,canvas將底圖與元素圖合成一張嘱蛋,縮略圖左對(duì)齊,滑動(dòng)縮略圖時(shí)大圖不聯(lián)動(dòng)媒抠。

問(wèn)題vue-awesome-swiper官方文檔帶縮略圖的示例昧甘,上下聯(lián)動(dòng)且縮略圖居中對(duì)齊户秤,當(dāng)設(shè)置參數(shù)centeredSlides: true左對(duì)齊了,但8張縮略圖,第5張開(kāi)始不能點(diǎn)擊际乘。

方案:將vue-awesome-swiper官方分頁(yè)器示例改成縮略圖樣式的。

效果

效果圖

demodemo效果網(wǎng)頁(yè)

備注:使用了postcss-px2rem將px轉(zhuǎn)成rem朋魔,使用配置可以上另一篇文章函荣。

1、安裝vue-awesome-swiper

yarn add vue-awesome-swiper // npm install vue-awesome-swiper

2浸踩、在main.js中引入

import Vue from 'vue'
import App from './App.vue'

import VueAwesomeSwiper from 'vue-awesome-swiper' // 引入vue-awesome-swiper
import './assets/css/reset.css' // 去掉標(biāo)簽的默認(rèn)樣式
import 'swiper/dist/css/swiper.css' // 引入swiper樣式
Vue.use(VueAwesomeSwiper /* { default global options } */) // 使用插件

new Vue({
  render: h => h(App)
}).$mount('#app')

3叔汁、Home.vue


<template>
  <div class="home">
    <div class="swiperWrap" v-if="recordsList.length > 0">
      <swiper :options="swiperOption" class="gallery-top" ref="swiperTop" @slideChangeTransitionEnd="setCurrIndex">
        <swiper-slide v-for="(item, index) in recordsList" :key="index">
          <div class="swiper-zoom-container">
            <img :src="item.backImageUrl" class="itemImg" :id="'backBg'+index"/>
            <img :src="item.coverImageUrl" class="itemImg" :id="'coverBg'+index"/>
          </div>
        </swiper-slide>
      </swiper>
    </div>
    <div class="swiper-pagination swiper-pagination-bullets" ref="navList" id="navList" slot="pagination"></div>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return{
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          spaceBetween: 10,
          clickable: true,
          renderBullet: (index, className) => {
            this.activeIndex = index
            // console.log('index:'+index)
            return `<span id=a${index} class="${className} swiper-pagination-bullet-custom">
            <img src="${this.recordsList[index].iconImageUrl}" class="iconImg" alt="0" />
            <em class="redPoint${index}"></em>
            </span>`
          }
        }
      },
      activeIndex: 0, // 當(dāng)前輪播的圖
      recordsList: [
        {
          "id": 1,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon4.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c1.png",
          "backImageUrl": "http://d.paper.i4.cn/max/2016/11/25/14/1480055792014_800031.jpeg",
        },
        {
          "id": 2,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon2.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c2.png",
          "backImageUrl": "http://img.creasy.365zhang.com/bg2.png",
        },
        {
          "id": 3,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon3.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c3.png",
          "backImageUrl": "http://img.creasy.365zhang.com/bg3.png",
        },
        {
          "id": 4,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon1.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c4.png",
          "backImageUrl": "http://img.creasy.365zhang.com/bg4.png",
        },
        {
          "id": 5,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon5.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c5.png",
          "backImageUrl": "http://img.creasy.365zhang.com/bg1.png",
        },
        {
          "id": 6,
          "iconImageUrl": "http://img.creasy.365zhang.com/icon6.png",
          "coverImageUrl": "http://img.creasy.365zhang.com/c6.png",
          "backImageUrl": "http://img.creasy.365zhang.com/bg2.png",
        }
      ]
    }
  },
  beforeMount() {
    let timer1 = setInterval(() => {
      let rootFontSize = (document.documentElement.clientWidth || document.body.clientWidth) / 10;
      document.getElementsByTagName("html")[0].style.fontSize = rootFontSize + "px";
      if (rootFontSize > 32) {
        clearInterval(timer1);
      }
    }, 60);
    let timer2 = setTimeout(() => {
      clearInterval(timer1);
      clearTimeout(timer2);
    }, 3000);
  },
  methods: {
    // 當(dāng)前模板索引
    setCurrIndex() {
      this.activeIndex = this.$refs.swiperTop.swiper.activeIndex;
    }
  }
}
</script>
<style lang="scss">
.home{
  background: #161616;
  height: 100%;
  padding-top: 128px;
  box-sizing: border-box;
}
.swiperWrap {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  margin-bottom: 25px;
}
.gallery-top {
  width: 630px;
  height: 840px;
}
.swiper-slide{
  width: 630px!important;
}
.swiper-slide-active{
  width: 630px!important;
}
.swiper-zoom-container img {
  border-radius: 30px;
}
.itemImg {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

// 縮略圖 icon 樣式,我是將分頁(yè)器樣式覆蓋改成縮略圖的
::-webkit-scrollbar{width:0;height:0}
.swiper-pagination {
  width: 100%;
  height: 180px;
  box-sizing: border-box;
  padding: 16px 25px;
  overflow-x: scroll;
  white-space: nowrap;
  position: static!important;
  overflow-y: hidden;
}
.swiper-pagination-bullet {
  width: 148px!important;
  height: 148px!important;
  border-radius: 24px!important;
  background: #d0d0d0!important;
  padding: 6px;
  border: 4px solid transparent;
  box-sizing: border-box;
  opacity: 1!important;
  margin-right: 14px;
  position: relative;
  font-size: 0;
}
.swiper-pagination-bullet-active {
  background: #fff!important;
  border: 4px solid #5c46ff;
  box-sizing: border-box;
}
.swiper-pagination-bullet:focus {
  outline: none!important;
}
.swiper-pagination-bullet-active:focus {
  outline: none!important;
}
.swiper-pagination-bullet:active {
  outline: none!important;
}
.swiper-pagination-bullet-active:active {
  outline: none!important;
}
.iconImg{
  width: 128px;
  height: 128px;
  border-radius: 20px;
}
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市据块,隨后出現(xiàn)的幾起案子码邻,更是在濱河造成了極大的恐慌,老刑警劉巖另假,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冒滩,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡浪谴,警方通過(guò)查閱死者的電腦和手機(jī)开睡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)苟耻,“玉大人篇恒,你說(shuō)我怎么就攤上這事⌒渍龋” “怎么了胁艰?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)智蝠。 經(jīng)常有香客問(wèn)我腾么,道長(zhǎng),這世上最難降的妖魔是什么杈湾? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任解虱,我火速辦了婚禮,結(jié)果婚禮上漆撞,老公的妹妹穿的比我還像新娘殴泰。我一直安慰自己,他們只是感情好浮驳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布悍汛。 她就那樣靜靜地躺著,像睡著了一般至会。 火紅的嫁衣襯著肌膚如雪离咐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天奉件,我揣著相機(jī)與錄音宵蛀,去河邊找鬼。 笑死瓶蚂,一個(gè)胖子當(dāng)著我的面吹牛糖埋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播窃这,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼瞳别,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼征候!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起祟敛,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疤坝,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后馆铁,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體跑揉,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年埠巨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了历谍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡辣垒,死狀恐怖望侈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情勋桶,我是刑警寧澤脱衙,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站例驹,受9級(jí)特大地震影響捐韩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鹃锈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一荤胁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧仪召,春花似錦寨蹋、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)秸苗。三九已至召娜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間惊楼,已是汗流浹背玖瘸。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留檀咙,地道東北人雅倒。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像弧可,于是被迫代替她去往敵國(guó)和親蔑匣。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353