繁体   English   中英

我想为每个 oninput 的元素添加特定数量

[英]I want to add specific amount to a element on every oninput

我制作了一个范围滑块,但我在输入时遇到问题我想以另一个数量添加特定数量,它应该在滑块的每个输入上增加数量。 我想在 2000 美元和 45000 美元中将按钮上的金额增加 150 美元,每当滑块范围接受输入时? 如果有人可以帮助我实现它,如果可以的话,那将是一个很大的帮助!

 var range = document.getElementById("range"); var val = document.getElementById("value"); var vvv = document.getElementById("vvv"); var saved = document.getElementById("saved"); val.innerHTML = "$" + range.value + "K"; range.oninput = function() { val.innerHTML = "$" + this.value + "K"; }
 html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; outline: 0; font-size: 100%; vertical-align: baseline; background: transparent; } body { line-height: 1; } article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } nav ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } a { margin: 0; padding: 0; font-size: 100%; vertical-align: baseline; background: transparent; } /* change colours to suit your needs */ ins { background-color: #ff9; color: #000; text-decoration: none; } /* change colours to suit your needs */ mark { background-color: #ff9; color: #000; font-style: italic; font-weight: bold; } del { text-decoration: line-through; } abbr[title], dfn[title] { border-bottom: 1px dotted; cursor: help; } table { border-collapse: collapse; border-spacing: 0; } /* change border colour to suit your needs */ hr { display: block; height: 1px; border: 0; border-top: 1px solid #cccccc; margin: 1em 0; padding: 0; } input, select { vertical-align: middle; } *, *:before, *:after { -webkit-box-sizing: border-box; box-sizing: border-box; } body { font-family: sans-serif; padding: 60px 20px; } @media (min-width: 600px) { body { padding: 60px; } } .head { display: flex; justify-content: center; margin-bottom: 80px; } .head h2 { font-size: 2.75rem; font-weight: bold; } .range-slider { display: flex; justify-content: center; } .range-slider { width: 100%; } .range-slider__range { -webkit-appearance: none; width: calc(90% - (73px)); height: 10px; border-radius: 5px; outline: none; padding: 0; margin: 0; -webkit-transition: 0.2s; opacity: 0.7; transition: opacity 0.2s; height: 20px; outline: none; background: linear-gradient(90deg, rgb(214, 214, 214) 60%, rgb(214, 214, 214) 60%); border-radius: 12px; } .range-slider__range:hover { opacity: 1; } .range-slider__range::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #2c3e50; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider__range::-webkit-slider-thumb:hover { background: #1abc9c; } .range-slider__range:active::-webkit-slider-thumb { background: #1abc9c; } .range-slider__range::-moz-range-thumb { width: 20px; height: 20px; border: 0; border-radius: 50%; background: #2c3e50; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider__range::-moz-range-thumb:hover { background: #1abc9c; } .range-slider__range:active::-moz-range-thumb { background: #1abc9c; } .range-slider__range:focus::-webkit-slider-thumb { -webkit-box-shadow: 0 0 0 3px #fff, 0 0 0 6px #1abc9c; box-shadow: 0 0 0 3px #fff, 0 0 0 6px #1abc9c; } .range-slider__value { display: inline-block; position: relative; width: 70px; color: #fff; line-height: 20px; text-align: center; border-radius: 3px; background: #2c3e50; padding: 5px 10px; margin-left: 10px; } .range-slider__value:after { position: absolute; top: 3px; left: -7px; width: 0; height: 0; border-top: 7px solid transparent; border-right: 7px solid #2c3e50; border-bottom: 7px solid transparent; content: ''; } ::-moz-range-track { background: #d7dcdf; border: 0; } input::-moz-focus-inner, input::-moz-focus-outer { border: 0; } .values h2 { font-weight: bold; font-size: 35px; color: #636363; } .values p { color: #24A98F; font-size: 20px; margin-top: 5px; } .buttons { margin-top: 50px; width: 100%; } .btn button { padding: 15px 80px; border: none; border-radius: 5px; background-color: #58DBC2; color: white; font-size: 25px; font-weight: bold; box-shadow: 0px 5px 0px #45CDB1; } .chk p { margin-top: 30px; color: #6C6363; font-size: 18px; font-weight: bold; line-height: 25px; } @media(max-width:500px) { .chk p { margin-top: 30px; color: #6C6363; font-size: 15px; font-weight: bold; line-height: 25px; } } @media(max-width:768px) { .head h2 { font-size: 30px; font-weight: bold; } } @media(max-width:440px) { .head h2 { font-size: 20px; font-weight: bold; } } @media(max-width:503px) { .btn button { padding: 10px 50px; border: none; border-radius: 5px; background-color: #58DBC2; color: white; font-size: 15px; font-weight: bold; box-shadow: 0px 5px 0px #45CDB1; } } @media(max-width:351px) { .btn button { padding: 10px 30px; border: none; border-radius: 5px; background-color: #58DBC2; color: white; font-size: 10px; font-weight: bold; box-shadow: 0px 5px 0px #45CDB1; } }
 <script src="https://kit.fontawesome.com/b99f2762a6.js" crossorigin="anonymous"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <div class="heading"> <div class="container"> <div class="row"> <div class="col-12 col-lg-12"> <div class="head"> <h2>How Much Will You Save?</h2> </div> </div> </div> </div> </div> <div class="range"> <div class="container"> <div class="row"> <div class="col-12 col-l-12"> <div class="range-slider"> <input id="range" class="range-slider__range" type="range" value="150" step="5" min="150" max="1000"> <span id="value" class="range-slider__value">0</span> </div> </div> </div> </div> </div> <div class="words text-center mt-5"> <div class="container"> <div class="row"> <div class="col-12 col-lg-5"> <div class="values"> <h2>Homie Flat Fee </h2> <p>What is this?</p> </div> </div> <div class="col-12 col-lg-2 mb-sm-2 mb-md-2 mb-lg-0 mt-3 mt-sm-0 mt-md-3"> <div class="values"> <h2>VS</h2> </div> </div> <div class="col-12 col-lg-5 "> <div class="values"> <h2 id="saved">$4500K</h2> <p>Traditional agent commission* </p> </div> </div> </div> </div> </div> <div class="buttons text-center"> <div class="container"> <div class="row"> <div class="col-12 col-lg-12"> <div class="btn"> <button>Click to Save <span id="vvv">$2000</span></button> </div> <div class="chk text-sm-center text-md-center"> <p>Savings calculations based on difference between 3% listing agent commission and Homie's flat listing fee. Savings may vary.</p> </div> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

let vvvMultiplier = .03
let savedMultiplier = .013
let template = (value) => {
  return `$${value}K`
}
range.oninput = function () {
  let value = Number(this.value)
  let inputValue = value
  let vvvValue = Math.floor(value * vvvMultiplier)
  let savedValue = Math.floor(value * savedMultiplier)
  let inputText = template(inputValue)
  let vvvText = template(vvvValue)
  let savedText = template(savedValue)
  val.innerHTML = inputText
  vvv.innnerHTML = vvvText
  saved.innerHTML = savedText
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM