...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 9, 2012

animate div moving from one place to another

Question by Vijay V

I am designing a potal framework and I have multiple portlets(divs) within the main div.
When I move a div, I want it to move to the top of the page and expand vertically. All of this works, however, I would like to animate the moving part.

this is what my code looks like

$('#smallpotletdiv').prependTo($('#maindiv'));

what would someone that knows more about animation suggest? How would I show the div moving from its current location and attach itself to the new location?

Answer by Starx

Play with the positions using animate and prepend at last. After the prepend is complete, change the values to default

$("#smallpotletdiv").animate({
      top: 300,
      left: 500     
}).prependTo($("#maindiv")).css({
      top: 'auto',
      left: 'auto'
});

Update:

You working codes will look something like this

current.animate({
  top: -30,
  left: -20     
}, 2000, function() {
    current.prependTo(prependToDiv).css({
       top: 'auto',
       left: 'auto'
    });        
});

Check Here

The problem with their execution were that, you didn’t position the element to be movable through left and top. For that an element has to be positioned absolutely i.e. position: absolute.

Read more

set key and values on array

Question by user983248

I have on array like:
$myarray = array("color", "red", "size", "big", "flavor", "bitter"); where color, size and flavor are the keys and the other are the values. How can I loop true the values only on the array.

I have a few arrays like that one so I only need to create a table and display their values like:

<table>
 <thead>
  <tr>
   <th>Color</th>
   <th>Size</th>
   <th>Flavor</th>
  </tr>
 </thead>
 <tbody>
       // I'm stuck here because I don't know how to get the values of each array
 </tbody>
</table>

Any help will be appreciated
Thanks

Answer by deceze

while (list(, $key) = each($array)) {
    $value = current($array);
    next($array);

    echo $key, ': ', $value, PHP_EOL;
}

But yes, you should really use a proper associative array instead of this makeshift solution.

Answer by Starx

You are defining array incorrectly.

$myarray=array(
    'color'=>'red',
    'size'=>'big',
    'flavor'=>'bitter'
);

Then use it with foreach

foreach($myarray as $key => $value) {
   echo $key; //echoes the indexes like color
   echo $value; //echoes values like red
}
Read more

Generate repeating hexagonal pattern with CSS3

Question by Archio

So, I need to make a repeating hexagonal pattern, using CSS. If images are needed, I can go there, but I’d prefer to just use CSS if possible.

Here’s an idea of what I’m trying to create:

enter image description here

