|
| 1 | +const SCROLL_ANIMATION_DURATION = 1000; |
| 2 | +const CAROUSEL_SCROLL_DOWN_BUTTON_OFFSET = {X: -5, Y: -25}; |
| 3 | + |
1 | 4 | $(document).ready(() => { |
2 | | - if ($('#slider').length) { |
3 | | - $('#slider').slick({ |
| 5 | + let slider = $("#slider"); |
| 6 | + if(slider.length === 0) return; |
| 7 | + if (slider.length) { |
| 8 | + slider.slick({ |
4 | 9 | dots: true, |
5 | 10 | autoplay: true, |
6 | 11 | autoplaySpeed: 5000, |
7 | 12 | mobileFirst: true, |
8 | 13 | swipe: true, |
9 | 14 | }); |
10 | 15 | } |
| 16 | + // Setup carousel scroll-down button click event |
| 17 | + let carousel_scroll_down_button = $("#carousel-scroll-down-button"); |
| 18 | + if(carousel_scroll_down_button.length === 0) return; |
| 19 | + let scrollTopOffset = 1; |
| 20 | + carousel_scroll_down_button.on("click", () => { |
| 21 | + let calculateFinalPosition = () => slider.find("img").height() + scrollTopOffset - $("nav").outerHeight(); |
| 22 | + let animateProperties = { scrollTop: calculateFinalPosition() } |
| 23 | + $("html, body").animate(animateProperties, {duration: SCROLL_ANIMATION_DURATION, specialEasing: {scrollTop: "easeOutCubic"}, step: function(now, fx) {fx.end = calculateFinalPosition();}} ); |
| 24 | + carousel_scroll_down_button.blur(); |
| 25 | + }); |
| 26 | + |
| 27 | + // Carousel arrow positioning function |
| 28 | + let recalculate_arrow_position = () => { |
| 29 | + let sliderBottom = slider.height() + slider.offset().top; |
| 30 | + let windowBottom = $(window).scrollTop() + $(window).height(); |
| 31 | + if ($(window).scrollTop() > sliderBottom) { |
| 32 | + carousel_scroll_down_button.css("visibility", "hidden"); |
| 33 | + } else { |
| 34 | + if(windowBottom < sliderBottom){ |
| 35 | + carousel_scroll_down_button.css("transform", `translate3d(${-carousel_scroll_down_button.width()/2 + CAROUSEL_SCROLL_DOWN_BUTTON_OFFSET.X}px, ${windowBottom-carousel_scroll_down_button.height() + CAROUSEL_SCROLL_DOWN_BUTTON_OFFSET.Y}px,0px)`); |
| 36 | + } else{ |
| 37 | + carousel_scroll_down_button.css("transform", `translate3d(${-carousel_scroll_down_button.width()/2 + CAROUSEL_SCROLL_DOWN_BUTTON_OFFSET.X}px, ${slider.height()-carousel_scroll_down_button.height() + CAROUSEL_SCROLL_DOWN_BUTTON_OFFSET.Y}px, 0px)`); |
| 38 | + } |
| 39 | + carousel_scroll_down_button.css("visibility", "visible"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Position carousel scroll-down button |
| 44 | + $(window).on("scroll load resize focus blur", recalculate_arrow_position); |
| 45 | + // Set interval (necessary for ensuring positioning when resize events don't trigger properly) |
| 46 | + $(window).on("resize", () => { |
| 47 | + const maxLoops = 10; |
| 48 | + let loops = 0; |
| 49 | + let tempInterval = setInterval(() => { |
| 50 | + if(loops >= maxLoops) clearInterval(tempInterval); |
| 51 | + recalculate_arrow_position(); |
| 52 | + loops++; |
| 53 | + }, 100); |
| 54 | + }); |
| 55 | + recalculate_arrow_position(); |
11 | 56 | }); |
0 commit comments