javascript - 防止此点击事件的 iOS 双击问题?

标签 javascript jquery ios click mobile-safari

有没有一种方法可以防止在 iOS 移动版 Safari 上针对下面显示的点击事件发生双击链接问题?

var self = this;
this.$thumbnail.on('click', function(e) {
  e.preventDefault();
  src = src + '&autoplay=1';
  $innerContainer.addClass('yt-hd-thumbnail-clicked');
  self.$elem.attr({
    'src': src
  });
});

我主要是在寻找 JQuery 解决方案。

更新:添加了完整的代码段示例

我提供了一个片段,展示了如何通过 iOS Safari 点击事件两次才能自动播放嵌入的视频:

我已经根据最近的回答对我的原始脚本进行了修改,但仍然无法解决双击问题。

见下文:

(function($, window, document, undefined) {
  "use strict";

  var defaults = {
    darkenThumbnail: false
  };

  function YouTubeHDThumbnail(element, options) {
    this.elem = element;
    this.$elem = $(element);
    this.settings = $.extend({}, defaults, options);
    this._defaults = defaults;
    this._name = "youTubeHDThumbnail";
    this.init();
  }

  $.extend(YouTubeHDThumbnail.prototype, {
    init: function() {
      (this.videoId = null), (this.$thumbnail = null);

      // retrieve HD thumbnail
      var src = this.$elem.attr("src"),
        srcSplit = src.split("?"),
        srcMain = null,
        srcPure = null;

      if (srcSplit.length > 0) {
        srcMain = srcSplit[0];
        srcPure = srcMain.split("/");
        this.videoId = srcPure.pop();
        this.$thumbnail = $("<a />")
          .attr({
            href: "#"
          })
          .addClass("yt-hd-thumbnail");
        let thumb = $("<img/>", {
          src: "http://i.ytimg.com/vi/" + this.videoId + "/maxresdefault.jpg"
        });
        thumb.on("load", () => {
          const src =
            thumb[0].width < 121 ?
            "http://i.ytimg.com/vi/" + this.videoId + "/hqdefault.jpg" :
            "http://i.ytimg.com/vi/" + this.videoId + "/maxresdefault.jpg";
          this.$thumbnail.append(
            $("<img/>", {
              src
            })
          );
        });
      } else {
        console.log("The src attribute of iframe is not valid.");
        return;
      }

      // create container
      var $outerContainer = $("<div />")
        .addClass("yt-hd-thumbnail-outer-container")
        .insertAfter(this.elem)
        .css("width", this.$elem.attr("width")),
        $innerContainer = $("<div />")
        .addClass("yt-hd-thumbnail-inner-container")
        .appendTo($outerContainer);

      // insert thumbnail and iframe
      if (this.settings.darkenThumbnail) {
        this.$thumbnail.addClass("yt-hd-thumbnail-darken");
      }
      $innerContainer.append(this.$thumbnail).append(this.elem);

      // add click handler to thumbnail
      var self = this;
      this.$thumbnail.on("click touchend", function(e) {
        if (e.handled === false) return;
        e.stopPropagation();
        e.preventDefault();
        e.handled = true;
        src = src + "&autoplay=1";
        $innerContainer.addClass("yt-hd-thumbnail-clicked");
        self.$elem.attr({
          src: src
        });
      });
    }
  });

  $.fn["youTubeHDThumbnail"] = function(options) {
    return this.each(function() {
      if (!$.data(this, "plugin_" + "youTubeHDThumbnail")) {
        $.data(
          this,
          "plugin_" + "youTubeHDThumbnail",
          new YouTubeHDThumbnail(this, options)
        );
      }
    });
  };
})(jQuery, window, document);

$('iframe[src*="youtube.com"]').addClass("yt-hd-thumbnail");

$("iframe.yt-hd-thumbnail").youTubeHDThumbnail({
  darkenThumbnail: true
});
.yt-hd-thumbnail-outer-container {
  position: relative;
  height: auto;
}

