我是否使用 jQuery noconflict 来避免插件冲突?

发布于 2024-09-07 08:33:26 字数 1574 浏览 8 评论 0原文


更新:我发现 jquery.bxslider 插件本身克隆并附加/前置是李造成了麻烦。不过,没有解决这个问题的方法,除非使用另一个脚本:(


我不得不让 jQuery 插件部分相互冲突,Boxslider (图像滑块)和 Colorbox (灯箱)它们都可以工作,但滑块脚本不知何故。广告到灯箱,因此第一个和最后一个图像会重复两次,

如果您查看示例页面

您会看到滑块中有 3 个图像,但打开灯箱时它会给您 5 个图像。

我已经用 尝试过运气noconflict(),但这只是停止了任何工作,经过一番搜索后,我假设存在一些命名空间问题,尽管我不知道在哪里或如何调试

我正在使用的脚本

<link type="text/css" media="screen" rel="stylesheet" href="assets/css/colorbox.css" />
<script type="text/javascript" src="assets/js/jquery.bxslider2.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#gallery').bxSlider({
        auto: true,
        wrapper_class: 'gallery-holder',
        auto: false,  
        speed: 100,
        pager: true,
        next_text: 'next',
        prev_text: 'back'
    });
}); 
</script>
<script type="text/javascript" src="assets/js/jquery.colorbox-min.js"></script>
<script type="text/javascript">
    $(window).load(function(){
        $("#gallery a[rel='group']").colorbox({maxWidth:"80%", maxHeight:"80%", scalePhotos: true});
    });
</script>

。我尝试过交换顺序,但这只会使一个或两个停止工作。我浪费了很多时间试图解决这个问题,所以任何帮助将不胜感激!


UPDATE: I've found that the jquery.bxslider plugin itself clones and appends/prepends the LIs causing the trouble. No solution to this though, except maybe using another script :(


I'm having to jQuery plugins partially conflicting with each other, Boxslider (an image slider) and Colorbox (a lightbox). They both work, but the slider script somehow ads to the lightbox so the first and last images get repeated twice.

This is easier to understand if you have a look at the example page.

You'll see there are 3 images in the slider, but when opening the lightbox it gives you 5 images.

I've tried my luck with noconflict(), but that just stopped anything from working at all. After some searching I'm assuming there are some namespacing issues, though I have no idea where or how to debug this.

The scripts I'm using are

<link type="text/css" media="screen" rel="stylesheet" href="assets/css/colorbox.css" />
<script type="text/javascript" src="assets/js/jquery.bxslider2.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#gallery').bxSlider({
        auto: true,
        wrapper_class: 'gallery-holder',
        auto: false,  
        speed: 100,
        pager: true,
        next_text: 'next',
        prev_text: 'back'
    });
}); 
</script>
<script type="text/javascript" src="assets/js/jquery.colorbox-min.js"></script>
<script type="text/javascript">
    $(window).load(function(){
        $("#gallery a[rel='group']").colorbox({maxWidth:"80%", maxHeight:"80%", scalePhotos: true});
    });
</script>

I've tried swapping the order around, which only made one or both stop working. I've wasted to much time trying to nail this down, so any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城追梦 2024-09-14 08:33:26

迟到总比不到好,是吗?我想我会分享我的解决方案,以防其他人遇到这个问题。

我最近使用 colorBox/bxSlider 组合并遇到了这个问题。当您初始化 bxSlider 时,它会将第一张和最后一张幻灯片复制到最后一个和第一个位置(分别)。我不太关心为什么,但我想这是为了支持循环。虽然理论上应该首先初始化 colorBox,但 bxSlider 中的某些内容会擦除 colorBox 设置的 $.data() 。

言归正传,我的快速解决方法是从重复的元素中删除类名。在 setChildrenLayout() 中,您可以看到项目在两个 $.each() 块中前置/后置的位置。您所要做的就是修改它以删除有问题的类名。

$.each($prependedChildren, function(index) {
    var moddedChild = $(this);
    moddedChild.children().removeClass("removeThisClass");
    $parent.prepend(moddedChild);
});

注意:我编写它是为了适合我的特定情况,如果您的列表项更复杂,则可能需要修改。

Better late than never, eh? Figured I'd share my solution in case anyone else runs into this problem.

I've recently been using a colorBox/bxSlider combo and had this issue. When you initialize the bxSlider it duplicates the first and last slides into the last and first positions (respectively). I wasn't much concerned with why but I imagine it's to support looping. While initializing the colorBox first should have worked in theory, something in bxSlider wipes the $.data() that colorBox sets.

