[Facebook]FMBLに画像スライダーを設置

※注意:2011/03/11以降新規追加はできないとのことです。
フェイスブック、「FBML」を廃止へ: インターネット広告のひみつ - ブログ

[メモ]FacebookファンページのFBMLで使えるタグ11選に続きまた、FacebookのFMBLネタ。
今回はJavaScriptも使って画像スライダーを作成します。
導入イメージ@facebook
[Facebook]FMBLに画像スライダーを設置サンプル
via:FBJS Animation. Part 3 - Fx accordion and slider

設定方法
まずはhtml
//ここから大きな画像を記述
<div id="app" onclick="appObserver.init();" onkeypress="appObserver.init();" onmousedown="appObserver.init();">
<div id="bigImage_0" class="bigImage"><img src="大画像パス1" /></div>
<div id="bigImage_1" class="bigImage" style="display: none"><img src="大画像パス2" /></div>
//以下画像分(2枚目以降はdisplay:none;に。またidの_Nの数字を加えていくこと)

<div class="slider" id="slider">
//次へリンク
<a href="#" class="sliderPrev inactive">次へ</a>
<div class="sliderWindow">
<div class="sliderImagesContainer">
//ここから小さな画像を記述
<a href="#" class="sliderImage sliderImageCurrent"><img src="小画像パス1" /></a>
<a href="#" class="sliderImage"><img src="小画像パス2" /></a>
//以下画像分(1枚目のみsliderImageCurrentを記述)
</div>
</div>
; //前へリンク
<a href="#" class="sliderNext">前へ</a>
<div class="clear"></div>
</div>
</div>

次はCSSで見た目を調整します。
<style> .slider { width: 480px; }
.slider a.sliderNext, .slider a.sliderPrev {
border: none;
text-decoration: none;
float: left;
display: block;
height: 12px;
line-height:12px;
text-align:center;
width: 30px;
color:#FFF;
}

.slider a.sliderNext {
margin-top: 24px;
margin-left: 5px;
background: #000;
color:#FFF;
}

.slider a.sliderPrev {
margin-top: 25px;
margin-right: 5px;
background: #000;
color:#FFF;
}

.slider a.sliderNext.inactive {
background: #ccc;
color:#FFF;
}

.slider a.sliderPrev.inactive {
background: #ccc;
color:#FFF;
}

.slider .clear {
float: none;
clear: both;
}

.slider .sliderImage {
border: 2px solid #ccc;
float: left;
margin: 0 3px 0 3px;
line-height: 0;
}

.slider .sliderImage:hover,
.slider .sliderImageCurrent
{
border-color: #519dd0;
}

.slider .sliderImage img{
border: none;
}

.sliderWindow {
overflow: hidden;
width: 400px;
float: left;
}

.slider .sliderImagesContainer {
width: 10000px;
}

.bigImage {
width: 480px;
text-align: center;
}
</style>

