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 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

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

              
                <div class="body">
    <div class="box" id="itemA"></div>
    <div class="box" id="itemB"></div>
    <div class="box" id="itemC"></div>
    <div class="box" id="itemD"></div>
    <div class="box" id="itemE"></div>
    <div class="box" id="itemF"></div>
</div>
              
            
!

CSS

              
                .body{
  display: flex;
  width:6000px;
  background: yellow;
  height:50vh;
}
.box{
  width: 100px;
  height: 100px;
  background: skyblue;
  margin: 10px 100px;
}

#itemA{
  width: 200px;
  margin: 10px 200px;
}

#itemB{
  height: 150px;
  margin: 10px 50px;
  background: red;
}

#itemC{
  height: 50px;
  width: 150px;
  background: orange;
}

#itemD{
  height: 70px;
  width: 300px;
  margin: 10px 200px;
  opacity: .5;
}

#itemE{
  height: 150px;
  width: 150px;
  margin: 10px 150px;
  background: white;
}

#itemF{
  height: 200px;
  width: 100px;
  background: black;
  color: white;
}
              
            
!

JS

              
                const getElementRectX = (elem) => {
    let elemRect = document.getElementById(elem).getBoundingClientRect()
    return elemRect.left;
}

let RectData = {};
let idData = ["itemA","itemB","itemC","itemD","itemE","itemF"];
//TODO自動的にidを全件取得できる様にする
let result = idData.map((value) => {
    let k = value;
    let v = getElementRectX(value);
    RectData[k] = v;
  
    document.getElementById(k).innerHTML = "x:" + v;
})

console.log(RectData)

//スクロール系の関数で便利
//scrollToTop("itemB") を実行すると itemBまでスクロールできます
function scrollToTop(elem) {
  let x =  RectData[elem]
  scrollTo(x, 0);
}

//要素を中央に持っていきたい時
const centeringRect = (elem) => {
    let item = document.getElementById(elem).clientWidth;
    let x = (window.innerWidth - item) / 2
    return x
}

//scrollToTop("itemB") を実行すると itemBまでスクロールできます
function scrollToCenter(elem) {
  let itemRect =  RectData[elem]
  console.log(itemRect)
  let additionalCenteringRect = centeringRect(elem)
  console.log(additionalCenteringRect)
  let x = itemRect - additionalCenteringRect;
    console.log(x)
  scrollTo(x, 0);
}
              
            
!
999px

Console