如何使用jQuery更改量规大小?

时间:2019-06-20 13:18:11

标签: javascript jquery html css

我创建了一个“找到的”量规,我想更改它的大小,因为现在看起来不太好。

我试图从CSS更改它,但是我的尝试完全破坏了测量范围。

我的代码:

$(document).ready(function () {
    var gaugeObj = $('#gauge1'),
        gaugeMin = gaugeObj.data('min'),
        gaugeMax = gaugeObj.data('max'),
        gaugeValue = gaugeObj.data('value');
    setRangeText(gaugeObj);
    setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue);

    function setRangeText(gaugeObj) {
        gaugeObj.find('.gauge-min').text(gaugeMin);
        gaugeObj.find('.gauge-max').text(gaugeMax);
        gaugeObj.find('.gauge-value').text(gaugeValue);
    }

    function setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue) {
        var percentage = (gaugeValue / gaugeMax),
            degrees = 180 * percentage,
            pointerDegrees = degrees - 90,
            spinner = gaugeObj.find('.gauge-spinner'),
            pointer = gaugeObj.find('.pointer');

        spinner.css('transform', 'rotate(' + degrees + 'deg)');
        pointer.css('transform', 'rotate(' + pointerDegrees + 'deg)');
    }

});
.gauge-wrapper {
    margin-top: 10px;
    padding-bottom: 4rem;
}

    .gauge-wrapper:not(:last-of-type) {
        margin-bottom: 10px;
    }

.gauge {
    width: 250px;
    height: 125px;
    position: relative;
    overflow: hidden;
}

.gauge-outer {
    display: inline-block;
    position: relative;
    width: 250px;
    height: 125px;

}

.gauge-inner {
    width: 250px;
    height: 125px;
    display: block;
    background-color: #ddd;
    border-radius: 250px 250px 0 0;
    z-index: 1;
}

    .gauge-inner::after {
        content: '';
        width: 170px;
        height: 85px;
        top: 40px;
        left: 40px;
        background-color: #fff;
        border-radius: 250px 250px 0 0;
        position: absolute;
        z-index: 3;
    }

.gauge-spinner {
    width: 250px;
    height: 125px;
    background: #4bd440;
    position: absolute;
    z-index: 2;
    transform-origin: top center;
    transform: rotate(0deg);
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
    transform: translateZ(0);
    border-radius: 0 0 250px 250px;
}

.gauge-range {
    position: relative;
}

.gauge-min, .gauge-max {
    position: absolute;
    padding-top: .5em;
    padding-bottom: .5em;
    width: 40px;
    color: #aaa;
    text-align: center;
}

.gauge-min {
    left: 0;
}

.gauge-max {
    right: 0;
}

.gauge-value {
    position: absolute;
    left: 0;
    right: 0;
    top: .5em;
    text-align: center;
    font-size: 2.5em;
    font-weight: 600;
    color: #000;
}

