Advertisement
thetenfold

Unroller

Sep 14th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.  
  6. <meta charset="UTF-8">
  7. <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
  8.  
  9. <title>Unroller by JoeSimmons</title>
  10.  
  11. <script type="text/javascript">
  12.  
  13. function unroll(obj) {
  14. 'use strict';
  15.  
  16. // error prevention
  17. var settings = obj || {};
  18.  
  19. // set constants
  20. var pause = Math.floor( (typeof settings.pause === 'number' ? settings.pause : 1) * 1000 ); // default 1 second
  21. var width = settings.width || 1000; // default 1000 px
  22. var interval = settings.interval || 33; // default 30 fps
  23. var increment = settings.increment || 5; // default 5 px
  24. var limit = typeof settings.limit === 'number' && settings.limit > 0 ? settings.limit : 0; // default infinite
  25.  
  26. // set other variables
  27. var element; // holds the element. is updated in .start()
  28. var current_iteration = 0; // keep track of how many times the text is unrolled
  29. var current_width; // keep track of the element's width
  30. var enabled = false; // this is so the unroller can be stopped, but then started again
  31. var intv; // a variable to hold a setInterval instance
  32. var thisObj; // so the constructor can have an easier time referring to the methods it returns
  33.  
  34. // this function handles all the 'width' style changes
  35. function changeWidth() {
  36. if (current_width > width) { // we hit our max width
  37. window.clearInterval(intv); // stop changing the width for this element
  38. current_iteration += 1;
  39.  
  40. // if the unroller hasn't hit its limit,
  41. // or the limit is infinite (0), keep unrolling
  42. if (limit === 0 || current_iteration < limit) {
  43. // after the pause amount, do this
  44. // pretty self-explanitory. that's the way I like to write
  45. window.setTimeout(function () {
  46. // check the 'enabled' boolean in-case the user stopped
  47. // the unroller when it was paused
  48. if (enabled === true) {
  49. current_width = 0;
  50. element.style.width = '0px';
  51. element.style.top = getRandomInt(0, 170) + 'px';
  52. thisObj.start(); // start unrolling again
  53. }
  54. }, pause);
  55. }
  56. } else { // we didn't hit our max width yet
  57. element.style.width = current_width + 'px'; // set the width
  58. current_width += increment; // increment the width
  59. }
  60. }
  61.  
  62. function getRandomInt(min, max) {
  63. return Math.floor( Math.random() * (max - min + 1) ) + min;
  64. }
  65.  
  66. // we use 'thisObj' (or another variable) so changeWidth()
  67. // can call this method, as well as the user
  68. thisObj = {
  69. 'start' : function () {
  70. // re-get the element in-case it was null when
  71. // the unroller was created
  72. if (!element) {
  73. element = document.getElementById(settings.id);
  74. }
  75.  
  76. if (element) { // throw an error if the id the user passed was null
  77. thisObj.stop(); // clear any old interval
  78. enabled = true;
  79. current_width = element.offsetWidth; // grab our current width
  80. intv = window.setInterval(changeWidth, interval);
  81. } else {
  82. throw new Error('Unroller: The ID "' + settings.id + '" is null.');
  83. }
  84. },
  85. 'stop' : function () {
  86. enabled = false;
  87. if (typeof intv === 'number') {
  88. window.clearInterval(intv);
  89. }
  90. }
  91. };
  92.  
  93. return thisObj;
  94. }
  95.  
  96. var newUnroll = new unroll( {
  97. 'id' : 'strap', // id of the element to use
  98. 'pause' : 0, // how many seconds to pause for between unrollings
  99. 'width' : 600, // max width of the element
  100. 'interval' : 1000 / 60, // interval when which the element's width will be changed
  101. 'increment' : 10, // how many pixels to widen the element by each interval
  102. 'limit' : -1 // how many times to unroll the element ( <= 0 is infinite )
  103. } );
  104.  
  105. </script>
  106.  
  107. </head>
  108.  
  109. <body>
  110.  
  111. <p id="strap" style="width: 0px; overflow: hidden; position: fixed; white-space: nowrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam et lacus enim. Etiam mattis ligula et tellus iaculis tempor. Etiam lobortis lobortis aliquet. Phasellus a tristique lacus, vitae aliquam quam. Phasellus non enim nibh. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam sit amet massa sed ipsum gravida iaculis. Duis feugiat massa non fringilla suscipit. Nam posuere posuere convallis. Aenean auctor urna eget nulla dignissim vestibulum. Vivamus hendrerit massa eu congue ornare. Sed id vulputate metus. Duis nec nibh sit amet mauris auctor feugiat in quis purus amet.</p>
  112.  
  113. <div style="position: fixed; bottom: 40%; left: 5%;">
  114. <input type="button" value="Start" onclick="newUnroll.start()" />
  115. &nbsp;&nbsp;
  116. <input type="button" value="Stop" onclick="newUnroll.stop()" />
  117. </div>
  118.  
  119. </body>
  120.  
  121. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement