HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<body>
<section class="addTicket-panel">
<div class="addTicket-img">
<img src="https://i.imgur.com/iRssPpJ.png" alt="">
<img src="https://i.imgur.com/vJt0znH.png" alt="">
</div>
<form action="" class="addTicket-form">
<h1 class="title"><span><i class="fas fa-plus-circle"></i></span>新增旅遊套票</h1>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketName">套票名稱</label>
<input type="text" id="ticketName" placeholder="請填寫套票名稱" name="套票名稱">
</div>
<div class="alert-message">
<p id="ticketName-message" data-message="套票名稱">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketImgUrl">圖片網址</label>
<input type="text" id="ticketImgUrl" placeholder="請填寫圖片網址" name="圖片網址">
</div>
<div class="alert-message">
<p id="ticketImgUrl-message" data-message="圖片網址">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketRegion">景點地區</label>
<select id="ticketRegion" name="景點地區">
<option value="" disabled selected hidden>請選擇景點地區</option>
<option value="台北">台北</option>
<option value="台中">台中</option>
<option value="高雄">高雄</option>
</select>
</div>
<div class="alert-message">
<p id="ticketRegion-message" data-message="景點地區">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketPrice">套票金額</label>
<input type="number" id="ticketPrice" placeholder="請填寫套票金額" name="套票金額" min="0">
</div>
<div class="alert-message">
<p id="ticketPrice-message" data-message="套票金額">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketNum">套票組數</label>
<input type="number" id="ticketNum" placeholder="請填寫套票組數" name="套票組數" min="1">
</div>
<div class="alert-message">
<p id="ticketNum-message" data-message="套票組數">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketRate">套票星級</label>
<input type="number" id="ticketRate" placeholder="請填寫套票星級" name="套票星級" min="1" max="10">
</div>
<div class="alert-message">
<p id="ticketRate-message" data-message="套票星級">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<div class="form-group">
<div class="addTicket-input">
<label for="ticketDescription">套票描述</label>
<textarea name="套票描述" id="ticketDescription" placeholder="請填寫套票描述 (限 100 字)"></textarea>
</div>
<div class="alert-message">
<p id="ticketDescription-message" data-message="套票描述">
<!-- <i class="fas fa-exclamation-circle"></i>
<span>必填!</span> -->
</p>
</div>
</div>
<input type="button" class="addTicket-btn btn" value="新增套票">
</form>
</section>
<section class="main-content">
<!-- 搜尋區 -->
<div class="search-area">
<select name="" class="regionSearch">
<option value="地區搜尋" disabled selected hidden>地區搜尋</option>
<option value="">全部地區</option>
<option value="台北">台北</option>
<option value="台中">台中</option>
<option value="高雄">高雄</option>
</select>
<p id="searchResult-text">本次搜尋共 3 筆資料</p>
</div>
<!-- 套票卡片區 -->
<ul class="ticketCard-area">
<!-- <li class="ticketCard">
<div class="ticketCard-img">
<a href="#">
<img src="https://i.imgur.com/QXa1fMZ.png" alt="">
</a>
<div class="ticketCard-region">高雄</div>
<div class="ticketCard-rank">10</div>
</div>
<div class="ticketCard-content">
<div>
<h3>
<a href="#" class="ticketCard-name">綠島自由行套裝行程</a>
</h3>
<p class="ticketCard-description">
嚴選超高CP值綠島自由行套裝行程,多種綠島套裝組合。
</p>
</div>
<div class="ticketCard-info">
<p class="ticketCard-num">
<span><i class="fas fa-exclamation-circle"></i></span>
剩下最後 <span id="ticketCard-num"> 87 </span> 組
</p>
<p class="ticketCard-price">
TWD <span id="ticketCard-price">$1400</span>
</p>
</div>
</div>
</li>
<li class="ticketCard">
<div class="ticketCard-img">
<a href="#">
<img src="https://i.imgur.com/4UHm8WX.png" alt="">
</a>
<div class="ticketCard-region">台北</div>
<div class="ticketCard-rank">2</div>
</div>
<div class="ticketCard-content">
<div>
<h3>
<a href="#" class="ticketCard-name">清境高空觀景步道</a>
</h3>
<p class="ticketCard-description">
清境農場青青草原數十公頃碧草,這些景觀豐沛了清境觀景步道的風格,也涵養它無可取代的特色。
</p>
</div>
<div class="ticketCard-info">
<div class="ticketCard-num">
<p>
<span><i class="fas fa-exclamation-circle"></i></span>
剩下最後 <span id="ticketCard-num"> 99 </span> 組
</p>
</div>
<p class="ticketCard-price">
TWD <span id="ticketCard-price">$240</span>
</p>
</div>
</div>
</li>
<li class="ticketCard">
<div class="ticketCard-img">
<a href="#">
<img src="https://i.imgur.com/H97Wgfn.png" alt="">
</a>
<div class="ticketCard-region">台中</div>
<div class="ticketCard-rank">7</div>
</div>
<div class="ticketCard-content">
<div>
<h3>
<a href="#" class="ticketCard-name">山林悠遊套票</a>
</h3>
<p class="ticketCard-description">
山林悠遊套票,結合南投清境高空步道、雙龍瀑布七彩吊橋、瑞龍瀑布園區之熱門景點。
</p>
</div>
<div class="ticketCard-info">
<div class="ticketCard-num">
<p>
<span><i class="fas fa-exclamation-circle"></i></span>
剩下最後 <span id="ticketCard-num"> 20 </span> 組
</p>
</div>
<p class="ticketCard-price">
TWD <span id="ticketCard-price">$1765</span>
</p>
</div>
</div>
</li> -->
</ul>
<!-- 查無關鍵字區 -->
<div class="cantFind-area">
<h3>查無此關鍵字資料</h3>
<img src="https://i.imgur.com/dZI8FJQ.png" alt="">
</div>
</section>
</body>
*, *::after, *::before {
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-transition: all .2s;
transition: all .2s;
}
html, body {
line-height: 1.5;
}
img {
max-width: 100%;
display: block;
}
body {
font-family: "Microsoft JhengHei";
}
.addTicket-panel {
-webkit-box-shadow: 0px 3px 6px #00000029;
box-shadow: 0px 3px 6px #00000029;
border-radius: 12px;
}
.addTicket-panel {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
max-width: 920px;
margin: 120px auto 100px;
padding: 60px 0;
}
@media (max-width: 768px) {
.addTicket-panel {
margin: 0 auto;
padding-top: 0;
-webkit-box-shadow: none;
box-shadow: none;
}
}
.addTicket-img, .addTicket-form {
width: 38%;
}
@media (max-width: 768px) {
.addTicket-img, .addTicket-form {
width: 90%;
}
}
.addTicket-img img {
margin: 0 auto 21px;
}
@media (max-width: 768px) {
.addTicket-img img:nth-child(2) {
display: none;
}
}
.title {
color: #00807E;
border-bottom: 3px solid #00807E;
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 36px;
}
.title span {
margin-right: 6px;
}
.addTicket-form {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
.addTicket-form .form-group {
margin-bottom: 16px;
}
.addTicket-form label {
color: #00807E;
font-weight: bold;
margin-bottom: 5px;
}
.addTicket-input {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
@media (max-width: 767px) {
.addTicket-input {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: start;
-ms-flex-align: start;
align-items: start;
}
}
.addTicket-input input, .addTicket-input textarea, .addTicket-input select {
width: 72%;
text-indent: 0.5rem;
background-color: #F7F7F7;
color: #818A91;
font-size: 1rem;
border: none;
border-bottom: 1px solid #00807E;
min-height: 38px;
}
@media (max-width: 767px) {
.addTicket-input input, .addTicket-input textarea, .addTicket-input select {
width: 100%;
}
}
.addTicket-input textarea::-webkit-input-placeholder {
font-size: 1rem;
}
.addTicket-input textarea:-ms-input-placeholder {
font-size: 1rem;
}
.addTicket-input textarea::-ms-input-placeholder {
font-size: 1rem;
}
.addTicket-input textarea::placeholder {
font-size: 1rem;
}
.addTicket-input textarea {
padding-top: 8px;
min-height: 111px;
}
.alert-message {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
margin-top: 3px;
color: #B21C01;
font-size: 0.85rem;
}
.alert-message p {
width: 72%;
}
@media (max-width: 767px) {
.alert-message p {
width: 100%;
}
}
.alert-message span {
margin-left: 5px;
font-weight: bold;
}
.addTicket-btn {
-ms-flex-item-align: end;
align-self: flex-end;
font-size: 1.25rem;
padding: 10px 40px;
}
@media (max-width: 767px) {
.addTicket-btn {
-ms-flex-item-align: center;
-ms-grid-row-align: center;
align-self: center;
}
}
.btn {
border: none;
background-color: #00807E;
color: #FFF;
border-radius: 5px;
cursor: pointer;
text-align: center;
}
.btn:hover {
background-color: #64C3BF;
}
.main-content {
background-color: #F7F7F7;
padding-bottom: 100px;
}
@media (max-width: 767px) {
.main-content {
padding-bottom: 30px;
}
}
.search-area {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
max-width: 920px;
margin: 0 auto;
padding: 50px 0;
}
.search-area p {
color: #818A91;
}
@media (max-width: 768px) {
.search-area {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
}
.regionSearch {
width: 27.5%;
min-height: 38px;
text-indent: 0.5rem;
color: #818A91;
font-size: 1rem;
border: 1px solid #CED4DA;
border-radius: 5px;
margin-right: 2.5%;
}
@media (max-width: 768px) {
.regionSearch {
margin-right: 0;
margin-bottom: 10px;
width: 80%;
}
}
.ticketCard-area {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
max-width: 1100px;
margin: 0 auto;
}
.ticketCard {
width: 31.3333%;
background-color: #fff;
-webkit-box-shadow: 0px 3px 6px #00000029;
box-shadow: 0px 3px 6px #00000029;
border: 1px solid #DEE2E6;
border-radius: 5px;
margin-bottom: 38px;
margin-right: 2%;
}
@media (max-width: 768px) {
.ticketCard {
width: 46%;
margin: 0 2% 40px;
}
}
@media (max-width: 767px) {
.ticketCard {
width: 90%;
margin: 0 5% 30px;
}
}
.ticketCard-img {
position: relative;
}
.ticketCard-img a {
display: block;
overflow: hidden;
}
.ticketCard-img img {
width: 100%;
height: 180px;
-o-object-fit: cover;
object-fit: cover;
}
.ticketCard-img img:hover {
-webkit-transform: scale(1.15);
transform: scale(1.15);
}
.ticketCard-region, .ticketCard-rank {
position: absolute;
border-radius: 0 5px 5px 0px;
color: #FFF;
text-align: center;
}
.ticketCard-region {
top: -13px;
left: 0;
font-size: 1.25rem;
background-color: #64C3BF;
padding: 12px 20px;
}
.ticketCard-rank {
bottom: -16px;
left: 0;
background-color: #00807E;
padding: 5px 8px;
min-width: 40px;
}
.ticketCard-content {
padding: 20px;
height: calc(100% - 180px);
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.ticketCard-name {
display: block;
text-decoration: none;
color: #00807E;
font-size: 1.5rem;
font-weight: bold;
padding-bottom: 5px;
border-bottom: 2px solid #00807E;
margin-bottom: 20px;
}
.ticketCard-name:hover {
color: #64C3BF;
}
.ticketCard-description {
color: #818A91;
margin-bottom: 32px;
}
.ticketCard-info {
color: #00807E;
font-family: 'Roboto', sans-serif;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.ticketCard-num {
font-weight: bold;
}
.ticketCard-price {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.ticketCard-price span {
font-size: 2rem;
}
.cantFind-area {
display: none;
margin-top: 70px;
text-align: center;
}
.cantFind-area h3 {
color: #00807E;
font-size: 2rem;
margin-bottom: 50px;
font-weight: bold;
}
.cantFind-area img {
margin: 0 auto;
}
@media (max-width: 768px) {
.cantFind-area {
margin-top: 0px;
}
}
// let data = [
// {
// "id": 0,
// "name": "肥宅心碎賞櫻3日",
// "imgUrl": "https://images.unsplash.com/photo-1522383225653-ed111181a951?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1655&q=80",
// "area": "高雄",
// "description": "賞櫻花最佳去處。肥宅不得不去的超讚景點!",
// "group": 87,
// "price": 1400,
// "rate": 10
// },
// {
// "id": 1,
// "name": "貓空纜車雙程票",
// "imgUrl": "https://images.unsplash.com/photo-1501393152198-34b240415948?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80",
// "area": "台北",
// "description": "乘坐以透明強化玻璃為地板的「貓纜之眼」水晶車廂,享受騰雲駕霧遨遊天際之感",
// "group": 99,
// "price": 240,
// "rate": 2
// },
// {
// "id": 2,
// "name": "台中谷關溫泉會1日",
// "imgUrl": "https://images.unsplash.com/photo-1535530992830-e25d07cfa780?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80",
// "area": "台中",
// "description": "全館客房均提供谷關無色無味之優質碳酸原湯,並取用八仙山之山冷泉供蒞臨貴賓沐浴及飲水使用。",
// "group": 20,
// "price": 1765,
// "rate": 7
// }
// ];
let data = []; //先設定一個變數賦予空陣列的值
//使用get取得資料
axios.get('https://raw.githubusercontent.com/hexschool/js-training/main/travelApi.json').then(function (response) {
// console.log(response); //小步測試確認是否資料有正確抓到
data = response.data.data; //注意資料的層級關係
init(); //呼叫資料(初始值畫面)
})
//選取變數並進行監聽相關節點資料
const list = document.querySelector(".ticketCard-area");
let searchData = [];
const search = document.querySelector(".search-area");
const ticketName = document.querySelector("#ticketName");
const ticketImgUrl = document.querySelector("#ticketImgUrl");
const ticketRegion = document.querySelector("#ticketRegion");
const ticketPrice = document.querySelector("#ticketPrice");
const ticketNum = document.querySelector("#ticketNum");
const ticketRate = document.querySelector("#ticketRate");
const ticketDescription = document.querySelector("#ticketDescription");
const addTicket = document.querySelector(".addTicket-btn");
//初始資料
function init(){
let str = "";
data.forEach(function(item){
let content = `<li class="ticketCard">
<div class="ticketCard-img">
<a href="#">
<img src="${item.imgUrl}" alt="">
</a>
<div class="ticketCard-region">${item.area}</div>
<div class="ticketCard-rank">${item.id}</div>
</div>
<div class="ticketCard-content">
<div>
<h3>
<a href="#" class="ticketCard-name">${item.name}</a>
</h3>
<p class="ticketCard-description">
${item.description}
</p>
</div>
<div class="ticketCard-info">
<p class="ticketCard-num">
<span><i class="fas fa-exclamation-circle"></i></span>
剩下最後 <span id="ticketCard-num"> ${item.group} </span> 組
</p>
<p class="ticketCard-price">
TWD <span id="ticketCard-price">${item.price}</span>
</p>
</div>
</div>
</li>`;
str+=content;
searchData.push(item);
});
list.innerHTML=str;
searchResult();
}
init();
//篩選資料
search.addEventListener("change",function(e){
if(e.target.value==undefined){
return;
}
let str = ""; //物件取值方式設定一個空字串
data.forEach(function(item){
if(e.target.value==item.area){
let content = `<li class="ticketCard">
<div class="ticketCard-img">
<a href="#">
<img src="${item.imgUrl}" alt="">
</a>
<div class="ticketCard-region">${item.area}</div>
<div class="ticketCard-rank">${item.id}</div>
</div>
<div class="ticketCard-content">
<div>
<h3>
<a href="#" class="ticketCard-name">${item.name}</a>
</h3>
<p class="ticketCard-description">
${item.description}
</p>
</div>
<div class="ticketCard-info">
<p class="ticketCard-num">
<span><i class="fas fa-exclamation-circle"></i></span>
剩下最後 <span id="ticketCard-num"> ${item.group} </span> 組
</p>
<p class="ticketCard-price">
TWD <span id="ticketCard-price">${item.price}</span>
</p>
</div>
</div>
</li>`;
str+=content;
list.innerHTML = str;
searchData.push(item);
searchResult();
}else if(e.target.value=="all"){
init();
}
})
})
//新增資料
//監聽觸發事件 //點擊
addTicket.addEventListener("click",function(e){
let obj = {}; //設定空物件 後續使用push上傳資料
obj.id = ""; //選取指定元素 準備push資料
obj.name = ticketName.value;
obj.imgUrl = ticketImgUrl.value;
obj.area = ticketRegion.value;
obj.description = ticketDescription.value;
obj.group = parseInt(ticketNum.value);
obj.price = parseInt(ticketPrice.value);
obj.rate = parseInt(ticketRate.value);
data.push(obj);
searchData.push(obj);
searchResult();
init();
})
//資料搜尋筆數
function searchResult(){
const searchResultNum = document.querySelector(".regionSearch")
let searchResult = document.querySelector("#searchResult-text");
console.log(searchResultNum.value);
data.area =
searchResult.innerHTML= `<span>本次搜尋共 ${searchData.length} 筆資料</span>`;
searchData = [];
}
Also see: Tab Triggers