百度地圖在移動端下click無效的解決方案


這是由於百度地圖在移動端屏蔽了click事件,在網上找到一種方法,利用touchClick方法來模擬click事件,代碼如下(需要JQ插件):

 1 //給jquery添加touchClick方法
 2 (function () {
 3     var defaults = {
 4         start: function (self, event) { },
 5         move: function (self, event) { },
 6         end: function (self, event) { }
 7     }
 8     $.fn.touchClick = function (opts) {
 9         if (typeof opts == "function") {
10             opts = $.extend({}, defaults, { end: opts });
11         } else {
12             opts = $.extend({}, defaults, opts);
13         }
14         this.each(function () {
15             var obj = $(this);
16             obj.bind("touchstart", function (event) {
17                 obj.data("move", false);
18                 opts.start.call(this, event);
19             }).bind("touchmove", function (event) {
20                 console.log(event.originalEvent.targetTouches.length);
21                 obj.data("move", true);
22                 opts.move.call(this, event);
23             }).bind("touchend", function (event) {
24                 if (obj.data("move")) {
25                     return;
26                 } else {
27                     opts.end.call(this, event);
28                 }
29                 obj.data("move", false);
30             });
31         });
32     };
33 })(jQuery);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM