JavaScript 缓动

标签 javascript jquery css wordpress

首先感谢您帮助我解决问题。 Stackoverflow 是一个很棒的社区。还知道我对 Javascript 很陌生。

此问题涉及我正在构建的网站:http://www.thewanderingguru.com/m2world2/?portfolio=the-trout

将浏览器的宽度更改为小于 960 像素,以便显示移动菜单。现在单击页面右侧的菜单图标。您将看到主菜单栏出现,并且左侧的站点 Logo 在页面下方重新调整。

我的问题是如何让 Logo 不“跳跃”上下。我做了一些研究,发现了我认为用于缓解过渡的 Jquery 库:例如 here但我不确定如何在我的网站上实现这一点。要知道该网站是由 Wordpress 运行的。

我将包含在单击菜单图标时管理 Logo 的类转换的代码。 '.ubermenu-skin-vanilla.ubermenu-responsive-toggle' 是菜单图标的类,单击该图标时,会将“world” Logo (div id= '#logo') 的类从 'logo1' 更改为 '标志2'

我尝试添加 CSS 过渡,但这并不能帮助缓解 #logo div 的跳跃。我认为需要 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>

这是我当前的 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。我知道在我的 CSS 中我使用了很多“!重要”,这不是最佳实践,但我使用的主题会不断覆盖我的代码,除非我这样做。谢谢!

最佳答案

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

您将transition 属性应用于两个 属性,但仅指定一个 持续时间(等等)。 Firefox 是这样解释的:

CSS analysis: the crucial property has a duration of 0s

注意到第一个持续时间为何总是0s?这就是问题所在(始终首先查看调试器和 CSS 分析器等)!

幸运的是,Firefox 还以这样的方式重新格式化该属性,以便我们知道如何更改语法并正确应用值:

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

现在两个转换属性都有一个持续时间,并且两个都有一个计时函数。将此应用于使用多个转换属性的所有规则,并且转换至少可以工作。现在您只需要稍微调整一下值就可以了。

另外:这是!重要,而不是!重要

关于JavaScript 缓动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135651/

相关文章:

javascript - 如何在 Elixir 中执行十六进制编码?

jquery - 如何使 jQuery 库不互相影响?

javascript - Velocity js 翻译问题

php - 即使浏览器的当前网址已更改,如何创建不可更改的 html 部分?

javascript - 如何创建倾斜(不水平)的幻灯片?

html - 是否有可能在网页中使用 CSS 将 "slide"的一个部分放在另一个之上

javascript - 使用 webpack 远程导入 Electron 库

javascript - 文档只能用 jQuery 加载

php - 如何在codeigniter 4中使用ajax

javascript - 如何用 Font Awesome 图标替换 bxSlider 控件