有些CSS样式需要在悬停时应用于元素,而CSS样式必须直接使用javascript/jquery,而不是通过样式表或$(this).addClass('someStyle')应用,因为我将DOM元素注入到另一个页面。
我们可以应用通常的css样式
$('#some-content').css({
marginTop: '60px',
display: 'inline-block'
});我们应该如何为:hover事件添加CSS样式?
我们是否必须求助于:
$('#some-content').hover(
function(){ $(this).css('display', 'block') },
function(){ $(this).css('display', 'none') }
)发布于 2012-12-04 16:16:00
我发现使用鼠标和鼠标比悬停更好。有更多的控制。
$("#somecontent").mouseenter(function() {
$(this).css("background", "#F00").css("border-radius", "3px");
}).mouseleave(function() {
$(this).css("background", "00F").css("border-radius", "0px");
});发布于 2012-12-04 16:16:55
试试这个:
$('#some-content').hover(function(){
$(this).css({ marginTop: '60px', display: 'inline-block' });
}, function(){
$(this).css({ //other stuff });
});或使用类
$('#some-content').hover(function(){
$(this).toggleClass('newClass');
});更多信息请参阅.hover()和.toggleClass()
发布于 2012-12-04 16:17:37
您应该将它们放在悬停事件中:
var elem = $('#elem');
elem.hover(function () {
// ... :hover, set styles
}, function () {
// ... this function is called when the mouse leaves the item, set back the
// normal styles
});但是,我完全建议将CSS放在类中,并在JS中使用这些类,您应该尽可能多地拆分这些语言。
https://stackoverflow.com/questions/13707134
复制相似问题