【问题标题】:Modifying a simple jquery function to change image opacity修改一个简单的jquery函数来改变图像不透明度
【发布时间】:2023-03-04 15:14:01
【问题描述】:
function toggle() {
  if ($(this).is(".open")) {
    $(this).animate({
      width: 400
    }, 500);
  } else {
    $(this).animate({
      width: $(this).data("width")
    }, 500);
  }
  $(this).toggleClass("open");
}
$("#portfolio_img1").click(function() {
  if (!$(this).data("width")) {
    var img = new Image();
    var _this = $(this);
    img.onload = function() {
      _this.data("width", this.width);
      toggle.call(_this);
    }
    img.src = $(this).css("backgroundImage").replace("url(\"", "").replace("\")", "");
  } else {
toggle.call(this);
  }
});

函数toggle 创建一个类“.open”,将图像宽度从裁剪宽度更改为全宽。在我的 CSS 中,我指定图像的不透明度为 0.5,鼠标悬停时变为不透明度 1。我还希望将具有“打开”类的图像的不透明度更改为 1,但是当我在 CSS 中执行 .open { opacity: 1 } 时,它没有任何影响。如何修改我的函数,以便在应用“open”类时,图像不透明度变为 1,而当“open”类被删除时,它又回到 0.5?谢谢

【问题讨论】:

  • CSS -> .open {opacity:1!important;} - 根据你添加该类的位置,你可能需要编写 .open img{opacity:1!important;}跨度>

标签: jquery css function opacity


【解决方案1】:

这样的事情可能会有所帮助:

//从悬停更改为检查您的情况下打开的类 $(这个).hover( 功能() { $(this).stop().animate({ opacity: 1.0 }, 800); }, 功能() { $(this).stop().animate({ opacity: 0.5 }, 800); }) });

希望对你有帮助

【讨论】:

  • 我不想在悬停时改变不透明度,我已经用 css 做到了。我希望通过函数将 opacity = 1 应用于“open”类。
猜你喜欢
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2013-06-30
  • 2012-05-03
  • 2013-02-26
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多