【问题标题】:How load on double-click instead of click for magnific popup如何在双击而不是单击以获取放大的弹出窗口时加载
【发布时间】:2015-12-06 06:40:01
【问题描述】:

我认为这将是一个非常简单的问题:如何覆盖 Magnific Popup 的默认加载方法:我不想在单击时加载图像,而是在双击时加载。

我正在使用该模块在网站上加载图像。这是页面的js代码:

 $(document).ready(function() {
    // override magnificPopup.resizeImage:
    $.magnificPopup.instance.resizeImage = betterResizeImage;

    var magnific = $('#photoList').magnificPopup({
        delegate: 'img', // child items selector, by clicking on it popup will open
        type: 'image',
        closeOnContentClick: true,
    });

    $.getJSON('/json', function(json) {
        console.log('got json');
        json.forEach(function(item) {
            var img=$('<img/>')
                .attr('src', '/img/' + item.thumbnail)
                .attr('class', 'thumnail')
                .attr('data-mfp-src', '/img/' + item.file_name)
                .on('load', function() {
                    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
                        console.log('error loading ' + item.thumbnail);
                    } else {
                        $('#photoList').append(img);
                    }            
                });
        });
    });
});

$('#photoList') 只是一个 div,其中包含在页面加载后附加到它的图像。我想要做的就是将单击事件释放为其他内容,而是在双击时打开 Magnific Popup。

我不知道在模块代码中的哪个位置注册了 onclick 事件以打开模块,以及我将如何覆盖它。 Mosh Feu 的回答很接近,但每次双击任何图像时都会打开 div 中的第一张图像。

Github 上的精彩弹出窗口: https://github.com/dimsemenov/Magnific-Popup

【问题讨论】:

  • 您的用户会理解这种对已建立的用户界面交互的更改吗?
  • PS:在 fastclick 插件中点击处理程序被覆盖
  • 添加代码会更好。人们将能够编辑您的代码并快速提供解决方案

标签: javascript jquery html popup magnific-popup


【解决方案1】:

解决办法是去掉click处理程序,添加双击(dblclick)事件。

// init magnific
var magnific = $('.magnific').magnificPopup({
  type: 'iframe'
});

// remove click handler of magnific
magnific.off('click');

// prevent the default click event of the link so it will no redirect to the `href`
magnific.on('click', function(e) {
  e.preventDefault();
});

// add double click handler and call the `open` function of magnific
magnific.on('dblclick', function(){
  magnific.magnificPopup('open')
});
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/jquery.magnific-popup.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/magnific-popup.min.css" rel="stylesheet" />

<a class="magnific" href="https://www.youtube.com/watch?v=ij_0p_6qTss" class="magnific">magnific</a>

更新

如果你想展示一个画廊,就是这样:

// init magnific
var magnific = $('.container').magnificPopup({
  delegate: 'a', // child items selector, by clicking on it popup will open
  type: 'image',
  gallery:{
    enabled:true
  }
});

var links = magnific.find('a');

// remove click handler of magnific
magnific.off('click');

// prevent the default click event of the link so it will no redirect to the `href`
links.on('click', function(e) {
  e.preventDefault();
});

// add double click handler and call the `open` function of magnific with a specific index
links.on('dblclick', function() {
  magnific.magnificPopup('open', links.index(this))
});
a {
  text-decoration:none;
}

img {
  width:100px;
}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/jquery.magnific-popup.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/magnific-popup.min.css" rel="stylesheet" />

<div class="container">
  <a href="http://www.freeimageslive.com/galleries/objects/general/pics/sun0824.jpg">
    <img src="http://www.freeimageslive.com/galleries/objects/general/sun0824_small.jpg" />
  </a>
  <a href="http://www.freeimageslive.com/galleries/objects/general/pics/sunobject.jpg">
    <img src="http://www.freeimageslive.com/galleries/objects/general/sunobject_small.jpg" />
  </a>
  <a href="http://www.freeimageslive.com/galleries/objects/general/pics/sun0824a.jpg">
    <img src="http://www.freeimageslive.com/galleries/objects/general/sun0824a_small.jpg" />
  </a>
</div>

http://jsbin.com/cusixax/edit?html,css,js,output

【讨论】:

  • 看起来这段代码实际上导致每次打开 div 中的任何图像时都会显示“#photoList”中的第一张图像。
  • 如果我理解正确,您想展示一个画廊,而不是单个资源(图像等)。如果是这样,请查看我的答案中的更新。我被添加了一个画廊 sn-p。
  • 其实我并没有在这种情况下使用画廊模式。我只希望每个图像作为独立图像单独加载。无论哪种方式,您模式的更改或多或少对我有用 - 我现在唯一的问题是“链接”变量没有设置,因为图像是异步加载的,但我会得到它的工作没有问题。谢谢,这很有帮助!
  • 我的荣幸。如果您对此有任何问题,请发表评论,我会尽力帮助您。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-31
  • 2023-02-09
相关资源
最近更新 更多