简体   繁体   English

使用Jquery进行幻灯片演示,图像不适合窗口

[英]Slideshow using Jquery, images do not fit window

I am trying to make a slideshow using jquery, I am a rooky in this code and am only familiar with css and html (though I am unsure how to position things in css). 我正在尝试使用jquery制作幻灯片,我在这段代码中还是个菜鸟,只熟悉css和html(尽管我不确定如何在css中放置内容)。 I want to create my slideshow and followed this template however I don't know how to change aspects of it, I tried messing around with it however no luck. 我想创建幻灯片并遵循此模板,但是我不知道如何更改它的方面,我尝试弄乱它,但是没有运气。 My images are much bigger than the slide window created, I want to fit the image to the window, since now only a portion of the image is shown, which doesn't look very good. 我的图像比创建的滑动窗口大得多,我想将图像适合窗口,因为现在只显示了图像的一部分,看起来不太好。

So I was wondering how I could fit the complete image in that slidewindow (not a portion) 所以我想知道如何在幻灯片窗口中放置整个图像(而不是一部分)

Here is what I have as html: 这是我拥有的html:

<div id="slideshow">
<div id="slideshowWindow">
    <div class="slide">
        <img src="Images/DSC_0419 copy.JPG" />
    </div>
    <div class="slide">
        <img src="Images/DSC_1019 copy.JPG" />
    </div>
    <div class="slide">
        <img src="Images/DSC_2975.JPG" />
    </div>
</div>
</div>

My CSS: 我的CSS:

#slideshow #slideshowWindow {
width:1000px;
height:700px;
margin:0;
padding:0;
position:relative;
overflow:hidden;

} }

#slideshow #slideshowWindow .slide {
margin:0;
padding:0;
width:1000px; 
height:700px;
float:left;
position:relative;

} }

And this is my Jquery script: 这是我的Jquery脚本:

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

    var currentPosition = 0;
    var slideWidth = 1000;
    var slides = $('.slide');
    var numberOfSlides = slides.length;
    var slideShowInterval;
    var speed = 3000;

    slideShowInterval = setInterval(changePosition, speed);
    slides.wrapAll('<div id="slidesHolder"></div>')
    slides.css({ 'float' : 'left' });
    $('#slidesHolder').css('width', slideWidth * numberOfSlides);   
    function changePosition() {
        if(currentPosition == numberOfSlides - 1) {
            currentPosition = 0;
        } else {
            currentPosition++;
        }
        moveSlide();
    }
    function moveSlide() {
            $('#slidesHolder')
              .animate({'marginLeft' : slideWidth*(-currentPosition)});
    }
});
</script>

Try ... 尝试...

#slideshow #slideshowWindow .slide img {
  height: 100%;
  width: 100%;
}

As it is, the CSS above will stretch the images ... if they are sized proportionately, this works fine ... 照原样,上面的CSS将拉伸图像...如果按比例调整大小,则效果很好...

However, if you might be dealing with some images that are portrait and some landscape, try setting only height or only width; 但是,如果您要处理的是纵向和横向的某些图像,请尝试仅设置高度或宽度;否则,请执行以下步骤。 then, add adjustments to center when needed. 然后,在需要时将调整添加到居中位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM