English 中文(简体)
简化婚姻手续
原标题:Simplifying jQuery - easy

I m 不是编纂java的最佳方法,但这是我可以提出的最佳办法:

http://jsfiddle.net/p77wC/

我只想简化以下文字:

$( #about )
  .hover(function() {
    $( #bg_mask ).stop().animate({ "height": "100px", },  1500 );
  }, function() {
    $( #bg_mask ).stop().animate({ "height": "40px", },  1500 );
  });

$( #blog )
  .hover(function() {
    $( #bg_mask ).stop().animate({ "height": "180px", },  1500 );
  }, function() {
    $( #bg_mask ).stop().animate({ "height": "40px", }, 1500 );
  });
$( #contact )
  .hover(function() {
    $( #bg_mask ).stop().animate({ "height": "250px", },  1500 );
  }, function() {
    $( #bg_mask ).stop().animate({ "height": "40px", }, 1500 );
  });

我确信,可以撰写一份可以归纳/简化我做些什么的发言。

从根本上说,这就是:

if hover over #about then ->animate width:x, if hover over #blog then ->animate width:xx, etc, etc, else if -> animate width:40px

我很想知道,即使你不愿意编纂该守则,即使你刚才向我指出了正确的方向,我还是很有意义。

乘客。

最佳回答
function process(id, height) {  
  $("#" + id).hover(function() {
    $( #bg_mask ).stop().animate({ "height": height + "px", },  1500 );
  }, function() {
    $( #bg_mask ).stop().animate({ "height": "40px", },  1500 );
  });
}

process("about",   100);
process("blog",    100);
process("contact", 250);
问题回答

奥基斯这里的版本并不要求任何额外的代码、每一代的休息室,并根据粗略的数学给他们一个估算。

$( #menu_wrapper div:not(#bg_mask) ).each(function(i) {
    $(this).data( animID ,i-1);
    $(this).hover(function() {           
        $( #bg_mask ).stop().animate({ "height": ((($(this).data( animID )*80)+100)-10)+"px" },  1500 );
      }, 
      function() {
            $( #bg_mask ).stop().animate({ "height": "40px" }, 1500 );
      });
});

rel=“nofollow”>fiddle





相关问题
Detect months with 31 days

Is there an analogous form of the following code: if(month == 4,6,9,11) { do something; } Or must it be: if(month == 4 || month == 6 etc...) { do something; } I am trying to write an if ...

&& (AND) and || (OR) in IF statements

I have the following code: if(!partialHits.get(req_nr).containsKey(z) || partialHits.get(req_nr).get(z) < tmpmap.get(z)){ partialHits.get(z).put(z, tmpmap.get(z)); } where partialHits ...

If / else order sequence issue

I have the following set up, a ddl (ddlProd, radBuyer) and autocomplete text box (txtProdAC, radProd) that when populated and their respective radio buttons are selected, a grid view of the data is ...

PHP if or statement not working

We are trying to use the below piece of code if (($_GET[ 1 ] != "1") || ($_GET[ 1 ] != "2")) { When we try this no matter what value the variable has it will evaluate as true even when data is ...

C++ String manipulation - if stament

I have the following code that works correctly. However after I add an else statement anything always evaluates to else wgetstr(inputWin, ch); //get line and store in ch variable str = ch; ...

Are one-line if / for -statements good Python style?

Every so often on here I see someone s code and what looks to be a one-liner , that being a one line statement that performs in the standard way a traditional if statement or for loop works. I ...

Which is faster - if..else or Select..case?

I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all! If var = 1 then Command for updating database ElseIf var = 2 then ...

热门标签