简体   繁体   English

jQuery:滚动时平滑动画字体大小更改

[英]jQuery: smooth animate font-size change on scroll

I have a jQuery animation function set-up to change the font size of the .header-wrap text when the document scrolls beyond 50px. 我有一个jQuery动画功能设置,当文档滚动超过50px时,可以更改.header-wrap文本的字体大小。 This works, I'm not so keen on the solution I've got though, it's not very smooth, ideally I'd like the font-size to change smoothly as you scroll down the page, instead of having to stop scrolling the re-initiate the animation etc. It's just a bit jagged. 这是有效的,我不是那么热衷于我已经得到的解决方案,它不是很顺利,理想情况下我希望在向下滚动页面时平滑地改变字体大小,而不是必须停止滚动 - 启动动画等等。它只是有点锯齿状。
jsFiddle demo: http://jsfiddle.net/cXxDW/ jsFiddle演示: http//jsfiddle.net/cXxDW/
HTML: HTML:

<div class="content-wrap"></div>
<div class="header-wrap">hello
    <br/>hello
    <br/>hello
    <br/>
</div>

jQuery: jQuery的:

$(document).scroll(function () {
    if (window.scrollY > 50) {
        $(".header-wrap").stop().animate({
            fontSize: '17px'
        });
    } else {
        $(".header-wrap").stop().animate({
            fontSize: '25px'
        });
    }
});

Any suggestions / better, smoother solutions to the one I've got are more than welcome and greatly appreciated! 任何建议/更好,更顺畅的解决方案,我得到的非常欢迎,非常感谢!

You wont get a very smooth animation of fontSize . 你不会得到一个非常流畅的fontSize动画。 But if you only need compatibility with modern browsers you could animate zoom instead. 但是,如果您只需要与现代浏览器兼容,则可以为zoom设置动画。

You would have to fix your margins and positioning since those will be animated as well. 你必须修复你的边距和定位,因为那些也会被动画化。

Here is a proof-of-concept fiddle . 这是一个概念验证小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM