{"id":3418,"date":"2021-01-11T04:55:50","date_gmt":"2021-01-11T04:55:50","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3418"},"modified":"2023-02-28T05:40:47","modified_gmt":"2023-02-28T05:40:47","slug":"js-tutorial-infinite-rotating-carousel","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/js-tutorial-infinite-rotating-carousel\/","title":{"rendered":"JS Tutorial &#8211; Infinite Rotating Carousel"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; Try playing with the parameters on the gui to the right. The Infinite Rotating Carousel is just one effect of WebGL.<\/p>\n<p><iframe style=\"width: 100%;\" title=\"JS Tutorial - Infinite Rotating Carousel With 100 List Item\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/WNJbvpR?default-tab=result&amp;theme-id=dark\" height=\"900\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/WNJbvpR\"><br \/>\nJS Tutorial &#8211; Infinite Rotating Carousel With 100 List Item<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const carousel = document.querySelector('.carousel__list');\r\nconst cells = carousel.querySelectorAll('.carousel__cell');\r\n\r\nconst cellWidth = carousel.offsetWidth;\r\nconst cellHeight = carousel.offsetHeight;\r\nconst cellSize = cellHeight;\r\nconst cellCount = 100;\r\n\r\nconst radius = Math.round((cellSize \/ 2) \/ Math.tan(Math.PI \/ cellCount));\r\nconst theta = 360 \/ cellCount;\r\n\r\nvar selectedIndex = 0;\r\n\r\nfunction rotateCarousel() {\r\n    const angle = theta * selectedIndex * -1;\r\n    carousel.style.transform = 'translateZ(' + -radius + 'px) ' + 'rotateX(' + -angle + 'deg)';\r\n    \r\n    const cellIndex = selectedIndex &lt; 0 ? (cellCount - ((selectedIndex * -1) % cellCount)) : (selectedIndex % cellCount);\r\n    \r\n    const cells = document.querySelectorAll('.carousel__cell');\r\n    cells.forEach((cell, index) =&gt; {\r\n        if(cellIndex === index) {\r\n            if(!cell.classList.contains('selected'))\r\n                cell.classList.add('selected');\r\n        }\r\n        else {\r\n            if(cell.classList.contains('selected')) {\r\n                cell.classList.remove('selected');\r\n            }\r\n        }\r\n    });\r\n}\r\n\r\nfunction selectPrev() {\r\n    selectedIndex--;\r\n    rotateCarousel();    \r\n}\r\n\r\nfunction selectNext() {\r\n    selectedIndex++;\r\n    rotateCarousel();    \r\n}\r\n\r\nvar prevButton = document.querySelector('.previous-button');\r\nprevButton.addEventListener('click', selectPrev);\r\n\r\nvar nextButton = document.querySelector('.next-button');\r\nnextButton.addEventListener('click', selectNext);\r\n\r\nfunction initCarousel() {    \r\n    for(let i = 0; i &lt; cells.length; i++) {\r\n        const cell = cells[i];\r\n        const cellAngle = theta * i;\r\n        cell.style.transform = 'rotateX(' + -cellAngle + 'deg) translateZ(' + radius + 'px)';\r\n    }\r\n\r\n    rotateCarousel();\r\n}\r\n\r\ninitCarousel();\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; Try playing with the parameters on the gui to <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/js-tutorial-infinite-rotating-carousel\/\" title=\"JS Tutorial &#8211; Infinite Rotating Carousel\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3835,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[325,335,264,263,103,227,265],"newstopic":[],"class_list":{"0":"post-3418","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"tag-background-effects","9":"tag-infinite-rotating-carousel","10":"tag-javascript-tutorial","11":"tag-js-tutorial","12":"tag-web-design","13":"tag-web-designer","14":"tag-webgl"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3418"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3418\/revisions"}],"predecessor-version":[{"id":3420,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3418\/revisions\/3420"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3835"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3418"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}