Skip to content

Instantly share code, notes, and snippets.

@sokcuri
Last active July 19, 2017 00:21
Show Gist options
  • Save sokcuri/ca934e43468b45c55b787cbc404a8054 to your computer and use it in GitHub Desktop.
Save sokcuri/ca934e43468b45c55b787cbc404a8054 to your computer and use it in GitHub Desktop.
makeObject
function getCurrentSlide() {
return document.querySelector('.main_slides_lst li:not([style*="display: none"])');
}
function extractSlideNumber(slideElement) {
return Number.parseInt(slideElement.className.replace(/s([0-9]+)/, '$1'));
}
function getPagination(num) {
return document.querySelector(`.slides_pagination li:nth-child(${num}) a`);
}
function getFirstSlide() {
return document.querySelector('.main_slides_lst li:nth-child(1)');
}
function getLastSlide() {
return document.querySelector('.main_slides_lst li:nth-last-child(1)');
}
function getPrevSlide() {
let currentSlide = getCurrentSlide();
let previousSlide = currentSlide.previousElementSibling || getLastSlide();
return previousSlide;
}
function getNextSlide() {
let currentSlide = getCurrentSlide();
let nextSlide = currentSlide.nextElementSibling || getFirstSlide();
return nextSlide;
}
function getSlide(num) {
return document.querySelector(`.main_slides_lst li:nth-child(${num})`);
}
function slidesPrevBtnHandler() {
let currentSlide = getCurrentSlide();
let previousSlide = getPrevSlide();
transitionSlide(currentSlide, previousSlide);
}
function slidesNextBtnHandler() {
let currentSlide = getCurrentSlide();
let nextSlide = getNextSlide();
transitionSlide(currentSlide, nextSlide);
}
function slidesPaginationHandler(changeSlide) {
let currentSlide = getCurrentSlide();
transitionSlide(currentSlide, changeSlide);
}
function registerEvents() {
document.querySelector('.slides_prev').addEventListener('click', slidesPrevBtnHandler);
document.querySelector('.slides_next').addEventListener('click', slidesNextBtnHandler);
document.querySelectorAll('.slides_pagination li a').forEach((el, i) => {
let changeSlide = getSlide(i + 1);
el.addEventListener('click', slidesPaginationHandler.bind(this, changeSlide));
})
}
function transitionSlide(destSlide, sourceSlide) {
// 현재 슬라이드와 바뀔 슬라이드는 같을 수 없음
if (destSlide === sourceSlide)
return;
let TSI = transitionSlideInfo;
// 바뀌는 도중에는 슬라이드를 변경할 수 없게끔 함. 시각적 효과를 위해
if (TSI.progress !== null &&
TSI.progress > 15 && TSI.progress <= 100)
return;
if (!TSI.destSlide) {
destSlide = destSlide;
destSlide.style.zIndex = '0';
destSlide.style.opacity = 1;
let slideNumber = extractSlideNumber(destSlide);
getPagination(slideNumber).classList.remove('now');
}
if (TSI.sourceSlide) {
TSI.sourceSlide.style.display = 'none';
TSI.sourceSlide.style.zIndex = '0';
TSI.sourceSlide.style.opacity = '';
let slideNumber = extractSlideNumber(TSI.sourceSlide);
getPagination(slideNumber).classList.remove('now');
}
sourceSlide.style.zIndex = '50';
sourceSlide.style.opacity = 0;
sourceSlide.style.display = '';
let slideNumber = extractSlideNumber(sourceSlide);
getPagination(slideNumber).classList.add('now');
TSI.sourceSlide = sourceSlide;
if (!TSI.destSlide) {
TSI.destSlide = destSlide;
TSI.progress = 0;
}
}
let transitionSlideInfo = {
destSlide: null,
sourceSlide: null,
progress: null
}
// dest z-index = 0, src z-index = 50으로 만들고
// dest opacity 1 -> 0, src opacity 0 -> 1로 시작
function slideAnimation() {
let TSI = transitionSlideInfo;
if (TSI.progress !== null) {
console.log('progress')
let opacityAmount = TSI.progress / 100;
TSI.destSlide.style.opacity = 1 - opacityAmount;
TSI.sourceSlide.style.opacity = opacityAmount;
TSI.oldSourceSlide = TSI.sourceSlide;
TSI.progress+=3;
if (opacityAmount >= 1) {
TSI.destSlide.style.opacity = '';
TSI.destSlide.style.display = 'none';
TSI.sourceSlide.style.opacity = '';
TSI.destSlide = null;
TSI.sourceSlide = null;
TSI.progress = null;
}
}
//console.log(`Test - ${new Date()}`)
requestAnimationFrame(slideAnimation);
}
document.addEventListener('DOMContentLoaded', () => {
registerEvents();
slideAnimation();
let currentSlide = getCurrentSlide();
let currentSlideNumber = extractSlideNumber(currentSlide);
getPagination(currentSlideNumber).classList.add('now');
});
html, body {
margin: 0;
}
.main_slides_lst {
padding: 0;
}
.slides_pagination {
padding: 0;
}
ul.main_slides_lst li {
width: 100% !important;
background-position: center;
background-position-y: -15px;
}
.slides_container ul.slides_pagination li a:hover,
.slides_container ul.slides_pagination li a.now {
opacity: 1.0;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./script.js"></script>
<title>배달의민족 슬라이딩~</title>
<style>
.main_slides_wrap {
text-align: center;
max-width: 1920px;
margin: 0 auto;
}
.slides_container {
position: relative;
height: 420px;
}
.main_slides_wrap .bx-wrapper {
text-align: left;
}
.bx-wrapper {
margin: 0 auto;
background: #fff;
}
.slides_container ul.slides_pagination {
z-index: 55;
position: relative;
bottom: 30px;
width: 100%;
height: 0;
overflow: visible;
}
ul.main_slides_lst {
overflow: hidden;
height: 420px;
}
ul.main_slides_lst li a {
display: block;
width: 980px;
height: 420px;
margin: 0 auto;
overflow: hidden;
text-indent: -3000px;
}
.slides_container ul.slides_pagination {
z-index: 55;
position: relative;
bottom: 30px;
width: 100%;
height: 0;
overflow: visible;
}
.slides_container ul.slides_pagination li {
display: inline-block;
vertical-align: top;
padding: 0 3px;
}
.slides_container div.slides_navi {
width: 100%;
}
.slides_container div.slides_navi a.slides_prev {
right: 50%;
margin-right: 530px;
background-position: left center;
}
.slides_container div.slides_navi a {
z-index: 55;
display: block;
width: 60px;
height: 92px;
text-indent: -3000px;
overflow: hidden;
position: absolute;
top: 50%;
margin-top: -46px;
background: url(https://cdn.bmf.kr/web/common/main_slides_navi.png) no-repeat center center;
}
.slides_container div.slides_navi a.slides_next {
left: 50%;
margin-left: 530px;
background-position: right center;
}
.slides_container ul.slides_pagination li a {
display: block;
width: 10px;
height: 0;
padding: 10px 0 0 0;
overflow: hidden;
background: #FFF;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
-webkit-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
opacity: 0.6;
filter: alpha(opacity=60);
}
a {
text-decoration: none;
color: #333;
}
</style>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="wrap main_slides_wrap">
<h2 class="blind">메인배너</h2>
<div id="main-banners" class="slides_container">
<div class="bx-wrapper" style="max-width: 100%; margin: 0px auto;"><div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 420px;"><ul class="main_slides_lst" data-ref_id="main-banners" style="width: auto; position: relative;">
<li class="s1" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170710/1499664994146_bb0efd9c4cc6.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/list.php?cno=5070000&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★복날 기획전')">
복날 기획전 </a>
</li>
<li class="s2" style="background-image: url(&quot;https://cdn.bmf.kr/banner/temp_main_banner/170712/1499835605040_f75b0b095cd4.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/honsul-salon.php&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 딩고')">
딩고 </a>
</li>
<li class="s3" style="background-image: url(&quot;https://cdn.bmf.kr/banner/temp_main_banner/170713/1499921375267_482833ebbe80.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/brand/list.php?cno=30080624&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 무교동 명낙지')">
중독 주의! 매운맛의 정석 </a>
</li>
<li class="s4" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170712/1499849909533_711d52b30484.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/list.php?cno=5140000&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 다이어트 기획전')">
다이어트 기획전 </a>
</li>
<li class="s5" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170706/1499316420194_a34c62ecf722.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/detail.php?hash=HB9D1&amp;cno=5070000&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ [보양식1-1][집밥의완성] 닭 보양식 2종')">
[보양식1-1][집밥의완성] 닭 보양식 2종 </a>
</li>
<li class="s6" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170711/1499762216226_982b82666d8c.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/list.php?cno=5070000&amp;ga_banner=1&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '☆ [보양식2-1]소식')">
[보양식2-1]소식 </a>
</li>
<li class="s7" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170707/1499410281964_51fb39a533ad.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/side-dish/list.php?cno=30010900&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 3,300원 카테고리 오픈')">
3,300원 카테고리 오픈 </a>
</li>
<li class="s8" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170706/1499309555843_973ea4f49765.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/brand/list.php?cno=30080330&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '☆ [개금밀면] 앵콜 이벤트')">
[개금밀면] 앵콜 이벤트 </a>
</li>
<li class="s9" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170704/1499145124189_ee205d29b823.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/camping_2nd.php&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 캠핑기획전 2 ')">
캠핑기획전 2 </a>
</li>
<li class="s10" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170706/1499315550920_bcb3087568df.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/brand/list.php?cno=30080406&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 다담x옹가 론칭')">
다담x옹가 론칭 </a>
</li>
<li class="s11" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170706/1499319971682_30379a1464d9.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 50;" aria-hidden="false">
<a href="https://www.baeminfresh.com/board/?db=bfevent&amp;no=161&amp;mari_mode=view%40view&amp;cate=&amp;page=1&amp;listURL=https%3A%2F%2Fwww.baeminfresh.com%2Fboard%2F%3Fdb%3Dbfevent&amp;search=&amp;search_str=&amp;temp=&amp;pno=&amp;ono=&amp;ifr=&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '★ 옹가솜씨 쿠킹클래스 초대')">
옹가솜씨 쿠킹클래스 초대 </a>
</li>
<li class="s12" style="background-image: url(&quot;https://cdn.bmf.kr/banner/main_banner/170405/1491367917020_ca4958af233a.jpg&quot;); float: none; list-style: none; position: absolute; width: 1920px; z-index: 0; display: none;" aria-hidden="true">
<a href="https://www.baeminfresh.com/promotion/list.php?cno=5190000&amp;ga_banner=1&amp;ga_banner=1" onclick="ga('send', 'event', 'Product', 'Click Banner', '☆ 새로운 맘 ')">
새로운 맘 </a>
</li>
</ul></div></div>
<div class="slides_navi">
<a href="#" class="slides_prev" title="이전">이전</a>
<a href="#" class="slides_next" title="다음">다음</a>
</div>
<ul class="slides_pagination">
<li><a href="#" class="">0</a></li>
<li><a href="#" class="">1</a></li>
<li><a href="#" class="">2</a></li>
<li><a href="#" class="">3</a></li>
<li><a href="#" class="">4</a></li>
<li><a href="#" class="">5</a></li>
<li><a href="#" class="">6</a></li>
<li><a href="#" class="">7</a></li>
<li><a href="#" class="">8</a></li>
<li><a href="#" class="">9</a></li>
<li><a href="#" class="now">10</a></li>
<li><a href="#">11</a></li>
</ul>
</div>
</div>
</body>
</html>
var healthObj = {
showHealth : function() {
console.log(`${this.name}님, 오늘은 ${this.lastTime}동안 운동하셨네요!`);
}
}
function makeObject(prop, proto) {
Object.keys(prop).forEach(key => prop[key] = {value: prop[key]});
return Object.create(proto, prop)
}
let healthActions = [
{
name: '진수',
lastTime: '22:30'
},
{
name: '수완',
lastTime: '11:45'
},
{
name: '예린',
lastTime: '16:32'
},
{
name: '동욱',
lastTime: '45:50'
}
]
healthActions.forEach(action => makeObject(action, healthObj).showHealth());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment