简体   繁体   English

使用Javascript +输入类型=范围滚动Div

[英]Scroll a Div using Javascript + Input type=range

i am currently working on a project where i have to scroll a certain section based on the movement of input type=range. 我目前正在一个项目中,我必须根据输入type = range的移动来滚动某个部分。 This is the code- 这是代码-

 $('input[type=range]').val('0'); $('input[type=range]').on('change input', function() { var max = 60; var value = $(this).val(); var percent = value / max; var height = $('.tooltip').height(); console.log("v",value); var top = value + "%"; console.log("t",top); $('.tooltip').css('top', top); }) 
 .tooltip { position: absolute; left: 0; background: silver; border-left: dotted 2px orange; padding: 2px; max-width: 200px; text-align: center; } input[type=range] { margin-top: 50px; width: 100%; } input[type=range]::-webkit-slider-thumb:after { content: ''; width: 100px; background: transparent; color: #000; border-left: dotted 2px orange; pointer-events: none; padding: 50px; position: relative; top: 50px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="height:250px;position:relative;border:2px solid;width:200px;overflow:hidden;"> <div class="tooltip"> <p>How can I get this border to <em>consistently</em> align with the one on the thumb?</p> </div> </div> <input type="range" min="0" max="100"/> 

- here is the working . - 这是工作 But the problem is, once i scroll upto 100% the scrolling div goes beyond the outer div. 但是问题是,一旦我滚动到100%,滚动div就会超出外部div。 I want the scrolling div to stop at the end of outer div, even when thew range is at 100%(the scrolling diov should not cross the outer div). 我希望滚动div停在外部div的末尾,即使w范围为100%(滚动diov不应越过外部div)。 Thanks in Advance. 提前致谢。

  1. change the max def in javascript part to 100, because the input has max="100" defined. 将javascript部分的最大清晰度更改为100,因为输入定义了max =“ 100”。
  2. use height difference instead of height. 使用高度差代替高度。

 $('input[type=range]').val('0'); $('input[type=range]').on('change input', function() { var max = 100; var value = $(this).val(); var percent = value / max; var parent_height = $('.tooltip').parent().height(); var height = $('.tooltip').height(); //console.log("p:" + parent_height + " s:" + height + " %:" + percent); var top = (parent_height - height) * percent; //console.log("t", top, "h", parent_height - height); if(percent <= 1) $('.tooltip').css('top', top + "px"); }) 
 .tooltip { position: absolute; left: 0; background: silver; border-left: dotted 2px orange; padding: 2px; max-width: 200px; text-align: center; } input[type=range] { margin-top: 50px; width: 100%; } input[type=range]::-webkit-slider-thumb:after { content: ''; width: 100px; background: transparent; color: #000; border-left: dotted 2px orange; pointer-events: none; padding: 50px; position: relative; top: 50px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="height:250px;position:relative;border:2px solid;width:200px;overflow:hidden;"> <div class="tooltip"> <p>How can I get this border to <em>consistently</em> align with the one on the thumb?</p> </div> </div> <input type="range" min="0" max="100" /> 

You need to add condition before set the left of the div . 您需要在设置div的左侧之前添加条件。

var tooltipWidth = $('.tooltip').width();

if(left + tooltipWidth > width){
    left = width - tooltipWidth;
}

Here is the working sample. 是工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM