JS实现仿微信支付弹窗功能

前端之家收集整理的这篇文章主要介绍了JS实现仿微信支付弹窗功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

先奉上效果

HTML代码

仿手机微信支付输入密码界面<a href="/tag/xiaoguo/" target="_blank" class="keywords">效果</a>

css

<div class="jb51code">
<pre class="brush:js;">
body {
margin: 0;
padding: 0;
font-size: 0.3rem;
font-family: "微软雅黑",arial;
}
ul,li {
margin: 0;
padding: 0;
list-style: none;
}
img {
display: block;
}

myBtn {

  1. display: block;
  2. width: 80%;
  3. height: auto;
  4. margin: 5rem auto;
  5. padding: 0.2rem;
  6. border-radius: 5px;
  7. border: 0;
  8. outline: none;
  9. font-family: "微软雅黑";
  10. color: #fff;
  11. background-color: #5CB85C;
  12. }
  13. /* 弹窗 */
  14. .modal {
  15. display: none;
  16. /* 默认隐藏 */
  17. position: fixed;
  18. z-index: 1;
  19. left: 0;
  20. top: 0;
  21. width: 100%;
  22. height: 100%;
  23. overflow: auto;
  24. background-color: rgb(0,0);
  25. background-color: rgba(0,0.4);
  26. -webkit-animation-name: fadeIn;
  27. -webkit-animation-duration: 0.4s;
  28. animation-name: fadeIn;
  29. animation-duration: 0.4s
  30. }
  31. /* 弹窗<a href="/tag/neirong/" target="_blank" class="keywords">内容</a> */
  32. .modal-content {
  33. position: fixed;
  34. bottom: 0;
  35. /*background-color: #fefefe;*/
  36. width: 100%;
  37. -webkit-animation-name: slideIn;
  38. -webkit-animation-duration: 0.4s;
  39. animation-name: slideIn;
  40. animation-duration: 0.4s
  41. }
  42. /**
  43. * 支付弹窗样式
  44. * **/
  45. .paymentArea-Entry {
  46. width: 90%;
  47. margin: 0 auto;
  48. padding-bottom: 0.3rem;
  49. background-color: #fff;
  50. }
  51. .paymentArea-Entry-Head {
  52. display: flex;
  53. display: -webkit-flex;
  54. height: 0.8rem;
  55. line-height: 0.8rem;
  56. padding: 0.2rem;
  57. border-bottom: 1px solid #5CB85C;
  58. }
  59. /* <a href="/tag/guanbi/" target="_blank" class="keywords">关闭</a>按钮 */
  60. .paymentArea-Entry-Head .close {
  61. width: 0.5rem;
  62. height: 0.5rem;
  63. padding: 0.15rem 0.15rem 0.15rem 0;
  64. }
  65. .paymentArea-Entry-Head .close:hover,.paymentArea-Entry-Head .close:focus {
  66. color: #000;
  67. text-decoration: none;
  68. cursor: pointer;
  69. }
  70. .paymentArea-Entry-Head .useImg {
  71. width: 0.8rem;
  72. height: 0.8rem;
  73. margin-right: 0.15rem;
  74. }
  75. .paymentArea-Entry-Head .tips-txt {
  76. font-size: 0.4rem;
  77. }
  78. .paymentArea-Entry-Content {
  79. position: relative;
  80. padding: 0.2rem 0;
  81. text-align: center;
  82. }
  83. .paymentArea-Entry-Content:after {
  84. content: "";
  85. position: absolute;
  86. bottom: 0;
  87. left: 0.3rem;
  88. right: 0.3rem;
  89. height: 1px;
  90. background-color: #ddd;
  91. }
  92. .paymentArea-Entry-Content .pay-name {
  93. font-size: 0.3rem;
  94. }
  95. .paymentArea-Entry-Content .pay-price {
  96. font-size: 0.4rem;
  97. font-weight: bold;
  98. }
  99. ul.paymentArea-Entry-Row {
  100. display: flex;
  101. display: -webkit-flex;
  102. justify-content: space-between;
  103. margin: 0.2rem 0.3rem 0 0.3rem;
  104. padding: 0;
  105. border: 1px solid #a2a2a2;
  106. }
  107. ul.paymentArea-Entry-Row li {
  108. position: relative;
  109. flex-grow: 1;
  110. min-width: 1rem;
  111. height: 0.8rem;
  112. line-height: 0.8rem;
  113. text-align: center;
  114. border-right: 1px solid #ddd;
  115. }
  116. ul.paymentArea-Entry-Row li:last-child {
  117. border-right: 0;
  118. }
  119. ul.paymentArea-Entry-Row li img {
  120. position: absolute;
  121. top: 50%;
  122. left: 50%;
  123. width: 0.5rem;
  124. height: 0.5rem;
  125. margin: -0.25rem 0 0 -0.25rem;
  126. }
  127. .paymentArea-Keyboard {
  128. margin-top: 1.2rem;
  129. background-color: #fff;
  130. }
  131. .paymentArea-Keyboard h4 {
  132. height: 0.5rem;
  133. line-height: 0.5rem;
  134. margin: 0;
  135. text-align: center;
  136. }
  137. .paymentArea-Keyboard h4 img {
  138. width: 0.93rem;
  139. height: 0.32rem;
  140. margin: 0 auto;
  141. }
  142. .paymentArea-Keyboard h4:active {
  143. background-color: #e3e3e3;
  144. }
  145. .paymentArea-Keyboard ul {
  146. border-top: 1px solid #ddd;
  147. }
  148. .paymentArea-Keyboard li {
  149. display: flex;
  150. display: -webkit-flex;
  151. justify-content: space-between;
  152. border-bottom: 1px solid #ddd;
  153. }
  154. .paymentArea-Keyboard li a {
  155. flex-grow: 1;
  156. display: block;
  157. min-width: 1rem;
  158. line-height: 1rem;
  159. border-right: 1px solid #ddd;
  160. font-size: 0.3rem;
  161. text-align: center;
  162. text-decoration: none;
  163. color: #000;
  164. }
  165. .paymentArea-Keyboard li:last-child,.paymentArea-Keyboard li a:last-child {
  166. border: 0;
  167. }
  168. .paymentArea-Keyboard li a:active {
  169. outline: none;
  170. background-color: #ddd;
  171. }
  172. /* <a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>动画 */
  173. @-webkit-keyframes slideIn {
  174. from {
  175. bottom: -300px;
  176. opacity: 0
  177. }
  178. to {
  179. bottom: 0;
  180. opacity: 1
  181. }
  182. }
  183. @keyframes slideIn {
  184. from {
  185. bottom: -300px;
  186. opacity: 0
  187. }
  188. to {
  189. bottom: 0;
  190. opacity: 1
  191. }
  192. }
  193. @-webkit-keyframes fadeIn {
  194. from {
  195. opacity: 0
  196. }
  197. to {
  198. opacity: 1
  199. }
  200. }
  201. @keyframes fadeIn {
  202. from {
  203. opacity: 0
  204. }
  205. to {
  206. opacity: 1
  207. }
  208. } </pre>

