forked from xserver-inc/cocoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
359 lines (320 loc) · 12.5 KB
/
Copy pathjavascript.js
File metadata and controls
359 lines (320 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* Cocoon WordPress Theme
* @author: yhira
* @link: https://wp-cocoon.com/
* @license: http://www.gnu.org/licenses/gpl-2.0.html GPL v2 or later
*/
/////////////////////////////////
// JavaScriptコード
/////////////////////////////////
( function ( $ ) {
/////////////////////////////////
//TOPへ戻るボタン
/////////////////////////////////
var prevScrollTop = -1;
var $window = $( window );
$window.scroll( function () {
//最上部から現在位置までの距離を取得して、変数[scrollTop]に格納
var scrollTop = $window.scrollTop();
var threashold = 600;
var s1 = prevScrollTop > threashold;
var s2 = scrollTop > threashold;
// スレッショルドを跨いだ時のみ処理をする
if ( s1 ^ s2 ) {
if ( s2 ) {
// トップへ戻るボタンが見えている時
$( '.body' ).addClass( 'go-to-top-visible' );
// //[.go-to-to]をゆっくりフェードインする
// $('.go-to-top').fadeIn('slow');
} else {
//トップへ戻る間だけ追加したクラスを除去
$( '.go-to-top-common' ).removeClass( 'go-to-top-up' );
// トップへ戻るボタンが見えていない時
$( '.body' ).removeClass( 'go-to-top-visible' );
// //それ以外だったらフェードアウトする
// $('.go-to-top').fadeOut('slow');
}
}
prevScrollTop = scrollTop;
} );
//ボタン(.go-to-top-common)のクリックイベント
$( '.go-to-top-common' ).click( function () {
//トップへ戻る間だけクラスを追加
$( this ).addClass( 'go-to-top-up' );
//ページトップへ移動する
$( 'body,html' ).animate(
{
scrollTop: 1,
},
800
);
} );
//ボタン(.go-to-toc-common)のクリックイベント
$( '.go-to-toc-common' ).click( function () {
//目次へ移動する
$( 'body,html' ).animate(
{
scrollTop: $( '.entry-content .toc' ).offset().top,
},
800
);
} );
//検索ボタンクリックでフォーカスを入力エリアに移す
$( '#search-menu-input' ).change( function ( e ) {
var searchEdit = $( '#search-menu-content .search-edit' ).first();
if ( e.target.checked ) {
searchEdit.focus();
} else {
searchEdit.blur();
}
} );
//下にスクロールで管理パネルを隠す
//上にスクロールで管理パネルを表示
var adminMenu = $( '#admin-panel' );
var adminHeight = adminMenu.outerHeight();
var adminStartPos = 0;
$( window ).scroll( function () {
var adminCurrentPos = $( this ).scrollTop();
if ( adminCurrentPos > adminStartPos ) {
if ( adminCurrentPos >= 200 ) {
adminMenu.css( 'bottom', '-' + adminHeight + 'px' );
}
} else {
adminMenu.css( 'bottom', 0 );
}
adminStartPos = adminCurrentPos;
} );
//モバイルボタンが固定じゃない場合
if ( cocoon_localize_script_options.is_fixed_mobile_buttons_enable != 1 ) {
//ヘッダーモバイルメニュー
var headerMenu = $( '.mobile-header-menu-buttons' );
var headerStartPos = 0;
$( window ).scroll( function () {
var headerCurrentPos = $( this ).scrollTop();
// メニュー名の折り返しで高さが変わるため、隠す直前に都度高さを取得する。
// box-shadowはouterHeightに含まれないため、影の残り対策として余裕を加える。
var headerHight = headerMenu.outerHeight() + 5;
// 画面幅が600px以下の場合は、--wp-admin--admin-bar--heightを考慮しない(WordPressアドミンバーが固定とならないため、ヘッダーメニューの位置を0にする)
if ( window.innerWidth <= 600 ) {
if ( headerCurrentPos > headerStartPos ) {
if ( headerCurrentPos >= 100 ) {
headerMenu.css( 'top', '-' + headerHight + 'px' );
}
} else {
headerMenu.css( 'top', 0 );
}
} else {
// CSS変数--wp-admin--admin-bar--heightの値を取得
var adminBarHeight =
getComputedStyle( document.documentElement )
.getPropertyValue( '--wp-admin--admin-bar--height' )
.trim() || '0px';
if ( headerCurrentPos > headerStartPos ) {
if ( headerCurrentPos >= 100 ) {
// 管理バーの高さを考慮してtopを設定
headerMenu.css(
'top',
'calc(' + adminBarHeight + ' - ' + headerHight + 'px)'
);
}
} else {
// 管理バーの高さを考慮してtopを設定
headerMenu.css( 'top', 'calc(' + adminBarHeight + ')' );
}
}
headerStartPos = headerCurrentPos;
} );
//フッターモバイルメニュー
var footerMenu = $( '.mobile-footer-menu-buttons' );
var footerStartPos = 0;
$( window ).scroll( function () {
var footerCurrentPos = $( this ).scrollTop();
// メニュー名の折り返しで高さが変わるため、隠す直前に都度高さを取得する。
// box-shadowはouterHeightに含まれないため、影の残り対策として余裕を加える。
var footerHeight = footerMenu.outerHeight() + 5;
if ( footerCurrentPos > footerStartPos ) {
if ( footerCurrentPos >= 100 ) {
footerMenu.css(
'bottom',
'calc( -1 * (env(safe-area-inset-bottom) + ' +
footerHeight +
'px) )'
);
}
} else if ( footerCurrentPos - footerStartPos < -8 ) {
footerMenu.css( 'bottom', 0 );
}
footerStartPos = footerCurrentPos;
} );
var headerButtons = $( '.mobile-header-menu-buttons' );
var footerButtons = $( '.mobile-footer-menu-buttons' );
headerButtons.click( function () {
headerButtons.css( 'z-index', '3' );
footerButtons.css( 'z-index', '2' );
} );
footerButtons.click( function () {
headerButtons.css( 'z-index', '2' );
footerButtons.css( 'z-index', '3' );
} );
}
//コメントボタンがクリックされたとき
const clickEventType = window.ontouchstart !== null ? 'click' : 'touchend';
$( document ).on(
clickEventType,
'#comment-reply-btn, .comment-reply-link',
function () {
$( '#comment-reply-btn' ).slideUp();
const respond = document.getElementById( 'respond' );
const styles = {
inset: 'auto',
position: 'static',
visibility: 'visible',
};
Object.entries( styles ).forEach( ( [ key, value ] ) => {
respond.style[ key ] = value;
} );
$( '#respond' ).slideDown();
}
);
//Google検索ボタン
$( '.sbtn' ).click( function () {
var w = $( this ).prev( '.sform' ).text();
if ( w )
window.open(
'https://www.google.co.jp/search?q=' + encodeURIComponent( w ),
'_blank'
);
} );
//スライドインサイドバーのアーカイブセレクトボックス選択処理
$( '.sidebar-menu-content .widget_archive select' ).change( function () {
document.location.href = this.options[ this.selectedIndex ].value;
} );
//スライドインサイドバーのカテゴリーセレクトボックス選択処理
$( '.sidebar-menu-content .widget_categories select' ).change( function () {
if ( this.options[ this.selectedIndex ].value > 0 ) {
this.parentNode.submit();
}
} );
function drawerCloser( selecter, checkbox ) {
$( selecter ).click( function () {
$( checkbox ).prop( 'checked', false );
// const href = $(this).attr('href');
// if (href.match(/#/)) {
// $(checkbox).prop('checked', false);
// }
} );
}
//モバイルメニューをクリックしたら閉じる
drawerCloser( '.menu-drawer .menu-item a', '#navi-menu-input' );
//モバイルサイドバーをクリックしたら閉じる
drawerCloser( '#slide-in-sidebar a', '#sidebar-menu-input' );
//モバイルヘッダーメニューのロゴ処理
$( '.mobile-menu-buttons' ).each( function () {
if ( $( this ).has( '.logo-menu-button' ).length ) {
$( this ).addClass( 'has-logo-button' );
}
} );
$( window ).on( 'load', function () {
$( '#carousel' ).addClass( 'loaded' );
} );
//FAQボックスのアコーディオン化用
$( '.is-style-accordion > .faq > .faq-answer' ).hide();
$( '.is-style-accordion > .faq > .faq-question' ).click( function () {
$( this ).next( '.is-style-accordion .faq-answer' ).slideToggle();
$( this ).toggleClass( 'active' );
} );
//モバイルサイドバー表示時にサイドバーを移動する・モバイルサイドバーを閉じる時に元の位置に戻す
$( document ).on( 'change', '#sidebar-menu-input', function () {
if ( $( this ).prop( 'checked' ) ) {
$( '#sidebar' ).appendTo( '#sidebar-menu-content' );
$( '#sidebar' ).attr( 'id', 'slide-in-sidebar' );
$( '#sidebar' ).addClass( 'slide-in-sidebar' );
//モバイルサイドバーのリンクをクリックしたら閉じる
drawerCloser( '#slide-in-sidebar a', '#sidebar-menu-input' );
} else {
$( '#sidebar' ).removeClass( 'slide-in-sidebar' );
$( '#slide-in-sidebar' ).attr( 'id', 'sidebar' );
$( '#sidebar' ).insertAfter( '#main' );
}
} );
//リサイズした時はサイドバーを元に戻す
var vw = window.innerWidth;
$( window ).resize( function () {
if ( vw != window.innerWidth ) {
$( '#sidebar-menu-input' ).prop( 'checked', false ).change();
}
vw = window.innerWidth;
} );
} )( jQuery );
/*
* Cocoon WordPress Theme incorporates code from "Youtube SpeedLoad" WordPress Plugin, Copyright 2017 Alexufo[http://habrahabr.ru/users/alexufo/]
"Youtube SpeedLoad" WordPress Plugin is distributed under the terms of the GNU GPL v2
*/
( function () {
var f = document.querySelectorAll( '.video-click' );
for ( var i = 0; i < f.length; ++i ) {
f[ i ].onclick = function () {
var iframe = this.getAttribute( 'data-iframe' );
this.parentElement.innerHTML = '<div class="video">' + iframe + '</div>';
};
}
} )();
// ヘッダーの位置オフセットを更新
( function () {
function updateHeaderOffset() {
const header = document.getElementById( 'header-container' );
let offset = 0;
if ( header && header.classList.contains( 'fixed-header' ) ) {
offset = header.offsetHeight;
}
// .mobile-header-menu-buttonsのtop・高さを取得して処理
const mobileHeaderMenuButtons = document.querySelector(
'.mobile-header-menu-buttons'
);
if ( mobileHeaderMenuButtons ) {
const computedStyle = window.getComputedStyle( mobileHeaderMenuButtons );
const topValue = parseFloat( computedStyle.top );
// ボタンの実寸高さ(ラベル折り返し時も実寸)
const height = mobileHeaderMenuButtons.offsetHeight;
if ( topValue >= 0 ) {
// topが0以上(表示中)の場合は、sticky用オフセットに高さを反映
offset += height;
}
// 本文余白用には高さのみを常時設定する。
// スクロールで隠れている最中(topが負)でも高さは変わらないため、
// 位置(top)に依存せず余白が安定する。
document.documentElement.style.setProperty(
'--cocoon--mobile-header-menu-buttons--height',
height + 'px'
);
}
document.documentElement.style.setProperty(
'--cocoon--header-container--position-offset',
offset + 'px'
);
}
function initHeaderOffset() {
updateHeaderOffset();
// モバイルヘッダーボタンのサイズ変化(遅延表示・ラベルの折り返し)を監視する。
// スクロールに依存しないため、初期表示時に高さが0pxになる問題を解消できる。
const target = document.querySelector( '.mobile-header-menu-buttons' );
if ( target && 'ResizeObserver' in window ) {
new ResizeObserver( updateHeaderOffset ).observe( target );
}
}
if ( document.readyState !== 'loading' ) {
initHeaderOffset();
} else {
document.addEventListener( 'DOMContentLoaded', initHeaderOffset );
}
// ブラウザサイズ変更時に更新(リサイズ処理が完了してから実行するため、少し遅延させる)
let resizeTimer;
window.addEventListener( 'resize', function () {
clearTimeout( resizeTimer );
resizeTimer = setTimeout( function () {
updateHeaderOffset();
}, 100 );
} );
// スクロール時にも更新(固定ヘッダーの表示切り替えに追従するため)
window.addEventListener( 'scroll', updateHeaderOffset );
} )();