javascript - ScrollTop in IE8 returning 0 - Stack Overflow

I've been looking this morning for an answers to my problem with getting the location of the windo

I've been looking this morning for an answers to my problem with getting the location of the window in IE8 to create a back to top button in IE8 with fade-in functionality.

Things that didn't work and returned zero in IE8:

window.pageYOffset 

$(window).scrollTop()

$(document).scrollTop()

$(this).scrollTop()

This is my code before my fix when it's only working for IE9+ & FF & Chrome

var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
    if (jQuery(this).scrollTop() > offset) {
        jQuery('.back-to-top').fadeIn(duration);
    } else {
        jQuery('.back-to-top').fadeOut(duration);
    }
});

jQuery('.back-to-top').click(function(event) {
    event.preventDefault();
    jQuery('html, body').animate({scrollTop: 0}, duration);
    return false;
})

And the CSS:

.back-to-top {
    position: fixed;
    bottom: 2em;
    right: 0px;
    text-decoration: none;
    color: #000000;
    background-color: #ebebeb;
    font-size: 12px;
    padding: 1em;
    display: none;
  }

.back-to-top:hover {  
  background-color: rgba(135, 135, 135, 0.50);
}

And here is the JSFiddle of the code that is not working: /

I've been looking this morning for an answers to my problem with getting the location of the window in IE8 to create a back to top button in IE8 with fade-in functionality.

Things that didn't work and returned zero in IE8:

window.pageYOffset 

$(window).scrollTop()

$(document).scrollTop()

$(this).scrollTop()

This is my code before my fix when it's only working for IE9+ & FF & Chrome

var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
    if (jQuery(this).scrollTop() > offset) {
        jQuery('.back-to-top').fadeIn(duration);
    } else {
        jQuery('.back-to-top').fadeOut(duration);
    }
});

jQuery('.back-to-top').click(function(event) {
    event.preventDefault();
    jQuery('html, body').animate({scrollTop: 0}, duration);
    return false;
})

And the CSS:

.back-to-top {
    position: fixed;
    bottom: 2em;
    right: 0px;
    text-decoration: none;
    color: #000000;
    background-color: #ebebeb;
    font-size: 12px;
    padding: 1em;
    display: none;
  }

.back-to-top:hover {  
  background-color: rgba(135, 135, 135, 0.50);
}

And here is the JSFiddle of the code that is not working: http://jsfiddle/VWOU/uG5mH/1/

Share asked Mar 8, 2014 at 7:26 FastSolutionsFastSolutions 1,8291 gold badge27 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The fix I've found was located here http://forums.asp/t/1618316.aspx

And it transforms my code to this (also cleaned up a little bit)

var offset = 220;
var duration = 500;
$(window).scroll(function() {
    if (document.documentElement.scrollTop || jQuery(this).scrollTop() > offset) {
        $('.back-to-top').fadeIn(duration);
    } else {
        $('.back-to-top').fadeOut(duration);
    }
});

$('.back-to-top').click(function(event) {
    event.preventDefault();
    $('html, body').animate({scrollTop: 0}, duration);
    return false;
})

And here is a working JSFiddle: http://jsfiddle/VWOU/uG5mH/3/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745073728a4609708.html

相关推荐

  • javascript - ScrollTop in IE8 returning 0 - Stack Overflow

    I've been looking this morning for an answers to my problem with getting the location of the windo

    20小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信