6

是否可以有一个带有 X 个子 div 的容器 div,每个子 div 在浏览器中都可以 100% 调整大小?我知道它可以用一个背景来完成,但不能用一排 div 来完成......这就是我打算做的(向左/向右滑动的幻灯片):

在此处输入图像描述

的HTML:

<div id="slideshowContent">
  <div id="slideshowImage_01" class="slideshowImage_container"></div>
  <div id="slideshowImage_02" class="slideshowImage_container"></div>
  <div id="slideshowImage_03" class="slideshowImage_container"></div>
  <div id="slideshowImage_04" class="slideshowImage_container"></div>
</div> <!-- End of id="slideshowContent" -->

...和CSS:

#slideshowContainer {
  z-index: 0;
  width: 11520px; height: 1080px;
  position: relative;
}

.slideshowImage {
  width: 1920px; height: 1080px;
  float: left;
}

谢谢。

佩德罗

编辑 1:我忘了提到每个单独的 div 都包含一个图像。

编辑 2:添加了我自己更新的FIDDLE

4

4 回答 4

3

这是完整的工作版本:http: //jsfiddle.net/y9zcf/7/

居中是使用 CSS 完成的,javascript 只是为了运行幻灯片。

HTML:

<div id="slideshowWrapper">
    <div id="slideshowContent">
        <div id="slideshowImage_01" class="slideshowImage_container active"></div>
        <div id="slideshowImage_02" class="slideshowImage_container"></div>
        <div id="slideshowImage_03" class="slideshowImage_container"></div>
        <div id="slideshowImage_04" class="slideshowImage_container"></div>
    </div>
</div>

CSS:

body, html {
    height: 100%;
    margin: 0;
}
#slideshowWrapper {
    overflow: hidden;
    width: 100%;
    height: 100%;
}
#slideshowContent {
    width: 400%; // 100% * number of slides
    height: 100%;
    position: relative;
}
#slideshowContent .slideshowImage_container {
    width: 25%; // 100% / number of slides
    height: 100%;
    float: left;
}

JS:

function slide() {
    var container = $('#slideshowContent'),
        nextDiv = container.find('.active').next('div'),
        slides = container.find('div'),
        next = nextDiv.length ? nextDiv : slides.first();

    slides.removeClass('active');
    next.addClass('active');
    $('#slideshowContent').animate({
        'left': '-' + next.index() * next.width() + 'px'
    }, 2000, function () {
        $(this).css({
            'left': '-' + next.index() * 100 + '%'
        });
    });

}

setInterval(function () {
    slide();
}, 5000);

这是 100% 可调整大小的,#slideshowWrapper可以设置为任何宽度/高度,并且里面的 div 可以正常工作。在动画后将CSS 设置为百分比值意味着 div 在过渡后仍可调整大小。

于 2013-06-04T14:26:11.967 回答
0

我能想到的唯一方法是使用javascript

CSS

#SlideShow {
    width:100%;
    overflow-x:hidden;
    position: relative;
}

#slideshowContent {
  z-index: 0;
  width: 11520px; 
  height: 1080px;
  position:absolute;
  left:0px;
  top:0px;
}

.slideshowImage_container img {
  max-width:100%;
}

JS

window.slideWidth = 0;
jQuery(document).ready(function(){
    window.onresize = funciton() {
        var window.slideWidth = jQUery("#SlideShow").width();
        jQuery("#slideshowContent slideshowImage_container").css("width",window.slideWidth+"px");
        jQuery("#slideshowContent").css("left","0px"); //or adjust the left to the current slide show image since they are now different lengths and the slideshow will be off.
    }
    window.resize();
});

并设置您的滑动技术以使用 window.slideWidth 作为滑动量并且只是#slideshowContent向左或向右滑动。

HTML

<div id="SlideShow">
  <div id="slideshowContent">
    <div id="slideshowImage_01" class="slideshowImage_container"></div>
    <div id="slideshowImage_02" class="slideshowImage_container"></div>
    <div id="slideshowImage_03" class="slideshowImage_container"></div>
    <div id="slideshowImage_04" class="slideshowImage_container"></div>
  </div>
</div>
于 2013-06-04T13:52:34.703 回答
0

看来您已经定义了要使用的宽度,所以我建议使用最大宽度和 100% 宽度来查看是否可以解决您的问题。

#slideshowContainer {
  z-index: 0;
  width: 100%;
  max-width: 1520px; 
  position: relative;
}

.slideshowImage {
  width: 100%;
  max-width: 1920px; 
  float: left;
}

如果您省略高度,它应该自动计算它。如果您不喜欢省略高度,您可以像设置宽度一样设置高度......

height: auto;
max-height: 1080px;

我希望这有帮助!

于 2013-06-04T14:00:36.997 回答
0

也许没有定位和浮动的水平 CSS 基础会让您感兴趣。

body, html {
    height:100%;
    padding:0;
    margin:0;
}
#slideshowWrapper {
    overflow: hidden;
    width: 100%; 
    height:100%;
}
#slideshowWrapper:hover {

}
#slideshowContent {
    height:100%;
    width:100%;
    text-indent:0;
    display:inline-block;
    white-space:nowrap;
}
.slideshowImage_container {
    height:100%;
    width:100%;
    display:inline-block;
    white-space:normal;
    text-indent:normal;
    text-align:center;
}
#slideshowImage_01 {
    background-color: #A0D0A8;
}
#slideshowImage_02 {
    background-color: #009FE3;
}
#slideshowImage_03 {
    background-color: #FCC99A;
}
#slideshowImage_04 {
    background-color: #D8B0CB;
}

http://jsfiddle.net/GCyrillus/PfbnW/1/

使用 CSS 动画作为额外的:(
只是为了展示, js 更坚实)不是那么灵活,每次添加或删除容器时都需要设置它。

#slideshowWrapper {
    animation: slidemeAnyDir 30s infinite;
    animation-play-state:running;
}
#slideshowWrapper:hover {
    animation-play-state:paused;
}

@keyframes slidemeAnyDir {
      0%, 15%, 100%      {text-indent:0;}
    17%, 32%,  84%, 98%  {text-indent:-100%;}
    34%, 49%,  68%, 82%  {text-indent:-200%;}
    51%, 66%%            {text-indent:-300%;}
}

为什么是文本缩进?因为它可以对文档在屏幕上显示的方向做出完美的反应。它将在父级或默认情况下设置方向的两侧滑动。

于 2013-06-04T17:00:25.107 回答