JQuery - use css in if if argument

I use vwfor previous divs, now I want to set an alert if the div is moved to the left. Here's the div:

<div id='pagination'>some stuff</div>

Now its width is set to 84vw, which was calculated by the early functions. I want to warn when its margin-left is equal -84vw. I tried this in js but no luck:

if ($('#pagination').css('margin-left') == '-84vw') {
    alert('you're good to go!');
}

Can someone help me with this argument? A real headache: I can't change vwto px.

+4
source share
4 answers

To convert pxand vwcontact this

1px = (100/document.documentElement.clientWidth) vw

, . viewport 500px wide ( 100vw),

1px = (100/500) = 0,2vw

. ,

alert('you\'re good to go!');
+7

. LHS px, RHS vw.

1px = 100/document.documentElement.clientWidth + 'vm';

var onePx = 100 / document.documentElement.clientWidth;
var valPX = $('#pagination').css('margin-left');
var valVW = Math.round( parseInt( valPX.replace(/[^-\d\.]/g, ''), 10 ) * onePx);
if ( valVW == -84) {
    alert('you\'re good to go!');
}
#pagination{
  margin-left:-84vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='pagination'>some stuff</div>
Hide result
0

vw, jQuery.css() pixels

0

.

, vw .

.

//Compute the percent width of the element container
var width = $('#container-of-pagination').width();
var parentWidth = $('#container-of-pagination').offsetParent().width();
var percent = 100*width/parentWidth;
//This will be used for the extra control.
var margin = parseInt( $('#pagination').css('margin-left') );
if ( percent == 84 && margin < 0 ) {
    alert('you\'re good to go!');
}
0

Source: https://habr.com/ru/post/1626955/


All Articles