【问题标题】:Clearing a responsive jQuery Cycle清除响应式 jQuery 循环
【发布时间】:2012-09-12 14:28:24
【问题描述】:

我使用jQuery cycle 创建了一个响应式图像滑块。

我使用的以下设置工作正常,除了包含 .cycle 的 div 未正确清除,使其下方的任何内容。

这是因为 div 是相对的,它的子元素是绝对的。

     $('.cycle').cycle({
        slideResize: true,
        containerResize: false,
        fit: 1,
        width: "fit"
     });

我的问题是,如何在没有固定高度或使用一些重事件 JavaScript 的情况下清除响应式 .cycle div?

这是我在 jsfiddle 上的代码:http://jsfiddle.net/blowsie/HuNfz/19/


更新:

我写了一些代码来修复循环的高度,它可以按预期工作(尽管它有时会出错),但它的事件很重而且不是很流畅。

我很想看到可以用纯 CSS 或更改循环设置来完成。

http://jsfiddle.net/blowsie/HuNfz/22/

【问题讨论】:

  • 我将 containerResize 属性更改为 true,然后它起作用了,但是它不再响应了..也许它有帮助..
  • 不,它需要响应!谢谢你
  • 不确定您是否愿意接受另一项技术,但我喜欢并使用Wilto's Dynamic Carousel。
  • @JeffreyLo 谢谢,但这并不能完全削减它,我需要淡入淡出效果和它没有的其他选项。

标签: jquery css jquery-plugins jquery-cycle


【解决方案1】:

Cycle 的响应不是很友好。但 Cycle2 绝对是。在这里查看:http://jquery.malsup.com/cycle2/

【讨论】:

【解决方案2】:

可能会添加一个调整大小的窗口:

$(window).resize(function () {
    $('.cycle').height($('img:visible').height());
});
setTimeout(300, $(window).resize());

http://jsfiddle.net/HuNfz/60/

【讨论】:

  • 持续轮询是一个非常糟糕的主意,除了该方法类似于我在问题中的模型。
【解决方案3】:

老帖子,我知道。

我们让它在生产中使用循环。 Cycle2 不是,现在也不是我们的选择。不幸的是,它涉及修改循环的内部数据。幸运的是,修复很简单。

当循环插件初始化时,它会为每个幻灯片的 DOM 对象添加两个属性 cycleW 和 cycleH,分别是每张幻灯片的初始宽度和高度。 Cycle 使用这些属性为每张幻灯片设置动画,而不管窗口的尺寸如何。

调整窗口大小时,需要手动清除cycleW和cycleH或将它们设置为预先确定的值。

示例(假设您的幻灯片容器是 .slideshow):

$(".slideshow").children("div").each(function(){
    this.cycleW = $(this).width();
    this.cycleH = $(this).height();
});

我们让它在生产中运行良好。

html 看起来像这样:

<div class="slideshow">
    <div class="slide"><img src=".." width="100%" height="auto"/></div>
    <div class="slide"><p>Copy copy copy no photo on slide </p></div>
    <div class="slide"><img src=".." width="100%" height="auto"/></div>
    <div class="slide"><p>Copy copy copy no photo on slide </p></div>
    ...
</div>

现在窗口调整大小功能。这是我们的版本。您可能需要对其进行自定义以满足您的需求。基本上,我们在幻灯片中存储固定图像的情况下的初始比率。有时我们有可变高度的幻灯片。此代码解决了这两种情况。然后,它重置每个幻灯片 DOM 元素的内部 cycleW 和 cycleH 值以适应它们的父容器

$(window).resize(function(){
    // first time around, save original width & height for ratios and complicated preloading
    $(".slideshow").each(function(i,elt){
        var originalWidth = $(elt).data("originalWidth"); // check existence of our ratio cache
        if (typeof originalWidth == "undefined") {
            var containerWidth = $(elt).parent().first().width(); // auto scale to container width.
            var containerHeight = $(elt).parent().first().height();
            $(elt).data("originalWidth",containerWidth);
            $(elt).data("originalHeight",containerHeight);
        }
    });

    // fix slideshows to their container width
    $(".slideshow").each(function(i,elt){
        var containerWidth = $(elt).parent().first().width();
        var originalWidth = $(elt).data("originalWidth")*1;
        var originalHeight = $(elt).data("originalHeight")*1;
        var height = Math.floor(originalHeight*containerWidth/originalWidth);   // maintain original ratio
        $(elt).css("width", containerWidth+"px").css("height",height+"px"); // container actual dimensions. height might get reset again below
        $(elt).children("div").css("width", containerWidth+"px"); // reset each slide width 
                                                                 // (fails once slide moves out, because the cycle plugin will use a cached value)
        $(elt).children("div").children().css("width", containerWidth+"px"); // fix direct children (images or divs - images should have height auto).

        // recompute max height based on inside slide, not just photos.
        // some slideshows have variable height slides.
        var maxHeight = 0;
        var panelArray = $(elt).children("div");
        for (var i = 0; i < panelArray.length; i++) {
            var height = $(panelArray[i]).height();
            if (height > maxHeight) maxHeight = height;
        }
        if (maxHeight > 0) {
            $(elt).height(maxHeight);
        }

        // fix internal cycle dimension cache (cycleW and cycleH on each slide)
        $(elt).children("div").each(function(){
            this.cycleW = containerWidth;
            this.cycleH = maxHeight;
        });
    });
});

【讨论】:

  • 只是一些指针,你的这段代码非常昂贵并且运行太频繁。您绝对应该将一些变量存储在更高的位置并消除函数的抖动。
  • 是的,我们在生产中对其进行了去抖 :) 这是我们为这个演示修改的一个简化示例,绝对可以优化。但问题是关于清除循环插件以提高响应能力,这篇文章对此进行了解释.话虽如此,既然你谈到了它,去抖动并不是平滑 UI 响应的答案。它可能会产生可怕的过渡,而收紧间隔可能会破坏目的。干杯并感谢您的评论:)
【解决方案4】:

默认情况下,插件将 position:relative 分配给 your 和 s ,它分配 position: absolute 和 z-index 值。这使得幻灯片成为一个浮动/不卡在堆栈中。我找到了 2 个解决方案:

  1. 将 clearfix 类添加到您的样式表中,并将 clearfix 的 css 样式添加到您的样式表中。

    .clearfix:{之后 内容: ”。”; 显示:块; 明确:两者; 可见性:隐藏; 行高:0; 高度:0; }

    .clearfix { 显示:内联块; }

    html[xmlns] .clearfix { 显示:块; }

    • html .clearfix { 高度:1%; }
  2. (不是很优雅)为你的循环类添加边框

    .cycle { 边框:1px 实心 #f00; }

希望其中一项有所帮助。

【讨论】:

猜你喜欢
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多