.pointer {
    display: block;
    width: 6px;
    height: 130px;
    background-color: #000;
    border-radius: 4px 4px 0 0;
    position: absolute;
    z-index: 4;
    bottom: 0;
    left: 122px;
    transform: rotate(-90deg);
    transform-origin: center bottom;
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <div class="gauge-wrapper">
                    <div class="gauge-outer" id="gauge1" data-min="1" data-max="150" data-value="77">
                        <div class="gauge">
                            <div class="gauge-inner"></div>
                            <div class="gauge-spinner alt"></div>
                        </div>
                        <div class="pointer"></div>
                        <div class="gauge-range">
                            <div class="gauge-min"></div>
                            <div class="gauge-max"></div>
                            <div class="gauge-value"></div>
                        </div>
                    </div>
                </div>

所以我尝试在CSS中更改此参数:

.gauge {
width: 250px;
height: 125px;
position: relative;
overflow: hidden;
}

经过几个小时的努力,我找不到如何更改此量表尺寸的解决方案。所以现在我想缩小它。 如何在不破坏仪表的情况下做到这一点?

2 个答案:

答案 0 :(得分:3)

您的问题有点不清楚。较小?小多少?您可以根据比例手动更改heightwidth。但是,您也可以使用transform:scale(0.x)将其缩小。

例如,我添加了.gauge-outer {transform:scale(0.5)},它将使所有1/2都具有原始大小。

$(document).ready(function () {
    var gaugeObj = $('#gauge1'),
        gaugeMin = gaugeObj.data('min'),
        gaugeMax = gaugeObj.data('max'),
        gaugeValue = gaugeObj.data('value');
    setRangeText(gaugeObj);
    setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue);

    function setRangeText(gaugeObj) {
        gaugeObj.find('.gauge-min').text(gaugeMin);
        gaugeObj.find('.gauge-max').text(gaugeMax);
        gaugeObj.find('.gauge-value').text(gaugeValue);
    }

    function setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue) {
        var percentage = (gaugeValue / gaugeMax),
            degrees = 180 * percentage,
            pointerDegrees = degrees - 90,
            spinner = gaugeObj.find('.gauge-spinner'),
            pointer = gaugeObj.find('.pointer');

        spinner.css('transform', 'rotate(' + degrees + 'deg)');
        pointer.css('transform', 'rotate(' + pointerDegrees + 'deg)');
    }

});
.gauge-wrapper {
    margin-top: 10px;
    padding-bottom: 4rem;
}

    .gauge-wrapper:not(:last-of-type) {
        margin-bottom: 10px;
    }

.gauge {
    width: 250px;
    height: 125px;
    position: relative;
    overflow: hidden;
}

.gauge-outer {
    display: inline-block;
    position: relative;
    width: 250px;
    height: 125px;

}

.gauge-inner {
    width: 250px;
    height: 125px;
    display: block;
    background-color: #ddd;
    border-radius: 250px 250px 0 0;
    z-index: 1;
}

    .gauge-inner::after {
        content: '';
        width: 170px;
        height: 85px;
        top: 40px;
        left: 40px;
        background-color: #fff;
        border-radius: 250px 250px 0 0;
        position: absolute;
        z-index: 3;
    }

.gauge-spinner {
    width: 250px;
    height: 125px;
    background: #4bd440;
    position: absolute;
    z-index: 2;
    transform-origin: top center;
    transform: rotate(0deg);
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
    transform: translateZ(0);
    border-radius: 0 0 250px 250px;
}

.gauge-range {
    position: relative;
}

.gauge-min, .gauge-max {
    position: absolute;
    padding-top: .5em;
    padding-bottom: .5em;
    width: 40px;
    color: #aaa;
    text-align: center;
}

.gauge-min {
    left: 0;
}

.gauge-max {
    right: 0;
}

.gauge-value {
    position: absolute;
    left: 0;
    right: 0;
    top: .5em;
    text-align: center;
    font-size: 1.2em;
    font-weight: 600;
    color: #000;
}

.pointer {
    display: block;
    width: 6px;
    height: 130px;
    background-color: #000;
    border-radius: 4px 4px 0 0;
    position: absolute;
    z-index: 4;
    bottom: 0;
    left: 122px;
    transform: rotate(-90deg);
    transform-origin: center bottom;
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
}

.gauge-outer {
transform:scale(0.5)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <div class="gauge-wrapper">
                    <div class="gauge-outer" id="gauge1" data-min="1" data-max="150" data-value="77">
                        <div class="gauge">
                            <div class="gauge-inner"></div>
                            <div class="gauge-spinner alt"></div>
                        </div>
                        <div class="pointer"></div>
                        <div class="gauge-range">
                            <div class="gauge-min"></div>
                            <div class="gauge-max"></div>
                            <div class="gauge-value"></div>
                        </div>
                    </div>
                </div>

答案 1 :(得分:1)

更新了以下类的css。

  1. .pointer
  2. .gauge
  3. .gauge-outer
  4. .gauge-inner
  5. .gauge-inner :: after
  6. .gauge-spinner
  

我刚刚展示了一种编辑方式,用于修改其高度,宽度,边框和其他CSS。您必须按照自定义尺寸对其进行编辑。

$(document).ready(function () {
    var gaugeObj = $('#gauge1'),
        gaugeMin = gaugeObj.data('min'),
        gaugeMax = gaugeObj.data('max'),
        gaugeValue = gaugeObj.data('value');
    setRangeText(gaugeObj);
    setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue);

    function setRangeText(gaugeObj) {
        gaugeObj.find('.gauge-min').text(gaugeMin);
        gaugeObj.find('.gauge-max').text(gaugeMax);
        gaugeObj.find('.gauge-value').text(gaugeValue);
    }

    function setGauge(gaugeObj, gaugeMin, gaugeMax, gaugeValue) {
        var percentage = (gaugeValue / gaugeMax),
            degrees = 180 * percentage,
            pointerDegrees = degrees - 90,
            spinner = gaugeObj.find('.gauge-spinner'),
            pointer = gaugeObj.find('.pointer');

        spinner.css('transform', 'rotate(' + degrees + 'deg)');
        pointer.css('transform', 'rotate(' + pointerDegrees + 'deg)');
    }

});
.gauge-wrapper {
    margin-top: 10px;
    padding-bottom: 4rem;
}

    .gauge-wrapper:not(:last-of-type) {
        margin-bottom: 10px;
    }

.gauge {
    width: 200px;
    height: 100px;
    position: relative;
    overflow: hidden;
}

.gauge-outer {
    display: inline-block;
    position: relative;
    width: 200px;
    height: 100px;

}

.gauge-inner {
    width: 200px;
    height: 100px;
    display: block;
    background-color: #ddd;
    border-radius: 200px 200px 0 0;
    z-index: 1;
}

    .gauge-inner::after {
        content: '';
        width: 116px;
        height: 60px;
        top: 40px;
        left: 40px;
        background-color: #fff;
        border-radius: 200px 200px 0 0;
        position: absolute;
        z-index: 3;
    }

.gauge-spinner {
    width: 200px;
    height: 100px;
    background: #4bd440;
    position: absolute;
    z-index: 2;
    transform-origin: top center;
    transform: rotate(0deg);
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
    transform: translateZ(0);
    border-radius: 0 0 200px 200px;
}

.gauge-range {
    position: relative;
}

.gauge-min, .gauge-max {
    position: absolute;
    padding-top: .5em;
    padding-bottom: .5em;
    width: 40px;
    color: #aaa;
    text-align: center;
}

.gauge-min {
    left: 0;
}

.gauge-max {
    right: 0;
}

.gauge-value {
    position: absolute;
    left: 0;
    right: 0;
    top: .5em;
    text-align: center;
    font-size: 2.5em;
    font-weight: 600;
    color: #000;
}

.pointer {
    display: block;
    width: 6px;
    height: 102px;
    background-color: #000;
    border-radius: 4px 4px 0 0;
    position: absolute;
    z-index: 4;
    bottom: 0;
    left: 100px;
    transform: rotate(-90deg);
    transform-origin: center bottom;
    transition: transform 800ms cubic-bezier(0.5, 0.03, 0.06, 1.01);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <div class="gauge-wrapper">
                    <div class="gauge-outer" id="gauge1" data-min="1" data-max="150" data-value="77">
                        <div class="gauge">
                            <div class="gauge-inner"></div>
                            <div class="gauge-spinner alt"></div>
                        </div>
                        <div class="pointer"></div>
                        <div class="gauge-range">
                            <div class="gauge-min"></div>
                            <div class="gauge-max"></div>
                            <div class="gauge-value"></div>
                        </div>
                    </div>
                </div>