It's probably something silly, however, with the code I have right now, I can edit non spoilered pictures on the fly, so that their thumbnail is changed to the spoiler thumbnail.
$("[src='path/to/original/thumbnail.jpg']").attr('src', "/static/spoiler.png")
however, the new the thumbnail retains the size of the original thumbnail, and my spoiler image looks extremely strange unless it's at the correct size.
How do I set the thumbnails size, after being changed to the spoiler image?
Width needs to be 94px, and height needs to be 128px, for the record.
Depends on how the image originally got its height and width. If via css, then simply change the height and width using JQuery's ".css()" method:
$("[src='/static/spoiler.png']").css('width', '94px').css('height': '128px');
Or if height and width is set via an attribute:
$("[src='/static/spoiler.png']").attr('width', "94px").attr('height', "128px")
Don't set the src as an attribute, set the property directly:
$("[src='path/to/original/thumbnail.jpg']").each(function(){
this.src = "/static/spoiler.png";
});
Related
I'm working on a site that uses Cloudinary and the LazySizes RIaS plugin and for the most part it works fine.
I have my config like this:
window.lazySizesConfig = window.lazySizesConfig || {};
window.lazySizesConfig.rias = window.lazySizesConfig.rias || {
prefix: 'https://res.cloudinary.com/user/image/fetch/q_auto:eco,f_auto,c_scale/w_{width}/',
absUrl: true
}
Most of the images I want to be full width of the column and they're fine. But some of the source images aren't that wide and I don't want them to be full width.
But LazySizes will set the same sizes value on all of the images which is equal to the width of the column which means that some get upscaled and stretched.
Does anyone know of a way, either with a data attribute in the markup or a conditional in the js config, which will allow me to tell LazySizes to not apply the default sizes output for certain images?
It seems if I try and use any value other that auto for data-sizes that LazySizes will fail to run for that image.
I thought I could maybe use the example you used for data-format which does output different values in the srcset but the sizes still equals the width of the column.
It turned out the answer to my particular situation was to add style:max-width: XXpx to the img tag if the width of the image was smaller than the max-width of the parent.
I added CSS to a checkbox and it works just fine.
I use a local file (named check.png) to use as the check background image and crop it inside the checkbox area.
Here are two checkboxes one checked and one unchecked
Now I'd like to know if it's possible, instead of manually setting 25px in the CSS file, to get the background image's real width and height and set the width to background.width and the height to background.height/2.
That would allow to have it automatically resize if I ever change check.png and still work properly
EDIT: I think I was misunderstood. I want the box to fit the background image, not the image to fit the box. I'll try the opposite anyway
EDIT2: Okay Nvm Apparently fitting the background in the box was a better idea than fitting the box in the image. Thanks :D
You don't have to write the exact width and height.
To scale the background image to fit inside its container, you can simply use:
background-size:contain;
set
background-size: 100% 100%;
background-repeat: no-repeat;
Use this javascript to get image width and height:
var img = new Image();
img.onload = function() {
alert(this.width + 'x' + this.height);
}
img.src = 'https://i.stack.imgur.com/4TObz.png';
#Durga's answer fits your need.
However, letting the resource file to decide your DOM component size seems not to be the best practice, doing the opposite way (use css to auto fit the image to your container, as #Koby suggested) is usually preferred.
I have a div container on which i have placed two more div's (say 1 & 2) for placing the content . I have set different ids for the div's and on clicking the link( of div 2) i am changing the background image of the background div. I am trying to set the background-size as cover to occupy the whole screen width but only the upper part of the image is getting displayed. Here is the code which i am using to set the background size for the div.
<script>
$(document).ready(function(){
//autoOpen: false,
$(".span12").css('background-image','url(../images/assorting_2.jpg)','background-size','100%');
<script>
I could have posted the images but i am unable to post it as i have less points.
Try this:
$(".span12").css({
'background-image':'url(../images/assorting_2.jpg) no-repeat',
'background-size':'cover'
});
See this for using multiple jQuery CSS properties. And this for using background cover.
Try like
$(".span12").css({
'background-image':'url(../images/assorting_2.jpg) no-repeat',
'background-size':'100%'
});
My blog posts live in a container that's 600px wide. When I have an image that's wider than 600px, I resize it via CSS (.post img {max-width: 600px})
I'd like users to be able to click on these resized images and see the full size version in a lightbox, but to do this I need to detect in Javascript which images have been thus resized (since I don't want to lightbox images that appear full size inline in the post)
You can check the image element's width property to get the rendered width of the image. If it's 600, the image is most likely to be shrinked. However, the image might originally as well be exactly 600 pixels wide.
If a browser supports the new HTML 5 naturalWidth property, you can get the original image width (in pixels) and compare that with the value of clientWidth.
I don't believe you can in the sense you are speaking as JS is going to read the image it is in the DOM. However what if you set the max-width in the JS:
Just Psuedocode
onload
{
if (img.width > 600px)
{
img.style = max-width: 600px;
img.lightbox();
}
}
Using Javascript to change an 's src... First time the width and height properties are being set correctly. The Second time they don't change. Why?
So I've got this blank image on my page... <img id="imgCropImage" />
So the idea is that every time a user uploads an image, the uploader's UploadComplete callback will set this image's src to the image that has just been uploaded so that it can be cropped. I then use this image's height and width properties in order to create a blown up crop preview.
It works great the first time. The browser or the DOM or something automagically set the img's height and width attributes which I access via the DOM after the image has loaded. It's beautiful.
If the user uploaded the wrong image, I want to allow them to upload a different one. This time however, the img's width and height attributes, accessed via javascript, remain the same as the prior image. The new image displays correctly in it's own width and height, it's just that the tag's properties do not change along with a new src.
Does anyone know why this happens? Or even better, how I could deterministically get the img's height and width upon loading a new src?
I <3 stackoverflow and I <3 you.
What about "removing" the height and width attributes.
$('#imgCropImage').removeAttr('height');
$('#imgCropImage').removeAttr('width');
That should "Reset" it. (If you're using JQuery that is).
Did you try to replace the previous image by a new one, with the new src?
var newImg = document.createElement('IMG');
newImg.src = newSrc;
oldImg.parentNode.replaceChild(newImg, oldImg);