我有问题弹出页脚与jquery。我的代码是work,但唯一的问题是它不能在第一次点击按钮时工作,而是通过第二次点击按钮来工作?有什么想法吗?下面是我的代码:
<script type="text/javascript">
jQuery(function($) {
var open = false;
$('#footerSlideButton').click(function () {
if(open === false) {
$('#footerSlideContainer').animate({ height: '0' });
$(this).css('backgroundPosition', 'top left');
open = true;
} else {
$('#footerSlideContainer').animate({ height: '150px' });
$(this).css('backgroundPosition', 'bottom left');
open = false;
}
});
});
</script>
发布于 2013-08-23 02:57:39
我会将其概括为:
(function($){
window.opened=false;
$('#footerSlideButton').on('click', function () {
$('#footerSlideContainer').animate({ height : opened ? '0' : '150px'});
$(this).css('backgroundPosition', opened ? 'top left' : 'bottom left');
opened = !opened;
});
})(jQuery);
您还可以将打开状态存储在元素数据中:
(function($){
$('#footerSlideButton').on('click', function () {
$('#footerSlideContainer').animate({ height : $(this).data('open') ? '0' : '150px'});
$(this).css('backgroundPosition', $(this).data('open') ? 'top left' : 'bottom left');
$(this).data('open', !$(this).data('open'));
});
})(jQuery);
https://stackoverflow.com/questions/18388560
复制相似问题