Shopify themes, liquid, logos, and UX
Hi everyone,
I'm using the Dawn theme (v15.1.0) and would like to display different banner images for desktop and mobile views on my homepage. Is there a way to set different banner images based on the device being used?
Any help or guidance on how to achieve this in Dawn would be greatly appreciated!
Thanks in advance!
Like if ios vs if android?
If you want to take it a step further and detect specific devices like iPhone or Android, you can use JavaScript to identify the device and then display the appropriate banner. Here’s how you can do that:
Add your banners for desktop, iPhone, and Android in your *.liquid (relevant section/snippet file). Give them unique classes such as .desktop-banner, .iphone-banner, and .android-banner.
Hide the banners by default using CSS:
<style>
.iphone-banner, .android-banner {
display: none;
}
@media only screen and (max-width: 767px) {
.desktop-banner {
display: none;
}
}
</style>
<div class="banner-container">
<!-- Desktop Banner -->
<img src="{{ 'desktop-banner-image.jpg' | asset_url }}" alt="Desktop Banner" class="desktop-banner" />
<!-- iPhone Banner -->
<img src="{{ 'iphone-banner-image.jpg' | asset_url }}" alt="iPhone Banner" class="iphone-banner" />
<!-- Android Banner -->
<img src="{{ 'android-banner-image.jpg' | asset_url }}" alt="Android Banner" class="android-banner" />
</div>
document.addEventListener('DOMContentLoaded', function() {
function detectDevice() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var desktopBanner = document.querySelector('.desktop-banner');
var iphoneBanner = document.querySelector('.iphone-banner');
var androidBanner = document.querySelector('.android-banner');
// Hide all banners initially
desktopBanner.style.display = 'none';
iphoneBanner.style.display = 'none';
androidBanner.style.display = 'none';
// Detect iPhone
if (/iPhone|iPad|iPod/i.test(userAgent)) {
iphoneBanner.style.display = 'block';
}
// Detect Android
else if (/Android/i.test(userAgent)) {
androidBanner.style.display = 'block';
}
// Default to Desktop
else {
desktopBanner.style.display = 'block';
}
}
// Run on page load
detectDevice();
});
This JavaScript will check the user's userAgent to determine if they are on an iPhone or an Android device and show the corresponding banner.
Let me know if you need further assistance or adjustments!
Remember, this is just an example, use the example for your specific use case since IDK what your code looks like. I hope it makes sense
Thank you for your response.
However, I don't have an index.liquid file in my Dawn theme. And where exactly should I place the JavaScript code?
Do you have coding experience? it would likely be the theme.liquid file. It is an example for you to use the JS in the file you wish to modify.
I wrote:*.liquid (relevant section/snippet file)
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024