uniapp监听当前元素是否出现在页面

uniapp开发小程序或app,有一些dom节点的抓取兼容问题,比如你并不能使用document.getByElementId等方法获取到页面上的dom节点,需要通过uni.createSelectorQuery()方法来获取到,并且要通过in(this)来声明它获取的位置,如下可获取到当前页面的节点

const query = uni.createSelectorQuery().in(this);

这是我实现的代码,可以参考,增加定时器是为了稳定性,uni获取节点时,有一定的延时性

let arr = []
				const {
					windowHeight
				} = uni.getSystemInfoSync()
				setTimeout(() => {
					if (this.isValidArray(this.activeContents) && this.activeContents.length > 0) {
						for (let i = 0; i < this.activeContents.length; i++) {
							if (this.activeContents[i].type == 2) {
								arr.push(this.activeContents[i])
							}
						}
						for (let j = 0; j < arr.length; j++) {
							//根据最初值,给一个完成状态
							if (arr[j].executed) {
								arr[j].judge = true
							} else {
								arr[j].judeg = false
							}
							let task = '.text' + arr[j].eId
							const query = uni.createSelectorQuery().in(this);
							// 选择节点
							query.select(task).boundingClientRect(data => {
								const {
									top,
									height
								} = data
								if ((top < windowHeight) && (top + height > 0)) {
									//如果已经完成了,不操作
									if (arr[j].judge) {

									} else {
										let params = {
											classId: 0,
											contentKey: arr[j].key,
											dsId: this.id,
											taskid: this.gId,
											isTeach: false
										}
										if (this.account && this.session) { //登录状态下
											this.$api.exectionWord(params).then(res => {
												//因为不请求接口,所以完成后本地赋值为true
												arr[j].judge = true
												this.noLoadingGet()
											})
										} else { //未登录预览
											arr[j].judge = true
										}
									}
									// console.log('元素在可视区域出现')
								} else {
									// console.log('元素在不可视区域出现')
								}
							}).exec()
						}
					}
				}, 500)

这就是一个判断当前页面是否出现该dom节点,来发起请求的操作了,因为我的页面有多个数据,和特殊的请求判断,所以在内部进行了一些逻辑处理,你们可以根据自己的需求进行代码书写

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想实现在每个分类选项卡下加入图文并排内容的功能,可以使用uniapp提供的grid组件和swiper组件结合起来,来实现这个需求。 首先,你需要在页面中引入tabbar组件、swiper组件和grid组件: ```html <template> <view> <tabbar :current="current" @change="tabChange"> <tabbar-item title="选项卡1"></tabbar-item> <tabbar-item title="选项卡2"></tabbar-item> <tabbar-item title="选项卡3"></tabbar-item> </tabbar> <swiper :current="current" @change="swiperChange"> <swiper-item> <grid :list="list1" /> </swiper-item> <swiper-item> <grid :list="list2" /> </swiper-item> <swiper-item> <grid :list="list3" /> </swiper-item> </swiper> </view> </template> ``` 在上面的代码中,我们创建了一个tabbar组件和一个swiper组件,分别用来实现选项卡和对应内容的切换。其中,swiper组件的每个swiper-item元素都包含一个grid组件,用来显示对应选项卡的图文并排内容。 接下来,你需要在页面的<script>标签中添加对应的逻辑代码,用来实现选项卡和内容的切换: ```javascript <script> export default { data() { return { current: 0, list1: [ { icon: "/static/img1.png", text: "图文1" }, { icon: "/static/img2.png", text: "图文2" }, { icon: "/static/img3.png", text: "图文3" } ], list2: [ { icon: "/static/img4.png", text: "图文4" }, { icon: "/static/img5.png", text: "图文5" }, { icon: "/static/img6.png", text: "图文6" } ], list3: [ { icon: "/static/img7.png", text: "图文7" }, { icon: "/static/img8.png", text: "图文8" }, { icon: "/static/img9.png", text: "图文9" } ] } }, methods: { tabChange(e) { this.current = e.index }, swiperChange(e) { this.current = e.current } } } </script> ``` 在上面的代码中,我们定义了一个current变量,用来记录当前选中的选项卡的索引值。然后,我们在tabChange方法和swiperChange方法中分别监听tabbar和swiper的切换事件,通过修改current变量的值来实现选项卡和内容的切换。同时,我们还定义了三个列表list1、list2和list3,分别对应于每个选项卡下的图文并排内容。 最后,我们需要在grid组件中引用这些列表,用来显示图文并排内容: ```html <template> <view> <tabbar :current="current" @change="tabChange"> <tabbar-item title="选项卡1"></tabbar-item> <tabbar-item title="选项卡2"></tabbar-item> <tabbar-item title="选项卡3"></tabbar-item> </tabbar> <swiper :current="current" @change="swiperChange"> <swiper-item> <grid :list="list1"> <block v-for="(item, index) in list1"> <view class="grid-item"> <image :src="item.icon" class="grid-icon"></image> <text class="grid-text">{{ item.text }}</text> </view> </block> </grid> </swiper-item> <swiper-item> <grid :list="list2"> <block v-for="(item, index) in list2"> <view class="grid-item"> <image :src="item.icon" class="grid-icon"></image> <text class="grid-text">{{ item.text }}</text> </view> </block> </grid> </swiper-item> <swiper-item> <grid :list="list3"> <block v-for="(item, index) in list3"> <view class="grid-item"> <image :src="item.icon" class="grid-icon"></image> <text class="grid-text">{{ item.text }}</text> </view> </block> </grid> </swiper-item> </swiper> </view> </template> ``` 在上面的代码中,我们使用了`<block>`标签来遍历每个列表中的元素,然后在每个元素中使用`<image>`标签和`<text>`标签来显示图文并排内容。同时,我们还为`<image>`标签和`<text>`标签添加了一些样式,用来设置图标和文字的大小和位置,具体样式可以根据实际需求进行调整。 这样,你就可以通过uniapp提供的tabbar组件、swiper组件和grid组件来实现点击选项卡切换加上在每个分类选项卡下加入图文并排内容的功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值