简体   繁体   English

悬停通过js或CSS?

[英]Hover through js or css?

Having a div element, I can style its bg-color through either Javascript or CSS . 有一个div元素,我可以通过JavascriptCSS其bg-color的CSS Is there any difference between the two, or which one should we use? 两者之间有什么区别,还是应该使用哪一个?

div:hover {background-color:gray;}

or 要么

$( div).hover(function() {
  $( this ).css('background-color','gray');//user Jquery here

});

There are also many things that can be done through JQ and CSS. 通过JQ和CSS也可以完成许多事情。 How do we chose which one to use? 我们如何选择使用哪一个?

I'll start by answering with what I know, but I'm sure many other people know a lot more than me. 我将从回答我所知道的内容开始,但是我敢肯定,还有很多人比我了解得多。

One of the reasons to use CSS over JS is that CSS allows hardware acceleration on mobile devices . 在JS上使用CSS的原因之一是CSS允许在移动设备上进行硬件加速 Javascript on the other hand, does not. 另一方面,JavaScript则不是。 This is not so important for a simple background-color change, but if you were doing any sort of transition and moving of an element, it will look a lot smoother and be more responsive (eg when the JS is locked up with a synchronous task, the animation will still run) with CSS v jQuery/JS. 对于简单的background-color更改来说,这并不是很重要,但是,如果您进行元素的任何形式的转换和移动,它将看起来更加平滑且响应更快(例如,当JS被同步任务锁定时) ,则动画仍将运行)以及CSS v jQuery / JS。 (I'm trying to find an example of this, I remember seeing one about a year ago). (我正在尝试寻找一个例子,我记得大约一年前见过)。

Another reason to use CSS over JS is that it keeps your code divided into different parts, the CSS for the styling, and the Javascript for the logic. 在JS上使用CSS的另一个原因是,它将代码分成不同的部分,样式用的CSS,逻辑用的Javascript。 Again, this is personal preference, but is why many frameworks are more popular than others. 同样,这是个人喜好,但这就是为什么许多框架比其他框架更受欢迎的原因。

On the other hand, a reason to use JS over CSS would be if you needed to filter a list, and display a style on a few elements. 另一方面,在CSS上使用JS的原因是,如果您需要过滤列表,并在一些元素上显示样式。 In this case, it would make sense to do this with Javascript. 在这种情况下,使用Javascript这样做是有意义的。 Purists would argue that you should use .addClass('myhoverclass') instead of .style(attr:val) as it allows you to use the benefits of CSS listed above. 纯粹主义者认为您应该使用.addClass('myhoverclass')而不是.style(attr:val)因为它可以使您使用上面列出的CSS的好处。

At the end of the day, it is a matter of preference. 归根结底,这是一个优先事项。 If you are looking for raw speed, or are using intense effects, use CSS as it will perform better on mobile, and can also be forced to use hardware acceleration on a PC . 如果您正在寻找原始速度或使用强烈的效果,请使用CSS,因为CSS在移动设备上的性能会更好,并且还可能被迫在PC上使用硬件加速 On the other hand, if it is easier for you to access an element with Javascript (eg filtering a list), then use Javascript. 另一方面,如果您更容易使用Javascript访问元素(例如,过滤列表),请使用Javascript。

You can see this link for more. 您可以查看此链接以获取更多信息。

Well, that's actually a pretty interesting question. 好吧,这实际上是一个非常有趣的问题。 I'm trying to think of how you would even do a benchmark on something like this, especially since most of the time neither CSS or JavaScript are going to be doing really computationally intensive things on a web page. 我正在尝试考虑如何对这样的事情进行基准测试,尤其是因为大多数时候CSS或JavaScript都不会在网页上真正进行计算密集型的事情。

My gut feel would say that use CSS as much as possible, but don't make it into a hard and fast rule. 我的直觉会说,尽可能多地使用CSS,但不要使其成为一成不变的规则。

a:hover {
  background-color: green;
}

is better semantically then 然后在语义上更好

$('a').onmouseover(function() {
  $(this).css('background-color','green');
})

BUT

$('a').onmouseover(function() {
  if (somethingelsehappened) {
    $(this).css('background-color','green');
  }
})

would be difficult (though not impossible) in CSS. 在CSS中将是困难的(尽管并非不可能)。 You could do it in this way. 您可以通过这种方式进行。

$('a').onmouseover(function() {
  if (somethingelsehappened) {
    $(this).addClass('Green');
  }
})

a.green {
  background-color: green;
}

This would really be a slightly more awkward way to do what could have been done in JavaScript directly, but I've been thinking about it for a couple of minutes, and even here, the right solution may very well be a CSS, for example if you were setting a lot of attributes when doing the hover. 直接执行JavaScript可能确实有点尴尬,但是我已经思考了几分钟,即使在这里,正确的解决方案也很可能是CSS,例如如果您在悬停时设置了很多属性。

** Please note that none of this code is expected to work, these are just for demonstration purposes only.** **请注意,该代码均无效,仅用于演示目的。**

Refer : When a task can be accomplished by either Javascript or CSS, is it better to use CSS? 参考: 当任务可以通过Javascript或CSS完成时,使用CSS更好吗?

You'd normally use CSS simply because CSS was meant for this kind of stuff. 通常,您之所以使用CSS只是因为CSS就是用于这种东西的。 With the JS/jQuery version is more code and it implies running the JS engine and the CSS engine. JS / jQuery版本具有更多代码,这意味着要运行JS引擎和CSS引擎。 With CSS, it's just the CSS engine. 使用CSS,它只是CSS引擎。 To recap, the JS version will still need the CSS version. 回顾一下,JS版本仍然需要CSS版本。

There are times, though, when you'll want to modify styles from JS, but usually for interactions more complex than a simple hover. 有时候,您可能想从JS修改样式,但是通常来说,交互要比简单的悬停更为复杂。 Even then though, you would want to use JS just to change just the CSS class, and add styles based on those CSS classes. 即使这样,您还是希望仅使用JS来更改CSS类,并基于这些CSS类添加样式。

Let me give you a couple simple examples: 让我举几个简单的例子:

1. Change text color on hover 1.悬停时更改文本颜色

CSS properties to change: color . 要更改的CSS属性: color

You'd use CSS here. 您将在此处使用CSS。 In the old days this was possible just for anchor elements, so you'd have used JS here for browser like IE6. 在过去,仅锚元素是可能的,因此您在这里已将JS用于IE6之类的浏览器。 This is no longer the case though, so a pure CSS solution is fine. 但是,现在不再是这种情况了,因此纯CSS解决方案就可以了。

2. Show a tooltip on hover 2.显示悬停工具提示

CSS properties to change: display , top , left , right , bottom . 要更改的CSS属性: displaytopleftrightbottom

You most likely want to use JS here. 您很可能想在这里使用JS。 The tooltip markup isn't probably a child of the hovered element, so you can't reference it in your CSS. 工具提示标记可能不是悬浮元素的子元素,因此您不能在CSS中引用它。 You may go with a CSS-only solution, but that requires that every element with a tooltip to encompass markup for that tooltip. 您可能会采用仅CSS的解决方案,但这要求带有工具提示的每个元素都包含该工具提示的标记。 A lot of redundant elements. 很多多余的元素。

JS is also needed here to calculate the exact position of the toolip relative to the element and/or cursor. 这里还需要JS,以计算出toolip相对于元素和/或光标的确切位置。

Note that this is an example where you can't use a CSS class for the listed properties. 请注意,这是一个示例,其中您不能将CSS类用于列出的属性。 You will style the look & feel of the tooltip in CSS, but the actual positioning has to be done in JS. 您将在CSS中设置工具提示的外观样式,但实际位置必须在JS中完成。

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

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