2015-04-22 17 views
3

当用户通过www.example.com/#div4来到一个网站时,我希望网址中指定的部分用#F37736(橙色)突出显示,然后在2秒内顺利地转换回#00A087(默认颜色)。如何从网址中的哈希中突出显示特定的div?

div将突出显示为“固定导航栏”的class

我已经试过:

var hash = false; 
checkHash(); 

function checkHash(){ 
    if(window.location.hash != hash) { 
    hash = window.location.hash; 
    } t=setTimeout("checkHash()",400); 
}; 
+0

您正在寻找这样的事情:http://api.jqueryui.com/highlight-effect/ –

+0

这不是完全清楚,什么是你想要做什么呢? – adeneo

回答

6

您可以查找散列,然后通过它的类名称来定位除法。您将立即将div的颜色更改为橙​​色,然后将其动画恢复为默认颜色。

尽管如此,您将需要包含jQuery Color library来为background-color设置动画,因为vanilla jQuery无法为background-color生成动画效果。你也可以使用jQuery UI's highlight effect,认为UI库的体积稍大一些。

$(document).ready(function() { 
    var hash = window.location.hash; 
    $('.' + hash).css('background-color', '#F37736').animate({ 
     backgroundColor: '#00A087' 
    }, 2000); 
}); 
0

我假设,你想突出显示某些事件的背景颜色。 尝试添加此CSS到您的代码。这将突出显示悬停时的背景颜色。

.fixed-nav-bar { 
    background-color: #f37736; 
} 

.fixed-nav-bar:hover { 
    background-color: #00a087; 
    -webkit-transition: background-color 2000ms linear; 
    -moz-transition: background-color 2000ms linear; 
    -o-transition: background-color 2000ms linear; 
    -ms-transition: background-color 2000ms linear; 
    transition: background-color 2000ms linear; 
} 

希望这会帮助你。

+0

想要一个临时事件,而不是事件:悬停抱歉.. –