Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

Behavior

Auto Save

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

              
                
<html lang="ja">

<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>TerraceTechフロントエンドエンジニア養成所</title>
</head>

<body>
  <div id="js-wrapper" class="wrapper">
    <ul id="js-ul"></ul>
    <button type="button" id="js-modal-btn" class="modal__btn">Click</button>
  </div>
  <div class="modal" id="js-modal">
    <p class="modal__text">Let's start the request</p>
    <button type="button" id="js-request-btn" class="request__btn">
      Start request
    </button>
    <a href="" class="modal__close-icon"><img src="./img/close-btn.png" alt="" class="modal__close-icon" /></a>
  </div>

</body>

</html>

              
            
!

CSS

              
                .modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50%;
  max-width: 500px;
  background-color: #fff;
  padding: 60px 30px;
  box-shadow: 0 12px 15px 0 rgba(0, 0, 0, 0.25);
  text-align: center;
  font-size: 20px;
  opacity: 0;
  pointer-events: none;
}

.modal__close-icon {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 30px;
  height: 30px;
  cursor: pointer;
}

.modal__btn {
  display: flex;
  justify-content: center;
  margin: 0 auto;
}

.modal__btn,
.request__btn {
  cursor: pointer;
  background-color: #d9f869;
  border: none;
  border-bottom: 5px solid #96cc00;
  padding: 20px 0;
  width: 200px;
  max-width: 100%;
  font-size: 18px;
  border-radius: 50px;
}

.modal__btn:hover,
.request__btn:hover {
  margin-top: 3px;
  border-bottom: 2px solid #96cc00;
}

.modal.visible {
  opacity: 1;
  pointer-events: auto;
}

              
            
!

JS

              
                const wrapper = document.getElementById('js-wrapper');
const modalBtn = document.getElementById('js-modal-btn');
const requestBtn = document.getElementById('js-request-btn');
const modal = document.getElementById('js-modal');


function addLoading() {
  const loading = document.createElement('img');
  loading.src = "./img/loading-circle.gif";
  loading.id = "loading";
  wrapper.appendChild(loading);
}

function removeLoading() {
  const loading = document.getElementById('loading');
  loading.remove();
}

async function fetchErrorHandling(response) {
  if (!response.ok) {
    throw new Error("サーバーエラーが発生しました");
  } else {
    return await response.json();
  }
}

async function fetchData() {
  const response = await fetch("https://jsondata.okiba.me/v1/json/fR9k5211007080400");
  const json = await fetchErrorHandling(response);
  return json;
}

function createLists(values) {
  const frag = document.createDocumentFragment();
  const ul = document.getElementById('js-ul');

  values.data.forEach(value => {
    const li = document.createElement('li');
    const anchor = document.createElement('a');
    const img = document.createElement('img');

    anchor.href = `/${value.a}.html`;
    img.src = value.img;
    img.alt = value.alt;

    frag.appendChild(li).appendChild(anchor).appendChild(img);
    anchor.insertAdjacentHTML('beforeend', value.text);
  });

  ul.appendChild(frag);
}


async function tryCreate() {
  addLoading();
  try {
    const responseData = await fetchData();
    if (!responseData.data) {
      throw new Error("適切なデータが見つかりませんでした");
    } else {
      createLists(responseData);
    }
  } catch (error) {
    const errorMessage = document.createElement("p");
    errorMessage.textContent = error;
    wrapper.appendChild(errorMessage);
    console.error(error);
  } finally {
    removeLoading();
  }
}

modalBtn.addEventListener('click', (e) => {
  e.target.style = "display:none";
  modal.classList.add('visible');
});

requestBtn.addEventListener('click', () => {
  modal.classList.remove('visible');
  tryCreate();
});

              
            
!
999px

Console