图片的切换:每天一个效果(1)

这个博客展示了如何在Vue项目中创建一个图片轮播组件。通过使用`v-for`指令遍历图片列表,并结合CSS样式及点击事件改变活动图片索引,实现了背景图片的切换。同时,代码应用了响应式设计,确保在不同屏幕尺寸下展示效果良好。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 图片效果:

 代码,在vue项目中直接打开可以用

<template>
  <div class="index" >
    <div class="container">
      <div v-for="(item,index) in list" :key="index" @click="openPho(index)" class="panel" :class="index == i ? 'active':''"  :style='item'>
      
      </div>
    

    </div>
  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      i:0,
      list: [
        "background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')",
        "background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')",
        "background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80')",
        "background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')",
        "background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')",
      ],
    };
  },
  watch: {},
  created() {},
  methods: {
    openPho(i){
      this.i=i
      console.log(i);
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="less" scoped >
.container {
  display: flex;
  width: 90vw;
}
.panel {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 50vh;
  border-radius: 50px;
  color: #fff;
  cursor: pointer;
  flex: 0.5;
  margin: 10px;
  position: relative;
  -webkit-transition: all 700ms ease-in;
}

.panel h3 {
  font-size: 24px;
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0;
}

.panel.active {
  flex: 5;
}

.panel.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.4s;
}

@media (max-width: 480px) {
  .container {
    width: 100vw;
  }

  .panel:nth-of-type(4),
  .panel:nth-of-type(5) {
    display: none;
  }
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值