Dear professionals. I myself recently started learning jQuery, I had such a problem. Here I downloaded the finished gallery, on the back of 5 blocks (14 in total), on the right and left of the button for changing blocks. In total there are 14 blocks (goods). I added below, 4 blocks separately (Category)
{Block_1 Block_2 Block_3 Block_4}
The first 5 products belong to the 1st category ( Блок_1
), the following 4 products of the 2nd category ( Блок_2
), the following 3 products to the 3rd category ( Блок_3
). The following 2 products to the 4th category ( Блок_4
). Blocks are simply highlighted. Here is my HTML code:
<ul> <li><div class="news-text"><p>!1!Text</p></div></li> <li><div class="news-text"><p>!2!Text</p></div></li> <li><div class="news-text"><p>!3!Text</p></div></li> <li><div class="news-text"><p>!4!Text</p></div></li> <li><div class="news-text"><p>!5!Text</p></div></li> <li><div class="news-text"><p>!6!Text</p></div></li> <li><div class="news-text"><p>!7!Text</p></div></li> <li><div class="news-text"><p>!8!Text</p></div></li> <li><div class="news-text"><p>!9!Text</p></div></li> <li><div class="news-text"><p>!10!Text</p></div></li> <li><div class="news-text"><p>!11!Text</p></div></li> <li><div class="news-text"><p>!12!Text</p></div></li> <li><div class="news-text"><p>!13!Text</p></div></li> <li><div class="news-text"><p>!14!Text</p></div></li> </ul> </div> <div class="buttom_block"> <ul> <li class="item1"> 1_блок </li> <li class="item2"> 2_блок </li> <li class="item3"> 3_блок </li> <li class="item4"> 4_блок </li> </ul> </div>
Js file What I added:
var index = 1; function next_button(index,current){ if (index <= 4) $('.item1').addClass("class"); if (index == 5) { $('.item1').removeClass("class"); $('.item2').addClass("class"); } if (index == 9) { $('.item2').removeClass("class"); $('.item3').addClass("class"); } if (index == 12) { $('.item3').removeClass("class"); $('.item4').addClass("class"); } if (index == 14) { $('.item4').removeClass("class"); $('.item1').addClass("class"); } } function prev_button(index){ if (index >= 13 && index <= 14) $('.item4').addClass("class"); if (index >= 10 && index <= 12){ $('.item4').removeClass("class"); $('.item3').addClass("class"); } if (index >= 6 && index <= 9){ $('.item3').removeClass("class"); $('.item2').addClass("class"); } if (index >= 1 && index <= 5){ $('.item2').removeClass("class"); $('.item1').addClass("class"); } if (index==14) $('.item1').removeClass("class"); }
This I added to the buttons themselves. For Next
:
next_button(index,current); index++; if (index>14) index= index-14;
For Prev
:
index--; if (index==0) index = index+14; prev_button(index);
How to make it possible to add an arbitrary number of products (for example, if you add a few more products to block 1)? That is, so that you can specify in the HTML file which product belongs to which block. I hope someone will respond to my problem. I would be very grateful for any useful information. Thanks in advance!