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.button.button--${button$}*4 -->
<div class="left">
	<div class="button button--1" data-article="article1">button1</div>
	<div class="button button--2" data-article="article2">button2</div>
	<div class="button button--3" data-article="article3">button3</div>
	<div class="button button--4" data-article="article4">button4</div>
</div>
<div class="right">
	<div class="article article--active article1" data-article="article1" data-color="#9EFF00">article1</div>
	<div class="article article2" data-article="article2" data-color="#FF1F00">article2</div>
	<div class="article article3" data-article="article3" data-color="#001BFF">article3</div>
	<div class="article article4" data-article="article4" data-color="#FF00E8">article4</div>
</div>

            
          
!
            
              .button
	width: 100px
	line-height: 30px
	border: 1px solid black
	border-radius: 10px
	text-align: center

	&--1
		background-color: #9EFF00
	
	&--2
		background-color: #FF1F00
	
	&--3
		background-color: #001BFF
	
	&--4
		background-color: #FF00E8

.left
	float: left

.right
	float: left
	width: 200px
	height: 150px
	padding: 20px
	background-color: yellow
	text-align: center

.article
	//通常状態は非表示
	display: none

	//activeになったものだけが表示される
	&--active
		display: block
            
          
!
            
              class Search {
	constructor() {
		this.init();
	}

	init() {
		//定数の設定
		this.setConst();
		//クリックイベントの監視とトリガーイベントの発行
		this.attachClickEvent();
		//イベントのリスン、及びトリガーするファンクションの設定
		this.listenEvent();
	}

	//定数の設定
	setConst() {
		this.$window = $(window);
		this.$clickTarget = $(".button");
		this.allArticle = $(".article");
		this.slideTarget = $(".right");
		this.searchData = "article";
		this.activeClass = "article--active"
		//State 現在開いている記事がどれか
		this.oepnedTargetData = "article1";
		this.targetData = null;
		this.$animationTarget = null;
		this.duration = 500;
	}
	
	//クリックイベントの監視とトリガーイベントの発行
	attachClickEvent() {
		this.$clickTarget.on("click.test", (event) => {
			const $target = $(event.currentTarget);
			this.targetData = $target.data(this.searchData);
			this.allArticle.each((index, element) => {
				const $element = $(element);

				if (!($element.data(this.searchData) === this.targetData)) {
					//jQueryのメソッド「.each」の中でreturn true をすると、
					//この要素の回を「飛ばす」
					//これ以下は実行されずに、次の要素へうつる
					return true;
				}

				if (this.targetData === this.oepnedTargetData) {
					//jQueryのメソッド「.each」の中でreturn false をすると、
					//全ての要素への実行を飛ばす。つまり each全体の実行がこの段階で終わる
					return false;
				}

				//アニメーションの対象を確定
				this.$animationTarget = $element;
				//対象を何色にするか、DOMから情報を取得
				this.color = $element.data("color");
				//カスタムイベントのトリガー
				this.$window.triggerHandler("trigger.test");
			});
		});
	}

	//イベントのリスン、及びトリガーするファンクションの設定
	listenEvent() {
		this.$window.on("trigger.test", () => {
			this.$clickTarget.off("click.test");
			this.slideTarget.velocity("slideUp", {
				duration: this.duration,
				complete: () => {
					this.allArticle.removeClass(this.activeClass);
					this.$animationTarget.addClass(this.activeClass);
					this.slideTarget.css("background-color", this.color)
					.velocity("slideDown", {
						duration: this.duration,
						complete: () => {
							this.attachClickEvent();
							this.oepnedTargetData = this.targetData;
						}
					});
				}
			});
		});
	}
}

new Search();




            
          
!
999px
Loading ..................

Console