简体   繁体   English

仅在移动设备上使用角度ui bootstrap轮播

[英]Use angular ui bootstrap carousel only on mobile devices

Is there any way to apply the bootstrap carousel only conditionally for mobile devices and use it's content as static html for desktop? 有没有办法只为移动设备有条件地应用bootstrap轮播,并将其内容用作桌面的静态html?

I've the specification to have no duplicated html to display the same content differently for mobile and desktop (defined by the respective bootstrap breakpoint definitions and the devices width). 我的规范没有重复的html来为移动和桌面显示相同的内容(由相应的bootstrap断点定义和设备宽度定义)。 That's why I can't just use two different containers for the content. 这就是为什么我不能只为内容使用两个不同的容器。 In the design there is a carousel given for mobile whose content should be displayed as static html for desktop. 在设计中有一个为移动设备提供的轮播,其内容应显示为桌面的静态html。

UI-Bootstrap doesn't handle conditionally showing ui components, but plain ol' angular do, you can write a directive that watches for screen size and resize events and updates a value accordingly, then use an ng-if with that value. UI-Bootstrap不处理有条件地显示ui组件,但是简单的'angular do,你可以编写一个指令来监视屏幕大小并调整事件大小并相应地更新一个值,然后使用带有该值的ng-if

  1. bind a value to the resize event and run $apply() when width changes 将值绑定到resize事件并在宽度更改时运行$apply()

  2. attach a watch to $window.innerWidth and listen to it's values, when apply will run the watch will test if the width is bigger or smaller than 768 px. 将手表附加到$window.innerWidth并听取它的值,当应用将运行时,手表将测试宽度是大于还是小于768像素。

     // inside directive app.directive('viewPortWatcher', function($window) { return { restrict: "AE", scope: { show: "=" }, link: function(scope, element) { var width = angular.element($window); width.bind('resize', function() { scope.$apply(); }); scope.$watch(function() { return $window.innerWidth; }, function(value) { if (value <= 768) { // sm scope.show = true; } else { scope.show = false; } }); } } } 
  3. in your html 在你的HTML中

     <view-port-watcher show="show"></view-port-watcher> <span ng-if="show"> show carousel </span> <span ng-if="!show"> don't show carousel </span> 

plunker plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM