html实现平移隐藏,点击按钮平移隐藏与显示div(使用translate)

.content {

position: absolute;

background: gray;

width: 100%;

height: 100%;

left: 0;

top: 0;

}

.leftDiv {

float:left;

position:relative;

width: 150px;

height: 80%;

background: pink;

transform: translateX(0);

transition: transform 0.5s;

}

.changeClass {

transform: translateX(-100%);

}

.changeBtn{position:absolute;top:50%;right:-45px;transform:translate(0,-50%);}

left conternt

切换

$(".changeBtn").click(function() {

if (!$(".leftDiv").hasClass("changeClass")) {

$(".leftDiv").addClass("changeClass")

} else {

$(".leftDiv").removeClass("changeClass")

}

})

如果右边内容跟着移动,完整代码如下:

改变dropdow的click 事件

ul {

list-style: none;

}

ul,

li {

margin: 0;

padding: 0;

}

.content {

position: absolute;

/*background: gray;*/

width: 100%;

height: 100%;

left: 0;

top: 0;

}

.leftDiv {

float: left;

position: absolute;

/*position:relative;*/

width: 150px;

height: 20%;

background: pink;

/* top:0;

left:0;*/

transition: transform 0.5s;

}

.toggle .leftDiv {

transform: translate(-150px, 0);

}

.toggle .right-content {

margin-left: 30px;

}

.changeBtn {

position: absolute;

top: 50%;

/*right:-45px;*/

left: 100%;

transform: translate(0, -50%);

width: 25px;

}

.left-content {

overflow: hidden;

white-space: nowrap;

}

.left-content ul li {

height: 35px;

line-height: 35px;

}

.right-content {

margin-left: 180px;

transition: margin-left 0.5s;

}

  • this is the first line
  • this is the second line

切换

  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line
  • this is the first line
  • this is the second line

$(".changeBtn").click(function() {

if (!$(".content").hasClass("toggle")) {

$(".content").addClass("toggle")

} else {

$(".content").removeClass("toggle")

}

})

注意:

css3 transform变换后,原来的位置还占据空间,那是因为 transform并没有让元素脱离标准流;

解决方法:

可以考虑在写了transform属性后,结合position:absolute脱离标准流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值