自动滚动不起作用?

时间:2015-06-09 06:50:11

标签: javascript jquery html css autoscroll

实际上,自动滚动在我之前的版本中运行良好(http://jsfiddle.net/Darious/m10wff0u/1/)。

现在,在修改了JAVASCRIPT后它无法正常工作。

$('#sim').each(function () {
    this.contentEditable = true;
});

var go = $('#go');
var stop = $('#stop');
var wordCount = 0;
var wordCountBox = $('#wordCountBox');
var timepassed = $('#timepassed');
var textRead = $('#textRead');


go.on("click", function (e) {
    e.preventDefault();
    startSim();
});

function startSim() {
    var speed = $('#speed').val();
    var boldWords = speed / 60;
    boldWords = boldWords < 1 ? 1 : Math.floor(boldWords);
    timeStart = $.now();
    var sim = $('#sim').text();
    var wordArray = sim.split(" ");
    var simWrap = $('#sim');

    var arrCount = wordArray.length;
    var alreadyRead = [];

    for (var i = 0; i < arrCount; i++) {
        (function (index) {
            setTimeout(function () {
                var pos = index;
                if (pos < 0) {
                    pos = 0;
                }
                alreadyRead.push(wordArray[pos]);
                wordArray[pos] = '<span class="grayx">' + wordArray[pos] + '</span>';
                if (pos > (boldWords - 1)) {
                    wordArray[pos - boldWords] = wordArray[pos - boldWords].replace("x", "dim");
                }
                var words = wordArray.join(" ");
                simWrap.html(words);
                wordCount++;
                if (pos == (arrCount - 1)) {
                    triggerDone();
                }
                $('#sim b:last')[0].scrollIntoView(false);
            }, i * speed);
        })(i);
    }
    // Function done
    function triggerDone() {
        wordCountBox.text(wordCount + ' Words Read');
        var timeEnd = $.now();
        var timeRes = timeEnd - timeStart;
        timeRes = parseInt(timeRes);
        timeRes = timeRes / 1000;
        timepassed.text(timeRes + " seconds have passed");
        alreadyRead = alreadyRead.join("");
        textRead.text(alreadyRead);
        var summary = $('#summary');
        summary.show();
        return;
    }
    stop.on("click", function (e) {
        e.preventDefault();
        triggerDone();
    });
}
#sim {
    width:800px;
    height:400px;
    border:solid 1px #2e2e2e;
    color:#B3B3B3;
    padding:5px;
    overflow-y:scroll;
    border:9px outset #0ADA0A;
    margin-top:1em;
    font-size:16pt;
    text-align:left;
    background-color:#333;
    column-count: 2;
    -moz-column-count: 2;
    -moz-column-gap: 20px;
    -webkit-column-count: 2;
    -webkit-column-gap : 20px;
    -moz-column-rule-color: #ccc;
    -moz-column-rule-style: solid;
    -moz-column-rule-width: 1px;
    -webkit-column-rule-color: #ccc;
    -webkit-column-rule-style: solid;
    -webkit-column-rule-width: 1px;
}
button {
    padding:10px 25px;
    color:#fafafa;
    transition:all 0.3s;
    cursor:pointer;
}
#go {
    background-color:#46A111;
    border:solid 1px #46A111;
}
#go:hover {
    background-color:#fafafa;
    color:#46A111;
}
#stop {
    background-color:#A11111;
    border:solid 1px #A11111;
}
#stop:hover {
    background-color:#fafafa;
    color:#A11111;
}
.grayx {
    font-weight: 600;
}
.graydim {
    color: dimgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input id="milliseconds" type="number" value="1000" step="25" min="0" max="1000" />
<label for="speed">in milliseconds</label>
<button id="go">START</button>
<button id="stop">STOP</button>
<input id="speed" type="number" value="300" step="10" min="0" max="1000" />
<label for="speed">WPM</label>
<div id="sim" cols="2" rows="2"></div>
<div class="summary" id="summary">
    <p id="wordCountBox"></p>
    <p id="timepassed"></p>
</div>

还有为什么它在Safari浏览器中不起作用?

有人可以解决这个问题吗?

0 个答案:

没有答案