【发布时间】:2018-09-25 18:44:21
【问题描述】:
我正在尝试主要使用jQuery 从头开始制作自己的幻灯片。我遇到的问题是当我尝试将其用于手机和平板电脑(通常较小的屏幕)时。所以我的第一个想法就是将所有px 更改为%,但这并没有那么好,我不知道如何以“最佳”方式解决这个问题。
这就是“正常”的样子 调整窗口大小时会出现问题,然后我在图像上看到这个黑条。我的目标是整个盒子应该调整大小并看起来像上面的那个。 (这是使用谷歌浏览器的移动预览截图)
你可以测试下面的sn-p,它直接显示一个黑条,因为窗口很小。
#AutoSlider{min-height: 400px;} 只是为了不达到 10 px 的高度。
var options = {
Image: {
Width: "100%",
Height: "80%",
BackgroundColor: "black",
SecPerSlide: "5000"
},
ThumbnailContainer: {
Width: "15%",
Height: "100%",
RowColor: "black"
},
ThumbnailCell: {
BackgroundColor: "black",
Space: "3px",
Width: "100%",
Height: "15%",
FadedEdge: "15%"
}
}
carsGallery = [];
var container = $("#AutoSlider");
var thumbnailContainer;
var thumbnailCell;
var activeImage;
var autoCount = 1;
var stopSlider = false;
var subImageSliderPage = 0;
$(document).ready(function () {
GetImages();
BuildImageSlider();
SetStyles();
startSlider();
});
function SetStyles() {
container.css({
'width': options.Image.Width,
'height': options.Image.Height,
'max-height': '650px',
'background-color': options.Image.BackgroundColor,
'position': 'relative',
'overflow': 'hidden'
});
thumbnailContainer.css({
'width': options.ThumbnailContainer.Width,
'height': options.ThumbnailContainer.Height,
'background-color': options.ThumbnailContainer.RowColor,
'position': 'absolute',
'right': '0'
});
thumbnailCell.css({
'width': options.ThumbnailCell.Width,
'height': options.ThumbnailCell.Height,
'background-color': options.ThumbnailCell.BackgroundColor,
'margin-bottom': options.ThumbnailCell.Space
});
thumbnailCellImage.css({
'max-width': '100%',
'max-height': '100%',
'height': '100%',
'width': '100%'
});
activeImage.css({
'max-width': '85%',
'max-height': '100%',
'vertical-align': 'middle'
});
$('.thumbnailCell img').last().css('margin-bottom', '-37px');
$('.thumbnailCell img').last().css('margin-top', '-37px');
}
function BuildImageSlider() {
container.append('<div class="dragscroll" id="ThumbNail"></div>');
thumbnailContainer = $("#ThumbNail");
container.append('<span style="display: inline-block;height: 100%;vertical-align: middle;" class="helper"></span><img style="position: absolute; left: -15%; right: 0; bottom: 0; margin: auto;" id="ActiveImage"/>');
for (var i = 0; i < carsGallery.length; i++) {
thumbnailContainer.append('<div class="thumbnailCell"><span style="display: inline-block;height: 100%;vertical-align: middle;"></span><img index="' + i + '" src="' + carsGallery[i].mainImages + '"/></div>');
}
container.append('<div style="pointer-events: none; background-image: linear-gradient(black, rgba(1, 0, 0, 0.0)); top: 0; right: 0; position: absolute; height: 50px; width: ' + options.ThumbnailCell.FadedEdge + ';"></div>');
container.append('<div style="pointer-events: none; background-image: linear-gradient(rgba(1, 0, 0, 0.0), black); bottom: 0; right: 0; position: absolute; height: 50px; width: ' + options.ThumbnailCell.FadedEdge + ';"></div>');
container.prepend('<img src="/images/Icons/defforward.png" onmouseover="pauseSlider(true);" onmouseout="pauseSlider(false);" style="bottom: 50%; right: 15%; position: absolute; z-index: 100;" class="arrow" id="rightArrow"/>');
container.prepend('<img src="/images/Icons/defback.png" onmouseover="pauseSlider(true);" onmouseout="pauseSlider(false);" style="bottom: 50%; left: 0; position: absolute; z-index: 100;" class="arrow" id="leftArrow"/>');
activeImage = $("#ActiveImage");
thumbnailCell = $(".thumbnailCell");
thumbnailCellImage = $(".thumbnailCell img");
}
function GetImages() {
for (var i = 0; i < $('.mainPhoto img').length; i++) {
var main = $('.mainPhoto:eq(' + i + ') img').attr('src');
var sub = getSubImages(i);
carsGallery.push({ mainImages: main, subImages: [sub] });
}
}
function getSubImages(main) {
var s = [];
$('.interior' + main).each(function (e) {
s.push($(this).attr('src'));
});
return s;
}
$(document).ready(function () {
$(".thumbnailCell img").click(function () {
$('#ActiveImage').attr('src', $(this).attr('src'));
autoCount = +$(this).attr('index');
});
$('#rightArrow').click(function () {
if (subImageSliderPage >= carsGallery[autoCount].subImages[0].length) {
activeImage.attr('src', carsGallery[autoCount].mainImages).stop(true, true).hide().fadeIn();
subImageSliderPage = 0;
}
else {
activeImage.attr('src', carsGallery[0].subImages[0][subImageSliderPage]).stop(true, true).hide().fadeIn();
subImageSliderPage++;
}
});
$('#leftArrow').click(function () {
});
});
function startSlider() {
activeImage.attr('src', carsGallery[0].mainImages).stop(true, true).hide().fadeIn();
timerId = setInterval(function () {
if (stopSlider != true) {
if (autoCount >= carsGallery.length - 1) {
autoCount = 0;
}
activeImage.attr('src', carsGallery[autoCount].mainImages).stop(true, true).hide().fadeIn();
autoCount++;
}
}, options.Image.SecPerSlide);
}
function pauseSlider(state) {
stopSlider = state;
}
#AutoSlider{
min-height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="col-sm-12 story-for-sale sold-cars-box">
<div class="col-sm-10 col-sm-offset-1 story-for-sale sold-cars-box">
<div id="AutoSlider" onmouseover="pauseSlider(true);" onmouseout="pauseSlider(false);">
<div hidden class="carImages">
<div class="mainPhoto">
<img src="https://cdn.motor1.com/images/mgl/7WjgW/s4/1974-lancia-stratos-hf-stradale-for-sale.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/0e/91/97/0e9197574f9f55aec58af0fe5ff265bb.jpg" class="interior0" />
</div>
<div class="mainPhoto">
<img src="http://dmi3w0goirzgw.cloudfront.net/gallery-images/840x560/274000/600/274681.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/35/39/bd/3539bdb4edefb3a862e30386b8519ea1.jpg" class="interior1" />
</div>
<div class="mainPhoto">
<img src="https://cdn.motor1.com/images/mgl/7WjgW/s4/1974-lancia-stratos-hf-stradale-for-sale.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/0e/91/97/0e9197574f9f55aec58af0fe5ff265bb.jpg" class="interior2" />
</div>
<div class="mainPhoto">
<img src="http://dmi3w0goirzgw.cloudfront.net/gallery-images/840x560/274000/600/274681.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/35/39/bd/3539bdb4edefb3a862e30386b8519ea1.jpg" class="interior3" />
</div>
<div class="mainPhoto">
<img src="https://cdn.motor1.com/images/mgl/7WjgW/s4/1974-lancia-stratos-hf-stradale-for-sale.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/0e/91/97/0e9197574f9f55aec58af0fe5ff265bb.jpg" class="interior4" />
</div>
<div class="mainPhoto">
<img src="http://dmi3w0goirzgw.cloudfront.net/gallery-images/840x560/274000/600/274681.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/35/39/bd/3539bdb4edefb3a862e30386b8519ea1.jpg" class="interior5" />
</div>
<div class="mainPhoto">
<img src="https://cdn.motor1.com/images/mgl/7WjgW/s4/1974-lancia-stratos-hf-stradale-for-sale.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/0e/91/97/0e9197574f9f55aec58af0fe5ff265bb.jpg" class="interior6" />
</div>
<div class="mainPhoto">
<img src="http://dmi3w0goirzgw.cloudfront.net/gallery-images/840x560/274000/600/274681.jpg"/>
</div>
<div class="interior">
<img src="https://i.pinimg.com/originals/35/39/bd/3539bdb4edefb3a862e30386b8519ea1.jpg" class="interior7" />
</div>
</div>
</div>
</div>
</div>
【问题讨论】:
-
很高兴为您提供帮助.. 当您接受时,我正在输入评论.. :) .. 如果您将图像设置为背景,您可以将背景大小设置为封面或包含,但是它可能会歪曲外观..滑块上的好工作:)
-
@RachelGallen 你的帮助为我节省了很多时间,谢谢! :) 我主要做后端,但喜欢在有时间的时候做一些 js
-
节省时间总是好的。时间很宝贵!谢谢你的称赞。继续努力????
标签: javascript jquery html css slider