2017-07-28 47 views
0

我試圖用線測驗進行兩對匹配。我在左邊有幾張圖片,右邊有幾張圖片,當你點擊一對圖片時,我需要將它與線條連接起來。它應該適用於任何組合,所以如果我點擊左側的圖像1和右側的圖像3,它們應該用線連接。然後,如果我再次點擊右側的圖像1和左側的圖像2,則應刪除上一行,並且需要創建這兩個圖像之間的新圖像。 HTML片段:用線連接圖像

function lineDistance(x, y, x0, y0){ 
 
    return Math.sqrt((x -= x0) * x + (y -= y0) * y); 
 
}; 
 

 
function drawLine(a, b, line) { 
 
    var pointA = $(a).offset(); 
 
    var pointB = $(b).offset(); 
 
    var pointAcenterX = $(a).width()/2; 
 
    var pointAcenterY = $(a).height()/2; 
 
    var pointBcenterX = $(b).width()/2; 
 
    var pointBcenterY = $(b).height()/2; 
 
    var angle = Math.atan2(pointB.top - pointA.top, pointB.left - pointA.left) * 180/Math.PI; 
 
    var distance = lineDistance(pointA.left, pointA.top, pointB.left, pointB.top); 
 

 
// Set Angle 
 
    $(line).css('transform', 'rotate(' + angle + 'deg)'); 
 

 

 
    // Set Width 
 
    $(line).css('width', distance + 'px'); 
 
        
 
    // Set Position 
 
    $(line).css('position', 'absolute'); 
 
    if(pointB.left < pointA.left) { 
 
    $(line).offset({top: pointA.top + pointAcenterY, left: pointB.left + pointBcenterX}); 
 
    } else { 
 
    $(line).offset({top: pointA.top + pointAcenterY, left: pointA.left + pointAcenterX}); 
 
    } 
 
} 
 
new drawLine('.a', '.b', '.line');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="question"> 
 
    <div id="old" class="left_side one_half svg left"> 
 
    <a href="#" data-number="1"><img class="a" src="assets/svg/Kerze.svg"></a> 
 
    <a href="#" data-number="2"><img src="assets/svg/Telefon.svg"></a> 
 
    <a href="#" data-number="3"><img src="assets/svg/Schreibmaschine.svg"></a> 
 
    <a href="#" data-number="4"><img src="assets/svg/TV_old.svg"></a> 
 
    <a href="#" data-number="5"><img src="assets/svg/Zeitstopper.svg"></a> 
 
    <a href="#" data-number="6"><img src="assets/svg/Besen.svg"></a> 
 
    <a href="#" data-number="7"><img src="assets/svg/Waschen.svg"></a> 
 
    </div> 
 
    <div class="left_side one_half svg right"> 
 
    <a href="#" data-letter="NS"><img src="assets/svg/Iwatch.svg"></a> 
 
    <a href="#" data-letter="RT"><img src="assets/svg/Laptop.svg"></a> 
 
    <a href="#" data-letter="TE"><img src="assets/svg/Staubsauger.svg"></a> 
 
    <a href="#" data-letter="IN"><img src="assets/svg/Waschmaschine.svg"></a> 
 
    <a href="#" data-letter="EI"><img src="assets/svg/TV_new.svg"></a> 
 
    <a href="#" data-letter="AL"><img src="assets/svg/Gluehbirne.svg"></a> 
 
    <a href="#" data-letter="BE"><img class="b" src="assets/svg/Iphone.svg"></a> 
 
    <div class="line"></div> 
 
    </div> 
 
</div>

我管理,使兩個圖像(從類B類)之間的線,並且它總是計算爲恰好在直角,但是我不會使它看起來像我上面所描述的那樣工作。有任何想法嗎?謝謝。

回答

