Jump to content

Подсказка внутри Input на jQuery

Sign in to follow this  
ArtemPitov

1,137 views

Простая реализация подсказки внутри input-a

 

Demo

 

HTML

<div class="inputer">
	<label>Название</label>
	<input type="text" name="">	
</div>

CSS

	.inputer {
		border: 1px solid #ddd;
		position: relative;
		display: block;
		width: 100%;
		max-width: 350px;
		border-radius: 4px;
		overflow: hidden;
	}

	.inputer label {
		position: absolute;
		top: 40%;
		left: 5px;
		opacity: 0;
		transition: all .1s linear;
		visibility: hidden;
		font-size: 12px;
		-webkit-user-select: none;
		   -moz-user-select: none;
		    -ms-user-select: none;
		     -o-user-select: none;
		        user-select: none;
	}

	.inputer label,
	.inputer input::placeholder {
		color: #777;
		font-weight: bold;
		opacity: .8;
	}

	.inputer.active label {
		opacity: 1;
		top: 2px;
		visibility: visible;
		font-size: 11px;
		opacity: .8;
	}

	.inputer.active input {
		padding: 16px 5px 2px !important;
	}

	.inputer input,
	.inputer input:hover,
	.inputer input:focus,
	.inputer input:active {
		box-shadow: none;
		outline: none;
		border:none;
		border-radius: 0; 
		display: block;
		width: 100%;
		padding: 9px 5px;
		font-size: 12px;
	} 

JS

	let inputerFn = (el) => 
	{
		let $el = $(el);
		$el.parent().toggleClass('active', $el.val().length > 0);	
	};

	$('.inputer input').each(function (idx, el) {
		let $el = $(el);
		$el.attr('placeholder', $el.siblings('label').text());
		
		inputerFn(el);
	}).on('input', function () {
		inputerFn(this);		
	});

 

  • +1 2
Sign in to follow this  


2 Comments


Recommended Comments

Guest
You are posting as a guest. If you have an account, please sign in.
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.