javascript - slider 坏移

标签 javascript jquery html css

我有一个正在使用的 slider ,每当我单击下一个或上一个按钮时,它都会以覆盖我网站某些元素的方式移动。我相信#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/

最佳答案

我修改了你的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 标签在语义上是不正确的,因为导航元素不是主标题。

我只是去快速修复一下。但如果您想让 slider 成为您页面中的一个独立组件,那么它应该是您应该采取的方向。

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

关于javascript - slider 坏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966880/

相关文章:

javascript - 获取特定值之后的字符串的一部分

html - 如何调整表格页脚的填充

javascript - 更改下拉项

html - 使用 <sub> 标签并保持行高

javascript - 是否可以删除 html 元素并让您的 child 使用 javascript?

javascript - 复选框未通过 jQuery 检查

javascript - JS - 点击事件无法正常工作

javascript - Jasmine 2.0 完成及 afterEach

javascript - 正则表达式替换字符串中 '&'中的所有 ""....在javascript中

javascript - 检查是否检查了子输入