【问题标题】:jQuery - Error in parsing value 'opacity' with .animate()jQuery - 使用 .animate() 解析值“不透明度”时出错
【发布时间】:2013-06-18 02:23:35
【问题描述】:

我制作了一个简单的 jQuery 脚本来淡入和淡出包含文本的 div。它在实践中工作正常,但我收到此错误,因为在某些时候不透明度超出了常规值范围 我必须说我在 javascrpt 和 jQuery 方面真的是个新手

function haikuSwitch(){
var $active = $("#haikuwrapper .haiku.active");
if ($active.lenght == 0) $active = $("#haikuwrapper .haiku:first");

var $next
$sibl = $active.siblings();
if ($sibl.lenght == 0) $next = $("#haikuwrapper .haiku:last");
else {
    var randnum = Math.floor(Math.random() * $sibl.length );
    $next = $( $sibl[ randnum ] );
}

$active.animate({'opacity': 0}, 2000)
    .css({'z-index': '10'})
    .removeClass('active');

$next.css({ 'opacity': 0, 'z-index': '10'})
    .addClass('active')
    .animate({'opacity': 1}, 2000);
    }

$(function() {
  $("#haikuwrapper .haiku:first").addClass('active')
    .css({ 'opacity': 1, 'z-index' : '10'});
      setInterval( "haikuSwitch()", 10000 );
    });

我找不到这个问题的根源。帮助?

【问题讨论】:

  • 1.您在浏览器中看到任何错误吗?我看到“长度”作为“长度”有拼写错误???? 2.你关闭了haikuSwitch功能吗? 3.不要通过 $(function(){}); 使代码复杂化; ...这个函数的内容将在没有任何匿名函数包装的情况下工作

标签: jquery jquery-animate opacity


【解决方案1】:

我认为不透明度范围没有问题。

我看到了一个语法错误...你拼错了height

$active.lenght 应该是$active.length

当empty or null$next 时,我认为它会引发错误的一个地方是$next

因此,为该选择器添加检查条件应该是个好主意..

$next.length && $next.css({ 'opacity': 0, 'z-index': '10'})

【讨论】:

  • 啊,我确实做到了!谢谢!但是,if / else 位不足以确保 $next 始终存在吗?
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 2011-08-29
  • 2021-07-30
  • 2012-05-01
  • 2010-12-31
  • 2019-08-16
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多