以下是一个使用 HTML、CSS 和 JavaScript 实现的少儿英文单词连线页面的示例代码。这个页面会展示一些英文单词和对应的图片,用户可以通过鼠标拖动线条将单词和正确的图片连接起来。

代码思路
  1. HTML 部分:用于创建页面的基本结构,包括单词列表、图片列表和一个用于绘制连线的画布。
  2. CSS 部分:用于设置页面的样式,使单词和图片排列整齐,画布覆盖整个页面。
  3. JavaScript 部分:用于处理鼠标事件,实现连线的绘制和判断连线是否正确。
示例代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>少儿英文单词连线</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }

        .word-list,
        .image-list {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .word,
        .image {
            cursor: pointer;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #fff;
            border-radius: 5px;
        }

        canvas {
            position: absolute;
            top: 0;
            left: 0;
            z-index: -1;
        }
    </style>
</head>

<body>
    <div class="word-list">
        <div class="word" data-word="apple">apple</div>
        <div class="word" data-word="banana">banana</div>
        <div class="word" data-word="cherry">cherry</div>
    </div>
    <div class="image-list">
        <img class="image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/1200px-Red_Apple.jpg"
            data-word="apple" alt="apple" width="100">
        <img class="image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Bananas_white_background.jpg/1200px-Bananas_white_background.jpg"
            data-word="banana" alt="banana" width="100">
        <img class="image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Cherry_Stella.jpg/1200px-Cherry_Stella.jpg"
            data-word="cherry" alt="cherry" width="100">
    </div>
    <canvas id="canvas"></canvas>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const words = document.querySelectorAll('.word');
        const images = document.querySelectorAll('.image');

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        let startElement = null;
        let startX, startY;

        words.forEach(word => {
            word.addEventListener('mousedown', (e) => {
                startElement = word;
                const rect = word.getBoundingClientRect();
                startX = rect.right;
                startY = rect.top + rect.height / 2;
                document.addEventListener('mousemove', drawLine);
                document.addEventListener('mouseup', endLine);
            });
        });

        function drawLine(e) {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(e.clientX, e.clientY);
            ctx.strokeStyle = 'blue';
            ctx.lineWidth = 2;
            ctx.stroke();
        }

        function endLine(e) {
            document.removeEventListener('mousemove', drawLine);
            document.removeEventListener('mouseup', endLine);

            images.forEach(image => {
                const rect = image.getBoundingClientRect();
                if (e.clientX >= rect.left && e.clientX <= rect.right &&
                    e.clientY >= rect.top && e.clientY <= rect.bottom) {
                    if (startElement.dataset.word === image.dataset.word) {
                        ctx.beginPath();
                        ctx.moveTo(startX, startY);
                        ctx.lineTo(rect.left, rect.top + rect.height / 2);
                        ctx.strokeStyle = 'green';
                        ctx.lineWidth = 2;
                        ctx.stroke();
                        alert('连线正确!');
                    } else {
                        alert('连线错误,请重试。');
                    }
                }
            });
            startElement = null;
        }
    </script>
</body>

</html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
代码解释
  1. HTML 部分
  • 创建了两个 div 元素,分别用于显示单词列表和图片列表。
  • 每个单词和图片都有一个 data-word 属性,用于存储对应的单词。
  • 创建了一个 canvas 元素,用于绘制连线。
  1. CSS 部分
  • 使用 flexbox 布局将单词列表和图片列表水平排列,并垂直居中。
  • 设置单词和图片的样式,使其具有鼠标指针效果和边框。
  • 将画布设置为绝对定位,覆盖整个页面。
  1. JavaScript 部分
  • 获取画布和绘图上下文。
  • 监听单词的 mousedown 事件,记录起始点,并开始监听 mousemovemouseup 事件。
  • mousemove 事件中,绘制临时连线。
  • mouseup 事件中,判断连线是否连接到正确的图片,并绘制最终连线。
注意事项
  • 示例中的图片使用了维基百科的公共图片链接,你可以根据需要替换为本地图片或其他图片链接。
  • 代码只是一个简单的示例,你可以根据需要添加更多的单词和图片,以及更复杂的交互效果。