【问题标题】:disable hover effect and double click issue on ipad(touch devices) using vue js使用 vue js 在 ipad(触摸设备)上禁用悬停效果和双击问题
【发布时间】:2021-03-02 14:48:33
【问题描述】:

在我的应用程序中,有多个卡片和左/右桨。对于台式机/笔记本电脑中的正常浏览(safari、firefox、chrome),当鼠标悬停在这些卡片上时,左/右拨片是可见的。在这些卡片中,所有链接/超链接在台式机/笔记本电脑中都可以正常工作

我正在使用@mouseover 来显示这些拨片来滚动这些卡片。在 ipad(触摸设备)中,会出现该问题。因为悬停效果在触控设备中不起作用。

问题

  1. 这些拨片只有在第一次点击后才可见,因为悬停不适用于触控设备。

所以,我想禁用触摸设备的悬停并在第一次渲染时显示拨片。我检查了媒体查询,但它不起作用。我没有使用 jquery。那么,如何使用 vue js 在触摸设备的初始加载时显示这些拨片。

代码 sn-p

    <div
              v-show="grid"
              class="second-row"
              @mouseover="showPaddle"
              @scroll.passive="setScrolledLeft"
            >
              <div class="paddles">
                <button
                  v-if="showRightPaddle"
                  tabindex="-1"
                  class="right-paddle paddle"
                  @click="scrollCards('right')"
                >
                  <i class="el-icon-arrow-right-fill" />
                </button>
                <button
                  v-if="showLeftPaddle"
                  tabindex="-1"
                  class="left-paddle paddle"
                  @click="scrollCards('left')"
                >
                  <i class="el-icon-arrow-left-fill" />
                </button>
              </div>
   ....card details html
</div>

方法代码 sn-p 显示 Paddle

showPaddle() {
  const secondRowEl = this.$el.querySelector('.second-row');
  const gridWidth = this.$el.querySelector('.grid').clientWidth;
  const scrollMax = gridWidth - secondRowEl.clientWidth;
  if (gridWidth > secondRowEl.clientWidth) {
    if (secondRowEl.scrollLeft > 0 && secondRowEl.scrollLeft < scrollMax) {
      this.showLeftPaddle = true;
      this.showRightPaddle = true;
    } else if (secondRowEl.scrollLeft >= scrollMax) {
      this.showLeftPaddle = true;
      this.showRightPaddle = false;
    } else if (secondRowEl.scrollLeft <= 0) {
      this.showLeftPaddle = false;
      this.showRightPaddle = true;
    }
  } else {
    this.showLeftPaddle = false;
    this.showRightPaddle = false;
  }
}

Css 样式

