JS+CSS案例:实时获取人民币与美元汇率+CNY与USD交换+单位转换成万元和亿元 ...

打印 上一主题 下一主题

主题 1035|帖子 1035|积分 3105

本例图片


功能先容



  • 自动获取当前汇率,输入美元(USD)输出 人民币(CNY),点击文本框后的货币单位,可以交换兑换比率。
  • 自动处理数字输入:文本框答应直接粘帖科学计数法情势的数字(自动过滤空格,逗号及其他字符),如:2,923,706,026 或 2 923 706 026 这样的数字。
  • 对转换后的以元为单位的数值举行单位换算,可以换算为万元和亿元为单位的两种数值。
需求发生:

现在正在做的一个电影网站,需要在页面前端把电影按票房金额排序。从外洋网站得到的电影全球票房数据通常如下:

国内的网站得到的票房数据如下:

我需要将两个网站得到的数值单位同一转换为人民币,并得到以万元为单位的数值。这样才能在网站后端输入数值后,实现前端页面的排序。于是我就在网站后台增长了这样一个汇率计算器。
现在我将这个功能提出来,单独做个页面,分享给需要的人。
HTML代码

  1. <body>
  2. <div class="box">
  3.         <fieldset>
  4.                <h2>CNY-USD 汇率计算器</h2>
  5.                <div class="rate">
  6.                 <em>当前汇率:</em><span  id="rate"></span>
  7.             </div>
  8.             <div class="currency">                 
  9.                 <input type="text" id="amountone" placeholder="0.00" value=""  oninput="convertToNumber(this);changeit();" />
  10.                 <select id="currency-one">                    
  11.                     <option value="USD" selected>USD</option>
  12.                     <option value="CNY" >CNY</option>
  13.                 </select>
  14.             </div>
  15.             <div class="currency">                 
  16.                 <input type="text" id="amounttwo" placeholder="0.00"  readonly />
  17.                 <select id="currency-two">                    
  18.                     <option value="CNY" selected>CNY</option>
  19.                     <option value="USD" >USD</option>                   
  20.                 </select>
  21.             </div>
  22.             <div class="currency">            
  23.                     <!--<button id="swap" onclick="changeit()">换算</button>-->
  24.                 <input type="text" id="spaResult" placeholder="0.00" readonly><span id="unit">单位</span>
  25.             </div>
  26.             <div>
  27.                 <button type="button" id="btn2" onclick="change()" value="亿元" />换算:亿元</button>       
  28.                 <button type="button" id="btn1" onclick="change()" value="万元" />换算:万元</button>                                                   
  29.             </div>        
  30.         </fieldset>
  31. </div>
  32. </body>
复制代码
CSS样式
  1. <style type="text/css">
  2.         /* Reset CSS  清除默认样式和浮动 */
  3. *{
  4.         margin:0;
  5.         padding: 0;
  6.         box-sizing: border-box;
  7. }
  8. html {
  9.         margin: 0px !important;
  10.         height: 100%;
  11. }
  12. body{
  13.         height: 100%;
  14.         font-size: 1.2rem;
  15. }
  16. body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,figure,fieldset,input,textarea,p,blockquote,th,td {
  17.         margin: 0;
  18.         padding: 0;
  19. }
  20. input {
  21.         border: none;
  22.         outline: none;
  23. }
  24. /* General Definitions  定义全局样式 */
  25. :root {
  26.     --bg-color:#999;
  27.     --primary-color: #139ce4;
  28.     --color:#fff;
  29.     --second-color:#999999;
  30. }
  31. body {
  32.         background: var(--bg-color);
  33.         font-family: "微软雅黑",Helvetica,weiruanyahei,Tahoma,Arial,Sans-serif;
  34.         color: #333;
  35. }
  36. /* ............................         Heading Define 定义<H>标签属性 */
  37. input[type=text]{
  38.     color: var(--primary-color);
  39.         border:none;
  40.         background: none;       
  41.         border-bottom: 2px solid var(--primary-color);
  42.         width: 75%;
  43.         padding: 5px;
  44.         text-align: right;
  45.         padding-right: 10px;
  46.         margin-right: 20px;
  47.     font-size: 1.8rem;
  48. }
  49. input::placeholder {
  50.   color: var(--primary-color); /* 你想要的颜色代码 */
  51. }
  52. h2{margin-bottom: 15px}
  53. fieldset{
  54.     border: 1px solid #d3d3d3;
  55.     padding: 20px;
  56.     margin: 10px;
  57.     -moz-border-radius:  10px;  
  58.     -webkit-border-radius: 10px;  
  59.     border-radius:  10px;   /* future proofing */  
  60.     -khtml-border-radius: 10px;  /* for old Konqueror browsers */   
  61.     padding: 40px;
  62.     background: #fff;
  63.     box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.3);
  64. }
  65. legend{
  66.         font-size: 1rem;
  67.         padding:0px 20px;
  68. }
  69.    
  70. button{
  71.   position: relative;   
  72.   padding: 1rem 3rem; /* 用 padding 撑起按钮的宽度和高度 ,并确保了按钮文字水平方向居中 */
  73.   font-family: "微软雅黑", sans-serif;
  74.   font-size: 1.5rem;  
  75.   line-height: 1.5rem; /* 行高和字号大小相等,可以实现按钮文字在按钮内垂直居中 */
  76.   font-weight:700;
  77.   color: var(--color);  /* 文字颜色为预定义的前景色 */
  78.   cursor: pointer;   /* 鼠标移动到按钮上时候的形状:手型 */
  79.   user-select: none;  /* 让用户不能选择按钮上的文字 */
  80.   white-space: nowrap; /* 避免英文单词间的空格导致文字换行 */
  81.   border-radius: 0.5rem;
  82.   text-decoration: none;
  83.   text-transform:uppercase; /* 字母自动修正为大写 */
  84.   transition: all .1s; /* 按钮响应动画效果的持续时间 */
  85.   margin: 1.5rem 0rem 1.5rem 2rem;
  86.   background-color:var(--primary-color);
  87.   border:none;
  88.   float: right;
  89. }
  90. button:hover{
  91.         background-color:var(--second-color);
  92. }
  93. button:active{
  94.     text-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
  95.     box-shadow: inset 5px 5px 5px 0 rgba(0, 0, 0, .3);
  96. }
  97. .box{
  98.   /* 居中 */
  99.   position: fixed;  
  100.   left: 50%;
  101.   top: 50%;
  102.   transform: translate(-50%, -50%);
  103.   width:50%;
  104. }
  105. .currency{
  106.         width: 100%;
  107.         float: left;
  108.     margin-bottom: 10px;
  109.     text-align: right;
  110. }
  111. .currency select {
  112.     padding: 10px;
  113.     -moz-appearance: none;
  114.     -webkit-appearance: none;
  115.     appearance: none;   
  116.     font-size: 16px;
  117.     background: #fff;
  118.     cursor: pointer;
  119. }
  120. .currency option{
  121.     font-size: 16px;
  122.     display: block;
  123.         border: none;
  124.     padding: 10px;
  125. }
  126. .rate {
  127.     color: var(--primary-color);
  128.     font-size: 14px;
  129.     padding: 0 10px;
  130.     text-align: right;
  131.     margin-bottom: 10px;
  132. }
  133. select:focus,
  134. input:focus,
  135. button:focus {
  136.     outline: 0;
  137. }
  138. @media (max-width: 600px) {
  139.     .currency input {
  140.     width: 200px;
  141. }
  142. }
  143. #amounttwo,
  144. #spaResult{
  145.     border-color:#999;
  146.     color: #999;
  147.     font-size: 1.2rem;
  148. }
  149. #amounttwo::placeholder,
  150. #spaResult::placeholder {
  151.     color: #999;
  152. }
  153. #unit{
  154.     width: 60px;
  155.     height: 36px;
  156.     text-align: center;
  157.     display: inline-block;
  158. }
  159. </style>