.yt-hd-thumbnail-inner-container {
  position: absolute;
  padding-top: 56.25%;
  width: 100%;
  height: 0;
  top: 0;
  left: 0;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail,
.yt-hd-thumbnail-inner-container>iframe {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-width: 0;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail {
  z-index: 2;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail img {
  max-width: 100%;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail.yt-hd-thumbnail-darken:before {
  display: block;
  position: absolute;
  content: "";
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #000;
  opacity: 0.15;
  -webkit-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail.yt-hd-thumbnail-darken:hover:before {
  opacity: 0;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail:after {
  display: block;
  position: absolute;
  content: "";
  -webkit-mask-image: url('data:image/svg+xml; utf8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 70"><path d="M97.94,10.95a12.53,12.53,0,0,0-8.81-8.81C81.3,0,50,0,50,0S18.7,0,10.87,2.06A12.79,12.79,0,0,0,2.06,11C0,18.77,0,35,0,35S0,51.31,2.06,59.05a12.53,12.53,0,0,0,8.81,8.81C18.78,70,50,70,50,70s31.3,0,39.13-2.06a12.53,12.53,0,0,0,8.81-8.81C100,51.31,100,35.08,100,35.08S100.08,18.77,97.94,10.95ZM40,50V20L66,35Z"/></svg>');
  mask-image: url('data:image/svg+xml; utf8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 70"><path d="M97.94,10.95a12.53,12.53,0,0,0-8.81-8.81C81.3,0,50,0,50,0S18.7,0,10.87,2.06A12.79,12.79,0,0,0,2.06,11C0,18.77,0,35,0,35S0,51.31,2.06,59.05a12.53,12.53,0,0,0,8.81,8.81C18.78,70,50,70,50,70s31.3,0,39.13-2.06a12.53,12.53,0,0,0,8.81-8.81C100,51.31,100,35.08,100,35.08S100.08,18.77,97.94,10.95ZM40,50V20L66,35Z"/></svg>');
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center center;
  mask-position: center center;
  background-repeat: no-repeat;
  background-position: center center;
  background-color: white;
  width: 75px;
  height: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  opacity: 0.85;
  -webkit-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

.yt-hd-thumbnail-inner-container>a.yt-hd-thumbnail:hover:after {
  opacity: 0;
}

.yt-hd-thumbnail-inner-container>iframe {
  max-width: 100%;
  opacity: 0;
  -webkit-transition: opacity 0.3s ease 0.3s;
  -moz-transition: opacity 0.3s ease 0.3s;
  transition: opacity 0.3s ease 0.3s;
}

.yt-hd-thumbnail-inner-container.yt-hd-thumbnail-clicked>a.yt-hd-thumbnail {
  display: none;
}

.yt-hd-thumbnail-inner-container.yt-hd-thumbnail-clicked>iframe {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<iframe width="560" height="315" src="https://www.youtube.com/embed/WUUM27T38IE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

最佳答案

我发现一种使用 touchend 的方法似乎可以解决问题。如果有人有一个修复程序,不需要我将其添加到我的所有点击功能中,我很想知道。

var self = this;
this.$thumbnail.on('click touchend', function(e) {
  if (e.handled === false) return;
  e.stopPropagation();
  e.preventDefault();
  e.handled = true;

  src = src + '&autoplay=1';
  $innerContainer.addClass('yt-hd-thumbnail-clicked');
  self.$elem.attr({
    'src': src
  });
});

关于javascript - 防止此点击事件的 iOS 双击问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788696/

相关文章:

javascript - React 18 TypeScript child FC

javascript - Laravel 5.6 通过 ajax 调用将 Blade 注入(inject) View

javascript - 在 javascript 中向代理添加(递归?)级别意识

javascript - 拦截javascript事件

javascript - 新的 react native 元素无法访问

ios - 带有 SSL 和证书的 iPhoneHTTPServer

ios - 使用 Swift SpriteKit 时出现错误 "Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0x0)"

JavaScript 在方法中使用构造函数参数,哪种方式有效?

javascript - 在新选项卡上打开链接不适用于 IE7-8 和 safari 所有版本?

javascript - Css 日期选择器未正确显示