老师,jQuery方法没滑出菜单时左侧的li怎么会多出来一块?

老师,jQuery方法没滑出菜单时左侧的li怎么会多出来一块?

http://img1.sycdn.imooc.com//climg/5c457e7d00019b4805790581.jpg

<!doctype html>

<html>

<head>

<meta charset='utf-8'>

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

<!--禁止格式检测:-->

<meta name="format-detection" content="telephone=no,email=no,address=no">

<title>移动端左滑导航菜单jQuery</title>

<link href="css/reset.css" rel="stylesheet">

<link href="css/demo-jq.css" rel="stylesheet">

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>

</head>

<body class="active">

<div class="outwrap">

<header class="header">header</header>

<div class="topbarbtn"></div>

<section class="maincontent">

<p>我是内容</p>

</section>

<footer class="footer">footer</footer>

</div>

<section class="slidebar">

<ul>

<li>左侧导航</li>

<li>左侧导航</li>

<li>左侧导航</li>

<li>左侧导航</li>

</ul>

</section>

<script>

var slideBarObj={

init:function(){

var _this=this;

//点击事件

$(".topbarbtn").on("click",function(){

if($(this).hasClass("active")){

_this.slideLeft();

}else{

_this.slideRight();

}

})

},

//显示

slideRight:function(){

$(".slidebar").animate({"width":"150px"},50);

$(".outwrap").animate({"margin-left":"150px"},50);

$(".topbarbtn").addClass("active");

},

//隐藏

slideLeft:function(){

$(".slidebar").animate({"width":"0"},50);

$(".outwrap").animate({"margin-left":"0"},50);

$(".topbarbtn").removeClass("active");

}

}

slideBarObj.init();

</script>

</body>

</html>

.header,.footer{

position:absolute;

left:0;

right:0;

height:45px;

line-height:45px;

font-size:1.4rem;

text-align:center;

}

.header{

border-bottom:1px solid red;

top:0;

}

.footer{

border-top:1px solid red;

bottom:0;

}

.outwrap{

position:absolute;

top:0;

bottom:0;

left:0;

right:0;

padding:45px 0;

margin-left:0;

}

.topbarbtn{

width:45px;

height:45px;

position:absolute;

line-height:2.6;

padding-left:20px;

top:0;

}

.topbarbtn::before{

content:"";

width:20px;

height:2px;

background:#999;

display:inline-block;

box-shadow:0 7px 0 #999,0 -7px 0 #999;

}

.slidebar{

position:absolute;

top:0;

bottom:0;

width:0;

background:#666;

z-index:100;

}

.slidebar li{

height:40px;

padding-left:10px;

line-height:40px;

color:#fff;

border-bottom:1px solid #333;

}

html,body,ul,ol,li,dd,dt,h1,h2,h3,h4,h5,h6,p{

margin:0;

padding:0;

}

html,body{

height:100%;

-webkit-font-size-adjust:100%; /*横屏或竖屏时防止字体缩放失真*/

font-size-adjust:100%;

}

html{

font-size:62.5%;

}

*{

box-sizing:border-box;

}

h1{font-size:1.8rem;}h2{font-size:1.6rem;}h3{font-size:1.4rem;}

input,button{

-webkit-appearance:none;

-moz-appearance:none;

appearance:none;

}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

js和jquery实现的方式不一样,js中slide-bar本身是有宽度的,只不过开始向左移动了150px隐藏起来了,通过点击按钮,将body向右移动150px。

而jquery中,slide-bar本身没有宽度,也没有位移,但是里面的文字是需要显示出来的,所以会有超出的部分,通过jquery改变slide-bar的宽度来实现显示。

可以在看看这两部分代码。

好帮手慕星星 2019-01-21 18:46:23

你好,代码中缺少一个样式:

http://img1.sycdn.imooc.com//climg/5c45a2f5000118ef06840539.jpg

可以完善测试下,祝学习愉快!

  • 提问者 小章鱼丸 #1
    老师那之前js方法怎么不用设置超出隐藏它就能正常显示啊?
    2019-01-21 20:58:43
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
组件化思想开发电商网页 18版
  • 参与学习           人
  • 提交作业       467    份
  • 解答问题       4826    个

本路径带你通过系统学习HTML5、JavaScript、jQuery的进阶知识,不仅如此,还会学习如何利用组件化的思想来开发网页,知识点+案例,使得所学可以更好的得到实践。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师