Ecommerce software home
Shopping Cart Software Forum for Ecommerce Templates
 
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

Find us on Facebook Follow us on Twitter View our YouTube channel
Search our site
Forum Search
Google Site Search
 All Forums
 Technical
 PHP (Unix / Linux / Apache) versions
 How to use different picture in desktop and mobile
Author « Topic »  

midvalleydrifter001
Ecommerce Template Expert

USA
936 Posts

Posted - 06/12/2020 :  11:21:57  
Hey all,

I have two different pictures (same size) that I want to use but in different views.

Is it possible to use one picture in desktop/laptop view and another different picture in mobile view?

So basically in mobile view the one picture overrides the desktop picture and visa versa.

Patrick



dbdave
ECT Moderator

USA
10468 Posts

Posted - 06/12/2020 :  11:38:37  
Hi Patrick, I think you will need to use javascript to do that.
I actually rearrange my items (detailpagelayout) on my product detail page based on screen width using javascript.
So I already have some of this written.
Send me a message with the details if you want some help with it.

PS - It might also be possible to do it with css and pseudo classes...

David

midvalleydrifter001
Ecommerce Template Expert

USA
936 Posts

Posted - 06/12/2020 :  12:07:07  
Thanks David,

I sent you an email.

Patrick

dbdave
ECT Moderator

USA
10468 Posts

Posted - 06/12/2020 :  20:18:49  
Hi Patrick, I thought I might post this here so others may see the solution.

The first thing is to add an ID tag to the image html there. This will allow us to target the image easily with javascript.
<img id="imgswp" src="images/large-view.jpg"/>

Next, we add some javascript to the page, at the bottom of the page before the closing </body> tag might be a good place.
NOTE - The number there in red is the screen width at which you want to replace the image.
Of course, you need to replace the paths to the images there in the code in green.

<script>
window.addEventListener("load", runimgswap);

function runimgswap() {
var w = window.innerWidth;
if (w <= "425"){
document.getElementById("imgswp").src = "images/small-view.jpg";
} else {
document.getElementById("imgswp").src = "images/large-view.jpg";
}
}
</script>


It's actually quite a simple bit of code, and as a matter of fact, you can accomplish it with less code than that, but the way I written it there allows for real time image swap if a user resizes the window. You can just trigger the function with the following.
On the opening <body> tag for the page, change it like so - <body onresize="runimgswap()">

That will give you the following demo https://www.floridasurpluswarehouse.com/dev/img-change.php although my demo is set to change at 600px.

If you do not want the real time image change, you could just simplify the javascript to
<script>
var w = window.innerWidth;
if (w <= "425"){
document.getElementById("imgswp").src = "images/small-view.jpg";
}
</script>



One last note - This will not work on IE9 and before, and I can add some legacy code to make it friendly to that, but who really uses that these days...

Let me know if you have questions.

Thanks,
David

Edited by - dbdave on 06/12/2020 20:24:20

midvalleydrifter001
Ecommerce Template Expert

USA
936 Posts

Posted - 06/12/2020 :  23:42:43  
David... you are a lifesaver.

This worked exactly like I was hoping.

The ability to have the two different size of pictures made it all fit perfect.

I did change the default swap width to 600px as this worked better.

Thanks again for your effort.

I GREATLY appreciate it and you.

Patrick

dbdave
ECT Moderator

USA
10468 Posts

Posted - 06/13/2020 :  09:48:33  
I'm glad I could help Patrick.

Thanks,
David

Phil
ECT Moderator

United Kingdom
7715 Posts

Posted - 06/14/2020 :  02:58:45  
Dave,
I can put that to good use so I've just nicked that
You can also go down this route using a <picture> tag - https://www.w3schools.com/tags/tag_picture.asp



* Database Migrations and Conversions*
* ASP to PHP Cart Conversions*

*Contact Us*
*Buy The PHP Capture Card Plugin*
*Rate Our Services/View Our Feedback*

dbdave
ECT Moderator

USA
10468 Posts

Posted - 06/14/2020 :  08:01:52  
Good Info Phil.
I See IE doesn't support it though until 13.0 which is like the chrome based version of Edge.

It's too bad Microsoft was so slow to get a decent browser. I do say though that the latest versions of Edge seem to work pretty well.
  « Topic »  
Jump To:
Shopping Cart Software Forum for Ecommerce Templates © 2002-2022 ecommercetemplates.com
This page was generated in 0.03 seconds. Snitz Forums 2000