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

              
                <ul>
  <li>up</li>
  <li id="box">box</li>
  <li>down</li>
</ul>


              
            
!

CSS

              
                ul li{list-style: none;padding: 200px;background: yellow}
#box{background: green}
 
              
            
!

JS

              
                // 某个元素是否出现在视口内
  (function () {
    function GetRect (element) {
      var rect = element.getBoundingClientRect() // 距离视窗的距离
      var top = document.documentElement.clientTop ? document.documentElement.clientTop : 0 // html元素对象的上边框的宽度
      var left = document.documentElement.clientLeft ? document.documentElement.clientLeft : 0
      return {
        top: rect.top - top,
        bottom: rect.bottom - top,
        left: rect.left - left,
        right: rect.right - left
      }
    }
    var $box = document.getElementById('box')
    var divH = $box.offsetHeight // div高度
    var windowH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight // 浏览器高度兼容写法
    window.onscroll = function () {
      var obj = GetRect($box)
      if (obj.top >= windowH) { // 在视口之下
        $box.innerText='在视口之下';
      }
      if (obj.top < windowH && obj.bottom >= windowH) { // 正在出现
        $box.innerText='正在出现'
      }
      if (obj.top > 0 && obj.top < windowH && obj.bottom > 0 && obj.bottom < windowH) { // 正在视口中
        $box.innerText='正在视口中'
      }
      if (obj.top <= 0 && obj.bottom <= divH && obj.bottom > 0) { // 正在离开视口
        $box.innerText='正在离开视口'
      }
      if (obj.bottom <= 0) { // 已经离开视口
        $box.innerText='在视口之上'
      }
      console.log($box.innerText)
    }
  })()

              
            
!
999px

Console