Home >Web Front-end >JS Tutorial >jQuery creates an alphabetical friendly page index (compatible with IE6/7/8)_jquery
Most web developers are likely familiar with the solution of using anchor links to jump to portions of the page. You can set a specific name attribute of the anchor link and use the href value as a hash symbol to skip pages. This effect is very practical when you need to list a long data set. For example, a FAQ page. This effect is often used in the form of questions and answers. However, page jumps are sometimes not good friends for visitors. Well, because it jumps directly with one click, unfamiliar visitors may be confused by this and don’t know where the current data goes,
In this tutorial, I will explore a solution to create a simple page index and employ anchor links on the page. The "jump" action animates scrolling down to the link's container. (Perfectly compatible with IE6, 7, 8)
Layout
First is the basic index.html page, I added a typical HTML5 document type. styles.css is our page style sheet and indexscroller.js is the customized jQuery code.
Don’t forget to introduce the Google jquery library when using jquery code. Old browsers do not support HTML5’s copy of the html5shiv trunk library. In the main body I'm using a custom Google Webfont, along with some artistic CSS3 effects.
一些默认的样式表的内容。除了典型的CSS重置 我用的是CSS3阴影
jQuery的scrollTop
jQuery有命名.scrollTop()方法,使用这种技术,可以拉动当前的像素值从页面顶部的任何其他选择的元素。我们从列表向下滚动,准确的锚链接。
#links Immediately after the internal link anchor is clicked, we call e.preventDefault(). This will stop the hash from jumping down instantly from the page appended to the URL. Then using a new jquery.hash attribute, we can exactly get the hash symbol after the href value. So, for example, our first index link would return the value "indexa".
Using this new attribute, we can match the name attribute on the corresponding anchor link page. We set a variable for this new anchorid, using anchorid.offset() to access the absolute pixels from the top. Finally add all this code into a simple jQuery .animate() method