.second-row {
      margin: 0 auto;
      padding: 0 0 0 4px;
      overflow-y: hidden;
      background: var(--gray40);
      -ms-overflow-style: none; 
      &::-webkit-scrollbar {
        display: none;
      }
      scrollbar-width: none;
      &:hover,
      &:focus {
        .paddles {
          display: block;
        }
      }

      .paddles {
        display: none;

        .paddle {
          border: none;
          padding: 0;
          height: 100px;
          width: 50px;
          cursor: pointer;
          position: fixed;
          margin-top: 33vh;
          z-index: 10;
          -webkit-transform: translateY(-50%);
          transform: translateY(-50%);
          text-align: center;
          font-size: 2.5em;
          color: #333333;
          background-color: rgba(204, 204, 204, 0.5);

          &:hover {
            background-color: rgba(204, 204, 204, 0.65);
          }

          &:focus {
            background-color: rgba(204, 204, 204, 0.8);
          }

          border-radius: 50px;
        }

        .left-paddle {
          &.paddle {
            > i {
              margin-left: -10px;
              font-size: 44px;
              position: relative;
              top: 2px;
            }

            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
          }
        }

        .right-paddle {
          right: 0;

          &.paddle {
            > i {
              margin-right: -10px;
              font-size: 44px;
              position: relative;
              top: 2px;
            }

            border-top-right-radius: 0;
            border-bottom-right-radius: 0;
          }
        }
      }

      .hidden {
        display: none;
      }

      .grid {
        display: block;
        &.product-grid {
          position: relative;
          height: calc(100vh - 194px);
          margin-bottom: 0;

          .item {
            position: absolute;
            width: rem(442);
            padding-right: rem(8);
            margin: 0;

            &.highlight {
              border: 1px solid #66afe9;
              border-radius: rem(4);
              outline: 0;
              box-shadow: 0 1px 1px 4px rgba(102, 175, 233, 0.6),
                0 0 2px rgba(102, 175, 233, 0.6);
              -moz-box-shadow: 0 1px 1px 4px rgba(102, 175, 233, 0.6),
                0 0 2px rgba(102, 175, 233, 0.6);
              -webkit-box-shadow: 0 1px 1px 4px rgba(102, 175, 233, 0.6),
                0 0 2px rgba(102, 175, 233, 0.6);
            }

            .app-card {
              .app-card__header {
                padding: 0.25rem 0.75rem;
                i {
                  position: relative;
                  top: rem(3);
                }
                .app-card__title {
                  &:first-child {
                    margin-left: 0;
                  }
                }
                .app-card__expand-icon {
                  margin-right: 0;
                  margin-top: 0.5rem;
                  height: 1rem;
                  right: 0.75rem;
                  i {
                    position: static;
                    color: var(--gray70);
                  }
                }
              }
            }
          }
        }
      }
    }

您能否帮助禁用触摸设备的悬停效果并在初始渲染时显示拨片?

【问题讨论】:

    标签: javascript css ios vue.js


    【解决方案1】:

    很遗憾,这个问题目前还没有完美的解决方案,而且将来也不大可能存在。 “网络意味着每个人都可以访问,无论他们使用哪种浏览器或设备。”这意味着所有内容都应该是可见的,并且W3C WAI 不赞成根据设备类型提供不同的内容

    但是,如果用户在不同的设备上查看内容,则更改内容的显示方式与完全隐藏内容是有区别的。您的场景属于第一类,我认为这很好。

    触摸屏设备(智能手机或平板电脑)不支持悬停事件。可以根据查看您的网站的用户代理(设备)以不同方式呈现页面,并且您有几个选项。不过总的来说,使用用户代理检测浏览器看起来很简单,但要做好,其实是一个非常难的问题。在下面详述的两个选项中,如果必须,我会选择选项 2。

    1. 使用 javascript 检测用户代理。 不推荐 - 阅读更多关于问题的信息here。
    let hasTouchScreen = false;
    if ("maxTouchPoints" in navigator) { 
        hasTouchScreen = navigator.maxTouchPoints > 0;
    } else if ("msMaxTouchPoints" in navigator) {
        hasTouchScreen = navigator.msMaxTouchPoints > 0; 
    } else {
        let mQ = window.matchMedia && matchMedia("(pointer:coarse)");
        if (mQ && mQ.media === "(pointer:coarse)") {
            hasTouchScreen = !!mQ.matches;
        } else if ('orientation' in window) {
            hasTouchScreen = true; // deprecated, but good fallback
        } else {
            // Only as a last resort, fall back to user agent sniffing
            var UA = navigator.userAgent;
            hasTouchScreen = (
                /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
                /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
            );
        }
    }
    if (hasTouchScreen) {
      // do some stuff on devices with smaller screens
    }
    
    1. 使用 javascript 检测屏幕大小(CSS 媒体查询也是一种选择,但您提到您已经尝试过)。 不推荐。例如,iPad Pro 12 的分辨率可与桌面设备媲美。
    if (window.innerWidth < 768) {
      // do some stuff on devices with screens smaller than
      // 768px wide
      window.addEventListener("resize", function() {
        /*refresh screen size dependent things*/
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2013-10-22
      • 1970-01-01
      • 2014-05-17
      相关资源
      最近更新 更多