简体   繁体   English

Javascript Easing

[英]Javascript Easing

First thanks for helping me with my question. 首先要感谢帮我解决问题。 Stackoverflow is a great community. Stackoverflow是一个很棒的社区。 Also know that I am very new to Javascript. 也知道我对Javascript很新。

This issue concerns a web site I am building: http://www.thewanderingguru.com/m2world2/?portfolio=the-trout 此问题涉及我正在构建的网站: http//www.thewanderingguru.com/m2world2/?portfolio = the-trout

Change the width of your browser to less than 960px so that the mobile menu appears. 将浏览器的宽度更改为小于960像素,以便显示移动菜单。 Now click on the menu icon on the right hand side of the page. 现在单击页面右侧的菜单图标。 You will see the main menu bar appear and the site logo on the left readjust itself lower down on the page. 您将看到主菜单栏出现,左侧的站点徽标在页面上向下调整。

My question is how do I get the logo to not "jump" down and up. 我的问题是如何让徽标不“向上”跳跃。 I did some research and found what I think are Jquery libraries for easing transitions: such as here but I am unsure about how to implement this on my site. 我做了一些研究,发现我认为Jquery库可以简化转换:比如这里,但我不确定如何在我的网站上实现它。 Know that this site runs off of Wordpress. 知道这个站点运行Wordpress。

I will include the code that manages the class transition for the logo when the menu icon is clicked. 单击菜单图标时,我将包含管理徽标类转换的代码。 '.ubermenu-skin-vanilla.ubermenu-responsive-toggle' is the class of the menu icon which, when clicked, changes the class of the "world" logo (div id= '#logo') from 'logo1' to 'logo2' '.ubermenu-skin-vanilla.ubermenu-responsive-toggle'是菜单图标的类,单击该图标时,将“world”徽标的类别(div id ='#logo')从'logo1'更改为' LOGO2'

I tried adding CSS transitions but that didn't help ease the jumping of the #logo div. 我尝试添加CSS过渡,但这无助于缓解#logo div的跳跃。 I think javascript easing is needed but I do not know how to implement it. 我认为需要javascript缓动,但我不知道如何实现它。 Thanks again for all your help and advice. 再次感谢您的所有帮助和建议。

Here is the javascript: 这是javascript:

<script type="text/javascript">jQuery(document).ready(function($) {$('.ubermenu-skin-vanilla.ubermenu-responsive-toggle').click(function(){$('#logo').toggleClass('logo1');$('#logo').toggleClass('logo2');});});</script>

And here is my current CSS: 这是我目前的CSS:

 @media only screen and (min-width: 0px) and (max-width:959px){ .logo1{ margin-top: 20px !Important; max-width: 90px !Important; transition: margin-top,max-width .7s ease-in-out !Important; -webkit-transition: margin-top,max-width .7s ease-in-out !Important; -o-transition: margin-top,max-width .7s ease-in-out !Important; -moz-transition: margin-top,max-width .7s ease-in-out !Important; } .logo2{ margin-top: 20px !Important; max-width: 90px !Important; transition: margin-top,max-width .7s ease-in-out !Important; -webkit-transition: margin-top,max-width .7s ease-in-out !Important; -o-transition: margin-top,max-width .7s ease-in-out !Important; -moz-transition: margin-top,max-width .7s ease-in-out !Important; } .logo2{ margin-top: 170px !Important; max-width: 90px !Important; transition: margin-top,max-width .7s ease-in-out !Important; -webkit-transition: margin-top,max-width .7s ease-in-out !Important; -o-transition: margin-top,max-width .7s ease-in-out !Important; -moz-transition: margin-top,max-width .7s ease-in-out !Important; } } 

PS. PS。 I know that in my CSS I'm using a lot of '!Important' which isn't best practice but the theme I'm using keeps overriding my code unless I do. 我知道在我的CSS中我使用了很多'!重要'这不是最佳实践,但我正在使用的主题不断覆盖我的代码,除非我这样做。 Thanks! 谢谢!

transition: margin-top,max-width .7s ease-in-out !Important;

You are applying the transition property to two properties but specify only one duration (etc.). 您正在将transition属性应用于两个属性,但仅指定一个持续时间(等)。 This is how Firefox interprets that: 这就是Firefox解释的方式:

CSS分析:关键属性的持续时间为0秒

Notice how the first duration is always 0s ? 注意第一个持续时间总是0s That's the issue (always look into the debugger and CSS analyzer, etc. first)! 这就是问题(首先要考虑调试器和CSS分析器等)!

Fortunately, Firefox also reformats the property in such a way that we know how to change the syntax and apply the values correctly: 幸运的是,Firefox还以一种我们知道如何更改语法并正确应用值的方式重新格式化属性:

transition: margin-top .7s ease-in-out,max-width .7s ease-in-out !important;

Now both transition properties have a duration and both have a timing function. 现在两个过渡属性都有持续时间,并且具有定时功能。 Apply this to all your rules that use multiple transition properties and the transition at least works. 将此应用于您使用多个转换属性的所有规则,并且转换至少有效。 Now you only need to tweak the values a bit and you should be fine. 现在你只需稍微调整一下这些值就可以了。

Also: it's !important , not !Important . 另外:它是!important ,不是!Important

You can set the duration of the animation when calling toggleClass by passing an additional parameter representing the number of milliseconds of the animation. 您可以通过传递表示动画毫秒数的附加参数来调用toggleClass时设置动画的持续时间。

Instead of calling: 而不是打电话:

<script type="text/javascript">jQuery(document).ready(function($) {$('.ubermenu-skin-vanilla.ubermenu-responsive-toggle').click(function(){$('#logo').toggleClass('logo1');$('#logo').toggleClass('logo2');});});</script>

try to use something like 试着用类似的东西

<script type="text/javascript">jQuery(document).ready(function($) {$('.ubermenu-skin-vanilla.ubermenu-responsive-toggle').click(function(){$('#logo').toggleClass('logo1',1000);$('#logo').toggleClass('logo2', 1000);});});</script>

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

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