• 基于uni-app框架的小程序——点击跟随事件


    scroll-view(组件)

    可滚动视图区域,用于区域滚动。(需要注意在webview渲染的页面中,区域的性能不及页面滚动)。 

     

     在这个案例,我们需要用到的属性有scroll-y、scroll-into-view、scroll-with-animation、@scroll这几个属性;类型、默认值、说明等截图做了解释。当然,当我们遇到某些功能时,也可自己查看文档。

    使用竖向滚动时,需要给  一个固定高度,那么我们怎么设置 height?

     

    大概的意思就是通过uni.getSystemInfo(OBJECT)中有一个成功的回调函数,且这个success回调函数中的参数中有一个windowHeight,是我们需要的——可使用窗口高度。

    1. onLoad(){
    2. // 动态获取中间需要滚动的高度
    3. uni.getSystemInfo({
    4. success:(res)=>{
    5. this.scroolH = res.windowHeight
    6. }
    7. })
    8. }

     使用横向滚动时,需要给添加white-space: nowrap;样式。

    1. <view class="span-5">
    2. <scroll-view scroll-y="true" :style="'height:'+scrollH+'px'" :scroll-into-view="leftscrollinto">
    3. <view v-for="(i,index) in cate" :key="index" class="left-height j-center d-flex a-center" :id="'tab'+index"
    4. :class="currentIndex == index ? 'action':''"
    5. @click='changeIndex(index)'>{{i.name}}
    6. view>
    7. scroll-view>
    8. view>

    我们可以看到,我们给标签设置的是纵向高度,并且给scroll-into-view这个属性动态绑定了一个字段(leftscrollinto)【在data中事先定义过了】,scroll-into-view这个属性将绑定的一个字段(非数字开头的id),也就是我们在data中定义的leftscrollinto

    所以我们在标签的子元素)动态绑定了一个非数字的id【:id=" 'tab'+index"】。标签这个父元素为了跟随这个子元素,scroll-into-view这个属性动态绑定的字段(leftscrollinto)的值是和这个子元素的id是同步的。所以在这个子元素上绑定了一个方法(changeIndex),这个方法会把当前点击的tab的index传给我们,然后我们通过【this.leftscrollinto = 'tab'+ index】也就同步了的Id。

    1. data() {
    2. return {
    3. currentIndex:0,
    4. leftscrollinto:'',
    5. rightscrollinto:'',
    6. cate: [],
    7. scrollH:'',
    8. rightTop:[]
    9. }
    10. },
    1. methods:{
    2. changeIndex(index){
    3. this.currentIndex = index
    4. this.leftscrollinto = 'tab'+ index
    5. this.rightscrollinto = 'rightscrollinto'+index
    6. }
    7. }

    当然,我们点击某一个索引时,是有样式的,这就用到了动态绑定样式的,我们可以用三元表达式,当currentIndex == index时,即当我们点击的tab的Index就是当前索引时,就给它赋予一个类名(带有样式)。

     

    emm……这个效果图有点丑,但不影响我们观看效果。

    当我们点击左边的tab时,右边也会对应跟随,这个不难,我们来看一下代码:

    结构:

    1. <view class="d-flex">
    2. <view class="span-5">
    3. <scroll-view scroll-y="true" :style="'height:'+scrollH+'px'" :scroll-into-view="leftscrollinto">
    4. <view v-for="(i,index) in cate" :key="index" class="left-height j-center d-flex a-center" :id="'tab'+index"
    5. :class="currentIndex == index ? 'action':''"
    6. @click='changeIndex(index)'>{{i.name}}
    7. view>
    8. scroll-view>
    9. view>
    10. <view class="flex-1">
    11. <scroll-view @scroll="scrollright" scroll-y="true" :style="'height:'+scrollH+'px'" :scroll-into-view="rightscrollinto" scroll-with-animation>
    12. <view v-for="(j,index) in cate" :key="index" class="right-item d-flex flex-wrap" :id="'rightscroll'+index">
    13. <view v-for="(item,i) in j.app_category_items" :key="i"
    14. class="span24-8 text-center"
    15. style="height: 200rpx;"
    16. x>
    17. <image :src="item.cover" class="image">image>
    18. <view class="">
    19. {{item.name}}
    20. view>
    21. view>
    22. view>
    23. scroll-view>
    24. view>
    25. view>

    1. methods:{
    2. changeIndex(index){
    3. this.currentIndex = index
    4. this.leftscrollinto = 'tab'+ index
    5. this.rightscrollinto = 'rightscroll'+index
    6. }
    7. }

    当我们改变index时,左右两个盒子的scroll-into-view的值会有相应的改变,即跟随点击的子元素id。

    那如果当我们划动右边的这个盒子,想要左边的tab进行相应的跟随呢?我觉得这是一个难点!!

    SelectorQuery(API)

    查询节点信息的对象。

    将选择器的选取范围更改为自定义组件 component 内,返回一个 SelectorQuery 对象实例。(初始时,选择器仅选取页面范围的节点,不会选取任何自定义组件中的节点)。

    selectorQuery.selectAll(selector)

    在当前页面下选择匹配选择器 selector 的所有节点,返回一个 NodesRef 对象实例,可以用于获取节点信息。

    1. // 封装一个获取节点信息的函数
    2. init(){
    3. const query = uni.createSelectorQuery().in(this)
    4. query.selectAll('.right-item').boundingClientRect(data=>{
    5. console.log(data)
    6. if(data){
    7. data.map((item,index)=>{
    8. var top = index > 0 ? this.rightTop[index-1]:0
    9. top += item.height
    10. this.rightTop.push(top)
    11. })
    12. }
    13. }).exec()
    14. }

    我们看一下我们获取到的节点信息,获取到了一个数组,里面分别有6个对象(分别对应的是左边tab的6个选项),且每一个选项对应的右边盒子的高度都是700。

    我们对这个data进行遍历,定义一个变量(top),对这个data中的每一个对象的height进行一个累加,同时把top往rightTop这个数组中添加数据,那么数组中的数据分别是700、1400、2100……

    这也对应了左边tab点击时,右边应该对应的高度,反之,我们通过划动左边盒子时获取的scrollTop来判断我们左边的tab应该对应哪一个。

    举个栗子,比如我们左边划动了750的距离,第一个对象的高度是700,那么此时我们的左边的tab是不是应该停留在第二个呢?

    那么我们怎么去获取scrollTop?怎么去通过划动的距离来设置左边应该跳转的index?

    1. <scroll-view @scroll="scrollright" scroll-y="true" :style="'height:'+scrollH+'px'" :scroll-into-view="rightscrollinto" scroll-with-animation>
    2. <view v-for="(j,index) in cate" :key="index" class="right-item d-flex flex-wrap" :id="'rightscroll'+index">
    3. <view v-for="(item,i) in j.app_category_items" :key="i"
    4. class="span24-8 text-center"
    5. style="height: 200rpx;"
    6. x>
    7. <image :src="item.cover" class="image">image>
    8. <view class="">
    9. {{item.name}}
    10. view>
    11. view>
    12. view>
    13. scroll-view>

     通过@scroll="scrollright",调用了scrollright这个方法。@scroll这个事件的返回值是

    我们可以拿到scrollTop。

    1. methods:{
    2. scrollright(e){
    3. var scrollTop = e.target.scrollTop
    4. //返回满足条件的第一个元素的位置索引号
    5. var index = this.rightTop.findIndex((v)=>v>scrollTop)
    6. if(index !=-1){
    7. this.changeIndex(index)
    8. }
    9. }
    10. }

     上面我们说了rightTop是一个数组。rightTop[700,1400,2100……4200]。在rightTop这个数组中,划动的距离【scrollTop】,满足大于rightTop数组中的第一个元素(每一个tab对应的scrollTop)的索引值。这个也是我们左边tab需要跳转的index。

  • 相关阅读:
    javaScript语法
    来看看你是不是真的了解 RSA 加密算法,查漏补缺!
    Java自定义注解解析
    竞赛选题 行人重识别(person reid) - 机器视觉 深度学习 opencv python
    由浅入深,走进深度学习(补充篇:神经网络基础)
    毕设 美容美发论文
    Docker从入门到上天系列第二篇:传统虚拟机和容器的对比以及Docker的作用以及所解决的问题
    武汉大学数据科学导论 WHU-data-science-introduction-996station GitHub鉴赏官
    大厂真题:【位运算】米哈游2023秋招-相加异或
    Debian10Standard无网络安装后,设置静态IP,安装openssh-server 221024记录
  • 原文地址:https://blog.csdn.net/m0_72383454/article/details/127809005