smooth scroll script

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

smooth scroll script

 
0
  #1
Jul 2nd, 2009
ayesha789 asked me how to use my smooth scroll script in code snippets section.

i created an example for him here :

htmlpage :

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>Untitled Page</title>
  5. <script type="text/javascript">
  6. function elementPosition(obj) {
  7. var curleft = 0, curtop = 0;
  8.  
  9. if (obj.offsetParent) {
  10. curleft = obj.offsetLeft;
  11. curtop = obj.offsetTop;
  12.  
  13. while (obj = obj.offsetParent) {
  14. curleft += obj.offsetLeft;
  15. curtop += obj.offsetTop;
  16. }
  17. }
  18.  
  19. return { x: curleft, y: curtop };
  20. }
  21.  
  22. function ScrollToControl(id)
  23. {
  24. var elem = document.getElementById(id);
  25. var scrollPos = elementPosition(elem).y;
  26. scrollPos = scrollPos - document.documentElement.scrollTop;
  27. var remainder = scrollPos % 50;
  28. var repeatTimes = (scrollPos - remainder) / 50;
  29. ScrollSmoothly(scrollPos,repeatTimes);
  30. window.scrollBy(0,remainder);
  31. }
  32. var repeatCount = 0;
  33. var cTimeout;
  34. var timeoutIntervals = new Array();
  35.  
  36. var timeoutIntervalSpeed;
  37. function ScrollSmoothly(scrollPos,repeatTimes)
  38. {
  39. if(repeatCount < repeatTimes)
  40. {
  41. window.scrollBy(0,50);
  42. }
  43. else
  44. {
  45. repeatCount = 0;
  46. clearTimeout(cTimeout);
  47. return;
  48. }
  49. repeatCount++;
  50. cTimeout = setTimeout("ScrollSmoothly('" + scrollPos + "','"+ repeatTimes +"')",10);
  51. }
  52.  
  53.  
  54. </script>
  55. </head>
  56. <body>
  57. <a href="#" onclick="ScrollToControl('mylinkToScroll')">scroll to target</a>
  58. <p>
  59. <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting
  60. industry. Lorem Ipsum has been the industry's standard dummy text ever since the
  61. 1500s, when an unknown printer took a galley of type and scrambled it to make a
  62. type specimen book. It has survived not only five centuries, but also the leap into
  63. electronic typesetting, remaining essentially unchanged. It was popularised in the
  64. 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
  65. recently with desktop publishing software like Aldus PageMaker including versions
  66. of Lorem Ipsum.</p>
  67. <div class="rc">
  68. <h2 class="why">
  69. <span>Why do we use it?</span></h2>
  70. <p>
  71. It is a long established fact that a reader will be distracted by the readable content
  72. of a page when looking at its layout. The point of using Lorem Ipsum is that it
  73. has a more-or-less normal distribution of letters, as opposed to using 'Content
  74. here, content here', making it look like readable English. Many desktop publishing
  75. packages and web page editors now use Lorem Ipsum as their default model text, and
  76. a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
  77. versions have evolved over the years, sometimes by accident, sometimes on purpose
  78. (injected humour and the like).</p>
  79. </div>
  80. &nbsp;
  81. <div style="clear: both">
  82. <div class="lc">
  83. <h2 class="where">
  84. <span>Where does it come from?</span></h2>
  85. <p>
  86. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
  87. in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
  88. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
  89. up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,
  90. and going through the cites of the word in classical literature, discovered the
  91. undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
  92. Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
  93. This book is a treatise on the theory of ethics, very popular during the Renaissance.
  94. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
  95. in section 1.10.32.</p>
  96. <p>
  97. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those
  98. interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by
  99. Cicero are also reproduced in their exact original form, accompanied by English
  100. versions from the 1914 translation by H. Rackham.</p>
  101. </div>
  102. <div class="rc">
  103. <h2 class="getsome">
  104. <span>Where can I get some?</span></h2>
  105. <p>
  106. There are many variations of passages of Lorem Ipsum available, but the majority
  107. have suffered alteration in some form, by injected humour, or randomised words which
  108. don't look even slightly believable. If you are going to use a passage of Lorem
  109. Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle
  110. of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined
  111. chunks as necessary, making this the first true generator on the Internet. It uses
  112. a dictionary of over 200 Latin words, combined with a handful of model sentence
  113. structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem
  114. Ipsum is therefore always free from repetition, injected humour, or non-characteristic
  115. words etc.</p>
  116. <p>
  117. <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting
  118. industry. Lorem Ipsum has been the industry's standard dummy text ever since the
  119. 1500s, when an unknown printer took a galley of type and scrambled it to make a
  120. type specimen book. It has survived not only five centuries, but also the leap into
  121. electronic typesetting, remaining essentially unchanged. It was popularised in the
  122. 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
  123. recently with desktop publishing software like Aldus PageMaker including versions
  124. of Lorem Ipsum.</p>
  125. <div class="rc">
  126. <h2 class="why">
  127. <span>Why do we use it?</span></h2>
  128. <p>
  129. It is a long established fact that a reader will be distracted by the readable content
  130. of a page when looking at its layout. The point of using Lorem Ipsum is that it
  131. has a more-or-less normal distribution of letters, as opposed to using 'Content
  132. here, content here', making it look like readable English. Many desktop publishing
  133. packages and web page editors now use Lorem Ipsum as their default model text, and
  134. a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
  135. versions have evolved over the years, sometimes by accident, sometimes on purpose
  136. (injected humour and the like).</p>
  137. </div>
  138. <p>
  139. &nbsp;
  140. </p>
  141. <div style="clear: both">
  142. <div class="lc">
  143. <h2 class="where">
  144. <span>Where does it come from?</span></h2>
  145. <p>
  146. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
  147. in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
  148. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
  149. up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,
  150. and going through the cites of the word in classical literature, discovered the
  151. undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
  152. Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
  153. This book is a treatise on the theory of ethics, very popular during the Renaissance.
  154. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
  155. in section 1.10.32.</p>
  156. <p>
  157. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those
  158. interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by
  159. Cicero are also reproduced in their exact original form, accompanied by English
  160. versions from the 1914 translation by H. Rackham.</p>
  161. </div>
  162. <div class="rc">
  163. <h2 class="getsome">
  164. <span>Where can I get some?</span></h2>
  165. <p>
  166. There are many variations of passages of Lorem Ipsum available, but the majority
  167. have suffered alteration in some form, by injected humour, or randomised words which
  168. don't look even slightly believable. If you are going to use a passage of Lorem
  169. Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle
  170. of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined
  171. chunks as necessary, making this the first true generator on the Internet. It uses
  172. a dictionary of over 200 Latin words, combined with a handful of model sentence
  173. structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem
  174. Ipsum is therefore always free from repetition, injected humour, or non-characteristic
  175. words etc.</p>
  176. <p>
  177. <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting
  178. industry. Lorem Ipsum has been the industry's standard dummy text ever since the
  179. 1500s, when an unknown printer took a galley of type and scrambled it to make a
  180. type specimen book. It has survived not only five centuries, but also the leap into
  181. electronic typesetting, remaining essentially unchanged. It was popularised in the
  182. 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
  183. recently with desktop publishing software like Aldus PageMaker including versions
  184. of Lorem Ipsum.</p>
  185. <div class="rc">
  186. <h2 class="why">
  187. <span>Why do we use it?</span></h2>
  188. <p>
  189. It is a long established fact that a reader will be distracted by the readable content
  190. of a page when looking at its layout. The point of using Lorem Ipsum is that it
  191. has a more-or-less normal distribution of letters, as opposed to using 'Content
  192. here, content here', making it look like readable English. Many desktop publishing
  193. packages and web page editors now use Lorem Ipsum as their default model text, and
  194. a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
  195. versions have evolved over the years, sometimes by accident, sometimes on purpose
  196. (injected humour and the like).</p>
  197. </div>
  198. <p>
  199. &nbsp;
  200. </p>
  201. <div style="clear: both">
  202. <div class="lc">
  203. <h2 class="where">
  204. <span>Where does it come from?</span></h2>
  205. <p>
  206. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
  207. in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
  208. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
  209. up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,
  210. and going through the cites of the word in classical literature, discovered the
  211. undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
  212. Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
  213. This book is a treatise on the theory of ethics, very popular during the Renaissance.
  214. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
  215. in section 1.10.32.</p>
  216. <p>
  217. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those
  218. interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by
  219. Cicero are also reproduced in their exact original form, accompanied by English
  220. versions from the 1914 translation by H. Rackham.</p>
  221. </div>
  222. <div class="rc">
  223. <h2 class="getsome">
  224. <span>Where can I get some?</span></h2>
  225. <p>
  226. There are many variations of passages of Lorem Ipsum available, but the majority
  227. have suffered alteration in some form, by injected humour, or randomised words which
  228. don't look even slightly believable. If you are going to use a passage of Lorem
  229. Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle
  230. of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined
  231. chunks as necessary, making this the first true generator on the Internet. It uses
  232. a dictionary of over 200 Latin words, combined with a handful of model sentence
  233. structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem
  234. Ipsum is therefore always free from repetition, injected humour, or non-characteristic
  235. words etc.</p>
  236. <p>
  237. <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting
  238. industry. Lorem Ipsum has been the industry's standard dummy text ever since the
  239. 1500s, when an unknown printer took a galley of type and scrambled it to make a
  240. type specimen book. It has survived not only five centuries, but also the leap into
  241. electronic typesetting, remaining essentially unchanged. It was popularised in the
  242. 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
  243. recently with desktop publishing software like Aldus PageMaker including versions
  244. of Lorem Ipsum.</p>
  245. <div class="rc">
  246. <h2 class="why">
  247. <span>Why do we use it?</span></h2>
  248. <p>
  249. It is a long established fact that a reader will be distracted by the readable content
  250. of a page when looking at its layout. The point of using Lorem Ipsum is that it
  251. has a more-or-less normal distribution of letters, as opposed to using 'Content
  252. here, content here', making it look like readable English. Many desktop publishing
  253. packages and web page editors now use Lorem Ipsum as their default model text, and
  254. a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
  255. versions have evolved over the years, sometimes by accident, sometimes on purpose
  256. (injected humour and the like).</p>
  257. </div>
  258. <p>
  259. &nbsp;
  260. </p>
  261. <div style="clear: both">
  262. <div class="lc">
  263. <h2 class="where">
  264. <span>Where does it come from?</span></h2>
  265. <p>
  266. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
  267. in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
  268. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
  269. up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,
  270. and going through the cites of the word in classical literature, discovered the
  271. undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
  272. Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
  273. This book is a treatise on the theory of ethics, very popular during the Renaissance.
  274. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
  275. in section 1.10.32.</p>
  276. <p>
  277. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those
  278. interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by
  279. Cicero are also reproduced in their exact original form, accompanied by English
  280. versions from the 1914 translation by H. Rackham.</p>
  281. </div>
  282. <div class="rc">
  283. <h2 class="getsome">
  284. <span>Where can I get some?</span></h2>
  285. <p>
  286. There are many variations of passages of Lorem Ipsum available, but the majority
  287. have suffered alteration in some form, by injected humour, or randomised words which
  288. don't look even slightly believable. If you are going to use a passage of Lorem
  289. Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle
  290. of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined
  291. chunks as necessary, making this the first true generator on the Internet. It uses
  292. a dictionary of over 200 Latin words, combined with a handful of model sentence
  293. structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem
  294. Ipsum is therefore always free from repetition, injected humour, or non-characteristic
  295. words etc.</p>
  296. <p>
  297. &nbsp;</p>
  298. <a id="mylinkToScroll" href="#">scroll target</a>
  299. </div>
  300. </div>
  301. </div>
  302. </div>
  303. </div>
  304. </div>
  305. </div>
  306. </div>
  307.  
  308. </body>
  309. </html>

i attach the file to this post you can download and run to see how it works if you are lazy enough.
Attached Files
File Type: zip HTMLPage1.zip (3.2 KB, 3 views)
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC