Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:16:47+00:00 2026-06-13T08:16:47+00:00

I have a Main div inside which i am trying to display another small

  • 0

I have a Main div inside which i am trying to display another small div on the coordinates captured on mouse click. Problem is that when i click on the extreme left, right, top, bottom the small div is going outside the main div. I have tried overflow:hidden on main div but i don’t want that. i want div to be positioned inside the main div no matter where i click.
Sample code:

$(document).ready(function() {
    $("#main_div").bind('click', function(event){

    var x = event.pageX-document.getElementById("main_div").scrollLeft;
    var y = event.pageY-document.getElementById("main_div").scrollTop;



    $("#container-5").css({"top":y,"left":x});
   });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:16:50+00:00Added an answer on June 13, 2026 at 8:16 am

    You can check if the coordinates of the click would put part of the small div outside of the main div, and if so change the coordinates so that the small div remains inside the main div. (See this jsFiddle.)

    $(document).ready(function() {
        $("#main_div").bind('click', function(event) {
            var $mainDiv = $("#main_div");
            var $container5 = $("#container-5");
    
            var x = event.pageX - $mainDiv.position().left;
            var y = event.pageY - $mainDiv.position().top;
    
            if (x + $container5.width() > $mainDiv.width()) {
                x = $mainDiv.width() - $container5.width();
            }
            if (y + $container5.height() > $mainDiv.height()) {
                y = $mainDiv.height() - $container5.height();
            }
    
            $container5.css({
                "top": y,
                "left": x
            });
        });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two MAIN DIVs inside a DIV which is a PART of the
I have a main wrapper div that is set 100% width. Inside that i
I have a main content area div that has a border. Inside this area
it's easier to explain with a schematic I have main div, which contains another
I have a main-section div that is set to inherit it's height from its'
I have a main parent div that is .resizable(). One of it's child divs,
I have a webpage which has a button placed inside a div. What i
I have a #pdfcontainer which is inside divs #main and #pdfmain. I'm using a
I have a div (main div) which contains several div elements(sub divs). I want
i have inside the body main 'container' div and inside this 'container' div there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.