简体   繁体   English

当窗口大小改变时,如何更改按钮的功能?

[英]How do I change the function of a button when the window size changes?

So I have two images, one for higher than 1440px wide resolution and one for lower. 所以我有两个图像,一个高于1440px宽分辨率,一个用于低分辨率。 I have a button to toggle on this image, unfortunately I can't figure how to get it to work so it toggles on the lores image below 1440px or the hires image above 1440px. 我有一个按钮来切换这个图像,不幸的是我无法想象如何让它工作,所以它切换到1440px以下的lores图像或1440px以上的雇佣图像。 Plus now my toggle_off function doesn't work with my attempted js to get it to work. 此外,现在我的toggle_off函数不适用于我尝试过的js以使其工作。

CSS: CSS:

.p_works
{
    width:100%;
}
@media (min-width: 1440px){
        #art1
        {
            display:inline;
        }
        #art1_lores
        {
            display:none;
        }
        #art2
        {
            display:none;
        }
        #art2_lores
        {   
            display:none;
        }
        }
@media (max-width: 1440px){
        #art1
        {
            display:none;
        }
        #art1_lores
        {
            display:inline;
        }
        #art2
        {
            display:none;
        }
        #art2_lores
        {   
            display:none;
        }
        }

I don't really know javascript so this is an attempt I'm sure is wrong, also I don't even know if I need to load a script for mathcmedia. 我真的不知道javascript所以这是一次尝试我肯定是错的,我甚至不知道我是否需要加载mathcmedia的脚本。

Javascript: 使用Javascript:

<script type="text/javascript">
    function toggle_on(id) {
        var hires = document.getElementById(id);
        var lores = document.getElementById(id) +'_lores';
        if (window.matchmedia("(min-width: 1440px)").matches) {
            hires.style.display = 'inline';
        }
        else {
            lores.style.display = 'inline';
        }
    }
    function toggle_off(id) {
        var e = document.getElementById(id);
        if(e.style.display = 'inline') 
        e.style.display = 'none';
    }
</script>

HTML: HTML:

<a href="#" onclick="toggle_on('art1'); toggle_off('art2'); toggle_off('art2_lores')">
            <img class="icons" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/icons/typewriter.jpg" alt="typewriter"/>
</a>
<a href="#" onclick="toggle_on('art2'); toggle_off('art1'); toggle_off('art1_lores');">
            <img class="icons" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/icons/wasting-water.jpg" alt="wasting water"/>
</a>
<img class="p_works" id="art1" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/Typewriter.jpg" alt="typewriter"/>
<img class="p_works" id="art1_lores" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/lores/Typewriter.jpg" alt="typewriter"/>
<img class="p_works" id="art2" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/wasting-water.jpg" alt="wasting water"/>
<img class="p_works" id="art2_lores" src="https://dl.dropboxusercontent.com/u/70582811/Website/Redesign/works/artwork/wasting-water.jpg" alt="wasting water"/>

you can see where I am here: http://www.dillonbrannick.com/redesign 你可以看到我在这里: http//www.dillonbrannick.com/redesign

The buttons are on the right; 按钮在右边; the icons, only the first two should work as of my code, but they do not. 图标,只有前两个应该在我的代码中起作用,但它们不起作用。

CodePen CodePen

Perhaps this way : 也许这样:

window.onresize = function(event) {
      var w=window.outerWidth;
      var h=window.outerHeight;


      // Size specific code here
}
function toggle_on_hires(id) {
        var e = document.getElementById(id);
        var w = window.innerWidth;
        if(w > 1440){
            e.style.display = 'inline';
        }
        else{
            e.style.display = 'none';
        }
    }
    function toggle_on_lores(id) {
        var e = document.getElementById(id);
        var w = window.innerWidth;
        if(w <= 1440){
            e.style.display = 'inline';
        }
        else{
            e.style.display = 'none';
        }
    }

I then called #art1 with toggle_on_hires and #art1_lores with toggle_on_lores. 然后,我使用toggle_on_hires调用#art1,使用toggle_on_lores调用#art1_lores。 So now it toggle the lower res image when the window resolution is below 1440px wide and higher resolution image when it's above. 所以现在它在窗口分辨率低于1440px宽时切换低分辨率图像,在高于1440px宽度时切换高分辨率图像。

暂无
暂无

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

相关问题 如何调整 IMG 元素的大小,使其在窗口大小更改时按比例位于窗口上的同一位置? - How do I resize an IMG element so that it is proportionately in the same place on the window when the window size changes? 如何更改窗口大小? - how do I change window size? 当窗口大小改变时如何改变元素的位置? - How to change a position of element when a window size changes? 如何更改 javascript 中的按钮大小 - How do I change a button size in javascript Bootstrap网格更改布局时,如何更改选择元素的大小? - How do I change the size of a select element when Bootstrap grid changes layout? 窗口大小更改时,是否可以自动更改组元素的大小? - Is it possible to auto change the size of group elements when the window size changes? 如何更改按钮的颜色,以便在使用jQuery单击按钮时将其更改为另一种颜色? - How do I change the color of a button so that it changes to another color when its clicked using jQuery? 我如何创建一个 javascript function 在按下表单提交按钮时更改 html - how do I create a javascript function that changes the html when a form submit button is pushed in django 当 window 变量在反应中发生变化时,如何更新 state - How do I update state when a window variable changes in react 当按钮和函数位于单独的文件中时,如何在react.js中创建弹出窗口? - How do I create a popup window in react.js when the button and function are in separate file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM