簡體   English   中英

如何在鼠標懸停在谷歌地圖標記上滾動窗口

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

我試圖在谷歌地圖標記上的鼠標懸停時滾動或滾動到特定的 div。 我在第一步失敗了。

這是我使用的代碼的初始形狀警報正在工作但窗口沒有滾動。

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

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

;

我的問題是為什么它不滾動?

沒有錯誤,但代碼不起作用。

試試這個有效的 jsbin以查看您要實現的目標的有效示例。 首先向下滾動頁面,您將看到一張地圖; 將鼠標懸停在標記上,窗口將滾動回頁面頂部。 代碼如下。

<!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>

希望這有助於為您指明正確的方向!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM