これまで何かを「バインド」または「バインド解除」する必要がなかったので、やりたいことに直接関連する例が見つからないため、混乱しています。
ここのページの特定のポイントに到達すると div をスクロールするプラグインがあります。
しかし、ウィンドウが特定の幅の場合にのみプラグインを起動し、ウィンドウが再びその幅を下回っている場合は「起動」/バインド解除します...
(ちなみに #contact-form は、私がスクロールしているコンテナで、ご想像のとおり、連絡先フォームが含まれています)
function contactForm() {
windowWidth = $(window).width();
if (windowWidth >= 1024) {
jQuery('#contact-form').containedStickyScroll();
} else {
jQuery('#contact-form').unbind(); // I don't want the plugin to fire
}
};
// Standard stuff...
$(document).ready(function() {
contactForm();
});
$(window).resize(function() {
contactForm();
});
この含まれるスティッキー スクロール機能は次のようになります。
$.fn.containedStickyScroll = function( options ) {
var defaults = {
oSelector : this.selector,
unstick : true,
easing: 'linear',
duration: 500,
queue: false,
closeChar: '^',
closeTop: 0,
closeRight: 0
}
var options = $.extend(defaults, options);
if(options.unstick == true){
this.css('position','relative');
this.append('<a class="scrollFixIt">' + options.closeChar + '</a>');
jQuery(options.oSelector + ' .scrollFixIt').css('position','absolute');
jQuery(options.oSelector + ' .scrollFixIt').css('top',options.closeTop + 'px');
jQuery(options.oSelector + ' .scrollFixIt').css('right',options.closeTop + 'px');
jQuery(options.oSelector + ' .scrollFixIt').css('cursor','pointer');
jQuery(options.oSelector + ' .scrollFixIt').click(function() {
getObject = options.oSelector;
jQuery(getObject).animate({ top: "0px" },
{ queue: options.queue, easing: options.easing, duration: options.duration });
jQuery(window).unbind();
jQuery('.scrollFixIt').remove();
});
}
jQuery(window).scroll(function() {
getObject = options.oSelector;
if(jQuery(window).scrollTop() > (jQuery(getObject).parent().offset().top) &&
(jQuery(getObject).parent().height() + jQuery(getObject).parent().position().top - 30) > (jQuery(window).scrollTop() + jQuery(getObject).height())){
jQuery(getObject).animate({ top: (jQuery(window).scrollTop() - jQuery(getObject).parent().offset().top) + "px" },
{ queue: options.queue, easing: options.easing, duration: options.duration });
}
else if(jQuery(window).scrollTop() < (jQuery(getObject).parent().offset().top)){
jQuery(getObject).animate({ top: "0px" },
{ queue: options.queue, easing: options.easing, duration: options.duration });
}
});
};