Skip to content

Instantly share code, notes, and snippets.

@maepon
Last active December 22, 2015 06:59
Show Gist options
  • Save maepon/6434811 to your computer and use it in GitHub Desktop.
Save maepon/6434811 to your computer and use it in GitHub Desktop.
Trust Formでフォームが長くなった時にフィールドの追加がめんどくさくなるのでフィールドのボックスが追従するようにしたJavaScript。検証は適当。
// admin/make-form.phpの最後にscriptタグで追加
(function($){
var $box = $('#standard-form'),
boxBaseOffsetY = $box.offset().top;
$box.css({
position: 'absolute',
left:0,
top:0,
width: '100%'
}).parent().css({
position: 'relative'
});
function moveBox(){
var $t = $(this)
toY = $t.scrollTop() - boxBaseOffsetY;
if (toY < 0){
$box.css({top: 0});
}else{
$box.css({top: toY + 'px'});
}
}
$(window).scroll(moveBox);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment