javascript 在fancybox jQuery中的Aftershow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24346313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:36:33  来源:igfitidea点击:

Aftershow in fancybox jQuery

javascriptjqueryfancyboxshare

提问by user3762663

I had the same problem this person: ShareThis plugin not working in FancyBox titleBut now I have gotten it work, except the

我遇到了同样的问题这个人:ShareThis plugin not working in FancyBox title但是现在我已经得到了它的工作,除了

afterShow: function(){
 stButtons.locateElements();
}

Where in this code should I put it? I've tried many places, but it just says error. It say's I am placing it wrong. Where should I place it in the script below?

我应该把它放在这段代码的什么地方?我试过很多地方,但它只是说错误。它说我放错了。我应该将它放在下面的脚本中的什么位置?

<script type="text/javascript">



$(document).ready(function(){
    $(".fancybox").fancybox({


             beforeShow: function() {
 var caption =  $(this.element).data("caption") ? $(this.element).data("caption") : "";
 this.title = this.title ? this.title + buildShareThis(this.href) + caption : buildShareThis(this.href) + caption;



        if (this.title) { ''

        // New line
            this.title += '<br />';



        }
    },




        nextEffect  : 'fade',
prevEffect  : 'fade',
padding     : 0,
margin      : [15, 15, 50, 15],
afterLoad   : addLinks,
beforeClose : removeLinks

        });


      function buildShareThis(url){
var customShareThis  = "<div class='share'>"; // class for styling maybe
 customShareThis += "<span class='st_facebook_hcount' displayText='Facebook' st_url='"+url+"'></span> ";
 customShareThis += "<span class='st_twitter_hcount' displayText='Tweet' st_url='"+url+"'></span>";
 customShareThis += "<span class='st_pinterest_hcount' displayText='Pinterest' st_url='"+url+"' st_img='"+url+"' ></span>";
 customShareThis += "<span class='st_tumblr_hcount' displayText='Tumblr' st_url='"+url+"'></span>";
 customShareThis += "</div>";
 return customShareThis;
}

    function addLinks() {
var list = $("#links");

if (!list.length) {    
    list = $('<ul id="links">');

    for (var i = 0; i < this.group.length; i++) {
        $('<li data-index="' + i + '"><label></label></li>').click(function() {     $.fancybox.jumpto( $(this).data('index'));}).appendTo( list );
    }

    list.appendTo( 'body' );
 }

list.find('li').removeClass('active').eq( this.index ).addClass('active');
}

function removeLinks() {
$("#links").remove();    
}

});

</script>

回答by JFK

You can set all fancybox API options, including fancybox callbacks (afterLoad, afterShow, beforeClose, etc.) like :

您可以设置所有的fancybox API选项,包括的fancybox回调(afterLoadafterShowbeforeClose等),如:

$(".fancybox").fancybox({
    nextEffect: 'fade',
    prevEffect: 'fade',
    padding: 0,
    margin: [15, 15, 50, 15],
    afterLoad: addLinks,
    afterShow: function () {
        stButtons.locateElements();
    },
    beforeClose: removeLinks
});

Assuming you have properly defined your addLinksand removeLinksfunctions somewhere else in your script.

假设您已经在脚本的其他地方正确定义了您的addLinksremoveLinks函数。