As part of the recent improvements I’ve been making to various websites I wanted to make images in galleries show enlarged versions when hovered over. बेसिक के साथ ऐसा करना सीएसएस is fairly trivial, but I didn’t just want to expand the original image, मैं चाहता था कि ब्राउज़र एक नया प्राप्त करे grab (larger) image too…
All of the images are already providing a full src-set so all I needed to do was to use a little jQuery (which is already loaded by wordpress anyway) to change the target display size to make the browser pull a larger image. अच्छी बात यह है कि सीएसएस works instantly so you get a larger image using the original file, and then the quality improves a split second later as the higher res file gets loaded.
The सीएसएस
The सीएसएस I used is as follows. मैंने इसे बस अपने विषय के style.css में जोड़ा है (or rather child-theme — always a good idea to use one)
dt.gallery-icon a img.size-थंबनेल:मंडराना { परिवर्तन: स्केल(3.0); }
नोट (1) — the above सीएसएस applies to gallery images only. It is unlikely anyone would want this functionality for all images displayed anywhere on the page, but you might want it for the main content section. You would need to check how your theme structures this, but in my case the main content area is a “section” so the following code would do the job
सेक्शन.एंट्री-कंटेंट p a img.size-थंबनेल:मंडराना { परिवर्तन: स्केल(3.0); }
नोट (2) — This code applies only to thumbnail images. For medium images you would replace “size-thumbnail” with “size-medium”, and for large with “size-large”.
नोट (3) — Because my thumbnails are 120x80px I want to increase them to 3x their original size. I already have a custom image size of 360×240 that wordpress creates for me that is included in the src-set. For hi-dpi screens there are also even larger image sizes that I have (उदा.. 720×480). Creating additional image sizes in wordpress is easy — there are lots of good guides online if you need to do this.
यदि आप 1.5x या 2.0x साधारण परिवर्तन करना चाहते हैं तो 3.0 जैसी इच्छा
jQuery
Add the following to your (बच्चा) विषय की स्क्रिप्ट फ़ाइल (assuming it has one). If it doesn’t have one, create your own, and ‘enqueue’ it with a custom function in the (बच्चा) विषय के कार्य।php
jQuery(डाक्यूमेंट).तैयार(समारोह(){ jQuery("dt.gallery-icon a img.size-थंबनेल").मंडराना(समारोह(){ jQuery(यह).attr("आकार","(अधिकतम चौड़ाई: 360पिक्सल) 100वीडब्ल्यू, 360पिक्सल"); }, समारोह(){ jQuery(यह).attr("आकार","(अधिकतम चौड़ाई: 120पिक्सल) 100वीडब्ल्यू, 120पिक्सल"); }); //उपरोक्त दोहराएं 5 प्रत्येक भिन्न छवि आकार के लिए यहाँ पंक्तियाँ जिन्हें आप संशोधित करना चाहते हैं });
The above code is applying to images with class thumbnail that are found inside any dt with a gallery-icon class — so in otherwords — only to images inside the wordpress built-in gallery structure. On hover the max-width is increased from 120 तक 360 (my . से मिलान करने के लिए 3x वृद्धि सीएसएस!), and then the second part of the function restores the original 120 width when the hover stops.
के साथ के रूप में सीएसएस ऊपर, you can adjust the numbers to change how much the image enlarges, और आप बदल सकते हैं dt.gallery-icon a img.size-thumbnail
section to target a different element or class depending on which images you want to target.
अपनी खुद की स्क्रिप्ट को कतारबद्ध करें
If your theme didn’t have a script file and you need to enqueue your own add the following to your (बच्चा) विषय के कार्य।php (assuming you called your script file image-zoom.js and saved it in the root folder of the theme)
wp_register_script( 'आईएमजी-ज़ूम', get_stylesheet_directory_uri() . '/image-zoom.js', सरणी( 'jquery' ), '', true);
wp_enqueue_script( 'आईएमजी-ज़ूम' );
उपरोक्त आपकी स्क्रिप्ट पंजीकृत करेगा, in the footer, making sure it is placed after jquery (so jquery should already be available when it executes)
छवि आकार जोड़ना
For completeness here’s how to add custom image sizes to wordpress. फिर, इसे अपने में जोड़ें (बच्चा) विषय के कार्य।php
add_image_size( 'चौथाई-चौड़ाई', 192, 144, असत्य );
Above I’ve added a new image-size called “quarter-width” with a maximum width of 192px and a maximum height of 144px. छवि को क्रॉप नहीं किया जाएगा (इसलिए अंत में असत्य).
WordPress will now create images of this size automatically when you upload images (you will need to use a thumbnail rebuild plugin to recreate the thumbnails for already-uploaded images).
This new image size will NOT show up for insertion in the editor though. To make it available also add the following code to your functions.php
समारोह my_custom_image_sizes( $आकार ) { वापसी array_merge( $आकार, सरणी( 'quarter-width' => __('तिमाही चौड़ाई'), ) ); }
मौजूदा सेटिंग्स का सम्मान करना
एक और काम जो आप करना चाहेंगे (to ensure responsive images work well) is to create larger (अर्थात. उच्च डीपीआई) thumbnail sizes that respect the जीयूआई of whether to crop thumbnails or not. To do that use the code below which as you will see is slightly modified from the more basic one above
add_image_size( 'प्रतिक्रिया-अंगूठे-2x', (get_option( 'थंबनेल_साइज_डब्ल्यू' ) == 0 ? 0 : '240'), 160, get_option( 'थंबनेल_क्रॉप' ) );
उपरोक्त कोड के लिए जाँच करता है 2 things — firstly it checks to see if the crop option is set, और अगर यह है, it mirrors it for our new responsive size. यह यह देखने के लिए भी जांचता है कि क्या चौड़ाई . पर सेट है 0 (अर्थात. no maximum) in which case our new image will also have no maximum either. I called the new image size “resp-thumb-2x” as it is a ‘responsive’ copy of the thumbnail that is twice as large (for hi-dpi screens with double the normal pixel density). I have also created a number of other sizes to make a fairly complete set.
Note — Since my original thumbnails were 120×80 I want my new ones to be 240×160 so those are the sizes I have specified (हार्ड कोडित). It would be possible to get both the height and width specified for the original thumbnail and multiply both by 2 to make this function fully generic, but I didn’t do so when I was developing it so I will leave that to you to figure out!
एक अंतिम टिप
अगर, मेरे जैसा, you create your own full set of new image sizes, you may not want wordpress to create it’s own extra hidden sizes. In addition to the original image, and the thumbnail, the medium, और बड़ी छवि, wordpress also create a “post-thumbnail”, a “medium-large”, a 1536×1536 and a 2048×2048. These are disabled in 2 different ways…
हटाने के लिए 1536 तथा 2048 चित्र सीधे-आगे हैं
निकालें_छवि_आकार( '1536x1536' ); //हटाना 1536 we dont need it
remove_image_size( '2048x2048' ); //हटाना 2048 हमें इसकी आवश्यकता नहीं है
The post-thumb and medium-large are slightly more complicated
//remove the built-in medium-large add_filter('मध्यवर्ती_छवि_आकार', समारोह($आकार) { वापसी array_diff($आकार, ['मध्यम बड़ा']); }); //remove the built-in post-thumbnail add_filter('मध्यवर्ती_छवि_आकार', समारोह($आकार) { वापसी array_diff($आकार, ['पोस्ट-थंबनेल']); });
Note that removing the post-thumbnail seems to sometimes have strange effects on the image library in the editor.
एक अंतिम अंतिम नोट
When deciding on image sizes (for larger images, less so thumbnails) you ideally want a size that common aspect ratio images will always resize nicely to. For that reason I recommend the following sizes
आकार | चौड़ाई | ऊंचाई (4:3) | ऊंचाई (16:9) | ऊंचाई (3:2) |
---|---|---|---|---|
त्रिमास | 192 | 144 | 108 | 128 |
तीसरा | 288 | 216 | 162 | 192 |
आधा | 384 | 288 | 216 | 256 |
Medium | 576 | 432 | 324 | 384 |
विपुल | 768 | 576 | 432 | 512 |
विशाल | 1152 | 864 | 648 | 768 |
एक्स्ट्रा लार्ज | 1536 | 1152 | 864 | 1024 |
Note how the widths specified produce whole number heights for all 3 of the most common aspect ratios. आपको वह चौड़ाई के साथ नहीं मिलेगा जैसे 150 or 200.
Also note the pattern in increases. कूद 2 तालिका में पंक्तियाँ और आकार दोगुना हो जाता है! सम पंक्तियाँ (288,576,1152 तथा 1535) are also 1.5x the previous size so these work well on semi-hi-dpi screens using a 50% डीपीआई वृद्धि. के इस सेट का उपयोग करके 7 sizes you have a good range of usable options for most viewports AND a good set of responsive images that wordpress will automatically add to the src-set since they will have consistent aspect ratios with no fractions that would lead to quality loss or cropping.
Currently for thumbnails I tend to either have unlimited width allowed OR I hard crop to a fixed 3:2 ratio so I use whole number sizes for thumbnails but I may eventually tweak my theme and extend the above system backwards by having options 48px and 96px wide (ध्यान दें कि इनके बीच कोई मूल्य नहीं है 2 that would work unfortunately — and since I use 60px wide mini thumbnails I haven’t opted to extend backwards). 72px दोनों के लिए काम करेगा 4:3 तथा 3:2 (और 64px के साथ काम करेगा 4:3 तथा 16:9). Since a majority of my images are taken on my dSLR these are typically 3:2 (the same as the aspect ratio of traditional 35mm film) इसलिए मैं इस आकार का भी उपयोग कर सकता हूं, जो तब बड़े आकार के साथ सभी तरह से काम करेगा, but the larger sizes are not cropped whereas my thumbnails are — I’m happy for larger images to be both landscape and portrait, but I want thumbnails to typically be landscape only to fit with my design, so that’s why I haven’t extended backwards. Hopefully as more and more logos are available as एसवीजीके, और एक बार जेपीईजी-XL निकट भविष्य में ब्राउज़र में आएगा, इसमें से बहुत कुछ हल हो जाएगा.
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”