Basically, I just need a way to create the hexagonal shapes, and then overlay text/images on top of them. I don’t have much code yet, because I’m not really sure where to start. The problem is, I could just use <div>s in the shape of a hexagon like shown in (http://css-tricks.com/examples/ShapesOfCSS/), but then they wouldn’t be connecting. I could use a repeating hexagon pattern, but then I wouldn’t be able to specify the exact location of the text or images I need in specific shapes. Thanks for any help in advance.

Answer by ScottS

This was a truly amazing question. Thank you for asking it. The great thing is the fact that:

This (Newly Revised) Fiddle Proves You Can Do It!

Original Fiddle Used–it utilized imgur.com images, which were not seeming to be very reliable in loading (I think I may have got a down vote because of that), so the new fiddle is using photobucket.com (let me know if there are persistent image loading issues). I’ve kept the original link because the explanation code below goes with that (there are a few differences in background-size or position to the new fiddle).

The idea came to me almost instantly after reading your question, but took some time to implement. I originally tried getting a single “hex” with a single div and just pseudo elements, but as best I could tell, there was no way to just rotate the background-image (which I needed), so I had to add some extra div elements to get the right/left sides of the hex, so that I could then use the pseudo elements as a means of background-image rotation.

I tested in IE9, FF, and Chrome. Theoretically any browser supporting CSS3 transform it should work in.

First Main Update (added explanation)

I have some time now to post some code explanation, so here goes:

First, hexagons are defined by 30/60 degree relationships and trigonometry, so those will be the key angles involved. Second, we start with a “row” for the hex grid to reside in. The HTML is defined as (the extra div elements help build the hex):

<div class="hexrow">
    <div>
        <span>First Hex Text</span>
        <div></div>
        <div></div>
    </div>
    <div>
        <span>Second Hex Text</span>
        <div></div>
        <div></div>
    </div>
    <div>
        <span>Third Hex Text</span>
        <div></div>
        <div></div>
    </div>
</div>

We are going to use inline-block for the hexagon display, but we don’t want them to accidentally wrap to the next line and ruin the grid, so white-space: nowrap solves that issue. The margin on this row is going to depend on how much space you want between hex’s, and some experimentation may be needed to get what you want.

.hexrow {
    white-space: nowrap;
    /*right/left margin set at (( width of child div x sin(30) ) / 2) 
    makes a fairly tight fit; 
    a 3px bottom seems to match*/
    margin: 0 25px 3px;
}

Using the immediate children of the .hexrow which are just div elements, we form the basis for the hex shape. The width will drive the horizontal of the top of the hex, the height is derived from that number since all the sides are equal length on a regular hexagon. Again, margin is going to depend on spacing, but this is where the “overlap” of the individual hexagons is going to occur to make the grid look occur. The background-image is defined once, right here. The shift left on it is to accommodate at least the added width for the left side of the hex. Assuming you want centered text, the text-align handles the horizontal (of course) but the line-height that matches the height is going to allow for a vertical centering.

.hexrow > div {
    width: 100px;
    height: 173.2px; /* ( width x cos(30) ) x 2 */
    /* For margin:
    right/left = ( width x sin(30) ) makes no overlap
    right/left = (( width x sin(30) ) / 2) leaves a narrow separation
    */
    margin: 0 25px;
    position: relative;
    background-image: url(http://i.imgur.com/w5tV4.jpg);
    background-position: -50px 0; /* -left position -1 x width x sin(30) */
    background-repeat: no-repeat;
    color: #ffffff;
    text-align: center;
    line-height: 173.2px; /*equals height*/
    display: inline-block;
}

Each odd number hex we are going to shift down in relation to the “row” and each even shift up. The shift calculation ( width x cos(30) / 2 ) is also the same as (height / 4).

.hexrow > div:nth-child(odd) {
    top: 43.3px; /* ( width x cos(30) / 2 ) */
}

.hexrow > div:nth-child(even) {
    top: -44.8px; /* -1 x( ( width x cos(30) / 2) + (hexrow bottom margin / 2)) */
}

We are using 2 child div elements to create the “wings” of the hex. They are sized the same as the main hex rectangle, and then rotated, and pushed “below” the main hex. Background-image is inherited so that the image is the same (of course), because the image in the “wings” is going to be “lined up” to that in the main rectangle. The pseudo elements are used to generate the images, because they need to be “rerotated” back to horizontal (since we rotated the parent div of them to create the “wings”).

The :before of the first will translate its background the width of the negative amount equal to the main portion of the hex plus the original background shift of the main hex. The :before of the second will change the origin point of the translation and will shift the main width on the x-axis, and half the height on the y-axis.

.hexrow > div > div:first-of-type {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
    overflow: hidden;
    background-image: inherit;

    -ms-transform:rotate(60deg); /* IE 9 */
    -moz-transform:rotate(60deg); /* Firefox */
    -webkit-transform:rotate(60deg); /* Safari and Chrome */
    -o-transform:rotate(60deg); /* Opera */
    transform:rotate(60deg);
}

.hexrow > div > div:first-of-type:before {
    content: '';
    position: absolute;
    width: 200px; /* width of main + margin sizing */
    height: 100%;
    background-image: inherit;
    background-position: top left;
    background-repeat: no-repeat;
    bottom: 0;
    left: 0;
    z-index: 1;

    -ms-transform:rotate(-60deg) translate(-150px, 0); /* IE 9 */
    -moz-transform:rotate(-60deg) translate(-150px, 0); /* Firefox */
    -webkit-transform:rotate(-60deg) translate(-150px, 0); /* Safari and Chrome */
    -o-transform:rotate(-60deg) translate(-150px, 0); /* Opera */
    transform:rotate(-60deg) translate(-150px, 0);

    -ms-transform-origin: 0 0; /* IE 9 */
    -webkit-transform-origin: 0 0; /* Safari and Chrome */
    -moz-transform-origin: 0 0; /* Firefox */
    -o-transform-origin: 0 0; /* Opera */
    transform-origin: 0 0;
}

.hexrow > div > div:last-of-type {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -2;
    overflow: hidden;
    background-image: inherit;

    -ms-transform:rotate(-60deg); /* IE 9 */
    -moz-transform:rotate(-60deg); /* Firefox */
    -webkit-transform:rotate(-60deg); /* Safari and Chrome */
    -o-transform:rotate(-60deg); /* Opera */
    transform:rotate(-60deg);
}

.hexrow > div > div:last-of-type:before {
    content: '';
    position: absolute;
    width: 200px; /* starting width + margin sizing */
    height: 100%;
    background-image: inherit;
    background-position: top left;
    background-repeat: no-repeat;
    bottom: 0;
    left: 0;
    z-index: 1;

    /*translate properties are initial width (100px) and half height (173.2 / 2 = 86.6) */
    -ms-transform:rotate(60deg) translate(100px, 86.6px); /* IE 9 */
    -moz-transform:rotate(60deg) translate(100px, 86.6px); /* Firefox */
    -webkit-transform:rotate(60deg) translate(100px, 86.6px); /* Safari and Chrome */
    -o-transform:rotate(60deg) translate(100px, 86.6px); /* Opera */
    transform:rotate(60deg) translate(100px, 86.6px);

    -ms-transform-origin: 100% 0; /* IE 9 */
    -webkit-transform-origin: 100% 0; /* Safari and Chrome */
    -moz-transform-origin: 100% 0; /* Firefox */
    -o-transform-origin: 100% 0; /* Opera */
    transform-origin: 100% 0;
}

This span houses your text. The line-height is reset to make the lines of text normal, but the vertical-align: middle works since the line-height was larger on the parent. The white-space is reset so it allows wrapping again. The left/right margin can be set to negative to allow the text to go into the “wings” of the hex.

.hexrow > div > span {
    display: inline-block;
    margin: 0 -30px;
    line-height: 1.1;
    vertical-align: middle;
    white-space: normal;
}

You can individual target rows and cells in those rows to change images, or span text settings, or opacity, or accommodate a larger image (to shift it to the place you want), etc. That is what the following do for the second row.

.hexrow:nth-child(2) > div:nth-child(1) {
    background-image: url(http://i.imgur.com/7Un8Y.jpg);
}

.hexrow:nth-child(2) > div:nth-child(1) > span {
    /*change some other settings*/
    margin: 0 -20px;
    color: black;
    font-size: .8em;
    font-weight: bold;
}

.hexrow:nth-child(2) > div:nth-child(2) {
    background-image: url(http://i.imgur.com/jeSPg.jpg);
}

.hexrow:nth-child(2) > div:nth-child(3) {
    background-image: url(http://i.imgur.com/Jwmxm.jpg);
    /*you can shift a large background image, but it can get complicated
    best to keep the image as the total width (200px) and height (174px)
    that the hex would be.
    */
    background-position: -150px -120px;
    opacity: .3;
    color: black;
}

.hexrow:nth-child(2) > div:nth-child(3) > div:before {
    /*you can shift a large background image, but it can get complicated
    best to keep the image as the total width (200px) and height (174px)
    that the hex would be.
    */
    background-position: -100px -120px; /* the left shift is always less in the pseudo elements by the amount of the base shift */
}

.hexrow:nth-child(2) > div:nth-child(4) {
    background-image: url(http://i.imgur.com/90EkV.jpg);
    background-position: -350px -120px;
}

.hexrow:nth-child(2) > div:nth-child(4) > div:before {
    background-position: -300px -120px;
}

Answer by Starx

I will provide a simple demo of how to create a hexagonal shape.

HTML

<div class="hex">
    <div class="box">
    </div>
</div>

CSS

.hex {
    width: 40px;
    height: 70px;
    margin: 20px;
    overflow: hidden;
}
.hex .box {
    -moz-transform: rotate(45deg);
    background: #f00;
    width: 50px;
    height: 50px;
    display: inline-block;
    margin: 10px -5px 10px -5px;
}
Read more

html mouse over event, can't show dialog

Question by user595234

I want to show a tooltip based on mouse over event . I have tried this code, but failed, nothing will show up.

Please advise.

<img onmouseover="showLongText();" BORDER=0 height=15 width=15 src="images/pick-button.gif"/>
<div id="longTextDiv" style="display:none;">
ddd
</div>

<script type="text/javascript">
function showLongText(className, fldName, objId){   
    var longTextDiv = $("#longTextDiv");
    //alert(longTextDiv);
    longTextDiv.style.leftPos += 10;
    longTextDiv.style.posLeft = event.clientX;
    longTextDiv.style.posTop = event.clientY;
    longTextDiv.style.display = "";
    longTextDiv.setCapture();           
}
</script>

Answer by Starx

Since you are already using jQuery. You should use

$("img").hover(function(e) {
    var longTextDiv = $("#longTextDiv");
    //alert(longTextDiv);
    longTextDiv.css({
       left : e.pageX,
       top : e.pageY, 
       display : "block"
    });
    longTextDiv.setCapture();           
});
Read more

How Remember Me works with Restful Web Service

Question by yogesh prajapati

I am working in Java,Restful Web Services at Server side and Jsp,javascript at Client side

I want to know how Remember me works

I have doing analysis so many sites and i am getting one thing is that they dont use Cookie for this purpose.

Answer by Starx

Remember Me works with Cookies. You dont need to use REST for this.

Check this answer from Balcus to see how.

Read more

How to make youtube embed code HTML valid for w3c validator?

Question by Shree

Here is my code.

<div>
    <object>
        <param name="movie" value="http://www.youtube.com/v/Cbspv1ZKR8o?version=3&amp;hl=en_US" />
        <param name="allowFullScreen" value="true" />
        <param value="transparent" name="wmode" />
        <param name="allowscriptaccess" value="always" />
        <embed src="http://www.youtube.com/v/Cbspv1ZKR8o?version=3&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" class="cssVideo"></embed>
    </object>
</div>

But the above code failed to pass the W3C Validator.
I search and try to follow these guides but I am unable to solve the problem.

Can anyone help me with this? Thanks.

Answer by Starx

Actually, you don’t need to always use the embeded code provided from youtube.

Below is a typical way to change the embedded version of the youtube and validate it as well.

<object type="application/x-shockwave-flash" width="425" height="350" data="http://www.youtube.com/v/n">
     <param name="movie" value="http://www.youtube.com/v/n" />
     <a href="http://get.adobe.com/flashplayer/" target="blank"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get flash player to play to this file" width="88" height="31" /></a><br />
</object>

Where n is the youtube id of the video.

See a demo validation result

Cheers Prabhu 🙂

Read more

Surrounding DIV background-color and inline-block

Question by JohnDoe

If I set the background color for a div containing divs which have display:inline-block set, the background color is not used.

JSFiddle links below.

Consider this snippets:

CSS:

.divisionOutputArea {
  background-color: yellow;
}
.divisionColumn[data-division=true][data-boxed=true] {
  border: 1px solid black;
  display: inline-block;
  float: left;
}
.divisionColumn[data-division=true] {
  display: inline-block;
  float: left;
}
.divisionColumn[data-result=true] {
  display: inline-block;
  float: left;
}

And this HTML:

<div class="divisionOutputArea">
    <div class="divisionColumn" data-division="true" data-boxed="true">
        1<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-division="true" data-boxed="true">
        2<br />2<br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-division="true" data-boxed="true">
        3<br />3<br />3<br /><br /><br />
    </div>
    <div class="divisionColumn" data-division="true" data-boxed="true">
        1<br /><br />1<br />1<br /><br />
    </div>
    <div class="divisionColumn" data-division="true" data-boxed="true">
        2<br /><br /><br />2<br />2<br />
    </div>
    <div class="divisionColumn" data-result="true">
        :<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        5<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        =<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        2<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        4<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        6<br /><br /><br /><br /><br />
    </div>
    <div class="divisionColumn" data-result="true">
        2<br /><br /><br /><br /><br />
    </div>
</div>

I would like to render the background of the used area of <div class="divisionOutputArea">[Contained divs as of example]</div> e.g. yellow.

Tried it on FF11 and Webkit. Cross-browser is not of an issue.

Not working:
http://jsfiddle.net/8BVZB/

Somewhat working, but not what I want:
http://jsfiddle.net/d6bM6/

Answer by Starx

The inner divs are float, so you need the clear the float.

A very easy way is to give overflow: hidden to the wrapping element. Demo

.divisionOutputArea {
  background-color: yellow;
  overflow: hidden;
}

However, a safe way is to give

<div style="clear: both;"></div>

at the end of wrapping element

Read more
April 8, 2012

Line Text with Nav Bar in HTML

Question by Eric

I have a navbar right under the title to the site, but I want to be line up the first and last items in the navbar with the beginning and end of the Title. I don’t have a live preview, but I attached an image. I can get it to line up in one browser, but when I open it in the other, its off again. Is there an easy way to line the text up so it works for everything? thank you

screenshot

HTML:

<body onload="play()">
    <div class="heading">UPRISING</div>
    <div class="container_12"> 
       <div id="topnav" align="center">
         <ul id="list-nav">
           <li><a href="home.html">HOME</a></li>
           <li><a href="about.html">ABOUT</a></li>
           <li><a href="trailer.html">TRAILER</a></li>
           <li><a href="stills.html">STILLS</a></li>
           <li><a href="news.html">NEWS</a></li>
           <li><a href="contact.html">CONTACT</a></li>
           <!-- <li><a href="distribution.html">DISTRIBUTION</a></li> -->
         </ul>
       </div>

<!-- START OF CONTAINER -->

  <div class="trailer">
    <img id="imgHolder" />    
  </div>
 </div> <!-- END OF CONTAINER 12 --> 

CSS:

#topnav li {
  margin-right: 110px;
}

#topnav li:nth-last-child(1) {
  margin-right: 0px;
}

Answer by user934278

You can work with text-align: justify

See a demo

Answer by Starx

This is a kind of problem which call for good old tables.

<table id="list-nav">
     <tr>
           <td><a href="home.html">HOME</a></td>
           <td><a href="about.html">ABOUT</a></td>
           <td><a href="trailer.html">TRAILER</a></td>
           <td><a href="stills.html">STILLS</a></td>
           <td><a href="news.html">NEWS</a></td>
           <td><a href="contact.html">CONTACT</a></td>          
     </tr>
</table>

The widths will be evenly distributed problem solved.

Or…

#list-nav li:last-child { text-align: right; }
Read more

PHP include doesn't work

Question by Chris

I’m not sure how simple is this to solve, but I assume I’m doing something wrong. I’m new to PHP, so bear with me, please.

When I started learning PHP, I always placed all my project files into the same folder along with index.php and thus included everything like this:

<?php
include('./translation.php');
?>

Later on in the process of learning as I gained experience and my skill increased, I had to start using folders and place my files into sub folders. I ended up successfully including my files with the following:

<?php
include('../translation.php');
?>

My trouble-free coding took an unexpected turn when I decided to start using sub-sub folders. After placing all the files even deeper into the file structure I was shocked to find out that I cannot include them anymore, using:

<?php
include('.../translation.php');
?>

Now I’m lost. What did I do wrong? Am I to understand that I cannot include files deeper than 2 directories in the project? Should I start using a different file system?

Answer by Starx

You got the concept incorrect.

  • . represents current directory
  • .. represents parent directory

These are the way a file system keeps in track of the file and browse depth.

But you cannot use ... to change the directory. As you go deeper, you have to use

For example:

include('../../translation.php');

includes the files, two level outside of current directory. Which is probably what you were trying to do.

Read more

Most efficient way to LIMIT results in a JOIN?

Question by johnnietheblack

I have a fairly simple one-to-many type join in a MySQL query. In this case, I’d like to LIMIT my results by the left table.

For example, let’s say I have an accounts table and a comments table, and I’d like to pull 100 rows from accounts and all the associated comments rows for each.

Thy only way I can think to do this is with a sub-select in in the FROM clause instead of simply selecting FROM accounts. Here is my current idea:

SELECT a.*, c.* FROM 
    (SELECT * FROM accounts LIMIT 100) a
    LEFT JOIN `comments` c on c.account_id = a.id
    ORDER BY a.id

However, whenever I need to do a sub-select of some sort, my intermediate level SQL knowledge feels like it’s doing something wrong.

Is there a more efficient, or faster, way to do this, or is this pretty good?

By the way…

This might be the absolute simplest way to do this, which I’m okay with as an answer. I’m simply trying to figure out if there IS another way to do this that could potentially compete with the above statement in terms of speed.

Answer by Priyank

Looks perfect to me.

Just wondering if it is ok with you to receive less or more than 100 rows from the above query.
Because if for a particular row of accounts table there are no rows in comments table then you would get less than 100 rows. Similarly if there are multiple matching rows in comments table for a row in accounts table then you may get more than 100 rows.

See: How can I optimize this query, takes more than a min to execute

Answer by Starx

No, The query is fine, just the way it is. You might want to filter the fields instead of a.* and c.* though.

Read more
...

Please fill the form - I will response as fast as I can!