简体   繁体   English

向 fancybox 添加自定义按钮

[英]Add a custom button to fancybox

I am using the code below to display the results of my Python programme and on click view them in fancybox我正在使用下面的代码来显示我的 Python 程序的结果,然后单击在 fancybox 中查看它们

function myFunction(myArr){

    $("#retrieved_results").empty(); //results keep changing, so everytime there are new results delete old content
    $.each(myArr, function(key, val) {
        var img_url     = folder_name+val;
        var dt          = new Date();
        const url       = new URL(img_url,'http://127.0.0.1:5000/');
        url.search='time= '+dt.getTime();
        img_url         = url.href;
        console.log(key+" "+img_url);
        $("#retrieved_results").append('<a class="fancybox" data-title-id="'+key+'" href="'+img_url+'" data-fancybox="images" >'+'<img id="'+key+'" src="'+img_url+'" width="165" height="165" hspace=5px vspace=5px>'+'</a>');             
    });
}

I want to add an additional button in fancybox that when clicked redirects user to another page for analysis of that result image.我想在 fancybox 中添加一个附加按钮,单击该按钮会将用户重定向到另一个页面以分析该结果图像。 Since the fancybox and images are being dynamically appended, I do not know how to add the button.由于 fancybox 和图像是动态附加的,我不知道如何添加按钮。

You did not specify the location of the button, therefore I will assume that you what to add custom button inside the toolbar.您没有指定按钮的位置,因此我假设您要在工具栏内添加自定义按钮。

First, check out "How to create custom button in the toolbar" section in the docs - https://fancyapps.com/fancybox/3/docs/#faq-3 - basically, just use btnTpl option.首先,查看文档中的“如何在工具栏中创建自定义按钮”部分 - https://fancyapps.com/fancybox/3/docs/#faq-3 - 基本上,只需使用btnTpl选项。

Sencond, since you are dynamically creating elements, check out "How to Use" section on the docs - https://fancyapps.com/fancybox/3/docs/#initialize-with-javascript - basically, use selector option (or modify defaults ).第二,由于您正在动态创建元素,请查看文档中的“如何使用”部分 - https://fancyapps.com/fancybox/3/docs/#initialize-with-javascript - 基本上,使用selector选项(或修改defaults )。

For anyone else still stuck, this is how I ended up doing it after following Janis's answer:对于其他仍然卡住的人,这就是我在遵循 Janis 的回答后最终这样做的方式:

CSS file: 
.fancybox-button--openimg svg path,

JAVASCRIPT file:
    $("#retrieved_results").on("focusin", function(){

        $.fancybox.defaults.btnTpl.openimg= '<button data-fancybox-openimg class="fancybox-button fancybox-button--openimg" title="Open Image" onclick="yourFunction();">' +
        '<svg xmlns="http://www.w3.org/2000/svg" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" width="512px" height="512px">'+

        '<path d="M508.271,194.568l-149.333-128c-3.146-2.708-7.583-3.323-11.396-1.594c-3.781,1.74-6.208,5.521-6.208,9.688v74.813     c-119.188,3.313-176.688,62.312-204.021,112.031c-23.771,43.219-29.104,86.01-30.302,103.573     c-0.229,2.688-0.344,5.427-0.344,8.219v0.031c0,0,0,0.01,0,0.021v0.01c0,0.01,0,0.01,0,0.01v0.031v0.01c0,0,0,0.01,0,0.021v0.01     c0,0.01,0,0.021,0,0.021c0,0.01,0,0.01,0,0.01v0.031c0,0.01,0,0.021,0,0.031c0,0,0,0.01,0,0.031v0.01v0.021v0.01     c0,0.01,0,0.021,0,0.031l0.01,0.031c0,0,0,0,0,0.01c0,0.021,0,0.031,0,0.031c0,0.01,0,0.021,0,0.031v0.031v0.031v0.01v0.031     c0,0.01,0,0.021,0,0.031c0,0,0,0.021,0.01,0.031c0,0,0,0.01,0,0.031v0.01c0,0.01,0,0.021,0,0.031v0.031c0,0.01,0,0.021,0,0.031     c-0.01,0.031,0.021,0.042,0.01,0.042v0.031c0,0.01,0,0.021,0,0.031v0.031c0,0.021,0.01,0.042,0.01,0.042v0.031     c0,0.01,0,0.021,0,0.031v0.01v0.021v0.01l0.01,0.021v0.01c0,0.01,0,0.021,0,0.021c0,0.01,0,0.01,0,0.01v0.031v0.01     c0.01,0.01,0.01,0.01,0.01,0.021c0,0,0,0.01,0,0.021v0.01c0,0.01,0,0.01,0,0.01v0.021c0.      01,0.031,0.01,0.031,0.01,0.031     c0,0.01,0,0.021,0,0.031v0.01v0.021v0.01c0.01,0.01,0.01,0.021,0.01,0.021c0,0.021,0,0.031,0,0.031     c0.667,5.26,5.156,9.323,10.583,9.323c5.885,0,10.656-4.76,10.667-10.646v-0.01c0-0.542,0.01-1.865,0.104-3.885     c4.656-88.698,168.521-108.823,213.229-112.667v73.875c0,4.167,2.427,7.948,6.208,9.688c3.802,1.729,8.229,1.125,11.396-1.594     l149.333-128c2.365-2.021,3.729-4.979,3.729-8.094C512,199.547,510.635,196.589,508.271,194.568z M362.667,307.474v-62.146     c0-2.917-1.188-5.698-3.302-7.708c-1.979-1.906-4.625-2.958-7.365-2.958c-0.156,0-0.323,0-0.49,0.01     c-7.51,0.344-142.177,7.281-208.521,64.323c21.948-57.885,75.969-128.333,208.958-128.333H352c2.833,0,5.542-1.125,7.542-3.125     s3.125-4.708,3.125-7.542V97.849l122.281,104.812L362.667,307.474z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#FFFFFF"/>'+
        '<path d="M437.333,303.047c-5.896,0-10.667,4.771-10.667,10.667v91.615c0,11.76-9.573,21.333-21.333,21.333H42.667     c-11.76,0-21.333-9.573-21.333-21.333V191.995c0-11.76,9.573-21.333,21.333-21.333h147.729c5.896,0,10.667-4.771,10.667-10.667     s-4.771-10.667-10.667-10.667H42.667C19.135,149.329,0,168.464,0,191.995v213.333c0,23.531,19.135,42.667,42.667,42.667h362.667     c23.531,0,42.667-19.135,42.667-42.667v-91.615C448,307.818,443.229,303.047,437.333,303.047z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#FFFFFF"/>'+  
//you can add svg path of the icon you want to show
        '</svg>'+'</button>';
        $("a.fancybox").fancybox({
         // fancybox API options here
         buttons : [ 'zoom','slideShow','thumbs','openimg','close']
         }); 
         }); 

});

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

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