PageSourceSearch

https://www.sky-homes.co.jp/-cmsadm-/wp-content/themes/skyhomes/as…/jquery/jquery.targetScroller.js?ver=1.0

js sky-homes.co.jp collected 2026-10-08 23:23:47 UTC 6,250 bytes, 157 lines download raw bytes

1/**
2 * jQuery Target Scroller Plugin
3 * ・aタグのhrefでハッシュ指定でスクロール可能です
4 * ・aタグ以外でもセレクター指定でスクロール可能です
5 * ・URLハッシュでスクロール可能です
6 * ・移動速度指定(時間指定)可能です
7 * ・スクロールエフェクト(easing)指定可能です
8 * ・スクロール後にコールバック関数実行可能です
9 *
10 * 注意点
11 * ・指定したターゲットが存在しなければスクロールしません(事前にチェックして実行しません)
12 * ・スクロール中にマウスホイールでスクロールが停止します(正常に停止します)
13 *
14 * 使用方法
15 * 引数指定はオプション
16 * $(selector).targetScroller({
17 *     target: スクロール先のselector(aタグのhrefより優先されます),
18 *     duration: スクロールにかける時間[ms],
19 *     easing: スクロールのエフェクト(jQuery Easing Pluginを読み込むことで使用できます),
20 *     callback: スクロール完了時に呼び出される関数(function() {・・・} で指定します)
21 * });
22 *
23 * 引数なしならデフォルト動作
24 * $(selector).targetScroller();
25 *
26 * デフォルト動作
27 * target: aタグのhrefで指定したターゲット(aタグ以外 or hrefなしならスクロールしません)
28 * duration: 1500[ms]
29 * easing: 'swing'
30 * callback: なし
31 *
32 * ハッシュありのaタグすべてに適用する時は
33 * $('a[href^="#"]').targetScroller();
34 * と指定すれば可能です。
35 */
36;jQuery(document).ready(function($) {
37    var mousewheelevent = 'onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll';
38
39    var targetScroller = function(elem, options) {
40        this.options = {};
41        if (elem === null) {
42            // 要素がnullならURLハッシュによるスクロール
43            this.load(options);
44        } else {
45            if (this.init(elem, options)) {
46                // ターゲットの位置を取得できればイベント登録
47                this.event(elem);
48            }
49        }
50    }
51    $.extend(targetScroller.prototype, {
52        defaults: {
53            duration: 1500,
54            easing: 'swing',
55            callback: function() {}
56        },
57        // 初期化
58        init: function(elem, options) {
59            var href = {};
60            // aタグでハッシュがあればターゲットとして使用する
61            if (elem.hash !== '' && typeof elem.hash !== 'undefined') {
62                href['target'] = elem.hash;
63            }
64            // 優先順位:オプション指定 > ハッシュ > デフォルト
65            this.options = $.extend({}, this.defaults, href, options);
66            if (typeof this.options.target !== 'undefined' && $(this.options.target).length) {
67                // ターゲットが指定されていて且つ存在していれば位置を取得
68                this.options = $.extend(this.options, $(this.options.target).eq(0).offset());
69                return true;
70            } else {
71                return false;
72            }
73        },
74        // スクロール
75        scroll: function(me) {
76            // スクロール前に再度ターゲットの存在確認および位置取得
77            if ($(this.options.target).length) {
78                // ターゲットが存在していれば位置を取得
79                this.options = $.extend(this.options, $(this.options.target).eq(0).offset());
80                // ヘッダー要素が指定されていて、それが position: fixed だった場合、スクロール位置に高さを考慮する
81                if (typeof this.options.header !== 'undefined'){
82                    $header = $(this.options.header);
83                    if ($header.length
84                        && 'fixed' === $header.first().css('position')
85                    ) {
86                        me.options.top = me.options.top - $header.height();
87                        if (me.options.top < 0) {
88                            me.options.top = 0;
89                        }
90                    }
91                }
92                me.body().animate(
93                    {
94                        scrollTop: me.options.top
95                    },
96                    me.options.duration,
97                    me.options.easing,
98                    function() {
99                        $(document).off(mousewheelevent);
100                        (me.options.callback)();
101                    }
102                );
103
104                $(document).on(mousewheelevent, function(e) {
105                    me.stop();
106                });
107            } else {
108            }
109        },
110        // スクロール停止
111        stop: function() {
112            this.body().stop(true);
113            $(document).off(mousewheelevent);
114        },
115        // ロード時のスクロール
116        load: function(options) {
117            if (location.hash) {
118                if (this.init({hash:location.hash}, options)) {
119                    // ページトップからスクロール
120                    this.body().scrollTop(0);
121                    this.scroll(this);
122                }
123            }
124        },
125        // スクロールさせる要素判定
126        body: function() {
127// 以下の判定方法ではhtmlがスクロールするブラウザでは不適切
128//            $('html').scrollTop(5);
129//            if ($('html').scrollTop() > 0) {
130//                return $('html');
131//            } else {
132//                return $('body');
133//            }
134            // htmlとbodyの両方をスクロールさせる
135            return $('html, body');
136        },
137        // イベント登録
138        event: function(elem) {
139            var me = this;
140            $(elem).on('click', function(e) {
141                me.scroll(me);
142                return false;
143            });
144        }
145    });
146
147    $.fn.targetScroller = function(options) {
148        return this.each(function() {
149            new targetScroller(this, options);
150        });
151    };
152
153    // URLハッシュによるスクロール
154    $.targetScroller = function(options) {
155        new targetScroller(null, options);
156    }
157});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.