当前位置:   article > 正文

[微信小程序]在图片上的特定区域点击实现功能_微信小程序image点击事件

微信小程序image点击事件

目录

一、功能描述

二、两种不同的解决方法

1.方法一:image组件和view组件相结合

2.方法二:获取点击事件的位置信息


一、功能描述

  对于前端初学者,在前端设计尤其是地图功能设计过程中,偶尔会碰到下面这个问题:实现在图片上特定区域内的点击进行跳转或其它功能。对于这个问题,本文提供两种不同的解决方法。

二、两种不同的解决方法

1.方法一:image组件和view组件相结合

结果展示

实现逻辑

在这个方法里,我们设置图片占满全屏,并设置其位置为相对位置。即position: relative;

之后,我们设置不同的view视图容器位置为绝地位置。即position: absolute;

然后设置image和view的z-index值,使得所有的view的z-index均大于image的

之后通过设置不同的view的top、left值来确定其位置,并设置width和height来设置其覆盖范围

代码片段

  1. wxml
  2. <view class="main">
  3. <image src="你的图片地址" mode="" class="pic"/>
  4. <view class="cloud" bindtap="CLOUD"></view>
  5. <view class="hk" bindtap="HK"></view>
  6. <view class="sj" bindtap="SJ"></view>
  7. <view class="tzl" bindtap="TZL"></view>
  8. <view class="sch" bindtap="SCH"></view>
  9. <view class="snsw" bindtap="SNSW"></view>
  10. <view class="mgxz" bindtap="MGXZ"></view>
  11. <view class="twt" bindtap="TWT"></view>
  12. <view class="tryc" bindtap="TRYC"></view>
  13. <view class="xkdt" bindtap="XKDT"></view>
  14. </view>
  15. <hsd_footer></hsd_footer>

  1. wxss
  2. .main {
  3. position: relative;
  4. height: 1061rpx;
  5. width: 750rpx;
  6. }
  7. .main .pic {
  8. position: relative;
  9. height: 1061rpx;
  10. width: 750rpx;
  11. z-index: 0;
  12. }
  13. .main .cloud {
  14. position: absolute;
  15. z-index: 2;
  16. top: 440rpx;
  17. left: 130rpx;
  18. width: 115rpx;
  19. height: 120rpx;
  20. background-color: transparent;
  21. }
  22. .main .hk {
  23. position: absolute;
  24. z-index: 2;
  25. top: 585rpx;
  26. left: 340rpx;
  27. width: 115rpx;
  28. height: 120rpx;
  29. background-color: transparent;
  30. }
  31. .main .sj {
  32. position: absolute;
  33. z-index: 2;
  34. top: 585rpx;
  35. left: 0rpx;
  36. width: 175rpx;
  37. height: 160rpx;
  38. background-color: transparent;
  39. }
  40. .main .tzl {
  41. position: absolute;
  42. z-index: 2;
  43. top: 170rpx;
  44. left: 155rpx;
  45. width: 130rpx;
  46. height: 100rpx;
  47. background-color: transparent;
  48. }
  49. .main .sch {
  50. position: absolute;
  51. z-index: 2;
  52. top: 285rpx;
  53. left: 240rpx;
  54. width: 160rpx;
  55. height: 130rpx;
  56. background-color: transparent;
  57. }
  58. .main .snsw {
  59. position: absolute;
  60. z-index: 2;
  61. top: 550rpx;
  62. left: 450rpx;
  63. width: 135rpx;
  64. height: 100rpx;
  65. background-color: transparent;
  66. }
  67. .main .mgxz {
  68. position: absolute;
  69. z-index: 2;
  70. top: 300rpx;
  71. left: 450rpx;
  72. width: 250rpx;
  73. height: 135rpx;
  74. background-color: transparent;
  75. }
  76. .main .twt {
  77. position: absolute;
  78. z-index: 2;
  79. top: 665rpx;
  80. left: 530rpx;
  81. width: 85rpx;
  82. height: 135rpx;
  83. background-color: transparent;
  84. }
  85. .main .tryc {
  86. position: absolute;
  87. z-index: 2;
  88. top: 690rpx;
  89. left: 220rpx;
  90. width: 85rpx;
  91. height: 135rpx;
  92. background-color: transparent;
  93. }
  94. .main .xkdt {
  95. position: absolute;
  96. z-index: 2;
  97. top: 810rpx;
  98. left: 350rpx;
  99. width: 120rpx;
  100. height: 135rpx;
  101. background-color: transparent;
  102. }

  1. json
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. //设置标记点
  8. markers: [
  9. {
  10. id: 4,
  11. latitude: 31.938841,
  12. longitude: 118.799698,
  13. width: 30,
  14. height: 30
  15. }
  16. ],
  17. latitude:'',
  18. longitude: '',
  19. },
  20. CLOUD() {
  21. wx.openLocation({
  22. latitude: 33.53761602081285, //目标位置的纬度
  23. longitude: 106.89011593077467, //目标位置的经度
  24. })
  25. },
  26. HK(){
  27. wx.openLocation({
  28. latitude:33.538952949314385, //目标位置的纬度
  29. longitude: 106.90333922121809, //目标位置的经度
  30. })
  31. },
  32. SJ(){
  33. wx.openLocation({
  34. latitude:33.53889035111852, //目标位置的纬度
  35. longitude: 106.91427726958082, //目标位置的经度
  36. })
  37. },
  38. TZL(){
  39. wx.openLocation({
  40. latitude: 33.59597002051454,
  41. longitude: 106.81963489641187,
  42. })
  43. },
  44. SNSW(){
  45. wx.openLocation({
  46. latitude: 33.53844588084152,
  47. longitude: 106.91443824626755,
  48. })
  49. },
  50. SCH(){
  51. wx.openLocation({
  52. latitude: 33.59288386524651,
  53. longitude: 106.80703582594046,
  54. })
  55. },
  56. MGXZ(){
  57. wx.openLocation({
  58. latitude:33.55299598713276 ,
  59. longitude: 106.91361805585858,
  60. })
  61. },
  62. TWT(){
  63. wx.openLocation({
  64. latitude: 33.52215281772647,
  65. longitude: 106.9233920221596,
  66. })
  67. },
  68. TRYC(){
  69. wx.openLocation({
  70. latitude: 33.5380812096245,
  71. longitude: 106.91394528200915,
  72. })
  73. },
  74. XKDT(){
  75. wx.openLocation({
  76. latitude: 33.5334644147482,
  77. longitude: 106.84775336441803,
  78. })
  79. },
  80. onLoad() {
  81. }
  82. })

