简体   繁体   English

jQuery Colorbox和自定义滚动条

[英]jquery colorbox and custom scrollbar

I would like to use a custom scrollbar with Colorbox 我想在Colorbox中使用自定义滚动条

I am using Wordpress so Colorbox is loaded using this plugin . 我正在使用Wordpress,因此使用此插件加载了Colorbox。 All the custom scrollbar files have been loaded as per the instructions. 所有自定义滚动条文件均已按照说明加载。 I'm guessing that I need to apply to the #cboxLoadedContent div so I've loaded as per this code, however it's not working: 我猜想我需要应用于#cboxLoadedContent div,因此我已按照此代码进行了加载,但是无法正常工作:

(function(jQuery){
    jQuery(window).load(function(){
        jQuery("#cboxLoadedContent").mCustomScrollbar();
    });
})(jQuery);

Using Firebug there's no alteration to the standard coding ie the JavaScript isn't firing. 使用Firebug不会更改标准编码,即JavaScript不会触发。 However if I add to the div #cboxContent the JavaScript fires and a class mCustomScrollbar _mCS_1 is added to the #cboxContent div. 但是,如果我将#cboxContent添加到div中, #cboxContent触发JavaScript,并将mCustomScrollbar _mCS_1类添加到#cboxContent div中。 But this doesn't have a scrollbar so nothing is shown. 但这没有滚动条,因此没有显示任何内容。

The question is why isn't it working on the right div ie #cboxLoadedContent ? 问题是为什么它不能在正确的div上工作,即#cboxLoadedContent

#cboxLoadedContent is appended and removed dynamically each time a colorbox is opened or closed. #cboxLoadedContent附加,并且每个颜色框一个被打开或关闭时动态除去。 Both plugins need to alter the markup and add their own wrappers , so simply calling mCustomScrollbar on either #cboxContent or #cboxLoadedContent won't work (mCustomScrollbar must wrap #cboxLoadedContent within .mCSB_container after #cboxLoadedContent is appended). 这两个插件都需要更改标记并添加自己的包装器 ,因此仅在#cboxContent#cboxLoadedContent上调用#cboxLoadedContent将不起作用(附加.mCSB_container 后, #cboxLoadedContent必须将#cboxLoadedContent .mCSB_container #cboxLoadedContent中)。

The best way is to call mCustomScrollbar function inside colorbox's onComplete callback. 最好的方法是在colorbox的onComplete回调中调用mCustomScrollbar函数。 This way the scrollbar is added when colorbox has done its work which is append #cboxLoadedContent and load the actual content. 这样,当colorbox完成其工作#cboxLoadedContent添加了#cboxLoadedContent并加载实际内容,从而添加了滚动条。

From colorbox examples: 从colorbox示例中:

$(".callbacks").colorbox({
    onComplete:function(){
        $("#cboxContent").mCustomScrollbar();
    }
});

Just a quick note about the code in the accepted answer. 关于接受的答案中的代码,请快速注意一下。 I couldn't get it to work with $("#cboxContent") as shown; 如图所示,我无法使其与$(“#cboxContent”)一起使用; I needed to use $('#cboxLoadedContent'). 我需要使用$('#cboxLoadedContent')。

$(".callbacks").colorbox({
    onComplete:function(){
        $("#cboxLoadedContent").mCustomScrollbar();
    }
});

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

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