CSS onmouseover gallery with php loop - php

I once created an image gallery using CSS which did the following:
Created a thumbnail gallery
Created a div where in there was placed 1x1 pixel images
on mouseover of thumbnails, these 1x1 pixel images expanded to fit the div size, with height being relative to length.
My code so far:
php:
echo '<div id="thumbnails">';
$files = glob("11-09-2012/*.*");
for ($i=1; $i<count($files); $i++)
{ //creating thumbnails
$num = $files[$i];
echo '<img src="'.$num.'" height="50px" id="thumb'.$files[$i].'"></img>';
};
echo '</div><div id="gallery">';
for ($i=1; $i<count($files); $i++)
{ //creating 1x1
$num = $files[$i];
echo '<img STYLE="position:absolute" src="'.$num.'" height="1px" width="1px" id="img'.$files[$i].'"></img>';
};
echo '</div>';
CSS:
#gallery {
margin: 0 auto;
border-style: solid;
border-width: 3px;
border-color: #fff;
width: 800px;
height: 600px;
}
Now I'm not sure where to go - Any help is appreciated - alternative ways to do this, is as well.
Best regards - Jesper

A naïve programmer might recommend jQuery, or attach an event to each individual image. Here's how the big boys do it:
(function() {
var box = document.getElementById('thumbnails'),
handler = function(e) {
e = e||window.event;
var tar = e.target || e.srcElement,
type = e.type, id = tar.id, m, img;
if( (m=id.match(/^thumb(.*)$/)) && (img=document.getElementById('img'+m[1]))) {
img.style.height = img.style.width = type == "mouseover" ? "auto" : "1px";
}
};
if( typeof box.attachEvent != "undefined") {
box.attachEvent('onmouseover',handler);
box.attachEvent('onmouseout',handler);
}
else {
box.addEventListener('mouseover',handler);
box.addEventListener('mouseout',handler);
}
})();
The only change you need to make to your HTML is to remove the width="1px" height="1px" from your images and instead add width:1px;height:1px;max-width:800px;max-height:600px; to the image's style. This would be better done in the CSS file:
#gallery>img {
position: absolute;
left: 0; top: 0;
width: 1px; height: 1px;
max-width: 800px;
max-height: 600px;
}

http://jsfiddle.net/yDUMT/
The jsfiddle shows the onMouseOver being used with a javascript function to enlarge the 1px when the thumbnail is mouse overed, and the onMouseOut being used with a javascript function to shrink the enlarged 1px image back down when the mouse is moved off the thumbnail.

Related

Border around active image in a gallery using css and jquery

I am rendering thumbnail images from a directory as horizontal gallery using PHP. I am trying to put a border around the clicked image by setting the image to active, which is not working. The following is the html and css.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<style>
#loaded_img_panel {
display:flex;
flex-wrap: nowrap;
}
#loaded_img_panel > ul > li {
list-style: none;
}
#loaded_img_panel ul li img {
display: inline;
width: 210px;
height:175px;
margin: 0;
padding:0;
cursor: pointer;
}
#loaded_img_panel img:active {
border: 0.4em solid red;
}
#loaded_img_panel img:hover {
opacity: 0.5;
border: 0.4em solid red;
}
</style>
</head>
<body>
<div class="loaded_img_panel" id="loaded_img_panel">
</div>
</body>
</html>
<script>
window.addEventListener('load',function(e) {
var folder = "thumbnails";
if (folder !== null && folder !== "") {
$.post("loadimages.php", {'folder' : folder}, function(output){
$("#loaded_img_panel").html(output).show();
});
}
});
//Put border around the selected image
$('#loaded_img_panel').on('click', 'img', function() {
$('#loaded_img_panel img').removeClass('active');
$(this).addClass('active');
})
</script>
The following is the php script:
loadimages.php
<?php
session_start();
$folder = $_POST['folder'];
$tardir = "projects/" . $folder . "/thumb/*.jpg" ;
$files = glob($tardir);
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
$filname = basename($num, ".jpg");
$filnam = substr($filname, -5);
$filnam = rtrim($filnam);
echo '<ul class="item">';
echo '<li><img src="'.$num.'" id="thumbNails'.$filnam.'"/>';
echo '<figcaption class="caption" name="caption">' . $filnam . '</figcaption>';
echo '</li></ul>';
}
?>
The php renders images from the directory and the css sets it to horizontal gallery. On hover and onclick I am able to see the red border, but when I release the mouse the box disappears.
I tried, changing the click path from img to #loaded_img_panel > ul > li > img and similar other variations but they were not working.
You need to change #loaded_img_panel img:active to #loaded_img_panel img.active. That way once you assign the active class to the image it will remain highlighted instead of just while you click on it (which is what :active) means.

