HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="wraper">
<div class="content" id="slide1">
<div class="hyun-slider-wrapper">
<ul class="hyun-slider">
<li class="hyun-slider-item"><span>1</span></li>
<li class="hyun-slider-item"><span>2</span></li>
<li class="hyun-slider-item"><span>3</span></li>
<li class="hyun-slider-item"><span>4</span></li>
<li class="hyun-slider-item"><span>5</span></li>
<li class="hyun-slider-item"><span>6</span></li>
</ul>
</div>
</div>
</div>
<div class="wraper">
<div class="content" id="slide2">
<div class="hyun-slider-wrapper">
<ul class="hyun-slider">
<li class="hyun-slider-item"><span>1</span></li>
<li class="hyun-slider-item"><span>2</span></li>
<li class="hyun-slider-item"><span>1</span></li>
<li class="hyun-slider-item"><span>3</span></li>
<li class="hyun-slider-item"><span>4</span></li>
<li class="hyun-slider-item"><span>5</span></li>
</ul>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
list-style: none;
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none
}
.wraper {
display: grid;
place-items: center;
width: 100%;
height: 400px;
}
.wraper .content{
position: relative;
margin: 0 auto;
/* width: 1100px; */
height: 300px;
}
.content .hyun-slider-wrapper {
position: relative;
z-index: 2;
height: 100%;
margin: 0 auto;
overflow: hidden;
}
.hyun-slider-wrapper .hyun-slider {
/* display: flex;
gap: 20px; */
position: relative;
left: 0;
}
.wraper .hyun-slider .hyun-slider-item {
display: grid;
place-items: center;
height: 300px;
background-color: #ff9f44d2;
border-radius: 25px;
font-size: 80px;
font-weight: bold;
color: #fff;
}
.wraper .hyun-slider .hyun-slider-item.clone{
/* background-color: #111111d2; */
}
.wraper .hyun-slider .hyun-slider-item img{
display: none;
}
/* 버튼 */
.wraper .content .btn_wrap{
display: flex;
justify-content: space-between;
width: 100%;
position: absolute;
z-index: 3;
top: 50%;
transform: translateY(-50%);
}
.wraper .btn_wrap button{
border: 0;
cursor: pointer;
background-color: #fff;
}
.wraper .btn_wrap button img{
width: 50px;
opacity: .05;
transition: opacity 0.4s ease-out;
}
.wraper .btn_wrap img:hover{
opacity: 0.2;
}
.wraper .btn_wrap .next img{ transform: rotate(-180deg);}
.content.showbutton .btn_wrap{
display: flex;
}
.content.showbutton .btn_wrap .perv{
position: absolute;
left: 50px;
}
.content.showbutton .btn_wrap .next{}
/* 페이지네이션 스타일 */
.hyun_pagination{
display: flex;
gap: 6px;
position: absolute;
left: 50%;
bottom: -45px;
transform: translateX(-50%);
}
.hyun_pagination li{
color: #dfdfdf88;
cursor: pointer;
font-size: 30px;
}
.hyun_pagination > li.active {
color: #7a7a7a88;
}
/* 재생,정지버튼 */
.wraper .hyun-play-button {
position: absolute;
right: 50px;
bottom: -30px;
}
.hyun-play-button button{
display: block;
position: relative;
z-index: 2;
width: 50px;
height: 20px;
border-radius: 30px;
background-color: #fff;
box-shadow: 0 0px 5px 2px rgba(0,0,0,0.1);
cursor: pointer;
border: 0;
}
.hyun-play-button button span {
width: 15px;
height: 15px;
position: absolute;
top: 50%;
left: 4px;
transform: translateY(-50%);
border-radius: 50%;
background: #dfdfdf88;
transition: left 0.2s ease-in;
}
.hyun-play-button button.stop span {
left: calc(100% - 19px);
background: #7a7a7a88;
}
function HyunSlider(param){
if(!param.id) {
console.error("슬라이드 아이디는 필수입니다.");
return
}
this.slideContent = document.querySelector(param.id)
this.sliderWrapper = this.slideContent.querySelector('.hyun-slider-wrapper')
this.sliderContainer = this.slideContent.querySelector('.hyun-slider')
this.slides = this.slideContent.querySelectorAll('.hyun-slider-item')
this.slideCount = this.slides.length;
this.slideWidth = param.width || 300
this.slideMargin = param.margin || 15
this.currentIndex = 0
this.loop = param.loop || false
this.pagination = param.pagination || false
this.viewslide = param.view || 3
// this.viewslide = Math.min(param.view || this.slideCount, this.slideCount);
this.speed = param.speed || 1
this.autoplay = param.autoplay
this.palybutton = param.palybutton
let slideWidth = this.slideWidth + 'px';
this.slides.forEach(slide => {
slide.style.width = slideWidth;
});
//3개이하 일때 슬라이드 생성안되도록
if (this.slideCount > 3) {
this.controlButton();
this.makeClone();
this.newPagination();
this.paginationActive();
this.drag();
this.autoPlay();
}
this.sliderContainer.style.display = 'flex';
this.sliderContainer.style.gap = this.slideMargin + 'px'
this.slideContent.style.width = (this.slideWidth + this.slideMargin) * this.viewslide + 120 + 'px';
this.sliderWrapper.style.width = (this.slideWidth + this.slideMargin) * this.viewslide - this.slideMargin + 'px';
}
HyunSlider.prototype.setInitialPos = function(){
//복사전 넓이
const initialTranslateValue = -((this.slideWidth + this.slideMargin) * this.slideCount);
this.sliderContainer.style.transform = `translateX(${initialTranslateValue}px)`;
}
HyunSlider.prototype.updateWidth = function(){
//복사후 넓이
const newWidth = (this.slideWidth + this.slideMargin) * (this.slideCount * 3) + 'px';
this.sliderContainer.style.width = newWidth;
}
HyunSlider.prototype.makeClone = function(){
if (this.loop) {
const _this = this
this.updateWidth(); //복사 후 전체 넓이값
this.setInitialPos();
// 뒤에 복사본
for (let i = 0; i < this.slideCount; i++) {
let cloneSlide = this.slides[i].cloneNode(true);
cloneSlide.classList.add('clone');
this.sliderContainer.appendChild(cloneSlide);
}
// 앞에 복사
for (let i = this.slideCount - 1; i >= 0; i--) {
let cloneSlide = this.slides[i].cloneNode(true);
cloneSlide.classList.add('clone');
this.sliderContainer.prepend(cloneSlide);
}
} else {
this.sliderContainer.style.width = (this.slideWidth + this.slideMargin) * this.slideCount - this.slideMargin + 'px';
//루프가 아닐 경우 셋팅값
}
}
HyunSlider.prototype.goToslide = function(idx){
const _this = this
this.sliderContainer.style.transition = `left 0.1s ease-out`;
this.sliderContainer.style.left = -idx * (this.slideWidth + this.slideMargin) + 'px';
this.currentIndex = idx;
if (this.currentIndex == this.slideCount || this.currentIndex == -this.slideCount) {
setTimeout(function () {
_this.sliderContainer.style.transition = "none"
const initialTranslateValue = -((this.slideWidth + this.slideMargin) * this.slideCount);
_this.sliderContainer.style.left = '0px';
_this.currentIndex = 0;
}, 100);
}
}
HyunSlider.prototype.controlButton = function(){
const buttonWrap = document.createElement('div');
buttonWrap.className = "btn_wrap";
this.slideContent.appendChild(buttonWrap);
buttonWrap.innerHTML +=
`<button class="prev"><img src="https://i.ibb.co/GdybzNY/btn.png" alt="btn" border="0"></button>
<button class="next"><img src="https://i.ibb.co/GdybzNY/btn.png" alt="btn" border="0"></button>`;
const navPrev= this.slideContent.querySelector('.prev')
const navNext= this.slideContent.querySelector('.next')
this.slideContent.classList.add('showbutton')
const _this = this
navNext.addEventListener('click', function () {
if (_this.loop) {
_this.goToslide(_this.currentIndex + 1);
_this.paginationActive();
} else {
if(_this.currentIndex < _this.slideCount -3) {
_this.goToslide(_this.currentIndex + 1);
_this.paginationActive();
}
}
});
navPrev.addEventListener('click', function () {
if (_this.loop) {
_this.goToslide(_this.currentIndex - 1);
_this.paginationActive();
} else {
if (_this.currentIndex > 0) {
_this.goToslide(_this.currentIndex - 1);
_this.paginationActive();
}
}
});
}
HyunSlider.prototype.newPagination = function(){
const _this = this
if(this.pagination){
const pagination = document.createElement('div');
pagination.className = "hyun_pagination";
this.slideContent.appendChild(pagination);
pagination.classList.add('showpagination')
const count = _this.loop ? this.slideCount : this.slideCount - 2
for (let i = 0; i < count; i++) {
if (i === 0) {
pagination.innerHTML += `<li class="active">•</li>`;
} else {
pagination.innerHTML += `<li>•</li>`;
}
}
this.paginationActive();
}
}
HyunSlider.prototype.paginationActive = function(){
const _this = this
const paginationItems = this.slideContent.querySelectorAll(".hyun_pagination > li");
const positiveIndex = (this.currentIndex + this.slideCount) % this.slideCount;
//무한 슬라이드를 구현하기 위해서 currentIndex + slideCount로
//양수로 값을 얻고 slideCount를 나누기 때문에 양수의 값을 얻어
//정상적인 인덱스에서 활동
if (paginationItems.length > 0) {
paginationItems.forEach((i) => {
i.classList.remove("active")
});
const pageIndex = _this.loop ? positiveIndex : _this.currentIndex
paginationItems[pageIndex].classList.add("active");
console.log(pageIndex);
paginationItems.forEach((item, index) => {
item.addEventListener('click', function () {
_this.goToslide(index);
_this.paginationActive();
item.classList.add("active");
});
});
}
}
HyunSlider.prototype.drag = function(){
const _this = this
let startPoint = 0;
let endPoint = 0;
this.sliderWrapper.addEventListener("mousedown", (e) => {
startPoint = e.pageX;
});
this.sliderWrapper.addEventListener("mouseup", (e) => {
endPoint = e.pageX;
if (startPoint < endPoint) {
if (this.loop) {
//왼쪽에서 오른쪽 왼쪽이 오른쪽보다 작을 경우
_this.goToslide(_this.currentIndex - 1);
_this.paginationActive();
} else {
if (_this.currentIndex > 0) {
_this.goToslide(_this.currentIndex - 1);
_this.paginationActive();
}
}
} else if (startPoint > endPoint) {
if (this.loop) {
//오른쪽에서 왼쪽 왼쪽이 오른쪽보다 클 경우
_this.goToslide(_this.currentIndex + 1);
_this.paginationActive();
} else {
// 마우스가 왼쪽으로 드래그 된 경우
if(_this.currentIndex < _this.slideCount -3) {
_this.goToslide(_this.currentIndex + 1);
_this.paginationActive();
}
}
}
});
}
HyunSlider.prototype.autoPlay = function(){
if (this.loop) {
if(this.autoplay){
const _this = this
_this.timer = undefined;
clearInterval(_this.timer);
function autoSlide(){
if(_this.timer == undefined) {
_this.timer = setInterval(function(){
_this.goToslide(_this.currentIndex + 1);
_this.paginationActive();
}, _this.speed * 1000);
}
}
autoSlide();
function stopSlide(){
clearInterval(_this.timer);
_this.timer = undefined;
}
if(this.palybutton) {
const newButtonwrap = document.createElement('div');
newButtonwrap.className = "hyun-play-button";
this.slideContent.appendChild(newButtonwrap);
newButtonwrap.innerHTML +=
'<button class="toggle"><span></span></button>';
const toggleBtn = this.slideContent.querySelector('.toggle');
toggleBtn.classList.add('show')
toggleBtn.addEventListener("click", () => {
toggleBtn.classList.toggle('stop');
if (toggleBtn.classList.contains('stop')) {
clearInterval(_this.timer);
_this.timer = undefined;
} else {
autoSlide();
}
});
this.slideContent.addEventListener('mouseleave',function(){
if (!toggleBtn.classList.contains('stop')) {
autoSlide();
}
});
}
this.slideContent.addEventListener('mouseenter',function(){
stopSlide();
});
}
}
}
//옵션
//슬라이드 갯수만큼 html에서
//<li class="hyun-slider-item"><span>0</span</li>
// 을 생성해주세요.
//loop : 복제
//view : 보여지는 갯수
//width : 슬라이드아이템 크기조절
//margin : 슬라이드 사이간격
//pagination : 페이지네이션 유무
//autoplay : 재생 (전제조건 loop일경우)
//playbutton : 정지,재생 버튼
//speed : 보여지는 속도 조절 (전제조건 loop일경우)
const slider1 = new HyunSlider({
id: '#slide1',
loop: true,
width: 200,
pagination: true,
autoplay: true,
palybutton: false,
margin: 10,
view: 3,
speed: 1,
})
const slider2 = new HyunSlider({
id: '#slide2',
loop: true,
width: 300,
pagination: false,
autoplay: false,
palybutton: false,
margin: 10,
view: 3,
speed: 1,
})
Also see: Tab Triggers