tailieunhanh - JQuery: Novice to Ninja- P17

JQuery: Novice to Ninja- P17:No matter what kind of ninja you are—a cooking ninja, a corporate lawyer ninja, or an actual ninja ninja—virtuosity lies in first mastering the basic tools of the trade. Once conquered, it’s then up to the full-fledged ninja to apply that knowledge in creative and inventive ways. | Construction Ajax and Interactivity 217 chapter_06 11_endless_scrolling excerpt checkscroll function var gallery_div if gallery_div 0 .scrollHeight - - 0 Our checkscroll function looks a bit complex at first but there s really little to it. We re going to be messing around with the gallery s containing object quite a bit in this function so we store a reference to it to avoid running the same selector over and over. This would lead to a significant performance hit especially as jQuery will generate lots of events whenever the user scrolls the scroll bar. Next we do some math to determine whether the scroll bar has hit the bottom yet. For this we ll need to break out of jQuery for a bit and deal with some plain old JavaScript. The scrollHeight property is a nonstandard JavaScript property that s nonetheless supported by all major browsers. It tells us the total scrollable height of an element unlike height which only tells us how much vertical space the element occupies on the page. In order to access it we need to pull the raw DOM node from the jQuery object the shortcut for this is 0 . By subtracting the element s height and the current scrolling position from this scrollHeight property we ll determine how far from the bottom of the element the user is scrolled to. If this equals 0 the scroll bar is resting at the bottom and we can load the images. But what happens if the user starts scrolling up and down like a crazy person Will we start firing off requests willy-nilly We sure will And as the requests start returning our code will start adding in images lots of them That may be a little inappropriate for our gallery control so as a final touch let s build in a small safeguard. We ll add a Boolean property to our GALLERY object called running. When we re about to load some data we ll first check that the running variable is set to true. If it is this means an Ajax call is currently .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.