How to crop a image to have 1:1 aspect ratio CSS

I want to show in a page all images from a specific file. I make it but I can't crop it like to show 4 images on a row and the heigh be the same like width.
This is php code for show images from file:
<?php
$files = glob("galery/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img class="img-display" src="'.$num.'" alt="random image">';
}
?>
This is CSS:
.img-display {
width: 25%; /* This make photos to aling 4 on a row*/
border: solid 5px white; /* This is for make a white spece between photos */
}
I want to show the imeges all at the same aspect ratio. Because if I have a portrait image, the heigh from this image is biger than the others.And if I give "max-heigh" is resizing the image and doesn't look good...
I try with clip but I can't make something good.
I reserch to google but can't find anything good for me.
You can use this:
.img-cropped {
object-fit: cover;
object-position: center center;
width: 250px;
height: 250px;
}
And it's browser support table: https://caniuse.com/#feat=object-fit
(thanks #giorgio)
This will display the image in 250x250 dimensions and the image will cover the container with center as the resize origin.
If you don't know the exact dimensions of the image, and will be using percentage as the width, then you need some javascript to equalize width and height's of the images.
For example:
var cats = document.querySelectorAll(".img-cropped");
var width = cats[0].clientWidth;
console.log(width);
for (var i = 0; i < cats.length; i++) {
cats[i].style.height = width + "px";
}
.container {
width: 80%;
margin: 0 auto;
}
.img-cropped {
object-fit: cover;
object-position: center center;
width: 24%;
}
<div class="container">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
</div>
This works! Thanks a lot for your help!
I leave here what I maked.
HTML/PHP:
<div class="galery-cont">
<?php
$files = glob("galery/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img class="img-cropped" src="'.$num.'" alt="random image">';
}
?>
</div>
<script>
var cats = document.querySelectorAll(".img-cropped");
var width = cats[0].clientWidth;
console.log(width);
for (var i = 0; i < cats.length; i++) {
cats[i].style.height = width + "px";
}
</script>
CSS:
.galery-cont {
padding-top: 210px;
width: 100%;
height: auto;
background: white;
}
.img-cropped {
border: solid 5px white;
object-fit: cover;
object-position: center center;
width: 25%;
}
But here I have another question...:) How can I make when I click on image(random one from the list) to open a slideshow like Modal and first image is image that I clicked.

Control audio volume with scrolling

