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

              
                <h1><strong>cursor parallax</strong> <br>(mouse moves)</h1>

<div class="list">
  <div class="item js-cursor-parallax">
    <div class="js-cursor-parallax__body">
      <div class="js-cursor-parallax__item">
        <img src="https://picsum.photos/id/433/500/400" alt="" width="500" height="400">
      </div>
    </div>
  </div>
  <div class="item js-cursor-parallax">
    <div class="js-cursor-parallax__body">
      <div class="js-cursor-parallax__item">
        <img src="https://picsum.photos/id/1062/500/400" alt="" width="500" height="400">
      </div>
    </div>
    <h2>文字など別の要素も含めたエリアで動かす</h2>
    <p class="">テキストテキストテキスト<br>テキストテキストテキスト</p>
  </div>
</div>
              
            
!

CSS

              
                .js-cursor-parallax__body {
  overflow: hidden;
}
/* 画像の移動スピード */
.js-cursor-parallax__item {
  transition: transform .1s;
}
/* 画像の拡大スピード */
.js-cursor-parallax__item img {
  transition: transform .3s;
}
.js-cursor-parallax:hover .js-cursor-parallax__item img {
  transform: scale(1.05);
}


/* テスト表示用style */
body {
  margin: 0;
  padding: 100px 0;
  text-align: center;
}
h1 {
  text-align: center;
  margin-bottom: 100px;
  font-size: 36px;
}
strong {
  font-size: 150%;
}
img {
  height: auto;
  max-width: 100%;
  vertical-align: top;
}
.list {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}
.item {
  overflow: hidden;
  border-radius: 10px;
  border: 1px solid #ccc;
  margin: 0 30px 60px;
  cursor: pointer;
  background: #f0f0f0;
}
              
            
!

JS

              
                //指定した範囲の中でマウス移動に視差効果を付ける
cursorParallax();
function cursorParallax() {
  const targets = document.querySelectorAll('.js-cursor-parallax');
  //要素が存在しない場合は処理しない
  if(targets.length === 0) {
    return;
  }

  //パララックス効果の強度を制御(画像の拡大率やスピードに合わせて調整する)
  //値が大きいほどパララックス効果は弱くなる
  const PARALLAX_FACTOR = 40;

  /**
   * 正規化(任意の範囲を、0から1の値に変換する)する関数
   * @param {number} value 対象の値
   * @param {number} min 下限値
   * @param {number} max 上限値
   */
  function norm (value, min, max) {
    return (value - min) / (max - min);
  }

  /**
   * 線形補間(範囲と割合を渡すと、その割合の位置にある値を返す)
   * @param {number} min 下限値
   * @param {number} max 上限値
   * @param {number} ratio 割合
   */
  function lerp (min, max, ratio) {
    return min + (max - min) * ratio;
  }

  targets.forEach(function(target) {
    //要素が存在しない場合は処理しない
    const item = target.querySelector('.js-cursor-parallax__item');
    if(!item) {
      return;
    }

    const itemStyle = item.style;
    //マウスがパララックスエリアに入った時のイベント
    let targetX,targetY,
    xCenterItem,yCenterItem,
    targetWidth,targetHeight,
    itemWidth,itemHeight;

    //ホバー時の処理
    target.addEventListener('mouseenter', function() {
      //動かすエリアの位置・サイズを取得
      const targetRect = target.getBoundingClientRect();
      targetX = targetRect.left;
      targetY = targetRect.top;

      targetWidth = targetRect.width;
      targetHeight = targetRect.height;

      //動かす要素のサイズを取得
      const itemRect = item.getBoundingClientRect();
      itemWidth = itemRect.width;
      itemHeight = itemRect.height;

      //動かす要素の中心位置を取得
      xCenterItem = itemWidth/2;
      yCenterItem = itemHeight/2;
    });

    //ホバー+移動中の処理
    target.addEventListener('mousemove', function(e) {
      //表示エリア内でのマウス座標を取得し動かす要素をどの程度移動させれば良いか計算
      const mouseX = e.clientX - targetX;
      const xNorm = norm(mouseX, 0, targetWidth);
      const x = (xCenterItem - lerp(0, itemWidth, xNorm)) / PARALLAX_FACTOR;

      const mouseY = e.clientY - targetY;
      const yNorm = norm(mouseY, 0, targetHeight);
      const y = (yCenterItem - lerp(0, itemHeight, yNorm)) / PARALLAX_FACTOR;

      //パララックスさせる要素にstyleの指定
      itemStyle.transform = `translate3d(${-x}px, ${-y}px,0)`;
    });

    //ホバー終了時にリセット
    target.addEventListener('mouseleave', function() {
      itemStyle.transform = '';
    });
  });
}
              
            
!
999px

Console