简体   繁体   English

使用jQuery,如何在调整大小后使元素保持焦点?

[英]With jquery how do you keep an element in focus after resize?

I'm trying to copy mashable.com's second menu in 640px & 320px. 我正在尝试以640px和320px复制mashable.com的第二个菜单。 I'm having trouble when resize is called as the menu item element becomes no longer in focus anymore when rotating from 640px to 320px. 当调用resize时我遇到麻烦,因为当菜单项元素从640px旋转到320px时不再成为焦点。 Can you help me please? 你能帮我吗?

Here is current code, I've taken out the resize function for the moment until I can get it sorted. 这是当前的代码,我暂时取出了resize函数,直到可以对其进行排序。

<script>
        jQuery(document).ready(function(){
            var sliderWidth = jQuery('.mosts').css('width');
            swidthnum = sliderWidth.replace("px", "");

            if (swidthnum >= 640) {
                jQuery('#slider').css('width', '150%');
                jQuery(".buttongroup1>.date-nav-1").click(function(){
                    jQuery("#slider").animate({left: "0px"});
                }); 
                jQuery(".buttongroup1>.date-nav-3").click(function(){
                    jQuery("#slider").animate({left: "-50%"});
                }); 
            }
            else if (swidthnum >= 320) {
                jQuery('#slider').css('width', '300%');
                jQuery(".buttongroup2>.date-nav-1").click(function(){
                    jQuery("#slider").animate({left: "0px"});
                }); 
                jQuery(".buttongroup2>.date-nav-2").click(function(){
                    jQuery("#slider").animate({left: "-100%"});
                });
                jQuery(".buttongroup2>.date-nav-3").click(function(){
                    jQuery("#slider").animate({left: "-200%"});
                });
            }

            jQuery(window).resize(function(){location.reload();});
        });             
</script>
<div class="buttongroup1">
        <button class="date-nav-1">WHAT'S TRENDING</button>
        <button class="date-nav-3">READ MORE</button>
    </div>
    <div class="buttongroup2">
        <button class="date-nav-1">TRENDING</button>
        <button class="date-nav-2">NEWEST</button>
        <button class="date-nav-3">READ MORE</button>
    </div>

I been trying for a week to get 640px to 320px to look good. 我尝试了一个星期,使640像素到320像素看起来不错。 I think pseudo code goes like this: when the page first loads have it focus .date-nav-1, when rotating keep the focus on that current element whether it be .date-nav-1 or .date-nav-3 every-time I add a resize function it looses focus :( 我认为伪代码是这样的:页面首次加载时将其焦点放在.date-nav-1上,而旋转时则将焦点始终放在当前元素上,无论它是.date-nav-1还是.date-nav-3 every-每当我添加一个调整大小的功能,它失去焦点:(

And help to get me going would be so awesome right now :) 现在帮助我前进的帮助真是太棒了:)

Maybe: 也许:

$(window).resize(function(){$("#my-element").focus();});

PS: why reload the page? PS:为什么要重新加载页面? I don't recommend this. 我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM