老师,麻烦检查下

来源:2-7 编程练习

豆包侠

2021-10-18 14:12:21

<!DOCTYPE html><html>    <head lang="en">        <meta charset="UTF-8">        <title>background-origin</title>        <style type="text/css">        *{margin: 0;padding: 0;border: none;}        div{            width: 800px;height: 400px;top: 0;left: 0;            border: 20px solid #d58df0;           background: url(http://climg.mukewang.com/582c342b0001fd6507000210.jpg)no-repeat;           background-size: cover;            background-origin: border-box;                    }        div:hover{                       width: 800px;height: 400px;border: 20px solid #d58df0;
            background: url(http://climg.mukewang.com/582c34220001091605000150.jpg)no-repeat 10px 10px;            background-size: cover;            background-origin: padding-box;                     }                       </style>    </head>    <body>        <div></div>            </body></html><!--     老师我原来想这么写 想问问为什么这么写出不来效果    <!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style type="text/css">        *{margin: 0;padding: 0; border: none;}        div{            width: 800px;height: 400px;            border: 20px solid transparent;            background: url(http://climg.mukewang.com/582c342b0001fd6507000210.jpg)no-repeat;            background-size: cover;            background-origin: border-box;        }        div:hover{            width: 800px;height: 400px;            border: 20px solid transparent;            background: url(http://climg.mukewang.com/582c34220001091605000150.jpg)no-repeat 10px 10px;            background-size: cover;            background-origin: padding-box;        }        span{            position: absolute;top: 0;left: 0;            width: 800px;height: 400px;            border: 20px solid #d58df0;        }    </style></head><body>    <div></div>    <span></span></body></html>-->

写回答

1回答

好帮手慕慕子

2021-10-18

同学你好,对于你的问题解答如下:

1、练习要求鼠标移入时,背景图片以内容左上角为原点,如下图所示:

https://img.mukewang.com/climg/616d12aa09c91bb305050101.jpg

建议修改:调整为content-box
https://img.mukewang.com/climg/616d12d7090858e210000322.jpg

2、通过检查元素可以发现,span元素绝对定位后,覆盖在div元素上显示了,如下图所示:

https://img.mukewang.com/climg/616d1322094dcb5c10000949.jpg

所以视觉上是鼠标移入了div区域,实际上鼠标移入的是span元素,导致无法实现效果。

祝学习愉快~

0

0 学习 · 40143 问题

查看课程