简体   繁体   中英

Javascript modal window requires scroll down to see

Am currently working with the Soundcloud API to pull images and track links for songs. Am currently showing track info through a modal window upon clicking the album image. However, for the songs on the bottom of the screen, the modal window only appears at the top of the screen, requiring a user to scroll up to see the track info. Probably has something to do with the css positioning but removing position:absolute only puts the modal window at the bottom of all the album images, requiring a scroll down. How can I make it so that a user's click on an image will open and start the modal window right where they currently are, without scrolling? Javascript / jquery /css answers all welcomed.

My CSS:

#modal { 
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.2);
    position: absolute;
    top: 0;
    left: 0;

}

#trackinfo {

    width:380px;
    height: 180px;
    padding: 20px; 
    margin-right: auto;
    margin-left: auto;
    margin-top: 100px;
    box-shadow: 10px 10px 5px;
    border-radius: 15px;
    text-align: center;
    background: #c4e5c1;
    font-family: Rockwell, "Courier Bold", Courier, Georgia, Times, "Times New Roman", serif;

}

.hidden { 
    display:none;
}

My Javascript for show and hide modal:

  function doSearch() {
    var searchTerm = document.getElementById('search').value;

    // Search soundcloud for artists
    SC.get('/tracks', { q: searchTerm}, function(tracks) {
      for(track in tracks) {

        var img = document.createElement('img');
        img.setAttribute("src", (tracks[track]["artwork_url"]));
        var title = tracks[track].title.replace("'", "\\\'").replace("\"", "\\\"");

        linky = document.createElement('a');
        linky.setAttribute("href", (tracks[track].permalink_url));
        console.log(linky);

        img.setAttribute("onclick", "showTrackInfo('" + title + "\\n"+ tracks[track].uri + " " + "\\n\\n(click to close) " + "')"); 

        console.log(img);


        if (tracks[track]["artwork_url"] == null) {
          console.log(""); } 
        else { 

          var Catalog = document.getElementById('catalog');
          Catalog.appendChild(img);
          $('div#catalog').append('<a href="'+linky+'"><img src="http://i.imgur.com/rGdvfl7.png"></a>');

        }
      }
    });
  };

  function showTrackInfo(track) { 
    var trackInfoElement = document.getElementById("trackinfo");
    trackInfoElement.appendChild(document.createTextNode(track));

    var modal = document.getElementById("modal"); 
    modal.setAttribute("class", ""); 
  }

  function hidemodal() { 
    var trackInfoElement = document.getElementById("trackinfo");
    trackInfoElement.childNodes;

    while ( trackInfoElement.firstChild ) trackInfoElement.removeChild( trackInfoElement.firstChild );

    var modal = document.getElementById("modal");
    modal.setAttribute("class", "hidden"); 
  }

The functions all work well, I just need to know how to position the modal box upon click so that the user doesn't need to scroll to see the trackinfo. Any help much appreciated.

Try position: fixed; on the dialog box, should make it fill the page no matter where they are on it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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