Last active
July 27, 2017 01:23
-
-
Save syamgot/fb3904dfd134d1b487d7172f374b77b6 to your computer and use it in GitHub Desktop.
コールバックメソッド付きアコーディオン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Aco = function(btnSelector, bodySelector) { | |
this.initialize(btnSelector, bodySelector); | |
} | |
Aco.prototype.isClosing = true; | |
Aco.prototype.time = 500; | |
Aco.prototype.$btn = null; | |
Aco.prototype.$body = null; | |
Aco.prototype.initialize = function(btnSelector, bodySelector) { | |
this.$btn = $(btnSelector); | |
this.$body = $(bodySelector); | |
var self = this; | |
this.$btn.click(function(){ | |
self.toggle(self.time); | |
}); | |
this.isClosing = false; | |
this.close(0); | |
return this; | |
} | |
Aco.prototype.toggle = function(time) { | |
if (typeof time != 'number') time = this.time; | |
this.isClosing ? this.open(time) : this.close(time); | |
} | |
Aco.prototype.beforeOpen = function(){} | |
Aco.prototype.afterOpen = function(){} | |
Aco.prototype.open = function(time) { | |
if (!this.isClosing) return; | |
if (typeof this.beforeOpen == 'function') this.beforeOpen(); | |
var self = this; | |
this.$btn.addClass('active'); | |
this.$body.slideDown(time, function(){ | |
if (typeof self.afterOpen == 'function') self.afterOpen(); | |
self.isClosing = false; | |
}); | |
} | |
Aco.prototype.beforeClose = function(){} | |
Aco.prototype.afterClose = function(){} | |
Aco.prototype.close = function(time) { | |
if (this.isClosing) return; | |
if (typeof this.beforeClose == 'function') this.beforeClose(); | |
var self = this; | |
this.$btn.removeClass('active'); | |
this.$body.slideUp(time, function(){ | |
if (typeof self.afterClose == 'function') self.afterClose(); | |
self.isClosing = true; | |
}); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="http://code.jquery.com/jquery.min.js"></script> | |
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> | |
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css"></link> | |
<title></title> | |
<script src="./aco.js"></script> | |
<script> | |
$(function(){ | |
var aco = new Aco('#acoBtn','#acoBody'); | |
aco.beforeOpen = function(){ console.log('beforeOpen'); } | |
aco.beforeClose = function(){ console.log('beforeClose'); } | |
aco.afterOpen = function(){ console.log('afterOpen'); } | |
aco.afterClose = function(){ console.log('afterClose'); } | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="acoBtn">hoge</button> | |
<pre id="acoBody"> | |
hogehoge | |
fugafuga | |
hogehoge | |
fugafuga | |
hogehoge | |
fugafuga | |
hogehoge | |
fugafuga | |
</pre> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment