Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
keep divs in view when browser resized vertically

Im working on a website that behaves like a desktop where users can drag icons round the screen within a container and then click them to open windows. The issue im having is keeping the icons in view at all times so if a user decreases the browser size vertically, the icons dont disappear off the bottom.

Ive got it working horizontally so if the browser width is reduced the icons will move with the window and stay in view, but I cant get it working vertically if the height of the browser is adjusted (once an icon has been dragged)

See the below example, load the snippet and view it fullscreen, then resize the browser vertically and the text stays in view at all times. But once you move the text then resize the browser it stays where it is rather than moving with the browser.

$(function() {
  $(".drag-me").draggable({
    containment: "window",
    stop: function(e, ui) {
      var perc = ui.position.left / ui.helper.parent().width() * 100;
      ui.helper.css('left', perc + '%');
    }
  });
});
.drag-me {
 font-family: 'Lato', sans-serif;
  font-weight: 300;
  font-size: 16px;
  line-height: 16px;
  position: absolute;
    cursor: pointer;
    top: 20% ;
    left: 50% ;
    width:50px;
    }
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

<div class="drag-me">
  <span>Drag me around!</span>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to do it on window resize event

I think that's what you need:

$(function() {
    $(window).on("resize", function(){
        if($(".drag-me").offset().top + $(".drag-me").height() >= $(window).height()){
          var topPos = $(window).height() - $(".drag-me").height();
          $(".drag-me").css({'top': topPos + 'px'});
        }
    });
  $(".drag-me").draggable({
    containment: "window",
    stop: function(e, ui) {
      var perc = ui.position.left / ui.helper.parent().width() * 100;
      ui.helper.css({'left': perc + '%'});
    }
  });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!