js

<div class="jb51code">
<pre class="brush:js;">
//定义根目录字体大小,通过rem实现适配
document.addEventListener("DOMContentLoaded",function() {
var html = document.documentElement;
var windowWidth = html.clientWidth;
//console.log(windowWidth)
html.style.fontSize = windowWidth / 7.5 + "px";
},false);
// 获取弹窗
var modal = document.getElementById('myModal');
// 打开弹窗的按钮对象
var btn = document.getElementById("myBtn");
// 获取 元素,用于关闭弹窗 that closes the modal
var span = document.getElementsByClassName("close")[0];
/创建密码输入框/
var entryArea = document.querySelector(".paymentArea-Entry-Row");
for(var i = 0; i < 6; i++) {
var li = document.createElement("li");
entryArea.appendChild(li);
}
/键盘操作/
var doms = document.querySelectorAll('ul li a');
var entryLis = document.querySelectorAll(".paymentArea-Entry-Row li");
var pwds = ""; //存储密码
var count = 0; //记录点击次数
for(var i = 0; i < doms.length; i++) {
! function(dom,index) {
dom.addEventListener('click',function() {
var txt = this.innerHTML;
switch(txt) {
case "清空":
if(count != 0) {
for(var i = 0; i < entryLis.length; i++) {
entryLis[i].innerHTML = "";
}
pwds = "";
count = 0;
console.log(pwds)
console.log(count)
}
break;
case "删除":
if(count != 0) {
console.log(pwds)
entryLis[count - 1].innerHTML = "";
pwds = pwds.substring(0,pwds.length - 1);
count--;
console.log(pwds)
console.log(count)
}
break;
default:
/通过判断点击次数 向输入框填充内容/
if(count < 6) {
/创建一个图片对象 插入到方框中/
var img = new Image();
img.src = "images/dd_03.jpg";
entryLis[count].appendChild(img);
/为存储密码的对象赋值/
if(pwds == "") {
pwds = txt;
} else {
pwds += txt;
}
count++;
if(pwds.length == 6) {
location.href = "https://www.baidu.com";
}
} else {
return;
alert("超出限制")
}
}
});
}(doms[i],i);
}
// 点击按钮打开弹窗
btn.onclick = function() {
modal.style.display = "block";
}
// 点击 (x),关闭弹窗
span.onclick = function() {
modal.style.display = "none";
if(count != 0) {
for(var i = 0; i < entryLis.length; i++) {
entryLis[i].innerHTML = "";
}
pwds = "";
count = 0;
}
}
// 在用户点击其他地方时,关闭弹窗
window.onclick = function(event) {
if(event.target == modal) {
modal.style.display = "none";
if(count != 0) {
for(var i = 0; i < entryLis.length; i++) {
entryLis[i].innerHTML = "";
}
pwds = "";
count = 0;
}
}
}
/开关键盘/
var openKey = document.querySelector(".paymentArea-Entry-Row");
var switchOfKey = document.querySelector(".paymentArea-Keyboard h4");
switchOfKey.addEventListener('click',function() {
var KeyboardH = document.querySelector(".paymentArea-Keyboard").clientHeight;
document.querySelector(".paymentArea-Keyboard").style.height = KeyboardH + "px";
document.querySelector(".paymentArea-Keyboard").style.backgroundColor = "transparent";
document.querySelector(".paymentArea-Keyboard h4").style.display = "none";
document.querySelector(".paymentArea-Keyboard ul").style.display = "none";
})
openKey.addEventListener('click',function() {
document.querySelector(".paymentArea-Keyboard").style.backgroundColor = "#fff";
document.querySelector(".paymentArea-Keyboard h4").style.display = "block";
document.querySelector(".paymentArea-Keyboard ul").style.display = "block";
})

总结

以上所述是小编给大家介绍的JS实现仿微信支付弹窗功能。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

猜你在找的JavaScript相关文章