Using Media Querys with dynamic Properties in Vue
So one of my pages always has a Div with a Background Image which changes based on Media Querys (I fetch a smaller resolution from my CDN on Phone for example).
Now since I get the Image Url on Page Load I need to set it into CSS, this would be fine for a computed Style property but those don´t support Media Queries.
The only solution I saw yet was some Resize Event Listener but I would prefer to get a clean solution.
Example Code Snippet how it looks in CSS with a predefined Url:
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
}
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jp"") no-repeat center;
}
}
javascript css vue.js vuejs2
add a comment |
So one of my pages always has a Div with a Background Image which changes based on Media Querys (I fetch a smaller resolution from my CDN on Phone for example).
Now since I get the Image Url on Page Load I need to set it into CSS, this would be fine for a computed Style property but those don´t support Media Queries.
The only solution I saw yet was some Resize Event Listener but I would prefer to get a clean solution.
Example Code Snippet how it looks in CSS with a predefined Url:
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
}
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jp"") no-repeat center;
}
}
javascript css vue.js vuejs2
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks likehouse.jpg
then I simply bring that at the end of my CDN string and thats it
– Badgy
Nov 21 '18 at 18:40
add a comment |
So one of my pages always has a Div with a Background Image which changes based on Media Querys (I fetch a smaller resolution from my CDN on Phone for example).
Now since I get the Image Url on Page Load I need to set it into CSS, this would be fine for a computed Style property but those don´t support Media Queries.
The only solution I saw yet was some Resize Event Listener but I would prefer to get a clean solution.
Example Code Snippet how it looks in CSS with a predefined Url:
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
}
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jp"") no-repeat center;
}
}
javascript css vue.js vuejs2
So one of my pages always has a Div with a Background Image which changes based on Media Querys (I fetch a smaller resolution from my CDN on Phone for example).
Now since I get the Image Url on Page Load I need to set it into CSS, this would be fine for a computed Style property but those don´t support Media Queries.
The only solution I saw yet was some Resize Event Listener but I would prefer to get a clean solution.
Example Code Snippet how it looks in CSS with a predefined Url:
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
}
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jp"") no-repeat center;
}
}
javascript css vue.js vuejs2
javascript css vue.js vuejs2
asked Nov 21 '18 at 17:47
BadgyBadgy
1,163421
1,163421
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks likehouse.jpg
then I simply bring that at the end of my CDN string and thats it
– Badgy
Nov 21 '18 at 18:40
add a comment |
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks likehouse.jpg
then I simply bring that at the end of my CDN string and thats it
– Badgy
Nov 21 '18 at 18:40
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks like
house.jpg
then I simply bring that at the end of my CDN string and thats it– Badgy
Nov 21 '18 at 18:40
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks like
house.jpg
then I simply bring that at the end of my CDN string and thats it– Badgy
Nov 21 '18 at 18:40
add a comment |
2 Answers
2
active
oldest
votes
Are your assets included in the build output? If so you should let Webpack deal with creating the paths instead of hardcoding them.
If that's not an option you can take a look at CSS image-set
which is a way to define responsive images outside of media queries. However, browser support isn't great.
Finally, you can use the srcset
attribute in HTML and then hide/show the element based on media queries.
-- Edit:
I forgot that you can actually bind styles in the VUE.js template.
v-bind:style
Here's an example. This is actually changing the style block that gets compiled on build. It's not an inline "computed" style.
-- Edit #2
Use VUE to set a CSS Variable --backgroundImage
this can then be used inside the media queries in your CSS. Someone made a great Codepen example.
CSS
:root {
--backgroundImage: 'blank.png';
}
div {
background-image: var(--backgroundImage);
}
VUE
watch: {
img(val){
element.style.setProperty('--backgroundImage', val)
}
}
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
add a comment |
The easiest solution that cross my mind is to save the windows width on page mount, and add dynamically a class according to the value.
data() {
return {
deviceWidth: 0
}
}
Set the value on component mount hook
created() {
this.deviceWidth = window.innerWidth;
}
And on you template
// handle extra sizes
<element
:class="{'image-ios': deviceWidth < 480, 'image-small': deviceWidth >= 480}"></element>
Notice that you don't need a resizeListener because the resize is not going to happen in real life entering from a smartphone, so the width of the screen in the moment of mounting/creation will be enought
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417859%2fusing-media-querys-with-dynamic-properties-in-vue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are your assets included in the build output? If so you should let Webpack deal with creating the paths instead of hardcoding them.
If that's not an option you can take a look at CSS image-set
which is a way to define responsive images outside of media queries. However, browser support isn't great.
Finally, you can use the srcset
attribute in HTML and then hide/show the element based on media queries.
-- Edit:
I forgot that you can actually bind styles in the VUE.js template.
v-bind:style
Here's an example. This is actually changing the style block that gets compiled on build. It's not an inline "computed" style.
-- Edit #2
Use VUE to set a CSS Variable --backgroundImage
this can then be used inside the media queries in your CSS. Someone made a great Codepen example.
CSS
:root {
--backgroundImage: 'blank.png';
}
div {
background-image: var(--backgroundImage);
}
VUE
watch: {
img(val){
element.style.setProperty('--backgroundImage', val)
}
}
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
add a comment |
Are your assets included in the build output? If so you should let Webpack deal with creating the paths instead of hardcoding them.
If that's not an option you can take a look at CSS image-set
which is a way to define responsive images outside of media queries. However, browser support isn't great.
Finally, you can use the srcset
attribute in HTML and then hide/show the element based on media queries.
-- Edit:
I forgot that you can actually bind styles in the VUE.js template.
v-bind:style
Here's an example. This is actually changing the style block that gets compiled on build. It's not an inline "computed" style.
-- Edit #2
Use VUE to set a CSS Variable --backgroundImage
this can then be used inside the media queries in your CSS. Someone made a great Codepen example.
CSS
:root {
--backgroundImage: 'blank.png';
}
div {
background-image: var(--backgroundImage);
}
VUE
watch: {
img(val){
element.style.setProperty('--backgroundImage', val)
}
}
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
add a comment |
Are your assets included in the build output? If so you should let Webpack deal with creating the paths instead of hardcoding them.
If that's not an option you can take a look at CSS image-set
which is a way to define responsive images outside of media queries. However, browser support isn't great.
Finally, you can use the srcset
attribute in HTML and then hide/show the element based on media queries.
-- Edit:
I forgot that you can actually bind styles in the VUE.js template.
v-bind:style
Here's an example. This is actually changing the style block that gets compiled on build. It's not an inline "computed" style.
-- Edit #2
Use VUE to set a CSS Variable --backgroundImage
this can then be used inside the media queries in your CSS. Someone made a great Codepen example.
CSS
:root {
--backgroundImage: 'blank.png';
}
div {
background-image: var(--backgroundImage);
}
VUE
watch: {
img(val){
element.style.setProperty('--backgroundImage', val)
}
}
Are your assets included in the build output? If so you should let Webpack deal with creating the paths instead of hardcoding them.
If that's not an option you can take a look at CSS image-set
which is a way to define responsive images outside of media queries. However, browser support isn't great.
Finally, you can use the srcset
attribute in HTML and then hide/show the element based on media queries.
-- Edit:
I forgot that you can actually bind styles in the VUE.js template.
v-bind:style
Here's an example. This is actually changing the style block that gets compiled on build. It's not an inline "computed" style.
-- Edit #2
Use VUE to set a CSS Variable --backgroundImage
this can then be used inside the media queries in your CSS. Someone made a great Codepen example.
CSS
:root {
--backgroundImage: 'blank.png';
}
div {
background-image: var(--backgroundImage);
}
VUE
watch: {
img(val){
element.style.setProperty('--backgroundImage', val)
}
}
edited Nov 21 '18 at 19:01
answered Nov 21 '18 at 18:24
Bryce HowitsonBryce Howitson
1,419516
1,419516
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
add a comment |
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
Yes I know of the Style bind but it doesn´t support Media Querys. No the Assets are from my CDN, I get the Image Path in the related Database Document that I fetch on Page load.
– Badgy
Nov 21 '18 at 18:40
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
@Badgy I think I see the problem now. How about setting a CSS variable via VUE? Then you could use the CSS Variable inside the media queries but pass it from the script. Here's an example (not mine).
– Bryce Howitson
Nov 21 '18 at 18:53
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
thank you thats what I was was looking for !
– Badgy
Nov 22 '18 at 6:38
add a comment |
The easiest solution that cross my mind is to save the windows width on page mount, and add dynamically a class according to the value.
data() {
return {
deviceWidth: 0
}
}
Set the value on component mount hook
created() {
this.deviceWidth = window.innerWidth;
}
And on you template
// handle extra sizes
<element
:class="{'image-ios': deviceWidth < 480, 'image-small': deviceWidth >= 480}"></element>
Notice that you don't need a resizeListener because the resize is not going to happen in real life entering from a smartphone, so the width of the screen in the moment of mounting/creation will be enought
add a comment |
The easiest solution that cross my mind is to save the windows width on page mount, and add dynamically a class according to the value.
data() {
return {
deviceWidth: 0
}
}
Set the value on component mount hook
created() {
this.deviceWidth = window.innerWidth;
}
And on you template
// handle extra sizes
<element
:class="{'image-ios': deviceWidth < 480, 'image-small': deviceWidth >= 480}"></element>
Notice that you don't need a resizeListener because the resize is not going to happen in real life entering from a smartphone, so the width of the screen in the moment of mounting/creation will be enought
add a comment |
The easiest solution that cross my mind is to save the windows width on page mount, and add dynamically a class according to the value.
data() {
return {
deviceWidth: 0
}
}
Set the value on component mount hook
created() {
this.deviceWidth = window.innerWidth;
}
And on you template
// handle extra sizes
<element
:class="{'image-ios': deviceWidth < 480, 'image-small': deviceWidth >= 480}"></element>
Notice that you don't need a resizeListener because the resize is not going to happen in real life entering from a smartphone, so the width of the screen in the moment of mounting/creation will be enought
The easiest solution that cross my mind is to save the windows width on page mount, and add dynamically a class according to the value.
data() {
return {
deviceWidth: 0
}
}
Set the value on component mount hook
created() {
this.deviceWidth = window.innerWidth;
}
And on you template
// handle extra sizes
<element
:class="{'image-ios': deviceWidth < 480, 'image-small': deviceWidth >= 480}"></element>
Notice that you don't need a resizeListener because the resize is not going to happen in real life entering from a smartphone, so the width of the screen in the moment of mounting/creation will be enought
answered Nov 21 '18 at 18:27
DobleLDobleL
1,587513
1,587513
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417859%2fusing-media-querys-with-dynamic-properties-in-vue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you post the VUE snippet that generates the image path?
– Bryce Howitson
Nov 21 '18 at 18:26
@BryceHowitson I fetch the Post Document on Page load from my Database, there I have a field called "imgName", which looks like
house.jpg
then I simply bring that at the end of my CDN string and thats it– Badgy
Nov 21 '18 at 18:40