欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

微信小程序设置悬浮类实现点击状态效果

最编程 2024-03-19 18:48:20
...
image.png

image.png

微信小程序中,可以用 hover-class 属性来指定元素的点击态效果。但是在在使用中要注意,大部分组件是不支持该属性的。

  • 目前支持 hover-class 属性的组件有三个:view、button、navigator。
  • 不支持 hover-class 属性的组件,同时也不支持 hover-stop-propagation、hover-start-time、hover-stay-time 这三个属性。
  • 当 hover-class 的值为 none 时,组件上不会有任何点击态效果。
属性名 类型 默认值 必填 说明
hover-class string none 指定按下去的样式类。当 hover-class="none" 时,没有点击态效果
hover-stop-propagation boolean false 指定是否阻止本节点的祖先节点出现点击态
hover-start-time number 50 按住后多久出现点击态,单位毫秒
hover-stay-time number 400 手指松开后点击态保留时间,单位毫秒

注意事项

  • hover-class样式显示的原理是 点击时把样式加到class的样式中,冲突时,谁在后面就显示谁!
  • 当组件中没有任何指定的类时,直接使用 hover-class 就会起到相应的作用,但是当组件中已经指定了其他可能与 hover-class 冲突的类时,hover-class 无效
  • 将 hover-class 指定的类放在对应 wss 文件的最末尾,这样就不会被其他类所覆盖
  • 通常,当一个 view 组件中包含 image 等不支持 hover-class 的组件,但又需要在该组件上使用 hover-stop-propagation 属性的作用时,需要将不支持 hover-class 的组件用view、button 或 navigator 包裹起来

** \color{red}{注意:按钮1、按钮2的包裹}\ **

<view class="normal-style" hover-class="press-style">
  <view class="btn-txt">按钮1</view>
  <view><image src="../../image/1.png" style="width: 50rpx; height: 50rpx;"></image></view>
  按钮2
</view>
.normal-style{
  color: white;
  line-height: 100rpx;
  text-align: center;
  background: blue;
  border-radius: 10rpx;
  width: 200rpx;
  height: 100rpx;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn-txt{
  color: white;
}

.press-style{
  box-shadow:0px 0px 32px rgba(195, 228, 212, 0.767); 
  color: black !important;
}