简体   繁体   English

如何在鼠标悬停在谷歌地图标记上滚动窗口

[英]How to scroll window on mouse over on google map marker

I am trying to scroll or to a specific div on mouseover on google maps marker.我试图在谷歌地图标记上的鼠标悬停时滚动或滚动到特定的 div。 I am failing on the first step.我在第一步失败了。

Here is the initial shape of the code I am using alert is working but the window is not scrolling.这是我使用的代码的初始形状警报正在工作但窗口没有滚动。

marker.addListener('mouseover', function() {
    window.scrollTo(0,0);
    //alert(id);
});

marker.addListener('mouseover', function() {
    $('html, body').animate({
        scrollTop: $('html, body').offset().top
    }, 2000);
})

; ;

My question is why isn't it scrolling?我的问题是为什么它不滚动?

There is no error but the code isn't working.没有错误,但代码不起作用。

Try this working jsbin to see a working example of what you're trying to achieve.试试这个有效的 jsbin以查看您要实现的目标的有效示例。 First scroll down the page and you'll see a map;首先向下滚动页面,您将看到一张地图; mouse over the marker and the window will scroll back to the top of the page.将鼠标悬停在标记上,窗口将滚动回页面顶部。 Code below.代码如下。

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        html,
        body {
            height: 1600px;
        }

        body {
            position: relative;
        }

        #map-wrapper {
            width: 100%;
            position: absolute;
            bottom: 0;
        }

        #map {
            height: 400px;
        }
    </style>
</head>

<body>
    <h2>This is the top of the page</h2>
    <h3> Scroll down the page to see a Google map <i style="font-size:24px;" class="fa">&#xf063;</i></h3>

    <div id="map-wrapper">
        <h3>This is the map placed at the bottom of the page</h3>
        <h3>Mouse over the marker to go back to the top</h3>
        <div id="map"></div>
    </div>

    <script>
        function initMap() {
            var uluru = { lat: -25.344, lng: 131.036 };
            var map = new google.maps.Map(
                document.getElementById('map'), { zoom: 4, center: uluru });
            var marker = new google.maps.Marker({ position: uluru, map: map });

            marker.addListener('mouseover', function() {
                $('html, body').animate({
                    scrollTop: $('html, body').offset().top
                }, 1000);
                // You can use window.scrollTo(0, 0) instead if you prefer
                // window.scrollTo(0, 0);
            })
        }
    </script>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=&libraries=places&callback=initMap" async defer></script>
</body>

</html>

Hope this helps point you in the right direction!希望这有助于为您指明正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM