酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

jQuery实现:Div用background - image显示图片指定部分的代码揭秘

要使用jQuery设置div的图片并显示图片的某一部分,可以使用以下代码: ```javascript $(document).ready(function() { var imgUrl = '图片地址'; // 替换为你的图片地址 var imgWidth = 100; // 图片宽度 var imgHeight = 100; // 图片高度 var cropX = 50; // 裁剪区域的x坐标 var cropY = 50; // 裁剪区域的y坐标 var cropWidth = 100; // 裁剪区域的宽度 var cropHeight = 100; // 裁剪区域的高度 $('div').css({ 'background-image': 'url(' + imgUrl + ')', 'background-position': -cropX + 'px ' + -cropY + 'px', 'background-size': imgWidth + 'px ' + imgHeight + 'px', 'width': cropWidth + 'px', 'height': cropHeight + 'px' }); }); ``` 这段代码首先设置了div的图片,然后通过调整位置和大小来实现裁剪效果。请将`imgUrl`替换为实际的图片地址,并根据需要调整其他参数。 ######[AI写代码神器 | 284点数解答 | 2024-06-06 11:40:54]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]