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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

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.

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
Loading ..................

Console