javascript - AngularJS 中带有控件的响应式幻灯片放映

标签 javascript css angularjs slideshow

<分区>

offtopic:抱歉我的英语不好。 ontopic:我正在尝试使用 AngularJS 中的控件进行响应式幻灯片放映(我也是 Angular 方面的新手)并且我被困在幻灯片控件的一部分,我得到了这个:

应用程序.js

angular.module('slider', [])
  .controller('sliderCtrl', ['$scope', function($scope) {
    var thumbs = $scope.thumbs = [];
    for(i=1;i<=11;i++) {
      thumbs.push({
        image: 'http://lorempixel.com/500/300/sports/' + i
      })
    }
  }])
  .service('thumbService', function() {
    this.resize = function(elem, scope) {
      elem.style.width = scope.containerWidth + 'px';
    }
  })
  .directive('carousel', ['$timeout', '$window', function($timeout, $window) {
    return {
      restrict: 'EA',
      replace: true,
      transclude: true,
      controller: 'sliderCtrl',
      templateUrl: 'carousel.html',
      link: function(scope, elem) {
        angular.element($window).on('load resize', function() {
          $timeout(function() {
            scope.containerWidth = elem[0].offsetWidth * 0.25;
          }, 200);
        });
      }
    }
  }])
  .directive('thumbs', ['$window', 'thumbService', function($window, thumbService) {
    return {
      restrict: 'EA',
      replace: true,
      templateUrl: 'thumbs.html',
      require: '^carousel',
      transclude: true,
      link: function(scope, elem) {
        thumbService.resize(elem[0], scope);
        scope.$watch('containerWidth', function() {
          thumbService.resize(elem[0], scope);
        });
      }
    };
  }])
  .directive('controls', [function() {
    return {
      restrict: 'E',
      replace: true,
      require: '^carousel',
      templateUrl: 'controls.html',
      link: function(scope, elem, attrs) {
        scope.goPrev = function() {
          // TODO
        }
        scope.goNext = function() {
          // TODO
        }
      }
    }
  }])
  .run(function($templateCache) {
    $templateCache.put('thumbs.html', '<div class="slider-gallery_item" ng-transclude></div>');
    $templateCache.put('carousel.html',
      '<div id="slider" class="slider-wrapper">' +
      ' <div class="slider-gallery" ng-transclude></div>' +
      ' <controls></controls>' +
      '</div>');
    $templateCache.put('controls.html',
      ' <div id="slider-controls"> ' +
      '   <div class="slider-gallery_prev" ng-click="goPrev()"><</div>' +
      '   <div class="slider-gallery_next" ng-click="goNext()">></div>' +
      ' </div>');
  })

样式.css

.slider-wrapper {
    margin: 0 auto;
    overflow: hidden;
    max-width: 100%;
}

.slider-gallery {
    transform: translate3d(0,0,0);
    width: 20000em;
    position: relative;
    top: 0;
}

.slider-gallery_item {
    width: 200px;
    float: left;
    padding: 0 10px;
}

.slider-gallery_item img {
    display: block;
    max-width: 100%;
    height: auto !important;
}

.slider-gallery {
    margin-top: 10px;
}

.slider-gallery_prev {
    cursor: pointer;
    float: left;
}

.slider-gallery_next {
    cursor: pointer;
    float: right;
}

你可以在http://plnkr.co/edit/25xt9qUvLaaxgYJQRUzJ?p=preview上看到它

如您所见,有点响应,但现在,我总共有 11 张图片,我该怎么做才能使下一个和上一个按钮起作用?不是技术问题,更多的是应用的方法,

谢谢!

最佳答案

在你的 Controller 上你可以声明两个方法,例如:

this.goNext = function(){
  $scope.thumbs.push($scope.thumbs.shift());
}

this.goPrev = function(){
  $scope.thumbs.unshift($scope.thumbs.pop());
}

然后您可以从指令的链接属性调用这些方法(不要忘记添加第 4 个参数 ctrl):

directive('controls', [function() {
    return {
      restrict: 'E',
      replace: true,
      require: '^carousel',
      templateUrl: 'controls.html',
      link: function(scope, elem, attrs, ctrl) {
        scope.goPrev = function() {
          ctrl.goPrev();
        }
        scope.goNext = function() {
          ctrl.goNext();
        }
      }
    }
  }])

关于javascript - AngularJS 中带有控件的响应式幻灯片放映,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26076333/

相关文章:

javascript - 将条件运算符与 ng-class 一起使用 - AngularJS

javascript - 绘制平行坐标以随机选择数据

javascript - 从 javascript webview 访问 iPhone GPS

html - 部分顶部和底部的波浪背景

html - 将元素扩展到 div 的宽度

javascript - MS Word 类似于 HTML 中的 DOM 操作

javascript - AngularJS:forEach http 获取数据 - 等待其他方法,直到加载循环中的所有数据

angularjs - 在 React 中使用自定义 Angular 指令

javascript - 如何从外部文件onload正确执行函数?

javascript - ReactJS 中的过滤器嵌套对象