【问题标题】:Horizontal slider with jQuery: 100% height, auto width for the images/children带有 jQ​​uery 的水平滑块:100% 高度,图像/子项的自动宽度
【发布时间】:2013-05-28 20:45:15
【问题描述】:

在过去的几天里,我一直在努力寻找解决问题的方法,但真的在任何地方都找不到,而且 Google 真的很讨厌我,所以我来了。这是一个很大的要求,我的良心在吞噬我的要求,但我不知道还能去哪里。

我正在为一名摄影师建立一个画廊,虽然我对 HTML 和 CSS 很自在,但我的 jQuery 技能正在受到打击(简而言之,它们并不好)- 惊喜,惊喜。

任务变得更加复杂,因为它是一种 100% 高度的画廊,而 100% 高度并不适合。我设法设置了一些,但它的功能确实受损。

在 Stack 和 Google 上挖掘后,我发现了 William Moynihan 的这个很棒的插件/小提琴: http://jsfiddle.net/wdm954/8GKz6/11/

它包含正是我的标记和 CSS,以及我一直在寻找的功能:滑动时使主图像居中的滑块,并且是无限的。

但是,由于图像上的width: auto; 属性,它不能很好地与height: 100%; 配合使用。以下行:

$(content).width(w * $(section).length);

由于 CSS 中的 auto 属性,似乎根本不计算容器的宽度(将其设置为零)。当我通过jQuery.css属性设置宽度为('width', 'auto')时,效果很好,但滑动功能不完善,导致左右移动时图片跳动。

我没有使用滑块,因为这是一种以水平方式实际布局内容的好方法,这在摄影师的网站上看起来很棒。并且拥有width: 100%; 将使垂直图像延伸到浏览器窗口之外,而水平图像“挂”在顶部,下方有大量空白。所以,我相信width: auto; 和height: 100% 是正确的、响应迅速的方式,但如果有人设法“说服”我,我一定会跟随你的领导。

当我在这里时,也许有人会礼貌地指出我正确的方向,以使这个画廊有限 - 在滑块的开始和结束处结束,左/右按钮相应地消失。

非常感谢任何帮助。这是代码本身,以防万一小提琴还不够:

<div class="container">
    <div class="gallery">
        <img src="../img/1.jpg" alt="Image" />
        <img src="../img/2.jpg" alt="Image" />
        <img src="../img/3.jpg" alt="Image" />
        <img src="../img/4.jpg" alt="Image" />
        <img src="../img/5.jpg" alt="Image" />
    </div>
</div>
<nav id="navigation">
    <a href="#" class="left">&#060;&#060;</a>
    <a href="#" class="right">&#062;&#062;</a>
</nav>

<script>
/*  jQuery Ghost Carousel
 *  Copyright (c) 2011 William Moynihan
 *  http://ghosttype.com
 *  Licensed under MIT
 *  http://www.opensource.org/licenses/mit-license.php
 *  May 31, 2011 -- v1.0
 */
$(function() {
    var content = '.container .gallery';
    var section = content + ' > img';

    function ghostCarousel() {

        var v = $(window).width();
        var w = $(section).width();
        var c = (w * $(section).length - v) / 2;

        $(content).width(w * $(section).length);
        $(content).css('margin-left', -c);
        $(content).css('width','auto');

        $('#navigation a.left').click(function(e) {
            e.preventDefault();
            if ($(content).is(':animated')) return false;
            $(content).animate({ marginLeft: '-=' +w }, 1000, function() {
                var first = $(section).eq(0);
                $(section).eq(0).remove();
                $(this).animate({ marginLeft: '+=' +w }, 0);
                $(this).append(first);
            });
        });
        $('#navigation a.right').click(function(e) {
            e.preventDefault();
            if ($(content).is(':animated')) return false;
            $(content).animate({ marginLeft: '+=' +w }, 1000, function() {
                var end = $(section).length - 1;
                var last = $(section).eq(end);
                $(section).eq(end).remove();
                $(this).animate({ marginLeft: '-=' +w }, 0);
                $(this).prepend(last);
            });
        });

    }

    ghostCarousel();

    $(window).resize(function() {
        var v = $(window).width();
        var w = $(section).width();
        var c = (w * $(section).length - v) / 2;
        $(content).css('margin-left', -c);
    });   
});
/* end "jQuery Ghost Carousel" */
</script>

