Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

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.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Alpha Dating</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
    integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Alegreya|Lobster|Roboto" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"
    integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">

</head>

<body>
  <!-- navigation -->
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="index.html">Alpha Dating</a>


    <form class="form-inline my-2 my-lg-0 ml-2 mr-auto float-left" id="search">
      <input class="form-control mr-sm-2" id="search-input" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      <span class="searchby ml-2"></span>
    </form>
    <ul class="navbar-nav float-right">
      <!-- <li class="nav-item male">
        <a href="#" class="nav-link">
          <i class="fas fa-mars" aria-hidden="true" data-id="male" id="male"></i>
        </a></li>
      <li class="nav-item female"><a href="#" class="nav-link">
          <i class="fas fa-venus" aria-hidden="true" data-id="female" id="female"></i>
        </a></li> -->
      <li class="nav-item"><a href="favorite.html" class="nav-link">
          <i class="far fa-heart favorite" data-id="favorite"></i>
        </a></li>
    </ul>

  </nav>

  <!-- data-panel -->
  <div class="container mt-5">
    <div class="row" id="data-panel">
      <!-- print user list here -->
    </div>
  </div>

  <!-- Modal -->
  <div class="modal fade" id="show-user-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle"
    aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
      <div class="modal-content">

        <div class="modal-body" id="show-user-body">
          <div class="row mx-2">
            <div class="img-thumbnail col-sm-5" id="show-user-image">
            </div>
            <div class="col-sm-7">
              <div class="modal-header pl-0">
                <h3 class="mt-3 pt-1" id="show-user-name">Modal title</h3>
                <div id="show-fav-icon"></div>
              </div>
              <div class="row mx-3 my-5 ">
                <div id="show-user-description">
                  <!-- show user description here -->
                </div>
              </div>
            </div>
          </div>
          <div class="clearfix"></div>

          <div class="row mx-5">
            <div class="social-media">
              <ul class="float-left list-unstyled nav justify-content-center ">
                <li><i class="fab fa-facebook"></i></li>
                <li><i class="fab fa-twitter-square"></i></li>
                <li><i class="fab fa-instagram"></i></li>
              </ul>
            </div>
          </div>


        </div>
      </div>
    </div>
  </div>

  <nav aria-label="Page navigation" class="mt-5">
    <ul class="pagination justify-content-center" id="pagination">
      <li class="page-item">
        <a class="page-link" href="#">1</a>
      </li>
    </ul>
  </nav>


  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
    integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"
    integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ"
    crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"
    integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm"
    crossorigin="anonymous"></script>
  <script src="index.js"></script>
</body>

</html>
              
            
!

CSS

              
                body {
  font-family: 'Roboto', sans-serif;
}

/* nav */
.navbar-brand {
  font-family: 'Lobster', cursive;
  font-size: 2.5rem !important;
}

/* .male, .female {
  display: inline-block;
  padding: 10px;
  font-size: 1.6rem;
} */

/* .fa-mars, .fa-venus {
  padding: 10px;
  border-radius: 5px;
  cursor: pointer;
} */

.favorite {
  display: inline-block;
  padding: 20px;
  font-size: 30px;
}

/* .hover-male {
  background-color: rgb(27, 102, 187);
  color: white
} */

/* .hover-female {
  color: #fff;
  background-color: rgb(243, 59, 59);
} */

.card {
  border-radius: 50% !important;
  margin-top: 30px;
  box-shadow: 0 0 3px 1px rgba(160, 158, 158, 0.5);
  transition: 0.3s;
  cursor: pointer
}

.card-img-top {
  border-radius: 50% !important;
}

.card:hover {
  margin-top: 15px;
  box-shadow: 0 3px 15px 3px rgba(100, 100, 100, 0.5);
}


#user-image {
  width: 450px;
}

.card-header {
  min-height: 50px;
}

.searchby {
  font-size:14px;
  font-weight: lighter;
  color:gray;
}

/* modal */

.modal-content .col-sm-5 {
  padding: 2px;
  width: 325px;
  height: 325px;
}

#show-user-image img {
  border-radius: 5%;
  width: 100%;
  height: 100%;
}

.fa-heart {
  color: #dc3545c4;
}

.social-media {
  padding-left: 35px;
}

.social-media ul li {
  padding: 0 15px;
  font-size: 30px;
  color: black;
}
              
            
!

