前端使用者體驗優化: JS & CSS 各類效果程式碼段
阿新 • • 發佈:2018-11-03
前言
- 不定時更新
- 線上預覽 https://zzyper.github.io/opti...
- 線上預覽的原始碼 https://github.com/zzyper/opt...
- 部分內容僅相容
webkit
核心,其他核心自行查詢
可控密度的虛線分隔線 css
.line { height: 1px; width: 100%; transform: scaleY(0.4); -webkit-transform: scaleY(0.4); background-image: linear-gradient(to right, #ccc 0%, #ccc 50%, transparent 50%); background-size: 4px 1px; background-repeat: repeat-x; }
<div class='line'></div>
- 通過更改
background-size
值控制密度展示 - 通過各屬性x,y及方向轉換實現橫/縱向不同的分隔線
文字漸隱 css
.article{ position: relative; } .mask{ position: absolute; width:100%; bottom:0; left: 0; height: 60px; background: linear-gradient(top, rgba(255,255,255,0), #fff); background: -webkit-linear-gradient(top, rgba(255,255,255,0), #fff); }
<div class="article"> 春眠不覺曉<br /> 處處聞啼鳥<br /> 夜來風雨聲<br /> 花落知多少 <div class="mask"></div> </div>
- 引導使用者下方仍有內容,即將接近底部時將
div.mask
隱藏
彈窗禁止/恢復背景層滾動 css+js
window.onload = function(){ document.getElementById('modalBtn').onclick = function (){switchModalStatus(true)}; document.getElementById('modal').onclick = function (){switchModalStatus(false)}; } function switchModalStatus(needShow){ var modal = document.getElementById('modal'); if(needShow){ modal.style.display = 'block'; disableBodyScroll(); }else{ modal.style.display = 'none'; enableBodyScroll(); } } function disableBodyScroll() { var body = document.body; window.stTemp = Math.max(body.scrollTop, document.documentElement.scrollTop); body.style.overflow = "hidden"; body.style.position = "fixed"; body.style.top = (-window.stTemp+'px'); } function enableBodyScroll() { var body = document.body; body.style.overflow = "scroll"; body.style.position = "static"; body.style.top = '0px'; body.scrollTop = window.stTemp; document.documentElement.scrollTop = window.stTemp; }
.modal{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .5);
z-index: 1;
}
.modal > .content{
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
padding: 10px;
width: 50%;
background: white;
border-radius: 12px;
}
<button id="modalBtn">點我彈窗</button> <div id="modal" class="modal" style="display:none"> <div class="content">我是彈窗</div> </div>
多行文字溢位顯示省略號 css
.text {
text-align: left;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
```<p class="text">我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容我有很多內容</p>
```
-
-webkit-line-clamp
控制顯示的行數