I'm making a website for a school project but i have never worked with coding before - so I'm sorry if i have no clue how to phrase my question properly..
I would like to lower the volume of my audio clip when i scroll down on my website. I'm having a hard time understanding the logic of JavaScript... I kinda want to stick to HTML and CSS as much as possible, but i do have to use jQuery for this, right?
I'm hoping some of you might point me in the right direction.. Thanks in advance!
Take a look at this example, hope may help you starting out (scroll on the video):
$(function() {
var video = $('#myVideo');
var videoEl = video[0];
var delta = 0.1; //you can choose whatever delta ( + delta + volume change speed )
video.on('wheel', function(event) {
event.preventDefault(); //prevent default page scroll;
//check for scroll down
if (event.originalEvent.deltaY > 0 && videoEl.volume - delta >= 0) {
videoEl.volume -= delta;
//check for scroll up
} else if(event.originalEvent.deltaY < 0 && videoEl.volume + delta <= 1) {
videoEl.volume += delta;
}
});
})
html {
padding: 20px 0;
background-color: #efefef;
}
body {
width: 400px;
padding: 40px;
margin: 0 auto;
background: #fff;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.5);
}
video {
width: 400px;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="myVideo" autobuffer controls autoplay>
<source id="mp4" src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" type="video/mp4">
</video>

Div is not rigged onto image and text is not centered properly?

I have a main Div which is the "master" of all divs. This div's width is 70% of the page. Inside this div, is another div that contains an image. This div is called mainImg. It is set to absolute positioning and all the rest of the elements are set to absolute as well. All these elements move absolutely relative to the main div.
Here's my code:
PHP/HTML
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
if ($id <= 3)
{
$images = $rows["image"];
$title = $rows["title"];
echo "<div id=main>";
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "</div>";
}
}
}?>
CSS
body{
position: relative;
}
#main{
position: relative;
width: 70%;
height: auto;
margin: 1.5% auto;
}
#mainImg{
position: absolute;
width: 65%;
}
#mainImg img{
width: 100%;
}
#mainTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 0%;
background-color: rgba(100, 0, 0, 0.7);
}
#mainTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 120%;
}
My problem that I am facing is that the div inside of mainImg (mainTitle), is not properly rigged inside the image. The div is a semi-transparent block that should fit perfectly on the bottom of the image with 25% height. Instead, the block is coming out of the image a bit. The other problem that I am facing is my text inside this mainTitle div. The text is centered, but not aligned in the middle of the div. I am trying to make the text responsive, with percentages, but whenever I resize the text always goes below the div. How do I fix these three problems? (Rigging the div properly, aligning the text, and keeping the text inside the div at all times for other window sizes?
The semi-transparent div is coming off the edge of the image
When browser is resized, this is what happens to the text
<?php
$res = $db->query("SELECT * FROM Articles");
if( $res->num_rows > 0 ) {
echo "<div id='main'>";/* Open main div outside the loop */
while( $rows = $res->fetch_object() ) {
$id = $rows->id;
if ( $id <= 3 ) {
$image = $rows->image;
$title = $rows->title;
if ( $id == 1 ) {
echo "
<div id='mainImg'>
<img src='$image'>
<div id='mainTitle'>
<h2>$title</h2>
</div>
</div>";
}
}
}
echo '</div>';/* close main div */
}
?>

Add margins to images on load?

I'm trying to figure out how to dynamically insert a margin into "mainImage". What I'm trying to do is vertically center the image, which has dynamic height and width (and dynamic client viewports). I can't figure out how to add the margins into the images as they're being loaded from javascript...
I have the following HTML:
<div id="content">
<img id="left" onclick="prevImage('images/52/image_','mainImage',52)" src="icons/arrow_left.png" />
<img id="right" onclick="nextImage('images/52/image_','mainImage',52)" src="icons/arrow_right.png"/>
<img id="mainImage" src="images/52/image_0.jpg" onload="addMargins()" />
</div>
with the following CSS:
div#content{
position: absolute;
margin-left: 20%;
margin-right: 5%;
width: 75%;
height: 95%;
text-align: center;
}
div#content img#left{
position: absolute;
left: 0;
top: 50%;
}
div#content img#right{
position: absolute;
right: 0;
top: 50%;
}
div#content img#mainImage{
max-height: 100%;
max-width: 95%;
}
and the javascript:
var key = 1;
function nextImage(thePath, id, max)
{
if(key == max)
{
key = 1;
}else{
key++;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
function prevImage(thePath, id, max)
{
if(key == 1)
{
key = max;
}else{
key--;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
So my idea was to do something in javascript that basically says:
"var viewport= viewport.height;
var mainImage= mainImage.height;
set marginTop to (viewport-mainImage)/2;
set marginBottom to (viewport-mainImage/2;"
and do that for each image as it loads via the "next/prev" buttons.
I wouldn't say this is the best way to do this. You probably want to look at using 'em's in your css to set the margins rather than doing it inline. The em's will ensure that the margins are relative to the size of the containing element.
However, if you want to do it this way, then you are on the right track. You will need to get a reference to the image and then set the marginTop property of the style.
var elem = document.getElementById(id); //reference to image
elem.style.marginTop = (viewport-mainImage)/2;
elem.style.marginBottom = (viewport-mainImage)/2;
I'm not 100% sure that your image will already have a height, so let me know if this works. If not, I'll check it out when I have a moment. Happy coding.

Categories