Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Created June 15, 2012 01:40
Show Gist options
  • Save s-hiroshi/2934165 to your computer and use it in GitHub Desktop.
Save s-hiroshi/2934165 to your computer and use it in GitHub Desktop.
jQuery > utility for web design
jQuery(function ($) {
// name space
function LightUtils(options) {
var prop;
for (prop in this) {
if (LightUtils.prototype.hasOwnProperty(prop)) {
var res = this[prop]();
}
}
}
// ページ内スクロール
LightUtils.prototype.pageScroll = function() {
$('a[href^=#]').click(function() {
// スクロールの速度
var speed = 400;// ミリ秒
// アンカーの値取得
var href= $(this).attr("href");
// 移動先を取得
var target = $(href == "#" || href === "" ? 'html' : href);
// 移動先を数値で取得
var position = target.offset().top;
// スムーススクロール
$(jQuery.support.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
return false;
});
};
// 要素の中央配置
LightUtils.prototype.setCenter = function() {
jQuery.fn.setCenter = function() {
// 自身のサイズ
var width = $(this).width();
var height = $(this).height();
var parentWidth;
var parentHeight;
var parentCssPos;
// 要素親のサイズ
if ($(this).parent().get(0) == $('body').get(0)) {
parentWidth = $(window).width();
parentHeight = $(window).height();
} else {
parentWidth = $(this).parent().width();
parentHeight = $(this).parent().height();
parentCssPos = $(this).parent().css('position');
if ((parentCssPos != 'absolute') && (parentCssPos != 'relative')) {
$(this).parent().css({
position: 'relative'
});
}
}
// オフセット
var offsetX = (parentWidth - width) / 2;
var offsetY = (parentHeight - height) / 2;
$(this).css({
position: 'absolute',
top: offsetY,
left: offsetX
});
return $(this);
};
return true;
};
LightUtils.prototype.getCurrentDirec = function() {
var url = document.URL;
var paths = url.split('/');
var direc;
function getDirec(arr) {
var end = arr[arr.length - 1];
// ディレクトリと見なさない条件
// 空文字
// ファイル名
if (String(end).length === 0 || end.match(/¥.(html|css|js|php)/)) {
return getDirec(arr.slice(0, arr.length - 1));
} else {
return arr[arr.length-1];
}
}
};
//
// マウスオーバー
// マウスオーバーすると画像をfilename-overへ変更する
//
LightUtils.prototype.setHover = function(selector) {
$(selector).hover(
function(i) {
var src = $(this).attr('src');
var pathArr = src.split('/');
file = pathArr[pathArr.length-1];
fileArr = file.split('.');
file = fileArr[0] + '-over.' + fileArr[1];
pathArr[pathArr.length-1] = file;
src = pathArr.join('/');
$(this).attr('src', src);
},
function(i) {
var src= $(this).attr('src');
var file = src.replace(/-over/,'');
$(this).attr('src', file);
}
);
};
var lightutils = new LightUtils();
lightutils.setHover('#nav img');
$('#main h2 span').each(function(i) {
// 実行
$(this).setCenter();
});
$('#nav li:last').css({
marginRight: 0
});
// フッターナビの最終要素のボーダー削除
$('#footer-nav li a:last').css('borderRight', 'none');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment