Initial commit, completed project

This commit is contained in:
NetMan 2024-02-19 14:19:54 +01:00
commit 591fb64e36
5 changed files with 214 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
node_modules/
node_modules/*

41
README.md Normal file
View File

@ -0,0 +1,41 @@
# odin-simple-image-slider
Simple image slider - slides through images
My third npm package with the-odin-project
## usage
install: `npm i odin-simple-image-slider`
import in your project:
`import imageSlider from "odin-simple-image-slider"`
the default style is at `./node_modules/odin-simple-image-slider/style.css` and is imported at `index.js` in the same location
### syntax
`imageSlider(...images)`
### example
```
import imageSlider from "./simple-image-slider/index.js";
import Grass from './grass.jpg';
import Snow from './snow.jpg';
import Pc from './pc.jpg';
import Desert from './desert.jpg';
const app = document.querySelector('#app');
app.append(imageSlider(Grass, Snow, Pc, Desert));
```
it returns a dom node that you can append using append/appendChild
## license
ISC

85
index.js Normal file
View File

@ -0,0 +1,85 @@
import "./style.css"
import { createElement, ChevronRight, ChevronLeft, Dot } from 'lucide';
export default function imageSlider(...images) {
const imageSliderHTML = document.createElement("div");
imageSliderHTML.classList.add('image-slider');
const imageContainer = document.createElement("div");
imageContainer.classList.add("slider-image-container");
imageContainer.style.cssText = `
--translate-by: -0vw;
transform: translateX(var(--translate-by));
`;
images.forEach(image => {
const element = new Image();
element.src = image;
element.classList.add("slider-image");
imageContainer.append(element)
})
imageSliderHTML.append(imageContainer);
const left = createElement(ChevronLeft);
left.classList.add("slider-btn-left", "slider-btn");
const right = createElement(ChevronRight);
right.classList.add("slider-btn-right", "slider-btn");
imageSliderHTML.append(left, right);
function updateDots() {
let currentPic = -parseInt(imageContainer.style.getPropertyValue('--translate-by')) / 100;
dots.childNodes.forEach(dot => {
dot.style.stroke = "gray";
})
dots.children[currentPic].style.stroke = "white";
}
left.addEventListener("click", () => {
clearInterval(myInterval);
let currentPic = -parseInt(imageContainer.style.getPropertyValue('--translate-by')) / 100;
if (currentPic <= 0) {
imageContainer.style.setProperty('--translate-by', `-${images.length-1}00vw`)
} else {
imageContainer.style.setProperty('--translate-by', `-${currentPic - 1}00vw`)
}
updateDots();
myInterval = setInterval(next, 6000);
})
function next() {
clearInterval(myInterval);
let currentPic = -parseInt(imageContainer.style.getPropertyValue('--translate-by')) / 100;
if (currentPic + 1 > images.length - 1) {
imageContainer.style.setProperty('--translate-by', `-0vw`)
} else {
imageContainer.style.setProperty('--translate-by', `-${currentPic + 1}00vw`)
}
updateDots();
myInterval = setInterval(next, 6000);
}
right.addEventListener("click", next)
let myInterval = setInterval(next, 6000);
const dots = document.createElement("div");
dots.classList.add("dots-contain")
for (let i in images) {
const dot = createElement(Dot);
dot.classList.add("slider-dot");
dot.addEventListener("click", () => {
imageContainer.style.setProperty('--translate-by', `-${i}00vw`)
updateDots(i);
})
dots.append(dot);
}
imageSliderHTML.append(dots);
updateDots();
return imageSliderHTML;
}

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "odin-simple-image-slider",
"version": "1.0.0",
"description": "Simple image slider",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"image",
"slider",
"simple",
"js",
"html",
"css",
"theodinproject"
],
"author": "NetMan",
"license": "ISC",
"dependencies": {
"lucide": "^0.331.0"
},
"repository": {
"type": "git",
"url": "https://git.638626.xyz/tod/odin-simple-image-slider.git"
}
}

58
style.css Normal file
View File

@ -0,0 +1,58 @@
.image-slider {
overflow: hidden;
height: 100dvh;
width: 100vw;
}
.slider-image-container {
height: 100%;
min-width: 100%;
display: flex;
flex-direction: row;
transition: all 0.5s ease-in-out;
}
.slider-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
background: #000000aa;
border-radius: 20px;
padding: 2px;
stroke: white;
}
.slider-btn-left {
left: 10px;
}
.slider-btn-right {
right: 10px;
}
.slider-image {
display: block;
flex: 0 0 100vw;
max-width: 100vw;
height: auto !important;
object-fit: cover;
}
.dots-contain {
display: flex;
flex-direction: row;
justify-content: center;
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background: #000000aa;
border-radius: 20px;
}
.slider-dot {
cursor: pointer;
transform: scale(1.5);
stroke: white;
}