为了账号安全,请及时绑定邮箱和手机立即绑定

继续研究vue组件vue-menu组件

标签:
Html5

其实这个很简单,出发点就是想做一个点击切换的功能
在这里插入图片描述
原生html+css+js代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    .nav-mobile-button {
        position: relative;
        float: left;
        padding: 9px 10px;
        margin-top: 8px;
        margin-right: 15px;
        margin-bottom: 8px;
        background-color: transparent;
        background-image: none;
        border: 1px solid transparent;
        outline: none;
        border-radius: 4px;
    }
    .nav-mobile-button .same:nth-child(1){
        margin-top: 0; 
    }
    .nav-mobile-button .same {
        display: block;
        margin-top: 4px;
        width: 22px;
        height: 2px;
        background: rgb(63, 81, 181);
        border-radius: 1px; 
    }
    .nav-mobile-button .icon-bar1 {
        transform: translate(0,0) rotate(-45deg);
    }
    .nav-mobile-button .icon-bar2 {
        display: none;
    }
    .nav-mobile-button .icon-bar3 {
        transform: translate(0,-250%) rotate(45deg);
    }
    .box{
        width: 400px;
        height: 500px;
        background-color: #eee;
        transform: translate(-400px,0);
    }
    </style>
</head>
<body>
<button class="nav-mobile-button">
    <span id="span1" class="same"></span>
    <span id="span2" class="same"></span>
    <span id="span3" class="same"></span>
</button>
<div class="box">
    
</div>
</body>
<script>
var oSpan1 = document.getElementById('span1'),
    oSpan2 = document.getElementById('span2'),
    oSpan3 = document.getElementById('span3');
var oBtn = document.querySelector(".nav-mobile-button"),
    oBox = document.querySelector(".box");
oBtn.onclick = function() {
    oSpan3.style.cssText = "transform: translate(0,-250%) rotate(45deg);transition: transform 1s;";
    oSpan1.style.cssText = "transform: translate(0,0) rotate(-45deg);transition: transform 1s;margin-top:4px;";
    oSpan2.style.display = "none";
    oBox.style.cssText = "transform: translate(0,0);transition: transform 1s;"
    oBtn.style.cssText = "z-index:1000;right:-48%;transition: right 1s;";
} 
</script>
</html>

在这里插入图片描述
点击三杠按钮之后切换成这种效果
在这里插入图片描述
将其改成了vue组件
menu.vue

<template>
<div>
	<button class="nav-mobile-button" @click="change" ref="btn">
	    <span id="span1" class="same"></span>
	    <span id="span2" class="same"></span>
	    <span id="span3" class="same"></span>
	</button>
	<div class="box" ref="box">
	    
	</div>
</div>
</template>

<script>
export default {
	name: 'menu',
	data() {
		return {

		}
	},
	methods: {
		change() {
			this.$emit("change")
		}
	}
}
</script>
<style scoped lang="scss">
*{
    margin: 0;
    padding: 0;
}
.nav-mobile-button {
    position: relative;
    float: left;
    padding: 9px 10px;
    margin-top: 8px;
    margin-right: 15px;
    margin-bottom: 8px;
    background-color: transparent;
    background-image: none;
    border: 1px solid transparent;
    outline: none;
    border-radius: 4px;
}
.nav-mobile-button .same:nth-child(1){
    margin-top: 0; 
}
.nav-mobile-button .same {
    display: block;
    margin-top: 4px;
    width: 22px;
    height: 2px;
    background: rgb(63, 81, 181);
    border-radius: 1px; 
}
.box{
    width: 400px;
    height: 500px;
    background-color: #eee;
    transform: translate(-400px,0);
}
</style>

引入应用如下
Menu.vue

<template>
	<v-menu ref="menu" @change="change"></v-menu>
</template>
<script>
import menu from "@/menu/menu"
export default {
	name: 'menu',
	components: {
		"v-menu": menu,
	},
	data() {
		return {
			b: false,
			vChilds: null,
			vBtn: null,
			vBox: null
		}
	},
	mounted() {
		this.vChilds = this.$refs.menu.$refs.btn.childNodes;
		this.vBtn = this.$refs.menu.$refs.btn;
		this.vBox = this.$refs.menu.$refs.box;
		this.vBtn.style.cssText = "z-index:1000;right:0;transition: right 1s;";
	},
	methods: {
		change() {
			if(!this.b){
				this.vChilds[0].style.cssText = "transform: translate(0,0) rotate(-45deg);transition: transform 1s;margin-top:4px;";
				this.vChilds[1].style.display = "none";
				this.vChilds[2].style.cssText = "transform: translate(0,-250%) rotate(45deg);transition: transform 1s;";
				this.vBtn.style.cssText = "z-index:1000;right:-26%;transition: right 1s;";
				this.vBox.style.cssText = "transform: translate(0,0);transition: transform 1s;";
				
			}else{
				this.vChilds[0].style.cssText = "transform: translate(0,0) rotate(0);transition: transform 1s;margin-top:4px;";
				this.vChilds[1].style.display = "block";
				this.vChilds[2].style.cssText = "transform: translate(0,0) rotate(0);transition: transform 1s;";
				this.vBtn.style.cssText = "z-index:1000;right:0;transition: right 1s;";
				this.vBox.style.cssText = "transform: translate(-400px,0);transition: transform 1s;";
			}
			this.b = !this.b;
		}
	}
}
</script>

至此大功告成

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
23
获赞与收藏
130

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

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

帮助反馈 APP下载

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

公众号

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

举报

0/150
提交
取消