2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JQuery でスライダー その2 終了<=>開始

Last updated at Posted at 2013-08-05

メモ

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf8" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>スライダー ケース2</title>
        <script src="/jq-plugin/jquery/jquery-1.10.2.js">
                        <!-- jquery 1.10.2 -->
        </script>
        <script>
            $(document).ready(function() {

                $.fn.slider = function(args) {
                    
                    var param = {};
                    
                    param.slider = args.slider || ".slider";
                    param.moveLeft = args.moveLeft || ".move-left";
                    param.moveRight = args.moveRight || ".move-right";
                    
                    var $this = $(this);
                    var viewpos = 0;
                    var $sliders = $this.find(param.slider);
                    
                    $sliders.each(function() {
                        $(this).css({
                            left : ($(this).index() * $(this).width())
                        });
                    });
                    $this.find(param.moveLeft).click(function() {
                        viewpos--;
                        if (viewpos < 1 - $sliders.size()) {
                            viewpos = 0;
                        }
                        $sliders.each(function() {
                            $(this).animate({
                                left : (($(this).index() + viewpos) * $(this).width())
                            }, param.speed);
                        });
                        return false;
                    });
                    $this.find(param.moveRight).click(function() {
                        viewpos++;
                        if (viewpos > 0) {
                            viewpos = 1 - $sliders.size();
                        }
                        $sliders.each(function() {
                            $(this).animate({
                                left : (($(this).index() + viewpos) * $(this).width())
                            }, param.speed);
                        });
                        return false;
                    });
                };

                // セレクタ変更
                $("#group1").slider({
                    slider : ".slider",
                    moveLeft : ".move-left",
                    moveRight : ".move-right",
                    speed: 100
                });
            });
        </script>
        <style>
            ul.controller li {
                list-style: none;
                display: inline-block;
            }
            div.contents {
                width: 500px;
                height: 300px;
                border: 1px solid black;
                overflow: hidden;
                position: relative;
            }
            div.contents div {
                position: absolute;
                width: 500px;
                height: 300px;
            }
        </style>
    </head>
    <body>
        <div id="group1">
            <ul class="controller">
                <li>
                    <a href="javascript:void(0)" class="move-left" >&lt;</a>
                </li>
                <li>
                    <div id="contents" class="contents">
                        <div class="slider">
                            <span>内容1</span>
                        </div>
                        <div class="slider">
                            <span>内容2</span>
                        </div>
                        <div class="slider">
                            <span>内容3</span>
                        </div>
                        <div class="slider">
                            <span>内容4</span>
                        </div>
                        <div class="slider">
                            <span>内容5</span>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="javascript:void(0)" class="move-right" >&gt;</a>
                </li>
            </ul>
        </div>
    </body>
</html>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?