关于Object.style.backgroundPosition

本文深入探讨了CSS背景位置属性的定义、用法和语法,通过具体实例展示了如何设置背景图像的位置,并提供了关键值及其含义的详细说明。

SOURCE:http://www.w3schools.com/jsref/prop_style_backgroundposition.asp

 

Definition and Usage

The backgroundPosition property sets or returns the position of a background-image within an element.

Syntax

Set the backgroundPosition property:

Object.style.backgroundPosition="position"

Return the backgroundPosition property:

Object.style.backgroundPosition

Tip: The default value for the backgroundPosition property is: 0% 0%.

ValueDescription
top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
If you only specify one keyword, the other value will be "center".
x% y%The x value indicates the horizontal position and the y value indicates the vertical position. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%.
xpos yposThe x value indicates the horizontal position and the y value indicates the vertical position. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and units
inheritThe setting of the background-position property is inherited from the parent element


 

Example

Example

Change the position of the background-image:

<html>
<head>
<style>
div
{
background-image: url('img_tree.png');
background-repeat: no-repeat;
width: 400px;
height: 400px;
border: 1px solid #000000;
}
</style>
<script type="text/javascript">
function displayResult()
{
document.getElementById("div1").style.backgroundPosition="center bottom";
}
</script>
</head>
<body>

<button type="button" onclick="displayResult()">Position background image</button>
<br />
<div id="div1">
</div>

</body>
</html>

