2014-06-12 79 views
2

好,所以,我的繼承人代碼:jQuery的麻煩的.css()

$(document).ready(function() { 
    $('.topbuttons').mouseenter(function() { 
     $(this).css("margin-bottom", "65px").height(60).width(110); 
    }); 
    $('.topbuttons').mouseleave(function() { 
     $(this).height(50).width(100); 
    }); 
}); 

如果我拿出的.css( 「下邊距」, 「65px」)一切完美,股利( .topbuttons)變得更大時,它的陰影。但是,每次按下.top按鈕時都會下壓div.topbuttons高度會增加。所以我想我可以將.topbuttons的底部邊距降低10px,因爲我們正在增加.topbuttons的大小10px。但它似乎沒有工作,我不知道爲什麼。請幫助

+2

小提琴請??? –

+0

作爲@Roko C. Bulijan寫道,如果可能與CSS然後只使用CSS。 ;) –

+0

到目前爲止的三個答案和很多意見,仍然沒有jsfiddle ... –

回答

7

我會做它在純CSS:jsBin demo

.topbuttons{ 
    /* OTHER STYLES HERE*/ 
    height: 60px; 
    width: 110px; 
    margin-bottom:65px; 
    transition: all 0.4s; /* if you want to apply some animation :) */ 
} 
.topbuttons:hover{ 
    height: 50px; 
    width: 100px; 
    margin-bottom:75px; /* increase margin bottom if you don't want 
          to make move the div below*/ 
} 
1

使用.height和.WIDTH的是用的CSS 錯誤的方法試試這個:

$(this).css({'margin-bottom':'65px', width: '110px', height: '60px' }); 
+2

'height(xy)'是'.css({height:xy})的縮寫' –

+0

@Roko C.Buljan。我知道這是一個簡寫......但是,如果我們使用.css ,那麼我們應該在.css中寫入寬度和高度而不是單獨。 –

0

如果問題與邊緣底部,那麼爲什麼你不分開這條線。

$(document).ready(function() { 
    $('.topbuttons').mouseenter(function() { 
     $(this).css("margin-bottom", "65px"); 
     $(this).height(60).width(110); 
    }); 
    $('.topbuttons').mouseleave(function() { 
     $(this).height(50).width(100); 
    }); 
}); 

或者刪除jQuery和使用CSS做

.topbuttons:hover{ 
    margin-bottom:65px; 
    height:60px; 
    width:100px; 
} 

我推薦的第二個選項...

+0

這是好的,但它沒有解決OP在按鈕懸停時按下內容的問題。 –

+0

不,他說下降問題會降低底部利潤。爲此,他使用底部邊距。他試圖增加高度10px並減少底部邊距10px .... – Shail

+0

真的嗎? *「如果我取出.css(」margin-bottom「,」65px「),一切都可以正常工作,div(.topbuttons)在被遮蓋時會變大。**但是,每次按下.topbutton時, 「* –

0

我想通了,我的代碼最終看上去像這樣,在CSS: #topbuttonsrow a div:hover { color:maroon; background-color:#f05e35; height:60px; width:110px; margin:50px 25px 65px 25px;

#topbuttonsrow是我的5個div的頂部。頂部