div单选多选-动态改变样式

单选:

需求:
两个div,一次只能点击一个,将点击的div样式进行改变-加背景图并改背景色
效果图:
在这里插入图片描述
HTML:
条件表达式动态绑定样式::class="{'active': isChange==index}

<div class="test" :class="{'active': isChange==index}" v-for="(item,index) in upArr"  :key="index" @tap="tapInfo(index,item.id)">
			<div class="upv1" >{{item.goods_name}}
				<div class="upv1_center"></div>
				<div class="upv1_money"></div>
				<div class="upv1_num">{{item.shop_price}}</div>
			</div>
			<div style="height: 20px"></div>
		</div>
data() {
			return {
				upArr:[
				{
					title:'升级V1',
					coast:10000
				},
				{
					title:'升级V2',
					coast:20000
				}
					],
				isChange:'-1',
				selected:false,
				good_id:''
			}
		},
tapInfo(index,good_id) {
				var that = this;
				console.log(index,good_id)
				that.goods_id = good_id;
				if(index!=this.isChange){
					that.selected = true;
					this.isChange = index;
				}else{
					this.isChange = -1;    //不注释则可以点击取消,注释之后点击就不再取消
					that.selected = false;
				}
			},
.active{
		.upv1{
			background: #FFF6F1;
			background-image:url("~@/static/img/selected.png");
			background-repeat:no-repeat;
			background-position:296px 38px;
			background-size: 33px;
		}
}
.test{
			.upv1{
				//margin: 20px 0px 0 33px;
				margin: 0 auto;
				width: 329px;
				height: 70px;
				//background: #FFF6F1;
				border-radius: 6px;
				border: 2px solid #FE9E67;


				//text-align: center;
				display: flex;
				justify-content: center; //flex布局下居中配置
				line-height: 70px;

				font-size: 16px;
				font-weight: bold;
				letter-spacing: 1px;

				.upv1_center{
					width: 100px;
				}
				.upv1_money{
					margin-top: 5px;
					font-size: 15px;
				}
				.upv1_num{
					font-size: 30px;
				}

			}
		}

多选:
效果图:
在这里插入图片描述
HTML:
三元运算符动态绑定样式::class="[rSelect.indexOf(item)!=-1?'fordata2':'fordata1']"

<div  v-for="(item,index) in list" :key="index" @click="onStorage(item,index)" :class="[rSelect.indexOf(item)!=-1?'fordata2':'fordata1']">
                <div class="data_wrapper">
                    <!--<div class="data_id">SN:{{item.id}}</div>-->
                    <div class="data_sn">SN:{{item.sn}}</div>
                    <div class="data_check">
                        <!--<image src="/static/img/uncheck.png" class="img_one2" mode="scaleToFill"></image>-->
                    </div>
                </div>
                <div style="width: 100%;background-color: #efefef;height: 1px"></div>
            </div>

JS:

data(){
            return{
                rSelect:[],
            }
        },
methods:{
            onStorage(value,e){
                console.log(this.rSelect.indexOf(value));
                if (this.rSelect.indexOf(value) !== -1) {
                    this.rSelect.splice(this.rSelect.indexOf(value), 1); //取消
                } else {
                    this.rSelect.push(value);//选中添加到数组里
                }
                console.log(this.rSelect);
            },
          }
.fordata1{
        .data_wrapper{
            background: #FAFAFA;
            display: flex;
            height: 50px;
            width: 100%;
            line-height: 50px;
            /*.data_id{
                color: #999999;
                margin-left: 15px;
                width: 20%;
            }*/
            .data_sn{
                color: #666;
                width: 85%;
                margin-left: 15px;
            }
            .data_check{
                background-image:url("~@/static/img/uncheck.png");
                background-repeat:no-repeat;
                background-size: 33px;
                background-position-y: 8px;

                width: 15%;
                height: 50px;
                line-height: 50px;
                /*.img_one2{
                    margin-top: 8px;
                    width: 30px;
                    height: 30px;
                }*/
            }
        }
    }

    .fordata2{
        .data_wrapper{
            background: #FAFAFA;
            display: flex;
            height: 50px;
            width: 100%;
            line-height: 50px;
            /*.data_id{
                color: #999999;
                margin-left: 15px;
                width: 20%;
            }*/
            .data_sn{
                color: #666;
                width: 85%;
                margin-left: 15px;
            }
            .data_check{
                background-image:url("~@/static/img/check.png");
                background-repeat:no-repeat;
                background-size: 33px;
                background-position-y: 8px;

                width: 15%;
                height: 50px;
                line-height: 50px;
                /*.img_one2{
                    margin-top: 8px;
                    width: 30px;
                    height: 30px;
                }*/
            }
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uniapp中实现单选多选和全选的方式有很多种,下面是其中一种可行的方法: 1. 单选 首先,在数据源中添加一个选中属性(例如isSelected),表示该项是否被选中。然后,在模板中使用v-model绑定isSelected属性即可实现单选。 ```html <template> <div> <div v-for="(item, index) in dataList" :key="item.id"> <input type="radio" :value="index" v-model="selectedIndex"> {{ item.name }} </div> </div> </template> <script> export default { data() { return { selectedIndex: -1, dataList: [ { id: 1, name: '选项1', isSelected: false }, { id: 2, name: '选项2', isSelected: false }, { id: 3, name: '选项3', isSelected: false } ] } } } </script> ``` 2. 多选 同样,在数据源中添加一个选中属性(例如isSelected),表示该项是否被选中。然后,在模板中使用v-model绑定一个数组,选中的项会被添加到该数组中。 ```html <template> <div> <div v-for="(item, index) in dataList" :key="item.id"> <input type="checkbox" :value="index" v-model="selectedList"> {{ item.name }} </div> </div> </template> <script> export default { data() { return { selectedList: [], dataList: [ { id: 1, name: '选项1', isSelected: false }, { id: 2, name: '选项2', isSelected: false }, { id: 3, name: '选项3', isSelected: false } ] } } } </script> ``` 3. 全选 首先,在数据源中添加一个选中属性(例如isSelected),表示该项是否被选中。然后,添加一个全选按钮,当点击全选按钮时,将所有项的isSelected属性设置为true或false即可实现全选。 ```html <template> <div> <button @click="selectAll">全选</button> <div v-for="(item, index) in dataList" :key="item.id"> <input type="checkbox" :value="index" v-model="selectedList"> {{ item.name }} </div> </div> </template> <script> export default { data() { return { selectedList: [], dataList: [ { id: 1, name: '选项1', isSelected: false }, { id: 2, name: '选项2', isSelected: false }, { id: 3, name: '选项3', isSelected: false } ] } }, methods: { selectAll() { this.dataList.forEach(item => { item.isSelected = true }) this.selectedList = this.dataList.map((item, index) => index) } } } </script> ``` 以上是实现单选多选和全选的简单示例,具体的实现方式可以根据实际需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值