margin-left перемещает только один элемент влево, а не всю строку - PullRequest
0 голосов
/ 06 октября 2019

При наведении курсора на каждый элемент карусели я использую jQuery для добавления -50px, поэтому весь ряд сдвигается влево. К сожалению, сдвигается только один элемент. Это не величина маржи, потому что, если я добавлю -200px, он все равно сделает то же самое. Может ли кто-нибудь пролить свет на то, почему весь ряд не сдвигается -50px?

$(document).ready(function() {
  $(".carousel-item img").mouseenter(function() {
    $(this).prev().css('margin-left', '-50px')
  });
  $(".carousel-item 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: 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;*/
  margin-right: 50px;
  opacity: 1;
  z-index: 950;
}

.item-img:not(:hover) {
  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;
}

.add-m {
  margin-right: 50px;
}
<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://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>




<div class="container-fluid text-center my-3 p-0">
  <div class="row mx-auto my-auto">
    <h2 class="text-center" style="color:white;">Social Media</h2>
    <div id="ld_Carousel" class="carousel slide w-100" data-ride="carousel">
      <div class="carousel-inner w-100" role="listbox">
        <!-- SET 1 -->
        <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>
</div>

1 Ответ

0 голосов
/ 06 октября 2019

Ваш скрипт сдвигает одно изображение, потому что вы нацеливаете только один элемент карусели на $(this).prev(). Вместо этого, чтобы сдвинуть всю строку, вы можете использовать $(this).closest(".carousel-item").

Ваш скрипт будет выглядеть так:

<script>
$(document).ready(function() {
  $(".carousel-item img").mouseenter(function() {
    $(this).closest(".carousel-item").css('margin-left', '-50px')
  });
  $(".carousel-item img").mouseleave(function() {
    $(this).closest(".carousel-item").css('margin-left', '0px')
  });
});
</script>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...