在scrollTop上发出一个div到一个百分比值

我正在处理下面的演示,并希望将具有百分比值50%的 scrollTop 应用于 scrollTop。这可能吗?


$("button").on("click", function(){

  $('#sc').animate({ scrollTop: 50+"%" }, 1);

});

#sc {

    background: khaki;

    width: 400px;

    height: 200px;

    position: relative;

    margin:20px;

    padding:0px;

    overflow:hidden;

    overflow-y: auto;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button>Scrool 50%</button>

<div id="sc">

<p>

</p>

</div>


千万里不及你
浏览 153回答 1
1回答

狐的传说

如果您想要滚动到总可滚动距离的中点,那么您将需要自己手动计算像素值。clientHeight元素的可滚动高度只是其和之间的差异scrollHeight。这些属性在 HTML 元素本身上是可用的,所以我们需要首先使用 获取它$('#sc')[0],它返回 DOM 节点:$("button").on("click", function(){&nbsp; var sc = $('#sc')[0];&nbsp; var scrollableHeight = sc.scrollHeight - sc.clientHeight;&nbsp; $('#sc').animate({ scrollTop: 0.5 * scrollableHeight }, 1);});请参阅概念验证示例:$("button").on("click", function(){&nbsp; var sc = $('#sc')[0];&nbsp; var scrollableHeight = sc.scrollHeight - sc.clientHeight;&nbsp; $('#sc').animate({ scrollTop: 0.5 * scrollableHeight }, 1);});#sc {&nbsp; &nbsp; background: khaki;&nbsp; &nbsp; width: 400px;&nbsp; &nbsp; height: 200px;&nbsp; &nbsp; position: relative;&nbsp; &nbsp; margin:20px;&nbsp; &nbsp; padding:0px;&nbsp; &nbsp; overflow:hidden;&nbsp; &nbsp; overflow-y: auto;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><button>Scrool 50%</button><div id="sc"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript