【问题标题】:How to swipe from left to right Ionic list item?如何从左向右滑动 Ionic 列表项?
【发布时间】:2018-01-03 21:14:13
【问题描述】:

我想向两边滑动 Ionic 列表项。 (即左右和左右)。它非常适合左右滑动,但我无法将列表项滑动到左侧。

我使用$ionicGesture 进行左右滑动,当我使用swiperight 事件时它也会给我一个警报:event($ionicGesture.on('swiperight', scope.reportEvent, elem)),但我不能让它在左侧显示ion-option-button .

这是我的指令和控制器代码:

.directive('onSwipeRight', function($ionicGesture) {
  return {
    restrict :  'A',
    link : function(scope, elem, attrs) {
      var gestureType = attrs.gestureType;
      switch(gestureType) {
        case 'swipeRight':
          $ionicGesture.on('swiperight', scope.reportEvent, elem);
          break;
        case 'swipeleft':
          $ionicGesture.on('swipeleft', scope.reportEvent, elem);
          break;
        case 'doubletap':
          $ionicGesture.on('doubletap', scope.reportEvent, elem);
          break;
        case 'tap':
          $ionicGesture.on('tap', scope.reportEvent, elem);
          break;
      }

    }
  }
})

.controller('ChatsCtrl', function($scope, Chats) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  }

  $scope.reportEvent = function (event) {
    alert("hi");
    console.log('Reporting : ' + event.type);
    event.preventDefault();
};

})

这是我的 html 代码。

<ion-view view-title="Chats">
    <ion-content>
        <ion-list can-swipe="true">
            <ion-item gesture-type="swipeRight" on-swipe-right="swipeRight()" class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}">

                <img ng-src="{{chat.face}}">
                <h2>{{chat.name}}</h2>
                <p>{{chat.lastText}}</p>
                <i class="icon ion-chevron-right icon-accessory"></i>
                <ion-option-button class="button-assertive" ng-click="share(item)" side="left">
                    Share
                </ion-option-button>
                <ion-option-button class="button-assertive" ng-click="remove(chat)" side="right">
                    Delete
                </ion-option-button>
            </ion-item>
        </ion-list>
    </ion-content>
</ion-view>

所以我想在左侧显示分享按钮,在右侧显示删除按钮。

谁能给我具体的解决方案?

【问题讨论】:

  • 你见过这个吗?您可以只使用 ionic 已经构建的指令,或者您可以编辑那里的指令,可能更容易构建您自己的指令。 ionicframework.com/docs/api/directive/ionList
  • 你能提供工作的codepen吗??
  • side-attribute on &lt;ion-option-button&gt; 是你自己的指令吗?我在文档中找不到。 ion-option-button 上的code comment 声明:“在列表项内创建一个选项按钮,当用户向左滑动该项时可见。”所以我想这是你的定制,对吧?你能把那个代码也加进去吗?

标签: angularjs ionic-framework


【解决方案1】:

我已经编辑了 ionic lib 来做类似的事情。但是我不能做 JSFiddle 或 Code Pen 我会给你我修改后的 ionic.css 和 ionic.bundle.js 的链接!

TL;DR

https://gist.githubusercontent.com/booris/847f044d2ef2a05101ce/raw/2274365384f5eed3e4538b269f3a7d7998eb22ed/ionic.css

https://gist.githubusercontent.com/booris/847f044d2ef2a05101ce/raw/2274365384f5eed3e4538b269f3a7d7998eb22ed/ionic.bundle.js

只需用你的替换它,开始一个离子项目空白。并将此 HTML 放入其中:

 <body ng-app="starter">
       <ion-pane>
            <ion-header-bar class="bar-stable">
                <h1 class="title">Ionic Blank Starter</h1>
            </ion-header-bar>
            <ion-content>
                <ion-list show-delete="false" can-swipe="true" swipe-direction="both">
                    <ion-item href="#">
                        Item 1
                        <ion-option-button side="right" class="button-light icon ion-heart"></ion-option-button>
                        <ion-option-button side="right" class="button-light icon ion-email"></ion-option-button>
                        <ion-option-button side="left" class="button-assertive icon ion-trash-a"></ion-option-button>
                    </ion-item>
                    <ion-item href="#">
                        Item 2
                        <ion-option-button class="button-light icon ion-heart"></ion-option-button>
                        <ion-option-button class="button-light icon ion-email"></ion-option-button>
                        <ion-option-button class="button-assertive icon ion-trash-a"></ion-option-button>
                    </ion-item>
                </ion-list>
            </ion-content>
        </ion-pane>
  </body>

您可以使用左、右或两者来指定擦除方向。 而在 ion-options-button 中,你可以给它一个侧面。

希望对您有所帮助,有什么需要就问吧!稍后我会尝试在代码中注释我的更改!

编辑: 我会试着解释我做了什么。

首先将要创建的ionOptionButton指令更改为按钮的div,一左一右

 //added second div with class item-options-left for the left buttons

var ITEM_TPL_OPTION_BUTTONS =
        '<div class="item-options invisible">' +
        '</div>' + '<div class="item-options-left invisible">' + 
        '</div>';
IonicModule.directive('ionOptionButton', [function () {
    function stopPropagation(e) {
        e.stopPropagation();
    }
    return {
        restrict: 'E',
        require: '^ionItem',
        priority: Number.MAX_VALUE,
        compile: function ($element, $attr) {
            $attr.$set('class', ($attr['class'] || '') + ' button', true);
            return function ($scope, $element, $attr, itemCtrl) {

                if (!itemCtrl.optionsContainer) {
                    itemCtrl.optionsContainer = jqLite(ITEM_TPL_OPTION_BUTTONS);
                    itemCtrl.$element.append(itemCtrl.optionsContainer);
                }

                //[NEW] if it as an attribute side = 'left' put the button in the left container
                if ($attr.side === 'left') {
                    angular.element(itemCtrl.optionsContainer[1]).append($element);
                    itemCtrl.$element.addClass('item-left-editable');
                } else{
                    angular.element(itemCtrl.optionsContainer[0]).append($element);
                    itemCtrl.$element.addClass('item-right-editable');
                }

                //Don't bubble click up to main .item
                $element.on('click', stopPropagation);
            };
        }
    };
}]);

将 CSS 添加到 ionic.css 文件中的左侧按钮

.item-options-left {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  height: 100%; }
.item-options-left .button {
  height: 100%;
  border: none;
  border-radius: 0;
  display: -webkit-inline-box;
  display: -webkit-inline-flex;
  display: -moz-inline-flex;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  -moz-align-items: center;
  align-items: center; }
.item-options .button:before {
  margin: 0 auto; }

现在更改 ion-list 控制器以接受滑动方向属性

.controller('$ionicList', [
  '$scope',
  '$attrs',
  '$ionicListDelegate',
  '$ionicHistory',
function ($scope, $attrs, $ionicListDelegate, $ionicHistory) {
            var self = this;

            //[NEW] object with can-swipe attr and swipe-direction side attr, default direction is left
            var swipe = {
                isSwipeable: true,
                side: 'left'
            };
            var isReorderShown = false;
            var isDeleteShown = false;

            var deregisterInstance = $ionicListDelegate._registerInstance(
                self, $attrs.delegateHandle,
                function () {
                    return $ionicHistory.isActiveScope($scope);
                }
            );
            $scope.$on('$destroy', deregisterInstance);

            self.showReorder = function (show) {
                if (arguments.length) {
                    isReorderShown = !!show;
                }
                return isReorderShown;
            };

            self.showDelete = function (show) {
                if (arguments.length) {
                    isDeleteShown = !!show;
                }
                return isDeleteShown;
            };

            //[NEW] get swipe direction attribute and store it in a variable to access in other function
            self.canSwipeItems = function (can) {
                if (arguments.length) {
                    swipe.isSwipeable = !!can;
                    swipe.side = $attrs.swipeDirection;
                }
                return swipe;
            };

            self.closeOptionButtons = function () {
                self.listView && self.listView.clearDragEffects();
            };
}]);

最后,你应该用这个替换slideDrag函数,只需在ionic.bundle.js中搜索它

//[NEW] add this var to the others in the function
var ITEM_OPTIONS_CLASS_RIGHT = 'item-options-left';

