【发布时间】:2016-04-14 20:18:50
【问题描述】:
当用户将页面滚动到页面的某个位置时,我有这段代码可以修改页面上元素的位置:
<script type="text/javascript">
jQuery(window).scroll(function() {
if(jQuery(window).scrollTop() > 150) {
jQuery("#tab_toggle #tab_toggle_bg").attr("style","top: 45% !important");
} else {
jQuery("#tab_toggle #tab_toggle_bg").removeAttr("style");
}
});
</script>
有没有办法在scrollTop效果触发时给这个添加类似于CSStransition: all 0.5s ease的效果?
【问题讨论】:
-
你真的应该使用 JQuery 的 'css' 属性而不是添加样式属性
-
你不能使用 CSS 属性来添加 !important
-
你可以使用 jquery 的 .animate 函数,但我不确定它是否会让你添加 !important 属性!
-
你可以很容易地做到这一点,而不需要 jQuery 缓动。只需通过 CSS 将
transition: all 0.5s ease添加到#tab_toggle #tab_toggle_bg,它就会产生缓动效果。 Here 是从 Varun 的小提琴中提取的样本。 -
@Harry 绝对正确!你应该把这个作为答案:) 我想知道为什么我不考虑尝试这个
标签: javascript jquery css css-transitions transition