【问题标题】:Disable mouse wheel scrolling on Google My Maps iframe在 Google 我的地图 iframe 上禁用鼠标滚轮滚动
【发布时间】:2019-07-11 21:01:21
【问题描述】:

我在禁用 Google 我的地图(自定义地图)的 iframe 上的滚动时遇到问题。因为这也有可点击的事件,所以我不能使用 CSS“pointer-events:none”。我也在 iframe 本身上尝试了“滚动:否”,但这两种方法都不起作用。我终于用javascript as了。

<script>
    $(".map").bind("mousewheel", function() {
        return false;
    });
</script>

它可以工作,但是当我单击其中一个链接时,它又开始滚动了。如何明确禁用滚动但仍使用可点击事件。

【问题讨论】:

    标签: javascript jquery html google-maps mymaps


    【解决方案1】:

    一个工作示例:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
        .scrolloff {
            pointer-events: none;
        }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
    
            $('#map').addClass('scrolloff');                // set the mouse events to none when doc is ready
    
            $('#overlay').on("mouseup",function(){          // lock it when mouse up
                $('#map').addClass('scrolloff'); 
                //somehow the mouseup event doesn't get call...
            });
            $('#overlay').on("mousedown",function(){        // when mouse down, set the mouse events free
                $('#map').removeClass('scrolloff');
            });
    
            $("#map").mouseleave(function () {              // becuase the mouse up doesn't work... 
                $('#map').addClass('scrolloff');            // set the pointer events to none when mouse leaves the map area
                                                            // or you can do it on some other event
            });
    
        });
    </script>
    </head>
    <body>
    <div id="overlay" class="map">
        <iframe id="map" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyCjUE83FHrXTQWf9umcNDzyu0s7aNzHszw
        &q=Space+Needle,Seattle+WA" width="100%" height="500" frameborder="0" ></iframe>
    </div>
    
    </body>
    </html>
    

    现场演示:http://kylelam.github.io/iframe.html

    【讨论】:

    • 您能否发布您在页面上使用的代码的 sn-p?
    • @BasMatthee 明白了你的想法,“addClass”坚持让鼠标滚轮不滚动。谢谢!当我有点击事件时,我再次调用 $(".map").bind("mousewheel", function() { return false; });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多