jquery - 尝试将 carouFredSel 放入可变宽度的容器中

标签 jquery html css responsive-design caroufredsel

我正在尝试使用响应式网格系统创建网站。列的宽度取决于浏览器窗口的宽度。在这些栏目中,我放了一个 carouFredSel 幻灯片。目的是让图像填充列的宽度,并使用 carouFredSel 在图像之间交叉淡入淡出。

问题是我的图像没有以正确的尺寸显示;左右边缘被剪掉了。当我使用 Chrome 检查时,它报告说我的主列 (.span_5_of_10) 宽度为 613px,它应该是这样,并且 carouFredSel 的包装器 div 大小相同,但 posterCarousel div 宽度为 661px。我不明白为什么。

我可以在 CSS 中将图像的宽度设置为 661 像素,这样可以解决所有问题,但我的设计将无法响应。如何让 carouFredSel 适合可变宽度的容器?

这是我的 HTML:

<div class="col span_5_of_10">
    <div class="posterCarousel">
        <img src="img/The-World-Needs-Heroes---Superman.png">
        <img src="img/The-World-Needs-Heroes---Knight.png">
        <img src="img/The-World-Needs-Heroes---Swashbuckler.png">
    </div>
</div>

文档底部的 JavaScript:

<script type="text/javascript">
    $(document).ready(function() {

        /*  CarouFredSel: a circular, responsive jQuery carousel.
            Configuration created by the "Configuration Robot"
            at caroufredsel.dev7studios.com
        */
        $(".posterCarousel").carouFredSel({
            width: "100%",
            items: {
                width: "100%"
            },
            scroll: {
                fx: "crossfade",
                pauseonhover: true
            },
            auto: 3000
        });

    });

</script>

以及控制我的图像的 CSS:

.posterCarousel img {
    width: 100%;
    z-index: 0;
    position: relative;
    margin: 0;
}

最佳答案

我最初想用 CSS 或通过重新配置 carouFredSel 来解决这个问题,但在为这个问题走了一个晚上之后,我决定编写更多 JavaScript 来解决 JavaScript 问题是有意义的。

我写了一个函数来获取列的当前宽度,然后将图像 CSS 设置为适当的值:

function setMainColumnWidth(){
    var mainColumnWidth = $(".span_5_of_10:first").width();
    $(".posterCarousel img").css("width", mainColumnWidth);
}

然后我在页面加载时和每次调整窗口大小时调用此函数:

$(document).ready(function() {
    setMainColumnWidth();
    /* carouFredSel code goes here */
});

$(window).resize(function() {
    setMainColumnWidth();
});

问题解决了!

关于jquery - 尝试将 carouFredSel 放入可变宽度的容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733067/

相关文章:

jquery - python列表到换行符分隔值

javascript - 获取 parent /祖先 UL

javascript - 你如何在 php 中使用 echo 打印 html 中的文本?

javascript - 使用 Accordion WordPress 插件时,Vimeo 嵌入全屏在 Chrome 中不起作用

asp.net - 向 .NET 发布请求在 jQuery 中有效,但在 Angulars 中无效 $http

php - 从php到ajax的返回值成功和错误

html - 向 div 添加额外的填充导致其他 div 向下移动 - CSS?

javascript - 将鼠标悬停在按钮外?

html - 跨多个页面的导航栏的django最佳实践

html - 带有 Trebuchet 字体的西类牙字符?