简体   繁体   English

我想阻止我的div移动

[英]I want to stop my div from moving

I have a code which causes the waterlevel in a bottle to go up or down by clicking a button. 我有一个代码,可通过单击按钮使瓶子中的水位上升或下降。 however, it can't stop without clicking on stop. 但是,如果不单击停止,就无法停止。 I need it to basically reset when it empties, and that the div of the waterlevel doesn't go below a certain point (or above a certain point). 我需要它在清空时基本重置,并且水位的div不会低于特定点(或高于特定点)。 If you're interested in what I'm making; 如果您对我的工作感兴趣; I'm making an alcohol simulator. 我正在制作酒精模拟器。 So you'll basically see the waterlevel of an alcohol drink going down (ie 100 px), and 100 px down means that you can see the waterlevel(alcogol level) in the stomach with ie 50 px, and that equals 20 px in the blood circulation and 10 px in the brains. 因此,您基本上会看到酒精饮料的水位下降(即100像素),而降低100像素意味着您可以看到胃中的水位(酒精度)为50像素,而等于20像素。血液循环和大脑中的10 px。 (just to get an idea.) (只是想一个主意。)

But if I click on the button of going down, the div will just leave the bottle and will have an endless journey. 但是,如果我单击下降按钮,则div只会离开瓶子,并且将有无尽的旅程。 (js please, not jquery) (请使用js,而不是jquery)

here's my JS code: 这是我的JS代码:

function move_img(str) {
var step=2 ; // change this to different step value
switch(str){
case "down":
var x=document.getElementById('i1').offsetTop;
x= x + step;
document.getElementById('i1').style.top= x - 1 + "px";
break;

case "up":
var x=document.getElementById('i1').offsetTop;
x= x -step;
document.getElementById('i1').style.top= x + "px";
break;

case "left":
var y=document.getElementById('i1').offsetLeft;
y= y - step;
document.getElementById('i1').style.left= y + "px";
break;

case "right":
var y=document.getElementById('i1').offsetLeft;
y= y + step;
document.getElementById('i1').style.left= y + "px";
break;
}
}


function auto(str) {
 myVar = setInterval(function(){ move_img(str); }, 2)  ;


}
setTimeout(function( ) { clearInterval( myVar ); }, 1);
function nana(){


}

Your fiddle doesn't work. 你的小提琴不起作用。 You'll need to specify the height as well as the top attribute, and I believe top has to be above bottom. 您需要指定高度以及top属性,我相信top必须在bottom之上。 I've made an example here . 我在这里举了一个例子。 The changes to your examples are: 对示例所做的更改是:

//javascript
if (str == 'up' || str == 'down') {
    var top = el.offsetTop;
    var height = el.offsetHeight;  //get height to modify
    console.log(height);
    if (str == 'up') {
        top -= step;
        height += step;  //adjust height
    } else {
        top += step;
        height -=step;  //adjust height
    }
    if (top_max >= top && top >= 0) {  //make sure #i1 is inside #i
        document.getElementById('i1').style.top = top + "px";
        document.getElementById('i1').style.height = height + "px";  //modify height
    } else {
        clearInterval(myVar)
    }
//css
#i1 {
  width: 100px;
  display: inline-block;
  background:red;
  position: absolute;
  top: 150px;  //initial top
  bottom: 250px;  //as as bottom of #i
  height:100px;  //initial height

} }

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

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