// the following establishes your own namespace, place everything about the scroll function in this namespacevar hlScroll = { // properties buffer: 50, // pixel buffer, positive is px above element, negative would move it so many pix below speed: 500, // scroll speed, 1000 = 1second // method scrollTo: function(e) { var gotoEle; // will be the element to find "top" too switch ($(this).attr('id')) { // same as an if else, but more visually sound on 2 strings in question case "next": // obviously go to next element // filter is jquery function that allows us to get only certain elements in a collection // here, our collection is ALL elements having class "highlight1" // the filter method is kind of like .each, it goes through each element, the i means "index" // in order for an element to stay in the collection, it must return true // in this case, looking for "next", I see if current element's top is higher than window top + our buffer // by including the buffer, I can make suer not to skip an element that might be "close" to the buffer but not over gotoEle = $('.highlight1').filter(function(i) { return $(this).offset().top > ($(window).scrollTop() + hlScroll.buffer) }).first(); // since we are getting next, we only want the first element returned, thus `.first()` will return just that! break; case "prev": // a lil dif from above, here we look for the last element who's top is higher than the window gotoEle = $('.highlight1').filter(function(i) { return $(this).offset().top < $(window).scrollTop() }).last(); break; } // the if statement here keeps the scroll from firing if no element found // if the element is found, we animate the page to scroll the element previously selected, - the buffer, keeping it so many pixels above the element (unless the buffer is negative) if (gotoEle.length) $("html, body").animate({ scrollTop: gotoEle.offset().top - hlScroll.buffer }, hlScroll.speed); // notice i selected "html, body". these are our main page elements which hold the page scroll // you could adjust this in the future to scroll a parent container instead! }}$(function() { $("#next, #prev").on("click", hlScroll.scrollTo).click();}); 示例var hlScroll = { buffer: 50, speed: 500, scrollTo: function(e) { var gotoEle; switch ($(this).attr('id')) { case "next": gotoEle = $('.highlight1').filter(function(i) { return $(this).offset().top > ($(window).scrollTop() + hlScroll.buffer) }).first(); break; case "prev": gotoEle = $('.highlight1').filter(function(i) { return $(this).offset().top < $(window).scrollTop() }).last(); break; } if (gotoEle.length) $("html, body").animate({ scrollTop: gotoEle.offset().top - hlScroll.buffer }, hlScroll.speed); }}$(function() { $("#next, #prev").on("click", hlScroll.scrollTo).click();});进一步阅读: .animate() .filter() .first() .last() .on() [正式称为.绑定,.. 删除 ,& .实时(在1.7之前的版本中) .scrollTop().animate().filter().first().last().on() [formally known as .bind, .delegate, & .live in versions pre-1.7].scrollTop() 这篇关于jQuery自动滚动到顶部的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 16:05
查看更多