【问题标题】:How do I change the delete behavior from right to left to be from left to right?如何将删除行为从右到左更改为从左到右?
【发布时间】:2018-04-14 03:26:59
【问题描述】:

如何将删除行为从右到左更改为从左到右?

isDeleting下面是完整代码

var TxtType = function(el, toRotate, period) {
    this.toRotate = toRotate;
    this.el = el;
    this.loopNum = 0;
    this.period = parseInt(period, 10) || 2000;
    this.txt = '';
    this.tick();
    this.lastDeletingStatus=0;
    this.isDeleting = 0;
};
var timer;
TxtType.prototype.tick = function() {
    var i = this.loopNum % this.toRotate.length;
    var fullTxt = this.toRotate[i];

    if (this.isDeleting===1) {
        this.txt = fullTxt.substring(0, this.txt.length - 1);
    } else {
        this.txt = fullTxt.substring(0, this.txt.length + 1);
    }

    this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';

    var that = this;
    var delta = 200 - Math.random() * 100;

    if (this.isDeleting===1) { delta /= 2; }

    if (this.isDeleting===0 && this.txt === fullTxt) {
        delta = this.period;
        this.isDeleting = 1;
    } else if (this.isDeleting===1 && this.txt === '') {
        this.isDeleting = 0;
        this.loopNum++;
        delta = 500;
    }

    if(this.isDeleting!==2){
    timer=setTimeout(function() {
	    that.tick();
        }, delta);
    }
};

TxtType.prototype.toggleStart=function(){
//start back up 
    if(this.isDeleting===2){
        this.isDeleting=this.lastDeletingStatus;
        this.lastDeletingStatus=2;
 }
//stop
else{
    this.lastDeletingStatus=this.isDeleting;
    this.isDeleting=2;
    clearTimeout(timer);
}
}
    var toggleStart=function(){
       txtType.toggleStart();
        txtType.tick();
    }
	var txtType;

    window.onload = function() {
        var elements = document.getElementsByClassName('typewrite');
        for (var i=0; i<elements.length; i++) {
            var toRotate = elements[i].getAttribute('data-type');
            var period = elements[i].getAttribute('data-period');
            if (toRotate) {
		txtType=new TxtType(elements[i], JSON.parse(toRotate), period);
            }
          
        }
        // INJECT CSS
        var css = document.createElement("style");
        css.type = "text/css";
        css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
        document.body.appendChild(css);
    };
body {
    background-color:#ce3635;
    text-align: center;
     color:#fff;
     padding-top:10em;
     font-family:Helvetica;
 }

* { color:#fff; text-decoration: none;}
<div class="type-wrap">
<h2>
  <a href="" class="typewrite" data-period="2000" data-type='[ "Hi, my name is Justin.", "I am Creative.", "I love Design with CSS.", "I love to Develop with javascript." ]'>
    <span class="wrap"></span></a>
</h2>
  
</div>

如何将删除行为从右到左更改为从左到右?

【问题讨论】:

    标签: javascript css arrays loops dom-events


    【解决方案1】:

    只需将从字符串末尾开始删除的部分更改为从开头开始:

    var TxtType = function(el, toRotate, period) {
        this.toRotate = toRotate;
        this.el = el;
        this.loopNum = 0;
        this.period = parseInt(period, 10) || 2000;
        this.txt = '';
        this.tick();
        this.lastDeletingStatus=0;
        this.isDeleting = 0;
    };
    var timer;
    TxtType.prototype.tick = function() {
        var i = this.loopNum % this.toRotate.length;
        var fullTxt = this.toRotate[i];
    
        if (this.isDeleting===1) {
            this.txt = this.txt.slice(1);
        } else {
            this.txt = fullTxt.substring(0, this.txt.length + 1);
        }
    
        this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';
    
        var that = this;
        var delta = 200 - Math.random() * 100;
    
        if (this.isDeleting===1) { delta /= 2; }
    
        if (this.isDeleting===0 && this.txt === fullTxt) {
            delta = this.period;
            this.isDeleting = 1;
        } else if (this.isDeleting===1 && this.txt === '') {
            this.isDeleting = 0;
            this.loopNum++;
            delta = 500;
        }
    
        if(this.isDeleting!==2){
        timer=setTimeout(function() {
    	    that.tick();
            }, delta);
        }
    };
    
    TxtType.prototype.toggleStart=function(){
    //start back up 
        if(this.isDeleting===2){
            this.isDeleting=this.lastDeletingStatus;
            this.lastDeletingStatus=2;
     }
    //stop
    else{
        this.lastDeletingStatus=this.isDeleting;
        this.isDeleting=2;
        clearTimeout(timer);
    }
    }
        var toggleStart=function(){
           txtType.toggleStart();
            txtType.tick();
        }
    	var txtType;
    
        window.onload = function() {
            var elements = document.getElementsByClassName('typewrite');
            for (var i=0; i<elements.length; i++) {
                var toRotate = elements[i].getAttribute('data-type');
                var period = elements[i].getAttribute('data-period');
                if (toRotate) {
    		txtType=new TxtType(elements[i], JSON.parse(toRotate), period);
                }
              
            }
            // INJECT CSS
            var css = document.createElement("style");
            css.type = "text/css";
            css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
            document.body.appendChild(css);
        };
    body {
        background-color:#ce3635;
        text-align: center;
         color:#fff;
         padding-top:10em;
         font-family:Helvetica;
     }
    
    * { color:#fff; text-decoration: none;}
    <div class="type-wrap">
    <h2>
      <a href="" class="typewrite" data-period="2000" data-type='[ "Hi, my name is Justin.", "I am Creative.", "I love Design with CSS.", "I love to Develop with javascript." ]'>
        <span class="wrap"></span></a>
    </h2>
      
    </div>

    如果你也想移动文本光标,在删除开始时从 DOM 中移除元素,然后在左侧插入它

    【讨论】:

    • 删除时如何保持文本位置不变?
    猜你喜欢
    • 2018-07-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多