// directives.js import Vue from "vue"; Vue.directive('dialog-drag-resize', { bind(el, binding) { // 拖动功能 const dialogHeader = el.querySelector('.el-dialog__header'); const dialog = el.querySelector('.el-dialog'); let isDragging = false; let startX, startY, startLeft, startTop; dialogHeader.style.cursor = 'move'; dialogHeader.addEventListener('mousedown', (e) => { isDragging = true; startX = e.clientX; startY = e.clientY; startLeft = dialog.offsetLeft; startTop = dialog.offsetTop; dialog.style.position = 'fixed'; dialog.style.margin = '0'; document.body.style.userSelect = 'none'; }); document.addEventListener('mousemove', (e) => { if (!isDragging) return; const dx = e.clientX - startX; const dy = e.clientY - startY; dialog.style.left = `${startLeft + dx}px`; dialog.style.top = `${startTop + dy}px`; }); document.addEventListener('mouseup', () => { isDragging = false; document.body.style.userSelect = ''; }); // 缩放功能 const resizeHandle = document.createElement('div'); // 创建SVG字符串(这里是一个简单的缩放箭头图标) const svgStr = `<svg t="1764837362721" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1786" width="200" height="200"><path d="M852.68743 966.232318 171.311547 966.232318c-62.714867 0-113.562988-50.846038-113.562988-113.558335L57.748558 171.324994c0-62.712297 50.848122-113.558335 113.562988-113.558335l681.376907 0c62.714867 0 113.562988 50.846038 113.562988 113.558335l0 681.348989C966.250418 915.38628 915.40332 966.232318 852.68743 966.232318zM909.469948 171.324994c0-31.356149-25.424061-56.779168-56.781494-56.779168L171.311547 114.545826c-31.357433 0-56.781494 25.423019-56.781494 56.779168l0 681.348989c0 31.357172 25.424061 56.779168 56.781494 56.779168l681.376907 0c31.358457 0 56.781494-25.423019 56.781494-56.779168L909.469948 171.324994zM824.297706 483.610416c-15.665413 0-28.390747-12.697183-28.390747-28.389584l0.887243-186.638771L604.102866 461.264479l-40.145947-40.144302 193.023924-193.016015L568.782006 228.104161c-15.693044 0-28.390747-12.697183-28.390747-28.389584s12.697704-28.389584 28.390747-28.389584l254.711349 0c7.929925 0 15.082105 3.27151 20.238756 8.53949 5.490263 4.657067 8.955319 11.449773 8.955319 19.850094l0 255.506255C852.68743 470.913233 839.989727 483.610416 824.297706 483.610416zM455.219017 852.673983 200.506645 852.673983c-7.929925 0-15.082105-3.270487-20.239779-8.538467-5.489239-4.65809-8.955319-11.423167-8.955319-19.850094L171.311547 568.779168c0-15.692401 12.726357-28.389584 28.390747-28.389584 15.69202 0 28.390747 12.697183 28.390747 28.389584l-0.887243 186.6664 192.690312-192.710047 40.173577 40.143279-193.050531 193.016015 188.198837 0c15.69202 0 28.390747 12.697183 28.390747 28.389584C483.608741 839.9768 470.911038 852.673983 455.219017 852.673983z" fill="#272636" p-id="1787"></path></svg>`; // 将SVG字符串转换为Data URL const svgDataUrl = `url("data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgStr)}")`; resizeHandle.className = 'resize-handle'; Object.assign(resizeHandle.style, { position: 'absolute', top: '0.8rem', right: '3rem', width: '1rem', height: '1rem', backgroundImage: svgDataUrl, backgroundSize: 'contain', backgroundRepeat: 'no-repeat', backgroundPosition: 'center', cursor: 'ne-resize', zIndex: '100' }); dialog.appendChild(resizeHandle); let isResizing = false; let startWidth, startHeight, startCX, startCY; const content = el.querySelector('.el-dialog__body'); resizeHandle.addEventListener('mousedown', (e) => { isResizing = true; startWidth = dialog.offsetWidth; startHeight = dialog.offsetHeight; startCX = e.clientX; startCY = e.clientY; dialog.style.transition = 'none'; // 禁用过渡避免闪烁 }); document.addEventListener('mousemove', (e) => { if (!isResizing) return; const dx = e.clientX - startCX; const dy = e.clientY - startCY; const newWidth = Math.max(200, startWidth + dx); // 最小宽度限制 const newHeight = Math.max(150, startHeight - dy); // 最小高度限制 dialog.style.width = `${newWidth}px`; dialog.style.height = `${newHeight}px`; // 内容等比例缩放 const scaleX = newWidth / startWidth; const scaleY = newHeight / startHeight; const scale = Math.min(scaleX, scaleY); // 取较小值保持比例 // const content2 = el.querySelector('.el-dialog__body'); // content2.style.transform = `scale(${scale})`; // content2.style.transformOrigin = 'top left'; }); document.addEventListener('mouseup', () => { if (isResizing) { isResizing = false; dialog.style.transition = ''; // 恢复过渡效果 } }); } }); 这段代码自定义的放大缩小dialog框的功能第二次点击页面出现拖影
最新发布
12-05
这个是background的设置JSON文件,请修复大小为3840*2160的照片显示大小不正确的问题(只显示了一部分): { "debug.onTaskErrors": "showErrors", "chat.tools.terminal.autoApprove": { ".\\temp.exe": true, "dir": true, "cl.exe /EHsc /Fe:temp.exe temp.cpp": { "approve": true, "matchCommandLine": true }, "Get-ChildItem -Path \"C:\\Program Files (x86)\\Microsoft Visual Studio\" -Filter vcvarsall.bat -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName": { "approve": true, "matchCommandLine": true }, "Get-ChildItem -Path 'C:\\Program Files','C:\\Program Files (x86)' -Filter vcvarsall.bat -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName": { "approve": true, "matchCommandLine": true }, "cl.exe": true, "temp.exe\"": true, "temp.exe'": true, "C:\\mingw64\\bin\\g++.exe": true }, "security.workspace.trust.untrustedFiles": "open", "editor.fontSize": 25, "c-cpp-compile-run.cpp-compiler": "C:\\mingw64\\bin\\g++.exe", "c-cpp-compile-run.c-compiler": "C:\\mingw64\\bin\\gcc.exe", "baidu.comate.license": "1fb793ce-6fa5-4cfa-a018-7b05e29d1499", "baidu.comate.username": "为啥啥名都没了", "workbench.colorTheme": "Default Light+", "baidu.comate.enableCodelens": { "enableInlineUnitTest": true, "enableInlineExplain": true, "enableInlineDocstring": true, "enableInlineSplit": true, "enableInlineComment": true, "enableInlineOptimize": true, "enableInlineLog": true }, "baidu.comate.completionLength": "auto", "C_Cpp.default.compilerPath": "", "github.copilot.nextEditSuggestions.enabled": true, "backgroundCover.imagePath": "c:\\Users\\oosev\\Pictures\\圣园末花锁屏壁纸.jpg", "backgroundCover.opacity": 0, "backgroundCover.randomImageFolder": "c:\\Users\\oosev\\Pictures", "background.usedefault": false, "background.enabled": true, "background.customImages": ["file:///c:/Users/oosev/Pictures/圣园末花锁屏壁纸.jpg"], "background.style": { "content": "''", "pointerEvents": "none", "position": "cover", "zIndex": "99999", "width": "100%", "height": "100%", "backgroundPosition": "100% 100%", "backgroundRepeat": "no-repeat", "opacity": 0.7 } }
11-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值