gpt4 book ai didi

javascript - 悬停时左侧边距不适用于 Carousel

转载 作者:行者123 更新时间:2023-11-28 14:00:54 26 4
gpt4 key购买 nike

悬停时,我希望我的图像将其他图像向左和向右移动。目前,悬停时,它会将所有图像仅向右移动。

使用这段代码:

.item-img:hover {
transform: scale(1.3);
margin: 0 70px 0 70px;
opacity: 1;
z-index: 950;
}

我本以为我会让两边的图像移动。但是不——还是对的!

因此而不是当前效果(仅右移) enter image description here

我试图让它双向移动,就像这样 enter image description here

html {
scroll-behavior: smooth;
}

body {
background-color: #696969;
}

a.controls {
position: absolute;
color: #fff;
text-decoration: none;
font-size: 6em;
background: #000;
width: 80px;
padding: 20px;
text-align: center;
z-index: 1;
}

a.controls:nth-of-type(1) {
height: 240.625px;
top: 120px;
bottom: 0;
left: 0;
background: linear-gradient(-90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 100%);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

a.controls:nth-of-type(2) {
height: 240.625px;
top: 120px;
bottom: 0;
right: 0;
background: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 100%);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
display: flex;
}

.carousel-inner .carousel-item-right.active,
.carousel-inner .carousel-item-next {
transform: translateX(25%);
}

.carousel-inner .carousel-item-left.active,
.carousel-inner .carousel-item-prev {
transform: translateX(-25%);
}

.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left {
transform: translateX(0);
}

.carousel-item:not(.active) {
display: none !important;
}

.carousel-item .active {
display: block !important;
}

.carousel-inner {
height: 500px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

.item-img {
object-fit: cover;
position: relative;
align-self: center;
width: 200px;
height: 200px;
margin: 0 3px;
transition: all 0.5s ease-in-out;
cursor: pointer;
z-index: 899;
}

.item-img:hover {
transform: scale(1.3);
margin: 0 70px 0 70px;
opacity: 1;
z-index: 950;
}

.item {
height: 240.625px !important;
}

.item__details {
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
font-size: 10px;
opacity: 0;
background: linear-gradient(to top, rgba(0, 0, 0, 0.9) 0%, rgba(0, 0, 0, 0) 100%);
transition: 450ms opacity;
}

.item__details:before {
content: '▶';
left: 0;
width: 100%;
font-size: 30px;
margin-left: 7px;
margin-top: -18px;
text-align: center;
z-index: 2;
}

.item__details:after {
margin-top: -25px;
margin-left: -25px;
width: 50px;
height: 50px;
border: 3px solid #ecf0f1;
line-height: 50px;
text-align: center;
border-radius: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1;
}

.item_title {
position: absolute;
bottom: 0;
padding: 10px;
}

.item:hover.item__details {
opacity: 1;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">


<div class="container-fluid text-center my-3 p-0">
<div class="row mx-auto my-auto">
<div id="ld_Carousel" class="carousel slide w-100" data-ride="carousel">
<div class="carousel-inner w-100" role="listbox">
<div class="carousel-item item active">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
</div>
</div>
<a class="carousel-control-prev controls" href="#ld_Carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next controls" href="#ld_Carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>

最佳答案

您必须使用 JQuery 来获得您正在寻找的效果,因为当前行为符合 CSS 规则...我所做的是将前一个元素向左推一点,以便它让路让我们的图像缩小...

工作代码段如下:

$(document).ready(function() {
$("img").mouseenter(function() {
$(this).prev().css('margin-left', '-10px')
});
$("img").mouseleave(function() {
$(this).prev().css('margin-left', '0px')
});
});
html {
scroll-behavior: smooth;
}

body {
background-color: #696969;
}

a.controls {
position: absolute;
color: #fff;
text-decoration: none;
font-size: 6em;
background: #000;
width: 80px;
padding: 20px;
text-align: center;
z-index: 1;
}

a.controls:nth-of-type(1) {
height: 240.625px;
top: 120px;
bottom: 0;
left: 0;
background: linear-gradient(-90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 100%);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

a.controls:nth-of-type(2) {
height: 240.625px;
top: 120px;
bottom: 0;
right: 0;
background: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 100%);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
display: flex;
}

.carousel-inner .carousel-item-right.active,
.carousel-inner .carousel-item-next {
transform: translateX(25%);
}

.carousel-inner .carousel-item-left.active,
.carousel-inner .carousel-item-prev {
transform: translateX(-25%);
}

.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left {
transform: translateX(0);
}

.carousel-item:not(.active) {
display: none !important;
}

.carousel-item .active {
display: block !important;
}

.carousel-inner {
height: 500px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
scroll-behavior: smooth;
}

.item-img {
object-fit: cover;
position: relative;
align-self: center;
width: 200px;
height: 200px;
margin: px;
transition: all 0.5s ease-in-out;
cursor: pointer;
z-index: 899;
}

.item-img:hover {
transform: scale(1.3);
margin: 0;
opacity: 1;
z-index: 950;
}

.item {
height: 240.625px !important;
}

.item__details {
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
font-size: 10px;
opacity: 0;
background: linear-gradient(to top, rgba(0, 0, 0, 0.9) 0%, rgba(0, 0, 0, 0) 100%);
transition: 450ms opacity;
}

.item__details:before {
content: '▶';
left: 0;
width: 100%;
font-size: 30px;
margin-left: 7px;
margin-top: -18px;
text-align: center;
z-index: 2;
}

.item__details:after {
margin-top: -25px;
margin-left: -25px;
width: 50px;
height: 50px;
border: 3px solid #ecf0f1;
line-height: 50px;
text-align: center;
border-radius: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1;
}

.item_title {
position: absolute;
bottom: 0;
padding: 10px;
}

.item:hover.item__details {
opacity: 1;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


<div class="container-fluid text-center my-3 p-0">
<div class="row mx-auto my-auto">
<div id="ld_Carousel" class="carousel slide w-100" data-ride="carousel">
<div class="carousel-inner w-100" role="listbox">
<div class="carousel-item item active">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://www.akberiqbal.com/JumboMob.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://www.akberiqbal.com/JumboMob.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
<img class="d-block col-2 img-fluid item-img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/70390/show-1.jpg">
</div>
</div>
<a class="carousel-control-prev controls" href="#ld_Carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next controls" href="#ld_Carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>

关于javascript - 悬停时左侧边距不适用于 Carousel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760665/

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