2010-08-11 92 views
1

我有这样的时刻:(名单是更长的时间,但是这仅仅是一个元素)jQuery的淡入淡出文本

<a href="Products.aspx" 
    onmouseover="onMouseOverCatDisplay(&quot;H5032.jpg&quot;, &quot;Go to: cars&quot;);"  
    onmouseout="onMouseOverCatDisplay(&quot;DSC_0414_SS.jpg&quot;, &quot;You see: bike&quot;);">Car</a> 

和HTML上面,我有这个JavaScript:

<script type="text/javascript" language="javascript"> 
// <![CDATA[ 
function onMouseOverCatDisplay(catimg, catnaam) 
{ 
    $("#lh").stop().animate({ color: "#1C1C1C" }, 2000); 
    $("#lh").html(catnaam); 
    $("#lh").stop().animate({ color: "#DBDBD6" }, 2000); 

    $("#imgCat").attr("src", catimg); 
} 
// ]]> 
</script> 

和这个:

<h4 id="lh">Bikes</h4> 
<img id="imgCat" src="img/bike.jpg" /> 

现在一切正常,但动画不起作用。

我想淡出H4,替换文本,然后褪色回。

编辑设置图像源也使用jQuery,而不是JavaScript的

EDIT2

重写了该部分,以便它不使用mouseout和mouseover来触发javascript。但不能想出一个办法,以另一种放慢参数传递给jQuery的(图像)

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('div.divLeftCatMenu a').hover(
     function() { 
      $(this).stop().animate({ color: '#E90E65', borderBottomColor: '#E90E65' }, 1000); 
      var catn = $(this).attr('title'); 
      $("#lh").html(catn); 
     }, 
     function() { 
      $(this).stop().animate({ color: '#CCC6C6', borderBottomColor: '#3e3e3e' }, 1000); 
      var catn = $("a.subCatLinksSelected").attr('title'); 
      $("#lh").html(catn); 
     }); 
+0

您是否尝试过用''' – 2010-08-11 13:39:35

+0

更换''"e;在JavaScript为什么你使用'this.document.getElementById(..)'?我猜'document.getElementById(..)'就够了。 – 2010-08-11 13:45:25

+0

Hello Avinash,当我使用单引号并替换了getelementbyid部分时,我得到了'未终止的字符串字面值' – 2010-08-11 14:08:54

回答

1

最终演示:http://jsfiddle.net/VdFD9/

如果你想做到这一点使用title属性,只需修改下面的代码,并将其设置为参考链接你的title属性(image links如果你想)。

HTML

<a class="subCatLinksSelected" href="#" style="cursor:pointer;" title="cars"> cars </a> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
<a class="subCatLinksSelected" href="#" style="cursor:pointer;" title="bikes"> bikes</a> 
<br /> 
<br /> 
<h4 id="lh">Bikes</h4> 

<img id="ctl00_ContentPlaceHolder1_men_imgCat" src="http://www.gravatar.com/avatar/6dec5eb240c49d979542d7cef64e9a8d?s=128&d=identicon&r=PG" /> 

的JavaScript

var arr = []; 
arr[0] = "http://www.gravatar.com/avatar/6dec5eb240c49d979542d7cef64e9a8d?s=128&d=identicon&r=PG"; 
arr[1] = "http://www.gravatar.com/avatar/e555bd971bc2f4910893cd5b785c30ff?s=128&d=identicon&r=PG"; 
arr[2] = "http://www.gravatar.com/avatar/54d38793d7a407446999b33b81d607fd?s=128&d=identicon&r=PG"; 

//for instance i'm using an array to cache your image links 
//if you can want these links as your anchor tag "title" attrib just modify the below code 

$(document).ready(function() { 

    var which_image = null; //detect which Image to use 
     $(".subCatLinksSelected").hover(function() { 

      var catn = $(this).attr('title'); 
      if(catn == 'cars') {   
       which_image = arr[1]; 
      } else { 
       which_image = arr[2]; 
      }  
      onMouseOverCatDisplay(which_image, 'Go to: ' + catn,'#0099f9'); 

     },function() { 

      var catn = $("a.subCatLinksSelected").first().attr('title'); 
      which_image = arr[0] 
      onMouseOverCatDisplay(which_image,'You see: ' + catn, '#000'); 

     }); 
    }); 


function onMouseOverCatDisplay(catimg, catnaam, color) { 

    $('#ctl00_ContentPlaceHolder1_men_imgCat').attr('src',catimg); 

    $("#lh") 
     .css({opacity:0.2,color:"#1c1c1c"}) 
     .html(catnaam) 
     .css({color: color}) 
     .stop() 
     .animate({opacity:1 },2000); 
    } 
+0

谢谢Avinash,gonne仔细看看明天(办公室结束的一天)会回到这个! – 2010-08-11 14:56:08

+0

我现在这http://jsfiddle.net/zQzEY/1/ 但我想从 2010-08-11 15:02:31

+0

确定,我会检查它为你:) – 2010-08-11 15:06:33

2
0
$(document).ready(function() { 
    $('div.divLeftCatMenu a').hover(
    function() { 
     $(this).stop().animate({ color: '#E90E65', borderBottomColor: '#E90E65' }, 1000); 
     var catn = $(this).attr('title'); 
     $("#lh").html(catn); 
    }, 
    function() { 
     $(this).stop().animate({ color: '#CCC6C6', borderBottomColor: '#3e3e3e' }, 1000); 
     var catn = $("a.subCatLinksSelected").attr('title'); 
     $("#lh").html(catn); 
    }); 

应该工作,但是,如果你想访问你需要将其绑定到每个图像功能...试试这个:

$(document).ready(function(){('div.divLeftCatMenu a')。hover( function(){(this).stop()。animate({color:'#E90E65', borderBottomColor:'#E90E65'},1000); var catn = $(this).attr('title'); $(“#lh”)。html(catn); } .bind($(一些选择的图像)), 函数(){$ (本).stop()动画({颜色: '#CCC6C6',borderBottomColor: '#3e3e3e' },1000); var catn = $(“a.subCatLinksSelected”)。attr('title'); $(“#lh”)。html(catn); } .bind($(某些选择器为您的图片)));

然后,您就可以使用访问图像中的每个功能,如this.src