最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open fancybox iframe when click on a div - Stack Overflow

programmeradmin1浏览0评论

Using fancybox2 and try to open YouTube video when click on div

<div class="vid" data-href="">This DIV should open fancybox</div>    

$(".vid").fancybox({
type: "iframe",
onStart: function (el, index) {
    var thisElement = $(el[index]);
    $.extend(this, {
        href: thisElement.data("href")
    });
  }
});

But its dosent work, please help.

jsfiddle

Using fancybox2 and try to open YouTube video when click on div

<div class="vid" data-href="http://www.youtube./embed/gGFXXWsCiVo?autoplay=1">This DIV should open fancybox</div>    

$(".vid").fancybox({
type: "iframe",
onStart: function (el, index) {
    var thisElement = $(el[index]);
    $.extend(this, {
        href: thisElement.data("href")
    });
  }
});

But its dosent work, please help.

jsfiddle

Share Improve this question asked Jul 1, 2015 at 15:28 Paul PodberezkoPaul Podberezko 352 silver badges4 bronze badges 1
  • you may want to make things simpler stackoverflow./a/31226347/1055987 – JFK Commented Jul 5, 2015 at 1:03
Add a ment  | 

2 Answers 2

Reset to default 5

Well, I don't know why you are plicating this fancybox thing like this, Why do you need your 'thisElement' var? You should give us some more details to be sure we really understand what you need... I give you here some tips to open fancybox.. :

when you use fancybox, just start it like this :

//event function(){
$('.vid').click(function(){
    $.fancybox({
        //settings
        'type' : 'iframe',
        'width'     : 640,
        'height'        : 480,
        //You don't need your thisElement var, see below : 
        'href'          : $(this).data('href')
    });
});

and put your settings into it,

here is a starting point for you, now just customize it to make it better for your needs :

http://jsfiddle/rtp7dntc/9/

Hope it helps!

To make things much simpler you could use the special data-fancybox attributes on your div like

<div class="vid" data-fancybox-href="http://www.youtube./embed/gGFXXWsCiVo?autoplay=1" data-fancybox-type="iframe">This DIV should open fancybox</div>

Then use a script as simple as this

jQuery(document).ready(function ($) {
    $('.vid').fancybox();
}); // ready

See JSFIDDLE

NOTE:

The disadvantage of triggering fancybox programmatically as in the accepted answer is that it will only trigger a single fancybox but won't work for a gallery, while you can bind a fancybox gallery of several divs using the data-fancybox-group attribute.

发布评论

评论列表(0)

  1. 暂无评论