JS

              
                (function () {
  const BASE_URL = 'https://lighthouse-user-api.herokuapp.com/'
  const INDEX_URL = BASE_URL + 'api/v1/users/'
  const data = []
  const dataPanel = document.getElementById('data-panel')
  const searchForm = document.getElementById('search')
  const searchBy = document.querySelector('.searchby')
  const favIcon = document.getElementById('show-fav-icon')
  const pagination = document.getElementById('pagination')
  const ITEM_PER_PAGE = 12
  let filter = []
  let paginationData = []
  let pageLink = 1

  axios.get(INDEX_URL).then((response) => {
    data.push(...response.data.results)
    getTotalPages(data)
    getPageData(1, data)
  })

  function filterData(factor) {
    const regex = new RegExp(factor, "i");
    filter = data.filter(function (item) {
      return (
        item.name.match(regex) ||
        item.age === parseInt(factor) ||
        item.gender.match(regex) ||
        item.surname.match(regex) ||
        item.region.match(regex) ||
        item.birthday.match(regex)
      );
    });
    filter.forEach(item => {
      ["name", "age", "surname"].forEach(key => {
        if (String(item[key]).match(regex)) {
          console.log(`id:${item.id} 資料的${key}包含搜尋關鍵字`);
          searchBy.innerText = "You are searching by " + factor
        }
      });
    });
  }

  function showUser(id) {
    const userName = document.getElementById('show-user-name')
    const userDiscription = document.getElementById('show-user-description')
    const userImage = document.getElementById('show-user-image')
    const favoriteIcon = document.getElementById('show-fav-icon')
    const url = INDEX_URL + id

    axios.get(url).then(response => {
      const data = response.data

      userName.textContent = data.name + " " + data.surname
      // favoriteIcon.innerHTML = `<i class="far fa-heart favorite" data-id="${id}"></i>`
      userImage.innerHTML = `<img src="${data.avatar}" class="img-fluid" id = "user-image" alt="Responsive image">`
      userDiscription.innerHTML = `
      <p>Region : ${data.region}</p>
      <p>Gender : ${data.gender}</p>
      <p>Age : ${data.age}</p>
      <p>Birthday : ${data.birthday}</p>
      <p>Email : ${data.email}</p>
      `

      // 依據favoritePeople去判斷愛心符號實心或空心
      const list = JSON.parse(localStorage.getItem('favoritePeople')) || []

      if (list.some(item => item.id === Number(id))) {
        favoriteIcon.innerHTML = `<i class="fas fa-heart favorite" data-id="${id}"></i>`
      } else {
        favoriteIcon.innerHTML = `<i class="far fa-heart favorite" data-id="${id}"></i>`
      }
    })
  }

  function renderUserList(data) {

    dataPanel.innerHTML = data.map(item =>
      `
        <div class="col-sm-4">
          <div class="card" data-toggle="modal" data-target="#show-user-modal" data-id = "${item.id}">
            <img class="card-img-top" src="${item.avatar}" alt="Card image cap">

          </div>
          <h5 class="my-2 ml-auto mr-auto text-center">${item.name + " " + item.surname}</h5>
        </div>
      `).join('')
  }

  function addFavoriteItem(id) {
    const list = JSON.parse(localStorage.getItem('favoritePeople')) || []
    const favoritePeople = data.find(item => item.id === Number(id))

    if (list.some(item => item.id === Number(id))) {
      // alert(`already liked ${item.name}`)
      console.log("already liked")
    } else {
      list.push(favoritePeople)
    }
    localStorage.setItem('favoritePeople', JSON.stringify(list))
  }

  function getTotalPages(data) {
    let totalPages = Math.ceil(data.length / ITEM_PER_PAGE) || 1
    let pageItemContent = ''
    for (let i = 0; i < totalPages; i++) {
      pageItemContent += `
        <li class="page-item">
          <a class="page-link" href="javascript:;" data-page="${i + 1}">${i + 1}</a>
        </li>
      `
    }
    pagination.innerHTML = pageItemContent
  }

  function getPageData(pageNum, data) {
    paginationData = data || paginationData
    let offset = (pageNum - 1) * ITEM_PER_PAGE
    let pageData = paginationData.slice(offset, offset + ITEM_PER_PAGE)
    renderUserList(pageData)
  }


  // EventListener:

  // listen to dataPanel
  dataPanel.addEventListener('click', (event) => {
    if (event.target.parentElement.matches('.card')) {
      showUser(event.target.parentElement.dataset.id)
    }
  })

  // listen to search btn
  searchForm.addEventListener('click', (event) => {
    event.preventDefault();
    if (event.target.matches('.btn')) {
      let searchOutput = document.getElementById('search-input').value
      if (searchOutput === '') {
        getTotalPages(data)
        getPageData(pageLink, data)
        searchBy.innerText = ""
      }
      filterData(searchOutput)
      getPageData(pageLink, filter)
      getTotalPages(filter)
    }
  })

  searchForm.addEventListener('keyup', (event) => {
    // detect keyup enter
    if (event.keyCode === 13) {
      let searchOutput = document.getElementById('search-input').value
      if (searchOutput === '') {
        getTotalPages(data)
        getPageData(pageLink, data)
        searchBy.innerText = ""
      }
      filterData(searchOutput)
      getPageData(pageLink, filter)
      getTotalPages(filter)
    } else if (event.keyCode === 8) {
      renderUserList(data)
      searchBy.innerText = ""
    }
  })

  // listen to modal
  favIcon.addEventListener('click', (event) => {
    if (event.target.classList.contains('far')) {
      event.target.classList.remove('far')
      event.target.classList.add('fas')
      addFavoriteItem(event.target.dataset.id)
    }
  })

  // listen to pagination click event
  pagination.addEventListener('click', event => {
    pageLink = event.target.dataset.page
    if (event.target.tagName === 'A') {
      getPageData(pageLink, data)
    }
    console.log(pageLink)
  })

})()
              
            
!
999px

Console