jQuery スニペット
前のからアップデート
//
$(function(){
});
//
$(window).on(‘load’, function(){
});
//
$(window).on(‘resize’, function(){
});
//
$(window).on(‘load resize’, function(){
});
//
var timer = false;
$(window).resize(function() {
if (timer !== false) {
clearTimeout(timer);
}
timer = setTimeout(function() {
// function
}, 250);
});
//
hoge();
function hoge() {
}
//
$(‘.hoge’).each(function(){
});
//
$(‘.hoge’).click(function() {
});
// 秒間隔
setInterval(function(){
},1000);
// 秒後
setTimeout(function(){
},1000);
// 秒後 clearInterval
timerID = setInterval(function(){
},1000);
clearInterval(timerID);
// ウィンドウサイズ
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var hogeWidth = $(‘.hoge’).css(‘width’);
var hogeHeight = $(‘.hoge’).css(‘height’);
// css
$(‘.hoge’).css({‘width’:‘1000px’});
// important
$(‘.hoge’).css({‘cssText’: ‘width: 1000px !important;’});
// 2の倍数
$(‘.hoge:nth-child(2n)’);
// 何番目(0~)
$(‘.hoge’).eq(0);
// animate
$(‘.hoge’).animate({‘top’:‘0px’},100,‘linear’);
// scrollTop
$(‘html,body’).animate({scrollTop:0},100,‘linear’);
// 要素があれば
var hoge = $(‘hoge’).length;
if (hoge) {
} else {
}
// ページ判定
var single = $(‘.single’).length;
if ( single ) {
$(‘.post img’).each(function(){
$(this).parent(‘p’).addClass(‘post_image’);
$(this).parent(‘a’).addClass(‘link_image’);
});
}
// クラスがあれば
if ( $(‘.hoge’).hasClass(‘active’) ) {
//alert(‘activeクラスがあります’);
} else {
//alert(‘activeクラスがありません’);
}
// cssプロパティが合えば
var tgt = $(‘.hoge’);
if(tgt.css(‘display’)==‘none’) {
// display = none;
} else if (tgt.css(‘display’)==‘block’) {
// display = block;
}
// pagetop
$(‘.pagetop a’).click(function(){
$(‘body, html’).stop().animate({scrollTop:0}, 600, ‘easeOutExpo’);
return false;
});
// scroll
var scrollPos = $(window).scrollTop();
var headerPos = 20;
if ( scrollPos < headerPos ) {
$(‘header’).removeClass(‘stick’);
} else {
$(‘header’).addClass(‘stick’);
}
// preload images
function preloadImages() {
jQuery.preloadImages = function(){
// var url = ‘http://’ + location.host + ‘/wp-content/themes/THEME_NAME’;
for(var i = 0; i<arguments.length; i++){
// jQuery(‘<img>‘).attr(‘src’, url + arguments[i]);
jQuery(‘<img>‘).attr(‘src’, arguments[i]);
}
};
$.preloadImages(
‘/images/image_01.png’,
‘/images/image_02.png’
);
}
// リンク拡大
$(‘.hoge’).click(function(){
var tgtLink = $(‘a’ ,this).length;
if (tgtLink) {
window.location=$(this).find(‘a’).attr(‘href’);
return false;
}
});
$(‘.hoge’).hover(function(){
$(this).css({‘cursor’:‘pointer’});
},function(){
$(this).css({‘cursor’:‘default’});
});
// 画像パス 差し替え
var tgtPath = $(‘hoge img’).attr(‘src’);
var replacePath = tgtPath.replace(‘logo_active.png’,‘logo.png’);
$(‘hoge img’).attr({src:replacePath});
// select 変更
$(‘.hoge’).change(function () {
var tgtVal = $(‘.hoge option:selected’).val();
if ( tgtVal == ‘A’ ) {
// if ‘A’
} else if ( tgtVal == ‘B’ ) {
// if ‘B’
}
}).change();
// input textarea focus / blur
$(‘input, textarea’).focus(function(){
// focus
}).blur(function(){
// blur
});
// ストップウォッチ
stopWatch();
function stopWatch() {
var startTime = (new Date()).getTime();
setInterval( function(){
$(‘.watch’).text(((new Date()).getTime()-startTime)/1000).toFixed(2);
}, 100);
$(‘body’).append(‘<p class=‘watch’></p>’);
$(‘.watch’).css({‘position’:‘absolute’, ‘top’:‘0’, ‘left’:‘0’, ‘color’:‘#000’, ‘background’:‘#fff’});
}