var SlideDrag = function (opts) {
        this.dragThresholdX = opts.dragThresholdX || 10;
        this.el = opts.el;
        this.item = opts.item;
        this.canSwipe = opts.canSwipe;
    };

    SlideDrag.prototype = new DragOp();

    SlideDrag.prototype.start = function (e) {
        var content, buttonsLeft, buttonsRight, offsetX, buttonsLeftWidth, buttonsRightWidth;

        if (!this.canSwipe().isSwipeable) {
            return;
        }

        if (e.target.classList.contains(ITEM_CONTENT_CLASS)) {
            content = e.target;
        } else if (e.target.classList.contains(ITEM_CLASS)) {
            content = e.target.querySelector('.' + ITEM_CONTENT_CLASS);
        } else {
            content = ionic.DomUtil.getParentWithClass(e.target, ITEM_CONTENT_CLASS);
        }

        // If we don't have a content area as one of our children (or ourselves), skip
        if (!content) {
            return;
        }

        // Make sure we aren't animating as we slide
        content.classList.remove(ITEM_SLIDING_CLASS);

        // Grab the starting X point for the item (for example, so we can tell whether it is open or closed to start)
        offsetX = parseFloat(content.style[ionic.CSS.TRANSFORM].replace('translate3d(', '').split(',')[0]) || 0;

        // Grab the buttons
        buttonsLeft = content.parentNode.querySelector('.' + ITEM_OPTIONS_CLASS);
        if (!buttonsLeft) {
            return;
        }

        //[NEW] get the Right buttons
        buttonsRight = content.parentNode.querySelector('.' + ITEM_OPTIONS_CLASS_RIGHT);
        if (!buttonsRight) {
            return;
        }

        // [NEW] added the same functionality to both sides, to make buttons visible when dragged
        if(e.gesture.direction === "left")
            buttonsLeft.classList.remove('invisible');
        else
            buttonsRight.classList.remove('invisible');

        //[NEW] added buttonRight and buttonLeft properties to currentDrag

        buttonsLeftWidth = buttonsLeft.offsetWidth;
        buttonsRightWidth = buttonsRight.offsetWidth;

        this._currentDrag = {
            buttonsLeft: buttonsLeft,
            buttonsRight: buttonsRight,
            buttonsLeftWidth: buttonsLeftWidth,
            buttonsRightWidth: buttonsRightWidth,
            content: content,
            startOffsetX: offsetX
        };
    };

    /**
     * Check if this is the same item that was previously dragged.
     */
    SlideDrag.prototype.isSameItem = function (op) {
        if (op._lastDrag && this._currentDrag) {
            return this._currentDrag.content == op._lastDrag.content;
        }
        return false;
    };

    SlideDrag.prototype.clean = function (isInstant) {
        var lastDrag = this._lastDrag;

        if (!lastDrag || !lastDrag.content) return;

        lastDrag.content.style[ionic.CSS.TRANSITION] = '';
        lastDrag.content.style[ionic.CSS.TRANSFORM] = '';
        if (isInstant) {
            lastDrag.content.style[ionic.CSS.TRANSITION] = 'none';
            makeInvisible();
            ionic.requestAnimationFrame(function () {
                lastDrag.content.style[ionic.CSS.TRANSITION] = '';
            });
        } else {
            ionic.requestAnimationFrame(function () {
                setTimeout(makeInvisible, 250);
            });
        }

        function makeInvisible() {
            lastDrag.buttonsLeft && lastDrag.buttonsLeft.classList.add('invisible');
            lastDrag.buttonsRight && lastDrag.buttonsRight.classList.add('invisible');
        }
    };

    SlideDrag.prototype.drag = ionic.animationFrameThrottle(function (e) {
        var buttonsLeftWidth;
        var buttonsRightWidth;

        // We really aren't dragging
        if (!this._currentDrag) {
            return;
        }

        // Check if we should start dragging. Check if we've dragged past the threshold,
        // or we are starting from the open state.
        if (!this._isDragging &&
            ((Math.abs(e.gesture.deltaX) > this.dragThresholdX) ||
                (Math.abs(this._currentDrag.startOffsetX) > 0))) {
            this._isDragging = true;
        }

        if (this._isDragging) {
            buttonsLeftWidth = this._currentDrag.buttonsLeftWidth;
            buttonsRightWidth = this._currentDrag.buttonsRightWidth;

            // Grab the new X point, capping it at zero
            //[NEW] added right swipe new position
            if (this.canSwipe().side === 'left' || (this.canSwipe().side === 'both' && e.gesture.direction === 'left'))
                var newX = Math.min(0, this._currentDrag.startOffsetX + e.gesture.deltaX);
            else if (this.canSwipe().side === 'right' || (this.canSwipe().side === 'both' && e.gesture.direction === 'right'))
                var newX = Math.max(0, this._currentDrag.startOffsetX + e.gesture.deltaX);

            var buttonsWidth = 0;
            if (e.gesture.direction === 'right')
                buttonsWidth = buttonsRightWidth;
            else
                buttonsWidth = buttonsLeftWidth;
            // If the new X position is past the buttons, we need to slow down the drag (rubber band style) 
            if (newX < -buttonsWidth) {
                // Calculate the new X position, capped at the top of the buttons
                newX = Math.min(-buttonsWidth, -buttonsWidth + (((e.gesture.deltaX + buttonsWidth) * 0.4)));
            }



            this._currentDrag.content.$$ionicOptionsOpen = newX !== 0;

            this._currentDrag.content.style[ionic.CSS.TRANSFORM] = 'translate3d(' + newX + 'px, 0, 0)';
            this._currentDrag.content.style[ionic.CSS.TRANSITION] = 'none';
        }
    });

    SlideDrag.prototype.end = function (e, doneCallback) {
        var self = this;

        // There is no drag, just end immediately
        if (!self._currentDrag) {
            doneCallback && doneCallback();
            return;
        }

        // If we are currently dragging, we want to snap back into place
        // The final resting point X will be the width of the exposed buttons
        var restingPoint;
        if (e.gesture.direction === 'left' && (this.canSwipe().side === 'left' || this.canSwipe().side === 'both'))
            restingPoint = -self._currentDrag.buttonsLeftWidth;
        if (e.gesture.direction === 'right' && (this.canSwipe().side === 'right' || this.canSwipe().side === 'both'))
            restingPoint = self._currentDrag.buttonsRightWidth;

        // Check if the drag didn't clear the buttons mid-point
        // and we aren't moving fast enough to swipe open
        var buttonsWidth = 0;
        if (e.gesture.direction === 'right') 
            buttonsWidth = self._currentDrag.buttonsRightWidth;
        else 
            buttonsWidth = self._currentDrag.buttonsLeftWidth;
        if (e.gesture.deltaX > -(buttonsWidth / 2)) {

            // If we are going left or right but too slow, or going right, go back to resting
            if ((e.gesture.direction == "left" || e.gesture.direction == "right")  && Math.abs(e.gesture.velocityX) < 0.3) {
                restingPoint = 0;
            } 

        }

        ionic.requestAnimationFrame(function () {
            if (restingPoint === 0) {
                self._currentDrag.content.style[ionic.CSS.TRANSFORM] = '';
                var buttonsLeft = self._currentDrag.buttonsLeft;
                var buttonsRight = self._currentDrag.buttonsRight;
                setTimeout(function () {
                    buttonsLeft && buttonsLeft.classList.add('invisible');
                    buttonsRight && buttonsRight.classList.add('invisible');
                }, 250);
            } else {
                self._currentDrag.content.style[ionic.CSS.TRANSFORM] = 'translate3d(' + restingPoint + 'px,0,0)';
            }
            self._currentDrag.content.style[ionic.CSS.TRANSITION] = '';


            // Kill the current drag
            if (!self._lastDrag) {
                self._lastDrag = {};
            }
            ionic.extend(self._lastDrag, self._currentDrag);
            if (self._currentDrag) {
                self._currentDrag.buttons = null;
                self._currentDrag.content = null;
            }
            self._currentDrag = null;

            // We are done, notify caller
            doneCallback && doneCallback();
        });
    };

