2017-04-21 68 views
0

一個變量的值我想要做這樣的事情:如何減去jQuery的

var hands_original =$(".sw_3--breit").position(); 
var hands_corrected = hands_original.top - 300px; 

,我想檢索與300像素減去Y座標值。 我會如何正確寫入?與「vh」或「%」等其他值有什麼不同?

回答

0

基本上你試圖用一個字符串來計算一個數字,並且這樣做效果不好。所以計算top與價值,然後串聯"px"

var hands_original =$(".sw_3--breit").position(); 
 
var hands_corrected = (hands_original.top - 300) + "px"; 
 
console.log(hands_corrected); 
 

 

 
$("button").click(function() { 
 
    $(".sw_3--breit").css("top", hands_corrected) 
 
})
.sw_3--breit{ 
 
    position: fixed; 
 
    top: 350px; 
 
    left: 0; 
 
    height: 100px; 
 
    width:100px; 
 
} 
 

 
body {height: 1000px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="sw_3--breit">.sw_3--breit</div> 
 

 
<button>move</button>