cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="test">
	<div class="toggle-button">toggleButton</div>
	<div class="toggle-area">toggle content Lorem ipsum dolor sit amet, consectetur adipisicing elit. 	Perferendis natus sunt iusto voluptatibus dolore distinctio inventore autem, culpa nemo quasi, et aliquid, aut officia eos sed doloribus quis reprehenderit ipsum magni aperiam nesciunt ullam. Quos dolorum dolorem obcaecati eligendi natus, voluptatum. </div>
</div>


            
          
!
            
              .test
	width: 202px
	margin: 0 auto

.toggle-button
	border: 1px solid green
	width: 200px
	height: 50px
	background-color: lightblue
	text-align: center
	line-height: 50px

.toggle-area
	width: 202px
	display: none
	background-color: #9933ff

            
          
!
            
              class toggle {
	constructor() {
		// 該当クラスが無ければ実行しない
		if ($(".test").length === 0) {
			//jsはreturnを実行すると、その時点で終了する
			return;
		}
		
		this.init();
	}
	
	init() {
		//定数の設定
		this.setTogglePram();
		//イベントのリスンとアニメーションの実行
		this.setToggleEvent();
		//ユーザー操作の取得をDOMに紐付ける
		this.setToggleClick();
		$.Velocity.mock = 1;
	}
	
	//定数を設定する
	setTogglePram() {
		//windowを使いまわすので保存
		this.$window = $(window);
		//state
		this.isOpen = false;
		//変化の時間
		this.duration = 300;
		//clickイベントの対象
		this.$toggelButton = $(".toggle-button");
		//トグルの対象
		this.$toggleTarget = $(".toggle-area");
	}
	
	//クリックイベントの設定
	//DOMへの操作(click等)→stateによる分岐→イベントのトリガー
	setToggleClick() {
		this.$toggelButton.on("click", () => {
			if(this.isOpen) {
					//DOM変更の関数をトリガーするためのイベント発行
					this.$window.triggerHandler("close.nakanishi");
			}else if(!this.isOpen) {
					//DOM変更の関数をトリガーするためのイベント発行
					this.$window.triggerHandler("open.nakanishi");
					//stateの変更
					this.isOpen = true;		
			}
		});
	}
	
	//イベントリスナーの設定
	// イベント→DOMの操作→stateの変更
	setToggleEvent() {
		this.$window.on({
			"open.nakanishi": () => {
				//clickイベントを止める
				this.offToggleEvent();
				//openのDOM操作
				this.$toggleTarget.velocity({
					backgroundColor: '#3333ff'
				},{
					duration: this.duration
				});
				
				this.$toggleTarget.velocity("slideDown", {
					queue: false,
					duration: this.duration,
					complete: () => {
						this.setToggleEvent();
						//stateの変更
						this.isOpen = true;
					}
			 	});
  		},
  		"close.nakanishi": () => {
				//clickイベントを止める
				this.offToggleEvent();
				//closeのDOM操作
				this.$toggleTarget.velocity({
					backgroundColor: '#9933ff'
				},{
					duration: this.duration
				});
				
				this.$toggleTarget.velocity("slideUp", {
					queue: false,
					duration: this.duration,
					complete: () => {
						this.setToggleEvent();
						//stateの変更
						this.isOpen = false;
					}
				});
  		}
		});
	}
	
	//リスナーを外す
	offToggleEvent() {
		this.$window.off(".nakanishi");
	}
}

new toggle();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console