Guest User

Untitled

a guest
Jul 16th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. /*globals window, $, clearInterval, setInterval */
  2.  
  3. $(function () {
  4. "use strict";
  5.  
  6. var hl,
  7. newsList = $('.news-headlines'),
  8. newsListItems = $('.news-headlines li'),
  9. firstNewsItem = $('.news-headlines li:nth-child(1)'),
  10. newsPreview = $('.news-preview'),
  11. elCount = $('.news-headlines').children(':not(.highlight)').index(),
  12. vPadding = (parseInt(firstNewsItem.css('padding-top').replace('px', ''), 10)) +
  13. (parseInt(firstNewsItem.css('padding-bottom').replace('px', ''), 10)),
  14. vMargin = (parseInt(firstNewsItem.css('margin-top').replace('px', ''), 10)) +
  15. (parseInt(firstNewsItem.css('margin-bottom').replace('px', ''), 10)),
  16. cPadding = (parseInt($('.news-content').css('padding-top').replace('px', ''), 10)) +
  17. (parseInt($('.news-content').css('padding-bottom').replace('px', ''), 10)),
  18. speed = 5000, // this is the speed of the switch
  19. myTimer = null,
  20. siblings = null,
  21. totalHeight = null,
  22. indexEl = 1,
  23. i = null;
  24.  
  25. // the css animation gets added dynamicallly so
  26. // that the news item sizes are measured correctly
  27. // (i.e. not in mid-animation)
  28. // Also, appending the highlight item to keep HTML clean
  29. newsList.append('<li class="highlight nh-anim"></li>');
  30. hl = $('.highlight');
  31. newsListItems.addClass('nh-anim');
  32.  
  33. function doEqualHeight(c) {
  34.  
  35. if (newsPreview.height() < newsList.height()) {
  36. newsPreview.height(newsList.height());
  37. } else if ((newsList.height() < newsPreview.height()) && (newsList.height() > parseInt(newsPreview.css('min-height').replace('px', ''), 10))) {
  38. newsPreview.height(newsList.height());
  39. }
  40.  
  41. if ($('.news-content:nth-child(' + c + ')').height() > newsPreview.height()) {
  42. newsPreview.height($('.news-content:nth-child(' + c + ')').height() + cPadding);
  43. }
  44. }
  45.  
  46. function doTimedSwitch() {
  47.  
  48. myTimer = setInterval(function () {
  49. if (($('.selected').prev().index() + 1) === elCount) {
  50. firstNewsItem.trigger('click');
  51. } else {
  52. $('.selected').next(':not(.highlight)').trigger('click');
  53. }
  54. }, speed);
  55.  
  56. }
  57.  
  58. $('.news-content').on('mouseover', function () {
  59. clearInterval(myTimer);
  60. });
  61.  
  62. $('.news-content').on('mouseout', function () {
  63. doTimedSwitch();
  64. });
  65.  
  66. function doClickItem() {
  67.  
  68. newsListItems.on('click', function () {
  69.  
  70. newsListItems.removeClass('selected');
  71. $(this).addClass('selected');
  72.  
  73. siblings = $(this).prevAll();
  74. totalHeight = 0;
  75.  
  76. // this loop calculates the height of individual elements, including margins/padding
  77. for (i = 0; i < siblings.length; i += 1) {
  78. totalHeight += $(siblings[i]).height();
  79. totalHeight += vPadding;
  80. totalHeight += vMargin;
  81. }
  82.  
  83. // this moves the highlight vertically the distance calculated in the previous loop
  84. // and also corrects the height of the highlight to match the current selection
  85. hl.css({
  86. top: totalHeight,
  87. height: $(this).height() + vPadding
  88. });
  89.  
  90. indexEl = $(this).index() + 1;
  91.  
  92. $('.news-content:nth-child(' + indexEl + ')').siblings().removeClass('top-content');
  93. $('.news-content:nth-child(' + indexEl + ')').addClass('top-content');
  94.  
  95. clearInterval(myTimer);
  96. // comment out the line below if you don't
  97. // want it to rotate automatically
  98. doTimedSwitch();
  99. doEqualHeight(indexEl);
  100. });
  101.  
  102. }
  103.  
  104. function doWindowResize() {
  105.  
  106. $(window).resize(function () {
  107.  
  108. clearInterval(myTimer);
  109. // click is triggered to recalculate and fix the highlight position
  110. $('.selected').trigger('click');
  111.  
  112. });
  113.  
  114. }
  115.  
  116. // this is the poor man's 'init' section
  117. doClickItem();
  118. doWindowResize();
  119. $('.selected').trigger('click');
  120.  
  121. });
Add Comment
Please, Sign In to add comment