javascript - 如何将 slider 按钮放置在页面的最左侧和最右侧

标签 javascript html css

我最近在我的站点中放置了一个 slider 演示文稿,问题是我希望它像横幅一样从最左边覆盖到最右边,并且图像要调整大小并适合横幅。这是到目前为止 slider 的图片:

还想知道图像和 slider 是否也可以随着网页的大小自动调整大小。这样它对移动设备友好,并且当它不是全屏时,网站也不会乱七八糟。 enter image description here

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Croydon Cycles</title>
    <link rel="stylesheet" href="shop-style.css">
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <link href="https://fonts.googleapis.com/css?family=Karla" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="parallax.min.js"></script>
</head>


<!DOCTYPE html>
<html lang="en">

<body>
    <div class="wrapper">
        <header>
            <nav>
                <div class="menu-icon">
                    <i class="fa fa-bars fa-2x"></i>
                </div>
                <div class="logo">
                    Croydon Cycles
                </div>
                <div class="menu">
                    <ul>
                        <li><a href="#">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Shop</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </nav>
        </header>
        <div class="slideshow-container">
            <div class="myslides fade">
                <div class="numbers">1 / 4</div>
                <div class="slider-1">
                    <img class="Fit" src="images/slider-1.jpg">
                </div>
                <div class="caption">Text 1</div>
            </div>
            <div class="myslides fade">
                <div class="numbers">2 / 4</div>
                <div class="slider-2">
                    <img src="images/2.jpg">
                </div>
                <div class="caption">Text 2</div>
            </div>
        <div class="myslides fade">
            <div class="numbers">3 / 4</div>
            <div><img src="images/3.jpg"></div>
            <div class="caption">Text 3</div>
        </div>
        <div class="myslides fade">
            <div class="numbers">4 / 4</div>
            <div><img src="images/4.jpg"></div>
            <div class="caption">Text 4</div>
        </div>
        <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
        <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>
    <br>
    <div style="text-align:center">
        <span class="dots" onclick="currentSlide(1)"></span> 
        <span class="dots" onclick="currentSlide(2)"></span> 
        <span class="dots" onclick="currentSlide(3)"></span> 
        <span class="dots" onclick="currentSlide(4)"></span> 
    </div>
    <script type="text/javascript">
        var slideIndex = 1;
        showSlide(slideIndex);

        function plusSlides(n){
            showSlide(slideIndex += n);
        }

        function currentSlide(n) {
            showSlide(slideIndex = n);
        }

        function showSlide(n){
            var i;
            var slides = document.getElementsByClassName("myslides");
            var dots = document.getElementsByClassName("dots");

            if (n > slides.length) { slideIndex = 1};

            if (n < 1) { slideIndex = slides.length};

            for (i=0;i<slides.length;i++) {
                slides[i].style.display = "none";
            };

            for (i=0;i<dots.length;i++) {
                dots[i].className = dots[i].className.replace(" active","");
            };

            slides[slideIndex-1].style.display = "block";
            dots[slideIndex-1].className += " active";
        }
    </script>
    <div class="content">
        <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
</div>
<script type="text/javascript">
    // Menu-toggle button
    $(document).ready(function() {
        $(".menu-icon").on("click", function() {
            $("nav ul").toggleClass("showing");
        });
    });

    // Scrolling Effect
    $(window).on("scroll", function() {
        if($(window).scrollTop()) {
            $('nav').addClass('black');
        }
        else {
            $('nav').removeClass('black');
        }
    })
</script>
</body>
</html>

CSS:

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    font-family: verdana,sans-serif;
    margin: 0;
}

body {
    font-family: "Helvetica Neue",sans-serif;
    font-weight: lighter;
}

header {
    width: 100%;
    height: 60px;
    background: url(hero.jpg) no-repeat 50% 50%;
    background-size: cover;
}

.content {
    width: 94%;
    margin: 4em auto;
    font-size: 20px;
    line-height: 30px;
    text-align: justify;
}


