懒加载谷歌地图

问题描述 投票:0回答:1

我想懒加载谷歌地图,以获得更好的页面加载速度,以利于SEO。我的javascript工作,但它仍然试图多次加载地图。我怎样才能让它只加载一次?我找到了其他的脚本,但我想尽可能少地使用代码。

我的脚本。

google = false;
$(window).scroll(function() {
   var hT = $('#google-map').offset().top,
       hH = $('#google-map').outerHeight(),
       wH = $(window).height(),
       wS = $(this).scrollTop();
   if (wS > (hT+hH-wH)){
        if (!google) $.getScript( 'https://maps.googleapis.com/maps/api/js?key={key}', function( google, textStatus, jqxhr ) {
            initialize(); 
        });   
   }
});

控制台日志。

js?key={key}&_=1591085205888:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205888:140
js?key={key}&_=1591085205890:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205890:140
js?key={key}&_=1591085205891:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205891:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.

先谢谢你!Peter

javascript google-maps lazy-loading
1个回答
1
投票

我认为你是运行脚本一遍又一遍?在if语句后加一个google = true?

google = false;
$(window).scroll(function() {
   var hT = $('#google-map').offset().top,
       hH = $('#google-map').outerHeight(),
       wH = $(window).height(),
       wS = $(this).scrollTop();
   if (wS > (hT+hH-wH)){
        if (!google) {
           google = true; // stop the check
           // enter your loading script here, use debounce and promise for async.
        }
   }
});

根据建议修改 音乐迷你也可以把它做成这样

    var $googleMap = $('#google-map'); // use a ref
    var $window = $(window); // use a ref
    var google = false;

$(window).scroll(function() {
    var hT = $googleMap.offset().top,
        hH = $googleMap.outerHeight(),
        wH = $window.height(),
        wS = $window.scrollTop();
    if (wS > (hT+hH-wH)){
         // if you do the unregister approach, you dont even need to check for google var
        if (!google) { 
            google = true; // stop the check         
            // enter your loading script here, use debounce and promise for async.
        // unregister your event as musefan said
                    }
               }
            });

0
投票

这个可以,只发射一次。谢谢 Sprep && Musefan。

    var fired = false;
    $(window).scroll(function() {
       var hT = $('#google-map').offset().top,
           hH = $('#google-map').outerHeight(),
           wH = $(window).height(),
           wS = $(this).scrollTop();
           wB = hT+hH-wH;
        if (wS > wB  && fired === false ){
            console.log('This will happen only once'); 
            fired = true;
            $.getScript( 'https://maps.googleapis.com/maps/api/js?key={key}', function( google, textStatus, jqxhr ) {
                initialize(); 
            });   
       }
    });
© www.soinside.com 2019 - 2024. All rights reserved.