满足条件时如何滚动iframe?

时间:2019-05-03 07:08:20

标签: javascript jquery iframe

我在滚动iframe时遇到问题。它在click / load事件上向下滚动。 但这在if(condition)中不起作用。

//works in click event but it does not work in if/else block
$("#btn").click(function () {
    $("#recording").contents().scrollTop(300);      //this works perfectly
});
if (data.scrollY != null) {
    $("#recording").contents().scrollTop(data.scrollY);
    console.log(data.scrollY)      //scroll position is being printed in console but iframe is not being srolled down don't know why
}
//my Iframe
<iframe id="recording" scrolling="yes" style="height:100%; width:100%;" src="localhost:44306/Home/CustomerDashboard"></iframe>

1 个答案:

答案 0 :(得分:0)

<script>
$(function(){
        //works in click event but it does not work in if/else block
        $("#btn").click(function () {
            $("#recording").contents().scrollTop(300);      //this works perfectly
        });
        if (data.scrollY != null) {

    document.getElementById('recording').scrollTop = parseInt(data.scrollY);

            console.log(data.scrollY)      //scroll position is being printed in console but iframe is not being srolled down don't know why
        }
});
</script>
        //my Iframe
        <iframe id="recording" scrolling="yes" style="height:100%; width:100%;" src="localhost:44306/Home/CustomerDashboard"></iframe>

我认为您可以尝试解析data.scrollY。