轮播图

Vue

缩略图控制循环(级联)

1.npm安装vue-awesome-swiper

npm install vue-awesome-swiper --save

2.全局挂载,在main.js中添加

import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper)

3.组件内复制修改代码
缩略图控制循环
4.修改样式和配置

direction: 'vertical',

封装全屏轮播图

<template>
  <div class="banner-self">
    <swiper :options="swiperOption" class="swiper-box" ref="mySwiper">
        <swiper-slide class="swiper-item" v-for="item in bannerList" :key='item.id'>
            <div class="banner" :style="{
                backgroundImage: 'url(' + item.pcBannerImg + ')'
            }">
            </div>
        </swiper-slide>                        
    </swiper>
    <!-- <div class="banner" :style="style"></div> -->
    <div class="circle">            
        <p class="title_ch">{{titleCh}}</p>
        <p class="title_bar"></p>
        <p class="title_en">{{titleEn}}</p>
    </div>
  </div>
</template>

<script>
import api from "@/api/api"
export default {
  name: 'bannerSelf',
  data () {
    return {
        bannerList: [],
        swiper: null,
        swiperOption: {
            direction: 'horizontal',
            slidesPerView: 1,
            spaceBetween: 0,
            mousewheel: false,
            autoHeight: false,
            resistanceRatio: 0,
            on: {
                slideChangeTransitionEnd: function () {
                    // alert(this.activeIndex);//切换结束时,告诉我现在是第几个slide
                },
            },
        }
    }
  }, 
    props: {
        imgUrl: {
          type: String,
          required: true,          
        }, 
        titleCh: {
          type: String,
          default: '中文'        
        },
        titleEn: {
          type: String,
          default: 'ENHLISH'        
        }, 
        bannerType: {
            type: Number,
            default: 0,
            // required: true, 
        },    
    },
    methods: {
        getBannerList() {            
            const params = {
                bannerType: this.bannerType
            }
            api.home.banner(params).then(res => {                            
                if (res.status == 200) {
                    this.bannerList = res.data                    
                }
            })            
        },
    },
    computed: {
        style() {
            return {
                backgroundImage: 'url(' + this.imgUrl + ')'
            }
        },
        // bannerList () {
        //     return [
        //         {
        //             pcBannerImg: this.imgUrl
        //         },
        //         {
        //             pcBannerImg: this.imgUrl
        //         },
        //     ]
        // }
    },
    watch: {
        bannerType: {
            handler (newValue, oldValue) {
          if (newValue) {                    
                    this.getBannerList()
                }
         },
            immediate: true
        }
    }
}
</script>

<style lang="less" scoped>
.banner-self {
    width: 100%;
    height: 100%;
}
.swiper-box {
  width: 100%;
  height: 100%;
  margin: 0 auto;
}
.banner {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}
.circle {
    display: inline-block;
    width: 6.8rem;
    height: 6.8rem;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    margin: 0 auto;
    text-align: center;
    position: absolute;
    z-index: 1000;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    border: 3px solid hsla(0,0%,100%,.4);
    .title_ch {
        margin-top: 1.9rem;
        font-size: 0.72rem;
        color: #ffffff;
    }
    .title_en {
        font-size: 0.4rem;
        color: #ffffff;
    }
    .title_bar {
        margin: 0.34rem auto;
        width: 3.6rem;
        height: 2px;
        background-color: #fff;
        opacity: 0.67;
    }
}
</style>

轮播图和其他区域联动

思想:当悬浮文章标题时,显示对应的轮播图。使用鼠标悬浮事件,让轮播图滑动到对应的图片。

<template>
    <div class="container clearfix">
        <div class="main-left fl">
            <swiper :options="swiperOption" class="swiper-box" ref="mySwiper">
                <swiper-slide class="swiper-item" v-for="item in newsList" :key='item.id'>
                    <div class="banner" :style="{
                        backgroundImage: 'url(' + item.thumbnail + ')'
                    }">
                    </div>
                </swiper-slide>                        
            </swiper>
        </div>
        <div class="main-right fr">
            <div class="main-content">
                <h2 class="page-tilte">新闻资讯</h2>
                <h3 class="page-subtilte">NEWS</h3>
                <ul @mouseover="slideStop" @mouseout="slideStart">
                    <li class="item" v-for="(item, index) in newsList" :key="item.id">
                        <h3 class="article-title" :class="slideIndex === index?'article-title-active':''" @click="toDetail(item.id)" @mouseover="changeSlide(index)">{{item.articleTitle}}</h3>                        
                        <div class="bottom clearfix">
                            <span class="time fl">{{item.updateTime}}</span>
                            <a class="fr to-detail" @click="toDetail(item.id)">查看详情</a>
                        </div>
                    </li>                    
                </ul>     
                <span class="more" @click="toList">更多&gt;</span>                           
            </div>
        </div>
    </div>
</template>

