简体   繁体   English

移动网站中带有图片和文字的按钮的最佳做法

[英]Best practice for a button with image and text in mobile website

Refer to the screenshot below, I have created a div to wrap another two div of image and text, then I use CSS position: absolute to make these div merge together. 参考下面的屏幕截图,我创建了一个div来包装图像和文本的另外两个div ,然后使用CSS position: absolute使这些div合并在一起。

However, I found that the button is not sensitive while testing on mobile devices, sometime I need to touch the button few time to take effect. 但是,我发现在移动设备上进行测试时该按钮不敏感,有时我需要触摸几次按钮才能生效。

So, is there something wrong for my code and what is the best practice to create a button with image and text? 那么,我的代码有什么问题吗?创建带有图像和文本的按钮的最佳实践是什么?

Thanks 谢谢

在此处输入图片说明

<div class="r">
    <div class="a">
        <div class="i"><img src="store_btn_on.png" /></div>
        <div class="t">Shatin</div>
    </div>
    <div class="b">
        <div class="i"><img src="store_btn_off.png" /></div>
        <div class="t">Causeway Bay</div>
    </div>
    <div class="c">
        <div class="i"><img src="store_btn_off.png" /></div>
        <div class="t">Kowloon Bay</div>                            
    </div>
</div>

Update for the part of javascript 更新部分javascript

addButtonListener($("#store > .r > .a"), function(event, target){
        $("#some_content").css("display", "none");
        $("#other_content").css("display", "block");
        $(".r > .b > .a > img").attr("src" , "store_btn_on.png");
        $(".r > .b > .b > img, .r > .b > .c > img").attr("src" , "store_btn_off.png");
});

function addButtonListener(targets, job){
    if ("ontouchstart" in document.documentElement){
        targets.each(function(){
            $(this)[0].addEventListener('touchstart', function(event){
                event.preventDefault();
                $(this).attr({ "x": event.targetTouches[0].clientX, "diffX": 0, "y": event.targetTouches[0].clientY, "diffY": 0 });
                $(this).addClass("on");
            }, false);
            $(this)[0].addEventListener('touchmove', function(event){
                $(this).attr({
                    "diffX": Math.abs(event.targetTouches[0].clientX - $(this).attr("x")),
                    "diffY": Math.abs(event.targetTouches[0].clientY - $(this).attr("y"))
                });
            }, false);
            $(this)[0].addEventListener("touchend", function(event){
                $(this).removeClass("on");
                if ($(this).attr("diffX") < 5 && $(this).attr("diffY") < 5){ $(job(event, $(this))); }
            }, false);
        });
    }
    else {
        targets.each(function(){
            $(this).mousedown(function(event){ event.preventDefault(); $(this).addClass("on"); });
            $(this).mouseup(function(event){ event.preventDefault(); if ($(this).hasClass("on")){ $(job(event, $(this))); } $(this).removeClass("on"); });
            $(this).hover(function(event){ $(this).removeClass("on"); });
        });
    }
}

I am not sure about the sensitivity of your button on mobile devices (you haven't shown any of your code for handling click events), but I think it is better to write your HTML like this: 我不确定您的按钮在移动设备上的敏感性(您尚未显示用于处理点击事件的任何代码),但是我认为这样编写HTML更好:

<div class="r">
    <div class="button on">
        <span>Shatin</span>
    </div>
    <div class="button">
        <span>Bay</span>
    </div>
    <div class="button">
        <span>Bay</span>                           
    </div>
</div>

Then use CSS in your stylesheet: 然后在样式表中使用CSS:

.button {
    background: url(http://domain.com/images/store_btn_off.png) no-repeat 0 0;
    /* Additional button styles */
}

.button.on {
    background: url(http://domain.com/images/store_btn_on.png) no-repeat 0 0;
}

.button span {
    color: #FFF;
    margin: auto;
}

This makes it easy to dynamically turn a button on or off just by adding or removing the on class. 这使得仅通过添加或删除on类就可以轻松动态地打开或关闭按钮。

Although not necessary, you may also be interested in looking at CSS3 gradients to create simple gradient background images like that, and then degrade nicely to an image in browsers without any gradient support. 尽管不是必需的,但您可能也有兴趣查看CSS3渐变以创建类似的渐变背景图像,然后在没有任何渐变支持的浏览器中很好地将其降级​​为图像。

The class names "r" and "b" are not very descriptive. 类名“ r”和“ b”不是很具描述性。 Unless some HTML/CSS minifier put those there and you have proper names in your development code, I would consider giving your classes more descriptive names as well. 除非有一些HTML / CSS缩小器将它们放在那里,并且您在开发代码中使用适当的名称,否则我将考虑为您的类提供更多的描述性名称。

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

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