0
var setLeft = false, setRight = false; 
$('.leftSide img').click(function(){ 
    $('.leftSide img').removeClass('a'); 
    $(this).addClass('a'); 
    setLeft = true; 
    new drawLine('.a', '.b', '.line'); 
}); 
$('.rightSide img').click(function(){ 
    $('.rightSide img').removeClass('b'); 
    $(this).addClass('b'); 
    setRight = true; 
    new drawLine('.a', '.b', '.line'); 
}); 

你可以用一個標誌變量,當單擊右側的圖像上,設定正確的國旗變量是真實的和做同樣與其他。

然後你的函數裏面drawLine只是檢查,如果這兩個標誌是真,那麼繪製ab之間的界限,並設置兩個標誌變量false

function lineDistance(x, y, x0, y0){ 
 
    return Math.sqrt((x -= x0) * x + (y -= y0) * y); 
 
}; 
 

 
function drawLine(a, b, line) { 
 

 
\t if(setLeft && setRight){ 
 
    \t setLeft = false; 
 
    setRight = false; 
 
    var pointA = $(a).offset(); 
 
    var pointB = $(b).offset(); 
 
    console.log(pointA); 
 
    console.log(pointB); 
 
    var pointAcenterX = $(a).width()/2; 
 
    var pointAcenterY = $(a).height()/2; 
 
    var pointBcenterX = $(b).width()/2; 
 
    var pointBcenterY = $(b).height()/2; 
 
    var angle = Math.atan2(pointB.top - pointA.top, pointB.left - pointA.left) * 180/Math.PI; 
 
    var distance = lineDistance(pointA.left, pointA.top, pointB.left, pointB.top); 
 

 
    // Set Angle 
 
    $(line).css('transform', 'rotate(' + angle + 'deg)'); 
 

 

 
    // Set Width 
 
    $(line).css('width', distance + 'px'); 
 

 
    // Set Position 
 
    $(line).css('position', 'absolute'); 
 
    if(pointB.left < pointA.left) { 
 
     $(line).offset({top: pointA.top + pointAcenterY, left: pointB.left + pointBcenterX}); 
 
    } else { 
 
     $(line).offset({top: pointA.top + pointAcenterY, left: pointA.left + pointAcenterX}); 
 
    } 
 
    } 
 
} 
 
//new drawLine('.a', '.b', '.line'); 
 
var setLeft = false, setRight = false; 
 
$('.leftSide img').click(function(){ 
 
\t $('.leftSide img').removeClass('a'); 
 
\t $(this).addClass('a'); 
 
    setLeft = true; 
 
    new drawLine('.a', '.b', '.line'); 
 
}); 
 
$('.rightSide img').click(function(){ 
 
\t $('.rightSide img').removeClass('b'); 
 
\t $(this).addClass('b'); 
 
    setRight = true; 
 
    new drawLine('.a', '.b', '.line'); 
 
});
.left{ 
 
    float:left; 
 
} 
 
.right{ 
 
    float:right; 
 
} 
 
.one_half{ 
 
    width:40%; 
 
} 
 
img{ 
 
    max-width:100%; 
 
} 
 
.line{ 
 
    background: red; 
 
    height:1px; 
 
} 
 
.question{ 
 
    position: relative; 
 
    overflow: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="question"> 
 
    <div id="old" class="left_side one_half svg left leftSide"> 
 
    <a href="#" data-number="1"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="2"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="3"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="4"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="5"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="6"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-number="7"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    </div> 
 
    <div class="left_side one_half svg right rightSide"> 
 
    <a href="#" data-letter="NS"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="RT"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="TE"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="IN"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="EI"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="AL"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <a href="#" data-letter="BE"><img src="https://www.w3schools.com/css/trolltunga.jpg"></a> 
 
    <div class="line"></div> 
 
    </div> 
 
</div>

+0

感謝answer.That是一個良好的開端,只是有一個問題,即行僅顯示了一對,當我點擊另一對OG圖像,前行消失了,而且,當你點擊左下方圖像右側的圖像時,它的效果很好,當我嘗試將圖像放在左上方的圖像上方時,該線條連接不好。 –