search

Home  >  Q&A  >  body text

javascript - The difference between html and body in jquery selector

//第一种是正常的点击返回顶部
// 滚动条
$('#back_to_top').click(function(){
    // $(window).scrollTop(0);
})

//第二种是加了滚动条变化的动画效果
// 滚动条
$('#back_to_top').click(function(){
    $('html,body').stop().animate({
        scrollTop:"0px"
    },"slow")
})

The first time is $(window) and the second time is $('html,body')
What are the differences between these three? When to use window, when to use html, and when to use body?
Also why sometimes both html and body are written in.

怪我咯怪我咯2745 days ago723

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-05-19 10:45:53

    $('html,body') Why do you need to write 2? It's because firefox ie does not support body, but chrome supports body, so I write it like this for compatibility. You can try firefox and chrome to understand.

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:45:53

    For forms and DOM objects, obtaining relevant properties is also related to the document model. As for the scrollTop you mentioned, the xhtml mode body is 0, html is the scroll height, and the backcompat mode returns
    window's scrollTop returns the scroll height pageYOffse

    reply
    0
  • Cancelreply