Using js to implement the html anchor function, you can locate the anchor as needed, which is more flexible than the anchor.

Source: Internet
Author: User
Today, I recorded a js function and found it useful. I would like to share it with you. Here I use the windowscrollTo () function of js. I will repeat the usage of this function. The syntax is scrollTo (x, y ), here, x indicates the x coordinate of the document displayed in the "display" Area of the window document. Today, y records a js function. I found this function very useful and I will share it with you. Here I use the js window. scrollTo () function. The usage of this function is repeated. The syntax is scrollTo (x, y). Here, x indicates the x coordinate of the document displayed in the "display" Area of the window document, y indicates the y coordinate of the document displayed in the document display area of the window. All general web pages scrollTo (0, n) can achieve vertical scrolling, but here I use horizontal scrolling, the same effect as win8, use scrollTo (n, 0 ). In order to move to the corresponding functional module horizontally by clicking the navigation bar, we set an Id for the div of each functional module and obtain the distance from the functional module to the left of the webpage through the Id! We can get it through the offsetLeft attribute of the element and write it to: document. getElementbyId (id ). offsetLeft, then move to the function module location. We simply call the js statement window. scrollTo (document. getElementbyId (id ). offsetLeft! If it is so simple, I will not write this blog record here! The effect I want to achieve is to "Slowly move" to the html anchor implemented using js. the anchor is the div of the function module mentioned above, use the Id of the div to manipulate the div. So how can we slowly move to the position of the anchor? This is a parallel movement effect. Of course, you can write the vertical movement effect during the implementation process. Now that we need to move from the current position to the specified position, we need to know the current position. Some Baidu data shows that window. pageXOffset can get the position of the current horizontal scroll bar distance coordinate. To start from the current position window. pageXOffset is smoothly moved to the target position document. getElementbyId (id ). offsetLeft, the distance between our equals points, for example, 10 equals points Xoffset10 = Math. round (window. pageXOffset-ocument.getElementbyId (id ). offsetLeft)/10); add each time, and stop when you know the similarity! Finally, I will give you the code, hoping to help you and understand the "essence". If you have any questions, please pay attention to it. 1 $ (function () {2 $ ('# Nav li '). click (function (e) {3 var divId = $ (this ). attr ('refertodiv '); 4 var scrollTox = document. getElementById (divId ). offsetLeft-document. getElementById ("divForumSupport "). offsetLeft; 5 var pageXOffset = window. pageXOffset; 6 Scrollx (pageXOffset, scrollTox); 7}); 8}); 9 var isFirstEnter = true; 10 var Xoffset10, addXoffset; 11 function Scrollx (oldXoffset, newXoffset) {12 if (isFirstEnter) 13 {14 Xoffset10 = Math. round (newXoffset-oldXoffset)/10); 15 addXoffset = oldXoffset; 16 isFirstEnter = false; 17} 18 addXoffset + = Xoffset10; 19 if (oldXoffset <= newXoffset) = (addXoffset <newXoffset) 20 {21 window. scrollTo (addXoffset, 0); 22 window. setTimeout (function () {Scrollx (oldXoffset, newXoffset)}, 1) 23} 24 else25 {26 window. scrollTo (newXoffset, 0); 27 isFirstEnter = true; 28} 29}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.