下面小編就為大家?guī)?lái)一篇JS 仿支付寶input文字輸入框放大組件的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享js原始碼給大家,也給大家做個(gè)參考。對(duì)js有興趣的一起跟隨小編過(guò)來(lái)看看吧
input輸入的時(shí)候可以在後邊顯示數(shù)字放大鏡
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS 仿支付寶input文本輸入框放大組件</title> <script src="js/jquery.min.js"></script> <style> * { margin: 0; padding: 0; border-width: 1px; } .parentCls {margin:5px 60px 0;} .js-max-input {border: solid 1px #ffd2b2; position:relative;background: #fffae5;padding: 0 10px 0 10px;font-size:20px;color: #ff4400} .inputElem4{ width: 300px; height: 36px; border: 1px solid #E0E0E0; padding-left: 10px; line-height: 36px; font-size: 14px; } </style> </head> <body> <p class="parentCls"> <input type="text" class="inputElem4" autocomplete = "off" maxlength="18"/> </p> <script src="js/jq22.js"></script> <script> // 初始化 $(function(){ new TextMagnifier({ inputElem: '.inputElem4', align: 'bottom', splitType: [6,4,4,4] }); }); </script> </body> </html>
/** * JS 仿支付寶的文本輸入框放大組件 */ function TextMagnifier(options) { this.config = { inputElem : '.inputElem', // 輸入框目標(biāo)元素 parentCls : '.parentCls', // 目標(biāo)元素的父類 align : 'right', // 對(duì)齊方式有 ['top','bottom','left','right']四種 默認(rèn)為top splitType : [3,4,4], // 拆分規(guī)則 delimiter : '-' // 分隔符可自定義 }; this.cache = { isFlag : false }; this.init(options); } TextMagnifier.prototype = { constructor: TextMagnifier, init: function(options) { this.config = $.extend(this.config,options || {}); var self = this, _config = self.config, _cache = self.cache; self._bindEnv(); }, /* * 在body后動(dòng)態(tài)添加HTML內(nèi)容 * @method _appendHTML */ _appendHTML: function($this,value) { var self = this, _config = self.config, _cache = self.cache; var html = '', $parent = $($this).closest(_config.parentCls); if($('.js-max-input',$parent).length == 0) { html += '<p class="js-max-input"></p>'; $($parent).append(html); } var value = self._formatStr(value); $('.js-max-input',$parent).html(value); }, /* * 給目標(biāo)元素定位 * @method _position * @param target */ _position: function(target){ var self = this, _config = self.config; var elemWidth = $(target).outerWidth(), elemHeight = $(target).outerHeight(), elemParent = $(target).closest(_config.parentCls), containerHeight = $('.js-max-input',elemParent).outerHeight(); $(elemParent).css({"position":'relative'}); switch(true){ case _config.align == 'top': $('.js-max-input',elemParent).css({'position':'absolute','top' :-elemHeight - containerHeight/2,'left':0}); break; case _config.align == 'left': $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':0}); break; case _config.align == 'bottom': $('.js-max-input',elemParent).css({'position':'absolute','top' :elemHeight + 4 + 'px','left':0}); break; case _config.align == 'right': $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':elemWidth + 2 + 'px'}); break; } }, /** * 綁定事件 * @method _bindEnv */ _bindEnv: function(){ var self = this, _config = self.config, _cache = self.cache; // 實(shí)時(shí)監(jiān)聽(tīng)輸入框值的變化 $(_config.inputElem).each(function(index,item){ $(item).keyup(function(e){ var value = $.trim(e.target.value), parent = $(this).closest(_config.parentCls); if(value == '') { self._hide(parent); }else { var html = $.trim($('.js-max-input',parent).html()); if(html != '') { self._show(parent); } } self._appendHTML($(this),value); self._position($(this)); }); $(item).unbind('focusin'); $(item).bind('focusin',function(){ var parent = $(this).closest(_config.parentCls), html = $.trim($('.js-max-input',parent).html()); if(html != '') { self._show(parent); } }); $(item).unbind('focusout'); $(item).bind('focusout',function(){ var parent = $(this).closest(_config.parentCls); self._hide(parent); }); }); }, /** * 格式化下 * @method _formatStr */ _formatStr: function(str){ var self = this, _config = self.config, _cache = self.cache; var count = 0, output = []; for(var i = 0, ilen = _config.splitType.length; i < ilen; i++){ var s = str.substr(count,_config.splitType[i]); if(s.length > 0){ output.push(s); } count+= _config.splitType[i]; } return output.join(_config.delimiter); }, /* * 顯示 放大容器 * @method _show */ _show: function(parent) { var self = this, _config = self.config, _cache = self.cache; if(!_cache.isFlag) { $('.js-max-input',parent).show(); _cache.isFlag = true; } }, /* * 隱藏 放大容器 * @method hide * {public} */ _hide: function(parent) { var self = this, _config = self.config, _cache = self.cache; if(_cache.isFlag) { $('.js-max-input',parent).hide(); _cache.isFlag = false; } } };
#效果圖
以上這篇JS 仿支付寶input文字輸入框放大組件的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持PHP中文網(wǎng)。
相關(guān)推薦:
jquery點(diǎn)擊文字內(nèi)容放大並居中實(shí)例分享
以上是JS 仿支付寶input文字輸入框放大組件的實(shí)例的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

WebSocket與JavaScript:實(shí)現(xiàn)即時(shí)監(jiān)控系統(tǒng)的關(guān)鍵技術(shù)引言:隨著互聯(lián)網(wǎng)技術(shù)的快速發(fā)展,即時(shí)監(jiān)控系統(tǒng)在各個(gè)領(lǐng)域中得到了廣泛的應(yīng)用。而實(shí)現(xiàn)即時(shí)監(jiān)控的關(guān)鍵技術(shù)之一就是WebSocket與JavaScript的結(jié)合使用。本文將介紹WebSocket與JavaScript在即時(shí)監(jiān)控系統(tǒng)中的應(yīng)用,並給出程式碼範(fàn)例,詳細(xì)解釋其實(shí)作原理。一、WebSocket技

如何使用WebSocket和JavaScript實(shí)現(xiàn)線上語(yǔ)音辨識(shí)系統(tǒng)引言:隨著科技的不斷發(fā)展,語(yǔ)音辨識(shí)技術(shù)已成為了人工智慧領(lǐng)域的重要組成部分。而基於WebSocket和JavaScript實(shí)現(xiàn)的線上語(yǔ)音辨識(shí)系統(tǒng),具備了低延遲、即時(shí)性和跨平臺(tái)的特點(diǎn),成為了廣泛應(yīng)用的解決方案。本文將介紹如何使用WebSocket和JavaScript來(lái)實(shí)現(xiàn)線上語(yǔ)音辨識(shí)系

如何利用JavaScript和WebSocket實(shí)現(xiàn)即時(shí)線上點(diǎn)餐系統(tǒng)介紹:隨著網(wǎng)路的普及和技術(shù)的進(jìn)步,越來(lái)越多的餐廳開(kāi)始提供線上點(diǎn)餐服務(wù)。為了實(shí)現(xiàn)即時(shí)線上點(diǎn)餐系統(tǒng),我們可以利用JavaScript和WebSocket技術(shù)。 WebSocket是一種基於TCP協(xié)定的全雙工通訊協(xié)議,可實(shí)現(xiàn)客戶端與伺服器的即時(shí)雙向通訊。在即時(shí)線上點(diǎn)餐系統(tǒng)中,當(dāng)使用者選擇菜餚並下訂單

JavaScript和WebSocket:打造高效的即時(shí)天氣預(yù)報(bào)系統(tǒng)引言:如今,天氣預(yù)報(bào)的準(zhǔn)確性對(duì)於日常生活以及決策制定具有重要意義。隨著技術(shù)的發(fā)展,我們可以透過(guò)即時(shí)獲取天氣數(shù)據(jù)來(lái)提供更準(zhǔn)確可靠的天氣預(yù)報(bào)。在本文中,我們將學(xué)習(xí)如何使用JavaScript和WebSocket技術(shù),來(lái)建立一個(gè)高效的即時(shí)天氣預(yù)報(bào)系統(tǒng)。本文將透過(guò)具體的程式碼範(fàn)例來(lái)展示實(shí)現(xiàn)的過(guò)程。 We

如何使用WebSocket和JavaScript實(shí)現(xiàn)線上預(yù)約系統(tǒng)在當(dāng)今數(shù)位化的時(shí)代,越來(lái)越多的業(yè)務(wù)和服務(wù)都需要提供線上預(yù)約功能。而實(shí)現(xiàn)一個(gè)高效、即時(shí)的線上預(yù)約系統(tǒng)是至關(guān)重要的。本文將介紹如何使用WebSocket和JavaScript來(lái)實(shí)作一個(gè)線上預(yù)約系統(tǒng),並提供具體的程式碼範(fàn)例。一、什麼是WebSocketWebSocket是一種在單一TCP連線上進(jìn)行全雙工

JavaScript教學(xué):如何取得HTTP狀態(tài)碼,需要具體程式碼範(fàn)例前言:在Web開(kāi)發(fā)中,經(jīng)常會(huì)涉及到與伺服器進(jìn)行資料互動(dòng)的場(chǎng)景。在與伺服器進(jìn)行通訊時(shí),我們經(jīng)常需要取得傳回的HTTP狀態(tài)碼來(lái)判斷操作是否成功,並根據(jù)不同的狀態(tài)碼來(lái)進(jìn)行對(duì)應(yīng)的處理。本篇文章將教你如何使用JavaScript來(lái)取得HTTP狀態(tài)碼,並提供一些實(shí)用的程式碼範(fàn)例。使用XMLHttpRequest

用法:在JavaScript中,insertBefore()方法用於在DOM樹(shù)中插入一個(gè)新的節(jié)點(diǎn)。這個(gè)方法需要兩個(gè)參數(shù):要插入的新節(jié)點(diǎn)和參考節(jié)點(diǎn)(即新節(jié)點(diǎn)將要插入的位置的節(jié)點(diǎn))。

JavaScript中的HTTP狀態(tài)碼取得方法簡(jiǎn)介:在進(jìn)行前端開(kāi)發(fā)中,我們常常需要處理與後端介面的交互,而HTTP狀態(tài)碼就是其中非常重要的一部分。了解並取得HTTP狀態(tài)碼有助於我們更好地處理介面?zhèn)骰氐馁Y料。本文將介紹使用JavaScript取得HTTP狀態(tài)碼的方法,並提供具體程式碼範(fàn)例。一、什麼是HTTP狀態(tài)碼HTTP狀態(tài)碼是指當(dāng)瀏覽器向伺服器發(fā)起請(qǐng)求時(shí),服務(wù)
