zoukankan      html  css  js  c++  java
  • vue实现轮播效果

    vue实现轮播效果

    效果如下:(不好意思,图有点大;)

    功能:点击左侧图片,右侧出现相应的图片;同时左侧边框变颜色。

     

     代码如下:(也可以直接下载文件)

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <title>test</title>
      <script src="vue.js"></script>
    </head>
    
    <body>
      <div id="app">
        <!-- 左侧img图片 -->
        <div class="leftlList ">
          <div v-for="(leftImg,index) in leftImgs" :key = "index">
            <label :key="index + 'A'">{{ index + 1 }}</label>
            <div @click="clickImg(index)" :key="index + 'B'" class="img" :class="[leftIndex == index?'listImgActived': '']">
              <img :src="leftImg.url">
              <!-- 11111 -->
            </div>
          </div>
        </div>
        <!-- 分割线 -->
        <div class="string"></div>
    
        <!-- 中间展示的图片 -->
        <div class="centerImg" v-for="(leftImg,index) in leftImgs" :key="index + 'C'">
          <img :src="leftImg.url" v-show="index == leftIndex">
        </div>
      </div>
    
      <script>
        new Vue({
          el: '#app',
          data: {
            leftIndex: 0,
            leftImgs: [{
                url: 'src/assets/a.jpg'
              },
              {
                url: 'src/assets/b.jpg'
              },
              {
                url: 'src/assets/c.jpg'
              },
              {
                url: 'src/assets/d.jpg'
              }
            ]
          },
          methods: {
            clickImg(index) {
              this.leftIndex = index;
            }
          }
        });
    
      </script>
    
      <style>
        body {
          padding: 0;
          margin: 0
        }
    
        #app {
          position: absolute;
          background-color: black;
           100%;
          height: 100%;
        }
    
        .leftlList {
          color: white;
          position: absolute;
          margin-top: 40px;
          margin-left: 40px;
           190px;
          height: calc(100% - 80px);
        }
    
        .leftlList div .img {
          display: inline-block;
          margin: 16px 14px;
          text-align: center;
          vertical-align: middle;
          /*  130px;
          height: 130px;
          line-height: 130px; */
        }
    
        .leftlList div .img img {
           130px;
          height: 130px;
          line-height: 130px;
        }
    
        #app .string {
          position: absolute;
          margin-left: 220px;
          margin-top: 40px;
          height: calc(100% - 80px);
          border: 2px solid pink;
          display: inline-block;
        }
    
        .centerImg {
          position: absolute;
           calc(100% - 430px);
          margin-left: 300px;
          margin-top: 70px;
          text-align: center;
          vertical-align: middle;
        }
    
        .listImgActived {
          border: 2px solid aqua;
        }
    
      </style>
    </body>
    
    </html>

     如果左侧不是图片,而是文字的话;

    可以把

     /*  130px;
          height: 130px;
          line-height: 130px; */
    这三行代码取消。

    另外,如果出现下面这样的报错的话:

     

     是因为key的值重复了。所以,只需要把key的值改下就可以了:

    例:

    <div v-for="(leftlist, index) in leftlists" :key="index"></div>

    <div v-for="(leftlist, index) in leftlists2" :key="'I'+ index"></div>

    <div v-for="(leftlist, index) in leftlists3" :key="'II',+ index"></div>

    这里例子中的 I,II 字符可以替换成你自己定义的任意字符,只是为了保证key的唯一性

    如果图片加载不出来,尝试一下

    url: require("../../assets/ckBG.jpg")
     
  • 相关阅读:
    4月13日
    java线程池开启多线程
    cenos 7 zookeeper Error contacting service. It is probably not running
    io.lettuce.core.RedisCommandTimeoutException: Command timed out
    springboot java jar指定启动的jar外部配置文件
    ApplicationContextAware
    YYYYMMdd和yyyyMMdd的区别
    gpg加解密异常
    第四周学习及读书笔记
    第3章 直流电机的工作原理及特性 学习笔记(二)
  • 原文地址:https://www.cnblogs.com/yaosusu/p/11523755.html
Copyright © 2011-2022 走看看