【问题标题】:Disable keyboard navigation (Wordpress) when PrettyPhoto modal is openPrettyPhoto 模式打开时禁用键盘导航 (Wordpress)
【发布时间】:2018-08-19 19:23:03
【问题描述】:

我正在从 CDN 加载 jQuery PrettyPhoto。我有这个 JS 可以从键盘启用帖子导航:

  // Add keyboard navigation for the next & previous post buttons
  $(document).keydown(function (e) {
    var url = false;
    if (e.which == 37) { // Left arrow key code
      url = $('a.prev-post').attr('href'); // change to match the pagination link classes in your theme
    } else if (e.which == 39) { // Right arrow key code
      url = $('a.next-post').attr('href');
    }
    if (url) {
      window.location = url;
    }
  });

我想添加一个布尔值,以防止在 PP 模式打开时执行此代码,但我不知道该怎么做。 PP中的相关代码是:

// Window/Keyboard events
$(window).unbind('resize.prettyphoto').bind('resize.prettyphoto',function(){ _center_overlay(); _resize_overlay(); });

if(pp_settings.keyboard_shortcuts) {
  $(document).unbind('keydown.prettyphoto').bind('keydown.prettyphoto',function(e){
    if(typeof $pp_pic_holder != 'undefined'){
      if($pp_pic_holder.is(':visible')){
        switch(e.keyCode){
          case 37:
            $.prettyPhoto.changePage('previous');
            e.preventDefault();
            break;
          case 39:
          $.prettyPhoto.changePage('next');
          e.preventDefault();
          break;
          case 27:
          if(!settings.modal)
            $.prettyPhoto.close();
            e.preventDefault();
            break;
        };
        // return false;
      };
    };
  });
};

我知道我可以在帖子导航上做这样的事情:

  // Add keyboard navigation for the next & previous post buttons

 var canUseArrows = true;
 $(document).keydown(function (e) {
    var url = false;
    if (e.which == 37 && canUseArrows) { // Left arrow key code
      url = $('a.prev-post').attr('href'); // change to match the pagination link classes in your theme
    } else if (e.which == 39 && canUseArrows) { // Right arrow key code
      url = $('a.next-post').attr('href');
    }
    if (url) {
      window.location = url;
    }
  });

但我不确定如何挂钩 PP 函数。

感谢收看,

【问题讨论】:

    标签: javascript jquery wordpress prettyphoto


    【解决方案1】:

    这可能会更有效率。

    // Add keyboard navigation for the next & previous post buttons
    $(document).keydown(function (e) {
    var url = false,
    // Check if the modal is open/visible.
    canNavi = ( ! $( '.pp_pic_holder' ).length );
    if (canNavi && e.which == 37) { // Left arrow key code
      url = $('a.prev-post').attr('href'); // change to match the pagination link classes in your theme
    } else if (canNavi && e.which == 39) { // Right arrow key code
      url = $('a.next-post').attr('href');
    }
    if (url) {
      window.location = url;
    }
    

    });

    来自 jQuery 文档:

    • 因为 :visible 是 jQuery 扩展而不是 CSS 规范的一部分,所以使用 :visible 的查询不能利用 本机 DOM querySelectorAll() 提供的性能提升 方法。使用 :visible 来选择时达到最佳性能 元素,首先使用纯 CSS 选择器选择元素,然后 使用 .filter(":visible")。

    • 大量使用此选择器可能会影响性能,因为它可能会强制浏览器在确定之前重新渲染页面 能见度。通过其他方法跟踪元素的可见性, 例如,使用类可以提供更好的性能。

    【讨论】:

    • $('.pp_pic_holder:visible')(选择元素)应该是这样,而不是$('.pp_pic_holder').is(':visible')(过滤选择的元素)。因为我相信.is(':visible') 与.filter(':visible') 类似(虽然返回值不同),所以jQuery 在过滤选择或匹配的元素时建议使用它。来自.is() doc,“它允许您在不修改的情况下测试 jQuery 对象的内容”,在我们的例子中,对象是$('.pp_pic_holder')。
    • 但是,如果您只想检查length 属性(即$( '.pp_pic_holder' ).length),那么我认为该决定是否由您决定。 =)
    【解决方案2】:

    似乎不可能“挂钩到 PP 功能”;但是,我尝试了以下方法,它对我有用:

      // Add keyboard navigation for the next & previous post buttons
      $(document).keydown(function (e) {
        var url = false,
          // Check if the modal is open/visible.
          canNavi = ! $('.pp_pic_holder').is(':visible');
        if (canNavi && e.which == 37) { // Left arrow key code
          url = $('a.prev-post').attr('href'); // change to match the pagination link classes in your theme
        } else if (canNavi && e.which == 39) { // Right arrow key code
          url = $('a.next-post').attr('href');
        }
        if (url) {
          window.location = url;
        }
      });
    

    【讨论】:

    • 谢谢;我会试一试,然后告诉你结果如何。
    • 效果很好。我显然想多了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2020-03-15
    • 2019-05-06
    • 1970-01-01
    • 2016-09-07
    • 2011-06-06
    相关资源
    最近更新 更多