Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open my jQuery modal as wide as my elements but no wider?

I’m using JQuery 1.12.4. I have the following DIV, which I intend to open as a dialog.

<div id="loginBox" style="display:none">
    <div>Login/Sign Up</div>
    <div id="loginLogos">
            <a href="/auth/google"><img border="0" alt="Google" src="http://www.racertracks.com/assets/google_plus_icon-8d7622763257233cf58e0465806f58d7c4b82b85271c2d03d0e02b2fadc4ac76.jpg"></a>
            <a href="/auth/facebook"><img border="0" alt="Facebook" src="http://runtrax.devbox.com:3000/assets/facebook-b74d7c49fd91aa6ca76518bf46c7b75fa3b23f47028bea4b2ffaa39f5776dd91.png"></a>
            <a href="/auth/twitter"><img border="0" alt="Twitter" src="http://runtrax.devbox.com:3000/assets/twitter_icon-7dbf8db24c3ad328ea28cba340e4f53e033b64b149850324822cdb622d77f331.png"> </a>
 </div></div>

What I’m noticing is that when I reduce the width of my screen to around 320 pixels (to simulate a mobile browser width), the dialog is opening wider than the total width of the elements — there is a lot of white space to the right of the images. See the Fiddle here -- https://jsfiddle.net/g4a60Lq7/3/ . How can I make the dialog open as wide as the elements and no wider? Below is how I’m opening the dialog …

  var opt;
opt = {
  autoOpen: false,
  modal: true,
  width: 'auto',
  dialogClass: 'noTitle',
  focus: function() {
    return $(this).dialog('option', 'width', $('#loginBox').width() + 50);
  }
};
$("#loginBox").dialog(opt);
return $("#loginBox").dialog("open");

Edit: Wanted to post the image in response to pritishvaidya's second plunker ...

enter image description here

like image 215
Dave Avatar asked Oct 11 '16 23:10

Dave


People also ask

How do I make my modal window wider?

Modal Size Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.

What is modal toggle?

modal(“toggle”) method in Bootstrap to toggle the modal. As shown below, the modal generates on the click of a button − $(document).ready(function(){ $("#button1").click(function(){ $("#newModal").modal("toggle"); }); });

Which HTML is correct for a button to open a modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do I get modal to pop on page load?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.


2 Answers

You can try this jsfiddle: https://jsfiddle.net/43f5qjc8/7/. Since the image source is not very reliable, here is another jsfiddle with smaller images from another source: https://jsfiddle.net/43f5qjc8/13/.

It involves some Javascript to calculate the minimial width needed to display the content, which may wrap on several lines. If there is a way to get the same result with CSS only, I haven't found it.

Here is the Javascript code:

$(function () {
    $('.opendialog').click(function () { openDialog(); });
    function openDialog() {
        var opt = {
            autoOpen: false,
            modal: true,
            width: 'auto',
            dialogClass: 'noTitle',
            focus: function () {
                var width = 0;
                $('#loginLogos > .logoRow').each(function () {
                    var x = $(this).position().left;
                    var w = $(this).outerWidth();
                    var tmpWidth = x + w;
                    width = Math.max(width, tmpWidth);
                });
                $('#loginLogos').width(width);
                var outerWidth = $('#loginBox').outerWidth();
                $('#loginLogos').width('auto');
                return $(this).dialog('option', 'width', outerWidth);
            }
        };
        $("#loginBox").dialog(opt);
        return $("#loginBox").dialog("open");
    }
});

The HTML markup:

<a href='#' class="opendialog">Open</a>
<div id="loginBox" style="display:none">
    <div>Login/Sign Up</div>
    <div id="loginLogos">
        <div class="logoRow">
            <a href="/auth/google"><img border="0" alt="Google" src="..."></a>
            <a href="/auth/facebook"><img border="0" alt="Facebook" src="..."></a>
        </div>
        <div class="logoRow">
            <a href="/auth/twitter"><img border="0" alt="Twitter" src="..."></a>
            <a href="/auth/linkedin"><img border="0" alt="LinkedIn" src="..."></a>
        </div>
    </div>
</div>

And the CSS styles:

body
{
    background-color: gray;
}

#loginBox
{
    font-family: 'russo_oneregular';
    font-size: 20px;
    display: inline-block;
}

#loginLogos
{
    position: relative;
}

.logoRow
{
    display: inline-block;
}
like image 137
ConnorsFan Avatar answered Oct 05 '22 00:10

ConnorsFan


Just use Bootstrap modal popup structure that is totally responsive.

  1. no need custom css.
  2. easily customize by bootstrap js attribute

    <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">   
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">       
    
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>   
    </div>
    

https://jsfiddle.net/zigri2612/0o41yogx/

like image 31
Zigri2612 Avatar answered Oct 05 '22 00:10

Zigri2612