我的解决方案并不完美,但它确实有效。还有其他方法可以做到这一点,我这样做是为了更好地了解 Ionic 的工作原理以及它们如何执行 Ionic 指令。

欢迎任何反馈,有了这个你可以尝试自己制作或改进这个。

【讨论】:

  • 这太棒了,谢谢!这对我很有效。只有一件小事-当您在两侧都有按钮并尝试向左/向右滑动时,回到中立位置有点困难。将其置于中立位置的唯一安全方法是稍微滚动整个列表。然后它就可以完美地重新组合了。
  • 也许你可以实现类似点击项目将其置于中立位置。
  • 已修复。问题出在 SlideDrag.prototype.end 中。有问题的行是 - if (e.gesture.deltaX > -(self._currentDrag.buttonsWidth / 2))。没有 self_currentDrag.buttonsWidth。有buttonsRightWidth 和buttonsLeftWidth。一旦解决了这个问题,滑回中立位置就像魔法一样。再次感谢您的出色解决方案!
  • 感谢@Dejan 提供的信息,您可以在这里发布您所做的,让我更新我的答案。
  • 当然,所以不要这样: if (e.gesture.deltaX > -(self._currentDrag.buttonsWidth / 2)) { put this: var buttonsWidth = 0; if (e.gesture.direction === 'right') buttonsWidth = self._currentDrag.buttonsRightWidth;否则buttonsWidth = self._currentDrag.buttonsLeftWidth; if (e.gesture.deltaX > -(buttonsWidth / 2)) {
【解决方案2】:

我创建了item-swipe-pane 指令,它在ion-item 内创建了一个容器,当项目向左或向右滑动时可见。

var ITEM_SWIPE_PANE_TPL = '<div class="item-options invisible item-swipe-pane"></div>';
var DIRECTION_RIGHT_CLASS = 'direction-right';
module.directive( 'itemSwipePane' , function() {
    return {
        restrict:   'E',
        require:    '^ionItem',
        link: function (scope, $element, attrs, itemCtrl) {
            var container;
            var direction = 'left';
            // Set direction
            if (attrs['direction'] && attrs['direction'] === 'right'){
                direction = 'right';
            } else if (attrs['direction'] && attrs['direction'] === 'left'){
                direction = 'left';
            }

            if (direction === 'left'){
                if (!itemCtrl.itemSwipeLeft){
                    itemCtrl.itemSwipeLeft = angular.element(ITEM_SWIPE_PANE_TPL);
                    itemCtrl.$element.append(itemCtrl.itemSwipeLeft);
                }
                container = itemCtrl.itemSwipeLeft;
            } else if (direction === 'right'){
                if (!itemCtrl.itemSwipeRight){
                    itemCtrl.itemSwipeRight = angular.element(ITEM_SWIPE_PANE_TPL);
                    // If direction is right, move position of item options
                    // to the left - override inherited right:0; 
                    itemCtrl.itemSwipeRight.css({right: 'auto'});
                    // "direction-right" is container selector. 
                    itemCtrl.itemSwipeRight.addClass(DIRECTION_RIGHT_CLASS);
                    itemCtrl.$element.append(itemCtrl.itemSwipeRight);
                }
                container = itemCtrl.itemSwipeRight;
            }

            container.append($element);
            // Animation to slowly close opened item.
            itemCtrl.$element.addClass('item-right-editable');
        } // link

    }; // return
}); // item-swipe-pane

属性direction 控制滑动方向。可能的值为leftright。默认方向为左。

您可以将任何内容放置在指令、按钮、文本、图像、图标、头像、背景图像等中。

从某种意义上说,容器非常原始,您放入其中的所有内容都必须通过 CSS 或其他方式进行格式化。

item-swipe-paneion-option-buttonion-delete-buttonion-reorder-button 指令兼容。

可以将两个item-swipe-panes 组合在同一个ion-item 上。每个都有不同的滑动方向。

以两个项目滑动窗格为例,一个在左侧,一个在右侧:

<ion-item>
  Two item-swipe-panes. One on the left, one on the right.
  <item-swipe-pane direction="right">
    <button class="button button-balanced ion-arrow-right-c"></button>
    <button class="button button-positive">Update</button>
    <button class="button button-royal">Add</button>
  </item-swipe-pane>

  <item-swipe-pane class="left-pane">
    <button class="button button-assertive">Delete</button>
    <button class="button button-calm">Share</button>
    <button class="button button-balanced ion-arrow-left-c"></button>
  </item-swipe-pane>        
</ion-item>

More item-swipe-pane examples 在 Codepen 上。

重要提示:

不幸的是Ionic Framework 不允许右滑(从左到右)列表项,所以我不得不对Ionic 库进行一些修改。这是summary of modifications to Ionic Framework

链接:

Modified Ionic library下载。

item-swipe-pane Github 上的指令。

【讨论】:

    【解决方案3】:

    这是一个示例代码,你可以使用它来实现它

    swipe-pane.html

       <div class="mk-swipe-pane">
            <div class="col-xs-4 swipe-actions-padding" ng-repeat="action in swipeActions">
                <div  ng-click="currentActionClick(action.actionName)"
                       ng-class="[action.actionCssclass]">
                        <div class="icon-font-size">
                            <i ng-class="[action.actionIcon]"></i>
                        </div>
                    {{action.actionName}}
                </div>
             </div>
        </div>
    

    swipe-pane.js

    angular.module('mk.Directives')
        .directive('mkSwipePane', function ($swipe) {
            return {
                templateUrl: "lib/mobikon/directives/notifications/swipe-pane/swipe-pane.html",
                restrict: 'E',
                scope: {
                    swipeActions: "="
                },
                replace: true,
                link: function ($scope, element) {
    
                    var MAX_VERTICAL_DISTANCE = 75,
                        MAX_VERTICAL_RATIO = 0.3,
                        MIN_HORIZONTAL_DISTANCE = 30,
                        startCoords,
                        valid,
                        elWidth = $(element).width(),
                        direction = 1,
                        pointerTypes = ['touch'],
                        delayForAnimation = 70;
    
                    $scope.currentActionClick = function (actionName) {
                        $scope.$emit('currentActionName', actionName);
                    };
    
                    function validSwipe(coords) {
                        if (!startCoords) return false;
                        var deltaY = Math.abs(coords.y - startCoords.y);
                        var deltaX = (coords.x - startCoords.x) * direction;
                        return valid && // Short circuit for already-invalidated swipes.
                            deltaY < MAX_VERTICAL_DISTANCE &&
                            deltaX > 0 &&
                            deltaX > MIN_HORIZONTAL_DISTANCE &&
                            deltaY / deltaX < MAX_VERTICAL_RATIO;
                    }
    
                    $swipe.bind(element, {
                        'start': function (coords, event) {
                            startCoords = coords;
                            valid = true;
                        },
                        'move': function (coords, event) {
    
                            var diffX = coords.x - startCoords.x;
                            if (diffX < 0) {
                                direction = -1; // For left swipe
                            } else {
                                direction = 1; // For right swipe
                            }
                            if (validSwipe(coords)) {
                                var marginLeft = parseInt($(element).css("marginLeft"));
                                if (direction === -1 && Math.abs(diffX) <= elWidth / 2) {
                                    $(element).prev().css({"margin-left": diffX});
                                } else if (direction === 1 && (marginLeft + diffX) <= 0) {
                                    $(element).prev().css({"margin-left": marginLeft + diffX});
                                }
                            }
                        },
                        'cancel': function (event) {
                            valid = false;
                        },
                        'end': function (coords, event) {
                            if (validSwipe(coords)) {
                                if (direction === -1) {
                                    $(element).prev().animate({"margin-left": "-50%"}, delayForAnimation);
                                    $scope.$emit('isCurrentRowClickable', {isSwiped: false});
                                } else {
                                    $(element).prev().animate({"margin-left": "0%"}, delayForAnimation);
                                    $scope.$emit('isCurrentRowClickable', {isSwiped: true});
    
                                }
                            }
                        }
                    }, pointerTypes);
                }
            }
        });
    
    require("./swipe-pane.html");
    require("./swipe-pane.scss");
    

    滑动窗格.scss

    @import "../../../../../views/mixins";
    
    [mk-swipe-pane], .mk-swipe-pane {
    
      display: inline-block;
      width: 50%;
      $icon-outline-color: $mk-pure-white;
      $icon-font-size: 35px;
      $icon-text-font-size: 16px;
      $icon-margin-top:-10px;
      $icon-padding-top:35%;
      $icon-padding-bottom:5px;
      $icon-container-height:120px;
      $icon-width:20px;
    
      @media screen and (max-width: 768px) {
    
         .swipe-actions-padding {
          padding-left: 0px;
          padding-right: 0px;
          float: none;
          display: inline-block;
         }
    
        .icon-font-size {
          font-size: $icon-font-size;
        }
    
        .email-icon {
          text-align: center;
          font-size: $icon-text-font-size;
          margin-top: $icon-margin-top;
          padding-top: $icon-padding-top;
          height: $icon-container-height;
          vertical-align: middle;
          background-color: $mk-swipe-action-1-icon-background-orange;
          color: $icon-outline-color;
        }
    
        .sms-icon {
          text-align: center;
          font-size: $icon-text-font-size;
          margin-top: $icon-margin-top;
          padding-top: $icon-padding-top;
          height: $icon-container-height;
          vertical-align: middle;
          background-color: $mk-swipe-action-2-icon-background-blue;
          color: $icon-outline-color;
        }
    
        .call-icon {
          text-align: center;
          font-size: $icon-text-font-size;
          margin-top: $icon-margin-top;
          padding-top: $icon-padding-top;
          height: $icon-container-height;
          vertical-align: middle;
          background-color: $mk-swipe-action-3-icon-background-green;
          color: $icon-outline-color;
        }
    
        .disabled {
          background-color: $mk-background-gray !important;
        }
      }
    }
    

    【讨论】:

      【解决方案4】:

      与其他答案不同,我为 swiper(这似乎是 ionic 2 中使用的滑块库)创建了一个角度包装器,专注于 ionic v1,而不是编辑框架本身。

      我的包装器是可用的here,还有一个演示here

      您可以使用npm install ionic-swiper 安装它,并按照README.md 中的说明导入:

      在带有 webpack 的 javascript 中(你也可以像普通的 js 一样导入整个包):

      import {moduleName as ionicSwiperModule} from 'ionic-swiper';
      
      angular.module('yourModule',[ionicSwiperModule]);
      

      编辑:

      自从我写了这个答案以来,我已经做了一些改变,所以这是使用我的库的更正确的方法:

      <ionic-swiper ng-repeat="i in [1,2,3]"
                    center-on-disable="{{ true || 'disable default center on disable behavior'}}"
                    is-swipable="{{ true || 'some prop to watch' }}"
                    left-swiper="{{:: true || 'or any prop that evaluate to a boolean' }}"
                    right-swiper="{{:: true || 'or any prop that evaluate to a boolean' }}">
          <!-- containerId is available inside this context -->
      
          <!-- Left transclude is optional -->
          <left-swiper class="side-item">
              Left
          </left-swiper>
      
          <!-- Central transclude is required -->
          <central-swiper class="central-item">
             Central {{:: containerId}}
          </central-swiper>
      
          <!-- Right transclude is optional -->
          <right-swiper class="side-item">
              Right
          </right-swiper>
      </ionic-swiper>
      

      这是原始答案用法示例:

      在 HTML 中(你也需要调整一些 css):

      <ionic-list>
          <div 
              swiper-container="true"
              class="swiper-container" 
              ng-repeat="item in [1,2,3,4,5,6]">
            <!-- containerId is available inside this context -->
      
            <div class="swiper-wrapper">
                      <ion-item swiper-slide="center">
                        This swiper container id is {{:: containerId }}
                      </ion-item>
                      <ion-item swiper-slide="right">
                          Right Button
                      </ion-item>
                      <ion-item swiper-slide="left">
                          Left Button
                      </ion-item>
              </div>
          </div>
      </ionic-list>
      

      这是演示中的 gif(我已将其记录在触摸板中,这就是为什么它看起来很“粘”)

      【讨论】:

      • 你知道如何将上述库转换为在 ionic 2 中使用。Ionic 提供了一个名为 ion-item-slidding 的默认组件,但它不允许我像上面那样具有灵活性1:1 滑动的条款。
      • @Sneha 不幸的是,我不知道 angular >=2 和 ionic >=2 (这是我的学习计划),所以我还不能移植.. 我很乐意帮忙我的空闲时间。您可以看一下 swiper lib 并尝试在 angular 2 组件中实现它..
      猜你喜欢
      • 2013-07-05
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多