Javascript 如何在输入占位符内制作键入转盘?
我希望我的输入占位符有一个打字转盘,类似于 我想在我的Javascript 如何在输入占位符内制作键入转盘?,javascript,jquery,css,placeholder,Javascript,Jquery,Css,Placeholder,我希望我的输入占位符有一个打字转盘,类似于 我想在我的#myInput占位符上显示 <div class="form-group"> <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"> <center><label class="heypal-label"><span id="heypal">HeyPal<span class="gra
#myInput
占位符上显示
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
海帕尔,我/
走!
有什么办法可以做到这一点吗?谢谢你链接到的示例使用CSS样式来模拟键入插入符号,所以你不能完全做到这一点,但是你可以使用完全相同的javascript,除了修改span的内部html,而是修改输入的
占位符
属性
let input = document.getElementById("myInput");
input.placeholder = 'my text';
显然,上面静态地设置占位符,但是没有理由不能使用所提供示例用于旋转占位符文本的相同setTimeout
tick策略
至于插入符号,我只使用“管道”符号:
。它并不完美,但已经足够接近了。您链接到的示例使用CSS样式来模拟键入插入符号,因此您不能完全做到这一点,但除此之外,您可以使用完全相同的javascript,只不过不是修改span的内部html,而是修改输入的占位符
属性
let input = document.getElementById("myInput");
input.placeholder = 'my text';
显然,上面静态地设置占位符,但是没有理由不能使用所提供示例用于旋转占位符文本的相同setTimeout
tick策略
至于插入符号,我只使用“管道”符号:
。虽然它并不完美,但它已经足够接近了。你只需要用你的输入元素和文本创建一个新的TxtRotate
对象
new TxtRotate(element, textArray, waitTimeMs);
然后,要获取占位符中的文本,只需在元素上设置占位符文本
this.el.placeholder = this.txt;
var TxtRotate=函数(el、旋转、周期){
this.toRotate=toRotate;
this.el=el;
this.loopNum=0;
this.period=parseInt(period,10)| 2000;
this.txt=“”;
这个。勾选();
this.isDeleting=false;
};
TxtRotate.prototype.tick=函数(){
var i=this.loopNum%this.toRotate.length;
var fullTxt=this.toRotate[i];
如果(这是删除){
this.txt=fullTxt.substring(0,this.txt.length-1);
}否则{
this.txt=fullTxt.substring(0,this.txt.length+1);
}
this.el.placeholder=this.txt;
var=这个;
var delta=300-Math.random()*100;
如果(这是删除){
delta/=2;
}
如果(!this.isDeleting&&this.txt===fullTxt){
delta=这个周期;
this.isDeleting=true;
}else if(this.isDeleting&&this.txt==''){
this.isDeleting=false;
这个.loopNum++;
δ=500;
}
setTimeout(函数(){
那。勾选();
},三角洲);
};
window.onload=函数(){
新的TXTROATE(document.getElementById('myInput'),[“PariMaria”、“Gentianas”、“LinneaSteliana”、“UlisesCristobal”、“Simonideslony”],2000年);
};代码>
海帕尔,我/
走!
您实际上只需要使用输入元素和文本创建一个新的TxtRotate
对象
new TxtRotate(element, textArray, waitTimeMs);
然后,要获取占位符中的文本,只需在元素上设置占位符文本
this.el.placeholder = this.txt;
var TxtRotate=函数(el、旋转、周期){
this.toRotate=toRotate;
this.el=el;
this.loopNum=0;
this.period=parseInt(period,10)| 2000;
this.txt=“”;
这个。勾选();
this.isDeleting=false;
};
TxtRotate.prototype.tick=函数(){
var i=this.loopNum%this.toRotate.length;
var fullTxt=this.toRotate[i];
如果(这是删除){
this.txt=fullTxt.substring(0,this.txt.length-1);
}否则{
this.txt=fullTxt.substring(0,this.txt.length+1);
}
this.el.placeholder=this.txt;
var=这个;
var delta=300-Math.random()*100;
如果(这是删除){
delta/=2;
}
如果(!this.isDeleting&&this.txt===fullTxt){
delta=这个周期;
this.isDeleting=true;
}else if(this.isDeleting&&this.txt==''){
this.isDeleting=false;
这个.loopNum++;
δ=500;
}
setTimeout(函数(){
那。勾选();
},三角洲);
};
window.onload=函数(){
新的TXTROATE(document.getElementById('myInput'),[“PariMaria”、“Gentianas”、“LinneaSteliana”、“UlisesCristobal”、“Simonideslony”],2000年);
};代码>
海帕尔,我/
走!
您可以尝试此版本
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.value =this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var element = document.getElementById('myInput');
var toRotate = element.getAttribute('data-rotate');
var period = element.getAttribute('data-period');
new TxtRotate(element, JSON.parse(toRotate), period);
}
</script>
</head>
<body>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input"
data-period="2000"
data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'>
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
</body>
</html>
在此处插入标题
var TxtRotate=函数(el、旋转、周期){
this.toRotate=toRotate;
this.el=el;
this.loopNum=0;
this.period=parseInt(period,10)| 2000;
this.txt=“”;
这个。勾选();
this.isDeleting=false;
};
TxtRotate.prototype.tick=函数(){
var i=this.loopNum%this.toRotate.length;
var fullTxt=this.toRotate[i];
如果(这是删除){
this.txt=fullTxt.substring(0,this.txt.length-1);
}否则{
this.txt=fullTxt.substring(0,this.txt.length+1);
}
this.el.value=this.txt;
var=这个;
var delta=300-Math.random()*100;
如果(this.isDeleting){delta/=2;}
如果(!this.isDeleting&&this.txt===fullTxt){
delta=这个周期;
this.isDeleting=true;
}else if(this.isDeleting&&this.txt==''){
this.isDeleting=false;
这个.loopNum++;
δ=500;
}
setTimeout(函数(){
那。勾选();
},三角洲);
};
window.onload=函数(){
var元素=document.getElementById('myInput');
var-toRotate=element.getAttribute('data-rotate');
var period=element.getAttribute('data-period');
新的TxtRotate(元素,JSON.parse(toRotate),句点);
}
海帕尔,我/
走!
您可以尝试此版本
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.value =this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var element = document.getElementById('myInput');
var toRotate = element.getAttribute('data-rotate');
var period = element.getAttribute('data-period');
new TxtRotate(element, JSON.parse(toRotate), period);
}
</script>
</head>
<body>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input"
data-period="2000"
data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'>
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
</body>
</html>
在此处插入标题
var TxtRotate=函数(el、旋转、周期){
this.toRotate=toRotate;
this.el=el;
this.loopNum=0;
this.period=parseInt(period,10)| 2000;
this.txt=“”;
这个。勾选();