前端入坑紀(jì) 27
工作中的一個(gè)多選效果停局,感覺不算太難,就上傳來(lái)分享下广匙。
OK潮剪,first things first!項(xiàng)目鏈接
HTML 結(jié)構(gòu)
<div id="selexWps" class="seletProds clear">
<div>
<span>積分<em>5000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
<div>
<span>積分<em>5000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
<div>
<span>積分<em>6000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
<div>
<span>積分<em>7000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
<div>
<span>積分<em>8000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
<div>
<span>積分<em>9000</em></span>
![](wrap/img/pic.png)
<p>請(qǐng)選擇</p>
</div>
</div>
<div class="endBtnsWrp clear">
<a class="endBtn" href="javascript:;">積分:<em id="talval">0</em></a>
<a class="endBtn" href="javascript:;">結(jié)算</a>
</div>
這真不曉得什么要特意說(shuō)明分唾,真要有的話抗碰,那底部的結(jié)算條,它是fixed 布局绽乔,固定在屏幕底部弧蝇。
CSS 結(jié)構(gòu)
body {
padding: 0;
margin: 0;
background-color: #fff;
padding-bottom: 60px;
}
p {
padding: 0;
margin: 0;
}
ul,
li {
padding: 0;
margin: 0;
list-style: none;
}
a {
text-decoration: none;
color: #525252;
}
.fl {
float: left;
}
.fr {
float: right;
}
.clear::after {
display: block;
content: "";
clear: both;
}
.seletProds>div {
float: left;
position: relative;
text-align: center;
border: 2px solid #d8d8d8;
box-sizing: border-box;
width: 30%;
margin: 1.666%;
}
.seletProds p {
font-size: 14px;
line-height: 37px;
border-top: 2px solid #d8d8d8;
text-align: center;
background-color: #fff;
color: #525252;
}
.seletProds>div img {
width: 80%;
}
.seletProds>div span {
position: absolute;
top: 0;
right: 0;
background: rgba(255, 92, 92, 0.79);
padding: 0 3px;
color: #fff;
height: 17px;
font-size: 12px;
}
.seletProds>div span em,
.endBtnsWrp a em {
font-style: normal;
}
.seletProds>div.on p {
color: #fff;
background-image: linear-gradient(-90deg, #53a0fe 0%, #5063ed 100%);
}
.endBtnsWrp {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
}
.endBtn {
display: block;
float: left;
width: 50%;
line-height: 50px;
font-size: 17px;
color: #fff;
background: #4a4a4a;
text-align: center;
}
.endBtnsWrp a:first-child {
background: #646464;
width: 60%;
}
.endBtnsWrp a:last-child {
background-color: #fcc647;
width: 40%;
}
css3的屬性用了不少,因?yàn)檫@個(gè)項(xiàng)目的出發(fā)點(diǎn)就是手機(jī)端的折砸。比如漸變背景看疗,偽元素等。每個(gè)商品是通過浮動(dòng)布局來(lái)實(shí)現(xiàn)的睦授。
JS 結(jié)構(gòu)
var odivs = document.getElementById('selexWps').getElementsByTagName("div");
var emTal = 0; // 這是總的積分?jǐn)?shù)两芳,初始為0
for (var i = 0; i < odivs.length; i++) {
// 先給每個(gè)點(diǎn)擊的項(xiàng)目添加個(gè)自定義屬性,以便區(qū)別點(diǎn)擊前和點(diǎn)擊后去枷,剛開始是off
odivs[i].setAttribute("clickv", "off");
odivs[i].onclick = function() {
// 點(diǎn)擊時(shí)怖辆,先獲取里面的積分文字是复,轉(zhuǎn)成數(shù)字類型
var themVal = Number(this.getElementsByTagName("em")[0].innerText);
// 這里判斷未點(diǎn)擊的點(diǎn)擊后,要處理的操作
if (this.getAttribute("clickv") == "off") {
emTal += themVal;
this.className = "on";
this.getElementsByTagName("p")[0].innerText = "已選擇";
this.setAttribute("clickv", "on");
} else {
// 這里自然是已點(diǎn)擊過竖螃,再點(diǎn)擊的操作
emTal -= themVal;
this.className = "";
this.getElementsByTagName("p")[0].innerText = "請(qǐng)選擇";
this.setAttribute("clickv", "off");
}
// 這里自然是把點(diǎn)完后淑廊,變更的最終積分?jǐn)?shù)添加到頁(yè)面的結(jié)算里
document.getElementById("talval").innerText = emTal;
// console.log(emTal)
}
}
js 備注已添加,相對(duì)來(lái)說(shuō)還算詳細(xì)斑鼻,有興趣的小伙伴可以嘗試實(shí)驗(yàn)下。加油吧A攒<崛酢!
好了关摇,到此荒叶,本文告一段落!感謝您的閱讀输虱!祝你身體健康些楣,闔家幸福!