實現下面這種效果
開發中要實現下面這種效果,相信大家第一眼就會想到用定位,直接把四個邊框定位上去就好了。
最開始我也是直接用的定位,封裝成了一個組件。後面佈局的時候出問題了,排列的時候很麻煩,對不起,調位置什麼的都不好控制,而且因爲要做到自適應,很不好做。後面想了很久,感覺可以用css的背景屬性來做,這樣的話這些問題全部都可以避免了。
直接上代碼
這個html文件,可以複製直接看下
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.frame {
display: inline-block;
padding: 7px;
background: linear-gradient(#00ffd4, #00ffd4) left top, linear-gradient(#00ffd4, #00ffd4) left top, linear-gradient(#00ffd4, #00ffd4) right top, linear-gradient(#00ffd4, #00ffd4) right top, linear-gradient(#00ffd4, #00ffd4) left bottom, linear-gradient(#00ffd4, #00ffd4) left bottom, linear-gradient(#00ffd4, #00ffd4) right bottom, linear-gradient(#00ffd4, #00ffd4) right bottom;
background-repeat: no-repeat;
background-size: 2px 20px, 20px 2px;
}
.frame-box {
width: 200px;
height: 200px;
border: #00ffd4 1px solid;
background: rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="frame">
<div class="frame-box">
</div>
</div>
<div class="frame">
<div class="frame-box">
</div>
</div>
</body>
</html>
當然,我項目是vue的,直接封裝成一個樣式組件,利用vue的<slot />
標籤,插槽。就可以直接想怎麼用就怎麼用啦,開心。
組件代碼
<template>
<div class="frame">
<div class="frame-box">
<slot />
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="scss" scoped>
.frame {
padding: 7px;
background: linear-gradient(#00ffd4, #00ffd4) left top,
linear-gradient(#00ffd4, #00ffd4) left top,
linear-gradient(#00ffd4, #00ffd4) right top,
linear-gradient(#00ffd4, #00ffd4) right top,
linear-gradient(#00ffd4, #00ffd4) left bottom,
linear-gradient(#00ffd4, #00ffd4) left bottom,
linear-gradient(#00ffd4, #00ffd4) right bottom,
linear-gradient(#00ffd4, #00ffd4) right bottom;
background-repeat: no-repeat;
background-size: 2px 20px, 20px 2px;
}
.frame-box {
width: 100%;
height: 100%;
border: #00ffd4 1px solid;
background: rgba(0, 0, 0, 0.6);
}
</style>
注意下css預處理器,別報錯了!!!希望能夠幫助到大家!
至於怎麼使用組件,額,我想應該不需要再說了。
個人水平有限,有問題歡迎大家留言指導,僅供學習和參考。
學海無涯!努力二字,共勉!