补充

通过以上代码,我们实现了,点击地图上不同地方,可以直接跳转到该地方的导航上

2.方法二:获取点击事件的位置信息

实现逻辑

使用 <image> 组件来展示图片,并结合 <view> 组件和触摸事件绑定来实现点击功能。

在下面的 handleTap 函数中,我们使用 event.detail.xevent.detail.y 获取点击事件的横坐标和纵坐标信息。然后,你可以根据这些坐标信息来判断点击是否在特定的区域范围内,并执行相应的功能逻辑。

wxml界面

  1. <view>
  2. <image src="/path/to/image.jpg" mode="aspectFit" bindtap="handleTap"></image>
  3. </view>

json界面

  1. Page({
  2. handleTap: function(event) {
  3. // 获取点击事件的坐标信息
  4. var x = event.detail.x;
  5. var y = event.detail.y;
  6. // 根据坐标信息进行相应的处理逻辑
  7. // 比如判断点击的区域是否在特定的位置范围内,然后执行相应的功能
  8. if (x > 100 && x < 200 && y > 200 && y < 300) {
  9. // 在特定区域内执行的逻辑
  10. console.log("点击了特定区域");
  11. // 执行你的功能代码
  12. }
  13. }
  14. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/489696
推荐阅读
相关标签
  

闽ICP备14008679号