Js operation scroll bar event instance, js scroll bar instance
This article describes how to operate a scroll bar event in js. Share it with you for your reference. The specific analysis is as follows:
I have been wondering how to monitor the events of the scroll bar. Now I understand it.
The code below is the code that listens to the scroll bar as long as it moves, and the div display and hidden code at the top is returned below
Window. onscroll = function () {var t = document.doc umentElement. scrollTop | document. body. scrollTop; if (t> 0) {$ (". cbbfixed ").css (" bottom "," 10px ");} else {$ (". cbbfixed ").css (" bottom ","-85px ");}}
Note:
T: The distance from the scroll bar to the top end
T> 0, that is, once the scroll bar is rolled, the if () Statement is executed immediately. The code in else () is that when the scroll bar reaches the top, the div at the top is returned to hide
Return to the top button click operation:
$ ("# Cgotop"). click (function () {$ ('body, html '). animate ({scrollTop: 0}, 100); return false ;});
Supplement:
1. Listen to the scroll bar event of an element
$ (Selector). scroll (function (){.......});
2. Get the scroll distance of the scroll bar
$ (Selector). scrollTop (); $ (selector). scrollLefft ();
I hope this article will help you design javascript programs.