Getting to the point, my quick fix was to strip the class name from the duplicated elements. In setChildrenLayout() you can see where the items get pre-/post-pended in the two $.each() blocks. All you have to do is modify it to remove the offending class name.

$.each($prependedChildren, function(index) {
    var moddedChild = $(this);
    moddedChild.children().removeClass("removeThisClass");
    $parent.prepend(moddedChild);
});

NOTE: I wrote it to work for my particular case, it might need modifications if your list items are more complex.

橙味迷妹 2024-09-14 08:33:26

发生的情况是,两个脚本都试图在同一组图像上进行操作,结果导致彼此崩溃。

Noconflict 基本上删除了 $ 的定义,因此其他使用 $ 的库(例如 Prototype)不会被覆盖。

因此, noconflict 在这里对你没有帮助。只使用其中一个插件怎么样?

What's happening is both scripts are trying to operate on the same sets of images, and just making each other break.

Noconflict basically removes the definition of $, so that other libraries which use $, such as Prototype, won't be overridden.

Thus, noconflict won't help you here. What about just using one of the plugins?

凉栀 2024-09-14 08:33:26

当您在 Firebug 中查看页面的源代码时,虽然滑块只显示三个图像,但实际上有 5 个 LI。您用于添加图像的选择器找到五个元素。

console.log($("#gallery a[rel='group']")); // yields....
>> [a.cboxElement B10_02.jpg, a.cboxElement  B10_01.jpg, a.cboxElement  B10_04.jpg, a.cboxElement  B10_02.jpg, a.cboxElement  B10_01.jpg] 

该数组有五个元素,这就是为什么颜色框有 5 个图像。

查看Curl获取的源代码,在任何JS运行之前,最初只有三个图像,BLADE/B10_02.jpg、B10_04.jpg和B10_01.jpg。所以,我想目标是看看何时添加这些内容。用于颜色盒的实际图像存储在作为第一个子项附加的单独区域中,那么什么是将这些添加到滑块 UL 中呢?

当您完全删除颜色框并仅初始化滑块时,请检查 Firebug 中的图库 UL。它有原来的三个 LI 还是五个?如果有五个,则 colorbox(或其他东西)会出于某种原因添加另外两个。如果有三个,请尝试重新添加颜色框初始化,并在重新加载页面时查看它是否有五个。这应该会稍微缩小一下添加这些内容的范围。

添加:
从源代码来看,bxSlider 似乎故意重复了第一个和最后一个项目。

var first = $this.children(':first').clone(); 
var last = $this.children(':last').clone(); 
/*etc, etc */
$this.append(first).prepend(last);

我建议尝试在滑块之前运行 colorbox 插件。当然,如果您更改源顺序,它不会首先运行,因为滑块上有 document.Ready() ,颜色盒上有 window.load() 。您可以在 Slider 执行之前在 document.ready() 中执行 colorbox 操作吗?

When you look at the source for your page in Firebug, while the Slider does only show three images, there are actually 5 LIs there. The selector you are using to add images finds five elements.

console.log($("#gallery a[rel='group']")); // yields....
>> [a.cboxElement B10_02.jpg, a.cboxElement  B10_01.jpg, a.cboxElement  B10_04.jpg, a.cboxElement  B10_02.jpg, a.cboxElement  B10_01.jpg] 

This array has five elements, which is why the colorbox has 5 images.

Looking at the source as fetched by Curl, before any JS runs, there are only three images originally, BLADE/B10_02.jpg, B10_04.jpg and B10_01.jpg. So, I guess the goal is to see when those are added. The actual images used for the colorbox are stored in a separate area appended to as the first child, so what is adding those to the slider UL?

When you remove the colorbox altogether and just have the slider initialize, inspect the gallery UL in Firebug. Does it have the original three LIs, or five? If it has five, colorbox (or something else) is adding the other two for some reason. If it has three, try re-adding the colorbox initialization, and see if it has five when you reload the page. That should narrow it down a little as to what is adding those.

addition:
From looking at the source, it looks like bxSlider intentionally duplicates the first and last items.

var first = $this.children(':first').clone(); 
var last = $this.children(':last').clone(); 
/*etc, etc */
$this.append(first).prepend(last);

I'd suggest trying to run the colorbox plugin before the Slider. It won't run first though if you change the source order, of course, since you have document.Ready() on the slider and window.load() on the colorbox. Can you do the colorbox in document.ready() before the Slider executes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文