首页 > 其他分享 >使用Vue自定义组件完成10×10图片表格的点击效果

使用Vue自定义组件完成10×10图片表格的点击效果

时间:2023-03-18 16:24:53浏览次数:43  
标签:10 Vue 自定义 img tu jpg im 组件 2023218125410855jixiang

1.创建一个web项目,使用<script>引入Vue.js

代码如下:

<script type="text/javascript" src="js/vue.js" ></script>

 

2.创建vue实例

<div id="app">
    
</div>
<script>

 var vm = new Vue({
   el:"#app",//通过el与div元素绑定

  })

</script>

3.将剪切好的图片放入img文件夹下,再通过vue的data的属性获得图片

var vm = new Vue({
                el:"#app",
                data:{
                    imgs:["img/tu/2023218125410855jixiang_1.jpg","img/tu/2023218125410855jixiang_2.jpg","img/tu/2023218125410855jixiang_3.jpg","img/tu/2023218125410855jixiang_4.jpg","img/tu/2023218125410855jixiang_5.jpg","img/tu/2023218125410855jixiang_6.jpg","img/tu/2023218125410855jixiang_7.jpg","img/tu/2023218125410855jixiang_8.jpg","img/tu/2023218125410855jixiang_9.jpg","img/tu/2023218125410855jixiang_10.jpg","img/tu/2023218125410855jixiang_11.jpg","img/tu/2023218125410855jixiang_12.jpg","img/tu/2023218125410855jixiang_13.jpg","img/tu/2023218125410855jixiang_14.jpg","img/tu/2023218125410855jixiang_15.jpg","img/tu/2023218125410855jixiang_16.jpg","img/tu/2023218125410855jixiang_17.jpg","img/tu/2023218125410855jixiang_18.jpg","img/tu/2023218125410855jixiang_19.jpg","img/tu/2023218125410855jixiang_20.jpg","img/tu/2023218125410855jixiang_21.jpg","img/tu/2023218125410855jixiang_22.jpg","img/tu/2023218125410855jixiang_23.jpg","img/tu/2023218125410855jixiang_24.jpg","img/tu/2023218125410855jixiang_25.jpg","img/tu/2023218125410855jixiang_26.jpg","img/tu/2023218125410855jixiang_27.jpg","img/tu/2023218125410855jixiang_28.jpg","img/tu/2023218125410855jixiang_29.jpg","img/tu/2023218125410855jixiang_30.jpg","img/tu/2023218125410855jixiang_31.jpg","img/tu/2023218125410855jixiang_32.jpg","img/tu/2023218125410855jixiang_33.jpg","img/tu/2023218125410855jixiang_34.jpg","img/tu/2023218125410855jixiang_35.jpg","img/tu/2023218125410855jixiang_36.jpg","img/tu/2023218125410855jixiang_37.jpg","img/tu/2023218125410855jixiang_38.jpg","img/tu/2023218125410855jixiang_39.jpg","img/tu/2023218125410855jixiang_40.jpg","img/tu/2023218125410855jixiang_41.jpg","img/tu/2023218125410855jixiang_42.jpg","img/tu/2023218125410855jixiang_43.jpg","img/tu/2023218125410855jixiang_44.jpg","img/tu/2023218125410855jixiang_45.jpg","img/tu/2023218125410855jixiang_46.jpg","img/tu/2023218125410855jixiang_47.jpg","img/tu/2023218125410855jixiang_48.jpg","img/tu/2023218125410855jixiang_49.jpg","img/tu/2023218125410855jixiang_50.jpg","img/tu/2023218125410855jixiang_51.jpg","img/tu/2023218125410855jixiang_52.jpg","img/tu/2023218125410855jixiang_53.jpg","img/tu/2023218125410855jixiang_54.jpg","img/tu/2023218125410855jixiang_55.jpg","img/tu/2023218125410855jixiang_56.jpg","img/tu/2023218125410855jixiang_57.jpg","img/tu/2023218125410855jixiang_58.jpg","img/tu/2023218125410855jixiang_59.jpg","img/tu/2023218125410855jixiang_60.jpg","img/tu/2023218125410855jixiang_61.jpg","img/tu/2023218125410855jixiang_62.jpg","img/tu/2023218125410855jixiang_63.jpg","img/tu/2023218125410855jixiang_64.jpg","img/tu/2023218125410855jixiang_65.jpg","img/tu/2023218125410855jixiang_66.jpg","img/tu/2023218125410855jixiang_67.jpg","img/tu/2023218125410855jixiang_68.jpg","img/tu/2023218125410855jixiang_69.jpg","img/tu/2023218125410855jixiang_70.jpg","img/tu/2023218125410855jixiang_71.jpg","img/tu/2023218125410855jixiang_72.jpg","img/tu/2023218125410855jixiang_73.jpg","img/tu/2023218125410855jixiang_74.jpg","img/tu/2023218125410855jixiang_75.jpg","img/tu/2023218125410855jixiang_76.jpg","img/tu/2023218125410855jixiang_77.jpg","img/tu/2023218125410855jixiang_78.jpg","img/tu/2023218125410855jixiang_79.jpg","img/tu/2023218125410855jixiang_80.jpg","img/tu/2023218125410855jixiang_81.jpg","img/tu/2023218125410855jixiang_82.jpg","img/tu/2023218125410855jixiang_83.jpg","img/tu/2023218125410855jixiang_84.jpg","img/tu/2023218125410855jixiang_85.jpg","img/tu/2023218125410855jixiang_86.jpg","img/tu/2023218125410855jixiang_87.jpg","img/tu/2023218125410855jixiang_88.jpg","img/tu/2023218125410855jixiang_89.jpg","img/tu/2023218125410855jixiang_90.jpg","img/tu/2023218125410855jixiang_91.jpg","img/tu/2023218125410855jixiang_92.jpg","img/tu/2023218125410855jixiang_93.jpg","img/tu/2023218125410855jixiang_94.jpg","img/tu/2023218125410855jixiang_95.jpg","img/tu/2023218125410855jixiang_96.jpg","img/tu/2023218125410855jixiang_97.jpg","img/tu/2023218125410855jixiang_98.jpg","img/tu/2023218125410855jixiang_99.jpg","img/tu/2023218125410855jixiang_100.jpg"
]
                }
            })

