简体   繁体   English

jQuery:悬停时淡入/淡出-移动设备上的替代方法?

[英]jQuery: fadeIn / fadeOut on hover - alternatives on mobile devices?

I'm using fadeIn and fadeOut in jQuery and it works fine on desktop. 我在jQuery中使用fadeIn和fadeOut,它在桌面上可以正常工作。 However, on mobile devices (I've only tested on iPhones though), the child div opens on touch but won't hide after touching an outside element. 但是,在移动设备上(虽然我仅在iPhone上进行过测试),子div可以在触摸时打开,但在触摸外部元素后不会隐藏。 I'm fairly new to jQuery so I'm not quite sure what kind of solution I could implement here. 我对jQuery很陌生,所以我不确定在这里可以实现哪种解决方案。 Perhaps mobile detection and execute touch to open/hide, although I have no idea how I can do this. 也许是移动检测并执行触摸以打开/隐藏,尽管我不知道该怎么做。 Here's my JSFiddle: 这是我的JSFiddle:

https://jsfiddle.net/9LL3mmzt/ https://jsfiddle.net/9LL3mmzt/

jQuery: jQuery的:

$(document).ready(function() {
  $(".parent").hover(function() {
    $(this).children(".child").fadeIn("fast");
  }, function() {
    $(this).children(".child").fadeOut("fast");
  });
});

HTML: HTML:

<div class="parent">
  <span>Peek-A-</span>
  <div class="child">
    <span>Boo</span>
  </div>
</div>

CSS: CSS:

.child {
  display: none;
}

I tried the first solution from this thread: time-out on jQuery hover function 我尝试了此线程的第一个解决方案: jQuery悬停功能超时

However, I could not make it work correctly due to my limited knowledge. 但是,由于我的知识有限,我无法使其正常运行。

The hover function isn't really what you should be using on touch devices. 悬停功能并不是您应该在触摸设备上真正使用的功能。 I think you need to check out the touchstart function. 我认为您需要检查一下touchstart功能。

Edit 1: An example is this: 编辑1:一个例子是这样的:

$('div.example').on("touchstart", function (e) {
    alert('testing');
});

But keep in mind that you can still use your hover javascript but you must specify this for only using it on no-touch devices. 但请记住,您仍然可以使用悬停JavaScript,但是必须指定此选项仅在非触摸设备上使用。 See Modernizr for this. 参见Modernizr

Hope this helps. 希望这可以帮助。

FYI @James Douglas as you can see I can't post any comments yet because of my reputation score. 仅供参考,@ James Douglas,您可以看到由于我的声誉得分,我无法发表任何评论。 And if possible please comment or help us find the anwser for this question I think that would be more useful. 如果可能的话,请发表评论或帮助我们找到这个问题的答案,我认为这会更有用。

Edit 2: Modernizr is very handy. 编辑2:Modernizr非常方便。 In your case you get a class touch or no-touch (on the html element) depending on which device you are. 在您的情况下,您将获得类别触摸还是非触摸(在html元素上),具体取决于您使用的设备。

So in my example you could use it then as $('.touch div.example')... 所以在我的示例中,您可以将其用作$('。touch div.example')...

There are possibly different solutions but I think this is the best way (also see What's the best way to detect a 'touch screen' device using JavaScript? ). 可能会有不同的解决方案,但是我认为这是最好的方法(另请参见使用JavaScript检测“触摸屏”设备的最好方法是什么? )。 You just need to add de modernizr.js file to your website (best in the head of your webpage). 您只需要将de modernizr.js文件添加到您的网站(最好在网页顶部)。

Edit 3: I have tested your jsfiddle example on iPhone and Android and it works. 编辑3:我已经在iPhone和Android上测试了您的jsfiddle示例,并且可以正常工作。 So for me it's a good. 所以对我来说这是一个好习惯。

I was able to make this work (at least on iPhones) using the example from @Nick's post and the solution from the thread I linked in OP: 我可以使用@Nick帖子中的示例以及我在OP中链接的线程的解决方案(至少在iPhone上)使此工作有效:

Here's the JSFiddle: https://jsfiddle.net/tkeaoffd/ 这是JSFiddle: https ://jsfiddle.net/tkeaoffd/

JQuery: JQuery的:

$(document).ready(function() {
  function hasTouch() {
    try {
      document.createEvent("TouchEvent");
      return (true);
    } catch (e) {
      return (false);
    }
  }
  var touchPresent = hasTouch();
  $('.parent').hover(function() {
    var caption = $(this).find('.child');
    caption.stop(true, true).fadeIn("fast");
    if (touchPresent) {
      $('.parent').on("touchstart", function(e) {
        $(this).children(".child").fadeToggle("fast");
      });
    }
  }, function() {
    $(this).find('.child').stop(true, true).fadeOut("fast");
  });
});

HTML / CSS: HTML / CSS:

<div class="parent">
  <span>Peek-A-</span>
  <div class="child">
    <span>Boo</span>
  </div>
</div>


.child {
  display: none;
}

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

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