In a recent project, we utilized the Viewer.js library to display images. However, the client requested a customized functionality for printing images.
Therefore, after conducting some research, I have compiled the findings in this article today.
Viewer.js
Viewer.js is an open-source library that essentially functions as a photo album browser. In terms of features, it includes functionalities such as zoom in and out, page navigation, left and right rotation, flipping, and more, making it quite powerful.
Additionally, due to its lack of significant dependencies, the process of porting the library into projects is relatively straightforward.
Let’s get back to the main topic. Let’s first take a look at the originally built-in features.
Alright, now let’s focus on the built-in toolbar below!
Customizing Icons and Functionalities
The client has requested the addition of a print button. So, let’s explore how the original icons are displayed.
Skipping the search process, after examining the viewer.css file, we found that the icons are encoded in Base64! Moreover, they are positioned using a method that involves slicing or cropping.
⬆️ After converting the Base64 to images, you can see all the toolbar buttons here (adjusted the hue for better visibility, as they were initially all white and hard to distinguish).
Now, let’s explore how each functionality is configured.
From the looks of it, adding a new icon seems to require a different approach. Unless you’re willing to modify the existing Base64-encoded images and append another one… which might work, but I’m not that adventurous.
After extensive searching and testing, I finally found a solution. This approach involves using the FontAwesome open-source icon library. I won’t go into the specifics of how to reference it here.
For this instance, I utilized the free version of FontAwesome 6.
Firstly, when calling the Viewer, make sure to explicitly define the toolbar to appear (if not defined, it will include all by default).
In the toolbar configuration, you can hide buttons you don’t want (here, we hide the zoom in and out buttons). Additionally, we’ve added a new “print” functionality (where printPic is a custom JavaScript function).
The next step is to make changes to the viewer.css file.
The .viewer-print class is generated when setting up the toolbar. Therefore, if your functionality is named “abc,” it should be represented as .viewer-abc::before.
Crucially, pay attention to the content in \f019; this is the Unicode encoding for the icon we want to use.
Certainly, you can use different versions of FontAwesome as well, just be mindful of the font-familysetting.
No more talk, let’s take a look at the results!
You can see that the icon appears perfectly! Congratulations on the successful customization!
Conclusion
Viewer.js is a fairly user-friendly image library. The overall calling and usage are quite straightforward, and it might be a good idea to write a dedicated tutorial in the future.
However, issues related to customizations like today’s are often hard to find information about on the internet.
💔 The entire article was written and tested independently. Feel free to quote or reference, but please avoid copying the entire content verbatim.
🧡You can support our team by sharing the post or clicking some ads, Thanks a lot
✅If you haveany problem about this post, please feel free to ask~
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"7fcef07182"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"7fcef07182","uagb_grid_ajax_nonce":"fecd63cf2e"};
( function() {
let elements = document.querySelectorAll( '.uagb-post-grid.uagb-block-efe84f35 .uagb-post-pagination-wrap a' );
elements.forEach(function(element) {
element.addEventListener("click", function(event){
event.preventDefault();
const link = event.target.getAttribute('href').match( /\/page\/\d+\// )?.[0] || '';
const regex = /\d+/; // regular expression to match a number at the end of the string
const match = link.match( regex ) ? link.match( regex )[0] : 1; // match the regular expression with the link
const pageNumber = parseInt( match ); // extract the number and parse it to an integer
window.UAGBPostGrid._callAjax({"btnBorderStyle":"none","block_id":"efe84f35","categories":"215","btnBorderLink":true,"btnBorderRadiusLink":true,"overallBorderLink":true,"overallBorderRadiusLink":true,"inheritFromTheme":true,"postType":"post","postDisplaytext":"No post found!","taxonomyType":"category","postsToShow":6,"enableOffset":false,"postsOffset":0,"displayPostDate":true,"displayPostExcerpt":true,"excerptLength":15,"displayPostAuthor":false,"displayPostTitle":true,"displayPostComment":true,"displayPostTaxonomy":false,"hideTaxonomyIcon":true,"taxStyle":"default","displayPostTaxonomyAboveTitle":"withMeta","displayPostImage":true,"imgSize":"large","imgPosition":"top","bgOverlayColor":"#000000","overlayOpacity":"50","displayPostLink":true,"newTab":false,"ctaText":"Read More","btnHPadding":"","btnVPadding":"","columns":3,"tcolumns":2,"mcolumns":1,"align":"left","width":"wide","order":"desc","orderBy":"date","rowGap":20,"rowGapTablet":20,"rowGapMobile":20,"columnGap":20,"bgType":"color","bgColor":"#f6f6f6","titleTag":"h4","titleFontSize":"","titleFontSizeType":"px","titleFontFamily":"","titleLineHeightType":"em","titleLoadGoogleFonts":false,"metaColor":"","highlightedTextColor":"#fff","highlightedTextBgColor":"#3182ce","metaFontSize":"","metaFontSizeType":"px","metaFontFamily":"","metaLineHeightType":"em","metaLoadGoogleFonts":false,"excerptColor":"","excerptFontSize":"","excerptFontSizeType":"px","excerptFontFamily":"","excerptLineHeightType":"em","excerptLoadGoogleFonts":false,"displayPostContentRadio":"excerpt","ctaBgType":"color","ctaBgHType":"color","ctaFontSize":"","ctaFontSizeType":"px","ctaFontFamily":"","ctaLineHeightType":"em","ctaLoadGoogleFonts":false,"paddingTop":20,"paddingBottom":20,"paddingRight":20,"paddingLeft":20,"contentPadding":20,"ctaBottomSpace":0,"ctaBottomSpaceTablet":0,"ctaBottomSpaceMobile":0,"imageBottomSpace":15,"titleBottomSpace":15,"metaBottomSpace":15,"excerptBottomSpace":25,"excludeCurrentPost":false,"contentPaddingUnit":"px","rowGapUnit":"px","columnGapUnit":"px","excerptBottomSpaceUnit":"px","paginationSpacingUnit":"px","imageBottomSpaceUnit":"px","titleBottomSpaceUnit":"px","metaBottomSpaceUnit":"px","ctaBottomSpaceUnit":"px","paddingBtnUnit":"px","mobilePaddingBtnUnit":"px","tabletPaddingBtnUnit":"px","paddingUnit":"px","mobilePaddingUnit":"px","tabletPaddingUnit":"px","isPreview":false,"taxDivider":", ","titleLetterSpacing":"","titleLetterSpacingType":"px","metaLetterSpacing":"","metaLetterSpacingType":"px","ctaLetterSpacing":"","ctaLetterSpacingType":"px","excerptLetterSpacing":"","excerptLetterSpacingType":"px","useSeparateBoxShadows":true,"boxShadowColor":"#00000070","boxShadowHOffset":0,"boxShadowVOffset":0,"boxShadowBlur":"","boxShadowSpread":"","boxShadowPosition":"outset","boxShadowColorHover":"","boxShadowHOffsetHover":0,"boxShadowVOffsetHover":0,"boxShadowBlurHover":"","boxShadowSpreadHover":"","boxShadowPositionHover":"outset","borderWidth":"","borderStyle":"none","borderColor":"","borderRadius":"","blockName":"post-grid","equalHeight":true,"postPagination":false,"pageLimit":10,"paginationBgActiveColor":"#e4e4e4","paginationActiveColor":"#333333","paginationBgColor":"#e4e4e4","paginationColor":"#777777","paginationMarkup":"","paginationLayout":"filled","paginationBorderColor":"#888686","paginationBorderSize":1,"paginationSpacing":20,"paginationAlignment":"left","paginationPrevText":"\u00ab Previous","paginationNextText":"Next \u00bb","layoutConfig":[["uagb\/post-image"],["uagb\/post-taxonomy"],["uagb\/post-title"],["uagb\/post-meta"],["uagb\/post-excerpt"],["uagb\/post-button"]],"post_type":"grid","equalHeightInlineButtons":false,"imageRatio":"inherit","imgEqualHeight":false,"paginationType":"ajax","extended_widget_opts_block":{},"extended_widget_opts":{},"extended_widget_opts_state":"","extended_widget_opts_clientid":"","dateUpdated":""}, pageNumber, 'efe84f35');
});
});
} )();
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};