2

I need to move div on click of one of the 4 buttons: left, right, top, bottom

By clicking on each button I want div to move to the left, right, top, bottom according to the clicked button.

function left(){
  $( "#rstSearch" ).position().left = $( "#rstSearch" ).position().left + 10;
}

function right(){
  $( "#rstSearch" ).position().right = $( "#rstSearch" ).position().right + 10;
}

function top2(){
  $( "#rstSearch" ).position().top = $( "#rstSearch" ).position().top + 10;
}

function bottom(){
  $( "#rstSearch" ).position().bottom = $( "#rstSearch" ).position().bottom + 10;
}
#rstSearch {
  background-color: blue;
  width:50px; 
  height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="btnLeft"  onclick="left();" class="button_air-medium"> left</button>    		 
<button type="button" id="btnRight"  onclick="right();" class="button_air-medium">right</button>
<button type="button" id="btnTop"  onclick="top2();" class="button_air-medium">top</button>
<button type="button" id="btnBottom"   onclick="bottom();" class="button_air-medium">bottom</button>




<div id="rstSearch" class="exosphere" ></div>

As you can see each button has an event listener that calls a function to move the div to said direction.

This however does not work, what do I have change to make it work?

1

4 Answers 4

1

Here you go with a solution https://jsfiddle.net/o2gxgz9r/14501/

function left(){
  $( "#rstSearch" ).css('left',$( "#rstSearch" ).position().left - 10 );
}

function right(){
  $( "#rstSearch" ).css('left', $( "#rstSearch" ).position().left + 10);
}

function top2(){
  $( "#rstSearch" ).css('top', $( "#rstSearch" ).position().top - 10);
}

function bottom(){
  $( "#rstSearch" ).css('top', $( "#rstSearch" ).position().top + 10);
}
#rstSearch {
  background-color: blue;
  width:50px; 
  height:50px;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="btnLeft"  onclick="left();" class="button_air-medium"> left</button>    		 
<button type="button" id="btnRight"  onclick="right();" class="button_air-medium">right</button>
<button type="button" id="btnTop"  onclick="top2();" class="button_air-medium">top</button>
<button type="button" id="btnBottom"   onclick="bottom();" class="button_air-medium">bottom</button>

<div id="rstSearch" class="exosphere" ></div>

You need to specify the position as absolute to the div container.

Element position has only two property left and top, you need to use these two & calculate based on that.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

1

Use instead of position the jquery css modifier add position fixed to your css.

function left(){
  $( "#rstSearch" ).css("left", $( "#rstSearch" ).position().left -= 10);
}

function right(){
  $( "#rstSearch" ).css("left", $( "#rstSearch" ).position().left += 10);
}

function top2(){
  $( "#rstSearch" ).css("top", $( "#rstSearch" ).position().top -= 10);
}

function bottom(){
  $( "#rstSearch" ).css("top", $( "#rstSearch" ).position().top += 10);
}
#rstSearch {
  background-color: blue;
  width:50px; 
  height:50px;
  position: fixed;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="btnLeft"  onclick="left();" class="button_air-medium"> left</button>    		 
<button type="button" id="btnRight"  onclick="right();" class="button_air-medium">right</button>
<button type="button" id="btnTop"  onclick="top2();" class="button_air-medium">top</button>
<button type="button" id="btnBottom"   onclick="bottom();" class="button_air-medium">bottom</button>

<div id="rstSearch" class="exosphere" ></div>

Comments

1

A different approach without jQuery and only two functions for movements on x and y-axis.

var rstSearch = document.getElementById('rstSearch');

function horizontal(value) {  
  rstSearch.style.left = (parseInt(rstSearch.style.left) || 0) + value + 'px';
}

function vertical(value) {  
  rstSearch.style.top = (parseInt(rstSearch.style.top) || 0) + value + 'px';
}
#rstSearch {
  background-color: blue;
  width: 50px; 
  height: 50px;
  position: relative;
}
<button type="button" id="btnLeft"   onclick="horizontal(-10);" class="button_air-medium">left</button>    		 
<button type="button" id="btnRight"  onclick="horizontal(10);" class="button_air-medium">right</button>
<button type="button" id="btnTop"    onclick="vertical(-10);" class="button_air-medium">top</button>
<button type="button" id="btnBottom" onclick="vertical(10);" class="button_air-medium">bottom</button>

<div id="rstSearch" class="exosphere"></div>

Comments

0

Change css little:

#rstSearch {
  background-color: blue;
  width:50px; 
  height:50px;
  position: absolute;  // added this
}

Here you go for updated jquery code:

function left(){
  $( "#rstSearch" ).attr('style','margin-right:10px;');
}

function right(){
  $( "#rstSearch" ).attr('style','margin-left:10px;');
}

function top2(){
  $( "#rstSearch" ).attr('style','margin-bottom:10px;');        
}

function bottom(){
  $( "#rstSearch" ).attr('style','margin-top:10px;');   
}

Check JSFIDDLE

2 Comments

this will move it only once though, make it read what it is currently and move by 10 instead maybe? Or am I wrong?
You're right @Dellirium. Also, adding a margin-bottom won't make the element to move up...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.