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.
<ul class="acc-menu" id="accordionMenu1">
<li data-extension="close">
<div class="main-title"><span class="folder"> </span><a>menu1</a></div>
<ul class="sub">
<li><a>sub1-1</a></li>
<li><a>sub1-2</a></li>
<li><a>sub1-3</a></li>
</ul>
</li>
<li data-extension="close">
<div class="main-title"><span class="folder"> </span><a>menu2</a></div>
<ul class="sub">
<li><a>sub2-1</a></li>
<li><a>sub2-2</a></li>
</ul>
</li>
<li data-extension="close">
<div class="main-title"><span class="folder"> </span><a>menu3</a> </div>
<ul class="sub">
<li><a>sub3-1</a></li>
<li><a>sub3-2</a></li>
<li><a>sub3-3</a></li>
<li><a>sub3-4</a></li>
</ul>
</li>
<li>
<div class="main-title"><span class="folder"> </span><a>menu4</a></div>
</li>
* {
padding: 0;
margin: 0;
}
body {
font-size: 9pt;
}
#accordionMenu1 {
margin: 10px auto;
width: 300px;
}
.acc-menu {
position: relative;
list-style: none;
border: 4px solid #eee;
}
.acc-menu >li {
overflow: hidden;
border: 1px solid #000;
cursor: pointer;
}
.acc-menu li .main-title {
background-color: #fff;
padding: 10px;
vertical-align: middle;
position: relative;
z-index: 100;
}
.acc-menu li .main-title a {
display: inline-block;
height: 20px;
line-height: 20px;
text-align: center;
vertical-align: middle;
}
.acc-menu li .main-title .folder {
display: inline-block;
width: 20px;
height: 20px;
overflow: hidden;
line-height: 20px;
text-align: center;
vertical-align: middle;
margin-right: 5px;
text-indent: -100px;
background-image: url("http://cfile3.uf.tistory.com/image/24185E335762538E2AE110");
/* 기본은 .close */
background-position: 0 -40px;
}
.acc-menu li .main-title .folder.empty {
background-position: 0 0;
}
.acc-menu li .main-title .folder.open {
background-position: 0 -20px;
}
.acc-menu li .main-title .folder.close {
background-position: 0 -40px;
}
.acc-menu li ul.sub {
list-style: none;
margin-left: 30px;
margin-bottom: 20px;
z-index: 90;
position: relative;
}
.acc-menu li ul.sub.open {
display: block;
}
.acc-menu li ul.sub.hide {
display: none;
}
.acc-menu li ul.sub li {
height: 20px;
padding: 10px;
line-height: 20px;
vertical-align: middle;
cursor: pointer;
}
.acc-menu li ul.sub li.select {
background-color: #bddeea;
}
(function($) {
// 플러그인 정의
$.fn.accordionMenu = function() {
this.each(function(index) {
var $this = $(this);
var accMenu = new AccordionMenu($this);
// n번째 인덱스에 해당하는 메뉴를 선택하는 기능(selectMenu())을
// 사용하기 위해 accordionMenu 플러그인에서 생성한 AccordionMenu 객체를 저장
$this.data('accordionMenu', accMenu);
});
return this;
};
// n번째 메뉴를 선택하는 플러그인 정의
$.fn.selectIndexItem = function(mainIndex, subIndex) {
this.each(function(index) {
var $this = $(this);
var indexMenu = $this.data('accordionMenu');
indexMenu.selectMenu(mainIndex, subIndex);
});
return this;
};
}(jQuery));
$(function() {
// 아코디언 메뉴 플러그인 실행
$('#accordionMenu1').accordionMenu();
// 사용자 정의 이벤트 등록하기
$('#accordionMenu1').on('open', function(e) {
console.log('open', e.$target.find('.main-title a').text());
});
$('#accordionMenu1').on('close', function(e) {
console.log('close', e.$target.find('.main-title a').text());
});
$('#accordionMenu1').on('selected', function(e) {
var oldText = '없음';
if (e.$oldItem) {
oldText = e.$oldItem.text();
}
console.log('select old = ', oldText + ', new = ' + e.$newItem.text());
});
// selectIndexItem 플러그인으로 0번째 서브 메뉴패널을 열고 1번째 서브 메뉴아이템을 선택
$('#accordionMenu1').selectIndexItem(0, 1);
});
function AccordionMenu(selector) {
// 프로퍼티 생성 및 초기화
this.$accordionMenu = null; // 메뉴 랩퍼를 담을 변수
this.$mainMenuItems = null; // 메인 메뉴아이템을 담을 변수
this.$selectSubItem = null; // 선택할 서브 메뉴아이템을 담을 변수
this.init(selector);
this.initSubMenuPanel();
this._initEvent();
}
AccordionMenu.prototype = {
// 요소 초기화
'init': function(selector) {
this.$accordionMenu = $(selector);
this.$mainMenuItems = this.$accordionMenu.children('li');
},
// 서브 패널 초기화 - 초기 시작시 닫힌 상태로 만들기
'initSubMenuPanel': function() {
var _self = this;
this.$mainMenuItems.each(function(index) {
var $item = $(this),
$subMenu = $item.find('.sub');
// 서브 메뉴가 없는 경우
if (!$subMenu.length) {
$item.attr('data-extension', 'empty');
} else {
if ($item.attr('data-extension') == 'open') {
_self.openSbuMenu($item);
} else {
if ($item.attr('data-extension') == 'open') {
_self.openSbuMenu('$item', false);
} else {
_self.closeSubMenu($item, false);
}
}
}
});
},
// 아이콘 상태 설정
'setIconState': function($item, state) {
var $folder = $item.find('.main-title .folder');
// 기존 클래스를 모두 제거
$folder.removeClass();
$folder.addClass('folder ' + state);
},
// 이벤트 초기화
'_initEvent': function() {
var _self = this,
$mainTitle = this.$mainMenuItems.find('.main-title'),
$subPanelItem = this.$mainMenuItems.find('.sub li');
$mainTitle.on('click', function(e) {
var $item = $(this).parent();
_self._toggleSubMenuPanel($item);
});
$subPanelItem.on('click', function(e) {
var $this = $(this);
_self.selectSubMenuItem($this)
});
},
// 서브 메뉴패널 열기 - animation 기본값은 true
'openSbuMenu': function($item, animation) {
if ($item != null) {
$item.attr('data-extension', 'open');
var $subMenu = $item.find('.sub');
if (!animation) {
$subMenu.css({
'margin-top': 0
});
} else {
$subMenu
.stop()
.animate({
'margin-top': 0
}, 300);
}
// 아이콘 상태를 open(-) 상태로 만들기
this.setIconState($item, 'open');
// 사용자 정의 open 이벤트 발생
this.dispatchEvent($item, 'open');
}
},
// 서브 메뉴패널 닫기
'closeSubMenu': function($item, animation) {
if ($item != null) {
$item.attr('data-extension', 'close');
var $subMenu = $item.find('.sub'),
subMenuPanelHeight = -$subMenu.outerHeight(true);
if (!animation) {
$subMenu.css({
'margin-top': subMenuPanelHeight
});
} else {
$subMenu
.stop()
.animate({
'margin-top': subMenuPanelHeight
}, 300);
}
// 아이콘 상태를 close(+) 상태로 만들기
this.setIconState($item, 'close');
// 사용자 정의 close 이벤트 발생
this.dispatchEvent($item, 'close');
}
},
// 서브 메뉴패널 열고 닫기
'_toggleSubMenuPanel': function($item) {
var extension = $item.attr('data-extension');
// 서브 메뉴패널이 없는 경우 실행하지 않는다.
if (extension == 'empty') {
return;
}
// 서브 메뉴 패널이 있는 경우에 실행
if (extension == 'open') {
this.closeSubMenu($item, true);
} else {
this.openSbuMenu($item, true);
}
},
// 인덱스 메뉴의 서브 메뉴패널 열기
'openSubMenuAt': function(index, animation) {
var $item = this.$mainMenuItems.eq(index);
this.openSbuMenu($item, animation);
},
// 인덱스 메뉴의 서브 매뉴패널 닫기
'closeSubMenuAt': function(index, animation) {
var $item = this.$mainMenuItems.eq(index);
this.closeSubMenu($item, animation);
},
// 서브 메뉴아이템 선택
'selectSubMenuItem': function($item) {
var $oldItem = this.$selectSubItem;
if (this.$selectSubItem != null) {
this.$selectSubItem.removeClass('select');
}
this.$selectSubItem = $item;
this.$selectSubItem.addClass('select');
// 사용자 정의 select 이벤트 발생
this._dispatchSelectEvent($oldItem, this.$selectSubItem);
},
// 인덱스를 활용한 메인 메뉴아이템과 서브 메뉴아이템 선택 기능
'selectMenu': function(mainIndex, subIndex, animation) {
// 메인 메뉴아이템
var $item = this.$mainMenuItems.eq(mainIndex);
// 서브 메뉴아이템
var $subMenuItem = $item.find('.sub li').eq(subIndex);
// 서브 메뉴아이템이 존재하는 경우에만 처리
if ($subMenuItem) {
// 서브 메뉴패널 열기
this.openSbuMenu($item, animation);
// 서브 메뉴아이템 선택
this.selectSubMenuItem($subMenuItem);
}
},
// 사용자 정의 open, close 이벤트 발생 처리
'dispatchEvent': function($item, eventName) {
var customEvent = jQuery.Event(eventName);
customEvent.$target = $item;
this.$accordionMenu.trigger(customEvent);
},
// 사용자 정의 select 이벤트 발생 처리
'_dispatchSelectEvent': function($oldItem, $newItem) {
var customEvent = jQuery.Event('selected');
customEvent.$oldItem = $oldItem;
customEvent.$newItem = $newItem;
this.$accordionMenu.trigger(customEvent);
}
};
Also see: Tab Triggers