asp.net 按钮行为的实现(例如,鼠标左键按住它会导致其文本稍微移动)

发布于 2024-11-09 07:26:07 字数 818 浏览 0 评论 0 原文

我在 asp.net 中有一个按钮,如下所示:

<asp:Button ID="btnSaveInrpvEdit" CssClass="btnSaveInrpvEdit" runat="server" 
    Text="" ValidationGroup="B" onclick="btnSaveInrpvEdit_Click" />

它是 css:

.btnSaveInrpvEdit
{
    background: url(/Images/Admin/btnSave.png) no-repeat left top;
    width: 155px;
    height: 63px;
    border: 0px;
    outline: none;
}
.btnSaveInrpvEdit:hover,.btnSaveInrpvEdit:active
{
    background: url(/Images/Admin/btnSave_Hover.png) no-repeat left 1px;
    cursor: pointer;
    outline: none;
}

所以它悬停的一切都很好...
此时我想实现关于鼠标左键HOLDINGRELEASING的按钮行为!
如果您注意 ASP.NET 中的常规按钮,您会看到单击鼠标左键并按住该按钮时,因此看起来它的文本已移动了一点...
另外,当我们释放鼠标左键时,该按钮将返回正常模式!

我如何使用 css 和 javascript 完成这项工作?

提前致谢

i have a button in asp.net like below :

<asp:Button ID="btnSaveInrpvEdit" CssClass="btnSaveInrpvEdit" runat="server" 
    Text="" ValidationGroup="B" onclick="btnSaveInrpvEdit_Click" />

and it's css :

.btnSaveInrpvEdit
{
    background: url(/Images/Admin/btnSave.png) no-repeat left top;
    width: 155px;
    height: 63px;
    border: 0px;
    outline: none;
}
.btnSaveInrpvEdit:hover,.btnSaveInrpvEdit:active
{
    background: url(/Images/Admin/btnSave_Hover.png) no-repeat left 1px;
    cursor: pointer;
    outline: none;
}

so every thing is ok about it's hovering...
at this time i want to implement this button behaviour about left mouse HOLDING and RELEASING on it!
if you attention to regular buttons in asp.net u will see when left mouse is clicked and holded on that button , so it seems it's text has been moved a bit...
also when we release left mouse, that button goes back to normal mode!

how can i do this job with css and javascript?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-11-16 07:26:07

首先,请注意按下按钮中的文本移动(以及如果文本移动)与 (或 input直接渲染的 元素。按钮样式和视觉行为一小部分取决于浏览器,而很大程度上取决于客户端使用的 Windows 版本和主题。对于实际上不移动文本的主题,手动移动背景图像可能看起来很奇怪。

也就是说,您可以使用 JavaScript(尤其是 jQuery)相对容易地模拟行为。

像这样的东西应该可以工作:

CSS(我在这里使用普通样式,因为按下通常会删除悬停样式)

.btnSaveInrpvEdit.mousedown
{
    background: url(/Images/Admin/btnSave.png) no-repeat 1px 1px;
}

JS + jQuery

$("#btnSaveInrpvEdit").mousedown(function () { $(this).addClass("mousedown"); });
$("#btnSaveInrpvEdit").mouseup(function () { $(this).removeClass("mousedown"); });

您可能还必须处理用户按下按钮,然后按住并将光标拖离按钮,这会导致按钮可以在视觉上取消按下,但如果将鼠标移回到按钮上,也会返回到按下状态。这有点棘手,但我确信您已经掌握了要点并且可以解决问题;)

作为至少防止按钮背景永远保持偏移的快速修复,您可以添加

$("#btnSaveInrpvEdit").mouseleave(function () { $(this).removeClass("mousedown"); });

First, note that the text moving (and if the text moves) in the pressed button has nothing to do with the <asp:Button (or the input element it renders) directly. The button styles and visual behaviour depend to a small part on the browser and to a large part on the windows version and theme the client uses. Manually moving the background image could look very weird on themes that don't actually move the text.

That said, you can emulate the behaviour relatively easy with JavaScript respectively jQuery in particular.

Something like this should work:

CSS (I used normal style here because pressing usually removes the hover style)

.btnSaveInrpvEdit.mousedown
{
    background: url(/Images/Admin/btnSave.png) no-repeat 1px 1px;
}

JS + jQuery

$("#btnSaveInrpvEdit").mousedown(function () { $(this).addClass("mousedown"); });
$("#btnSaveInrpvEdit").mouseup(function () { $(this).removeClass("mousedown"); });

You probably also have to handle the user pressing the button but then holding and dragging the cursor away from the button, which causes the button to un-press visually but will also return to the pressed state if you move the mouse back over the button. This is a bit more tricky but I'm sure you got the gist of it and can work something out ;)

As a quick fix to at least prevent the button background staying offset forever you can add

$("#btnSaveInrpvEdit").mouseleave(function () { $(this).removeClass("mousedown"); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文