2017-01-01 32 views
5

請告訴我如何以最正確的方式進行此操作。當特定div可見時更改顏色

HTML:

<div id="fixed-red" class="fixed-red"></div> 
<div id="fixed-green" class="fixed-green"></div> 
<div id="fixed-blue" class="fixed-blue"></div> 
<div id="red" class="red"></div> 
<div id="green" class="green"></div> 
<div id="blue" class="blue"></div> 

CSS:

html,body{ 
    height:100%; 
} 
.fixed-red,.fixed-green,.fixed-blue{ 
    width:30px; 
    height:30px; 
    position:fixed; 
    top:10px; 
    left:10px; 
    background:#333; 
} 
.fixed-green{ 
    top:50px; 
} 
.fixed-blue{ 
    top:90px; 
} 
.red-active{ 
    background:#f00; 
} 
.green-active{ 
    background:#0f0; 
} 
.blue-active{ 
    background:#00f; 
} 
.red,.green,.blue{ 
    width:100%; 
    height:100%; 
} 
.red{ 
    background:#900; 
} 
.green{ 
    background:#090; 
} 
.blue{ 
    background:#009; 
} 

我想添加/當用戶開啓/關閉red,​​,或blue的div刪除red/green/blue-active類的fixed-red/green/blue的div(當它們可見時),所以當用戶在其上時,小div將分別用大的顯示div的顏色突出顯示。

謝謝!

+0

工作撥弄我想添加事件'的on',在'red','green'和'藍'大分區,但我不知道如何檢查它們是否可見 – user7362793

回答

4

我不得不稍微調整一下代碼,以便代碼可以更多D.R.Y. 這些類現在被color類所取代。

$.fn.isInViewport = function() { 
 
    var elementTop = $(this).offset().top; 
 
    var elementBottom = elementTop + $(this).outerHeight(); 
 

 
    var viewportTop = $(window).scrollTop(); 
 
    var viewportBottom = viewportTop + $(window).height(); 
 

 
    return elementBottom > viewportTop && elementTop < viewportBottom; 
 
}; 
 

 
$(window).on('resize scroll', function() { 
 
    $('.color').each(function() { 
 
     var activeColor = $(this).attr('id'); 
 
    if ($(this).isInViewport()) { 
 
     $('#fixed-' + activeColor).addClass(activeColor + '-active'); 
 
    } else { 
 
     $('#fixed-' + activeColor).removeClass(activeColor + '-active'); 
 
    } 
 
    }); 
 
});
html, 
 
body { 
 
    height: 100%; 
 
} 
 

 
.fixed-red, 
 
.fixed-green, 
 
.fixed-blue { 
 
    width: 30px; 
 
    height: 30px; 
 
    position: fixed; 
 
    top: 10px; 
 
    left: 10px; 
 
    background: #333; 
 
} 
 

 
.fixed-green { 
 
    top: 50px; 
 
} 
 

 
.fixed-blue { 
 
    top: 90px; 
 
} 
 

 
.red-active { 
 
    background: #f00; 
 
} 
 

 
.green-active { 
 
    background: #0f0; 
 
} 
 

 
.blue-active { 
 
    background: #00f; 
 
} 
 

 
.color { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
#red { 
 
    background: #900; 
 
} 
 

 
#green { 
 
    background: #090; 
 
} 
 

 
#blue { 
 
    background: #009; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="fixed-red" class="fixed-red red-active"></div> 
 
<div id="fixed-green" class="fixed-green"></div> 
 
<div id="fixed-blue" class="fixed-blue"></div> 
 
<div id="red" class="color"></div> 
 
<div id="green" class="color"></div> 
 
<div id="blue" class="color"></div>

這裏是一個

https://jsfiddle.net/BoyWithSilverWings/ds9x55z7/

+0

謝謝你的回答,但我懷疑人們使用'懸停'這種事情 – user7362793

+0

我想你可以使用'mouseenter mouseleave'事件。 U可以看到一個https://jsfiddle.net/BoyWithSilverWings/ds9x55z7/1/的樣本(但的確是懸停功能) –

+0

謝謝,但我再次認爲這不是這樣做的(使用鼠標) 。我可以想'scrollTop',檢查元素的高度,但不知道這是否是正確的方法,以及如何使這 – user7362793