分享一個色塊跟隨鼠標移動的菜單代碼

demo地址:
http://www.dtzhuanjia.com/pri...
html:css

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>菜單</title>    
    <meta name="Keywords" content="">
    <meta name="description" content="">
    <link rel="stylesheet" href="http://www.dtzhuanjia.com/private/plugin/menu/index.css">
</head>
<body>
    <!--塊-->
    <ul class="hc-ul ul-block clearfix">        
        <li>菜單1</li>
        <li>菜單2</li>
        <li>菜單3</li>
        <li>菜單4</li>
        <li>菜單5</li>
        <span class="curBlock"></span>
    </ul>
    <div style="margin:20px 0;width:100%;height:1px;"></div>
    <!--線-->
    <ul class="hc-ul ul-line clearfix">        
        <li>菜單1</li>
        <li>菜單2</li>
        <li>菜單3</li>
        <li>菜單4</li>
        <li>菜單5</li>
        <span class="curBlock"></span>
    </ul>
    <script src="http://www.dtzhuanjia.com/js/basic/jquery.js"></script>
    <script src="http://www.dtzhuanjia.com/private/plugin/menu/index.js"></script>
    <script>
        var hc = new HoverChange({
            hcWidth : "200px",
            hcType : "block",
            hcBg : "#ff5943",
            hcDuration : "0.6s",
        });
        var hc1 = new HoverChange({
            hcType : "line",
            hcBg : "#0096f0"
        });
    </script>
</body>
</html>

js代碼:
//跟隨移動html

function HoverChange(obj){
    _this = this;
    _this.obj = obj||{};
    _this.type = obj.hcType;//模塊類型[block|line]
    _this.hcWidth = obj.hcWidth||"100px";//塊寬度
    _this.hcBg = obj.hcBg||"#0096f0";//塊顏色
    _this.hcDuration = obj.hcDuration||"0.5s";//塊速度
    
    //綁定事件相關
    _this.container = ".ul-"+_this.type,//當前容器
    _this.curLi = _this.container+" li",//移入的li
    _this.curBlock = _this.container+" .curBlock",//色塊
    _this.method = "_this.changing(_this)";//對應方法   
    
    //初始化方法
    _this.init(_this.curLi,_this.curBlock);
}
HoverChange.prototype ={
    init : function(li,block){
        $(block).css({
            "width" : _this.hcWidth,
            "background-color": _this.hcBg,
            "transition-duration" : _this.hcDuration,
            "margin-left" : ($(li).width()-parseInt(_this.hcWidth))*0.5+"px"
        });
        $(li).hover(function(){
            _this.index = $(this).index();//當前li的索引
            _this.liWidth = $(this).width();//當前li的寬度
            _this.changing(block);
        },function(){
            _this.index = $(this).index();//當前li的索引
            _this.liWidth = $(this).width();//當前li的寬度
            _this.changing(block);     
        });  
    },
    changing : function(block){
        //塊狀
        $(block).addClass("active").css({
            "left":_this.liWidth*_this.index+"px",
        });
    }
}

css代碼:jquery

.hc-ul{width:1200px;margin:0 auto;padding:0;}
.hc-ul{background:#f1f1f1;height:50px;position:relative;}
.hc-ul li{float:left;width:20%;text-align:center;cursor:pointer;height: 50px;line-height: 50px;position:relative;z-index:999;list-style-type:none;}
.hc-ul .curBlock {left:0;border-radius:100px;background-color:transparent;position:absolute;transition:left;}

/*塊*/
.ul-block .curBlock{height:50px;}
/*線*/
.ul-line .curBlock{bottom:1px;height:2px;}

說明:
html代碼中:chrome

var hc = new HoverChange({
    hcWidth : "200px",
    hcType : "block",
    hcBg : "#ff5943",
    hcDuration : "0.6s",
});

有四個參數:分別是色塊寬度(100px),類型(block),背景顏色("#0096f0"),速度("0.5s"),括號中爲默認值,能夠按照本身需求填寫或不填。
因爲代碼用jquery編寫,因此須要引入jquery。在chrome內核瀏覽器測試無問題。
demo截圖瀏覽器

圖中上行類型爲block,下行類型爲line測試

歡迎討論,如需轉載請註明出處,謝謝ui

相關文章
相關標籤/搜索