复制代码
JS代码
  1. <script type="text/javascript">
  2. // 当输入框点击时,清除转换后的数值
  3. document.getElementById('amountone').addEventListener('click', function() {
  4.     document.getElementById('spaResult').value = '0.00';
  5.     //document.getElementById('amounttwo').value = '0.00';
  6. });
  7. // 转换货币单位
  8. function change() {
  9.         const clickedButtonId = event.target.id;
  10.         //alert(clickedButtonId);
  11.     var strInput = document.all.amounttwo.value;  
  12.     if (clickedButtonId =='btn1'){
  13.             change_unit = 10000;
  14.             document.getElementById("unit").innerHTML='万元';
  15.     } else {
  16.             change_unit = 100000000;
  17.             document.getElementById("unit").innerHTML='亿元';
  18.     }
  19.     if (strInput != "")
  20.     {
  21.         var numInput = parseFloat(strInput);
  22.         var numOutput = (numInput/change_unit).toFixed(2)
  23.         document.all.spaResult.value = numOutput;
  24.     }
  25. };
  26. // 文本框允许直接粘帖科学计数法形式的数字(自动过滤空格,逗号及其他字符)
  27. function convertToNumber(inputElement) {
  28.     var value = inputElement.value;
  29.     var numericValue = value.replace(/[^0-9]/g, '');
  30.     var number = Number(numericValue);
  31.     if (!isNaN(number)) {
  32.         inputElement.value = number;
  33.     }
  34. }
  35. // 汇率计算
  36. // 获取货币1对象
  37. let currencyEl_one = document.getElementById("currency-one");
  38. // 获取货币2对象
  39. let currencyEl_two = document.getElementById("currency-two");
  40. // 获取货币1对应的汇率对象
  41. let amountEl_one = document.getElementById("amountone");
  42. // 获取货币2对应的汇率对象
  43. let amountEl_two = document.getElementById("amounttwo");
  44. // 获取提示框对应的对象
  45. let rateEl = document.getElementById("rate");
  46. // 检查初始状态下 是否需要换算汇率
  47. getData();
  48. // 货币2发生变化时 触发的事件
  49. currencyEl_two.onchange = getData;
  50. // 货币1发生变化时 触发的事件
  51. currencyEl_one.onchange = getData;
  52. // 获取按钮 货币1和货币2是否都有值
  53. let btn = document.getElementById("swap");
  54. // 点击交换按钮时 触发的事件
  55. function changeit() {
  56.   // console.log("!!!");
  57.   let currency_one = currencyEl_one.value;
  58.   let currency_two = currencyEl_two.value;
  59.   fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
  60.     .then(res => res.json())
  61.     .then(data => {
  62.       const rate = data.rates[currency_two];
  63.       rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`;
  64.       amountEl_two.value = (amountEl_one.value * rate).toFixed(2);
  65.     });
  66. };
  67. // 得到汇率
  68. function getData() {
  69.     let currency_one = currencyEl_one.value;
  70.     let currency_two = currencyEl_two.value;
  71.     fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
  72.       .then(res => res.json())
  73.       .then(data => {
  74.         const rate = data.rates[currency_two];
  75.         rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`;
  76.         amountEl_two.value = (amountEl_one.value * rate).toFixed(2);
  77.       });
  78. }
  79. // 汇率计算结束
  80. </script>
复制代码
大功告成

我个人是从计划师过渡到前端然后慢慢接触网站开发的,水平有限,所有代码仅供参考。
路过的朋侪,资助点个免费的赞吧!谢谢啦!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表