滑块坏移位

时间:2015-06-21 16:38:39

标签: javascript jquery html css

我正在使用这个滑块,每当我点击下一个或上一个按钮时,它就会覆盖我网站的某些元素。我相信#mask中的问题,我试图改变它,但没有任何区别,任何想法,如果它导致问题或其他什么?和任何解决方案?

代码:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>Gallery</title>
        <meta name="description" content="An interactive getting started guide for Brackets.">
        <link rel="stylesheet" href="">
<style>
       body {
    margin: 0;
}
#wrapper {
    width: 100%;
    height: 350px;
    /*position: absolute;*/
    top: 0;
    left: 0;
    background-color: white;
    overflow: hidden;
}
#nav p {
    position: absolute;
    top: 100px;
    cursor:pointer;
    color:grey;

}
#prev {
    left: 40px;
}
#next {
    right: 40px;
}
#mask {
    width: 50000px;
    height: 100%;
    background-color: white;
}
.item {
    width: 1200px;
    height: 100%;
    float: left;
    background-color: white;
}
.content img {
    height: 100px;
    width: 17%;
    float:left;
    margin-right: 10px;
    margin-bottom: 10px;
}
.content {
    width: 50%;
    height: 220px;
    top: 20%;
    margin: auto;
    background-color: white;
    position: relative;
}
.content a {
    position: relative;
    top: -17px;
    left: 170px;
}
.selected {
    background: #fff;
    font-weight: 700;
}
.clear {
    clear:both;
}



        </style>
    </head>

    <body>
<div id="wrapper">
    <div id="nav">
        <h1><p id="prev">&lt;</p></h1>
        <h1><p id="next">&gt;</p></h1>
    </div>
    <div id="mask">
        <div id="item1" class="item"> <a name="item1"></a>

            <div class="content">
                <img id="image1" src="http://placehold.it/350x150" alt="slot1" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
            </div>
        </div>
        <div id="item2" class="item">
            <div class="content">
                <img id="image1" src="http://placehold.it/350x150" alt="slot2" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
            </div>
        </div>
        <div id="item3" class="item">
            <div class="content">
                <img id="image1" src="http://placehold.it/350x150" alt="slot2" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
            </div>
        </div>
    </div>
</div>






<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$(document).ready(function () {
    var newItem = 0;
    var itemCount = $('.item').length;

    function shift(direction) {
        var $mask = $('#mask'),
            $items = $('.item'),
            currentItem = $mask.data('currentItem');

        if (currentItem === undefined) {
            currentItem = 0;
        }

        $mask.data('currentItem', newItem).animate({
            marginLeft: -newItem * $items.eq(0).width()
        });
    }

    $('#prev').click(function () {
        if (newItem === 0) {
            newItem = itemCount - 1;
        } else {
            newItem--;
        }
        return shift();
    });
    $('#next').click(function () {
        if (newItem === itemCount - 1) {
            newItem = 0;
        } else {
            newItem++;
        }
        return shift();
    });

    function resizePanel() {
        width = $(window).width();
        height = $(window).height();

        $('#wrapper, .item').css({
            width: width,
            height: height
        });
    }

    $(window).resize(function () {
        resizePanel();
    });
    resizePanel();
});
        </script>
    </body>
</html>

JSFIDDLE:https://jsfiddle.net/ew98y5fv/

1 个答案:

答案 0 :(得分:2)

我修改了你的HTML:

<body>
<div id="wrapper">
    <div id="slider-container">
        <div id="nav">
            <p id="prev">&lt;</p>
            <p id="next">&gt;</p>
        </div>
        <div class="slider-view-area">
            <div id="mask">
                <div id="item1" class="item"> <a name="item1"></a>

                    <div class="content">
                        <img id="image1" src="http://placehold.it/350x150" alt="slot1" />
                        <img id="image2" src="http://placehold.it/350x150" />
                        <img id="image3" src="http://placehold.it/350x150" />
                        <img id="image4" src="http://placehold.it/350x150" />
                        <img id="image5" src="http://placehold.it/350x150" />
                        <img id="image6" src="http://placehold.it/350x150" />
                        <img id="image7" src="http://placehold.it/350x150" />
                        <img id="image8" src="http://placehold.it/350x150" />
                        <img id="image9" src="http://placehold.it/350x150" />
                        <img id="image10" src="http://placehold.it/350x150" />
                    </div>
                </div>
                <div id="item2" class="item">
                    <div class="content">
                        <img id="image1" src="http://placehold.it/350x150" alt="slot2" />
                        <img id="image2" src="http://placehold.it/350x150" />
                        <img id="image3" src="http://placehold.it/350x150" />
                        <img id="image4" src="http://placehold.it/350x150" />
                        <img id="image5" src="http://placehold.it/350x150" />
                        <img id="image6" src="http://placehold.it/350x150" />
                        <img id="image7" src="http://placehold.it/350x150" />
                        <img id="image8" src="http://placehold.it/350x150" />
                        <img id="image9" src="http://placehold.it/350x150" />
                        <img id="image10" src="http://placehold.it/350x150" />
                    </div>
                </div>
                <div id="item3" class="item">
                    <div class="content">
                        <img id="image1" src="http://placehold.it/350x150" alt="slot2" />
                        <img id="image2" src="http://placehold.it/350x150" />
                        <img id="image3" src="http://placehold.it/350x150" />
                        <img id="image4" src="http://placehold.it/350x150" />
                        <img id="image5" src="http://placehold.it/350x150" />
                        <img id="image6" src="http://placehold.it/350x150" />
                        <img id="image7" src="http://placehold.it/350x150" />
                        <img id="image8" src="http://placehold.it/350x150" />
                        <img id="image9" src="http://placehold.it/350x150" />
                        <img id="image10" src="http://placehold.it/350x150" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

这是css:

body {
    margin: 0;
}
#wrapper {
    background-color: blue;
    padding: 20px 0;
}

#slider-container {
    padding: 20px 50px;
    height: 350px;
    background-color: white;
    overflow: hidden;
    position: relative;
}

.slider-view-area {
    overflow: hidden;
}

#nav p {
    position: absolute;
    top: 100px;
    cursor:pointer;
    color:grey;

}
#prev {
    left: 20px;
    font-size: 30px;
}
#next {
    right: 20px;
    font-size: 30px;
}
#mask {
    width: 50000px;
    height: 100%;
    background-color: white;
}
.item {
    width: 1200px;
    height: 100%;
    float: left;
    background-color: white;
}
.content img {
    height: 100px;
    width: 17%;
    float:left;
    margin-right: 10px;
    margin-bottom: 10px;
}
.content {
    width: 50%;
    height: 220px;
    top: 20%;
    margin: auto;
    background-color: white;
    position: relative;
}
.content a {
    position: relative;
    top: -17px;
    left: 170px;
}
.selected {
    background: #fff;
    font-weight: 700;
}
.clear {
    clear:both;
}

我删除了导航元素周围的h1标签,因为您不应该使用标头标签进行显示更改,例如使图标看起来更大。为此你应该使用CSS。在这种情况下,h1标签在语义上是不正确的,因为导航元素不是主标题。

我刚刚去了一个快速解决方案。但是如果你想让滑块成为你页面中的一个独立组件,你应该采取的方向。

这是jsfiddle:https://jsfiddle.net/ew98y5fv/3/