wordpress - Using onmouseover to change image size with wp_get_attachment_image involved -
below part of code use display multiple rows of products tiny thumbnails (35x35 pixels) next each product. admin page created, spreadsheet managing products, less space take better.
everything works great, have client "mouseover" view larger image. understand how "onmouseover" and/or work css achieve enlargement of image, that's traditional <img src... >
element.
is possible alter or tap wp_get_attachment_image same effect? can't paste code <img>
element because "not there", speak, type (sorry lack of terminology, assume function rather static code can work with).
or, there way not set array(35,35) , make <li>
size container, make image fill container, , work element image alteration?
if ( $attachments ) { foreach ( $attachments $attachment ) { $thumbimg = wp_get_attachment_image( $attachment->id, array(35,35) ); $productlist .= '<li style="display: inline-block; vertical-align: top; margin: 0px; margin-right: 7px;">' . $thumbimg . '</li>'; } }
i've done before in 2 different ways:
1. display image background image
php: (note i've moved inline css make easier see code changes)
if ( $attachments ) { foreach ( $attachments $attachment ) { $thumbimg = wp_get_attachment_image_src( $attachment->id, array(35,35) ); $productlist .= '<li class="prodimgcontainer" style="background-image:url('.$thumbimg[0] .')"></li>'; } }
css:
.prodimgcontainer{ width: 35px; height: 35px; background-repeat: no-repeat; background-position: center center; background-size: 100%; // inline css: display: inline-block; vertical-align: top; margin: 0px; margin-right: 7px; } .prodimgcontainer:hover{ background-size: 105%; }
2. use overflow:hidden on container element "crop" visible portion of image
php:
if ( $attachments ) { foreach ( $attachments $attachment ) { $thumbimg = wp_get_attachment_image( $attachment->id, array(35,35) ); $productlist .= '<li class="prodimgcontainer">' . $thumbimg . '</li>'; } }
css:
.prodimgcontainer{ width: 35px; height: 35px; overflow: hidden; // inline css: display: inline-block; vertical-align: top; margin: 0px; margin-right: 7px; } .prodimgcontainer img{ width: 35px; } .prodimgcontainer img:hover{ width: 39px; height: 39px; /* move position of image remains centered */ margin-top: -2px; margin-left: -2px; }
this work percentages, images fixed width, used dimensions it's clearer what's happening.
note:
these examples based on work have done, concept valid in both cases , should work, however, i've made code changes above off top of head hasn't been tested.
i found option 1 makes easier work image, if images different sizes or need responsive.
Comments
Post a Comment