.logo {
    line-height: 60px;
    position: fixed;
    float: left;
    margin: 16px 46px;
    color: #fff;
    font-weight: bold;
    font-size: 20px;
    letter-spacing: 2px;
}

nav {
    position: fixed;
    width: 100%;
    line-height: 60px;
    z-index:2;
}

nav ul {
    line-height: 60px;
    list-style: none;
    background: rgba(0, 0, 0, 0);
    overflow: hidden;
    color: #fff;
    padding: 0;
    text-align: right;
    margin: 0;
    padding-right: 40px;
    transition: 1s;
}

nav.black ul {
    background: #000;
}

nav ul li {
    display: inline-block;
    padding: 16px 40px;;
}

nav ul li a {
    text-decoration: none;
    color: #fff;
    font-size: 16px;
}

.menu-icon {
    line-height: 60px;
    width: 100%;
    background: #000;
    text-align: right;
    box-sizing: border-box;
    padding: 15px 24px;
    cursor: pointer;
    color: #fff;
    display: none;
}

@media(max-width: 786px) {

    .logo {
          position: fixed;
          top: 0;
          margin-top: 16px;
    }

    nav ul {
          max-height: 0px;
          background: #000;
    }

    nav.black ul {
          background: #000;
    }

    .showing {
          max-height: 34em;
    }

    nav ul li {
          box-sizing: border-box;
          width: 100%;
          padding: 24px;
          text-align: center;
    }

    .menu-icon {
          display: block;
    }

}

body{
    font-family: verdana,sans-serif;

    margin: 0;

}

.slideshow-container{
    width: 800px;
    position: relative;
    margin: auto;
}

.numbers{
    position: absolute;
    top: 0;
    color: #f2f2f2;
    padding: 8px 12px;
    font-size: 12px;
}

.myslides{
    display: none;
}

.prev , .next{
    position: absolute;
    top: 50%;
    font-size: 18px;
    font-weight: bold;
    padding: 16px;
    margin-top: -22px;
    border-radius: 0 3px 3px 0;
    color: #fff;
    cursor: pointer;
}

.next{
    right: 0;
    border-radius: 3px 0 0 3px;
}

.prev:hover,.next:hover{
    background-color: rgba(0,0,0,0.8);
}

.caption{
    text-align: center;
    position: absolute;
    bottom: 8px;
    width: 100%;
    color: #f2f2f2;
    font-size: 15px;
    padding: 8px 22px;
}

.dots{
    width: 13px;
    height: 13px;
    background: gray;
    display: inline-block;
    border-radius: 50%;
    cursor: pointer;
}

.fade{
    animation-name:fade;
    animation-duration:1.5s;
}

@keyframes fade{
    from {opacity: 0.4;}
    to {opacity: 1;}
}

.active, .dot:hover {
    background-color: #333;
}

.slider-1 {
    display: grid;
    height: 100%;
    z-index:0;
}

.Fit {
    max-width: 100%;
    max-height: 100vh;
    margin: auto;
    z-index:0;  
}

最佳答案

你的 slider 只有 800px 大,prev 和 next 从它确定它们的位置,所以你需要一个更大的容器来让它们相对于它的位置。

您可以添加一个宽度为 100% 的容器并将您的按钮移到它上面。

.slideshow {

    width: 100%;

    position: relative;

    margin: auto;

}
<div class="slideshow">
  <div class="slideshow-container">
		....
  </div>
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

关于javascript - 如何将 slider 按钮放置在页面的最左侧和最右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768841/

相关文章:

html - 当打印到实际打印机时,当空格位于 <br> 标记之前时,IE 将在空格后打印空行,但当 IE 浏览器显示 HTML 时则不会

javascript - 从前一个函数添加到数组

javascript - https 上的不安全内容

javascript - 进度条不工作

javascript - 如何使用 jquery 突出显示事件选项卡

javascript - HTML5 : canvas animated not work in action

javascript - 如何创建像指标一样的循环进度(饼图)

html - 在 li 中间对齐搜索输入 - CSS

javascript - HTML5 日期选择器 - 删除日期箭头

html - Bootstrap 列以适应宽度