Javascript 使用jquery悬停增加div的不透明度

Javascript 使用jquery悬停增加div的不透明度,javascript,jquery,html,css,opacity,Javascript,Jquery,Html,Css,Opacity,我使用jquery hover在div网格中递增地增加div的不透明度,每次鼠标在该div上移动。出于某种原因,当我向包含不透明度的变量添加增量时,比如0.1,而不是从1.1223408增加到2.1223408,它会递增到1.12234080.1。它似乎把它当作一个字符串而不是一个数字 我想开始工作的代码是: function opacity(){ $('.cell').css("backgroundColor", "black"); $('.cell').css("opaci

我使用jquery hover在div网格中递增地增加div的不透明度,每次鼠标在该div上移动。出于某种原因,当我向包含不透明度的变量添加增量时,比如0.1,而不是从1.1223408增加到2.1223408,它会递增到1.12234080.1。它似乎把它当作一个字符串而不是一个数字

我想开始工作的代码是:

function opacity(){

    $('.cell').css("backgroundColor", "black");
    $('.cell').css("opacity", 0);

    $('.cell').hover(function(){

    var value = $(this).css("opacity");

    if(value<1){
        value += 0.1;   
        $(this).css("opacity", value);
    }
}
函数不透明度(){
$('.cell').css(“背景色”、“黑色”);
$('.cell').css(“不透明度”,0);
$('.cell').hover(函数(){
var值=$(this.css(“不透明度”);

如果(value代码中的悬停函数缺少一个端括号/方括号,请参见以下内容:

function opacity(){

  $('.cell').css("backgroundColor", "black");
  $('.cell').css("opacity", 0);

  $('.cell').hover(function(){
    var value = $(this).css("opacity");

    if(value<1){
        value += 0.1;   
        $(this).css("opacity", value);
    }
  }, function(){
    //do something on mouseout
  });

}
函数不透明度(){
$('.cell').css(“背景色”、“黑色”);
$('.cell').css(“不透明度”,0);
$('.cell').hover(函数(){
var值=$(this.css(“不透明度”);

如果(value代码中的悬停函数缺少一个端括号/方括号,请参见以下内容:

function opacity(){

  $('.cell').css("backgroundColor", "black");
  $('.cell').css("opacity", 0);

  $('.cell').hover(function(){
    var value = $(this).css("opacity");

    if(value<1){
        value += 0.1;   
        $(this).css("opacity", value);
    }
  }, function(){
    //do something on mouseout
  });

}
函数不透明度(){
$('.cell').css(“背景色”、“黑色”);
$('.cell').css(“不透明度”,0);
$('.cell').hover(函数(){
var值=$(this.css(“不透明度”);
if(value
.css(“不透明”)
将返回字符串。因此,在执行某些数学操作之前,必须使用
parseFloat
将其转换为数值

$('.cell').hover(function(){
  var value = parseFloat($(this).css("opacity"));    
  if(value<1){
    value += 0.1;           
    $(this).css("opacity", value);
  }
});
.css(“不透明”)
将返回字符串。因此,在执行某些数学操作之前,必须使用
parseFloat
将其转换为数值

$('.cell').hover(function(){
  var value = parseFloat($(this).css("opacity"));    
  if(value<1){
    value += 0.1;           
    $(this).css("opacity", value);
  }
});

很可能是以字符串的形式获取的。为确保获取正确的类型,请使用
parseFloat
。此外,如果您将起始不透明度更改为其他数字,可以将悬停函数钳形写入1,这样,如果从0.5开始,它就不会停止在0.95

function opacity() {

    $('.cell').css("backgroundColor", "black");
    $('.cell').css("opacity", 0);

    $('.cell').hover( function() {

       var value = parseFloat($(this).css("opacity"));
       value += 0.1;
       $(this).css("opacity", Math.min(value, 1));

    });

}

很可能是以字符串的形式获取的。为确保获取正确的类型,请使用
parseFloat
。此外,如果您将起始不透明度更改为其他数字,可以将悬停函数钳形写入1,这样,如果从0.5开始,它就不会停止在0.95

function opacity() {

    $('.cell').css("backgroundColor", "black");
    $('.cell').css("opacity", 0);

    $('.cell').hover( function() {

       var value = parseFloat($(this).css("opacity"));
       value += 0.1;
       $(this).css("opacity", Math.min(value, 1));

    });

}

修好了!谢谢!修好了!谢谢!