Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Carousel JS
- (function () {
- "use strict";
- // ========== Page Loaded Activate ==========
- var carouselStartId;
- function carouselStart() {
- return setInterval(function () {
- var $activeImg = $("#carousel-2 .carousel_img picture.active-img"),
- $nextImg = $activeImg.next(),
- // Pagination Dots
- $activePagination = $("#carousel-2 .carousel_pagination.active-img"),
- $nextPagination = $activePagination.next()
- // Logic
- if ($activeImg.is(":last-child")) {
- $nextImg = $activeImg.siblings().first();
- $nextPagination = $activePagination.siblings().first();
- } else {
- $nextImg = $activeImg.next();
- $nextPagination = $activePagination.next();
- }
- // Animation: Image
- TweenMax.fromTo($activeImg, 1, { xPercent: 0, scale: 1 },
- { xPercent: -10, scale: 0.9, display: "none", zIndex: 1, ease: Power2.easeInOut });
- TweenMax.fromTo($nextImg, 1, { xPercent: 100, scale: 1, zIndex: 9, display: "none" },
- { xPercent: 0, scale: 1, display: "block", zIndex: 9, ease: Power2.easeInOut });
- // Image Active
- $activeImg.removeClass("active-img");
- $nextImg.addClass("active-img");
- // Pagination Active
- $activePagination.removeClass("active-img");
- $nextPagination.addClass("active-img");
- }, 4000);
- }
- // ===== Carousel Pause =====
- function carouselPause() {
- clearInterval(carouselStartId);
- TweenMax.to($(".carouselPause"), 0.2, { x: 20, opacity: 0, display: "none", scale: 0.3, ease: Power2.easeInOut });
- TweenMax.fromTo($(".carouselPlay"), 0.2, { x: -20, opacity: 0, scale: 0.3, display: "none" },
- { x: 0, opacity: 1, display: "block", scale: 1, ease: Power2.easeInOut });
- }
- // ===== Carousel Play =====
- function carouselPlay() {
- clearInterval(carouselStartId);
- carouselStartId = carouselStart();
- TweenMax.to($(".carouselPlay"), 0.2, { x: 20, opacity: 0, scale: 0.3, display: "none", ease: Power2.easeInOut });
- TweenMax.fromTo($(".carouselPause"), 0.2, { x: -20, opacity: 0, scale: 0.3, display: "none" },
- { x: 0, opacity: 1, scale: 1, display: "block", ease: Power2.easeInOut });
- }
- // ===== Activate Carousel when DOM ready =====
- $(document).ready(carouselStartId = carouselStart());
- window.onfocus = function () {
- if (!carouselStartId && $(".carouselPause").css("display") == "block") { // check if carouselId is null(preventing Safari from calling onfocus event twice) && carousel is playing
- carouselStartId = carouselStart();
- }
- };
- window.onblur = function () {
- clearInterval(carouselStartId);
- carouselStartId = null; // clear the carouselId
- };
- // Pause Button Click
- $(".carouselPause").click(function () {
- carouselPause();
- });
- // Play Button Click
- $(".carouselPlay").click(function () {
- carouselPlay();
- });
- var carouselArrowFired = false;
- // ===== Next Arrow =====
- $("#carousel-2 .carousel_arrow-next").click(function () {
- if (!carouselArrowFired) {
- carouselArrowFired = true;
- if ($(".carouselPause").css("display") === "block") {
- // Clear carouselStart window object then call again
- clearInterval(carouselStartId);
- carouselStartId = carouselStart();
- } else {
- clearInterval(carouselStartId);
- }
- // Carousel Images
- var $activeImg = $("#carousel-2 .carousel_img .active-img"),
- $nextImg = $activeImg.next()
- // Pagination Dots
- $activePagination = $("#carousel-2 .carousel_pagination.active-img"),
- $nextPagination = $activePagination.next()
- // Logic
- if ($activeImg.is(":last-child")) {
- $nextImg = $activeImg.siblings().first();
- $nextPagination = $activePagination.siblings().first();
- } else {
- $nextImg = $activeImg.next();
- $nextPagination = $activePagination.next();
- }
- // Animation: Image
- TweenMax.fromTo($activeImg, 1, { },
- { xPercent: -10, scale: 0.9, display: "none", zIndex: 1, ease: Power2.easeInOut });
- TweenMax.fromTo($nextImg, 1, { xPercent: 100, scale: 1, zIndex: 9, display: "none" },
- { xPercent: 0, scale: 1, display: "block", zIndex: 9, ease: Power2.easeInOut });
- // Prevent click event get firing multiple times within 700ms
- setTimeout(function () {
- carouselArrowFired = false;
- }, 700);
- // Image Active
- $activeImg.removeClass("active-img");
- $nextImg.addClass("active-img");
- // Pagination Active
- $activePagination.removeClass("active-img");
- $nextPagination.addClass("active-img");
- }
- });
- // ===== Previous Arrow =====
- $("#carousel-2 .carousel_arrow-prev").click(function () {
- if (!carouselArrowFired) {
- carouselArrowFired = true;
- if ($(".carouselPause").css("display") == "block") {
- // Clear carouselStart window object then call again
- clearInterval(carouselStartId);
- carouselStartId = carouselStart();
- } else {
- clearInterval(carouselStartId);
- }
- // Carousel Images
- var $activeImg = $("#carousel-2 .carousel_img .active-img"),
- $prevImg = $activeImg.prev()
- // Pagination Dots
- $activePagination = $("#carousel-2 .carousel_pagination.active-img"),
- $prevPagination = $activePagination.prev()
- // Logic
- if ($activeImg.is(":first-child")) {
- $prevImg = $activeImg.siblings().last();
- $prevPagination = $activePagination.siblings("a").last();
- } else {
- $prevImg = $activeImg.prev();
- }
- // Animation
- TweenMax.fromTo($activeImg, 1, { xPercent: 0, scale: 1, zIndex: 9 },
- { xPercent: 100, scale: 1, display: "none", ease: Power2.easeInOut });
- TweenMax.fromTo($prevImg, 1, { xPercent: -10, scale: 0.9, zIndex: 1, display: "none" },
- { xPercent: 0, scale: 1, display: "block", zIndex: 9, ease: Power2.easeInOut });
- // Prevent click event get firing multiple times within 700ms
- setTimeout(function () {
- carouselArrowFired = false;
- }, 700);
- // Image Active
- $activeImg.removeClass("active-img"),
- $prevImg.addClass("active-img")
- // Pagination Active
- $activePagination.removeClass("active-img"),
- $prevPagination.addClass("active-img")
- }
- });
- // ========== Pagination Click Events ==========
- $("#carousel-2 .carousel_pagination").click(function () {
- if ($(".carouselPause").css("display") == "block") {
- // Clear carouselStart window object then call again
- clearInterval(carouselStartId);
- carouselStartId = carouselStart();
- } else {
- clearInterval(carouselStartId);
- }
- var nextActiveIndex = $(this).attr("data-carousel-item"), // return an index
- currentIndex = $("#carousel-2 .carousel_pagination.active-img").attr("data-carousel-item");
- // Carousel Images
- var $activeImg = $("#carousel-2 .carousel_img .active-img"),
- $nextImg = $("#carousel-2 .carousel_img").find("picture[carousel-index='" + nextActiveIndex + "']"),
- // Pagination Dots
- $activePagination = $("#carousel-2 .carousel_pagination.active-img"),
- $nextPagination = $("#carousel-2 .carousel_controls").find("a[data-carousel-item='" + nextActiveIndex + "']");
- // Condition for animation forward or backward
- if (nextActiveIndex > currentIndex) {
- // Animation: Image
- TweenMax.fromTo($activeImg, 1, { xPercent: 0, scale: 1 },
- { xPercent: -10, scale: 0.9, display: "none", zIndex: 1, ease: Power2.easeInOut });
- TweenMax.fromTo($nextImg, 1, { xPercent: 100, scale: 1, zIndex: 9, display: "none" },
- { xPercent: 0, scale: 1, display: "block", zIndex: 9, ease: Power2.easeInOut });
- } else {
- // Animation
- TweenMax.fromTo($activeImg, 1, { xPercent: 0, scale: 1 },
- { xPercent: 100, scale: 1, display: "none", ease: Power2.easeInOut });
- TweenMax.fromTo($nextImg, 1, { xPercent: -10, scale: 0.9, zIndex: 1, display: "none" },
- { xPercent: 0, scale: 1, display: "block", zIndex: 9, ease: Power2.easeInOut });
- }
- // Image Active
- $activeImg.removeClass("active-img");
- $nextImg.addClass("active-img");
- // Pagination Active
- $activePagination.removeClass("active-img");
- $nextPagination.addClass("active-img");
- });
- // ========== Carousel Mobile JS ==========
- // Don't use jQuery selector in Hammer.js
- var container = document.getElementById("carousel-2");
- var el = document.getElementById("carousel-img");
- var paginationControl = document.getElementById("carousel-pagination");
- var imgActive,
- imgNext,
- imgPrev,
- paginationActive,
- paginationNext,
- paginationPrev,
- startX = 0,
- BREAK_POINT_RATIO = .55,
- BREAK_POINT = container.offsetWidth * BREAK_POINT_RATIO,
- swipeLeftTriggered,
- swipeRightTriggered,
- swipeLeftReset,
- swipeRightReset,
- TRANSLATE_RATIO = .8,
- transform = {
- translate: { x: startX },
- };
- var mc = new Hammer.Manager(el, { inputClass: Hammer.TouchInput }); // Mobile Input Only
- mc.add(new Hammer.Pan({
- direction: Hammer.DIRECTION_HORIZONTAL,
- threshold: 0,
- pointers: 0
- }));
- // ========== Pan Detection ==========
- mc.on("panstart", function () {
- // Stop carousel automation when panning
- if ($(".carouselPause").css("display") == "block") {
- carouselPause();
- }
- transform = {
- translate: { x: startX },
- };
- imgActive = el.getElementsByClassName("active-img")[0];
- paginationActive = paginationControl.getElementsByClassName("active-img")[0];
- imgNext = el.getElementsByClassName("active-img")[0].nextElementSibling;
- imgPrev = el.getElementsByClassName("active-img")[0].previousElementSibling;
- paginationNext = paginationControl.getElementsByClassName("active-img")[0].nextElementSibling;
- paginationPrev = paginationControl.getElementsByClassName("active-img")[0].previousElementSibling;
- if (imgActive == el.children[4]) {
- imgNext = el.firstElementChild;
- paginationNext = paginationControl.firstElementChild;
- } else if (imgActive == el.children[0]) {
- imgPrev = el.lastElementChild;
- paginationPrev = paginationControl.children[4];
- }
- });
- // ========== Pan Left (Go next) & Pan Right (Go previous) ==========
- mc.on("panleft panright", function (ev) {
- if (imgActive) {
- var panDistance = parseInt((startX + ev.deltaX) * TRANSLATE_RATIO);
- // Condition: Pannig Right
- if (panDistance > 0) {
- TweenLite.set(imgActive, {
- force3D: true,
- x: panDistance,
- zIndex: 9
- });
- TweenLite.set(imgPrev, {
- force3D: true,
- xPercent: Math.min((panDistance / BREAK_POINT) * 10 - 10, 0),
- display: "block",
- scale: Math.min((panDistance / BREAK_POINT) * 0.1 + 0.9, 1),
- zIndex: 1
- });
- } else if (panDistance < 0) { // Condition: Pannig Left
- TweenLite.set(imgActive, {
- force3D: true,
- xPercent: Math.max((BREAK_POINT - (-panDistance)) / BREAK_POINT * 10 + (-10), -10),
- scale: Math.max((BREAK_POINT - (-panDistance)) / BREAK_POINT * 0.1 + 0.9, 0.9),
- zIndex: 2
- });
- TweenLite.set(imgNext, {
- force3D: true,
- x: panDistance,
- display: "block",
- xPercent: 100,
- scale: 1,
- zIndex: 9
- });
- }
- // update the follow finger translate value
- if (ev.deltaX >= 0) {
- transform.translate = {
- x: startX + ev.deltaX
- };
- }
- swipeLeftTriggered = false;
- swipeLeftReset = false;
- swipeRightTriggered = false;
- swipeRightReset = false;
- // check the pan distance:
- if (panDistance <= (-BREAK_POINT)) {
- // user has swiped this object enough to trigger the event!
- swipeLeftTriggered = true;
- } else if (panDistance > (-BREAK_POINT) && panDistance < 0) {
- // reset
- swipeLeftReset = true;
- } else if (panDistance >= BREAK_POINT) {
- // user has swiped this object enough to trigger the event!
- swipeRightTriggered = true;
- } else if (panDistance < BREAK_POINT && panDistance > 0) {
- // reset
- swipeRightReset = true;
- }
- }
- });
- // ========== Pan Released ==========
- mc.on("panend pancancel", function () {
- if (swipeLeftTriggered) {
- swipeLeftFunc("enter");
- } else if (swipeLeftReset) {
- swipeLeftFunc();
- } else if (swipeRightTriggered) {
- swipeRightFunc("enter");
- } else if (swipeRightReset) {
- swipeRightFunc();
- }
- });
- // ========== Function: Swipt Left; Execute when releasing ==========
- function swipeLeftFunc(enter) {
- if (enter) {
- TweenLite.to(imgActive, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: -10,
- display: "none",
- scale: 0.9,
- zIndex: 1
- });
- TweenLite.to(imgNext, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: 0,
- display: "block",
- scale: 1,
- zIndex: 9
- });
- TweenLite.to(imgPrev, .3, {
- display: "none",
- });
- imgActive.classList.remove("active-img");
- imgNext.classList.add("active-img");
- paginationActive.classList.remove("active-img");
- paginationNext.classList.add("active-img");
- } else { // Reset position
- TweenLite.to(imgActive, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: 0,
- z: 0,
- zIndex: 2,
- scale: 1
- });
- TweenLite.to(imgNext, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: 100,
- display: "none",
- scale: 1,
- zIndex: 9
- });
- }
- }
- // ========== Function: Swipt Right; Execute when releasing ==========
- function swipeRightFunc(enter) {
- if (enter) {
- TweenLite.to(imgActive, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- z: 0,
- xPercent: 100,
- display: "none",
- scale: 1,
- zIndex: 9
- });
- TweenLite.to(imgPrev, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- z: 0,
- xPercent: 0,
- display: "block",
- scale: 1,
- zIndex: 1
- });
- TweenLite.to(imgNext, .3, {
- display: "none",
- });
- imgActive.classList.remove("active-img");
- imgPrev.classList.add("active-img");
- paginationActive.classList.remove("active-img");
- paginationPrev.classList.add("active-img");
- } else { // Reset position
- TweenLite.to(imgActive, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: 0,
- zIndex: 9,
- scale: 1,
- display: "block",
- });
- TweenLite.to(imgPrev, .3, {
- force3D: true,
- ease: Cubic.easeInOut,
- x: 0,
- xPercent: -20,
- display: "none",
- scale: 0.9,
- zIndex: 1,
- });
- }
- } // End of Carousel Mobile JS
- })(); // End of Carousel JS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement