Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="wrapper">
<ul class="price_wrapper">
  <li class="origin_price">소비자가 : 50,000원</li>
  <li class="sale_price">판매가 : 35,000원</li>
</ul>
  </div>
            
          
!
            
              .wrapper{
  display:flex;
  justify-content:center;
  align-items:center;
  width:100vw;
  height:100vh;
  background-color:#41da8c;
  
}
.price_wrapper{
  list-style:none;
  padding: 2rem;
  background:#fff;
  border-radius:4px;  
  box-shadow:0px 3px 3px rgba(0,0,0,0.4);
  li{
    text-align:left;
    margin-bottom:0.5rem;
    .sale_percent{
      background-color:#ff4a21;
      padding:0.5rem 0.75rem;
      margin-left:1rem;
      color:#fff;
      border-radius:30px;
      font-weight:bold;
      font-size:0.725rem;      
    }    
  }
  .origin_price{
      font-size:0.875rem;
      color:#adadad;
      text-decoration:line-through;
    }
  .sale_price{
    font-weight:bold;
  }
  
}
            
          
!
            
              function discountPercent() {
    var originPrice = document.querySelector('.origin_price');
    var discountPrice = document.querySelector('.sale_price');
  
    /**
     * originPrice가 undefined와 Null값이 아닐 경우에만 
     * 스크립트가 실행되도록 조건 설정
     */

    if (originPrice != undefined && originPrice != null) {

        // 소비자가에서 문자열('원') 삭제 
        originPrice = originPrice.textContent.replace(/\D+/g, '');
        // 판매가에서 문자열('원') 삭제
        discountPrice = discountPrice.textContent.replace(/\D+/g, '');

        
        /** 
         * originPrice 가 0원일 경우 '-infinity원'으로 출력되는 문제가 발생 
         * originprice 가 0이 아닐 경우에만 계산하도록 조건 설정         
         * */
        if (originPrice != 0) {
            // 퍼센티지 계산 후 소수점 아래 버림 
            var salePoint = 100 - (Math.floor(discountPrice / originPrice * 100));

            // span 노드 생성
            var salePercent = document.createElement('span');

            // 생성된 span 노드에 xx% 형식으로 문자열 생성
            salePercent.textContent = salePoint + '% 할인';

            // sale__percent 클래스 지정
            salePercent.classList.add('sale_percent');
            
            // 삽입할 위치 지정
            document.querySelector('.price_wrapper li:last-child').append(salePercent);
        }
    }
}
discountPercent();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console