与 CSS 一起:

html, body { padding: 0px; }

.container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;   
}
.container .gallery > img {
    position: relative;
    float: left;
    height: 100%;
}

【问题讨论】:

  • 好的,我来看看这个身高问题。有限的功能和动态按钮应该不会太难实现,但现在你能给我提供你想在画廊中使用的图像的尺寸吗?此外,如果您可以将 5 个示例图像放在网上某个地方,那么您可以直接在 jsfiddle 演示中引用它们。
  • 我同意@Ambrosia:我不确定我是否在小提琴上看到了这个问题,需要图像才能更有帮助。在 imgur 上创建一个帐户,在那里加载一些图像,然后在你的小提琴中调用它们。
  • 我不想给出答案,但是如果您对此感到困惑,这可能会对您有所帮助。我使用 carouFredsel 来满足我所有的滑块/轮播需求。即使您的 jquery 生锈了,并且有大量示例可以从以下示例中“获取想法”:coolcarousels.frebsite.nl/c/31 甚至是coolcarousels.frebsite.nl/c/2,我也很容易使用。它有一个方便的代码生成器。无论如何,它可能会为您节省很多麻烦。查看:caroufredsel.dev7studios.com
  • 哇。已经两个多月了,我真的没想到会有人回答。这个问题或多或少得到了解决,但不是以我满意的方式,所以在这里,以防万一...... @Ambrosia,就是这样,我正在处理自动宽度,因为上传是动态的(Wordpress ),并且垂直和水平图像都在起作用。在这种情况下,尺寸真的很重要吗?
  • @TravisHeeter,这是我所做的一个例子,但问题仍然存在,因为容器似乎没有正确计算图像宽度,所以它要么跳过最后两个或三个大分辨率的图像(即 1920x1080),或者它在较小的分辨率的末尾创建一个巨大的黑色间隙(我使用了 Sly 插件,因为上面的代码似乎根本不适合我)。 olgapalet.com/beautiful-people 以防万一,Sly 插件:darsa.in/sly

标签: javascript jquery html css


【解决方案1】:

为了使其有限,你只需要理解和修改这两个函数,

        $('#gcNav a.left').click(function(e) {
            e.preventDefault();
            if ($(content).is(':animated')) return false;
            $(content).animate({ marginLeft: '-=' +w }, 1000, function() {
                var first = $(section).eq(0);//this is first
                $(section).eq(0).remove();
                $(this).animate({ marginLeft: '+=' +w }, 0);
                $(this).append(first);//adding
            });
        });
        $('#gcNav a.right').click(function(e) {
            e.preventDefault();
            if ($(content).is(':animated')) return false;
            $(content).animate({ marginLeft: '+=' +w }, 1000, function() {
                var end = $(section).length - 1;
                var last = $(section).eq(end);//this is last
                $(section).eq(end).remove();
                $(this).animate({ marginLeft: '-=' +w }, 0);
                $(this).prepend(last);//adding
            });
        });

现在,在这段代码中,它可以通过单击 .left 和 .right 来实现,如果你想让它变得有限,

只计算幻灯片的长度,停止添加幻灯片,我已经添加了 cmets..

我刚刚指出了路……

我希望这会有所帮助...

【讨论】:

  • 已经两个多月了,我认为没有人会回答我的问题,但你的似乎是我正在寻找的解决方案(我找到了一个有效的,但它并不完美)。我会尝试并回复您。
猜你喜欢
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多