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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

            
              <div class="main">
    <div class="main__inner">
        <h1 class="main__ttl">
            <a href="https://nocebo.jp/post-1151/" target="_blank">スライダーを作成する</a>
        </h1>
        <div class="slider">
            <nav class="slider__nav">
                <a href="javascript:;" class="slider__prev"><i class="fas fa-angle-left"></i></a>
                <!-- <a href="javascript:;" class="slider__zoom"><i class="fas fa-search-plus"></i></a> -->
                <a href="javascript:;" class="slider__next"><i class="fas fa-angle-right"></i></a>
            </nav>
            <div class="slider__inner">
                <div class="slider__main">
                    <ul class="slider__list">
                        <li class="slider__elm slider__elm--01">
                            <a href="javascript:;">
                                <img src="https://nocebo.jp/data/zoomslider/img/01.jpg" alt="images">
                            </a>
                        </li>
                        <li class="slider__elm slider__elm--02">
                            <a href="javascript:;">
                                <img src="https://nocebo.jp/data/zoomslider/img/02.jpg" alt="images">
                            </a>
                        </li>
                        <li class="slider__elm slider__elm--03">
                            <a href="javascript:;">
                                <img src="https://nocebo.jp/data/zoomslider/img/03.jpg" alt="images">
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <!-- ====================================__  .slider  ============================= -->
    </div>
</div>
<!-- ====================================__  .main  ============================= -->
            
          
!

CSS

            
              *
{
    box-sizing: border-box;

    text-decoration: none;

    touch-action: none;
}

img
{
    user-select: none;
    pointer-events: none;
}


$baseWidth: 768px;

.contents
{
    height: 100%;
}

.main
{
    display: flex;

    width: $baseWidth;
    min-height: 100%;
    margin: 0 auto;

    background-color: #eee;

    align-items: center;

    .main__inner
    {
        padding: 50px 0;
    }

    .main__ttl
    {
        font-size: 30px;
        font-weight: bold;

        margin-bottom: 20px;
        margin-left: 10px;

        color: #000;
    }
}


