JS onscroll / scrollTop无法在IE和Firefox中运行

时间:2015-11-04 15:47:16

标签: javascript jquery

我正在尝试使用javascript将浮动代码放入Joomla网站。

使用此脚本,代码在Chrome上运行良好:

<script>
window.onscroll = function() {myFunction();};
var towerRightAd = document.getElementById("bodyTowerRightAd");

function myFunction() {
    if (document.body.scrollTop > 150) {
        if (towerRightAd.className != "bodyTowerRighttAd floatTowerAds" )  {
            towerRightAd.className = towerRightAd.className + " floatTowerAds";
        }
    }
    else
    {
        towerRightAd.className = "bodyTowerRightAd";
    }
}
</script>

如何让这段代码适用于FF和IE。

演示:http://jsbin.com/vebenikoje/1/edit?html,css,js,output

1 个答案:

答案 0 :(得分:0)

试试这个:

   window.onscroll = function() {
      myFunction();
    };

    var towerRightAd = document.getElementById("bodyTowerRightAd");

    function myFunction() {
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop;

      if (scrollTop > 150) {
        if (towerRightAd.className != "bodyTowerRighttAd floatTowerAds" ) {
           towerRightAd.className = towerRightAd.className + " floatTowerAds";
        }
      } else {
        towerRightAd.className = "bodyTowerRightAd";
      }
    }