用flex布局实现软键盘

软键盘用来模拟真实键盘,我们知道键盘中一些特殊符号键的大小和普通字符键的大小是不一样的,例如空格键。但一般各个键间距是一样的,这样的一种布局适合用flex布局来实现。

我们实现一个简单的例子:

我们实现上述图片布局。代码如下:

<html>
 
<head>
<style type="text/css">

#app{
    width: 800px;
    height: 300px;
    outline: red dotted 3px;   
    
}
#line_1{
    width: 800px;
    height: 100px;
    display: flex; /*flex 布局*/
}
#line_2{
    width: 800px;
    height: 100px;
    margin-top: 10px;
    display: flex;
}

.line1Style{
    height: 100px;
    background-color: grey;
    color: greenyellow;
    text-align: center; /*字符水平居中*/
    line-height: 100px; /*字符垂直居中*/
    margin-right: 5px;  /*各个item直接间距*/
    flex-shrink: 0;  /*不进行缩放*/
}
.line2Style{
    height: 100px;
    background-color: grey;
    color: greenyellow;
    text-align: center;
    line-height: 100px;
    margin-right: 5px;
    flex-shrink: 0;
}

</style>
</head>
 
<body>    

    <script src="./vue.js"></script>

    <div id="app">
        <div id="line_1">
            <div :id="'line_1_' + i" class="line1Style" :style="line1StyleD()" v-for="(item, i) in listData1">{{item}}</div>
        </div>
        <div id="line_2">
            <div :id="'line_2_' + i" class="line2Style" :style="line2StyleD(i)" v-for="(item, i) in listData2">{{item}}</div>
        </div>
    </div>

<script>
    var app = new Vue({
        data : function(){
            return {
                listData1 : ["Q", "W", "E", "R", "T", "Y", "U"],
                listData2 : ["B", "Z", "X", "Space", "N", "M"]
            }
        },
        methods : {
            line1StyleD : function(){
                return{
                    width : "100px" //所有item 宽度为100px 
                }
            },  
            line2StyleD : function(i){
                if(i === 3){
                    return{
                        width : "205px"  //Space 这个item宽度为其他item的1倍
                    }
                }else{
                    return{
                        width : "100px"
                    }
                }
            }
        }
    }).$mount("#app");
</script>
</body>
</html>
发布了180 篇原创文章 · 获赞 16 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/liubangbo/article/details/104092050
今日推荐