<script>
import api from "@/api/api"
import allIcon from '@/utils/local-img'
export default {
    data() {
        return {
            allIcon: allIcon,
            newsList: [],
            slideIndex: 0,
            // swiper: null,
            swiperOption: {}
        }
    },
    created () {
        this.initSlide()
        this.getNewsList()        
    },
    methods: {
        // 获取列表
        getNewsList () {
            api.home.newsList().then(res => {
                if (res.status == 200) {
                    res.data.list.forEach((item) => {
                        item.updateTime = this.$moment(item.updateTime).format('YYYY/MM/DD')
                    })
                    this.newsList = res.data.list
                }
            })            
        },
        // 跳转新闻
        toList () {
            this.$router.push({
                path: '/news',                          
            })
        },
        toDetail (id) {
           this.$router.push({
                path: '/newsDetail',
                query: {
                    id: id
                }                
            }) 
        },
        /**
         * @desc 轮播图-----------------------------------------
         */
        // 初始化
        initSlide () {
            let that = this
            that.swiperOption = {
                autoplay: {
                    delay: 1500,
                    stopOnLastSlide: false,
                    disableOnInteraction: true,
                },
                direction: 'horizontal',
                slidesPerView: 1,
                spaceBetween: 0,
                mousewheel: false,
                autoHeight: false,
                resistanceRatio: 0,
                on: {                    
                    slideChangeTransitionEnd: function () {
                        // alert(this.activeIndex);//切换结束时,告诉我现在是第几个slide
                        that.slideIndex = this.activeIndex
                    },
                },
            }
        },
        // 改变轮播
        changeSlide (index) {            
            this.swiper.slideTo(index, 1000, false)
        },
        // 自动轮播暂停
        slideStop () {
            this.swiper.autoplay.stop();
        },
        // 自动轮播开启
        slideStart () {
            this.swiper.autoplay.start();
        },
    },    
    computed: {
        swiper () {
            return this.$refs.mySwiper.swiper
        },
        style() {
            return {
                backgroundImage: 'url(' + allIcon.home.news + ')'
            }
        }
    }
}
</script>
<style lang="less" scoped>
// 左右布局
.main-left {
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}
.container {
  height: 100%;
  overflow: hidden;
}
.main-left {
  width: 50%;
  height: 100%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
  }
}
.main-right {
  width: 50%;
  display: flex;  
//   align-items: center;
//   justify-content: center;
}
// 主体内容
.main-content {
    position: relative;
    width: 100%;  
    // height: 5rem;  
    padding: 1.4rem 2rem 0 1.4rem;
    // padding-top: 1.5rem;
}
.page-tilte {  
  font-size: 0.34rem;
  color: #333;  
//   font-weight: bold;  
}
.page-subtilte {
    font-size: 0.2rem;
    color: #c4c4c4;  
    margin-bottom: 0.4rem;
}
// 列表项
.item {
    margin-bottom: 0.3rem;
}
.article-title {  
    cursor: pointer;
    font-size: 0.2rem;
    color: #686868;  
    line-height: 0.3rem;
    // margin-bottom: 5px;
    text-align: justify;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    &:hover {
        color: #FF7D41;
    }
}
.article-title-active {
    color: #FF7D41;
}

.bottom {  
  font-size: 0.14rem;
  color: #b3b3b3;  
  line-height: 0.3rem;
}
// .time {
//     font-size:12px;
// }
.to-detail {
  color: #FF7D41;
}
// 更多
.more {  
    cursor: pointer;
    position: absolute;
    font-size: 0.18rem;
    color: #FF7D41; 
    top: 1.2rem;
    right: 2rem; 
}

.swiper-box {
  width: 100%;
  height: 100%;
  margin: 0 auto;
}
.banner {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}
</style>

更改swiper上下切换按钮样式

outline: none;为去除点击时的边线效果

<div class="partner-institutions">      
  <swiper ref="mySwiper" :options="swiperOptionsPartner" class="swiper-box">
    <swiper-slide v-for="(item,index) in partnerList" :key="index">
      <div class="banner-slide">
        <img :src="item" alt="">
      </div>
    </swiper-slide>        
  </swiper>
  <div class="swiper-button-prev prev1"></div>
  <div class="swiper-button-next next1"></div>
</div>
.partner-institutions
  margin-top 99px
  margin-bottom 99px
  position relative 
  >>> .swiper-button-prev:after, .swiper-button-next:after {
    font-size 20px    
  }
  .swiper-box
    width 900px        
    .banner-slide
      width 100px
      height 120px
      display flex
      justify-content center
      align-items center     
      img 
        width 100%           
  .prev1 {
      width: 40px;
      height: 40px;
      border-radius: 50%;  
      box-sizing border-box      
      background-color: rgba(1,1,1,0.5);
      color: #fff;          
      cursor: pointer;    
      z-index: 90;
      border 0 
      outline: none;         
      &:hover {
          background-color: rgba(6,126,242,1);
      }
  }
  .next1 {    
      width: 40px;
      height: 40px;
      border-radius: 50%;                     
      background-color: rgba(1,1,1,0.5);
      color: #fff;          
      cursor: pointer;  
      z-index: 90;  
      outline: none;  
      &:hover {
          background-color: rgba(6,126,242,1);
      } 
  }
swiperOptionsPartner: {        
  slidesPerView: 6,
  navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
  },  
  loop : true,
  autoplay: {
    delay: 2000,
    disableOnInteraction: false,
  },               
},

参考

swiper demo
swiper github

网站导航

网站导航

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容