Javascript/jquery window.location.hash 移动到屏幕中央

发布于 2024-12-26 11:29:06 字数 1381 浏览 2 评论 0原文

我有一个表单,我正在尝试使用 jquery 运行一些基本验证。我有空锚点,其名称与输入的名称相对应。当发现第一个字段为空时,它会存储输入的名称,当验证脚本完成时,它会调用 window.location.hash = name + "hash",这是空锚点的名称。问题是,它正在移动窗口,以便锚点显示在屏幕的最顶部,我希望它移动窗口,以便锚点显示在屏幕的中间。这可能吗?这就是我所拥有的。

function formValidate()
{
var $retValue = true;
var $inputs = $('.req');
var $winLoc = '';
$inputs.each( function()
{
    if($(this).val() === "" && this.name != "salesmanName2")
    {
        var $helper = this.name + "Help";
        var $pointer = this.name + "Pointer";
        event.preventDefault();
        ($(this).addClass('reqMissing'));
        showHelper($helper, $pointer);
        if($winLoc == '' && this.name != "salesmanName2")
        {
            $winLoc = this.name + "Anchor"; 
        }
        $retValue = false;
    }
    else
    {
        ($(this).removeClass('reqMissing'));    
    }
});
if($winLoc !== '')
{
    window.location.hash=$winLoc;
}
return $retValue;   
}

这是表格的片段。

<li>
    <a name="controlNumberAnchor"></a>
    <label for='controlNumber'>Control Number: </label>
    <input class='req' type='text' name='controlNumber' size='10' /><em>*</em>
    <div id='controlNumberPointer' class='pointer'></div>
    <div id='controlNumberHelp' class='helpBox'>Control number required</div>
</li>

I have a form that I'm trying to run some basic validation on using jquery. I have empty anchors with names that correspond to the names of the inputs. When the first field is found empty, it stores the name of the input and when the validations script is done, it calls window.location.hash = name + "hash", which is the name of the empty anchor. The problem is, it's moving the window so that the anchor is showing at the very top of the screen, and I'd like it to move the windows so the anchor shows in the middle of the screen. Is this possible? Here's what I have.

function formValidate()
{
var $retValue = true;
var $inputs = $('.req');
var $winLoc = '';
$inputs.each( function()
{
    if($(this).val() === "" && this.name != "salesmanName2")
    {
        var $helper = this.name + "Help";
        var $pointer = this.name + "Pointer";
        event.preventDefault();
        ($(this).addClass('reqMissing'));
        showHelper($helper, $pointer);
        if($winLoc == '' && this.name != "salesmanName2")
        {
            $winLoc = this.name + "Anchor"; 
        }
        $retValue = false;
    }
    else
    {
        ($(this).removeClass('reqMissing'));    
    }
});
if($winLoc !== '')
{
    window.location.hash=$winLoc;
}
return $retValue;   
}

And here's a snippet of the form.

<li>
    <a name="controlNumberAnchor"></a>
    <label for='controlNumber'>Control Number: </label>
    <input class='req' type='text' name='controlNumber' size='10' /><em>*</em>
    <div id='controlNumberPointer' class='pointer'></div>
    <div id='controlNumberHelp' class='helpBox'>Control number required</div>
</li>

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

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

发布评论

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

评论(3

月亮是我掰弯的 2025-01-02 11:29:06

仅使用哈希是不可能的。您必须使用基于哈希计算的值来设置文档的 scrollTop 。试试这个。

function formValidate()
{
var $retValue = true;
var $inputs = $('.req');
var $winLoc = '';
$inputs.each( function()
{
    if($(this).val() === "" && this.name != "salesmanName2")
    {
        var $helper = this.name + "Help";
        var $pointer = this.name + "Pointer";
        event.preventDefault();
        ($(this).addClass('reqMissing'));
        showHelper($helper, $pointer);
        if($winLoc == '' && this.name != "salesmanName2")
        {
            $winLoc = this.name + "Anchor"; 
        }
        $retValue = false;
    }
    else
    {
        ($(this).removeClass('reqMissing'));    
    }
});
if($winLoc !== '')
{
    //window.location.hash=$winLoc;
    var top = $('[name=' + $winLoc + ']').offset().top;
    $(document).scrollTop(top - ($(window).height()/2)); 
}
return $retValue;   
}

It is not possible by just using hash. You will have to set the scrollTop of the document by with the calculcated value based on the hash. Try this.

function formValidate()
{
var $retValue = true;
var $inputs = $('.req');
var $winLoc = '';
$inputs.each( function()
{
    if($(this).val() === "" && this.name != "salesmanName2")
    {
        var $helper = this.name + "Help";
        var $pointer = this.name + "Pointer";
        event.preventDefault();
        ($(this).addClass('reqMissing'));
        showHelper($helper, $pointer);
        if($winLoc == '' && this.name != "salesmanName2")
        {
            $winLoc = this.name + "Anchor"; 
        }
        $retValue = false;
    }
    else
    {
        ($(this).removeClass('reqMissing'));    
    }
});
if($winLoc !== '')
{
    //window.location.hash=$winLoc;
    var top = $('[name=' + $winLoc + ']').offset().top;
    $(document).scrollTop(top - ($(window).height()/2)); 
}
return $retValue;   
}
小耗子 2025-01-02 11:29:06

恐怕锚定到屏幕顶部是标准的 .hash 行为。 AFAIK,没有办法覆盖它。

您可以通过将文档中的锚点放置在比您想要居中的内容高半个屏幕的位置来实现您想要的行为。很棘手,但对于已知尺寸特征的内容是可能的。

I'm afraid that anchor-to-top-of-screen is standard .hash behaviour. AFAIK, there's no way to override it.

You may be able to achieve the behaviour you want by putting your anchors one half-screen higher in the document than the content you want to be centred. Tricky, but possible with content of known dimensional characteristics.

苏璃陌 2025-01-02 11:29:06

您还可以尝试使用 CSS 解决此问题,即使用负边距+填充。它不会完全居中,但会从顶部移动锚点。

a[name] {
  padding-top: 150px;
  margin-top: -150px;
  display: inline-block; 
}

a[name]:hover {
  text-decoration:none;
}

You can also try solving this with CSS, by using a negative margin+padding. It won't be exactly center but it will move anchors from the top.

a[name] {
  padding-top: 150px;
  margin-top: -150px;
  display: inline-block; 
}

a[name]:hover {
  text-decoration:none;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文