• 微信小程序的常用事件的用法


    微信小程序中,事件绑定是非常常见的操作。以下是一些常用事件的具体用法和示例:

    1. bindtap 或 catchtap

    点击事件,当用户点击某个元素时触发。

    html
    
    <view bindtap="handleTap">点击我<iew>
    
    javascript
    // JS 文件
    Page({
      handleTap() {
        wx.showToast({
          title: '你点击了这个元素',
          icon: 'none'
        });
      }
    });
    

    2. bindinput 或 catchinput
    输入事件,当用户在输入框中输入内容时触发。

    html

    javascript
    // JS 文件
    Page({
      handleInput(event) {
        console.log('输入的内容:', event.detail.value);
      }
    });
    
    1. bindchange 或 catchchange
      选择改变事件,当用户选择不同的选项或切换开关状态时触发。
    html
    
    <picker mode="selector" range="{{array}}" bindchange="handleChange">
      <view class="picker">
        当前选择:{{index}}
      <iew>
    picker>
    
    javascript
    // JS 文件
    Page({
      data: {
        array: ['选项1', '选项2', '选项3'],
        index: 0
      },
      handleChange(event) {
        this.setData({
          index: event.detail.value
        });
      }
    });
    
    1. bindsubmit 或 catchsubmit
      表单提交事件,当用户提交表单时触发。
    html
    
    <form bindsubmit="handleSubmit">
      <input name="input" placeholder="请输入内容"/>
      <button formType="submit">提交<tton>
    form>
    
    javascript
    // JS 文件
    Page({
      handleSubmit(event) {
        console.log('表单数据:', event.detail.value);
      }
    });
    
    1. bindscroll 或 catchscroll
      滚动事件,当页面或组件滚动时触发。
    html
    
    <scroll-view bindscroll="handleScroll" scroll-y style="height: 300px;">
      <view wx:for="{{array}}" wx:key="*this">{{item}}<iew>
    scroll-view>
    
    javascript
    // JS 文件
    Page({
      data: {
        array: Array.from({ length: 50 }, (_, i) => `项目 ${i}`)
      },
      handleScroll(event) {
        console.log('滚动位置:', event.detail.scrollTop);
      }
    });
    
    1. bindlongpress 或 catchlongpress
      长按事件,当用户长时间按住某个元素时触发。
    html
    
    <view bindlongpress="handleLongPress">长按我<iew>
    
    javascript
    // JS 文件
    Page({
      handleLongPress() {
        wx.showToast({
          title: '长按事件触发',
          icon: 'none'
        });
      }
    });
    
    1. bindanimationend 或 catchanimationend
      动画结束事件,当动画播放完毕时触发。
    html
    
    <view animation="{{animation}}" bindanimationend="handleAnimationEnd">动画元素<iew>
    
    javascript
    // JS 文件
    Page({
      onLoad() {
        const animation = wx.createAnimation({
          duration: 1000,
          timingFunction: 'ease',
        });
        this.animation = animation;
        animation.scale(2, 2).step();
        this.setData({
          animation: animation.export()
        });
      },
      handleAnimationEnd() {
        wx.showToast({
          title: '动画结束',
          icon: 'none'
        });
      }
    });
    
    1. bindload 或 catchload
      加载完成事件,当某个资源(如图片)加载完成时触发。
    html
    
    <image src="/path/to/image" bindload="handleImageLoad"/>
    
    javascript
    // JS 文件
    Page({
      handleImageLoad() {
        wx.showToast({
          title: '图片加载完成',
          icon: 'none'
        });
      }
    });
    

    总结

    1. bindtap 或 catchtap:点击事件,当用户点击某个元素时触发。
    2. bindinput 或 catchinput:输入事件,当用户在输入框中输入内容时触发。
    3. bindchange 或 catchchange:选择改变事件,当用户选择不同的选项或者切换开关状态时触发。
    4. bindsubmit 或 catchsubmit:表单提交事件,当用户提交表单时触发。
    5. bindscroll 或 catchscroll:滚动事件,当页面滚动时触发。
    6. bindlongpress 或 catchlongpress:长按事件,当用户长时间按住某个元素时触发。
    7. bindanimationend 或 catchanimationend:动画结束事件,当动画播放完毕时触发。
    8. bindload 或 catchload:加载完成事件,当某个资源加载完成时触发,例如图片加载完成。

    以上仅是常见的一些事件,在实际开发中还有更多类型的事件可以使用。你可以根据具体的业务需求选择适合的事件,并在对应的元素上绑定相应的事件处理函数来实现交互逻辑。

  • 相关阅读:
    R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
    AUTOSAR AP硬核知识点梳理(1)
    3D人物建模用哪个软件入门比较快?
    C语言:指针(函数回调)
    彻底讲清电气转换器(I-P电流型、E-P电压型)与电气比例阀的区别
    Node.js 零基础入门 Node.js 零基础入门第二天 2.4 模块的加载机制
    OAuth2.0
    AJAX学习日记——Day 3
    Python 中的内存泄漏问题
    暴力递归转动态规划(六)
  • 原文地址:https://blog.csdn.net/ike_kenny/article/details/139856382