vue实例之组件开发:图片轮播组件

news/2024/7/9 23:35:21 标签: vue

一、普通方式:

其中,index是关键。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
html, body{
    margin: 0;
    padding: 0;
}
.photo-player{
    width: 456px;
    height: 670px;
    overflow: hidden;
    position: relative;
}
.photo-player-lists{
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 456px;
    height: 670px;
}
.photo-player-lists li{
    list-style-type: none;
    width: 456px;
    height: 670px;
}
.photo-player-lists li img{
    width: 456px;
    height: 670px;
}
.photo-player-button{
    position: absolute;
    margin: 0;
    padding: 0;
    bottom: 30px;
    left: 198px;
    list-style-type: none;
}
.photo-player-button li{
    list-style-type: none;
    width: 10px;
    height: 10px;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 6px;
    float: left;
    cursor: pointer;
}
.white{
    background: #ffffff;
}
.active{
    background: #0000ff;
}
</style>
<title>Title</title>

</head>
<body>
<div id="app">
    <my-player :photos="photos"></my-player>
    <my-player :photos="photos"></my-player>
    <my-player :photos="photos"></my-player>
</div>

<script src="libs/vue.js"></script>
<script>
Vue.component("my-player", {
    template: `<div class="photo-player">
            <ul class="photo-player-lists">
                <li v-for="(value, key) in photos" v-if="key==index"><img :src="value" /></li>
            </ul>

            <ul class="photo-player-button">
                <li v-for="(value, key) in photos" :class="['white', {'active': key == index}]" @click="index = key"></li>
            </ul>
        </div>`,
    props: ["photos"],
    data: function(){
        return {
            index: 0
        }
    },
    methods: {
        change(){
            let that = this;
            let len = this.photos.length;
            setInterval(function(){
                that.index++;
                if(that.index == len){
                    that.index = 0;
                }

            }, 1000*3);
        }
    },
    mounted(){
        this.change();
    }
});

let app = new Vue({
    el: "#app",
    data: {
        photos: ["images/08.jpg", "images/13.jpg", "images/16.jpg"]
    }
});
</script>
</body>
</html>

二、单文件组件形式:

PhotoPlayer.vue

<template>
    <div class="photo-player">
            <ul class="photo-player-lists">
                <li v-for="(value, key) in photos" v-if="key==index" :key="key"><img :src="value" /></li>
            </ul>

            <ul class="photo-player-button">
                <li v-for="(value, key) in photos" :class="['white', {'active': key == index}]" @click="index = key" :key="key"></li>
            </ul>
    </div>
</template>

<script>
export default {
    data(){
        return {
            index: 0
        }
    },
    props: ["photos"],
    methods: {
        change(){
            let that = this;
            let len = this.photos.length;
            setInterval(function(){
                that.index++;
                if(that.index == len){
                    that.index = 0;
                }

            }, 1000*5);
        }
    },
    mounted(){
        this.change();
    }
}
</script>

<style scoped>
html, body{
    margin: 0;
    padding: 0;
}
.photo-player{
    width: 456px;
    height: 670px;
    overflow: hidden;
    position: relative;
}
.photo-player-lists{
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 456px;
    height: 670px;
}
.photo-player-lists li{
    list-style-type: none;
    width: 456px;
    height: 670px;
}
.photo-player-lists li img{
    width: 456px;
    height: 670px;
}
.photo-player-button{
    position: absolute;
    margin: 0;
    padding: 0;
    bottom: 30px;
    left: 198px;
    list-style-type: none;
}
.photo-player-button li{
    list-style-type: none;
    width: 10px;
    height: 10px;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 6px;
    float: left;
    cursor: pointer;
}
.white{
    background: #ffffff;
}
.active{
    background: #0000ff;
}
</style>

使用时:

在某个单文件组件中引用PhotoPlayer.vue

<template>
  <div>
    <PhotoPlayer :photos="photos"></PhotoPlayer>
  </div>
</template>

<script>
import PhotoPlayer from './PhotoPlayer'
export default {
  data() {
    return {
      photos: [require("../assets/08.jpg"), require("../assets/13.jpg"), require("../assets/16.jpg")]
    }
  },
  components: {
    PhotoPlayer
  }
};
</script>

<style scoped="">

</style>

注意:

定义了一个数组,数组里面装有图片的路径,使用for循环渲染页面时,图片路径对但是图片不显示。

解决办法:

数组里面图片的路径要写成如下:

image:require('../assets/login.png')

渲染的时候要写:

<img :src="item.image" />

 


http://www.niftyadmin.cn/n/886783.html

相关文章

vue实例之组件开发:多标签切换组件/tabs切换组件

一、自定义方式&#xff1a; 定义组件Tabs.vue <template><div class"tabs"><button v-for"(tab, key) in tabs" :key"key" click"indexkey" :class"{active: keyindex}">{{tab}}</button><ul…

go 切片及其基本操作

1.1切片 Go中常用的数据结构是切片&#xff0c;即动态数组&#xff0c;其长度并不固定&#xff0c;我们可以向切片中追加元素&#xff0c;它会在容量不足时自动扩容。 在 Go 语言中&#xff0c;切片类型的声明方式与数组有一些相似&#xff0c;不过由于切片的长度是动态的&am…

vue插件开发的两种方法:以通知插件toastr为例

方法一&#xff1a; 1、写插件&#xff1a; 在 src 文件夹下面建 lib 文件夹用于存放插件&#xff0c;lib 文件夹下再建toastr文件夹&#xff0c;在toastr文件夹下新建 toastr.js 和 toastr.vue两个文件。整个项目目录如下所示&#xff1a; toastr.vue 的内容如下&#xff1a…

webuploader如何判断是否上传的是空文件?

在beforeFileQueued事件中可以判断&#xff1a; // 当有文件被添加进队列的时候 uploader.on( beforeFileQueued, function( file ) {if(file.size 0){alert("请不要上传空文件");} });

ztree中如何通过点击节点文字就可以选中节点前的选择框,并且父级和子级之间进行选择联动?

配置信息如下&#xff1a; var UsersTreeSetting {check : {enable : true,chkboxType : {"Y" : "ps","N" : "ps"}},view : {dblClickExpand : false,selectedMulti: false},data : {simpleData : {enable : true}},callback : {bef…

vue实例之组件开发:购物车实现

效果图 文件目录 Index.vue&#xff1a; <template><div class"shopping"><Header /><Itemv-for"item in items":key"item.id":item"item"change-number"changeNumber"/><Footer :items"…

vue实例:实现数据的增删改查(纯前端)

<template><div class"container"><div class"input-bar"><table><tbody><td>标题&#xff1a;</td><td><input type"text" v-model"addObj.title"/></td><td>发布…

GO语言的defer 关键字

介绍 Go 语言的 defer 会在当前函数返回前执行传入的函数&#xff0c;它会经常被用于关闭文件描述符、关闭数据库连接以及解锁资源。 作为一个编程语言中的关键字&#xff0c;defer 的实现一定是由编译器和运行时共同完成的&#xff0c;不过在深入源码分析它的实现之前我们还…