gpt4 book ai didi

javascript - 如何更改图像宽度以填充所有容器,或将其居中对齐?

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:25 30 4
gpt4 key购买 nike

我正在设置一个光滑的 slider ,我想要我用来填充所有容器的图像,但相反,它填充了它的一部分,向左对齐,具体取决于图像的宽度。我该如何解决?或在中心显示图像...

你可以明白我的意思: https://fantarimo.blogspot.com/2019/05/test-247.html

const $left = $(".left");
const $gl = $(".gallery");
const $gl2 = $(".gallery2");
const $photosCounterFirstSpan = $(".photos-counter span:nth-child(1)");

$gl2.on("init", (event, slick) => {
$photosCounterFirstSpan.text(`${slick.currentSlide + 1}/`);
$(".photos-counter span:nth-child(2)").text(slick.slideCount);
});

$gl.slick({
rows: 0,
slidesToShow: 2,
arrows: false,
draggable: false,
useTransform: false,
mobileFirst: true,
responsive: [
{
breakpoint: 768,
settings: {
slidesToShow: 3
}
},
{
breakpoint: 1023,
settings: {
slidesToShow: 1,
vertical: true
}
}
]
});

$gl2.slick({
rows: 0,
useTransform: false,
prevArrow: ".arrow-left",
nextArrow: ".arrow-right",
fade: true,
asNavFor: $gl
});

function handleCarouselsHeight() {
if (window.matchMedia("(min-width: 1024px)").matches) {
const gl2H = $(".gallery2").height();
$left.css("height", gl2H);
} else {
$left.css("height", "auto");
}
}

$(window).on("load", () => {
handleCarouselsHeight();
setTimeout(() => {
$(".loading").fadeOut();
$("body").addClass("over-visible");
}, 300);
});

$(window).on(
"resize",
_.debounce(() => {
handleCarouselsHeight();
/*You might need this code in your projects*/
//$gl1.slick("resize");
//$gl2.slick("resize");
}, 200)
);

$(".gallery .item").on("click", function() {
const index = $(this).attr("data-slick-index");
$gl2.slick("slickGoTo", index);
});

$gl2.on("afterChange", (event, slick, currentSlide) => {
$photosCounterFirstSpan.text(`${slick.currentSlide + 1}/`);
});

* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

img {
max-width: 100%;
height: auto;
}

button {
background: none;
border: none;
outline: none;
cursor: pointer;
}

body {
font: normal 18px/1.5 monospace;
overflow: hidden;
background: #424242;
}

.over-visible {
overflow: visible;
}

.loading {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
background: white;
}

.container {
max-width: 880px;
padding: 20px 10px;
margin: 0 auto;
}

.synch-carousels {
position: relative;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}

.synch-carousels > * {
width: 100%;
}

.synch-carousels .right {
order: -1;
}

.synch-carousels .left {
overflow: hidden;
}

.synch-carousels .gallery {
display: none;
}

.synch-carousels .slick-slide {
outline: none;
}

.synch-carousels .slick-vertical .slick-slide {
border: none;
}

.synch-carousels .gallery .slick-list {
height: auto !important;
margin: 0 -20px;
}

.synch-carousels .gallery .slick-slide {
cursor: pointer;
}

.synch-carousels .gallery .slick-slide {
margin: 0 20px;
}

.synch-carousels .nav-arrows {
display: flex;
position: absolute;
bottom: -50px;
left: 50%;
transform: translateX(-50%);
}

.synch-carousels .nav-arrows svg {
fill: white;
}

.synch-carousels .arrow-left {
margin-right: 35px;
}

.synch-carousels .photos-counter {
position: absolute;
top: 10px;
right: 0;
padding: 0 20px;
color: white;
background: #292929;
}

@media screen and (min-width: 480px) {
.synch-carousels .right {
margin-bottom: 20px;
}

.synch-carousels .gallery {
display: block;
}
}

@media screen and (min-width: 1024px) {
.synch-carousels .right {
position: relative;
width: calc(100% - 230px);
margin-bottom: 0;
order: 2;
}

.synch-carousels .left {
width: 210px;
}

.synch-carousels .gallery .slick-slide {
margin: 0 0 20px 0;
}

.synch-carousels .gallery .slick-list {
margin: 0;
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick-theme.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.css" rel="stylesheet"/>


<div class="loading">Carousel is loading...</div>
<div class="container">
<div class="synch-carousels">

<div class="left child">
<div class="gallery">
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery1.jpg" alt="">
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery2.jpg" alt="">
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery3.jpg" alt="">
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery4.jpg" alt="">
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery5.jpg" alt="">
</div>
</div>
</div>

<div class="right child">
<div class="gallery2">
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery1.jpg" alt="" />
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery2.jpg" alt="" />
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery3.jpg" alt="" />
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery4.jpg" alt="" />
</div>
<div class="item">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/gallery5.jpg" alt="" />
</div>
</div>
<div class="nav-arrows">
<button class="arrow-left">
<!--SVGs from iconmonstr.com-->
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd"><path d="M2.117 12l7.527 6.235-.644.765-9-7.521 9-7.479.645.764-7.529 6.236h21.884v1h-21.883z"/></svg>
</button>
<button class="arrow-right">
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd"><path d="M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z"/></svg>
</button>
</div>
<div class="photos-counter">
<span></span><span></span>
</div>
</div>
</div>
</div>

最佳答案

将图像上的最大宽度更改为最小宽度

img {
min-width: 100%;
height: auto;
}

关于javascript - 如何更改图像宽度以填充所有容器,或将其居中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56122824/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com