Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - Info box similar to Google Image Search Results

Here are some factors I would like to borrow from Googles Image Search Results:

  • Each item sits next to each other and falls to the next row if it cant fit in the current row because of the browser windows width.

  • When one item is clicked on, an info box slides open directly below the row of the selected item, it fills the width of the browser, also an indicator is visible at the bottom of the selected item.

  • When the info box is opened, if the browser width is changed, the items fall into a row that it can fill (top first) or fall into the row below. this does not affect the 100% width of the info box or its position directly below the row of the currently selected item.

I already have implemented the first bullet by creating DIVs that have display: inline-block

I want a solution thats mostly HTML and CSS first, and JavaScript (JQuery, AJAX etc.) only if necessary, I dont like using too many HTML attributes except for ones I'd need to make this work, like id, class, src etc. I usually use CSS whenever possible instead.

If I can stick to just HTML elements and then style them with CSS and what ever remnants can't be fulfilled with just CSS and HTML be fulfilled with JavaScript (or any derivative JQuery, AJAX etc).

an example of where im at so far:

<html>
    <head>
        <style>
            div
                {
                     display: inline-block;
                     height: 200px;
                     width: 200px;
                }
        </style>
    </head>
    <body>
       <div>
           <img/>
       </div>
       <div>
           <img/>
       </div>
       <div>
           <img/>
       </div>
       <div>
           <img/>
       </div>
       <div>
           <img/>
       </div>
       <div>
           <img/>
       </div>
    </body>
</html>

EDIT: OK heres what I came up with, it does exactly what I wanted it to do, hope this helps anyone looking to do the same.

<html>
    <head>
        <script>
            "Some JavaScript/JQuery etc code that will,
             first put the 'info' DIV block right after
             the selected item (code-wise), then alternates
             the 'display' CSS property of the '.info' class
             from none to normal so it renders just below the
             selected item. Just changing none to normal is
             enough because of the 'clear' and 'float' properties."
        </script>
        <style>
            div.item
                {
                    background-color: blue;
                    border: red 5px solid;
                    display: inline-block;
                    height: 200px;
                    width: 200px;
                }

            div.info
                {
                    background-color: grey;
                    float: left;
                    clear: both;
                    width: 100%;
                    height: 200px;
                    display: normal;
                }
        </style>
    </head>
    <body>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="info">
           Here lies some cool details about the item selected.
           Have your JS code put this DIV block right after the selected DIV block in code.
           You will notice that no matter how much you change the width of the browser,
           this block always remains under the selected item as long as your JavaScript
           put this code block directly after the selected item. 
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
       <div class="item">
           <img src="some_relative_image.png"/>
       </div>
    </body>
</html>

try this and resize the browsers width while the display property of the info DIV is set to normal. You will notice all the item boxes rise above the info DIV as the width increases and they fall under it as the width decreases. Hope this helps someone ;D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to avoid javascript as much as possible, you can create a <div class='info'> with the details about the image after each <div class="item"> like so:

<div class="item">
    <img src="some_relative_image.png"/>
</div>
<div class="info">
    Details about the first image.
</div>
<div class="item">
    <img src="some_relative_image.png"/>
</div>
<div class="info">
    Details about the second image.
</div>
<div class="item">
    <img src="some_relative_image.png"/>
</div>
<div class="info">
    Details about the third image.
</div>

After that, replace this line in your css:

div.info
{
    display: normal;
}

with:

div.info
{
    display: none;
}

This will make the details hidden when the page is loaded.


And finally, insert the following jQuery code to make the details appear when <div class="item"> is clicked:

$("div.item").click(function (){
    $("div.info").css("display", "none");
    $(this).find("+ div.info").css("display", "block"); 
});

jsFiddle here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...