4.创建组件

在组件中插入图片,用v-show来控制图片是否显示,定义一个change方法来改变show,用@click来监听这个事件。

用v-bind简写为::src="im" src绑定im

<template id="temp"> //组件的名字
    <div class="item" @click="change">
        <img :src="im" v-show="show" />    
    </div>
</template>

5.组件具体是什么

props接收im数据,im在父组件中定义 数据是数组imgs

Vue.component("test-div",{
    template:"#temp",//这个组件具体是什么
    props:["im"],
    data:function(){
    return{
        show:true,                        
                img:"img/tu/2023218125410855jixiang_1.jpg"
        }
    },
        methods:{
            //方法
            change:function(){
                this.show = !this.show
            }
        }
    });        

6.图片放入盒子内

一张一张的放太过于麻烦,用v-for用来循环插入图片

用v-bind简写为: :im="i"  im绑定i在每次循环imgs接收到的值

<div id="app">
            <div class="content">
                <test-div v-for="i in imgs" :im="i"></test-div>
            </div>
 </div>

7.css部分

<style>
        .content{
            width: 1040px;
                border: 3px solid palevioletred;
                margin: 0 auto;
                display: flex;
                justify-content: space-around;
                flex-wrap: wrap;
        }    
        .item{
            height: 100px;
            width: 100px;
            background-color: #DE407E;
            margin: 2px 0px;
        }
        </style>

8.效果展示

 附件链接:

链接: https://pan.baidu.com/s/1nnzadYt7bw9_TLNfQzLROQ?pwd=ufj7 提取码: ufj7 复制这段内容后打开百度网盘手机App,操作更方便哦

 

 

 

标签:10,Vue,自定义,img,tu,jpg,im,组件,2023218125410855jixiang
From: https://www.cnblogs.com/cccoop/p/17230994.html

相关文章

  • 100道python基础题——(6)
    编写一个程序,根据给定的公式计算并打印值:。以下是C和H的固定值:C是50。H是30。D是一个变量,它的值应该以逗号分隔的序列输入到程序中。例子假设程序的输入序列是逗号分隔的......
  • Vue.js 插件
    视频插件功能:用于增强Vue本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。定义插件:对象.install=function......
  • Vue.js mixin混入
    视频mixin(混入)功能:可以把多个组件共用的配置提取成一个混入对象使用方式:第一步定义混合:{data(){....},methods:{....}....}第二步使用混入:......
  • 100道python基础题——(5)
    Python简明教程---20,Python类中的属性与方法-码农充电站-博客园(cnblogs.com)问题:定义一个至少有两个方法的类:    getString:从控制台输入获取字符串......
  • Vue.js props配置(微信收款确认流程)
    视频直接修改外来数据props会产生警告props配置项1.功能:让组件接收外部传过来的数据2.传递数据:```<Demoname="xxx"/>```3.接收数据:1.第一种方式(只接收......
  • day10(javaSEPro)
    day10static关键字static叫静态,只能修饰成员变量、成员方法,不能修饰类,也不能修饰局部的东西static修饰成员变量static修饰成员变量的特点:被类的所有对象共享可以通......
  • Vue.js ref属性(类比id)
    视频1.被用来给元素或子组件注册引用信息(id的替代者)2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)3.使用方式:1.打标识:```<h1ref=......
  • Tomcat监控原理cmdline-jmxclient-0.10.3.jar源码解析
    【摘要】cmdline-jmxclient-0.10.3.jar是一个开源jar包,在zabbix3.0中常用于测试的一个工具,可以用来测试jmx是否配置正确。在监控方面后还可以对Tomcat各种属性进行监控。......
  • Vue.js 修改默认配置(main.js改变,vue.config.js改变)
    视频使用vueinspect>output.js可以查看到Vue脚手架的默认配置。使用vue.config.js可以对脚手架进行个性化定制,详情见:https://cli.vuejs.org/zhmain.js改变对应的......
  • Vue-依赖注入(provide-inject)【祖孙传值】
    转自:https://blog.csdn.net/qq_37831545/article/details/129030309非响应式写法:   可响应的写法:  ......