Jump to content
  • Sign Up
  • 0

Как сделать меню 3 картинками


mut4bor
 Share

Question

Пытаюсь сделать меню с 3 картинками, чтобы при нажатии на стрелки вправо и влево картинки переключались. Есть предположение, что можно это сделать через массив, но не знаю как это реализовать на jQuery.

 

HTML

<div class="products">
        <div id="productsPICS">
 
          <div>
            <img src="img/products/1.jpg">
          </div>
 
          <div>
            <img src="img/products/2.jpg">
          </div>
 
          <div>
            <img src="img/products/3.jpg">
          </div>
          
        </div>
 
        <div id="leftButton" class="button">
          <img src="img/icons/leftarrow.ico">
        </div>
 
        <div id="rightButton" class="button">
          <img src="img/icons/rightarrow.ico">
        </div>
 
      </div>

 

CSS

 

.products {
  position: relative;
  text-align: center;
  box-sizing: border-box;
  width: 1000px;
  display: flex;
  align-items: center;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 100px;
  user-select: none;
}
 
#productsPICS {
  height: 600px;
  position: relative;
}
 
#productsPICS img {
  width: 100%;
  border: 0px;
  border-radius: 5px;
  opacity: 1;
}
 
#productsPICS div:nth-child(1) {
  display: block;
}
 
#productsPICS div:nth-child(2) {
  display: none;
}
 
#productsPICS div:nth-child(3) {
  display: none;
}
 
.button {
  position: absolute;
  width: 45px;
  aspect-ratio: 1;
  border: 0px solid black;
  border-radius: 5px;
  display: flex;
  align-items: center;
  opacity: 0.5;
  transition: opacity 150ms linear;
  background-color: #fff;
  top: 260px;
  z-index: 2;
}
 
.button:hover {
  opacity: 1;
  cursor: pointer;
}
 
.button img {
  width: 60%;
  aspect-ratio: 1;
  margin-left: auto;
  margin-right: auto;
}
 
#leftButton {
  left: 10px;
}
 
#rightButton {
  right: 10px;
}

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Пусть у вас будет массив элементов. Храним индекс текущего (активного) элемента, и по нажатию кнопки либо увеличиваем его значение (идем вперед), либо уменьшаем (идем назад). Для простоты можно по модулю делить, чтобы границы не обрабатывать и по кругу гулять по массиву.

По этому индексу обращаемся к элементу из списка узлов, вешаем ему класс, например .visible (ну и, соответственно, для предыдущего активного элемента снимаем его).

Вот и вся логика.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy