简体   繁体   English

当用户单击fancybox IMG时,在新选项卡中打开外部网站

[英]Open external website in new tab when user click fancybox IMG

Have setup Fancybox to popup to first time visitors. 已将Fancybox设置为首次访问者弹出。 Fancybox contains a image and i want link in that image. Fancybox包含图像,我想在该图像中链接。 So when user clicks it a new tab with the href opens. 因此,当用户单击它时,会打开一个带有href的新选项卡。

    <div id="usplayers" class="fancybox" style="max-width:500px;overflow:none;display: inline- block;">
    <a href="External URL" target="_blank">
        <img src="/folder/img.gif"> </a>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $(".fancybox").fancybox();
    });
</script>
<script type="text/javascript">
    function popit() {
        setTimeout(function () {
            $("#usplayers").trigger('click');
        }, 2000);
    }
    $(document).ready(function () {
        var visited = $.cookie('hello');
        if (visited == 'yes') {
            nothing();
        } else {
            popit();
        }
        $.cookie('hello', 'yes', {
            expires: 15
        });
    });
</script>

But it's not working with the external link, Only way to open it somehow is to click mouse scroll. 但是它不能与外部链接一起使用,只能以某种方式打开它是单击鼠标滚动。

What you can do is to wrap the opened image in fancybox inside an anchor tag <a> that will target the external URL. 你可以做的是wrap打开的图像wrap在一个定位外部URL的锚标记<a>中的fancybox中。

First you have to build your html properly in order to bind fancybox like : 首先,您需要正确构建您的html,以便绑定fancybox,如:

<a class="fancybox" href="{the image that you want to open in fancybox}">
  <img src="{the thumnail that users see on your page}" alt="" />
</a>

... if the visitors click on your thumbnail, fancybox will display the image you targeted in the href attribute of the <a> tag (this will be also fired by your popit() function). ...如果访问者点击缩略图,fancybox将在<a>标签的href属性中显示您定位的图像(这也将由您的popit()函数触发)。

Then you will need to use a fancybox callback to wrap the opened image an another <a> tag that will open the external URL in a new tab .... so your code should look like : 然后,您将需要使用fancybox回调wrap打开的图像wrap为另一个<a>标签,该标签将在新选项卡中打开外部URL ....因此您的代码应如下所示:

<script type="text/javascript">
function popit() {
    setTimeout(function () {
        $("#usplayers").trigger('click');
    }, 2000);
}
$(document).ready(function () {
    var visited = $.cookie('hello');
    if (visited == 'yes') {
        // nothing(); // this is not defined
        return false; // use this instead
    } else {
        popit();
    }
    $.cookie('hello', 'yes', {
        expires: 15
    });
    $(".fancybox").fancybox({
        // here you wrap the opened image
        afterShow: function () {
            $(".fancybox-image").wrap("<a href='http://jsfiddle.net' target='_blank' />");
        }
    });
});
</script>

See JSFIDDLE 请参阅JSFIDDLE

EDIT : 编辑

Based on @blachawk 's comment, if you have more than one element to display in fancybox and each element should link to a different external URL, you could dynamically pass each URL using a (HTML5) data-* attribute like : 根据@blachawk的评论,如果你有多个元素要在fancybox中显示,并且每个元素应链接到不同的外部URL,你可以使用(HTML5) data-*属性动态传递每个URL,如:

<a id="usplayers" data-url="jsfiddle.net" title="fire fancybox" class="fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>

... then, within the same callback, fetch the data-url attribute's value and set the href of the wrapping <a> tag like : ...然后,在同一个回调中,获取data-url属性的值并设置包装<a>标签的href ,如:

$(".fancybox").fancybox({
    afterShow: function () {
        var url = "http://" + $(this.element).data("url");
        $(".fancybox-image").wrap("<a href='"+url+"' target='_blank' />");
    }
});

Of course, see updated JSFIDDLE 当然,请参阅更新的JSFIDDLE

Change: 更改:

<a href="External URL" target="_blank">
<img   src="/folder/img.gif" </a>

To: 至:

<a href="External URL" target="_blank">
    <img src="/folder/img.gif" alt="" />
</a>

You can't do that with fancyBox. 你不能用fancyBox做到这一点。 The code was built to pop up an image in a popup window on top of the page you are on, not open a new tab to display info about the image you just opened. 该代码用于在您所在页面顶部的弹出窗口中弹出图像,而不是打开新选项卡以显示有关刚刚打开的图像的信息。

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

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