简体   繁体   English

如何删除:hover?

[英]How do I remove :hover?

I have a small problem with a script.我有一个脚本的小问题。
I want to have a default action on :hover for clients with Javascript disabled, but for those with Javascript enabled I want another action (actually... same action, but I want to add a small transition effect).对于禁用了 Javascript 的客户端,我想在:hover上执行默认操作,但对于启用了 Javascript 的客户端,我想要另一个操作(实际上......相同的操作,但我想添加一个小的过渡效果)。

So... How can I do this?那么...我该怎么做? I am using jQuery.我正在使用 jQuery。

Apply two classes to the relvant element. 将两个类应用于relvant元素。 one contains the hover behaviour, and one contains all the other styling. 一个包含悬停行为,一个包含所有其他样式。

You can then use the jquery 然后,您可以使用jquery

$(element).removeClass('hover');

method to remove the class with the hover behaviour and then apply whatever you want using 使用悬停行为删除类的方法,然后应用您想要的任何内容

$(element).bind('mouseover', function () { doSomething(); });
$(element).bind('mouseout', function () { doSomething(); });

How about putting the :hover fall-back in a stylesheet that is only loaded if javascript is disabled? :hover回退放在仅在javascript被禁用时加载的样式表中怎么样?

<noscript>
  <link href="noscript.css" rel="stylesheet" type="text/css" />
</noscript>

Here is a solution without hack classes: 这是一个没有黑客类的解决方案:

CSS: CSS:

a {color: blue;}
a:hover {color: red;}

jQuery (uses jQueryUI to animate color): jQuery(使用jQueryUI为颜色设置动画):

$('a').hover( 
  function() {
    $(this)
      .css('color','blue')
      .animate({'color': 'red'}, 400);
  },
  function() {
    $(this)
      .animate({'color': 'blue'}, 400);
  }
);

demo 演示

I think the best approach would be to leave the :hover behavior as a fall-back for non-javascript users and then use JQuery to create mouseover and mouseout event handlers to create a different effect for javascript-enabled users. 我认为最好的方法是将:hover行为留作非javascript用户的后备,然后使用JQuery创建mouseover和mouseout事件处理程序,为启用javascript的用户创建不同的效果。

JQuery Javascript Library - Events/mouseover JQuery Javascript库 - 事件/鼠标悬停

It's a very old question but I feel the urge to tell that modernizr provides a very good way to implement these kind of fallbacks. 这是一个非常古老的问题,但我觉得要告诉那个modernizr提供了一种非常好的方法来实现这种后备。

Just include modernizr in the head and you can do these: 只需在头部包含modernizr,您就可以执行以下操作:

.no-js a:hover {
   set background color and stuff like that
   for cases when no javascript is present
}

On the other hand if you want to do this the other way and only set css when js is present 另一方面,如果您想以另一种方式执行此操作,并且仅在存在js时设置css

.js a:hover {
   set background color to default
   and the text decoration
}

It is more or less the same solution as adding a hover tag to the markup, but a little more robust. 它与向标记添加悬停标记或多或少是相同的解决方案,但更加健壮。

I HAVE FOUND YOUR SOLUTION 我找到了你的解决方案

basically you start out by redefining what you did with the css hover. 基本上你是从重新定义你用css悬停所做的事情开始的。 (naturally you would do this by dynamically pulling the information from the style) then do whatever you want to in jquery with mouseover/mouseout events (当然你会通过从样式中动态提取信息来实现这一点)然后使用mouseover / mouseout事件在jquery中执行任何操作

this allows you to keep the :hover event in your css because jquery is binding your original styles to the element. 这允许你在你的css中保留:hover事件,因为jquery将你的原始样式绑定到元素。 In essence disabling the :hover event. 实质上禁用了:悬停事件。

if your css is: 如果你的css是:

a.class {
  background-color: #000000;
  background-position: 0 0;
  }
a.class:hover {
  background-color: #ffffff;
  background-position: 100% -50px;
  }

your jquery would be somthing like: 你的jquery会是这样的:

jQuery("a.class").each(function () {

  var oldBackgroundColor = jQuery(this).css("backgroundColor");
  var oldBackgroundPosition = jQuery(this).css("backgroundPosition");

  jQuery(".class").css({
        'backgroundColor':oldBackgroundColor,
        'backgroundPosition':oldBackgroundPosition
  });

})
.bind("mouseover", function() {

  doSomething();

})
.bind("mouseout", function() {

  doSomething();

})

You could strip all :hover style rules from document.styleSheets. 您可以从document.styleSheets中删除所有:悬停样式规则。

Just go through all CSS styles with JavaScript and remove all rules, which contain ":hover" in their selector. 只需使用JavaScript浏览所有CSS样式并删除所有规则,这些规则在其选择器中包含“:hover”。 I use this method when I need to remove :hover styles from bootstrap 2. 我需要删除时使用此方法:bootstrap 2中的悬停样式。

_.each(document.styleSheets, function (sheet) { 
    var rulesToLoose = []; 
    _.each(sheet.cssRules, function (rule, index) { 
        if (rule.selectorText && rule.selectorText.indexOf(':hover') > 0) { 
            rulesToLoose.push(index);
        }
    });

    _.each(rulesToLoose.reverse(), function (index) {
        if (sheet.deleteRule) {
            sheet.deleteRule(index);
        } else if (sheet.removeRule) {
            sheet.removeRule(index);
        }
    });
});

I did use underscore for iterating arrays, but one could write those with pure js loop as well: 我确实使用下划线来迭代数组,但是也可以用纯js循环编写那些:

for (var i = 0; i < document.styleSheets.length; i++) {}

You can globally enable behavior across the entire document by using a single css rule, and then disable that rule in one statement in javascript, while installing the new event handler. 您可以使用单个css规则在整个文档中全局启用行为,然后在安装新事件处理程序时在javascript中的一个语句中禁用该规则。

Add a class to your html body tag: 在html body标签中添加一个类:

<html>
  <body class="use-hover">
  ...

Default behavior in your css, let's say to bold links on hover: 你的css中的默认行为,让我们说悬停时的粗体链接:

body.use-hover a:hover
  font-weight: bold

And in your js, when run will remove the default behavior and do something else: 在你的js中,运行时将删除默认行为并执行其他操作:

$(function() {
  $('body').removeClass('use-hover');
  $('a').live('mouseover', function() {
    // Do something when hovered
  }).live('mouseout', function() {
    // Do something after hover is lost
  });
});
You can redraw element after click
function redraw(element) {
if (!element) { return; }

let n = document.createTextNode(' ');
let disp = element.style.display;  // don't worry about previous display style

element.appendChild(n);
element.style.display = 'none';

setTimeout(function(){
    element.style.display = disp;
    n.parentNode.removeChild(n);
}, 100); // you can play with this timeout to make it as short as possible

} }

Vanilla JS version of Mikael Lepistö's answer Mikael Lepistö 答案的香草 JS 版本

for (var i = 0; i < document.styleSheets.length; i++) {
    var sheet = document.styleSheets[i];
    var rulesToLose = [];
    for (var j = 0; j < sheet.cssRules.length; j++) {
        var rule = sheet.cssRules[j];

        if (rule && rule.selectorText && rule.selectorText.indexOf(':hover') >= 0) {
            rulesToLose.push(j);
        }
    }

    // Iterate backwards to prevent pointing to the wrong index while sheet rules get deleted
    for (var k = rulesToLose.length - 1; k >= 0; k--) {
        sheet.deleteRule(rulesToLose[k]);
    }
}

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

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