[英]jQuery: Slidedown li on Hover and SlideUp on mouse out
How can i make this with jquery. 我怎么能用jquery做这个。 When mouse is over Product 1, to slidedown only elements on this category, for example :
当鼠标位于产品1上时,仅滑动此类别中的元素,例如:
Item1 Item2 Item3 Item4 Item1 Item2 Item3 Item4
And when the mouse is leave the Product 1, to slideup. 当鼠标离开产品1时,进行滑动。 The same with another ones
与另一个相同
<div>
<li>Product1</li>
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>
<li>Product 2</li>
<ul>
<li>Item product 2</li>
<li>Item product 2</li>
<li>Item product 2</li>
</ul>
<li>Product 3</li>
<ul>
<li>Item product 2</li>
<li>Item product 2/li>
</ul>
</ul>
</div>
Check your markup - I don't think it's quite right. 检查你的标记 - 我认为这不对。 That will help big time.
这将有助于大时间。 Also, I would change it to have produt1 to be an anchor tag or something - just something to give the user the impression they can do something with that element.
此外,我会改变它以使produt1成为锚标签或其他东西 - 只是为了给用户提供他们可以用该元素做某事的印象。
Here is a jsFiddle 这是一个jsFiddle
CSS CSS
ul li ul {
display: none;
}
jQuery jQuery的
$('ul li a').hover(function(event){
$(this).next('ul').slideToggle('fast', function(){
// Done.
});
event.preventDefault();
});
You could also change the .hover
to use .click
or some other event. 你也可以改变
.hover
使用.click
或其他一些事件。
Firstly, your HTML is incorrect- ul
can only contain li
directly- and you are missing some tags, you need to change it to: 首先,您的HTML不正确 -
ul
只能直接包含li
而且您缺少某些标记,您需要将其更改为:
<div>
<ul>
<li>Product1
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>
</li>
<li>Product 2
<ul>
<li>Item product 2</li>
<li>Item product 2</li>
<li>Item product 2</li>
</ul>
</li>
<li>Product 3
<ul>
<li>Item product 2</li>
<li>Item product 2/li></li>
</ul>
</li>
</ul>
</div>
Secondly, you can actually achieve this using pure CSS to animate on the max-height
property, thereby maintaining a good seperation of concerns between content (HTML), function (JS) and style (CSS)- and reducing uneccessary overhead. 其次,您可以使用纯CSS在
max-height
属性上实现动画,从而在内容(HTML),函数(JS)和样式(CSS)之间保持良好的关注 - 并减少不必要的开销。
ul li ul{
overflow:hidden;
max-height:0;
transition:max-height .5s ease-in;
}
ul li:hover ul{
max-height:100px;
}
Try something like that. 尝试类似的东西。
//style
.product {
display: none;
}
//html
<ul class="products">
<li class="product">Product1</li>
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>
<li class="product">Product 2</li>
<ul>
<li>Item product 2</li>
<li>Item product 2</li>
<li>Item product 2</li>
</ul>
<li class="product">Product 3</li>
<ul>
<li>Item product 2</li>
<li>Item product 2/li>
</ul>
</ul>
//jQuery
var product = $('.product');
product.on('hover', function (e) {
if(e.type === 'mouseenter') {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.