最後にJSを記述します。
<script> var appObserver = { isInited: false, init: function() { if (!appObserver.isInited) { (new fx(document.getElementById('slider'))).slider( { onclick: function(next, prev) { (new fx(document.getElementById('bigImage_' + prev))).fadeIn( function() { // document.getElementById('bigImage_' + next).setStyle('display','block'); (new fx(document.getElementById('bigImage_' + next))).fadeOut(); } ) } } ); appObserver.isInited = true; } } }; var utils = { hasClass: function(element, className) { return element.getClassName().match(new RegExp('(\\s|^)'+className+'(\\s|$)')); }, addClass: function(element, className) { if (!utils.hasClass(element,className)) element.setClassName(element.getClassName() + " " + className); }, removeClass: function(element, className) { if (utils.hasClass(element,className)) { var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); element.setClassName(element.getClassName().replace(reg,' ')); } }, findByClass: function(className, rootElement) { var root = rootElement || document.getRootElement(); var elements = []; var child = root.getChildNodes(); for(var i = 0; i < child.length; i++) { if (utils.hasClass(child[i], className)) { elements.push(child[i]); } else { elements = elements.concat(utils.findByClass(className, child[i])); } } return elements; } }; function fx(element) { // make this instance visible in private functions var self = this; this.element = element; // set default slide direction this.direction = fx.VERTICAL; /** * Fade in * * @param duration integer slide down duration * @param callback function callback for slide down ending * @return fx */ this.fadeIn = function(duration, callback) { fade('in', duration, callback); return this; }; /** * Fade out * * @param duration integer slide up duration * @param callback function callback for slide up ending * @return fx */ this.fadeOut = function(duration, callback) { fade('out', duration, callback); return this; }; /** * Fade toggle * * @param duration integer slide up duration * @param callback function callback for slide up ending * @return fx */ this.fadeToggle = function(duration, callback) { if (this.element.getStyle('display') == 'none' || this.element.getClientHeight() == 0 || this.element.getClientWidth() == 0) return this.fadeOut(duration, callback); else return this.fadeIn(duration, callback); }; /** * Set slide direction * * @param direction fx.HORIZONTAL, fx.VERTICAL or fx.DIAGONAL * @return fx */ this.direction = function(direction) { if (direction == fx.HORIZONTAL || direction == fx.VERTICAL || direction == fx.DIAGONAL) this.direction = direction; return this; }; this.slider = function(params) { // extend default options var options = { prev: 'sliderPrev', next: 'sliderNext', image: 'sliderImage', imageContainer: 'sliderImagesContainer', imageCurrent: 'sliderImageCurrent', inactive: 'inactive', imageLeftMargin: 6, onclick: null }; if (params) for(var key in params) options[key] = params[key]; var imageContainer = utils.findByClass(options.imageContainer, this.element)[0]; // find all images var images = utils.findByClass(options.image, imageContainer); // set up default position var currentPosition = 0; // calculate positions' offsets var positionOffsets = []; for(var position = 0; position < images.length; position++) { if (position == 0) { positionOffsets[position] = 0; } else { positionOffsets[position] = positionOffsets[position - 1]; positionOffsets[position] -= images[position].getOffsetWidth(); positionOffsets[position] -= options.imageLeftMargin; } } // find next and prev buttons var next = utils.findByClass(options.next, this.element); var prev = utils.findByClass(options.prev, this.element); // bind next navigation if (next.length) { next[0].addEventListener('click', function(event){ if (currentPosition < images.length - 4) { currentPosition++; (new fx(imageContainer)).move(positionOffsets[currentPosition]); } if (currentPosition == 0) { utils.addClass(prev[0], options.inactive); } else { utils.removeClass(prev[0], options.inactive); } if (currentPosition >= images.length - 4) { utils.addClass(next[0], options.inactive); } else { utils.removeClass(next[0], options.inactive); } }); } // bind previous navigation if (prev.length) { prev[0].addEventListener('click', function(event){ if (currentPosition > 0) { currentPosition--; (new fx(imageContainer)).move(positionOffsets[currentPosition]); } if (currentPosition == 0) { utils.addClass(prev[0], options.inactive); } else { utils.removeClass(prev[0], options.inactive); } if (currentPosition >= images.length - 4) { utils.addClass(next[0], options.inactive); } else { utils.removeClass(next[0], options.inactive); } }); } // bind item onclick callback for(var i = 0; i < images.length; i++) { images[i].setTabIndex(i).addEventListener('click', function(event){ var currentImage = utils.findByClass(options.imageCurrent, self.element)[0]; utils.removeClass(currentImage, options.imageCurrent); var target = (utils.removeClass(event.target, options.image)) ? event.target : event.target.getParentNode(); utils.addClass(target, options.imageCurrent); if (options.onclick) options.onclick.apply(target, [target.getTabIndex(), currentImage.getTabIndex()]); }); } }; this.move = function(step, duration, callback) { if (!duration) { duration = fx.NORMAL; // set default duration } else if (typeof duration == "function") { callback = duration; duration = fx.NORMAL; // set default duration } else if (duration != parseInt(duration) || Math.abs(parseInt(duration)) == Math.NaN || parseInt(duration) <= 0) { duration = fx.NORMAL; // set default duration } else { duration = Math.abs(parseInt(duration)); } var animationObject = Animation(self.element); animationObject.duration(duration); animationObject.to('marginLeft', step); // set slide ending callback if (typeof callback == "function") animationObject .checkpoint(1, callback); animationObject.ease().blind().go(); }; /** * Slide animation * * @private * * @param type could by up or down * @param duration integer slide up duration * @param callback function callback for slide up ending * */ function slide(type, duration, callback) { // check duration and callback if (!duration) { duration = fx.NORMAL; // set default duration } else if (typeof duration == "function") { callback = duration; duration = fx.NORMAL; // set default duration } else if (duration != parseInt(duration) || Math.abs(parseInt(duration)) == Math.NaN || parseInt(duration) <= 0) { duration = fx.NORMAL; // set default duration } else { duration = Math.abs(parseInt(duration)); } var animationObject = Animation(self.element); // first of all we need to show or hide element if (type == 'down') animationObject.show(); else animationObject.hide(); // set slide direction if (self.direction == fx.DIAGONAL) animationObject .to('width', (type == 'down') ? 'auto': 0) .to('height', (type == 'down') ? 'auto': 0); else if (self.direction == fx.HORIZONTAL) animationObject .to('width', (type == 'down') ? 'auto': 0); else animationObject .to('height', (type == 'down') ? 'auto': 0); if (type == 'down') { if (self.direction == fx.DIAGONAL) animationObject .from('width', 0) .from('height', 0); else if (self.direction == fx.HORIZONTAL) animationObject .from('width', 0); else animationObject .from('height', 0); } animationObject.duration(duration); // set slide ending callback if (typeof callback == "function") animationObject .checkpoint(1, callback); animationObject.blind().go(); } /** * Fade animation * * @private * * @param type could by in or out * @param duration integer slide up duration * @param callback function callback for slide up ending * */ function fade(type, duration, callback) { // check duration and callback if (!duration) { duration = fx.NORMAL; // set default duration } else if (typeof duration == "function") { callback = duration; duration = fx.NORMAL; // set default duration } else if (duration != parseInt(duration) || Math.abs(parseInt(duration)) == Math.NaN || parseInt(duration) <= 0) { duration = fx.NORMAL; // set default duration } else { duration = Math.abs(parseInt(duration)); } var animationObject = Animation(self.element); // first of all we need to show or hide element // and set initial opacity if (type == 'out') { animationObject.show().from('opacity', 0); } else { animationObject.hide().from('opacity', 1); } // set fade animation animationObject .to('opacity', (type == 'out') ? 1 : 0); animationObject.duration(duration); // set slide ending callback if (typeof callback == "function") animationObject .checkpoint(1, callback); animationObject.blind().go(); } this._debug = function(msg) { (new Dialog()).showMessage('Debug', msg, 'Close'); } }; // duration constants fx.SLOW = 800; fx.NORMAL = 500; // default fx.FAST = 200; // direction constants fx.HORIZONTAL = 'horizontal'; fx.VERTICAL = 'vertical'; // default fx.DIAGONAL = 'diagonal'; </script>
これで完成です。
まるっとコピーして画像を変えてしまえば完成なので、リッチなファンページにお使いください。
また、参照サイトFBJS Animation. Part 3 - Fx accordion and sliderにはアコーディオンの導入方法もあります。
skuare.netのファンページはこちら

sponsors

「[Facebook]FMBLに画像スライダーを設置」をシェアする

記事作成:
記事URL:

TOP > > > [Facebook]FMBLに画像スライダーを設置