简体   繁体   English

如何仅显示一张图像并将其居中放置在滑块中?

[英]How do I show just one image and center it in a slider?

I'm using iosslider but can't seem to show just one image in the slider at a time. 我正在使用iosslider,但似乎无法一次仅在滑块中显示一个图像。 I'm also trying to center the single image shown. 我还试图将显示的单个图像居中。 Here is what I have for HTML: 这是我拥有的HTML:

HTML: HTML:

<div class = 'iosSlider'>
    <div class = 'slider'>
        <div class="item">
            <img src="/images/nature1.png" />
        </div>
        <div class="item">
            <img src="/images/nature2.png" />
        </div>
    </div>
</div>

My CSS looks like this: 我的CSS看起来像这样:

.iosSlider {
/* required */
position: relative;
top: 0;
left: 0;
overflow: hidden;

width: 100%;
height: 300px;

border: solid 1px #ff0000;
}

/* slider */
.iosSlider .slider {
/* required */
width: 100%;
height: 100%;
}

/* slide */
.iosSlider .slider .slide {
/* required */
float: left;

width: 100%;
height: 100%;
}

.iosSlider .slider .item img {
width: auto;
height: 300px;

border: solid 1px #00ff00;  
}

I'm basically trying to show just one centered image at a time in the slider. 我基本上是想一次只在滑块中显示一个居中图像。 Below is what I currently see. 以下是我目前看到的内容。 As you can see the image to the right is also shown. 如您所见,还显示了右侧的图像。

Any idea how I can do this with my CSS? 知道如何使用CSS做到这一点吗?

Try resizing the window and you'll see my issue. 尝试调整窗口大小,您会看到我的问题。 I'm running this on a mobile webpage that has 320x480 resolution. 我正在具有320x480分辨率的移动网页上运行此代码。

在此处输入图片说明

Give the images width/height of 100% and you can also position them absolutely: 将图片的宽度/高度设置为100%,您也可以将其绝对定位:

 .iosSlider .slider .item img {
     width: 100%;
     height: 100%;
     position:absolute;
     top:0;
     left:0;
     border: solid 1px #00ff00;  
}

Ok do one thing use javascript or jQuery here to loop through your set of images. 好的,在这里使用javascript或jQuery来遍历您的一组图像是一件好事。 Now add another class to the centered or focused image when jQuery function selects it like <div class = 'iosSlider'> <div class = 'slider'> <div class="item current"> <img src="/images/nature1.png" /> </div> <div class="item"> <img src="/images/nature2.png" /> </div> </div> </div> 现在,当jQuery函数选择它时,将另一个类添加到居中或聚焦的图像中,例如<div class = 'iosSlider'> <div class = 'slider'> <div class="item current"> <img src="/images/nature1.png" /> </div> <div class="item"> <img src="/images/nature2.png" /> </div> </div> </div>

and when the next image got focus, remove this class "current" from 1st image and apply to 2nd one and so on. 然后当下一幅图像成为焦点时,从第一幅图像中删除此类“当前”,并应用于第二幅图像,依此类推。 And wrtite css for this class current liek allign: center; margin-left:auto; margin-right:auto; 而对于这个班级里克·阿里根来说,这是allign: center; margin-left:auto; margin-right:auto; allign: center; margin-left:auto; margin-right:auto; and other necessary css. 和其他必要的CSS。

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

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