使用Javascript和HTML5制作拖放排序游戏

时间:2015-11-17 14:20:17

标签: javascript html css html5

我尝试实现我在教科书中找到的用于学习Javascript的游戏。

我希望能够使用HTML5拖放对数字进行排序,以及使用纯Javascript。

我已经尝试按照教程http://www.w3schools.com/html/html5_draganddrop.asp来实现它,但我没有成功。我已经制作了完全相同的方法,但将draggable="true"应用于number-field元素似乎无法正常工作。

有人可以就如何开始提供一些建议 - 如果可能的话,提供一些源代码吗?

我被困了3个多小时,而且我已经能够拖动一个外div元素了。

这是HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>The Fun Sorting Game</title>
        <link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=Raleway:400,100,200,300,500,600,700,800,900" />
        <link type="text/css" rel="stylesheet" href="styles.css" />
    </head>
    <body>
        <h1>The Fun Sorting Game</h1>

        <div id="sorting-game">
            <div class="block">
                <div class="dropable-area">Drop here</div>
                <div class="number-field">1</div>
            </div>
            <div class="block active">
                <div class="dropable-area">Drop here</div>
                <div class="number-field">2</div>
            </div>
            <div class="block">
                <div class="dropable-area">Drop here</div>
                <div class="number-field">3</div>
            </div>
            <div class="block">
                <div class="dropable-area">Drop here</div>
                <div class="number-field">4</div>
            </div>
            <div class="block">
                <div class="dropable-area">Drop here</div>
                <div class="number-field">5</div>
            </div>
            <div class="block edge active">
                <div class="dropable-area">Drop here</div>
            </div>
        </div>
        <p class="status">Games played: <output id="gamecount">0</output></p>

        <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
        <script src="client.js"></script>
    </body>
</html>

和相应的CSS:

body {
    background-color: rgba(94,186,165, 1);
    color: white;
    font-family: "Raleway", sans-serif;
    font-size: 16px;
    font-weight: 100;
}

h1 {
    font-weight: 100;
    letter-spacing: 2px;
    text-align: center;
}

p.status {
    font-style: italic;
    text-align: center;
}

/* ==========================================================================
   Game
   ========================================================================== */

#sorting-game {
    width: 400px;
    margin: 0 auto;
    position: relative;
}

.block {
    padding-top: 5px;
    transition: opacity 0.5s ease-out;
    width: 400px;
}

.block * {
    pointer-events: none;
}

.block.edge {
    padding-top:5px;
    padding-bottom:5px;
}

.block .number-field {
    padding: 10px;
    background-color: rgba(195,213,209,0.5);
    cursor: move;
    margin: 5px 0 0;
    text-align: center;
    text-shadow: 1px 1px 1px #000;
    border-radius: 5px;
    box-shadow:  1px 1px 3px 0px rgba(0, 0, 0, 0.7);
}

.block .dropable-area {
    border-radius: 5px;
    color: #818181;
    cursor: pointer;
    font-style: italic;
    height: 0px;
    line-height: 40px;
    overflow: hidden;
    text-align: center;
    transition: height 0.2s ease-out;
}

.block.active .dropable-area {
    height: 40px;
    margin: 5px 0;
    background: rgba(195,213,209,1);
    border: 3px dashed #818181;
}

0 个答案:

没有答案