HTML / CSS / JS / JQUERY:即使调整窗口大小,也可以修复对象

时间:2016-12-18 11:45:43

标签: javascript jquery html css fixed

我目前正在尝试制作一些带有方框的滑块,它们显示了滑块的当前值。由于互联网上的所有方式都不起作用,我只是试着移动一个带有滑块的盒子。但是当我的窗户较小时,盒子不在正确的位置,当我调整窗口大小时,它们就会移动。有没有人知道如何将它们放在正确的位置,即使窗口较小或调整大小?

这是我的代码(这仅用于测试,绝对不是完美或最终):

<html>

<head>
    <title>Home</title>

    <!-- CSS -->
    <link rel="stylesheet" href="stylesheet.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

    <!-- JQuery -->
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</head>

<body>

    <div id="login-box">
        <div id="logo"><img border="0" src="logo.png"></div>
        <div id="text_boost">Boost your account now!</div>

        <div id="slider1"><input id="range1" type="range" min="10" max="100" value="0" step="1" onchange="change(1)"></div>
        <div id="slider2"><input id="range2" type="range" min="0.5" max="4" value="0" step="0.5" onchange="change(2)"/></div>
    </div>


    <div id="sliderInfo1" class= "ui-widget-content" style="background-color: #d9d9d9; border-radius:10px; width: 59px;
                                                          height: 22px; text-align:center; display: table">
        <span style="line-height: 22px"></span>
        <div style="display: table-cell; vertical-align: middle; text-align: center; font-size:12px;"><span id="slider_1">10.000</span></div>
    </div>

    <div id="sliderInfo2" class= "ui-widget-content" style="background-color: #d9d9d9; border-radius:10px; width: 59px;
                                                          height: 22px; text-align:center; display: table">
        <span style="line-height: 22px"></span>
        <div style="display: table-cell; vertical-align: middle; text-align: center; font-size:12px;"><span id="slider_2">500</span></div>
    </div>

<script type="text/javascript">
    var newPoint, newPlace, offset;

    var savedOffset_1 = document.getElementById("sliderInfo1").getBoundingClientRect().top - 
                    document.getElementById("range1").getBoundingClientRect().top;
    change(1);

    var savedOffset_2 = document.getElementById("sliderInfo2").getBoundingClientRect().top - 
                    document.getElementById("range2").getBoundingClientRect().top;
    change(2);

    function change (id) { 
        var el = $("#range" + id);
        var top = el.position().top;
        var left = el.position().left;

        var bodyRect = document.getElementById("range" + id).getBoundingClientRect(),
            elemRect = document.getElementById("sliderInfo" + id).getBoundingClientRect(),
            offset_2   = elemRect.left - bodyRect.left;

        if(id == 1) {
            offset_1 = savedOffset_1;
        } else if(id == 2) {
            offset_1 = savedOffset_2;
        }

        // Measure width of range input
        var width = 430;

        // Figure out placement percentage between left and right of input
        newPoint = (el.val() - el.attr("min")) / (el.attr("max") - el.attr("min"));

        offset = -1;

        // calculate new box place
        if (newPoint < 0) { 
            newPlace = 0;
        } else if (newPoint > 1) { 
            newPlace = (bodyRect.left - width); 
        } else { 
            newPlace = width * newPoint + left + width + (59 / 2); 
            offset -= newPoint; 
        }

        // move box
        $("#sliderInfo" + id).css({ position: "fixed", left: newPlace, marginLeft: offset + "%", top: top - offset_1 - 5 + "px",
                                width: 59 + "px", height: 22 + "px", display: "table", fontSize: 12 + "px", 
                                backgroundColor: "#d9d9d9", borderRadius: 10 + "px", verticalAlign: "middle", textAlign: "center",
                                lineHeight: 22 + "px"})
                         .text(numberWithCommas(el.val() * 1000));

    }

    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
    }

    </script>
</body>

我的CSS: http://pastebin.com/rASpFWjN

Full Screen Window

Resized Window

1 个答案:

答案 0 :(得分:0)

通过将它们定位为固定,您还必须捕获window.onresizewindow.onscroll事件,并相应地定位标签。这是因为position: fixed始终与您的浏览器视口相关。因此,无论窗口有多大或滚动多少,position: fixed; top: 100px; left: 100px的元素总是距窗口左上角100px。

但是,在您的情况下,使用position: fixed会使代码变得不必要地复杂化。您可以将标签放在滑块div中,这样它们将始终相对于滑块。

<div id="slider1">
  <div id="sliderInfo1" class="sliderInfo">
    <span id="slider_1">10.000</span>
  </div>

  <input id="range1" type="range" min="10" max="100" value="0" step="1" />
</div>

<div id="slider2">
  <div id="sliderInfo2" class="sliderInfo">
    <span id="slider_2">500</span>
  </div>

  <input id="range2" type="range" min="0.5" max="4" value="0" step="0.5" />
</div>

这也意味着您必须在javascript中进行更少的位置计算。

function change(id) {
  var thumbWidth = 28;
  var el = $("#range" + id);
  var inputWidth = el.width() - thumbWidth;
  var newPoint = (el.val() - el.attr("min")) / (el.attr("max") - el.attr("min"));
  var newPlace = inputWidth * newPoint;

  // add half the thumb width to the new left to position correctly.
  newPlace += (thumbWidth / 2);    

  $("#sliderInfo" + id).css({
    left: newPlace,
  }).text(numberWithCommas(el.val() * 1000));
}

https://jsfiddle.net/dnm7qr6j/

上的完整示例