javascript - 模态关闭时的溢出问题

标签 javascript html css angularjs

我的应用通过滑动来显示列表上的隐藏操作,用户可以在其中决定删除列表中的元素。

它使用 HammerJS 来处理滑动事件,并使用 SweetAlert2 来确认操作。

问题是当 SweetAlert 弹出窗口被关闭时。一旦用户单击取消并关闭模式,所有按钮就会突然可见。整个文档向左移动。

我创建了一个 JSFiddle复制它。

重现步骤:

  • 在其中一项上从右向左滑动以显示操作;
  • 点击“x”删除元素;
  • 点击取消。

我也把下面代码的内容贴出来供引用:

HTML:

<div ng-app="app" ng-controller="mainCtrl" class="wrapper">
  <div class="items" swipe>
    <div ng-repeat="item in items" class="item">
       <div>
         <div class="item-wrapper">
           <div class="logo">
             <img src="http://2.bp.blogspot.com/-Y2XrnrXJmXs/Uf5Y_bfr4jI/AAAAAAAAALk/ydouC9lEmDE/s1600/Logogap+Logobb.jpg" />
           </div>
           <div class="info">
             <div class="title">
               {{item.title}}
             </div>
             <div class="description">
               {{item.description}}
             </div>
           </div>
         </div>
         <div class="offset-action">
           <button ng-click="delete()">
             X
           </button>
         </div>
       </div>
    </div>
  </div>
</div>

CSS:

body {
  overflow: hidden;
}

.wrapper {
  border: 1px solid grey;
  min-width: 350px;
  max-width: 800px;
}

.items {
  overflow: hidden;
  position: relative;
}

.item {
  padding: 10px 15px;
  position: relative;
  transition: transform 250ms ease-in-out;
}

.item.show-actions {
  transform: translateX(-70px);
}

.item-wrapper {
  align-items: center;
  display: flex;
}

.logo {
  width: 80px;
}

.logo img {
  margin: auto;
  width: 80px;
  height: auto;
}

.offset-action {
  align-items: center;
  background: #B11A1F;
  display: flex;
  position: absolute;
  top: 0;
  bottom: 0;
  text-align: center;
  right: -70px;
  width: 70px;
}

button {
  background: transparent;
  border: none;
  color: white;
  width: 100%;
  height: 100%;
}

Javascript:

angular.module('app', [])

.controller('mainCtrl', function($scope) {
    $scope.items = [
    {title: 'Lorem Ipsum', description: 'Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.'},
    {title: 'Lorem Ipsum', description: 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'},
    {title: 'Lorem Ipsum', description: 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'},
    {title: 'Lorem Ipsum', description: 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'}
  ];

  $scope.delete = function() {
    swal({
        title: 'Lorem Ipsum',
      text: 'Dolor sit amet?',
      type: 'error',
      showCancelButton: true
    }).then(function(action) {
        if (action.value) {
        console.log('success');
      } else {
        swal.noop;
      }
    })
  }
})  

.directive('swipe', function() {
    return function(scope) {
    scope.$watch('items', function() {
      var $items = $('.item');
      var show_action = 'show-actions';

      function hideActions() {
        $items.removeClass(show_action);
      }

      $items.each(function() {
        var $item = $(this);

        $item.hammer().on('swipeleft', function() {
          var $this = $(this);
          $items.removeClass(show_action);
          if (!$this.hasClass(show_action)) {
            $this.addClass(show_action);
          }
        });

        $item.hammer().on('tap', hideActions);
      });
    });
  };
});

最佳答案

SweetAlert CSS 样式与您的 CSS 样式之间存在冲突,导致此问题。

一个简单的修复方法是将此 CSS 添加到 CSS 文件的末尾:

.offset-action {
  display: none;
}

.show-actions .offset-action {
  display: flex;
}

https://jsfiddle.net/saeedahmadi/1L7zc25b/

此外,Razvan 的回答将通过在关闭模式后摆脱焦点来完成这项工作:

https://stackoverflow.com/a/52028143/5939933

“在 delete() 方法的顶部添加 document.activeElement.blur();”

关于javascript - 模态关闭时的溢出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52001313/

相关文章:

javascript - 如何使用 jQuery 创建 Cookie?

java - 如何从applet返回文件到jsp

javascript - Firebase 模块在部署功能时需要旧版本的 Node

javascript - Jquery 实时验证

javascript - HTML5 - 启动共享相同变量的游戏对象

html - 如何使图片在画廊页面上均匀对齐(即平铺)?

CSS 问题背景变黑

html - 100% 宽度的 div,带有显示水平滚动条的填充

html - Chrome CSS 问题,span 文本底部不可移除的空白

javascript - 通过将 css id 和 class 传递给 jquery 方法动态创建 div