2015-10-30 154 views
9

我使用谷歌pubads http://development-client-server.com/ds/這是偉大的,直到你到達實際的故事頁面(見http://development-client-server.com/ds/speech-more-common-autism/),當右上邊欄廣告將加載,然後迅速消失。谷歌pubads出現,然後消失

我已經縮小到了stickySidebars函數,我用它來粘貼左側的社交媒體欄和右側的職位列表div(在Google廣告的下面)。但是,粘性功能應該不會影響Google廣告呢?

這裏的JS功能我用,我已經重寫好幾次(並試圖說服客戶使用出來已經是)。

<script> 

// Sticky Sidebars 

function stickySidebars() { 

    var length = $('.post-content').height() - $('article .sharing-links').height() + $('.post-content').offset().top; 
    var lengthSidebar = $('.sticky-container').height() - $('aside .job-listings').height() + $('.sticky-container').offset().top -30; 

    $(window).scroll(function() { 

     // Sharing Links 

     var scroll = $(this).scrollTop() + 90; 
     var height = $('article .sharing-links').height() + 'px'; 

     if (scroll < $('.post-content').offset().top) { 

      $('article .sharing-links').css({ 
       'position': 'absolute', 
       'top': 'auto', 
       'bottom': 'auto' 
      }); 

     } else if (scroll > length) { 

      $('article .sharing-links').css({ 
       'position': 'absolute', 
       'bottom': '0', 
       'top': 'auto' 
      }); 

     } else { 

      $('article .sharing-links').css({ 
       'position': 'fixed', 
       'top': '90px', 
       'height': height 
      }); 

     } 

     // Sidebar 

     var heightSidebar = $('aside .job-listings').height() + 'px'; 

     if (scroll < $('aside .job-listings').offset().top) { 

      $('aside .job-listings').css({ 
       'position': 'absolute', 
       'top': '300px', 
       'bottom': 'auto' 
      }); 

     } else if (scroll > lengthSidebar) { 

      $('aside .job-listings').css({ 
       'position': 'absolute', 
       'bottom': '30px', 
       'top': 'auto' 
      }); 

     } else { 

      if (scroll < $('.sticky-container').offset().top + 300) { 

       $('aside .job-listings').css({ 
        'position': 'absolute', 
        'top': '300px', 
        'bottom': 'auto' 
       }); 

      } else { 

       $('aside .job-listings').css({ 
        'position': 'fixed', 
        'top': '90px', 
        'height': heightSidebar 
       }); 
      } 

     } 
    }); 
} 

$(window).on('load',function(){ 
    if($(window).width() > 1100){ 
     stickySidebars(); 
    } 
}); 

$(window).resize(function() { 
    if($(window).width() > 1100){ 
     stickySidebars(); 
    } 
}); 
</script> 
+1

所以只是爲了確認:當你註釋掉/刪除'stickySidebars()'的廣告顯示爲預期? – tw16

+1

哦地獄 - 它在我移除stickySidebars()時確實有效。現在它不。 –

回答

6

該問題不是由粘性邊欄造成的。它是由該位的代碼引起的:

$(window).on('load',function(){ 
    // Other unrelated functions here... 

    /*****************************/ 
    // Move Sidebar in Mobile 
    /*****************************/ 

    if($(window).width() <= 980){ 
     $("aside").appendTo(".mobile-sidebar"); 
    } else { 
     $("aside").insertAfter(".single-article article"); 
    } 
}); 

本質上加載廣告,然後您將容器(在aside),這將導致廣告消失。

有幾個不同的選擇,但本質上,你要麼需要在谷歌的廣告腳本代碼段後運行,或者您需要刷新的廣告。要刷新廣告,您應該能夠在if else語句後直接運行這一行代碼:

googletag.pubads().refresh() 

這會刷新所有廣告。根據您的設置,您可以將變量傳遞給refresh(),以便特定的廣告可以刷新。

var slot1 = googletag.pubads().display('/1234567/sports', [728, 90], 'div-1'); 

googletag.pubads().refresh([slot1]); 

Google Reference Docs for refresh()

+0

奇怪的是,它最初是我刪除的stickySidebars代碼,然後工作。感謝這! –