v-for循环后,点击事件控制子组件显示与隐藏

//   先说思路  整体代码在下面可以直接使用


1.给按钮添加点击事件,用来控制子组件显示与隐藏
    @click="ArticleDetail(index)"
2.声明一个变量
    activeIndex: -1, // 初始化点击的索引值,
3.子组件使用v-show进行响应式
    v-show="activeIndex===index"
4.写方法
    	ArticleDetail(index) {
				if (this.activeIndex !== index) {
					this.activeIndex = index;
				} else {
					this.activeIndex = -1;
				}
			},
5.子组件可以正常展示与掩藏,不过我们希望点击其他地方是,自组件必须进行关闭
created() {
			// w3c ocument.addEventListener用法
			// 在文档中添加点击事件。当用户在文档任何地方点击时,在 id="demo" 的 <p> 元素上输出                         
            // "Hello World":
			// document.addEventListener("click", function(){
			//     document.getElementById("demo").innerHTML = "Hello World";
			// });

//当用户进行点击任意地方时,进行调用控制子组件显示与掩藏的方法
			document.addEventListener("click", (e) => {
				if (e.changedTouches === undefined) {
					this.ArticleDetail()
				}
			});
},

2.我把全部代码粘一下,可以直接使用的

<template>
	<view class="wrapper">
		<div class="cards" id="v-content" v-bind:style="{minHeight: Height+'px'}">
			<div class="card" v-for="(item,index) in 2" :key="index">
				<div class="card-img">
				</div>
				<div class="card-right">
					<view class="card-title">杭州高端餐厅配送</view>
					<view class="card-tit">设计师以日式空间的设计原则为基础,提出了整体式的设计概念,以最少的材料色调打造出一个简洁..[全文]</view>
					<view class="right-img">
						<!-- <img src="@/assets/image/touxiang.png" alt="" /> -->
						<view class="" style="width: 85px;height: 70px;background: #eee;"></view>
					</view>
					<div class="vard-address">杭州市 · 杭州市祥茂路16号威格科技园16号</div>
					<div class="d-between">
						<view class="" style="margin-top: 5px;">8分钟前</view>
						<div class="d-flex">
							<!-- <u-icon name="more-dot-fill" colr="#576B95" size="35" @click="ArticleDetail(index)">
							</u-icon> -->
							<div @click="ArticleDetail(index)">点击我!!!</div>
						</div>
						<view class="pop d-around" v-show="activeIndex===index">
							<view class="" @click="like()">点赞</view>
							<view class="" @click="comment()">评论</view>

							<view class="" @click="deleted()">删除</view>
						</view>
					</div>
				</div>
			</div>
		</div>
		<Footer>111</Footer>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: 0,
				activeIndex: -1, // 初始化点击的索引值,
				Height: 0
			};
		},
		mounted() {
			//动态设置内容高度 让footer始终居底   header+footer的高度是100
			this.Height = document.documentElement.clientHeight - 100;
			//监听浏览器窗口变化 
			window.onresize = () => {
				this.Height = document.documentElement.clientHeight - 100
			}
		},
		created() {
			// w3c ocument.addEventListener用法
			// 在文档中添加点击事件。当用户在文档任何地方点击时,在 id="demo" 的 <p> 元素上输出 "Hello World":
			// document.addEventListener("click", function(){
			//     document.getElementById("demo").innerHTML = "Hello World";
			// });
			document.addEventListener("click", (e) => {
				if (e.changedTouches === undefined) {
					this.ArticleDetail()
				}
			});
		},
		methods: {
			ArticleDetail(index) {
				if (this.activeIndex !== index) {
					this.activeIndex = index;
				} else {
					this.activeIndex = -1;
				}
			},
			like() {
				this.ArticleDetail()
				console.log(1);
			},
			comment() {
				this.ArticleDetail()
				console.log(2);
			},
			deleted() {
				this.ArticleDetail()
				console.log(3);
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .tab .tab-bar[data-v-8a659874]::after {
		content: '';
		width: 100%;
		height: 1px;
		position: absolute;
		bottom: 0;
		left: 0;
		background: #fff;
	}


	.cards {
		padding: 0 10px 50px 10px;

		.card {
			display: flex;
			font-size: 12px;
			margin-top: 10px;

			.card-img {
				img {
					width: 40px;
					height: 40px;
				}

				.card-btn {
					color: #fff;
					padding: 2px 4px;
					margin-top: 4px;
					line-height: 14px;
					border-radius: 4px;
					text-align: center;
					background: #58A875;
				}
			}

			.card-right {
				position: relative;
				font-size: 12px;
				margin-left: 8px;

				.card-title {
					font-size: 14px;
					color: #0042A2;
				}

				.card-tit {
					margin-top: 5px;
					width: 100%;
				}

				.right-img {
					img {
						width: 85px;
						height: 70px;
						margin-top: 8px;
					}
				}

				.vard-address {
					color: #0042A2;
					margin-bottom: 3px;
				}

				.pop {
					width: 108px;
					height: 32px;
					color: #fff;
					right: 24px;
					top: 155px;
					z-index: 2;
					border-radius: 3px;
					position: absolute;
					background: #000;
				}

				.comment {
					width: 100%;
					padding: 9px;
					margin-top: 14px;
					font-size: 16px;
					background: #F3F3F5;

					.comment-top {
						width: 100%;
						display: flex;
						flex-wrap: wrap;
						padding-bottom: 4px;
						border-bottom: 1px solid #CECECE;


					}

					.comment-bom {
						.paragraph {
							display: flex;
							margin-bottom: 3px;
						}
					}

					.name {
						color: #576B95;
						white-space: nowrap;
					}

					.mar {
						margin: 0 2px;
						color: #CECECE;
						white-space: nowrap;
					}
				}
			}
		}
	}
</style>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值