[SPIP] +2.1.12
[velocampus/web/www.git] / www / squelettes / javascript / smoothscroll.js
diff --git a/www/squelettes/javascript/smoothscroll.js b/www/squelettes/javascript/smoothscroll.js
new file mode 100644 (file)
index 0000000..9b8d4a0
--- /dev/null
@@ -0,0 +1,145 @@
+// JavaScript Document\r
+/* Smooth scrolling\r
+   Changes links that link to other parts of this page to scroll\r
+   smoothly to those links rather than jump to them directly, which\r
+   can be a little disorienting.\r
+   \r
+   sil, http://www.kryogenix.org/\r
+   \r
+   v1.0 2003-11-11\r
+   v1.1 2005-06-16 wrap it up in an object\r
+*/\r
+\r
+var ss = {\r
+  fixAllLinks: function() {\r
+    // Get a list of all links in the page\r
+    var allLinks = document.getElementsByTagName('a');\r
+    // Walk through the list\r
+    for (var i=0;i<allLinks.length;i++) {\r
+      var lnk = allLinks[i];\r
+      if ((lnk.href && lnk.href.indexOf('#') != -1) && \r
+          ( (lnk.pathname == location.pathname) ||\r
+           ('/'+lnk.pathname == location.pathname) ) && \r
+          (lnk.search == location.search)) {\r
+        // If the link is internal to the page (begins in #)\r
+        // then attach the smoothScroll function as an onclick\r
+        // event handler\r
+        ss.addEvent(lnk,'click',ss.smoothScroll);\r
+      }\r
+    }\r
+  },\r
+\r
+  smoothScroll: function(e) {\r
+    // This is an event handler; get the clicked on element,\r
+    // in a cross-browser fashion\r
+    if (window.event) {\r
+      target = window.event.srcElement;\r
+    } else if (e) {\r
+      target = e.target;\r
+    } else return;\r
+\r
+    // Make sure that the target is an element, not a text node\r
+    // within an element\r
+    if (target.nodeName.toLowerCase() != 'a') {\r
+      target = target.parentNode;\r
+    }\r
+  \r
+    // Paranoia; check this is an A tag\r
+    if (target.nodeName.toLowerCase() != 'a') return;\r
+  \r
+    // Find the <a name> tag corresponding to this href\r
+    // First strip off the hash (first character)\r
+    anchor = target.hash.substr(1);\r
+    // Now loop all A tags until we find one with that name\r
+    var allLinks = document.getElementsByTagName('a');\r
+    var destinationLink = null;\r
+    for (var i=0;i<allLinks.length;i++) {\r
+      var lnk = allLinks[i];\r
+      if (lnk.name && (lnk.name == anchor)) {\r
+        destinationLink = lnk;\r
+        break;\r
+      }\r
+    }\r
+  \r
+    // If we didn't find a destination, give up and let the browser do\r
+    // its thing\r
+    if (!destinationLink) return true;\r
+  \r
+    // Find the destination's position\r
+    var destx = destinationLink.offsetLeft; \r
+    var desty = destinationLink.offsetTop;\r
+    var thisNode = destinationLink;\r
+    while (thisNode.offsetParent && \r
+          (thisNode.offsetParent != document.body)) {\r
+      thisNode = thisNode.offsetParent;\r
+      destx += thisNode.offsetLeft;\r
+      desty += thisNode.offsetTop;\r
+    }\r
+  \r
+    // Stop any current scrolling\r
+    clearInterval(ss.INTERVAL);\r
+  \r
+    cypos = ss.getCurrentYPos();\r
+  \r
+    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);\r
+    ss.INTERVAL =\r
+setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);\r
+  \r
+    // And stop the actual click happening\r
+    if (window.event) {\r
+      window.event.cancelBubble = true;\r
+      window.event.returnValue = false;\r
+    }\r
+    if (e && e.preventDefault && e.stopPropagation) {\r
+      e.preventDefault();\r
+      e.stopPropagation();\r
+    }\r
+  },\r
+\r
+  scrollWindow: function(scramount,dest,anchor) {\r
+    wascypos = ss.getCurrentYPos();\r
+    isAbove = (wascypos < dest);\r
+    window.scrollTo(0,wascypos + scramount);\r
+    iscypos = ss.getCurrentYPos();\r
+    isAboveNow = (iscypos < dest);\r
+    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {\r
+      // if we've just scrolled past the destination, or\r
+      // we haven't moved from the last scroll (i.e., we're at the\r
+      // bottom of the page) then scroll exactly to the link\r
+      window.scrollTo(0,dest);\r
+      // cancel the repeating timer\r
+      clearInterval(ss.INTERVAL);\r
+      // and jump to the link directly so the URL's right\r
+      location.hash = anchor;\r
+    }\r
+  },\r
+\r
+  getCurrentYPos: function() {\r
+    if (document.body && document.body.scrollTop)\r
+      return document.body.scrollTop;\r
+    if (document.documentElement && document.documentElement.scrollTop)\r
+      return document.documentElement.scrollTop;\r
+    if (window.pageYOffset)\r
+      return window.pageYOffset;\r
+    return 0;\r
+  },\r
+\r
+  addEvent: function(elm, evType, fn, useCapture) {\r
+    // addEvent and removeEvent\r
+    // cross-browser event handling for IE5+,  NS6 and Mozilla\r
+    // By Scott Andrew\r
+    if (elm.addEventListener){\r
+      elm.addEventListener(evType, fn, useCapture);\r
+      return true;\r
+    } else if (elm.attachEvent){\r
+      var r = elm.attachEvent("on"+evType, fn);\r
+      return r;\r
+    } else {\r
+      alert("Handler could not be removed");\r
+    }\r
+  } \r
+}\r
+\r
+ss.STEPS = 25;\r
+\r
+ss.addEvent(window,"load",ss.fixAllLinks);\r