LinkedNestBlog

[AngularJS] Auto Slide Show

by jinwon lee

Auto Slide Show For Angular JS 

 

Ref.] 

1. http://www.sitepoint.com/creating-slide-show-plugin-angularjs/

2. Demo Site : http://extremecss.com/demos/slider/

 

Need To : 

Angular JS 로 웹사이트 대문의 이미지들을 슬라이드 쇼 형태로 나타낸 화면에 자동 슬라이드 쇼를 적용하고 싶었다.

 

Solve To : 

아래와 같이 timeout을 이용, 자동 슬라이드 쇼를 적용.

 

Source : 

1. js (빨간색 영역이 자동 슬라이드를 위해 추가한 코드) 

 (function(){

 

var app = angular.module('website', ['ngAnimate', 'ngTouch']);

 

 

 

    app.controller('MainCtrl', function ($scope, $timeout) {

        

    $scope.slides = [

                     {image: '/img/slideshow/img00.jpg', description: 'Image 00'},

                     {image: '/img/slideshow/img01.jpg', description: 'Image 01'},

                     {image: '/img/slideshow/img02.jpg', description: 'Image 02'},

                     {image: '/img/slideshow/img03.jpg', description: 'Image 03'},

                     {image: '/img/slideshow/img04.jpg', description: 'Image 04'}

                 ];

 

        $scope.direction = 'left';

        $scope.currentIndex = 0;

 

        $scope.setCurrentSlideIndex = function (index) {

            $scope.direction = (index > $scope.currentIndex) ? 'left' : 'right';

            $scope.currentIndex = index;

        };

 

        $scope.isCurrentSlideIndex = function (index) {

            return $scope.currentIndex === index;

        };

 

        $scope.prevSlide = function () {

            $scope.direction = 'left';

            $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;

        };

 

        $scope.nextSlide = function () {

            $scope.direction = 'right';

            $scope.currentIndex = ($scope.currentIndex > 0) ? --$scope.currentIndex : $scope.slides.length - 1;

        };

        

/* Start: auto slide show */

 

var timer;

 

var sliderFunc=function(){

timer=$timeout(function(){

$scope.nextSlide();

timer=$timeout(sliderFunc,5000);

},5000);

};

 

sliderFunc();

 

$scope.$on('$destroy',function(){

$timeout.cancel(timer);

});

 

/* End : auto slide show */        

    })

    .animation('.slide-animation', function () {

        return {

            beforeAddClass: function (element, className, done) {

                var scope = element.scope();

 

                if (className == 'ng-hide') {

                    var finishPoint = element.parent().width();

                    if(scope.direction !== 'right') {

                        finishPoint = -finishPoint;

                    }

                    TweenMax.to(element, 0.5, {left: finishPoint, onComplete: done });

                }

                else {

                    done();

                }

            },

            removeClass: function (element, className, done) {

                var scope = element.scope();

 

                if (className == 'ng-hide') {

                    element.removeClass('ng-hide');

 

                    var startPoint = element.parent().width();

                    if(scope.direction === 'right') {

                        startPoint = -startPoint;

                    }

 

                    TweenMax.fromTo(element, 0.5, { left: startPoint }, {left: 0, onComplete: done });

                }

                else {

                    done();

                }

            }

        };

    });

 

})();

2. css

 body {

    overflow: hidden;

    background-color: white;

}

 

.slider {

    width: 800px;

    height: 550px;

    overflow: hidden;

    position: relative;

    background: #e6e6e6;

    border: 20pxsolid#FFF;

    margin-top: 20px;

}

 

.slide {

    position: absolute;

    top: 0;

    left: 0;

}

 

.arrow {

    position: absolute;

    z-index: 1002;

    display: block;

    top: 50%;

    margin-top: -35px;

    width: 36px;

    height: 71px;

    outline: none;

    cursor: pointer;

    background: url(../img/slideshow/btn/blank.gif)no-repeat;

}

 

.arrow.prev {

    opacity: 0.2;

    left: 20px;

    background-image: url(../img/slideshow/btn/paddle_prev_999.png);

    transition: 0.2slinearall;

}

 

.arrow.next {

    opacity: 0.2;

    right: 20px;

    background-image: url(../img/slideshow/btn/paddle_next_999.png);

    transition: 0.2slinearall;

}

 

.arrow.prev:hover {

    opacity: 1;

    background-image: url(../img/slideshow/btn/paddle_prev_fff.png);

}

 

.arrow.next:hover {

    opacity: 1;

    background-image: url(../img/slideshow/btn/paddle_next_fff.png);

}

 

 

 

/*---------------------------------------------------------

NAV

---------------------------------------------------------*/

 

.nav-slide {

    text-align: center;

    display: block;

    position: absolute;

    z-index: 1002;

    left: 0;

    bottom: -4px;

    right: 0;

    height: 48px;

}

 

.nav-slide .wrapper {

    margin: 0 auto;

    width: 100%;

    padding: 1em 0 .8em;

}

 

.nav-slide ul {

    margin: 0;

    width: 100%;

}

 

.nav-slide .dot,.nav-slide .dot a {

    display: inline-block;

    zoom: 1;

}

 

.dots .dot {

    position: relative;

    margin: 0 8px;

    width: 12px;

    height: 12px;

}

 

.dots .dot a {

    position: absolute;

    top: 2px;

    left: 2px;

    width: 6px;

    height: 6px;

    text-indent: 100%;

    white-space: nowrap;

    overflow: hidden;

    background: #FFF;

    border: 1pxsolidtransparent;

    outline: none;

    -webkit-box-shadow: none;

    -moz-box-shadow: none;

    box-shadow: none;

    -webkit-border-radius: 50%;

    -moz-border-radius: 50%;

    border-radius: 50%;

    -webkit-transition: background-color0.25s,border-color0.25s;

    -moz-transition: background-color0.25s,border-color0.25s;

    transition: background-color0.25s,border-color0.25s;

}

 

.dots .dot a.active {

    border-color: #FFF;

    background-color: transparent;

}

3. jsp

 <!DOCTYPEhtml>

<htmlng-app="website">

<head>

    <metacharset="utf-8">

    <title>AngularJS Animate Slider</title>

 

 

    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/slideshow.css">

</head>

 

<bodyng-controller="MainCtrl">

 

<divclass="container slider">

    <imgng-repeat="slide in slides"class="slide slide-animation nonDraggableImage"

         ng-swipe-right="nextSlide()"ng-swipe-left="prevSlide()"

         ng-hide="!isCurrentSlideIndex($index)"ng-src="{{slide.image}}">

 

    <aclass="arrow prev"href="#"ng-click="nextSlide()"></a>

    <aclass="arrow next"href="#"ng-click="prevSlide()"></a>

    <navclass="nav-slide">

        <div class="wrapper">

            <ul class="dots">

                <liclass="dot"ng-repeat="slide in slides">

                    <ahref="#"ng-class="{'active':isCurrentSlideIndex($index)}"

                       ng-click="setCurrentSlideIndex($index);">{{slide.description}}</a></li>

            </ul>

        </div>

    </nav>

</div>

 

<scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>

<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular-animate.min.js"></script>

<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular-touch.min.js"></script>

<scriptsrc="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.10.3/TweenMax.min.js"></script>

 

<scripttype="text/javascript"src="${pageContext.request.contextPath}/js/angular/slide/slideshow.js"></script>

 

</body>

</html>

 

[AngularJS] Auto Slide Show · LinkedNest Blog