app/template/default/Block/developmet_comom_slider_.twig line 1

Open in your IDE?
  1. {% block main %}
  2. <style>
  3. .box_pdcommon {
  4.   display: flex;
  5.   flex-wrap: wrap;
  6.   justify-content: space-around;
  7.   width: 100%;
  8.   height: 180px;
  9.   z-index: 1;
  10.   background-image: url("https://nakatomishop.sakura.ne.jp/prod/merchandiseimage/25winter/wi2025_top_h250.png"); 
  11.   background-size: cover;
  12. }
  13. .fit-picture_pdcommon3 {
  14.   width: 100%;
  15. }
  16. .carousel-container {
  17.   display: flex;
  18.   gap: 20px;
  19.   flex-wrap: wrap;
  20.   justify-content: center;
  21.   margin-top: 10px;
  22.   margin-bottom: 10px;
  23. }
  24. .carousel_common {
  25.   position: relative;
  26.   width: 320px;
  27.   height: 150px;
  28.   overflow: hidden;
  29.   border: 1px solid #aaa;
  30.   border-radius: 5px;
  31.   background: #fff;
  32. }
  33. .carousel-track {
  34.   display: flex;
  35.   transition: transform 0.5s ease-in-out;
  36. }
  37. .carousel-item {
  38.   flex: 0 0 100%;
  39.   text-align: center;
  40.   background: #ddd;
  41.   padding: px 0;
  42.   font-size: 24px;
  43. }
  44. .prev, .next {
  45.   position: absolute;
  46.   top: 50%;
  47.   transform: translateY(-50%);
  48.   background: rgba(0,0,0,0.5);
  49.   color: #fff;
  50.   border: none;
  51.   font-size: 24px;
  52.   cursor: pointer;
  53.   padding: 5px 10px;
  54. }
  55. .prev { left: 5px; }
  56. .next { right: 5px; }
  57. /* ドットマーカー */
  58. .dots {
  59.   position: absolute;
  60.   bottom: 10px;
  61.   width: 100%;
  62.   display: flex;
  63.   justify-content: center;
  64.   gap: 8px;
  65. }
  66. .dot {
  67.   width: 10px;
  68.   height: 10px;
  69.   border-radius: 50%;
  70.   background: rgba(0,0,0,0.3);
  71.   cursor: pointer;
  72.   transition: background 0.3s;
  73. }
  74. .dot.active {
  75.   background: rgba(0,0,0,0.8);
  76. }
  77. /* レスポンシブ */
  78. @media (max-width: 1000px) {
  79.   .carousel { width: 45%; }
  80. }
  81. @media (max-width: 600px) {
  82.   .carousel { width: 100%; }
  83. }
  84. </style>
  85. <div class="box_pdcommon">
  86.   <div class="carousel-container">
  87.     <div class="carousel_common" id="carousel1">
  88.       <div class="carousel-track">
  89.         <div class="carousel-item">
  90.           <a href="https://www.nakatomishop.com/products/detail/1404">
  91.           <img class="fit-picture_pdcommon3" src="https://nakatomishop.sakura.ne.jp/prod/merchandiseimage/25winter/image_spacification_320_150.png"></a>
  92.         </div>
  93.         <div class="carousel-item">
  94.           <a href="https://www.nakatomishop.com/products/detail/1404">
  95.           <img class="fit-picture_pdcommon3" src="https://nakatomishop.sakura.ne.jp/prod/merchandiseimage/25winter/image_spacification_320_150.png"></a>
  96.         </div>
  97.         <div class="carousel-item">
  98.           <a href="https://www.nakatomishop.com/products/detail/1404">
  99.           <img class="fit-picture_pdcommon3" src="https://nakatomishop.sakura.ne.jp/prod/merchandiseimage/25winter/image_spacification_320_150.png"></a>
  100.         </div>
  101.         <div class="carousel-item">
  102.           <a href="https://www.nakatomishop.com/products/detail/1404">
  103.           <img class="fit-picture_pdcommon3" src="https://nakatomishop.sakura.ne.jp/prod/merchandiseimage/25winter/image_spacification_320_150.png"></a>            
  104.         </div>
  105.       </div>
  106.        <button class="prev">‹</button>
  107.        <button class="next">›</button>
  108.       <div class="dots"></div>
  109.     </div>
  110.     <div class="carousel_common" id="carousel2">
  111.       <div class="carousel-track">
  112.         <div class="carousel-item">2-1</div>
  113.         <div class="carousel-item">2-2</div>
  114.         <div class="carousel-item">2-3</div>
  115.         <div class="carousel-item">2-4</div>
  116.       </div>
  117.        <button class="prev">‹</button>
  118.        <button class="next">›</button>
  119.       <div class="dots"></div>
  120.     </div>
  121.     <div class="carousel_common" id="carousel3">
  122.       <div class="carousel-track">
  123.         <div class="carousel-item">3-1</div>
  124.         <div class="carousel-item">3-2</div>
  125.         <div class="carousel-item">3-3</div>
  126.         <div class="carousel-item">3-4</div>
  127.       </div>
  128.         <button class="prev">‹</button>
  129.         <button class="next">›</button>
  130.       <div class="dots"></div>
  131.     </div>
  132.   </div>
  133. </div>
  134. <script>
  135. document.querySelectorAll('.carousel_common').forEach(carousel => {
  136.   const track = carousel.querySelector('.carousel-track');
  137.   const items = Array.from(track.children);
  138.   const prevButton = carousel.querySelector('.prev');
  139.   const nextButton = carousel.querySelector('.next');
  140.   const dotsContainer = carousel.querySelector('.dots');
  141.   let index = 0;
  142.   let slideWidth = carousel.offsetWidth;
  143.   // 🧹ドット重複防止
  144.   dotsContainer.innerHTML = "";
  145.   // ドット生成
  146.   items.forEach((_, i) => {
  147.     const dot = document.createElement('div');
  148.     dot.classList.add('dot');
  149.     if (i === 0) dot.classList.add('active');
  150.     dot.addEventListener('click', () => {
  151.       index = i;
  152.       moveToSlide();
  153.       updateDots();
  154.       resetAuto();
  155.     });
  156.     dotsContainer.appendChild(dot);
  157.   });
  158.   const dots = dotsContainer.querySelectorAll('.dot');
  159.   const updateSlideWidth = () => {
  160.     slideWidth = carousel.offsetWidth;
  161.     track.style.transition = 'none';
  162.     track.style.transform = `translateX(-${slideWidth * index}px)`;
  163.   };
  164.   window.addEventListener('resize', updateSlideWidth);
  165.   updateSlideWidth();
  166.   const moveToSlide = () => {
  167.     track.style.transition = 'transform 0.5s ease-in-out';
  168.     track.style.transform = `translateX(-${slideWidth * index}px)`;
  169.     updateDots();
  170.   };
  171.   const updateDots = () => {
  172.     dots.forEach(dot => dot.classList.remove('active'));
  173.     dots[index].classList.add('active');
  174.   };
  175.   nextButton.addEventListener('click', () => {
  176.     index = (index + 1) % items.length;
  177.     moveToSlide();
  178.     resetAuto();
  179.   });
  180.   prevButton.addEventListener('click', () => {
  181.     index = (index - 1 + items.length) % items.length;
  182.     moveToSlide();
  183.     resetAuto();
  184.   });
  185.   // 自動スライド
  186.   let interval = setInterval(() => {
  187.     index = (index + 1) % items.length;
  188.     moveToSlide();
  189.   }, 3000);
  190.   const resetAuto = () => {
  191.     clearInterval(interval);
  192.     interval = setInterval(() => {
  193.       index = (index + 1) % items.length;
  194.       moveToSlide();
  195.     }, 3000);
  196.   };
  197.   carousel.addEventListener('mouseenter', () => clearInterval(interval));
  198.   carousel.addEventListener('mouseleave', resetAuto);
  199.   // スワイプ操作
  200.   let startX = 0;
  201.   let isDragging = false;
  202.   carousel.addEventListener('touchstart', e => {
  203.     startX = e.touches[0].clientX;
  204.     isDragging = true;
  205.     track.style.transition = 'none';
  206.   });
  207.   carousel.addEventListener('touchmove', e => {
  208.     if (!isDragging) return;
  209.     const moveX = e.touches[0].clientX - startX;
  210.     track.style.transform = `translateX(${-slideWidth * index + moveX}px)`;
  211.   });
  212.   carousel.addEventListener('touchend', e => {
  213.     isDragging = false;
  214.     const diff = e.changedTouches[0].clientX - startX;
  215.     if (diff > 50) index = (index - 1 + items.length) % items.length;
  216.     else if (diff < -50) index = (index + 1) % items.length;
  217.     moveToSlide();
  218.     resetAuto();
  219.   });
  220. });
  221. </script>
  222. {% endblock %}