.slider
{
    position: relative;

    width: $baseWidth;

    .slider__inner
    {
        overflow: hidden;

        width: 100%;
    }

    .slider__elm
    {
        img
        {
            vertical-align: bottom;
        }
    }

    .slider__nav
    {
        position: absolute;
        z-index: 10;
        bottom: 0;

        display: flex;

        width: 100%;

        justify-content: space-between;

        a
        {
            font-size: 30px;
            font-weight: bold;

            display: block;

            width: 33.2%;
            padding: 25px;
            padding-bottom: 20px;

            text-align: center;
            text-decoration: none;

            color: #000;
            background-color: rgba(#fff, .7);

            &:hover
            {
                background-color: rgba(#fff, 1);
            }
        }
    }

    .slider__list
    {
        display: flex;

        width: max-content;

        li
        {
            width: $baseWidth;

            img
            {
                width: 100%;

                pointer-events: none;
            }
        }
    }
}
            
          
!

JS

            
              // /* ===========================================================
// # Point
// =========================================================== */
class Point {
    constructor(x = 0, y = 0) {
        this.x = x || 0;
        this.y = y || 0;
    }
    static distance(a, b) {
        const dx = a.x - b.x;
        const dy = a.y - b.y;
        return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
    }
    static interpolate(p1, p2, t) {
        const x = p1.x * (1 - t) + p2.x * t;
        const y = p1.y * (1 - t) + p2.y * t;
        return new Point(x, y);
    }
    add(p) {
        this.x += p.x;
        this.y += p.y;
        return this;
    }
    subtract(p) {
        this.x -= p.x;
        this.y -= p.y;
        return this;
    }
    clone() {
        return new Point(this.x, this.y);
    }
}
// /* ===========================================================
// # EventTouch
// =========================================================== */
class EventTouch {
    constructor() {
        if (EventTouch.instance) return;
        EventTouch.instance = this;
        // -----------------------------
        this.isDown = false; //PC用

        this.currentPoint = new Point();
        this.startPoint = new Point();
        this.diffPoint = new Point();

        this.touchesdiff = [];
        this.touchesStartPoints = [new Point(), new Point()];
        this.touchesCurrentPoints = [new Point(), new Point()];

        this.scale = 1;

        // PC
        document.addEventListener("mousedown", this.onStart.bind(this), false);
        document.addEventListener("mousemove", this.onMove.bind(this), false);
        document.addEventListener("mouseup", this.onUp.bind(this), false);
        // SP
        document.addEventListener("touchstart", this.onStart.bind(this), false);
        document.addEventListener("touchmove", this.onMove.bind(this), false);
        document.addEventListener("touchend", this.onUp.bind(this), false);

        document.addEventListener("gesturechange", this.onGestureChange.bind(this), false);
    }

    originalEvent(e) {
        // jquery使用時用にoriginalEvent取得
        return (e = e.originalEvent ? e.originalEvent : e);
    }
    mouseDelta(e) {
        return e.deltaY ? -(e.deltaY) : e.wheelDelta ? e.wheelDelta : -(e.detail);
    }

    clientPosition(e) {
        this.originalEvent(e);
        if (e.touches) {
            return new Point(e.touches[0].clientX, e.touches[0].clientY);
        } else {
            return new Point(e.clientX, e.clientY);
        }
    }

    onStart(e) {
        this.originalEvent(e);
        this.isDown = true;
        this.diffPoint = new Point();
        this.startPoint = this.clientPosition(e);

        if (!e.touches) return;
        for (let i = e.touches.length - 1; i >= 0; i--) {
            this.touchesStartPoints[i] = new Point(e.touches[i].clientX, e.touches[i].clientY);
        }
    }

    onMove(e) {
        this.originalEvent(e);
        this.currentPoint = this.clientPosition(e);

        if (!this.isDown) return;
        this.diffPoint = new Point(this.currentPoint.x, this.currentPoint.y).subtract(this.startPoint);

        if (!e.touches) return;
        for (let i = e.touches.length - 1; i >= 0; i--) {
            this.touchesCurrentPoints[i] = new Point(e.touches[i].clientX, e.touches[i].clientY);
        }
    }

    onUp() {
        this.isDown = false;
    }

    onGestureChange(e) {
        this.originalEvent(e);
        let scale = 1;
        if (e.scale) {
            scale = e.scale;
        } else {
            const startDistance = Point.distance(this.touchesStartPoints[0], this.touchesStartPoints[1]);
            const currentDistance = Point.distance(this.touchesCurrentPoints[0], this.touchesCurrentPoints[1]);
            scale = currentDistance / startDistance;
        }
        this.scale = scale;
    }
}
// /* ===========================================================
// # Slider
// =========================================================== */
class Slider {
    constructor() {
        // -----------------------------
        this.slider = document.querySelector(".slider");
        this.slider__list = document.querySelector(".slider__list");
        this.slider__main = document.querySelector(".slider__main");
        this.slider__prev = document.querySelector(".slider__prev");
        this.slider__next = document.querySelector(".slider__next");
        this.slider__zoom = document.querySelector(".slider__zoom");
        this.slider__elm = document.querySelectorAll(".slider .slider__elm");
        // -----------------------------
        this.state = "ready";
        this.currentID = 1;
        // -----------------------------
        this.factor = 0.15; // イージング 係数
        this.fraction = 0.001; // スライド切り替え終点位置の繰り上げ・切り下げ 端数
        this.sliderWidth = 0; //スライド自体の幅
        this.responseRatio = 0.1; // mouse&touchmove時にスライドorもと位置に戻すかの判定
        // -----------------------------
        this.currentPoint = new Point(); // 現在位置
        this.targetPoint = new Point(); // 目標位置
        // -----------------------------
        this.resize();
        this.events();
        this.elements();
        this.update();
    }

    events() {
        this.slider__prev.addEventListener("click", () => {
            this.slide("prev");
        }, false);
        this.slider__next.addEventListener("click", () => {
            this.slide("next");
        }, false);
        // this.slider__zoom.addEventListener("click", () => {
        //     Modal.instance.open(this.currentID);
        // }, false);

        this.slider__main.addEventListener("mousemove", this.onMove.bind(this), false);
        this.slider__main.addEventListener("touchmove", this.onMove.bind(this), false);
        this.slider__main.addEventListener("mouseup", this.onUp.bind(this), false);
        this.slider__main.addEventListener("touchend", this.onUp.bind(this), false);

        window.addEventListener("resize", this.resize.bind(this), false);
    }

    resize() {
        this.sliderWidth = this.slider.clientWidth;
        Object.assign(this.slider__main.style, {
            transform: `translate(${-this.sliderWidth}px, 0px)`
        });
    }

    onMove() {
        if (this.state != "ready") return;
        if (!EventTouch.instance.isDown) return;
        this.targetPoint.x = EventTouch.instance.diffPoint.x;
    }

    onUp() {
        if (this.state != "ready") return;
        const ratio = Math.abs(EventTouch.instance.diffPoint.x) / this.sliderWidth;
        if (ratio < this.responseRatio) {
            this.targetPoint.x = 0;
            return;
        }
        const state = (Math.sign(EventTouch.instance.diffPoint.x) != 1) ? "next" : "prev";
        this.slide(state);
    }

    clear() {
        this.currentPoint = new Point();
        this.targetPoint = new Point();
        this.state = "ready";
    }

    elements() {
        const slider__elm = document.querySelectorAll(".slider .slider__elm");
        const firstNode = slider__elm[0];
        const lastNode = slider__elm[slider__elm.length - 1];

        if (this.state == "next") {
            this.slider__list.append(firstNode);
        } else {
            this.slider__list.insertBefore(lastNode, firstNode);
        }
    }

    slide(_state) {
        if (this.state != "ready") return;
        this.state = _state;

        if (_state == "prev") {
            this.targetPoint.x = this.sliderWidth;
            this.currentID--;
        }
        if (_state == "next") {
            this.targetPoint.x = -this.sliderWidth;
            this.currentID++;
        }

        if (this.currentID < 1) this.currentID = this.slider__elm.length;
        if (this.currentID > this.slider__elm.length) this.currentID = 1;
    }

    update() {
        window.requestAnimationFrame(this.update.bind(this));

        this.currentPoint.x += Number(this.targetPoint.x - this.currentPoint.x) * this.factor;
        const ratio = Math.abs(this.currentPoint.x) / this.sliderWidth;

        if (ratio > (1.0 - this.fraction)) {
            this.currentPoint.x = this.targetPoint.x;
            this.elements();
            this.clear();
        }
        if (ratio < this.fraction) {
            this.currentPoint.x = this.targetPoint.x = 0;
        }

        Object.assign(this.slider__list.style, {
            transform: `translate(${this.currentPoint.x}px, 0px)`
        });
    }
}
/* ===========================================================
# DOMContentLoaded
=========================================================== */
window.addEventListener("DOMContentLoaded", () => {
    new EventTouch();
    new Slider();
}, false);
            
          
!
999px

Console