下面小編就為大家?guī)硪黄狫S 仿支付寶input文本輸入框放大組件的實例。小編覺得挺不錯的,現(xiàn)在就分享js源碼給大家,也給大家做個參考。對js感興趣的一起跟隨小編過來看看吧
input輸入的時候可以在后邊顯示數(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', // 輸入框目標元素 parentCls : '.parentCls', // 目標元素的父類 align : 'right', // 對齊方式有 ['top','bottom','left','right']四種 默認為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后動態(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); }, /* * 給目標元素定位 * @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; // 實時監(jiān)聽輸入框值的變化 $(_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文本輸入框放大組件的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持PHP中文網(wǎng)。
相關推薦:
以上是JS 仿支付寶input文本輸入框放大組件的實例的詳細內(nèi)容。更多信息請關注PHP中文網(wǎng)其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

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

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

WebSocket與JavaScript:實現(xiàn)實時監(jiān)控系統(tǒng)的關鍵技術引言:隨著互聯(lián)網(wǎng)技術的快速發(fā)展,實時監(jiān)控系統(tǒng)在各個領域中得到了廣泛的應用。而實現(xiàn)實時監(jiān)控的關鍵技術之一就是WebSocket與JavaScript的結(jié)合使用。本文將介紹WebSocket與JavaScript在實時監(jiān)控系統(tǒng)中的應用,并給出代碼示例,詳細解釋其實現(xiàn)原理。一、WebSocket技

如何使用WebSocket和JavaScript實現(xiàn)在線語音識別系統(tǒng)引言:隨著科技的不斷發(fā)展,語音識別技術已經(jīng)成為了人工智能領域的重要組成部分。而基于WebSocket和JavaScript實現(xiàn)的在線語音識別系統(tǒng),具備了低延遲、實時性和跨平臺的特點,成為了一種被廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現(xiàn)在線語音識別系

如何利用JavaScript和WebSocket實現(xiàn)實時在線點餐系統(tǒng)介紹:隨著互聯(lián)網(wǎng)的普及和技術的進步,越來越多的餐廳開始提供在線點餐服務。為了實現(xiàn)實時在線點餐系統(tǒng),我們可以利用JavaScript和WebSocket技術。WebSocket是一種基于TCP協(xié)議的全雙工通信協(xié)議,可以實現(xiàn)客戶端與服務器的實時雙向通信。在實時在線點餐系統(tǒng)中,當用戶選擇菜品并下單

JavaScript和WebSocket:打造高效的實時天氣預報系統(tǒng)引言:如今,天氣預報的準確性對于日常生活以及決策制定具有重要意義。隨著技術的發(fā)展,我們可以通過實時獲取天氣數(shù)據(jù)來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來構建一個高效的實時天氣預報系統(tǒng)。本文將通過具體的代碼示例來展示實現(xiàn)的過程。We

如何使用WebSocket和JavaScript實現(xiàn)在線預約系統(tǒng)在當今數(shù)字化的時代,越來越多的業(yè)務和服務都需要提供在線預約功能。而實現(xiàn)一個高效、實時的在線預約系統(tǒng)是至關重要的。本文將介紹如何使用WebSocket和JavaScript來實現(xiàn)一個在線預約系統(tǒng),并提供具體的代碼示例。一、什么是WebSocketWebSocket是一種在單個TCP連接上進行全雙工

JavaScript教程:如何獲取HTTP狀態(tài)碼,需要具體代碼示例前言:在Web開發(fā)中,經(jīng)常會涉及到與服務器進行數(shù)據(jù)交互的場景。在與服務器進行通信時,我們經(jīng)常需要獲取返回的HTTP狀態(tài)碼來判斷操作是否成功,根據(jù)不同的狀態(tài)碼來進行相應的處理。本篇文章將教你如何使用JavaScript獲取HTTP狀態(tài)碼,并提供一些實用的代碼示例。使用XMLHttpRequest

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

JavaScript中的HTTP狀態(tài)碼獲取方法簡介:在進行前端開發(fā)中,我們常常需要處理與后端接口的交互,而HTTP狀態(tài)碼就是其中非常重要的一部分。了解和獲取HTTP狀態(tài)碼有助于我們更好地處理接口返回的數(shù)據(jù)。本文將介紹使用JavaScript獲取HTTP狀態(tài)碼的方法,并提供具體代碼示例。一、什么是HTTP狀態(tài)碼HTTP狀態(tài)碼是指當瀏覽器向服務器發(fā)起請求時,服務
