我一直在試圖擴大使用砌體。 這個想法是,當用戶將鼠標懸停在一個盒子(div)上並等待該div 2秒鐘時,該腳本應該調用一個可以擴展div的函數。jQuery settimeout()無法正常工作
它的全部工作除了腳本不等待2秒鐘,但立即執行該功能。 我在尋找更多關於setTimeOut()函數的信息。但我相信我已經正確實施了它。有人可以幫幫我嗎?
HTML:
<!DOCTYPE html>
<html class=''>
<head>
<meta charset='UTF-8'>
<meta name="robots" content="noindex">
<link rel="stylesheet" type="text/css" href="responsive_masonry.css" />
<!-- <link rel="stylesheet" type="text/css" href="fluid_responsive_masonry.css" /> -->
</head>
<body>
<h1>Masonry - animate item size with jQuery</h1>
<div class="masonry">
<!-- <div class="grid-sizer"></div> -->
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
<div class="item">
<div class="item-content"></div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='http://masonry.desandro.com/masonry.pkgd.js'></script>
<script src="new_layout.js"></script>
<!-- <script src="layout.js"></script> -->
</body>
</html>
JS:
var timer = 0;
var delay = 2000;
var $container = $('.masonry').masonry({
columnWidth: 60
});
$(function() {
$container.on('mouseenter', '.item-content', function() {
timer = setTimeout(ToggleExpansion($(this)), delay);
});
$container.on('mouseleave', '.item-content', function() {
clearTimeout(timer);
});
$container.on('click', '.item-content', function() {
if ($(this).parent('.item').hasClass('is-expanded')) {
ToggleExpansion ($(this));
}
else {
$container.children().removeClass('is-expanded');
ToggleExpansion ($(this));
}
});
});
function ToggleExpansion (thisObj) {
if (!$(this).parent('.item').hasClass('is-expanded')) {
$container.children().removeClass('is-expanded');
thisObj.parent('.item').toggleClass('is-expanded');
}
$container.masonry();
}
CSS:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.masonry {
background: #EEE;
max-width: 640px;
}
.masonry .item {
float: left;
}
/* item is invisible, but used for layout */
.item,
.item-content,
.grid-sizer {
width: 60px;
height: 60px;
}
.item {
border: none;
background: transparent;
}
/* item-content is visible, and transitions size */
.item-content {
width: 60px;
height: 60px;
background: #D26;
border: 2px solid #333;
border-color: hsla(0, 0%, 0%, 0.5);
border-radius: 5px;
-webkit-transition: width 0.4s, height 0.4s;
-moz-transition: width 0.4s, height 0.4s;
-o-transition: width 0.4s, height 0.4s;
transition: width 0.4s, height 0.4s;
}
.item:hover .item-content {
border-color: white;
background: #A2C;
cursor: pointer;
}
/* both item and item content change size */
.item.is-expanded,
.item.is-expanded .item-content{
width: 180px;
height: 120px;
}
.item.is-expanded {
z-index: 2;
}
.item.is-expanded .item-content {
background: #F90;
}
*「jQuery settimeout()不能正常工作」*'setTimeout'不是一個jQuery的東西。這是一個瀏覽器的東西。 – 2014-08-27 09:44:56