/** * A responsive slider jQuery plugin with CSS animations * * @copyright Copyright 2013-2015 Joan claret * @license MIT * @author Joan Claret Teruel * * Licensed under The MIT License (MIT). * Copyright (c) Joan Claret Teruel * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ ; (function ($, document, window, undefined) { 'use strict'; const animatInList = ["bounceInRight", "bounceInUp", "fadeIn", "fadeInDown", "fadeInDownBig", "fadeInLeft", "fadeInLeftBig", "fadeInRight", "fadeInRightBig", "fadeInUp", "fadeInUpBig", "flipInX", "flipInY", "lightSpeedIn", "rotateIn", "rotateInDownLeft", "rotateInDownRight", "rotateInUpLeft", "rotateInUpRight", "rollIn", "zoomIn", "zoomInDown", "zoomInLeft", "zoomInRight", "zoomInUp", "slideInDown", "slideInLeft", "slideInRight", "slideInUp",]; const animatOutList = ["bounceOutRight", "bounceOutUp", "bounceOut", "bounceOutDown", "bounceOutLeft", "fadeOut", "fadeOutDown", "fadeOutDownBig", "fadeOutLeft", "fadeOutLeftBig", "fadeOutRight", "fadeOutRightBig", "fadeOutUp", "fadeOutUpBig", "flipOutX", "flipOutY", "lightSpeedOut", "rotateOut", "rotateOutDownLeft", "rotateOutDownRight", "rotateOutUpLeft", "rotateOutUpRight", "hinge", "rollOut", "zoomOut", "zoomOutDown", "zoomOutLeft", "zoomOutRight", "zoomOutUp", "slideOutDown", "slideOutLeft", "slideOutRight", "slideOutUp"]; var jcSlider = $.fn.jcSlider = function (options) { var $this = $(this); $this.css({ 'overflow': 'hidden' }); var animationItem = $this.find('.jc-animation'), animationItemsLength = animationItem.length, animationCurrentItem = Math.floor(Math.random() * animationItemsLength), jcSliderInterval = null; // hide all items excepting animationCurrentItem $this.find('.jc-animation:not(:nth-of-type(' + (animationCurrentItem + 1) + '))').hide(); // get settings var settings = $.extend({ // default settings animationIn: 'bounceInRight', animationOut: 'bounceOutLeft', stopOnHover: true, loop: true }, options); // Detect when animations (keyframes) end function whichAnimationEvent() { var t, el = document.createElement('fakeelement'); var animations = { 'animation': 'animationend', 'OAnimation': 'oAnimationEnd', 'MozAnimation': 'animationend', 'WebkitAnimation': 'webkitAnimationEnd' }; for (t in animations) { if (el.style[t] !== undefined) { return animations[t]; } } } var animationEvent = whichAnimationEvent(); var lastAnimationIn = ""; var lastAnimationOut = ""; var changeImage = function () { lastAnimationIn = 'animated ' + settings.animationIn; lastAnimationOut = 'animated ' + settings.animationOut; const randNum = Math.floor(Math.random() * animatInList.length); settings.animationIn = animatInList[randNum]; const randNum2 = Math.floor(Math.random() * animatOutList.length); settings.animationOut = animatOutList[randNum2]; var animateOut = 'animated ' + settings.animationOut, animateIn = 'animated ' + settings.animationIn; // stop animation if loop is false and we are on the last image if (settings.loop === false && animationCurrentItem === (animationItemsLength - 2)) { clearInterval(jcSliderInterval); } animationItem.eq(animationCurrentItem) .removeClass(lastAnimationIn) // reset enter animation .addClass(lastAnimationOut) // exit animation // when exit animation is finished, move next item .one(animationEvent, function () { // move current item animationItem.eq(animationCurrentItem) .removeClass(lastAnimationOut) // reset exit animation .hide(); // hide // select next item animationCurrentItem = Math.floor(Math.random() * animationItemsLength); if (animationCurrentItem === animationItemsLength) { animationCurrentItem = 0; } // move next item animationItem.eq(animationCurrentItem) .show() // show .addClass(animateIn); // next item animation }); } // main function var jcSliderAnimation = function () { jcSliderInterval = setInterval(changeImage, 1000 * 60 * 15); }; // Initialise the animation function jcSliderAnimation(); window.onkeydown = function (event) { var e = event || window.event || arguments.callee.caller.arguments[0]; console.log(e) if (e && e.keyCode === 34) { // 按下了pageDown clearInterval(jcSliderInterval) changeImage() jcSliderAnimation() } } if (settings.stopOnHover === true) { // Stop the animation on hover $this.hover( function () { clearInterval(jcSliderInterval); }, function () { jcSliderAnimation(); }); } } ; /* * Export as a CommonJS module */ if (typeof module !== 'undefined' && module.exports) { module.exports = jcSlider; } })(window.jQuery || window.Zepto || window.$, document, window); // Pending Zepto support