简体   繁体   English

在这种情况下使用CSS或Jquery有什么区别

[英]What is the difference between using CSS or Jquery in this case

I am creating a menu bar at the moment which uses 2 images designed in Photoshop , one image is normal and one image is on hover effect. 我正在创建一个菜单栏,使用Photoshop中设计的2个图像,一个图像正常,一个图像处于悬停效果。

I have seen that I can use both css and JQuery to create the hover effect, I find CSS much easier to use for example : 我已经看到我可以使用css和JQuery来创建悬停效果,我发现CSS更容易使用,例如:

image {
background-image:url ..
}

image:hover {
background-image:url ...}

on hover just change the image whereas the jquery code is a little bit longer I believe 在悬停时只是更改图像,而jquery代码有点长,我相信

If anyone of you could tell me the different between using CSS and JQuery in this case and what would be the JQuery code of doing the same thing that I have done above using CSS . 如果你们中的任何人都能告诉我在这种情况下使用CSS和JQuery之间的区别,以及使用CSS做上述事情的JQuery代码是什么。

Thanks in advance 提前致谢

A simple answer, whenerver you can do something using CSS instead of Scripting, do it. 一个简单的答案,无论你使用CSS而不是脚本做什么,都可以做到。

It makes future code editing much easier as well, since you know where the hover is applied, but with scripts, you may find it difficult to trace the event handlers, especially if they are applied using complex selectors that dont mention the name of the element you are trying to edit. 它使得将来的代码编辑也变得更加容易,因为您知道应用悬停的位置,但是使用脚本,您可能会发现很难跟踪事件处理程序,特别是如果它们是使用不提及元素名称的复杂选择器应用的话。你正在尝试编辑。

Someone else can comment on jQuery, but for all purposes, if you can achieve your desired effect with CSS, stick with that. 其他人可以评论jQuery,但是出于各种目的,如果你能用CSS实现你想要的效果,那就坚持下去吧。 The less scripts firing off in the background, the better. 在后台启动的脚本越少越好。

Note, I would use a sprite for your image and change the background position on hover, so that you aren't getting the quick flash as the browser loads the hover image. 请注意,我会为您的图像使用精灵并更改悬停时的背景位置,以便在浏览器加载悬停图像时不会获得快速闪光。

Stack the two images into one single JPG/Gif. 将两个图像堆叠成一个JPG / Gif。 If it's 50x100px, 如果它是50x100px,

image {
   background:url('/images/img.jpg') no-repeat;
}

image:hover {
   background:url('/images/img.jpg') no-repeat 0 -50px;
}

It depends on the css. 这取决于CSS。 certain stuff isn't going to be supported by older browsers, if you need to support them. 如果您需要支持它们,旧浏览器将不支持某些内容。 You should almost always do it in css instead of js if possible, with caveats of of course. 如果可能的话,你应该几乎总是用css而不是js来做,当然需要注意。 Sometimes it is far more complicated in css. 有时它在CSS中要复杂得多。 other times the browsers may not implement the things you need, (like animation) 其他时候浏览器可能无法实现您需要的东西(如动画)

Generally though if you can do it in css, and you are sure that all the browsers you need to support, can use your solution then go for the style solution over js 一般来说,如果您可以在css中完成,并且您确定需要支持的所有浏览器,可以使用您的解决方案,那么请使用js的样式解决方案

Well using jQuery you could use the mouseenter() and mouseleave() functions to add a class and remove a class accordingly, thus giving you the hover effect, but this would take more code than just CSS as you'd need the jQuery code, plus the CSS style to go along with the class you're adding. 好吧,使用jQuery你可以使用mouseenter()mouseleave()函数来添加一个类并相应地删除一个类,从而为你提供悬停效果,但这需要更多的代码而不仅仅是CSS,因为你需要jQuery代码,加上CSS样式与你要添加的类一起使用。

One thing to bare in mind though if you're bothered about mobile users is that hover effects basically add another click for a user trying to get somewhere. 有一点需要注意的是,如果你对移动用户感到困扰,那么悬停效果基本上会为尝试到达某个地方的用户添加另一个点击。 The first click will initialise the hover state, the next click will direct a user to the url. 第一次单击将初始化悬停状态,下一次单击将引导用户访问该URL。 Whereas with jQuery this won't be a problem, however, if a user has Javascript turned off in their browser it will be an issue, so a fallback will need to be taken in to consideration. 然而,使用jQuery这不会是一个问题,但是,如果用户在浏览器中关闭了Javascript,那将是一个问题,因此需要考虑回退。

So it just depends which approach you want to take. 所以它只取决于你想采取哪种方法。

CSS is much quicker because it has fewer steps to machine code. CSS更快,因为它有更少的机器代码步骤。 Javascript, which is all that jQuery is, has to go through an engine like Chromes V8. Javascript就是jQuery,它必须通过像Chromes V8这样的引擎。 Therefore it is much slower than CSS. 因此它比CSS慢得多。 You shouldn't waste time rewriting logic in javascript that is already built into the browser, as is the case with the CSS :hover pseudo selector. 您不应该浪费时间在已经内置到浏览器中的javascript中重写逻辑,就像CSS :hover伪选择器的情况一样。

To do 去做

image {
  background-image: url(image1.jpg);
}

image:hover {
  background-image: url(image2.jpg);
}

in jQuery would be: 在jQuery中将是:

$(image).on( 'mouseenter', function(){
  $(this).css({ background-image : 'url(image2.png)' })
}).on( 'mouseleave', function(){
  $(this).css({ background-image : 'url(image1.png)' })
});

Try googling a bit more... "use jquery to apply on hover affect" -> 尝试谷歌搜索更多...“使用jquery应用于悬停影响” - >

  $("p").hover(function(){
    $("p").css("background-image","url ..");
    },function(){
    $("p").css("background-image","url ......");
  });

Source: http://www.w3schools.com/jquery/event_hover.asp 资料来源: http//www.w3schools.com/jquery/event_hover.asp

As for the other part of your question, the above anwser pretty much covered it. 至于你问题的另一部分,上面的anwser几乎涵盖了它。

You can read Benefits of using CSS3 vs JQuery here enter link description here . 您可以在此处阅读使用CSS3和JQuery的好处,在此处输入链接描述 But that doesn't mean you can't use JQuery. 但这并不意味着你不能使用JQuery。

If you just want to change the image, CSS is the best way to do. 如果您只想更改图像,CSS是最好的方法。 It's much faster and more simple not only on implement but also when running website than using jQuery. 它不仅在实现上而且在运行网站时比使用jQuery更快更简单。 However, if you want a little bit effect when changing the images (like fade or moving effect...), then jQuery must be your choice. 但是,如果您在更改图像时需要一点点效果(如淡入淡出或移动效果......),那么jQuery必须是您的选择。

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

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