【问题标题】:change :hover to click/tap function on mobile/touch devices not working更改:悬停在移动/触摸设备上的单击/点击功能不起作用
【发布时间】:2015-08-05 23:21:23
【问题描述】:

所以,我正在尝试使用 Modernizr 的 no-touch/touch 类将 :hover 函数更改为单击函数,以用于页面上的特定元素(标题)。从理论上讲,这应该可行,但不知何故,它在移动/触摸设备上只能点击一次,这意味着 如果我再次点击/点击它,它不会“取消悬停” 。我可以通过点击页面上的另一个元素来“取消悬停”,但非常希望标题在<figure> 再次点击/点击时消失。

如果我将 js 更改为必须单击的 no-touch 设备,则它可以正常工作。我在这里错过了什么?

小提琴:https://fiddle.jshell.net/bh3aLkcL/3/

恐怕我的 js 技能至少可以说很差(阅读:不存在),我一直在使用另一个帖子中的 sn-p:Change hover interaction to click for touch screen devices

其余的工作,所以它只是一件事。非常感谢任何帮助!

Javascript:

// For hovering becoming click via Modernizr
//$('body').hasClass('no-touch') ? event = 'mouseenter mouseleave' : event = 'click';
!$('body').hasClass('no-touch') ? event = 'mouseenter mouseleave' : event = 'click';

$('.design-section figure').on(event, function () {
$(this).toggleClass('open');
});

HTML:

<section id="work" class="content-section text-left" data-offset="100px">
<div class="design-section">
    <div class="container">
        <div class="col-lg-8 col-lg-offset-2">
            <img src="http://cutepuppyclub.com/wp-content/uploads/2015/05/White-Cute-Puppy-.jpg" width="100%" class="img-responsive" alt="Playing the dilemma game">
            <figure>
                <figcaption>
                    <p>test text</p>
                </figcaption>
            </figure>
       </div>
    </div>
</div>
</section>

CSS:

figure {
    padding: 0;
    margin-top: 0;
    position: relative;
    display: block;
    overflow: hidden;
}

figcaption {
    position: absolute;
    background: rgba(0,0,0,.3);
    color: #fff;
}

figure.open figcaption {
    -ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
    filter: alpha(opacity=100);
    opacity: 1;
}

.design-section figcaption {
  opacity: 0;
  bottom: -30%; 
  left: 0;
  -webkit-transition: all 0.6s ease;
  -moz-transition:    all 0.6s ease;
  -o-transition:      all 0.6s ease;
  padding: 0;
  width:100%;
  display:block;
}

.design-section figure {
    height:120px;
    margin-top:-120px;
    z-index:1;
}

.design-section img {
    padding-top:0;
    margin-top:14px;
    z-index:0;
}

.design-section figcaption p {
    margin:0;
    padding: 1.5% 2.5%;
    font-size:15px;
}

.design-section figure.open figcaption{
    bottom: 0;
}

附:我正在使用 Bootstrap,但这在这件事上应该没什么好说的。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    您可以将多个事件指定为参数,因此只需包含 touchstart 即可在用户点击移动设备时添加操作。

    $('.design-section figure').on('mouseover mouseout touchstart', function () {
        $(this).toggleClass('open');
    });
    

    【讨论】:

      【解决方案2】:

      你不需要使用 Modernizr 来检查触摸事件,你可以这样做this way

      var event = ('ontouchstart' in window) ? 'click' : 'mouseenter mouseleave';
      
      $('.design-section figure').on(event, function () {
          $(this).toggleClass('open');
      });
      

      另外,您对Conditional (ternary) Operator 的使用是错误的,我已修复它。阅读right syntax

      【讨论】:

      • 非常感谢!但是,我发现 :hover 功能现在即使在非触摸设备上也是一个点击(即使我将 no-touch 更改为 touch )。当我在我的桌面浏览器上测试它时,我现在必须点击才能看到效果,即使 no-touch 类是打开的。 no-touch类时是否可以同时获得:hover函数和touch类时的点击函数?
      • 太棒了,这就像一个魅力!非常感谢!!
      • @loulyshas 你不应该忘记这不是最好的解决方案——当用户有鼠标和触摸屏时,情况如何?这将是糟糕的用户体验。
      • 感谢您的评论!我很清楚这并不理想,但这是我能想到的最佳解决方案;它应该适用于大多数设备(通过单击或悬停)(稍后我将在不同的设备上运行一些 UX 测试,但我最初的观察是这将是最好的解决方案,尽管并不理想)。
      • 如此处所述stackoverflow.com/a/6447935/474597 这可能会导致误报。
      猜你喜欢
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多