How we can create a 3D image slider using HTML, CSS, and JavaScript? Solution: See this 3D Perspective Image Slider Using CSS and JavaScript, Responsive 3D IMG Carousel Program.
Previously I have shared many slider programs, but this is a 3D perspective slider that reacts to the mouse cursor’s movement. Basically, a Perspective view is a view of a three-dimensional image that portrays height, width, and depth for a more realistic image or graphic. The slider’s image will rotate and scale on hover direction like a 3D perspective model.
Today you will learn to create Responsive 3D IMG Carousel Slider. Basically, there are 5 images in the slider, each slide item contains a heading, an image, and a text line. There are 3 blocks for heading, image, and text with perfect alignment. The heading block is aligned in the top-left corner of the image, and the text is aligned in the bottom-right corner. And in the background, you will see the current slider image with a blurred overlay.
This slider has customized mouse cursor with left and right arrow, the left cursor will reveal when you will navigate to the left slide of the image and the right will reveal on the right side. Also, the image, heading, a text box will perspectively rotate according to mouse direction, it will take a perfect rotate and scale. On the bottom side, you will see nav dots to identify the number of images and also you can jump to any image.
So, Today I am sharing 3D Perspective Image Slider Using CSS and JavaScript. There I have used CSS for styling and creating the 3D effect, and JavaScript for creating the functions. There I have not used jQuery or any other library, you can call it pure CSS and JavaScript program. Also, you can use this program on your website because it is a unique and complete program.
If you are thinking now how this 3D image slider actually is, then see the preview given below.
Preview Of Responsive 3D IMG Carousel
See this video preview to getting an idea of how this image carousel looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
3D Perspective Image Slider Using CSS and JavaScript Source Code
Before sharing source code, let’s talk about it. First I have created the layout using HTML elements like divs, paragraph, heading, and image, etc. I have created the image sections with 5 divs for 5 images and created other divs for placing text and headings. Also, I have created other elements for the navigation dots which are placed at the bottom of the slider.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS I gave basic values like size, position, margin, padding, etc. There I have used CSS variables for store and call values easily. I have used CSS transform-style: preserve-3d command to creating a 3D effect. Used CSS @media query to creating responsive elements.
JavaScript handling here there functions of the program, like next and previous navigation, 3D tilt, dot nav, mouse arrow cursor, etc. There I have used JS document.getElementById fetch or calling the elements. The JS part is little complicated, you must have good knowledge of JavaScipt to understand the code.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second file for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>3D Perspective Slider | Webdevtrick.com</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="style.css"> </head> <body> <div class="slider" id="slider" style="--img-prev:url(https://webdevtrick.com/wp-content/uploads/programming.jpg);"> <div class="contents" id="slider-content"> <div class="imageLIST"> <div class="imageITEM imageITEM--active" data-id="1"><img src="https://webdevtrick.com/wp-content/uploads/programming.jpg"/></div> <div class="imageITEM" data-id="2"><img src="https://webdevtrick.com/wp-content/uploads/gadget.jpg"/></div> <div class="imageITEM" data-id="3"><img src="https://webdevtrick.com/wp-content/uploads/design.jpg"/></div> <div class="imageITEM" data-id="4"><img src="https://webdevtrick.com/wp-content/uploads/cons.jpg"/></div> <div class="imageITEM" data-id="5"><img src="https://webdevtrick.com/wp-content/uploads/auto.jpg"/></div> </div> <div class="slider__text"> <div class="textITEMS textITEMS--active" data-id="1"> <div class="headings"> <h3>Coding</h3> </div> <div class="txtINFO"> <p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”</p> </div> </div> <div class="textITEMS" data-id="2"> <div class="headings"> <h3>Drone</h3> </div> <div class="txtINFO"> <p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”</p> </div> </div> <div class="textITEMS" data-id="3"> <div class="headings"> <h3>Design</h3> </div> <div class="txtINFO"> <p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”</p> </div> </div> <div class="textITEMS" data-id="4"> <div class="headings"> <h3>Buildings</h3> </div> <div class="txtINFO"> <p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”</p> </div> </div> <div class="textITEMS" data-id="5"> <div class="headings"> <h3>AutoM</h3> </div> <div class="txtINFO"> <p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”</p> </div> </div> </div> </div> <div class="navigations"> <div class="navARROWS"> <div class="navARROW navARROW--left" id="left">to left</div> <div class="navARROW navARROW--right" id="right">to right</div> </div> <div class="bottomDOTS" id="slider-dots"> <div class="bottomDOT bottomDOT--active" data-id="1"></div> <div class="bottomDOT" data-id="2"></div> <div class="bottomDOT" data-id="3"></div> <div class="bottomDOT" data-id="4"></div> <div class="bottomDOT" data-id="5"></div> </div> </div> </div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import url("https://fonts.googleapis.com/css?family=Lora:700"); @import url("https://fonts.googleapis.com/css?family=Open+Sans"); :root { --z-distance: 8.519vw; --from-left: 1; --mobile-bkp: 650px; } *, *::before, *::after { box-sizing: border-box; } body { min-height: 100vh; margin: 0; padding: 0; overflow: hidden; font-family: Lora, serif; font-size: calc(14px + .3vw); } .slider { width: 100vw; height: 100vh; display: flex; -webkit-perspective: 1000px; perspective: 1000px; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } .slider::before, .slider::after { content: ''; left: -1vw; top: -1vh; display: block; position: absolute; width: 102vw; height: 102vh; background-position: center; background-size: cover; will-change: opacity; z-index: -1; box-shadow: 0 0 0 50vmax rgba(0, 0, 0, 0.7) inset; } .slider::before { background-image: var(--img-prev); } .slider::after { transition: opacity 0.7s; opacity: 0; background-image: var(--img-next); } .slider--bg-next::after { opacity: 1; } .contents { margin: auto; width: 65vw; height: 32.5vw; max-height: 60vh; will-change: transform; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; pointer-events: none; -webkit-transform: translateZ(var(--z-distance)); transform: translateZ(var(--z-distance)); } .imageLIST { overflow: hidden; position: absolute; width: 100%; height: 100%; z-index: 0; box-shadow: 0 0 5em #000; } .imageITEM { position: absolute; top: 0; left: 0; height: 100%; width: 100%; will-change: transform; transition-timing-function: ease-in; visibility: hidden; } .imageITEM img { display: block; position: relative; left: -1em; top: -1em; width: calc(100% + 1em * 2); height: calc(100% + 1em * 2); -o-object-fit: cover; object-fit: cover; will-change: transform; } .imageITEM--active { z-index: 20; visibility: visible; } .imageITEM--subactive { z-index: 15; visibility: visible; } .imageITEM--next { -webkit-transform: translateX(100%); transform: translateX(100%); } .imageITEM--prev { -webkit-transform: translateX(-100%); transform: translateX(-100%); } .imageITEM--transit { transition: opacity 0.7s, -webkit-transform 0.7s; transition: transform 0.7s, opacity 0.7s; transition: transform 0.7s, opacity 0.7s, -webkit-transform 0.7s; } .slider__text { position: relative; height: 100%; } .textITEMS { position: absolute; width: 100%; height: 100%; padding: 0.5em; -webkit-perspective: 1000px; perspective: 1000px; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } .textITEMS > * { overflow: hidden; position: absolute; } .textITEMS h3, .textITEMS p { transition: -webkit-transform 0.35s ease-out; transition: transform 0.35s ease-out; transition: transform 0.35s ease-out, -webkit-transform 0.35s ease-out; line-height: 1.5; overflow: hidden; } .textITEMS h3 { background-color: rgba(255, 255, 255, 0.5); } .textITEMS p { font-family: 'Open Sans', sans-serif; padding: 1em; color: white; text-align: center; background-color: rgba(0, 0, 0, 0.5); } .textITEMS h3::before, .textITEMS p::before { content: ''; position: absolute; top: 0; left: 0; width: 105%; height: 100%; -webkit-transform: translateX(0); transform: translateX(0); transition: -webkit-transform 0.35s ease-out 0.28s; transition: transform 0.35s ease-out 0.28s; transition: transform 0.35s ease-out 0.28s, -webkit-transform 0.35s ease-out 0.28s; } .textITEMS h3::before { background-color: #000; } .textITEMS p::before { background-color: #fff; } .textITEMS h3 { margin: 0; font-size: 3.5em; padding: 0 .3em; position: relative; font-weight: 700; -webkit-transform: translateX(-100%); transform: translateX(-100%); } .textITEMS p { margin: 0; -webkit-transform: translateX(100%); transform: translateX(100%); } .headings { top: -0.5em; -webkit-transform: translateZ(3em); transform: translateZ(3em); } .txtINFO { bottom: 0; right: 0; max-width: 75%; min-width: -webkit-min-content; min-width: -moz-min-content; min-width: min-content; -webkit-transform: translateZ(2em); transform: translateZ(2em); } .textITEMS--active h3, .textITEMS--active p { -webkit-transform: translateX(0); transform: translateX(0); } .textITEMS--active h3::before { -webkit-transform: translateX(102%); transform: translateX(102%); } .textITEMS--active p::before { -webkit-transform: translateX(-102%); transform: translateX(-102%); } .textITEMS--backwards h3::before, .textITEMS--backwards p::before { transition: -webkit-transform 0.35s ease-in; transition: transform 0.35s ease-in; transition: transform 0.35s ease-in, -webkit-transform 0.35s ease-in; } .textITEMS--backwards h3, .textITEMS--backwards p { transition: -webkit-transform 0.35s ease-in 0.35s; transition: transform 0.35s ease-in 0.35s; transition: transform 0.35s ease-in 0.35s, -webkit-transform 0.35s ease-in 0.35s; } .navigations { position: absolute; left: 0; top: 0; width: 100%; height: 100%; text-align: center; } .navARROWS { display: flex; justify-content: space-between; width: 100%; position: absolute; top: 0; left: 0; } .navARROW { height: 100vh; width: 50vw; text-indent: -9999px; white-space: nowrap; } .navARROW--left { --arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 4 4'%3E %3Cpolyline points='3 1 1 2 3 3' stroke='white' stroke-width='.3' stroke-opacity='.5' fill='none'%3E%3C/polyline%3E %3C/svg%3E"); cursor: var(--arrow) 40 40, auto; } .navARROW--right { --arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 4 4'%3E %3Cpolyline points='1 1 3 2 1 3' stroke='white' stroke-width='.3' stroke-opacity='.5' fill='none'%3E%3C/polyline%3E %3C/svg%3E"); cursor: var(--arrow) 40 40, auto; } .bottomDOTS { margin-top: 88vh; display: inline-flex; position: relative; padding: 1em; pointer-events: none; } .bottomDOTS::before { content: ''; position: absolute; left: calc(1em + 1em + 2px); top: calc(1em + 2px); width: calc(1em - 2px * 2); height: calc(1em / 2 - 2px * 2); background-color: rgba(255, 255, 255, 0.9); transition: -webkit-transform 0.7s ease-out; transition: transform 0.7s ease-out; transition: transform 0.7s ease-out, -webkit-transform 0.7s ease-out; -webkit-transform: translateX(calc((1em + 1em * 2) * (var(--from-left) - 1))); transform: translateX(calc((1em + 1em * 2) * (var(--from-left) - 1))); } .bottomDOT { margin: 0 1em; width: 1em; height: 0.5em; border: 2px solid rgba(255, 255, 255, 0.5); cursor: crosshair; pointer-events: all; display: inline-block; } .bottomDOT:hover { border-color: rgba(255, 255, 255, 0.7); } .bottomDOT:active { border-color: rgba(255, 255, 255, 0.5); } @media only screen and (max-width: 650px) { .slider::before, .slider::after { display: none; } .contents { width: 100vw; height: 100vh; max-height: 100vh; } .txtINFO { bottom: 50%; left: 50%; -webkit-transform: translate(-50%, 50%); transform: translate(-50%, 50%); } .txtINFO p { padding: 1em .8em; } .headings { top: 5vh; left: 10vw; -webkit-transform: translateZ(0); transform: translateZ(0); } .headings h3 { font-size: 2.5em; } .bottomDOTS { background-color: rgba(0, 0, 0, 0.3); } .navARROW { width: 10vw; position: relative; cursor: auto; } .navARROW:active { -webkit-filter: brightness(0.5); filter: brightness(0.5); } .navARROW::before { content: ''; background-image: var(--arrow); background-size: cover; width: 8vw; height: 8vw; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .navARROW--left { background-image: linear-gradient(to right, rgba(0, 0, 0, 0.7) 0, transparent 100%); } .navARROW--left:active { background-image: linear-gradient(to right, rgba(0, 0, 0, 0.9) 0, transparent 100%); } .navARROW--right { background-image: linear-gradient(to left, rgba(0, 0, 0, 0.7) 0, transparent 100%); } .navARROW--right:active { background-image: linear-gradient(to left, rgba(0, 0, 0, 0.9) 0, transparent 100%); } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
// Code By Webdevtrick ( https://webdevtrick.com ) function lerp({ x, y }, { x: targetX, y: targetY }) { const fraction = 0.1; x += (targetX - x) * fraction; y += (targetY - y) * fraction; return { x, y }; } class Slider { constructor(el) { const imgClass = this.IMG_CLASS = 'imageITEM'; const textClass = this.TEXT_CLASS = 'textITEMS'; const activeImgClass = this.ACTIVE_IMG_CLASS = `${imgClass}--active`; const activeTextClass = this.ACTIVE_TEXT_CLASS = `${textClass}--active`; this.el = el; this.contentEl = document.getElementById('slider-content'); this.onMouseMove = this.onMouseMove.bind(this); // taking advantage of the live nature of 'getElement...' methods this.activeImg = el.getElementsByClassName(activeImgClass); this.activeText = el.getElementsByClassName(activeTextClass); this.images = el.getElementsByTagName('img'); document.getElementById('slider-dots'). addEventListener('click', this.onDotClick.bind(this)); document.getElementById('left'). addEventListener('click', this.prev.bind(this)); document.getElementById('right'). addEventListener('click', this.next.bind(this)); window.addEventListener('resize', this.onResize.bind(this)); this.onResize(); this.length = this.images.length; this.lastX = this.lastY = this.targetX = this.targetY = 0; } onResize() { const htmlStyles = getComputedStyle(document.documentElement); const mobileBreakpoint = htmlStyles.getPropertyValue('--mobile-bkp'); const isMobile = this.isMobile = matchMedia( `only screen and (max-width: ${mobileBreakpoint})`). matches; this.halfWidth = innerWidth / 2; this.halfHeight = innerHeight / 2; this.zDistance = htmlStyles.getPropertyValue('--z-distance'); if (!isMobile && !this.mouseWatched) { this.mouseWatched = true; this.el.addEventListener('mousemove', this.onMouseMove); this.el.style.setProperty( '--img-prev', `url(${this.images[+this.activeImg[0].dataset.id - 1].src})`); this.contentEl.style.setProperty('transform', `translateZ(${this.zDistance})`); } else if (isMobile && this.mouseWatched) { this.mouseWatched = false; this.el.removeEventListener('mousemove', this.onMouseMove); this.contentEl.style.setProperty('transform', 'none'); } } getMouseCoefficients({ pageX, pageY } = {}) { const halfWidth = this.halfWidth; const halfHeight = this.halfHeight; const xCoeff = ((pageX || this.targetX) - halfWidth) / halfWidth; const yCoeff = (halfHeight - (pageY || this.targetY)) / halfHeight; return { xCoeff, yCoeff }; } onMouseMove({ pageX, pageY }) { this.targetX = pageX; this.targetY = pageY; if (!this.animationRunning) { this.animationRunning = true; this.runAnimation(); } } runAnimation() { if (this.animationStopped) { this.animationRunning = false; return; } const maxX = 10; const maxY = 10; const newPos = lerp({ x: this.lastX, y: this.lastY }, { x: this.targetX, y: this.targetY }); const { xCoeff, yCoeff } = this.getMouseCoefficients({ pageX: newPos.x, pageY: newPos.y }); this.lastX = newPos.x; this.lastY = newPos.y; this.positionImage({ xCoeff, yCoeff }); this.contentEl.style.setProperty('transform', ` translateZ(${this.zDistance}) rotateX(${maxY * yCoeff}deg) rotateY(${maxX * xCoeff}deg) `); if (this.reachedFinalPoint) { this.animationRunning = false; } else { requestAnimationFrame(this.runAnimation.bind(this)); } } get reachedFinalPoint() { const lastX = ~~this.lastX; const lastY = ~~this.lastY; const targetX = this.targetX; const targetY = this.targetY; return (lastX == targetX || lastX - 1 == targetX || lastX + 1 == targetX) && ( lastY == targetY || lastY - 1 == targetY || lastY + 1 == targetY); } positionImage({ xCoeff, yCoeff }) { const maxImgOffset = 1; const currentImage = this.activeImg[0].children[0]; currentImage.style.setProperty('transform', ` translateX(${maxImgOffset * -xCoeff}em) translateY(${maxImgOffset * yCoeff}em) `); } onDotClick({ target }) { if (this.inTransit) return; const dot = target.closest('.bottomDOT'); if (!dot) return; const nextId = dot.dataset.id; const currentId = this.activeImg[0].dataset.id; if (currentId == nextId) return; this.startTransition(nextId); } transitionItem(nextId) { function onImageTransitionEnd(e) { e.stopPropagation(); nextImg.classList.remove(transitClass); self.inTransit = false; this.className = imgClass; this.removeEventListener('transitionend', onImageTransitionEnd); } const self = this; const el = this.el; const currentImg = this.activeImg[0]; const currentId = currentImg.dataset.id; const imgClass = this.IMG_CLASS; const textClass = this.TEXT_CLASS; const activeImgClass = this.ACTIVE_IMG_CLASS; const activeTextClass = this.ACTIVE_TEXT_CLASS; const subActiveClass = `${imgClass}--subactive`; const transitClass = `${imgClass}--transit`; const nextImg = el.querySelector(`.${imgClass}[data-id='${nextId}']`); const nextText = el.querySelector(`.${textClass}[data-id='${nextId}']`); let outClass = ''; let inClass = ''; this.animationStopped = true; nextText.classList.add(activeTextClass); el.style.setProperty('--from-left', nextId); currentImg.classList.remove(activeImgClass); currentImg.classList.add(subActiveClass); if (currentId < nextId) { outClass = `${imgClass}--next`; inClass = `${imgClass}--prev`; } else { outClass = `${imgClass}--prev`; inClass = `${imgClass}--next`; } nextImg.classList.add(outClass); requestAnimationFrame(() => { nextImg.classList.add(transitClass, activeImgClass); nextImg.classList.remove(outClass); this.animationStopped = false; this.positionImage(this.getMouseCoefficients()); currentImg.classList.add(transitClass, inClass); currentImg.addEventListener('transitionend', onImageTransitionEnd); }); if (!this.isMobile) this.switchBackgroundImage(nextId); } startTransition(nextId) { function onTextTransitionEnd(e) { if (!e.pseudoElement) { e.stopPropagation(); requestAnimationFrame(() => { self.transitionItem(nextId); }); this.removeEventListener('transitionend', onTextTransitionEnd); } } if (this.inTransit) return; const activeText = this.activeText[0]; const backwardsClass = `${this.TEXT_CLASS}--backwards`; const self = this; this.inTransit = true; activeText.classList.add(backwardsClass); activeText.classList.remove(this.ACTIVE_TEXT_CLASS); activeText.addEventListener('transitionend', onTextTransitionEnd); requestAnimationFrame(() => { activeText.classList.remove(backwardsClass); }); } next() { if (this.inTransit) return; let nextId = +this.activeImg[0].dataset.id + 1; if (nextId > this.length) nextId = 1; this.startTransition(nextId); } prev() { if (this.inTransit) return; let nextId = +this.activeImg[0].dataset.id - 1; if (nextId < 1) nextId = this.length; this.startTransition(nextId); } switchBackgroundImage(nextId) { function onBackgroundTransitionEnd(e) { if (e.target === this) { this.style.setProperty('--img-prev', imageUrl); this.classList.remove(bgClass); this.removeEventListener('transitionend', onBackgroundTransitionEnd); } } const bgClass = 'slider--bg-next'; const el = this.el; const imageUrl = `url(${this.images[+nextId - 1].src})`; el.style.setProperty('--img-next', imageUrl); el.addEventListener('transitionend', onBackgroundTransitionEnd); el.classList.add(bgClass); }} const sliderEl = document.getElementById('slider'); const slider = new Slider(sliderEl); // ------------------ Demo stuff ------------------------ // let timer = 0; function autoSlide() { requestAnimationFrame(() => { slider.next(); }); timer = setTimeout(autoSlide, 5000); } function stopAutoSlide() { clearTimeout(timer); this.removeEventListener('touchstart', stopAutoSlide); this.removeEventListener('mousemove', stopAutoSlide); } sliderEl.addEventListener('mousemove', stopAutoSlide); sliderEl.addEventListener('touchstart', stopAutoSlide); timer = setTimeout(autoSlide, 2000); |
That’s It. Now you have successfully created 3D Perspective Image Slider Using CSS and JavaScript, 3D IMG Carousel. If you have any doubt or questions comment down below.
Thanks For Visiting, Keep Visiting.
Wow. Your source code has nothing for the javascript. Can you send that along?